Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created June 5, 2011 18:16
Show Gist options
  • Save rednaxelafx/1009240 to your computer and use it in GitHub Desktop.
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
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