summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-05 21:18:56 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-05 21:18:56 +0100
commit64223f61fc8413ad69ba0e814fbbc552a6c57c33 (patch)
tree79f47335e71dfe6d702fbca520513b2b7eb90fae
parentaefb3dbf687b46775bca1b600317bc053648b211 (diff)
Pass the receiver instance to KotlinProxyOverrides.invoke
The generated returnValue call now carries the stub instance the method was called on (null for statics), so overrides can keep per-compiler state instead of statics: the performance manager is now stored in an IdentityHashMap keyed by receiver, and OVERRIDE log lines carry the receiver's identityHashCode as inst=0x… .
-rw-r--r--CLAUDE.md20
-rw-r--r--README.md41
-rw-r--r--stubs/src/kotlin/proxy/KotlinProxyLog.java24
-rw-r--r--stubs/src/kotlin/proxy/KotlinProxyOverrides.java33
-rwxr-xr-xtools/gen_class.py22
5 files changed, 99 insertions, 41 deletions
diff --git a/CLAUDE.md b/CLAUDE.md
index 19b963e..1bfdfac 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -25,6 +25,8 @@ table). Read it before changing generation behavior.
stub calls into.
- `stubs/src/kotlin/proxy/KotlinProxyOverrides.java` — per-method custom return
values: the extension point for actually *implementing* stub methods.
+- `stubs/src/kotlin/proxy/ProxyPerformanceManager.java` — override support
+ class (per-compiler-instance perf manager) returned by the overrides.
- `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).
@@ -46,14 +48,16 @@ python3 tools/gen_class.py \
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
+ stubs/src/kotlin/proxy/KotlinProxyOverrides.java \
+ stubs/src/kotlin/proxy/ProxyPerformanceManager.java
# 3. Package
jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min .
```
-The result is 5 class files + manifest: the three seeded stubs plus
-`kotlin/proxy/KotlinProxyLog` and `kotlin/proxy/KotlinProxyOverrides`. The
+The result is 6 class files + manifest: the three seeded stubs plus
+`kotlin/proxy/KotlinProxyLog`, `kotlin/proxy/KotlinProxyOverrides`, and
+`kotlin/proxy/ProxyPerformanceManager`. 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.
@@ -71,8 +75,10 @@ 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.
+An `OVERRIDE <owner>.<member> inst=0x… -> <dump>` line shows a
+`KotlinProxyOverrides.invoke` implementation supplied the return value
+(`inst` is the receiver instance the method was called on — omitted for
+statics).
The marker path is fixed regardless of the daemon's working dir or missing
`-Dkotlin.proxy.log`, so it is the reliable proof.
@@ -95,7 +101,9 @@ Sanity-check generated bytecode with `javap -c stubs/classes_min/org/jetbrains/k
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
+ real arguments and the receiver — the stub instance the method was called
+ on, `null` for statics — so overrides can keep per-instance state) 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
diff --git a/README.md b/README.md
index ed6dc0d..2c14965 100644
--- a/README.md
+++ b/README.md
@@ -13,6 +13,7 @@ kotlin-proxy/
├── stubs/
│ ├── src/kotlin/proxy/KotlinProxyLog.java # the logger the stubs call
│ ├── src/kotlin/proxy/KotlinProxyOverrides.java # per-method custom return values
+│ ├── src/kotlin/proxy/ProxyPerformanceManager.java # override support class
│ └── classes_min/ # generated .class files (build output)
├── jars/
│ ├── defs.txt # 28 kotlin-2.2.10 jars (definition lookup)
@@ -55,12 +56,14 @@ output directory must not contain anything you need.
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
+ stubs/src/kotlin/proxy/KotlinProxyOverrides.java \
+ stubs/src/kotlin/proxy/ProxyPerformanceManager.java
```
-`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
+`KotlinProxyLog` (the logger every stub calls), `KotlinProxyOverrides`
+(the per-method return-value hook you implement against), and
+`ProxyPerformanceManager` (an override support class the hook returns) 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.)
@@ -71,13 +74,14 @@ classpath so your overrides may import compiler types (`CommonCompilerArguments`
jar cf dist/kotlin-proxy-compiler-stubs.jar -C stubs/classes_min .
```
-Result: 5 class files + manifest
+Result: 6 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
+kotlin/proxy/ProxyPerformanceManager.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler$Companion.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.class
org/jetbrains/kotlin/cli/jvm/K2JVMCompilerKt.class
@@ -154,8 +158,10 @@ The list files contain one jar path per line; `#` starts a comment.
- 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
+ `KotlinProxyLog.returnValue(member, owner, retType, args, instance)` +
+ `checkcast`, where `instance` is the receiver — the stub instance the
+ method was called on (`this`), or `null` for statics. 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.
@@ -180,12 +186,14 @@ something for; return `NO_VALUE` for everything else.
```java
public static Object invoke(String member, String ownerDotName,
- String returnDotName, Object[] args) {
+ String returnDotName, Object[] args,
+ Object instance) {
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
+ // (and `instance`, to keep per-compiler-instance state)
return new MyPerformanceManager();
}
return NO_VALUE;
@@ -199,8 +207,15 @@ public static Object invoke(String member, String ownerDotName,
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`.
+- `instance` is the receiver — the stub instance the method was called on
+ (the daemon's `K2JVMCompiler` object) — so you can keep per-compiler state
+ across calls (identity-keyed, e.g. in an `IdentityHashMap`). It is `null`
+ for static methods (e.g. everything on the `K2JVMCompilerKt` file facade).
- Returning `NO_VALUE` keeps the default (real no-arg instance, or null).
- An override is logged as `OVERRIDE <owner>.<member> -> <dump>`.
+ An override is logged as
+ `OVERRIDE <owner>.<member> [inst=0x…] -> <dump>` — the `inst` tag
+ (the receiver's `identityHashCode`, omitted for statics) lets you
+ correlate which compiler instance a method ran on.
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
@@ -275,9 +290,11 @@ 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`).
+- `OVERRIDE <name>.<member> [inst=0x…] -> <dump>` — a
+ `KotlinProxyOverrides.invoke` implementation supplied the return value
+ (logged once, in `KotlinProxyLog.returnValue`); `inst` is the receiver
+ instance's `identityHashCode` (omitted for static calls), so you can see
+ which compiler instance the method ran on.
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 3afdb38..af5db37 100644
--- a/stubs/src/kotlin/proxy/KotlinProxyLog.java
+++ b/stubs/src/kotlin/proxy/KotlinProxyLog.java
@@ -53,20 +53,28 @@ public final class KotlinProxyLog {
/**
* 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.
+ * custom implementations, passed the full argument list and the receiver
+ * (the stub instance the method was called on, null for statics) — 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.
+ * The generated bytecode pushes the args array FIRST (member, owner,
+ * return type and receiver last) so its stack stays shallow (peak 5) —
+ * 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) {
+ String ownerDotName, String returnDotName,
+ Object instance) {
Object v = KotlinProxyOverrides.invoke(member, ownerDotName,
- returnDotName, args);
+ returnDotName, args, instance);
if (v != KotlinProxyOverrides.NO_VALUE) {
log("OVERRIDE " + ownerDotName + "." + member
+ + (instance != null
+ ? " inst=0x"
+ + Integer.toHexString(System.identityHashCode(instance))
+ : "")
+ " -> " + dump(v));
return v;
}
diff --git a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java
index d40417e..8064d19 100644
--- a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java
+++ b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java
@@ -8,6 +8,10 @@ import org.jetbrains.kotlin.config.JvmTarget;
import org.jetbrains.kotlin.config.Services;
import org.jetbrains.kotlin.utils.KotlinPaths;
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.Map;
+
/**
* Per-method custom return values for the generated stubs.
*
@@ -19,29 +23,42 @@ import org.jetbrains.kotlin.utils.KotlinPaths;
*
* 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.
+ * args == null.
+ *
+ * instance is the receiver — the stub instance the method was called on — so
+ * you can keep per-compiler state (identity-keyed; see performanceManagers).
+ * It is null for static methods. 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;
+ /**
+ * Per-compiler-instance state, keyed by the stub receiver instance.
+ * Identity semantics (each K2JVMCompiler instance is its own entry),
+ * synchronized because daemon compilations may run on different threads.
+ */
+ private static final Map<Object, Compiler> compilers =
+ Collections.synchronizedMap(new IdentityHashMap<>());
private KotlinProxyOverrides() {
}
public static Object invoke(String member, String ownerDotName,
- String returnDotName, Object[] args) {
+ String returnDotName, Object[] args,
+ Object instance) {
if ("org.jetbrains.kotlin.cli.jvm.K2JVMCompiler".equals(ownerDotName)) {
+ Compiler compiler = compilers.computeIfAbsent(instance, (key) -> new Compiler());
+
if ("createPerformanceManager".equals(member)) {
K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0];
// Services services = (Services) args[1];
- sPerformanceManager = new ProxyPerformanceManager(JvmTarget.Companion.fromString(compilerArgs.getJvmTarget()));
- return sPerformanceManager;
+ compiler.performanceManager = new ProxyPerformanceManager(JvmTarget.Companion.fromString(compilerArgs.getJvmTarget()));
+ return compiler.performanceManager;
}
if ("getPlatform".equals(member)) {
- return sPerformanceManager.getTargetPlatform();
+ return compiler.performanceManager.getTargetPlatform();
}
if ("doExecute".equals(member)) {
K2JVMCompilerArguments compilerArgs = (K2JVMCompilerArguments) args[0];
@@ -53,4 +70,8 @@ public final class KotlinProxyOverrides {
}
return NO_VALUE;
}
+
+ private static class Compiler {
+ ProxyPerformanceManager performanceManager;
+ }
}
diff --git a/tools/gen_class.py b/tools/gen_class.py
index 32f00c8..8e1e0f4 100755
--- a/tools/gen_class.py
+++ b/tools/gen_class.py
@@ -12,8 +12,9 @@ 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.
+custom implementations, passed the full argument list and the receiver —
+the stub instance the method was called on, null for statics) 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.
@@ -43,10 +44,11 @@ CALL_ARG_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"
LOADED_DESC = "(Ljava/lang/String;)V"
# 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).
+# (peak 5 instead of 9 if it were pushed last). The receiver (the stub
+# instance the method was called on; null for statics) comes last.
RETURN_VALUE_DESC = \
"([Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;" \
- "Ljava/lang/String;)Ljava/lang/Object;"
+ "Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;"
# access flags
A_PUBLIC, A_PRIVATE, A_PROTECTED = 0x1, 0x2, 0x4
@@ -435,9 +437,10 @@ def log_body(cp, member, dotname, desc, is_static, call_ref):
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."""
+ the full argument list and the receiver — aload_0, or null for statics)
+ 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:
call_arg_ref = cp.methodref(KPL, "call", CALL_ARG_DESC)
@@ -450,17 +453,18 @@ def log_body(cp, member, dotname, desc, is_static, call_ref):
max_stack = 2
ti = new_instance_target(desc)
if ti is not None:
- # [argsArray, member, owner, returnType] -> returnValue(...) -> Object
+ # [argsArray, member, owner, returnType, instance] -> returnValue(...)
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 += b"\x2a" if not is_static else b"\x01" # receiver: aload_0 / null
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 += bytes([0xc0]) + struct.pack(">H", cp.cls(ti))
- max_stack = 4
+ max_stack = 5
else:
push, _ = ret_bytes(desc)
b += push