summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rwxr-xr-xtools/gen_class.py98
1 files changed, 90 insertions, 8 deletions
diff --git a/tools/gen_class.py b/tools/gen_class.py
index fb398e9..32f00c8 100755
--- a/tools/gen_class.py
+++ b/tools/gen_class.py
@@ -10,6 +10,10 @@ verbatim — exact super/interfaces, every field, every method (including
synthetic $default and erasure bridges) with its exact name + descriptor
and access flags — and gives each concrete method a trivial body that
logs the call via kotlin.proxy.KotlinProxyLog and returns a default value.
+Reference-return methods instead ask KotlinProxyLog.returnValue for their
+value, which dispatches to kotlin.proxy.KotlinProxyOverrides.invoke (per-method
+custom implementations, passed the full argument list) before falling back to
+a real no-arg instance of the declared return type.
The optional Signature attribute is OMITTED entirely, so there are no
generics in the emitted class files and therefore no generics clashes.
@@ -37,7 +41,12 @@ OBJ = "java/lang/Object"
CALL_DESC = "(Ljava/lang/String;Ljava/lang/String;)V"
CALL_ARG_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"
LOADED_DESC = "(Ljava/lang/String;)V"
-NEW_INST_DESC = "(Ljava/lang/String;)Ljava/lang/Object;"
+# Arg order MUST match KotlinProxyLog.returnValue: the Object[] args array
+# comes FIRST — the generated body builds it while the stack is shallowest
+# (peak 4 instead of 8 if it were pushed last).
+RETURN_VALUE_DESC = \
+ "([Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;" \
+ "Ljava/lang/String;)Ljava/lang/Object;"
# access flags
A_PUBLIC, A_PRIVATE, A_PROTECTED = 0x1, 0x2, 0x4
@@ -340,6 +349,73 @@ def first_param_ref_slot(desc, is_static):
return (0 if is_static else 1), True
+# valueOf boxes for primitive params (so Object[] position i is always param i)
+BOX = {
+ "Z": ("java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"),
+ "B": ("java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"),
+ "S": ("java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"),
+ "C": ("java/lang/Character", "valueOf", "(C)Ljava/lang/Character;"),
+ "I": ("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"),
+ "J": ("java/lang/Long", "valueOf", "(J)Ljava/lang/Long;"),
+ "F": ("java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"),
+ "D": ("java/lang/Double", "valueOf", "(D)Ljava/lang/Double;"),
+}
+# opcodes per the JVMS table (https://javaalmanac.io/bytecode/opcodes/),
+# verified byte-for-byte against javac output
+LOAD_WIDE = {"Z": 0x15, "B": 0x15, "S": 0x15, "C": 0x15, "I": 0x15, # iload
+ "J": 0x16, "F": 0x17, "D": 0x18} # lload fload dload
+LOAD_SHORT = {"Z": 0x1A, "B": 0x1A, "S": 0x1A, "C": 0x1A, "I": 0x1A, # iload_0
+ "J": 0x1E, "F": 0x22, "D": 0x26} # lload_0 fload_0 dload_0
+AASTORE, ANEWARRAY = 0x53, 0xBD
+
+
+def int_const(n):
+ """Bytes pushing int n (0..65535)."""
+ if 0 <= n <= 5:
+ return bytes([0x03 + n]) # iconst_0..iconst_5
+ if n <= 0xFF:
+ return bytes([0x10, n]) # bipush
+ return bytes([0x11]) + struct.pack(">h", n) # sipush
+
+
+def load_param(slot, t):
+ """Bytes loading the parameter (first descriptor char t) at slot."""
+ if t in "L[":
+ return (bytes([0x2A + slot]) if slot <= 3 # aload_0..3 / aload
+ else bytes([0x19, slot]))
+ wide = LOAD_WIDE[t]
+ return (bytes([LOAD_SHORT[t] + slot]) if slot <= 3 # iload_0 et al.
+ else bytes([wide, slot]))
+
+
+def param_layout(desc, is_static):
+ """[(type first char, slot)] per parameter, in order."""
+ params = desc[1:desc.rindex(")")]
+ out, i, s = [], 0, (0 if is_static else 1)
+ while i < len(params):
+ sl, j = type_slots(params, i)
+ out.append((params[i], s))
+ i, s = j, s + sl
+ return out
+
+
+def args_array_body(cp, desc, is_static):
+ """Bytes pushing an Object[] of the method's parameters — references as
+ is, primitives boxed, so position i is always parameter i — or
+ aconst_null for a param-less method. Straight-line; peak stack is 4."""
+ p = param_layout(desc, is_static)
+ if not p:
+ return b"\x01" # aconst_null
+ b = int_const(len(p)) + bytes([ANEWARRAY]) \
+ + struct.pack(">H", cp.cls(OBJ)) # anewarray Object
+ for idx, (t, slot) in enumerate(p):
+ b += b"\x59" + int_const(idx) + load_param(slot, t)
+ if t not in "L[":
+ b += bytes([0xB8]) + struct.pack(">H", cp.methodref(*BOX[t]))
+ b += bytes([AASTORE])
+ return b
+
+
def new_instance_target(desc):
"""Internal return type to materialize via KotlinProxyLog.newInstance, or
None to keep the default null return. Only for class (L...;) returns,
@@ -357,8 +433,10 @@ def log_body(cp, member, dotname, desc, is_static, call_ref):
"""Straight-line: log the call (with the first reference param, when
there is one), produce the return value, return.
- Reference-class returns materialize a real instance via
- KotlinProxyLog.newInstance so the daemon can proceed past e.g.
+ Reference-class returns ask KotlinProxyLog.returnValue, which dispatches
+ to KotlinProxyOverrides.invoke (per-method custom implementations, passed
+ the full argument list) and falls back to materializing a real instance
+ of the declared return type — so the daemon can proceed past e.g.
createArguments() into doExecute() with a populated args object."""
slot, has_arg = first_param_ref_slot(desc, is_static)
if has_arg:
@@ -372,13 +450,17 @@ def log_body(cp, member, dotname, desc, is_static, call_ref):
max_stack = 2
ti = new_instance_target(desc)
if ti is not None:
- ni = cp.methodref(KPL, "newInstance", NEW_INST_DESC)
- # newInstance returns Object; checkcast to the declared return type so
+ # [argsArray, member, owner, returnType] -> returnValue(...) -> Object
+ rv = cp.methodref(KPL, "returnValue", RETURN_VALUE_DESC)
+ b += args_array_body(cp, desc, is_static)
+ b += ldc(cp.string(member)) + ldc(cp.string(dotname)) \
+ + ldc(cp.string(ti.replace("/", ".")))
+ b += bytes([0xb8]) + struct.pack(">H", rv)
+ # returnValue returns Object; checkcast to the declared return type so
# the verifier is happy (null passes checkcast, so un-instantiable
# types still just return null).
- b += ldc(cp.string(ti.replace("/", "."))) \
- + bytes([0xb8]) + struct.pack(">H", ni) \
- + bytes([0xc0]) + struct.pack(">H", cp.cls(ti))
+ b += bytes([0xc0]) + struct.pack(">H", cp.cls(ti))
+ max_stack = 4
else:
push, _ = ret_bytes(desc)
b += push