Skip to content

Method references to user functions#18828

Merged
rmuir merged 13 commits intoelastic:masterfrom
rmuir:ownReferences
Jun 12, 2016
Merged

Method references to user functions#18828
rmuir merged 13 commits intoelastic:masterfrom
rmuir:ownReferences

Conversation

@rmuir
Copy link
Copy Markdown
Contributor

@rmuir rmuir commented Jun 11, 2016

This allows stuff like:

int mycompare(int i, int j) { j - i } 
...
list.sort(this::mycompare)

It is like a manual transmission lambda. The main idea here is allowing pointers to functions in our own class (whether user explicitly wrote them or we wrote them on their behalf).

So a few cleanups were needed:

  1. allow creating FunctionRef to these (lots of refactoring here)
  2. allow dynamic code (Def) to access these. We need a runtime whitelist, and we'd like to keep all methods private, avoid reflection/security issues/etc. We add a whitelist to the class itself, a pointer to each function like this:
private static final MethodHandle handle$mycompare$2;

These are initialized in <clinit> with ldc (they are simply constants).
We can lookup from this by name and arity exactly, and we have what we need.
3. general cleanup of functions in our ASM code (thanks @uschindler).

@uschindler
Copy link
Copy Markdown
Contributor

I also cleaned up the MethodWriter stuff to no longer nest. The code was ununderstandable! :-)

@uschindler
Copy link
Copy Markdown
Contributor

uschindler commented Jun 11, 2016

+1 to merge current state to master! Next is real lambdas!

@uschindler uschindler mentioned this pull request Jun 12, 2016
18 tasks
@jdconrad
Copy link
Copy Markdown
Contributor

Thanks @rmuir and @uschindler! This all looks great to me.

@rmuir rmuir merged commit 7c8eb18 into elastic:master Jun 12, 2016
@clintongormley clintongormley changed the title painless: method references to user functions Method references to user functions Jun 13, 2016
@clintongormley clintongormley added :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache and removed :Plugin Lang Painless labels Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement v5.0.0-alpha4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants