Created
June 5, 2011 18:16
-
-
Save rednaxelafx/1009240 to your computer and use it in GitHub Desktop.
A simple invokedynamic example, invoking a virtual method (Object.toString), with BiteScript. Code modified from BiteScript's example/invokedynamic.bs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.lang.invoke.MethodHandle | |
| import java.lang.invoke.MethodType | |
| import java.lang.invoke.CallSite | |
| import java.lang.invoke.ConstantCallSite | |
| import java.lang.invoke.MethodHandles::Lookup | |
| JClass = java.lang.Class | |
| JInteger = java.lang.Integer | |
| # Our main method, which does one invokedynamic | |
| main do | |
| # handle for our bootstrap, which binds invokedynamic to a CallSite | |
| bootstrap = mh_invokestatic this, :bootstrap, CallSite, Lookup, string, MethodType | |
| push_int 12345 | |
| invokestatic JInteger, :valueOf, [JInteger, int] | |
| invokedynamic :_, [string, object], bootstrap | |
| invokestatic this, :print, [void, string] | |
| returnvoid | |
| end | |
| # The method we want to invoke, prints given string | |
| public_static_method :print, [], void, string do | |
| aload 0 | |
| aprintln | |
| returnvoid | |
| end | |
| # The bootstrap method, which binds our dynamic call | |
| public_static_method :bootstrap, [], CallSite, Lookup, string, MethodType do | |
| # Constant since we bind just once directly | |
| new ConstantCallSite | |
| dup | |
| # Locate the method indicated by name + type on current class | |
| aload 0 # Lookup | |
| ldc object # this class | |
| ldc 'toString' # String | |
| ldc string # MethodType | |
| invokestatic MethodType, :methodType, [MethodType, JClass] | |
| invokevirtual Lookup, :findVirtual, [MethodHandle, JClass, string, MethodType] | |
| # adapt the method handle to appropriate type | |
| aload 2 | |
| invokevirtual MethodHandle, :asType, [MethodHandle, MethodType] | |
| # finish constructing call site and return | |
| invokespecial ConstantCallSite, '<init>', [void, MethodHandle] | |
| areturn | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment