@@ -32,6 +32,9 @@ import com.github.jonathanxd.kores.builder.self
32
32
import com.github.jonathanxd.kores.common.DynamicMethodSpec
33
33
import com.github.jonathanxd.kores.common.MethodInvokeSpec
34
34
import com.github.jonathanxd.kores.common.MethodTypeSpec
35
+ import com.github.jonathanxd.kores.common.FieldAccessHandleSpec
36
+ import com.github.jonathanxd.kores.common.MethodInvokeHandleSpec
37
+ import com.github.jonathanxd.kores.common.DynamicConstantSpec
35
38
import com.github.jonathanxd.kores.serialization.BootstrapArgListSerializer
36
39
import com.github.jonathanxd.kores.serialization.BootstrapArgSerializer
37
40
import com.github.jonathanxd.kores.type.KoresType
@@ -41,10 +44,35 @@ import java.lang.reflect.Type
41
44
import java.util.function.Supplier
42
45
43
46
/* *
44
- * A dynamic invocation of a method.
47
+ * Represents a dynamic invocation of a method. The mechanism of a dynamic invocation is very simple, when JVM encounters an
48
+ * `invokedynamic` instruction, it calls the [bootstrap] method (which is a static method defined in [bootstrapLocalization])
49
+ * to resolve the [target method][dynamicMethod]. Once resolved, the [target method][dynamicMethod] keeps linked to the
50
+ * call-site and there is no way to revert this. Subsequent calls are routed to the resolved method without invoking the bootstrap.
51
+ * This allows optimizations to take in place (like the JIT compiler optimizations).
45
52
*
46
- * Note: this class does not extends [MethodInvocation] because it is not
47
- * a normal invocation.
53
+ * The [dynamicMethod] corresponds to the dynamic method that need to be resolved, it contains important information about
54
+ * the method that need to be resolved. The [DynamicMethodSpec.name] and [DynamicMethodSpec.typeSpec] (which are provided as
55
+ * [String] and [MethodType], respectively) are available to the bootstrap method, but [DynamicMethodSpec.arguments] is not,
56
+ * as specified in the documentation of the property.
57
+ *
58
+ * Additional information can be provided through [bootstrapArgs] and are passed as the last argument of the
59
+ * [bootstrap method][bootstrap]. The last parameter of [bootstrap method][bootstrap] can be a vararg, an [Array] of [Any],
60
+ * or various parameters of the types accepted by bootstrap methods as specified in JVM Specification and in the
61
+ * [java.lang.invoke] package documentation. The known allowed parameter types are:
62
+ * - Literal Constants
63
+ * - [Int], [Float], [Long], [Double], [String] (it includes [dynamic constant][DynamicConstantSpec]).
64
+ * - Type Constants
65
+ * - [Type]/[Class]
66
+ * - Field and Method specification
67
+ * - [MethodHandle]
68
+ * - Descriptors
69
+ * - [MethodType]/[TypeDescriptor] (since Java 12)
70
+ *
71
+ *
72
+ * ### Relevant documents
73
+ * - [Java Virtual Machine Support for Non-Java Languages](https://docs.oracle.com/javase/8/docs/technotes/guides/vm/multiple-language-support.html)
74
+ * - [Understanding Java method invocation with invokedynamic](https://blogs.oracle.com/javamagazine/understanding-java-method-invocation-with-invokedynamic)
75
+ * - [Chapter 6. The Java Virtual Machine Instruction Set#invokedynamic](https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-6.html#jvms-6.5.invokedynamic)
48
76
*/
49
77
interface InvokeDynamicBase : TypedInstruction {
50
78
@@ -60,13 +88,39 @@ interface InvokeDynamicBase : TypedInstruction {
60
88
val bootstrap: MethodInvokeSpec
61
89
62
90
/* *
63
- * Specification of dynamic method.
91
+ * The [Type] that declares the [bootstrap method][bootstrap].
92
+ *
93
+ * This is the same value provided via [MethodTypeSpec] to the [bootstrap].[methodTypeSpec][MethodInvokeSpec.methodTypeSpec].
94
+ */
95
+ val bootstrapLocalization: Type
96
+ get() = this .bootstrap.methodTypeSpec.localization
97
+
98
+ /* *
99
+ * Specification of the method to invoke dynamically. This information is used by the [bootstrap method][bootstrap]
100
+ * to resolve the target invocation method.
101
+ *
102
+ * Arguments provided to [DynamicMethodSpec] are passed to the method resolved by the [bootstrap].
64
103
*/
65
104
val dynamicMethod: DynamicMethodSpec
66
105
67
106
/* *
68
- * Bootstrap method Arguments, must be an [String], [Int],
69
- * [Long], [Float], [Double], [KoresType] or [MethodInvokeSpec].
107
+ * Bootstrap method Arguments, must be one of the following types:
108
+ * - [String]
109
+ * - [Int],
110
+ * - [Long]
111
+ * - [Float]
112
+ * - [Double]
113
+ * - [KoresType]/[Type]
114
+ * - [MethodInvokeSpec] (normally translated into [MethodHandle] at runtime, by the JVM)
115
+ * - [FieldAccessHandleSpec] (normally translated into [MethodHandle] at runtime, by the JVM)
116
+ * - [MethodInvokeHandleSpec] (normally translated into [MethodHandle] at runtime, by the JVM)
117
+ * - [TypeSpec] (normally translated into [MethodType] at runtime, by the JVM)
118
+ * - [DynamicConstantSpec] (as specified in [JEP 309](https://openjdk.java.net/jeps/309), translated into a constant).
119
+ *
120
+ * This is the value provided to the bootstrap method which resolves the target method to invoke. Those values
121
+ * are stored in the **ConstantPool** and are not provided to the target method.
122
+ *
123
+ * Arguments that must be provided to the target method must be provided in the [dynamicMethod] specification.
70
124
*/
71
125
@Serializable(with = BootstrapArgListSerializer ::class )
72
126
val bootstrapArgs: List <Any >
0 commit comments