summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CLAUDE.md35
-rw-r--r--Makefile26
-rw-r--r--README.md81
-rw-r--r--stubs/src/kotlin/proxy/KotlinProxyLog.java30
-rw-r--r--stubs/src/kotlin/proxy/KotlinProxyOverrides.java56
-rw-r--r--stubs/src/kotlin/proxy/ProxyPerformanceManager.java11
-rwxr-xr-xtools/gen_class.py98
7 files changed, 302 insertions, 35 deletions
diff --git a/CLAUDE.md b/CLAUDE.md
index cb6150d..19b963e 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -21,8 +21,10 @@ table). Read it before changing generation behavior.
- `tools/gen_class.py` — self-contained Python stub generator (the core). Emits
`.class` files **directly** (not Java source) by copying the real class's
erased structure verbatim.
-- `stubs/src/kotlin/proxy/KotlinProxyLog.java` — the one *real* class in the jar;
- every generated stub calls into it.
+- `stubs/src/kotlin/proxy/KotlinProxyLog.java` — the logger every generated
+ stub calls into.
+- `stubs/src/kotlin/proxy/KotlinProxyOverrides.java` — per-method custom return
+ values: the extension point for actually *implementing* stub methods.
- `jars/defs.txt`, `jars/scan.txt`, `jars/fallback.txt` — jar list files feeding
the generator (one jar path per line, `#` = comment).
- `dist/kotlin-proxy-compiler-stubs.jar` — the build artifact (gitignored).
@@ -39,18 +41,22 @@ python3 tools/gen_class.py \
--seed 'org/jetbrains/kotlin/cli/jvm/K2JVMCompiler,org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion,org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt' \
--out stubs/classes_min
-# 2. Compile the logger into the SAME tree (MUST run after step 1, which wiped it)
-javac -nowarn -proc:none -d stubs/classes_min stubs/src/kotlin/proxy/KotlinProxyLog.java
+# 2. Compile the real classes into the SAME tree (MUST run after step 1, which
+# wiped it). The -cp lets KotlinProxyOverrides import real compiler types.
+javac -nowarn -proc:none -d stubs/classes_min \
+ -cp "$(grep -v '^[[:space:]]*#' jars/defs.txt | tr '\n' ':')" \
+ stubs/src/kotlin/proxy/KotlinProxyLog.java \
+ stubs/src/kotlin/proxy/KotlinProxyOverrides.java
# 3. Package
jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min .
```
-The result is 4 class files + manifest: the three seeded stubs plus
-`kotlin/proxy/KotlinProxyLog`. The seed trio are **the only** classes stubbed
-(the "trio" mode); their superclasses/interfaces (`CLICompiler`,
-`K2JVMCompilerArguments`, …) are *not* stubbed and resolve from the real jars on
-the daemon classpath.
+The result is 5 class files + manifest: the three seeded stubs plus
+`kotlin/proxy/KotlinProxyLog` and `kotlin/proxy/KotlinProxyOverrides`. The
+seed trio are **the only** classes stubbed (the "trio" mode); their
+superclasses/interfaces (`CLICompiler`, `K2JVMCompilerArguments`, …) are
+*not* stubbed and resolve from the real jars on the daemon classpath.
## Testing / verifying (there is no unit-test suite)
@@ -65,6 +71,8 @@ machinery NPEs after calling into the stub). That is the intended state. To
confirm the stub actually ran *inside the out-of-process daemon*, read the marker
file `/tmp/kotlin-proxy-marker.txt` — a daemon run shows thread names
`RMI TCP Connection(N)` and `LOAD …/dist/kotlin-proxy-compiler-stubs.jar` lines.
+An `OVERRIDE <owner>.<member> -> <dump>` line shows a
+`KotlinProxyOverrides.invoke` implementation supplied the return value.
The marker path is fixed regardless of the daemon's working dir or missing
`-Dkotlin.proxy.log`, so it is the reliable proof.
@@ -83,8 +91,13 @@ Sanity-check generated bytecode with `javap -c stubs/classes_min/org/jetbrains/k
`StackMapTable` frames are needed. `<clinit>` calls `KotlinProxyLog.loaded`;
constructors call `super()` (enums call `Enum.<init>("stub",0)`); concrete
methods log and then return a default — but reference-class returns
- (except `String`) materialize a *real* instance via `newInstance` so the daemon
- can proceed (e.g. `createArguments()` yields a real `K2JVMCompilerArguments`).
+ (except `String`) build an `Object[]` of the call's arguments (primitives
+ boxed; `null` when param-less) and ask `KotlinProxyLog.returnValue`, which
+ consults `KotlinProxyOverrides.invoke` first (per-method custom
+ implementations — the hook for actually *implementing* a stub method with
+ real arguments) and falls back to materializing a *real* instance of the
+ declared return type so the daemon can proceed (e.g. `createArguments()`
+ yields a real `K2JVMCompilerArguments`).
Param-less erasure bridges forward to their concrete sibling instead of
logging+null (the daemon dispatches through bridges).
- **Classpath shadowing (the subtle part):** KGP seeds the daemon classpath from
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..7f4f548
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,26 @@
+.PHONY: all clean compileStubs
+
+all: dist/kotlin-proxy-compiler-stubs.jar
+
+clean:
+ rm -f dist/kotlin-proxy-compiler-stubs.jar
+ rm -rf stubs/classes_min/*
+
+stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class: tools/gen_class.py jars/scan.txt jars/defs.txt jars/fallback.txt
+ python3 tools/gen_class.py \
+ --scan-jars jars/scan.txt \
+ --def-jars jars/defs.txt \
+ --fallback-jars jars/fallback.txt \
+ --seed-only --no-closure \
+ --seed 'org/jetbrains/kotlin/cli/jvm/K2JVMCompiler,org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion,org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt' \
+ --out stubs/classes_min
+
+DEFS = $(shell grep -v '^[[:space:]]*#' jars/defs.txt | tr '\n' ':')
+
+compileStubs: stubs/classes_min/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class jars/defs.txt stubs/src/kotlin/proxy/*.java
+ javac -nowarn -proc:none -d stubs/classes_min \
+ -cp "$(DEFS)" \
+ stubs/src/kotlin/proxy/*.java
+
+dist/kotlin-proxy-compiler-stubs.jar: compileStubs
+ jar cf $@ -C stubs/classes_min .
diff --git a/README.md b/README.md
index ff88a88..ed6dc0d 100644
--- a/README.md
+++ b/README.md
@@ -11,8 +11,9 @@ compile inside the stub. **Pure replacement: no passthrough, no fallback.**
kotlin-proxy/
├── tools/gen_class.py # bytecode stub generator (self-contained)
├── stubs/
-│ ├── src/kotlin/proxy/KotlinProxyLog.java # the logger the stubs call
-│ └── classes_min/ # generated .class files (build output)
+│ ├── src/kotlin/proxy/KotlinProxyLog.java # the logger the stubs call
+│ ├── src/kotlin/proxy/KotlinProxyOverrides.java # per-method custom return values
+│ └── classes_min/ # generated .class files (build output)
├── jars/
│ ├── defs.txt # 28 kotlin-2.2.10 jars (definition lookup)
│ ├── scan.txt # 6 AGP-9.4.1 jars (which kotlin classes AGP touches)
@@ -48,14 +49,21 @@ This emits exactly the three seeded classes into `stubs/classes_min/`:
`gen_class.py` **wipes `--out` first** and regenerates everything, so the
output directory must not contain anything you need.
-### 2. Compile the logger into the same tree
+### 2. Compile the real classes into the same tree
```bash
-javac -nowarn -proc:none -d stubs/classes_min stubs/src/kotlin/proxy/KotlinProxyLog.java
+javac -nowarn -proc:none -d stubs/classes_min \
+ -cp "$(grep -v '^[[:space:]]*#' jars/defs.txt | tr '\n' ':')" \
+ stubs/src/kotlin/proxy/KotlinProxyLog.java \
+ stubs/src/kotlin/proxy/KotlinProxyOverrides.java
```
-`KotlinProxyLog` is the one real class in the jar; every generated stub calls
-into it. (Step 1 wiped the directory, so `javac` must run **after** it.)
+`KotlinProxyLog` (the logger every stub calls) and `KotlinProxyOverrides`
+(the per-method return-value hook you implement against) are the only real
+classes in the jar. The `-cp` puts the real compiler jars on javac's
+classpath so your overrides may import compiler types (`CommonCompilerArguments`,
+`Services`, …). (Step 1 wiped the directory, so `javac` must run
+**after** it.)
### 3. Package the jar
@@ -63,12 +71,13 @@ into it. (Step 1 wiped the directory, so `javac` must run **after** it.)
jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min .
```
-Result: 4 class files + manifest
+Result: 5 class files + manifest
```
$ jar tf dist/kotlin-proxy-compiler-stubs.jar | grep -v '/$'
META-INF/MANIFEST.MF
kotlin/proxy/KotlinProxyLog.class
+kotlin/proxy/KotlinProxyOverrides.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt.class
@@ -141,12 +150,18 @@ The list files contain one jar path per line; `#` starts a comment.
- **Concrete methods:** call
`KotlinProxyLog.call(member, dotName)` — or the 3-arg overload passing the
**first reference parameter**, so the log shows the boundary argument
- (typically `K2JVMCompilerArguments`), then produce the return value:
- - reference-class returns (except `String`): `KotlinProxyLog.newInstance(retType)`
- + `checkcast` — reflectively instantiates the real (un-stubbed) return
- class so the daemon can proceed (e.g. `createArguments()` yields a real
- `K2JVMCompilerArguments`). Types without a usable no-arg ctor (interfaces,
- enums, abstract) yield `null`, as before.
+ (typically `K2JVMCompilerArguments`) — then produce the return value:
+ - reference-class returns (except `String`): build an `Object[]` of the
+ call's arguments (references as-is, primitives boxed — so position `i`
+ is parameter `i`; `null` for a param-less method), then ask
+ `KotlinProxyLog.returnValue(member, owner, retType, args)` + `checkcast`.
+ That first dispatches to `KotlinProxyOverrides.invoke` — your per-method
+ custom implementation, see below — and falls back to
+ `KotlinProxyLog.newInstance(retType)`, which reflectively instantiates
+ the real (un-stubbed) return class so the daemon can proceed (e.g.
+ `createArguments()` yields a real `K2JVMCompilerArguments`). Types
+ without a usable no-arg ctor (interfaces, enums, abstract) yield
+ `null`, as before.
- everything else: the JVM default (`0`, `null`, …).
- **Param-less erasure bridges:** forward to their concrete sibling (same name,
narrower return type) instead of logging + null — the daemon dispatches
@@ -155,6 +170,43 @@ The list files contain one jar path per line; `#` starts a comment.
real one: call the concrete method.
- **Abstract methods / interface methods:** no body.
+## Implementing a stub method (custom return values)
+
+`KotlinProxyOverrides` (a real class in the jar, `stubs/src/kotlin/proxy/`) is
+the extension point: every stub method with a reference-class return type
+(other than `String`) asks it for its return value **before** falling back to
+the default. Implement exactly the methods you want the stub to actually do
+something for; return `NO_VALUE` for everything else.
+
+```java
+public static Object invoke(String member, String ownerDotName,
+ String returnDotName, Object[] args) {
+ if ("createPerformanceManager".equals(member)
+ && "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler".equals(ownerDotName)) {
+ CommonCompilerArguments a = (CommonCompilerArguments) args[0];
+ Services services = (Services) args[1];
+ // build/return whatever this method should return, using the real args
+ return new MyPerformanceManager();
+ }
+ return NO_VALUE;
+}
+```
+
+- `member` / `ownerDotName` identify the method; `returnDotName` is its
+ **declared** return type (what the generated body `checkcast`s the result
+ to — your value must be assignable to it, or `ClassCastException`).
+- `args[i]` is the `i`-th parameter of the call, so your implementation works
+ on the **real** arguments the daemon handed the compiler: reference types
+ as-is, primitives boxed (`Integer`, `Long`, `Boolean`, …). A param-less
+ method gets `args == null`.
+- Returning `NO_VALUE` keeps the default (real no-arg instance, or null).
+ An override is logged as `OVERRIDE <owner>.<member> -> <dump>`.
+
+Rebuild with steps **2–3** after editing (no need to re-run the generator).
+Your overrides may reference the real compiler types — they resolve from the
+real jars on the daemon classpath; compile `KotlinProxyOverrides.java` with
+the jars from `jars/defs.txt` on `javac`'s classpath if you import them.
+
## Using the jar in a build
The daemon is a separate JVM. Its compiler classpath comes from the Gradle
@@ -223,6 +275,9 @@ RMI TCP Connection(2)-127.0.0.1 | CALL org.jetbrains.kotlin.cli.jvm.K2JVMCompil
- `CALL <name>.<member> arg=<dump>` — boundary argument: compiler-argument
objects are dumped via `toCommonList()`; other objects via their
non-null fields across the hierarchy.
+- `OVERRIDE <name>.<member> -> <dump>` — a `KotlinProxyOverrides.invoke`
+ implementation supplied the return value (logged once, in
+ `KotlinProxyLog.returnValue`).
A `newInstance FAIL <type> : <exception>` line means a stub method's declared
return type has no usable no-arg constructor (e.g. `PerformanceManager`), so
diff --git a/stubs/src/kotlin/proxy/KotlinProxyLog.java b/stubs/src/kotlin/proxy/KotlinProxyLog.java
index 50e347a..3afdb38 100644
--- a/stubs/src/kotlin/proxy/KotlinProxyLog.java
+++ b/stubs/src/kotlin/proxy/KotlinProxyLog.java
@@ -51,9 +51,33 @@ public final class KotlinProxyLog {
}
/**
- * Called by stub methods that declare a reference return type (other than
- * String). Reflectively instantiates the real, un-stubbed class of that
- * return type so the caller (the daemon) can keep going: e.g.
+ * Return value for stub methods with a reference return type (other than
+ * String). Asks {@link KotlinProxyOverrides#invoke} first — per-method
+ * custom implementations, passed the full argument list — and falls back
+ * to materializing a real no-arg instance of the declared return type.
+ * The generated body checkcasts the result to the declared type.
+ *
+ * The generated bytecode pushes the args array FIRST (member, owner and
+ * return type last) so its stack stays shallow (peak 4) — keep this
+ * parameter order in sync with RETURN_VALUE_DESC in gen_class.py.
+ */
+ public static Object returnValue(Object[] args, String member,
+ String ownerDotName, String returnDotName) {
+ Object v = KotlinProxyOverrides.invoke(member, ownerDotName,
+ returnDotName, args);
+ if (v != KotlinProxyOverrides.NO_VALUE) {
+ log("OVERRIDE " + ownerDotName + "." + member
+ + " -> " + dump(v));
+ return v;
+ }
+ return newInstance(returnDotName);
+ }
+
+ /**
+ * Default return-value materialization (the fallback of
+ * {@link #returnValue}). Reflectively instantiates the real, un-stubbed
+ * class of the declared return type so the caller (the daemon) can keep
+ * going: e.g.
* K2JVMCompiler.createArguments() returns a genuine K2JVMCompilerArguments,
* which lets parseCommandLineArguments run and doExecute be reached with the
* fully-populated args (the boundary we want to log). Returns null when the
diff --git a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java
new file mode 100644
index 0000000..d40417e
--- /dev/null
+++ b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java
@@ -0,0 +1,56 @@
+package kotlin.proxy;
+
+import org.jetbrains.kotlin.cli.common.ExitCode;
+import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments;
+import org.jetbrains.kotlin.com.intellij.openapi.Disposable;
+import org.jetbrains.kotlin.config.CompilerConfiguration;
+import org.jetbrains.kotlin.config.JvmTarget;
+import org.jetbrains.kotlin.config.Services;
+import org.jetbrains.kotlin.utils.KotlinPaths;
+
+/**
+ * Per-method custom return values for the generated stubs.
+ *
+ * Every stub method with a reference return type (other than String) asks
+ * here for its return value (via {@link KotlinProxyLog#returnValue}) before
+ * falling back to the default — a real no-arg instance of the declared return
+ * type, or null. Implement the methods you want the stub to actually do
+ * something for; return {@link #NO_VALUE} for everything else.
+ *
+ * args[i] is the i-th parameter of the call: reference types as-is,
+ * primitives boxed (Integer, Long, Boolean, ...). A param-less method gets
+ * args == null. Rebuild the jar (steps 2-3) after editing.
+ */
+public final class KotlinProxyOverrides {
+
+ /** Return this from invoke() to keep the default return value. */
+ public static final Object NO_VALUE = new Object();
+
+ private static ProxyPerformanceManager sPerformanceManager;
+
+ private KotlinProxyOverrides() {
+ }
+
+ public static Object invoke(String member, String ownerDotName,
+ String returnDotName, Object[] args) {
+ if ("org.jetbrains.kotlin.cli.jvm.K2JVMCompiler".equals(ownerDotName)) {
+ if ("createPerformanceManager".equals(member)) {
+ K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0];
+ // Services services = (Services) args[1];
+ sPerformanceManager = new ProxyPerformanceManager(JvmTarget.Companion.fromString(compilerArgs.getJvmTarget()));
+ return sPerformanceManager;
+ }
+ if ("getPlatform".equals(member)) {
+ return sPerformanceManager.getTargetPlatform();
+ }
+ if ("doExecute".equals(member)) {
+ K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0];
+ CompilerConfiguration config = (CompilerConfiguration) args[1];
+ Disposable disposable = (Disposable) args[2];
+ KotlinPaths paths = (KotlinPaths) args[3];
+ return ExitCode.COMPILATION_ERROR;
+ }
+ }
+ return NO_VALUE;
+ }
+}
diff --git a/stubs/src/kotlin/proxy/ProxyPerformanceManager.java b/stubs/src/kotlin/proxy/ProxyPerformanceManager.java
new file mode 100644
index 0000000..1e2e354
--- /dev/null
+++ b/stubs/src/kotlin/proxy/ProxyPerformanceManager.java
@@ -0,0 +1,11 @@
+package kotlin.proxy;
+
+import org.jetbrains.kotlin.config.JvmTarget;
+import org.jetbrains.kotlin.util.PerformanceManager;
+import org.jetbrains.kotlin.platform.jvm.JvmPlatforms;
+;
+class ProxyPerformanceManager extends PerformanceManager {
+ ProxyPerformanceManager(JvmTarget targetVersion) {
+ super(JvmPlatforms.INSTANCE.jvmPlatformByTargetVersion(targetVersion), "proxy perf manager");
+ }
+}
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