datatype. For example, Ruby would not
allow the following:
a = " 40"
b = a + 2
In this example, Ruby will not implicitly cast the number 2, which has a
Fixnum type, to a string.
The Challenge of Compiling
Dynamically Typed Languages
Consider the following dynamically
typed method (of a hypothetical programming language), addtwo, which
adds any two numbers (that can be of
any numeric type) and returns the sum:
def addtwo(a, b)
a + b;
end
Suppose your organization is implementing a compiler and runtime system for the programming language in
which the method addtwo is written.
In a strongly typed language, whether
typed statically or dynamically, the
behavior of + (the addition operator)
depends on the types of
the operands.
A compiler for a statical-
ly typed language chooses
which implementation of
+ is appropriate based on
the static types of a and b.
For example, a Java com-
piler implements + with
the iadd JVM instruction if
a and b are of type int. The
addition operator will be compiled to
a method call because the JVM’s iadd
instruction requires the operand types
to be statically known.
invokedynamic InvokeDynamic
REF_invokeStatic:
Example.mybsm:
"(Ljava/lang/invoke/MethodHandles/Lookup;
Ljava/lang/String;
Ljava/lang/invoke/MethodType;)
Ljava/lang/invoke/CallSite;":
+:
"(Ljava/lang/Integer;
Ljava/lang/Integer;)
Ljava/lang/Integer;";
COMMUNITY
JAVA IN ACTION
See listing as text
such as invokevirtual, in which linkage
behavior specific to Java classes and
interfaces is hardwired by the JVM.
In the previous addtwo example, the
invokedynamic call site is +. An
invokedynamic call site is linked to a method
by means of a bootstrap method, which
is a method specified by the compiler
for the dynamically typed language that
is called once by the JVM to link the call
site. The object returned from the bootstrap method permanently determines
the call site’s behavior.
Listing 1 shows an example of an
invokedynamic instruction. Note that
this example uses the syntax of the
ASM Java bytecode manipulation and
analysis framework, and line breaks
have been added for clarity.
In this example, the runtime system
links the dynamic call site specified by
the invokedynamic instruction (which is
+, the addition operator) to the method
IntegerOps.adder. (The IntegerOps class
belongs to the library that accompa-
nies the dynamic language’s runtime
system your organization is implement-
ing.) It does this by using the bootstrap
method
Example.mybsm, which your
organization is responsible for writing.
ABOUT US
invokedynamic can
simplify and improve
the implementation
of compilers and
runtime systems.
The invokedynamic
Instruction
Java SE 7 introduces the
invokedynamic instruction,
which enables the runtime
system to customize the
linkage between a call site
and a method implemen-
tation. This contrasts with
other JVM instructions,
LEARN MORE
•;Java Virtual Machine Support for
Non-Java Languages
•;java.lang.invoke Package
•;The Da Vinci Machine Project
•;John Rose’s blog at oracle.com (John
Rose is the project lead for the Da Vinci
Machine Project and the specification
lead for the invokedynamic instruction.)
blog