diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-05 21:18:56 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-05 21:18:56 +0100 |
| commit | 64223f61fc8413ad69ba0e814fbbc552a6c57c33 (patch) | |
| tree | 79f47335e71dfe6d702fbca520513b2b7eb90fae /stubs/src/kotlin | |
| parent | aefb3dbf687b46775bca1b600317bc053648b211 (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… .
Diffstat (limited to 'stubs/src/kotlin')
| -rw-r--r-- | stubs/src/kotlin/proxy/KotlinProxyLog.java | 24 | ||||
| -rw-r--r-- | stubs/src/kotlin/proxy/KotlinProxyOverrides.java | 33 |
2 files changed, 43 insertions, 14 deletions
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; + } } |
