From 97eba6c97b81fa31c64cea6680afa4c645dd2da5 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 18:34:56 -0300 Subject: [PATCH 01/14] feat(DexFactory): report classes generated at runtime Record every class resolution that was not served by a prebuilt class from the APK: the requested and loaded names, whether the base is an interface, and whether the dex was generated in this process or reused from the on-disk dex cache. The list is process-wide and exposed through com.tns.DexFactory.getRuntimeGeneratedClasses(); debuggable apps also log one line per class under the JS tag. --- test-app/app/src/main/assets/app/mainpage.js | 1 + .../app/tests/testRuntimeGenerationReport.js | 44 +++++++++++++++++++ .../src/main/java/com/tns/DexFactory.java | 28 +++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index 8227c792e..fecb38fa0 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -73,6 +73,7 @@ require("./tests/testTypedArrayArgs"); require("./tests/dex-interface-implementation"); require("./tests/testClassForNameDiscovery"); require("./tests/testPrebuiltExtendClasses"); +require("./tests/testRuntimeGenerationReport"); require("./tests/testProxyRegistrationGc"); require("./tests/testInterfaceImplementation"); require("./tests/testPrimitiveCallbackReturns"); diff --git a/test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js b/test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js new file mode 100644 index 000000000..22e1122f3 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js @@ -0,0 +1,44 @@ +describe("Runtime class generation report", function () { + + function reportEntriesFor(loadedName) { + var lines = com.tns.DexFactory.getRuntimeGeneratedClasses(); + var result = []; + for (var i = 0; i < lines.length; i++) { + var fields = String(lines[i]).split(" "); + if (fields[2] === loadedName) { + result.push({ source: fields[0], kind: fields[1], loaded: fields[2], requested: fields[3] }); + } + } + return result; + } + + it("lists a class generated at runtime", function () { + // A computed member hides the extend from the static binding generator. + var ext = "ex" + "tend"; + var ReportedObject = java.lang.Object[ext]({ + toString: function () { + return "reported"; + } + }); + var instance = new ReportedObject(); + expect(instance.toString()).toBe("reported"); + + var entries = reportEntriesFor(instance.getClass().getName()); + expect(entries.length).toBe(1); + expect(["generated", "cached"]).toContain(entries[0].source); + expect(entries[0].kind).toBe("class"); + expect(entries[0].requested).toMatch(/^com\.tns\.gen\.java\.lang\.Object_/); + }); + + it("does not list a class loaded from the APK", function () { + var UnreportedObject = java.lang.Object.extend({ + toString: function () { + return "unreported"; + } + }); + var instance = new UnreportedObject(); + expect(instance.toString()).toBe("unreported"); + + expect(reportEntriesFor(instance.getClass().getName())).toEqual([]); + }); +}); diff --git a/test-app/runtime/src/main/java/com/tns/DexFactory.java b/test-app/runtime/src/main/java/com/tns/DexFactory.java index d913a023b..592a6ea59 100644 --- a/test-app/runtime/src/main/java/com/tns/DexFactory.java +++ b/test-app/runtime/src/main/java/com/tns/DexFactory.java @@ -78,6 +78,30 @@ public class DexFactory { static long totalMultiDexTime = 0; static long totalLoadDexTime = 0; + // Process-wide: every runtime (main and workers) has its own DexFactory. + private static final ArrayList runtimeGeneratedClasses = new ArrayList(); + + /** + * The classes this process resolved without a prebuilt class from the APK, + * one line each: {@code }. + * "cached" means the dex was reused from the on-disk dex cache. + */ + public static String[] getRuntimeGeneratedClasses() { + synchronized (runtimeGeneratedClasses) { + return runtimeGeneratedClasses.toArray(new String[0]); + } + } + + private static void recordRuntimeGeneratedClass(String requestedName, Class loadedClass, boolean isInterface, boolean generated) { + String entry = (generated ? "generated" : "cached") + " " + (isInterface ? "interface" : "class") + " " + loadedClass.getName() + " " + requestedName; + synchronized (runtimeGeneratedClasses) { + runtimeGeneratedClasses.add(entry); + } + if (com.tns.Runtime.isDebuggable()) { + Log.i("JS", (generated ? "Generated class at runtime: " : "Loaded runtime-generated class from the dex cache: ") + loadedClass.getName() + " (requested as " + requestedName + ")"); + } + } + public Class resolveClass(String baseClassName, String name, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException { String fullClassName = className.replace("$", "_"); String originalFullClassName = fullClassName; @@ -131,9 +155,10 @@ public Class resolveClass(String baseClassName, String name, String className String contentDigest = computeContentDigest(classToProxy, methodOverrides, implementedInterfaces, isInterface); File dexFile = this.getDexFile(desiredDexClassName, contentDigest); + boolean generated = dexFile == null; // generate dex file - if (dexFile == null) { + if (generated) { long startGenTime = System.nanoTime(); if (logger.isEnabled()) { logger.write("generating proxy in place"); @@ -199,6 +224,7 @@ public Class resolveClass(String baseClassName, String name, String className classStorageService.storeClass(result.getName(), result); this.injectedDexClasses.put(originalFullClassName, result); + recordRuntimeGeneratedClass(className, result, isInterface, generated); return result; } From fb881d286cfb2d142af779dff0a59b709b99574c Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 18:46:56 -0300 Subject: [PATCH 02/14] fix: resolve Interface.extend({...}) to the interface's shared class An anonymous extend of an interface asked DexFactory for a per-call-site class name, while the class it actually loads, the static binding generator's prebuilt class and the dex file it writes are all named after the interface. Neither the prebuilt probe nor the dex cache probe could hit, so every such extend regenerated and injected a dex on every process start, even for interfaces prebuilt in the APK. Without extra implemented interfaces, resolve these extends under the per-interface name, the same class new Interface({...}) uses, so the APK class, the in-process class map and the dex cache all serve them. That also fixes: - extends of nested interfaces, which tried to load the class with a `$` while it is generated with `_` (ClassNotFoundException); - extends of a non-prebuilt interface in a worker, whose constructor re-resolved the class by its real name, missed the per-site cache entry and generated a broken proxy (StackOverflowError). Key the extended-constructor cache by call site for interface bases. Every anonymous extend of an interface shares one Java class, so keying by that class handed every later call site the constructor, and so the implementation object, of the first extend of that interface in the process. The class-keyed entry is still created once, by the first extend, for instances Java creates on its own. --- test-app/app/src/main/assets/app/mainpage.js | 1 + .../assets/app/tests/interfaceExtendWorker.js | 31 ++++ .../app/tests/testInterfaceExtendClasses.js | 168 ++++++++++++++++++ .../runtime/src/main/cpp/MetadataNode.cpp | 15 +- .../src/main/java/com/tns/DexFactory.java | 35 +++- 5 files changed, 241 insertions(+), 9 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/interfaceExtendWorker.js create mode 100644 test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index fecb38fa0..dc9308378 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -74,6 +74,7 @@ require("./tests/dex-interface-implementation"); require("./tests/testClassForNameDiscovery"); require("./tests/testPrebuiltExtendClasses"); require("./tests/testRuntimeGenerationReport"); +require("./tests/testInterfaceExtendClasses"); require("./tests/testProxyRegistrationGc"); require("./tests/testInterfaceImplementation"); require("./tests/testPrimitiveCallbackReturns"); diff --git a/test-app/app/src/main/assets/app/tests/interfaceExtendWorker.js b/test-app/app/src/main/assets/app/tests/interfaceExtendWorker.js new file mode 100644 index 000000000..1c7a68d26 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/interfaceExtendWorker.js @@ -0,0 +1,31 @@ +// Entry for testInterfaceExtendClasses: extends an interface the static binding +// generator never sees, so this worker's runtime generates its class. +var ext = "ex" + "tend"; +var Action = java.security.PrivilegedExceptionAction; +var result = {}; + +// Reflection makes Java, not JS, call the implementation. +function callFromJava(instance, methodName) { + return instance.getClass().getMethod(methodName, []).invoke(instance, []); +} +try { + var Extended = Action[ext]({ + run: function () { + return "worker extend"; + } + }); + var extended = new Extended(); + result.extendedClass = String(extended.getClass().getName()); + result.extendedRun = String(callFromJava(extended, "run")); + + var implemented = new Action({ + run: function () { + return "worker new"; + } + }); + result.implementedClass = String(implemented.getClass().getName()); + result.implementedRun = String(callFromJava(implemented, "run")); +} catch (e) { + result.error = String(e && e.message || e); +} +postMessage(result); diff --git a/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js new file mode 100644 index 000000000..ca5ca477e --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js @@ -0,0 +1,168 @@ +describe("Anonymous extends of an interface", function () { + var ext = "ex" + "tend"; + + // Reflection makes Java, not JS, call the implementation. + function callFromJava(instance, methodName) { + return instance.getClass().getMethod(methodName, []).invoke(instance, []); + } + + function reportEntriesFor(loadedName) { + var lines = com.tns.DexFactory.getRuntimeGeneratedClasses(); + var result = []; + for (var i = 0; i < lines.length; i++) { + var fields = String(lines[i]).split(" "); + if (fields[2] === loadedName) { + result.push({ source: fields[0], kind: fields[1], loaded: fields[2], requested: fields[3] }); + } + } + return result; + } + + it("load the interface's class from the APK when it is prebuilt", function () { + var Extended = java.lang.Runnable.extend({ + run: function () {} + }); + var instance = new Extended(); + + expect(instance.getClass().getName()).toBe("com.tns.gen.java.lang.Runnable"); + expect(reportEntriesFor("com.tns.gen.java.lang.Runnable")).toEqual([]); + }); + + it("keep each call site's implementation when they extend the same interface", function () { + var calls = []; + var First = java.lang.Runnable.extend({ + run: function () { + calls.push("first"); + } + }); + var Second = java.lang.Runnable.extend({ + run: function () { + calls.push("second"); + } + }); + + expect(Second).not.toBe(First); + callFromJava(new Second(), "run"); + callFromJava(new First(), "run"); + expect(calls).toEqual(["second", "first"]); + }); + + it("return the same constructor each time one call site runs", function () { + function extendRunnable(implementation) { + return java.lang.Runnable.extend(implementation); + } + var calls = []; + var First = extendRunnable({ + run: function () { + calls.push("first"); + } + }); + var Again = extendRunnable({ + run: function () { + calls.push("again"); + } + }); + + expect(Again).toBe(First); + callFromJava(new Again(), "run"); + expect(calls).toEqual(["first"]); + }); + + it("work with a generated class after new Interface({...}) of the same interface", function () { + // Computed members hide both uses from the static binding generator. + var Action = java.security.PrivilegedAction; + var implemented = new Action({ + run: function () { + return "implemented"; + } + }); + var Extended = Action[ext]({ + run: function () { + return "extended"; + } + }); + var extended = new Extended(); + + expect(String(callFromJava(implemented, "run"))).toBe("implemented"); + expect(String(callFromJava(extended, "run"))).toBe("extended"); + expect(String(extended.getClass().getName())).toBe(String(implemented.getClass().getName())); + expect(reportEntriesFor("com.tns.gen.java.security.PrivilegedAction").length).toBe(1); + }); + + it("work with a generated class before new Interface({...}) of the same interface", function () { + var Closeable = java.lang.AutoCloseable; + var calls = []; + var Extended = Closeable[ext]({ + close: function () { + calls.push("extended"); + } + }); + var extended = new Extended(); + var implemented = new Closeable({ + close: function () { + calls.push("implemented"); + } + }); + + callFromJava(extended, "close"); + callFromJava(implemented, "close"); + expect(calls).toEqual(["extended", "implemented"]); + expect(String(implemented.getClass().getName())).toBe(String(extended.getClass().getName())); + expect(reportEntriesFor("com.tns.gen.java.lang.AutoCloseable").length).toBe(1); + }); + + it("keep each implementation when new and extend of a prebuilt interface are mixed", function () { + var calls = []; + var implementedBefore = new java.lang.Runnable({ + run: function () { + calls.push("new before"); + } + }); + var Extended = java.lang.Runnable.extend({ + run: function () { + calls.push("extend"); + } + }); + var extended = new Extended(); + var implementedAfter = new java.lang.Runnable({ + run: function () { + calls.push("new after"); + } + }); + + callFromJava(implementedAfter, "run"); + callFromJava(extended, "run"); + callFromJava(implementedBefore, "run"); + expect(calls).toEqual(["new after", "extend", "new before"]); + expect(extended.getClass().getName()).toBe("com.tns.gen.java.lang.Runnable"); + expect(implementedAfter.getClass().getName()).toBe("com.tns.gen.java.lang.Runnable"); + }); + + it("load the generated class of a nested interface", function () { + var calledF = false; + var Extended = com.tns.tests.ImplementInterfaceTest.I3.extend({ + f: function () { + calledF = true; + }, + g: function () {} + }); + var instance = new Extended(); + + com.tns.tests.ImplementInterfaceTest.triggerF(instance); + expect(calledF).toBe(true); + expect(instance.getClass().getName()).toBe("com.tns.gen.com.tns.tests.ImplementInterfaceTest_I3"); + }); + + it("work with a class generated in a worker", function (done) { + var worker = new Worker("./interfaceExtendWorker.js"); + worker.onmessage = function (msg) { + worker.terminate(); + expect(msg.data.error).toBeUndefined(); + expect(msg.data.extendedRun).toBe("worker extend"); + expect(msg.data.implementedRun).toBe("worker new"); + expect(msg.data.extendedClass).toBe("com.tns.gen.java.security.PrivilegedExceptionAction"); + expect(msg.data.implementedClass).toBe(msg.data.extendedClass); + done(); + }; + }); +}); diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index 41e0e797e..e8f7815aa 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -1948,7 +1948,11 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfo::New(isolate, *cachedData.extendedCtorFunction); info.GetReturnValue().Set(cachedExtendedCtorFunc); @@ -2000,7 +2004,14 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfoInstanceTemplate(), fullExtendedName, node); - cache->ExtendedCtorFuncCache.emplace(fullExtendedName, cacheData); + cache->ExtendedCtorFuncCache.emplace(extendedCtorKey, cacheData); + // CreateExtendedJSWrapper looks instances created from Java up by their + // class name; the first extend of the class keeps serving those. Each + // entry needs its own handles: the cache destructor frees them per entry. + if (extendedCtorKey != fullExtendedName && + cache->ExtendedCtorFuncCache.find(fullExtendedName) == cache->ExtendedCtorFuncCache.end()) { + cache->ExtendedCtorFuncCache.emplace(fullExtendedName, ExtendedClassCacheData(extendFunc, extendFuncTemplate->InstanceTemplate(), fullExtendedName, node)); + } if (frame.check()) { frame.log("Extending: " + node->m_name); diff --git a/test-app/runtime/src/main/java/com/tns/DexFactory.java b/test-app/runtime/src/main/java/com/tns/DexFactory.java index 592a6ea59..2c9eb73e0 100644 --- a/test-app/runtime/src/main/java/com/tns/DexFactory.java +++ b/test-app/runtime/src/main/java/com/tns/DexFactory.java @@ -104,12 +104,27 @@ private static void recordRuntimeGeneratedClass(String requestedName, Class l public Class resolveClass(String baseClassName, String name, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException { String fullClassName = className.replace("$", "_"); + + // A named proxy (`Base.extend('a.b.C', {...})` / @JavaProxy) asks for + // exactly that Java class name; the substitutions below are for the + // anonymous form only, where the name is derived from the base. + boolean isNamedProxy = !fullClassName.startsWith(COM_TNS_GEN_PREFIX) && fullClassName.contains("."); + + // An anonymous extend of an interface asks for a per-call-site name, but + // it gets the class `new Interface({...})` uses, whose body depends only + // on the interface. Resolving it under that name lets the APK's prebuilt + // class and the dex cache serve every call site. Extra implemented + // interfaces add methods the shared class lacks. + boolean usesSharedInterfaceClass = !baseClassName.isEmpty() && isInterface && !isNamedProxy && !hasImplementedInterfaces(implementedInterfaces); + if (usesSharedInterfaceClass) { + fullClassName = COM_TNS_GEN_PREFIX + this.getClassToProxyName(baseClassName).replace("$", "_"); + } String originalFullClassName = fullClassName; // The static binding generator keeps a `$` from the script's file name in an // anonymous extend's class name; the runtime already replaced the base class's `$`. Class pregeneratedClass = null; - if (!fullClassName.equals(className)) { + if (!usesSharedInterfaceClass && !fullClassName.equals(className)) { pregeneratedClass = loadPregeneratedClass(className.replace("-", "_")); } if (pregeneratedClass == null) { @@ -137,15 +152,10 @@ public Class resolveClass(String baseClassName, String name, String className // strip the `com.tns.gen` off the base extended class name String desiredDexClassName = this.getClassToProxyName(fullClassName); - // A named proxy (`Base.extend('a.b.C', {...})` / @JavaProxy) asks for - // exactly that Java class name; the substitutions below are for the - // anonymous form only, where the name is derived from the base. - boolean isNamedProxy = !fullClassName.startsWith(COM_TNS_GEN_PREFIX) && fullClassName.contains("."); - // when interfaces are extended as classes, we still want to preserve // just the interface name without the extra file, line, column information if (!baseClassName.isEmpty() && isInterface && !isNamedProxy) { - fullClassName = COM_TNS_GEN_PREFIX + classToProxy; + fullClassName = COM_TNS_GEN_PREFIX + classToProxy.replace("$", "_"); } // The thumb only changes on reinstall, so a cache key of name + thumb @@ -273,6 +283,17 @@ public static String strJoin(String[] array, String separator) { return sbStr.toString(); } + private static boolean hasImplementedInterfaces(String[] implementedInterfaces) { + if (implementedInterfaces != null) { + for (String implementedInterface : implementedInterfaces) { + if (implementedInterface != null && !implementedInterface.isEmpty()) { + return true; + } + } + } + return false; + } + private Class loadPregeneratedClass(String name) { try { if (logger.isEnabled()) { From 832cdc8324bbf2e3083cd8eb7985d254c55e6727 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 18:58:40 -0300 Subject: [PATCH 03/14] fix(DexFactory): share and safely write cached interface dexes The class generated for an anonymous interface implementation has every abstract method of the interface regardless of which methods the JS object defines, so its method overrides no longer feed the dex cache key. Call sites, runtimes and process starts implementing the same interface with different method sets now share one cached dex instead of each writing its own. Write the dex and jar under a unique temporary name in the same directory and rename them into place, so a process killed mid-write cannot leave a truncated file that a later start trusts because it exists. The jar is made read-only before it is published. --- .../app/tests/testInterfaceExtendClasses.js | 28 +++++++++ .../java/com/tns/bindings/ProxyGenerator.java | 27 +++++++-- .../src/main/java/com/tns/DexFactory.java | 60 ++++++++++++++----- 3 files changed, 96 insertions(+), 19 deletions(-) diff --git a/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js index ca5ca477e..ba6ebe4ed 100644 --- a/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js +++ b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js @@ -165,4 +165,32 @@ describe("Anonymous extends of an interface", function () { done(); }; }); + + it("reuse the cached class of an interface whose implementations define different methods", function (done) { + var worker = new Worker("./interfaceExtendWorker.js"); + worker.onmessage = function () { + worker.terminate(); + // Only the worker has loaded this class, so this runtime has to load + // the dex the worker cached, although this object defines more methods. + // An extend, unlike new Action({...}), cannot reuse the class the + // worker left in the process-wide JNI class cache. + var Action = java.security.PrivilegedExceptionAction; + var Extended = Action[ext]({ + run: function () { + return "main extend"; + }, + describe: function () { + return "not a Java method"; + } + }); + + expect(String(callFromJava(new Extended(), "run"))).toBe("main extend"); + var entries = reportEntriesFor("com.tns.gen.java.security.PrivilegedExceptionAction").filter(function (entry) { + return entry.requested.indexOf("_tests_testInterfaceExtendClasses_") > 0; + }); + expect(entries.length).toBe(1); + expect(entries[0].source).toBe("cached"); + done(); + }; + }); }); diff --git a/test-app/runtime-binding-generator/src/main/java/com/tns/bindings/ProxyGenerator.java b/test-app/runtime-binding-generator/src/main/java/com/tns/bindings/ProxyGenerator.java index 1cbfc8b0f..f886a9507 100644 --- a/test-app/runtime-binding-generator/src/main/java/com/tns/bindings/ProxyGenerator.java +++ b/test-app/runtime-binding-generator/src/main/java/com/tns/bindings/ProxyGenerator.java @@ -78,17 +78,34 @@ public String generateProxy(String proxyName, String cacheDigest, ClassDescripto return proxyPath; } + /** + * The dex only appears under its final name once complete: DexFactory + * trusts any dex that exists, so a process killed mid-write must not leave + * a truncated one there. The temp name keeps the proxy's thumb so the + * thumb purge also removes orphans, and is unique because several runtimes + * can generate the same proxy at once. + */ private String saveProxy(String proxyName, byte[] proxyBytes) throws IOException { File file = new File(path + File.separator + proxyName + ".dex"); File parentDir = file.getParentFile(); if (parentDir != null && !parentDir.exists()) { parentDir.mkdirs(); } - file.createNewFile(); - FileOutputStream stream = new FileOutputStream(file); - stream.write(proxyBytes); - stream.flush(); - stream.close(); + File tempFile = File.createTempFile(file.getName() + ".", ".tmp", parentDir); + try { + FileOutputStream stream = new FileOutputStream(tempFile); + try { + stream.write(proxyBytes); + stream.flush(); + } finally { + stream.close(); + } + if (!tempFile.renameTo(file)) { + throw new IOException("Could not move " + tempFile + " to " + file); + } + } finally { + tempFile.delete(); + } return file.getAbsolutePath(); } diff --git a/test-app/runtime/src/main/java/com/tns/DexFactory.java b/test-app/runtime/src/main/java/com/tns/DexFactory.java index 2c9eb73e0..a52574536 100644 --- a/test-app/runtime/src/main/java/com/tns/DexFactory.java +++ b/test-app/runtime/src/main/java/com/tns/DexFactory.java @@ -11,6 +11,7 @@ import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -161,8 +162,12 @@ public Class resolveClass(String baseClassName, String name, String className // The thumb only changes on reinstall, so a cache key of name + thumb // cannot see an edit to the proxy's contents: under HMR a named // proxy's new method overrides would silently load the previous dex. - // The digest carries the contents into the file name. - String contentDigest = computeContentDigest(classToProxy, methodOverrides, implementedInterfaces, isInterface); + // The digest carries the contents into the file name. An anonymous + // interface implementation gets every abstract method whatever the JS + // object defines, so its overrides stay out of the key and all its + // call sites share one cached dex. + String[] digestedOverrides = isInterface && !isNamedProxy ? null : methodOverrides; + String contentDigest = computeContentDigest(classToProxy, digestedOverrides, implementedInterfaces, isInterface); File dexFile = this.getDexFile(desiredDexClassName, contentDigest); boolean generated = dexFile == null; @@ -194,18 +199,7 @@ public Class resolveClass(String baseClassName, String name, String className File jarFile = new File(jarFilePath); if (!jarFile.exists()) { - FileOutputStream jarFileStream = new FileOutputStream(jarFile); - ZipOutputStream out = new ZipOutputStream(jarFileStream); - - out.putNextEntry(new ZipEntry("classes.dex")); - byte[] dexData = new byte[(int) dexFile.length()]; - FileInputStream fi = new FileInputStream(dexFile); - fi.read(dexData, 0, dexData.length); - fi.close(); - - out.write(dexData); - out.closeEntry(); - out.close(); + writeJar(dexFile, jarFile); } jarFile.setReadOnly(); @@ -283,6 +277,44 @@ public static String strJoin(String[] array, String separator) { return sbStr.toString(); } + /** + * The jar only appears under its final name once complete and read-only: + * its existence is the cache hit, so a process killed mid-write must not + * leave a truncated one there. The temp name keeps the jar's thumb so + * purgeDexesByThumb also removes orphans, and is unique because several + * runtimes can resolve the same class at once. + */ + private void writeJar(File dexFile, File jarFile) throws IOException { + byte[] dexData = new byte[(int) dexFile.length()]; + DataInputStream in = new DataInputStream(new FileInputStream(dexFile)); + try { + in.readFully(dexData); + } finally { + in.close(); + } + + File tempFile = File.createTempFile(jarFile.getName() + ".", ".tmp", jarFile.getParentFile()); + try { + ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tempFile)); + try { + out.putNextEntry(new ZipEntry("classes.dex")); + out.write(dexData); + out.closeEntry(); + } finally { + out.close(); + } + tempFile.setReadOnly(); + + // A runtime that finished first already published identical contents, + // and may have a class loader on that file. + if (!jarFile.exists() && !tempFile.renameTo(jarFile) && !jarFile.exists()) { + throw new IOException("Could not move " + tempFile + " to " + jarFile); + } + } finally { + tempFile.delete(); + } + } + private static boolean hasImplementedInterfaces(String[] implementedInterfaces) { if (implementedInterfaces != null) { for (String implementedInterface : implementedInterfaces) { From c9937b3a71edd21989009511749266a84bc1d1f1 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 19:30:46 -0300 Subject: [PATCH 04/14] feat(sbg): prebuild bindings for the app module's own Java classes The static binding generator runs before javac and only loads classes from android.jar and the dependency jars, so it dropped every JS subclass or implementation of a class or interface declared in the app module itself (App_Resources/Android/src/main/java), and all of them were generated and dexed on the device instead. A new compileAppClassesForSbg task compiles the module's Java sources, minus the generator's own output, into build/nativescript/sbg-app-classes with the JDK Gradle runs on, at the app's compileOptions levels. runSbg appends that directory to the generator's dependency list; the metadata generator's list is unchanged. The pass is best-effort: javac writes no class files after its first error, so sources that report errors (they need Kotlin, annotation processors or generated code) are left out and the rest recompiled, and any failure logs one line and leaves the generator as before. -PskipSbgAppClasses turns it off. GetInterfaceNames reads class directories too, so the JS parser recognises new App.Interface({...}) call sites, and reading a class directory skips missing directories and unparsable class files. DexFactory.getRuntimeGeneratedClasses() keeps each entry once, so runtimes resolving the same classes cannot grow it without bound. The runtime binding generator specs hide their call sites from the static binding generator so they keep exercising runtime generation, and new specs cover app-module types both prebuilt and generated at runtime, in the main runtime and in a worker. --- test-app/app/build.gradle | 126 ++++++++++++++++++ test-app/app/src/main/assets/app/mainpage.js | 1 + .../app/tests/appModuleBindingsWorker.js | 31 +++++ .../assets/app/tests/testAppModuleBindings.js | 123 +++++++++++++++++ .../tests/testsForRuntimeBindingGenerator.js | 9 +- .../com/tns/tests/AppModuleBindingTarget.java | 25 ++++ .../GetInterfaceNames.java | 43 +++++- .../files/impl/FileSystemHelperImpl.java | 4 + .../test/GetInterfaceNamesTest.java | 61 +++++++++ .../src/main/java/com/tns/DexFactory.java | 11 +- 10 files changed, 424 insertions(+), 10 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js create mode 100644 test-app/app/src/main/assets/app/tests/testAppModuleBindings.js create mode 100644 test-app/app/src/main/java/com/tns/tests/AppModuleBindingTarget.java create mode 100644 test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/GetInterfaceNamesTest.java diff --git a/test-app/app/build.gradle b/test-app/app/build.gradle index 9e0245f29..f7e68d069 100644 --- a/test-app/app/build.gradle +++ b/test-app/app/build.gradle @@ -18,6 +18,7 @@ * -PandroidXMaterial=[androidx_material_version] * -PappPath=[app_path] * -PappResourcesPath=[app_resources_path] +* -PskipSbgAppClasses //don't show the app's own Java classes to the static binding generator */ import groovy.json.JsonSlurper @@ -25,12 +26,17 @@ import groovy.xml.XmlSlurper import org.apache.commons.io.FileUtils import javax.inject.Inject +import java.nio.charset.Charset +import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Paths import java.nio.file.StandardCopyOption import java.security.MessageDigest import java.util.jar.JarEntry import java.util.jar.JarFile +import javax.tools.Diagnostic +import javax.tools.DiagnosticCollector +import javax.tools.ToolProvider import static org.gradle.internal.logging.text.StyledTextOutput.Style @@ -488,8 +494,121 @@ if (failOnCompilationWarningsEnabled()) { ///////////////////////////// EXECUTION PHASE ///////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// +def SBG_APP_CLASSES_DIR = layout.buildDirectory.dir("nativescript/sbg-app-classes") +def sbgAppClassesEnabled = { -> + !project.hasProperty("skipSbgAppClasses") || project.property("skipSbgAppClasses").toString() == "false" +} + +// The static binding generator runs before javac, so on its own it cannot see the +// classes and interfaces declared in this module's Java sources (App_Resources) and +// leaves every JS subclass of them to be generated on the device. This compiles them +// for it alone. Best-effort: sources that only compile together with the rest of the +// build (Kotlin, annotation processors, generated code) are left out, and any failure +// leaves the generator exactly as it would be without this task. +tasks.register("compileAppClassesForSbg") { + dependsOn "collectAllJars" + onlyIf { sbgAppClassesEnabled() } + + def classesDir = SBG_APP_CLASSES_DIR + def dependenciesFile = file("$BUILD_TOOLS_PATH/$SBG_JAVA_DEPENDENCIES") + // The first line of the static binding generator's Generator.AUTO_GENERATED_FILE_PROLOGUE. + // It writes named classes (`extend("a.b.C", ...)`, @JavaProxy) outside com/tns/gen too. + def sbgSourcePrefix = "/* AUTO-GENERATED FILE. DO NOT MODIFY.".getBytes(StandardCharsets.UTF_8) + def sources = fileTree(OUTPUT_JAVA_DIR) { + include "**/*.java" + exclude "com/tns/gen/**" + }.filter { File source -> + try { + !source.withInputStream { Arrays.equals(it.readNBytes(sbgSourcePrefix.length), sbgSourcePrefix) } + } catch (IOException ignored) { + true + } + } + def sourceLevel = provider { android.compileOptions.sourceCompatibility.toString() } + def targetLevel = provider { android.compileOptions.targetCompatibility.toString() } + def sourceEncoding = provider { android.compileOptions.encoding ?: "UTF-8" } + + inputs.files(sources).skipWhenEmpty().withPathSensitivity(PathSensitivity.RELATIVE).withPropertyName("sources") + inputs.files(provider { + dependenciesFile.exists() ? dependenciesFile.readLines().findAll { it } : [] + }).withPropertyName("classpath") + inputs.property("sourceLevel", sourceLevel) + inputs.property("targetLevel", targetLevel) + inputs.property("sourceEncoding", sourceEncoding) + outputs.dir(classesDir).withPropertyName("classes") + + doLast { + def outDir = classesDir.get().asFile + outDir.deleteDir() + outDir.mkdirs() + + def remaining = sources.files.sort() + def total = remaining.size() + def leftOut = [] + def firstError = null + try { + def compiler = ToolProvider.getSystemJavaCompiler() + if (compiler == null) { + logger.warn("compileAppClassesForSbg: ${System.getProperty('java.home')} has no Java compiler, so the app's own classes get no prebuilt bindings") + return + } + def options = ["-proc:none", "-nowarn", + "-source", sourceLevel.get(), "-target", targetLevel.get(), + "-encoding", sourceEncoding.get(), + "-classpath", dependenciesFile.readLines().findAll { it }.join(File.pathSeparator), + "-d", outDir.absolutePath] + + // javac writes no class files after its first error, so each failing round + // leaves out the sources that reported errors and compiles the rest again. + // A round that cannot leave any source out ends the loop. + def compiled = false + while (!remaining.isEmpty() && !compiled) { + def diagnostics = new DiagnosticCollector() + def fileManager = compiler.getStandardFileManager(diagnostics, Locale.ROOT, Charset.forName(sourceEncoding.get())) + try { + def units = fileManager.getJavaFileObjectsFromFiles(remaining).toList() + def sourceByUri = [units, remaining].transpose().collectEntries { unit, source -> [(unit.toUri()): source] } + compiled = compiler.getTask(new StringWriter(), fileManager, diagnostics, options, null, units).call() + if (!compiled) { + def errors = diagnostics.diagnostics.findAll { it.kind == Diagnostic.Kind.ERROR } + if (firstError == null && !errors.isEmpty()) { + firstError = errors[0].getMessage(Locale.ROOT).readLines().take(2).join(" ").replaceAll(/\s+/, " ") + } + def failing = errors.collect { it.source == null ? null : sourceByUri[it.source.toUri()] }.toSet() + if (failing.isEmpty() || failing.contains(null)) { + break + } + remaining.removeAll(failing) + leftOut.addAll(failing) + } + } finally { + fileManager.close() + } + if (!compiled) { + outDir.deleteDir() + outDir.mkdirs() + } + } + + if (!compiled && !remaining.isEmpty()) { + logger.warn("compileAppClassesForSbg: the app's Java sources did not compile on their own, so the app's own classes get no prebuilt bindings (${firstError})") + } else if (!leftOut.isEmpty()) { + def names = leftOut.collect { it.name }.sort() + logger.info("compileAppClassesForSbg: ${leftOut.size()} of ${total} Java sources get no prebuilt bindings because they only compile with the rest of the build: ${names.take(5).join(', ')}${names.size() > 5 ? ', ...' : ''} (${firstError})") + } + } catch (Exception | LinkageError e) { + outDir.deleteDir() + outDir.mkdirs() + logger.warn("compileAppClassesForSbg: the app's own classes get no prebuilt bindings: ${e}") + } + } +} + task runSbg(type: BuildToolTask) { dependsOn "collectAllJars" + dependsOn "compileAppClassesForSbg" + inputs.files(tasks.named("compileAppClassesForSbg")).withPropertyName("appClasses") + inputs.property("sbgAppClassesEnabled", provider { sbgAppClassesEnabled() }) def rootPath = "" if (!findProject(':static-binding-generator').is(null)) { rootPath = Paths.get(project(':static-binding-generator').projectDir.path, "build/libs").toString() @@ -514,8 +633,15 @@ task runSbg(type: BuildToolTask) { args paramz + def sbgDependenciesFile = file("$BUILD_TOOLS_PATH/$SBG_JAVA_DEPENDENCIES") doFirst { new File("$OUTPUT_JAVA_DIR/com/tns/gen").deleteDir() + + // Not written by collectAllJars: the metadata generator reads the real compiled classes. + def appClassesDir = SBG_APP_CLASSES_DIR.get().asFile + if (sbgAppClassesEnabled() && appClassesDir.isDirectory()) { + sbgDependenciesFile.withWriterAppend { it.println(appClassesDir.absolutePath) } + } } } diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index dc9308378..8d3450e47 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -75,6 +75,7 @@ require("./tests/testClassForNameDiscovery"); require("./tests/testPrebuiltExtendClasses"); require("./tests/testRuntimeGenerationReport"); require("./tests/testInterfaceExtendClasses"); +require("./tests/testAppModuleBindings"); require("./tests/testProxyRegistrationGc"); require("./tests/testInterfaceImplementation"); require("./tests/testPrimitiveCallbackReturns"); diff --git a/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js b/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js new file mode 100644 index 000000000..4e840dd7f --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js @@ -0,0 +1,31 @@ +// Entry for testAppModuleBindings: every call is hidden from the static binding +// generator, so this worker's runtime generates the classes. +var ext = "ex" + "tend"; +var Target = com.tns.tests.AppModuleBindingTarget.WorkerRuntimeGenerated; +var result = {}; + +function callFromJava(instance, methodName) { + return String(instance.getClass().getMethod(methodName, []).invoke(instance, [])); +} + +try { + var Extended = com.tns.tests.Button1[ext]({ + toString: function () { + return "worker extend"; + } + }); + var extended = new Extended(); + result.extendedClass = String(extended.getClass().getName()); + result.extendedCall = callFromJava(extended, "toString"); + + var implemented = new Target({ + describe: function () { + return "worker new"; + } + }); + result.implementedClass = String(implemented.getClass().getName()); + result.implementedCall = callFromJava(implemented, "describe"); +} catch (e) { + result.error = String(e && e.message || e); +} +postMessage(result); diff --git a/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js b/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js new file mode 100644 index 000000000..c21477ef6 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js @@ -0,0 +1,123 @@ +describe("Bindings of app-module types", function () { + // A computed member hides an extend from the static binding generator. + var ext = "ex" + "tend"; + + // Reflection makes Java, not JS, call the implementation. + function callFromJava(instance, methodName) { + return String(instance.getClass().getMethod(methodName, []).invoke(instance, [])); + } + + function reportEntriesFor(loadedName) { + var lines = com.tns.DexFactory.getRuntimeGeneratedClasses(); + var result = []; + for (var i = 0; i < lines.length; i++) { + var fields = String(lines[i]).split(" "); + if (fields[2] === String(loadedName)) { + result.push({ source: fields[0], kind: fields[1], loaded: fields[2], requested: fields[3] }); + } + } + return result; + } + + describe("App-module types prebuilt by the static binding generator", function () { + it("prebuilds an extend of an app-module class", function () { + var Extended = com.tns.tests.Button1.extend({ + toString: function () { + return "prebuilt extend"; + } + }); + var instance = new Extended(); + + expect(callFromJava(instance, "toString")).toBe("prebuilt extend"); + expect(instance instanceof com.tns.tests.Button1).toBe(true); + expect(reportEntriesFor(instance.getClass().getName())).toEqual([]); + }); + + it("prebuilds new of an app-module interface", function () { + var implemented = new com.tns.tests.AppModuleBindingTarget({ + describe: function () { + return "prebuilt new"; + } + }); + + expect(callFromJava(implemented, "describe")).toBe("prebuilt new"); + expect(String(implemented.getClass().getName())).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget"); + expect(reportEntriesFor(implemented.getClass().getName())).toEqual([]); + }); + + it("prebuilds new of a nested app-module interface", function () { + var implemented = new com.tns.tests.AppModuleBindingTarget.Nested({ + describe: function () { + return "prebuilt nested"; + } + }); + + expect(callFromJava(implemented, "describe")).toBe("prebuilt nested"); + expect(String(implemented.getClass().getName())).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget_Nested"); + expect(reportEntriesFor(implemented.getClass().getName())).toEqual([]); + }); + }); + + describe("App-module types generated at runtime", function () { + it("generates a hidden extend of an app-module class", function () { + var Extended = com.tns.tests.Button1[ext]({ + toString: function () { + return "runtime extend"; + } + }); + var instance = new Extended(); + + expect(callFromJava(instance, "toString")).toBe("runtime extend"); + expect(instance instanceof com.tns.tests.Button1).toBe(true); + var entries = reportEntriesFor(instance.getClass().getName()); + expect(entries.length).toBe(1); + expect(entries[0].kind).toBe("class"); + expect(entries[0].requested).toMatch(/^com\.tns\.gen\.com\.tns\.tests\.Button1_/); + }); + + it("generates a hidden new of an app-module interface", function () { + var Target = com.tns.tests.AppModuleBindingTarget.RuntimeGenerated; + var implemented = new Target({ + describe: function () { + return "runtime new"; + } + }); + + expect(callFromJava(implemented, "describe")).toBe("runtime new"); + expect(String(implemented.getClass().getName())).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget_RuntimeGenerated"); + var entries = reportEntriesFor(implemented.getClass().getName()); + expect(entries.length).toBe(1); + expect(entries[0].kind).toBe("interface"); + }); + + it("generates a hidden extend of a nested app-module interface", function () { + var Target = com.tns.tests.AppModuleBindingTarget.ExtendedAtRuntime; + var Extended = Target[ext]({ + describe: function () { + return "runtime interface extend"; + } + }); + var instance = new Extended(); + + expect(callFromJava(instance, "describe")).toBe("runtime interface extend"); + expect(String(instance.getClass().getName())).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget_ExtendedAtRuntime"); + var entries = reportEntriesFor(instance.getClass().getName()); + expect(entries.length).toBe(1); + expect(entries[0].kind).toBe("interface"); + }); + + it("generates app-module types in a worker", function (done) { + var worker = new Worker("./appModuleBindingsWorker.js"); + worker.onmessage = function (msg) { + worker.terminate(); + expect(msg.data.error).toBeUndefined(); + expect(msg.data.extendedCall).toBe("worker extend"); + expect(msg.data.implementedCall).toBe("worker new"); + expect(msg.data.implementedClass).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget_WorkerRuntimeGenerated"); + expect(reportEntriesFor(msg.data.extendedClass).length).toBe(1); + expect(reportEntriesFor(msg.data.implementedClass).length).toBe(1); + done(); + }; + }); + }); +}); diff --git a/test-app/app/src/main/assets/app/tests/testsForRuntimeBindingGenerator.js b/test-app/app/src/main/assets/app/tests/testsForRuntimeBindingGenerator.js index 3c6c82685..ff260e7e4 100644 --- a/test-app/app/src/main/assets/app/tests/testsForRuntimeBindingGenerator.js +++ b/test-app/app/src/main/assets/app/tests/testsForRuntimeBindingGenerator.js @@ -1,4 +1,6 @@ describe("Tests for runtime binding generator", function () { + // Computed members and aliases keep these call sites from the static binding + // generator, so their classes are generated at runtime. var myCustomEquality = function(first, second) { return first == second; @@ -12,7 +14,7 @@ describe("Tests for runtime binding generator", function () { __log("TEST: When_generating_a_proxy_of_nested_interface_at_runtime"); - var MyButton = com.tns.tests.Button1.extend("MyButton10", { + var MyButton = com.tns.tests.Button1["extend"]("MyButton10", { toString : function() { return "button1"; }, @@ -36,7 +38,7 @@ describe("Tests for runtime binding generator", function () { __log("TEST: When_generating_a_proxy_of_android_class_at_runtime"); - var MyButton = com.tns.tests.Button1.extend("MyButton36", { + var MyButton = com.tns.tests.Button1["extend"]("MyButton36", { toString : function() { return "button1"; }, @@ -50,7 +52,8 @@ describe("Tests for runtime binding generator", function () { }); it("When_implementing_interface_and_its_implemented_interfaces", function() { - var impl = new com.tns.tests.MyDerivedPublicInterface({ + var DerivedInterface = com.tns.tests.MyDerivedPublicInterface; + var impl = new DerivedInterface({ methodOfMyDerivedPublicInterface: function(input) { return "Le java " + input; }, diff --git a/test-app/app/src/main/java/com/tns/tests/AppModuleBindingTarget.java b/test-app/app/src/main/java/com/tns/tests/AppModuleBindingTarget.java new file mode 100644 index 000000000..8c64691a4 --- /dev/null +++ b/test-app/app/src/main/java/com/tns/tests/AppModuleBindingTarget.java @@ -0,0 +1,25 @@ +package com.tns.tests; + +// Fixtures for testAppModuleBindings.js. Each interface has a single kind of JS +// implementation: RuntimeGenerated, ExtendedAtRuntime and WorkerRuntimeGenerated +// only through calls hidden from the static binding generator, so their classes +// are never prebuilt. +public interface AppModuleBindingTarget { + String describe(); + + interface Nested { + String describe(); + } + + interface RuntimeGenerated { + String describe(); + } + + interface ExtendedAtRuntime { + String describe(); + } + + interface WorkerRuntimeGenerated { + String describe(); + } +} diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/GetInterfaceNames.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/GetInterfaceNames.java index dbf0250b5..90c9faa85 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/GetInterfaceNames.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/GetInterfaceNames.java @@ -27,23 +27,60 @@ public static void generateInterfaceFile(List rows) currentDir = System.getProperty("user.dir"); String outputFileName = Main.SBG_INTERFACE_NAMES; + List interfacesList = collectInterfaceNames(rows); + PrintWriter out = ensureOutputFile(outputFileName); + for (String line : interfacesList) { + out.println(line); + } + + out.close(); + } + + /** + * The interfaces declared in the given dependencies: jars, and directories of + * class files. Nested interfaces are named with '.' separators. + */ + public static List collectInterfaceNames(List rows) { List interfacesList = Collections.synchronizedList(new ArrayList<>()); final ThrowingConsumer consumer = dr -> { String pathToDependency = dr.getRow(); if (pathToDependency.endsWith(".jar")) { generateInterfaceNames(pathToDependency, interfacesList); + } else { + generateInterfaceNamesFromDirectory(new File(pathToDependency), interfacesList); } }; rows.parallelStream().forEach(consumer); - for (String line : interfacesList) { - out.println(line); + return interfacesList; + } + + // Class directories hold the app's own classes compiled on a best-effort basis + // before the build's real compilation, so an unreadable entry is skipped + // rather than failing the build. + private static void generateInterfaceNamesFromDirectory(File dir, List interfacesList) { + File[] entries = dir.listFiles(); + if (entries == null) { + return; } - out.close(); + for (File entry : entries) { + if (entry.isDirectory()) { + generateInterfaceNamesFromDirectory(entry, interfacesList); + } else if (entry.getName().endsWith(CLASS_EXT)) { + try { + JavaClass clazz = new ClassParser(entry.getAbsolutePath()).parse(); + if (clazz.isInterface()) { + interfacesList.add(clazz.getClassName().replace('$', '.')); + } + } catch (IOException | RuntimeException e) { + System.out.println("Skipping unreadable class file " + entry.getAbsolutePath() + ": " + e); + } + } + } } private static void generateInterfaceNames(String pathToJar, List interfacesList) { diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/files/impl/FileSystemHelperImpl.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/files/impl/FileSystemHelperImpl.java index 899aa269d..f06144eea 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/files/impl/FileSystemHelperImpl.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/files/impl/FileSystemHelperImpl.java @@ -122,6 +122,9 @@ public ClassesCollection readClassesFromDirectory(String directoryPath) { while (!d.isEmpty()) { File cur = d.pollFirst(); File[] files = cur.listFiles(); + if (files == null) { + continue; + } for (File f : files) { if (f.isFile() && f.getName().endsWith(CLASS_EXT)) { ClassParser cp = new ClassParser(f.getAbsolutePath()); @@ -133,6 +136,7 @@ public ClassesCollection readClassesFromDirectory(String directoryPath) { throw new RuntimeException("Error while parsing class file!", e); } else { e.printStackTrace(); + continue; } } String name = clazz.getClassName(); diff --git a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/GetInterfaceNamesTest.java b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/GetInterfaceNamesTest.java new file mode 100644 index 000000000..ae163805b --- /dev/null +++ b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/GetInterfaceNamesTest.java @@ -0,0 +1,61 @@ +package org.nativescript.staticbindinggenerator.test; + +import com.example.MyClass; +import com.example.MyInterface; + +import org.apache.commons.io.FileUtils; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.nativescript.staticbindinggenerator.DataRow; +import org.nativescript.staticbindinggenerator.GetInterfaceNames; + +import java.io.File; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; + +public class GetInterfaceNamesTest { + public interface NestedInterface { + void run(); + } + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void testCollectsInterfacesFromAClassDirectory() throws Exception { + File classesDir = temporaryFolder.newFolder("classes"); + copyClassFile(MyInterface.class, classesDir); + copyClassFile(MyClass.class, classesDir); + copyClassFile(NestedInterface.class, classesDir); + FileUtils.writeStringToFile(new File(classesDir, "com/example/NotAClass.class"), "garbage", StandardCharsets.UTF_8); + + List names = GetInterfaceNames.collectInterfaceNames(Collections.singletonList(new DataRow(classesDir.getAbsolutePath()))); + + Assert.assertEquals( + new HashSet<>(Arrays.asList( + "com.example.MyInterface", + "org.nativescript.staticbindinggenerator.test.GetInterfaceNamesTest.NestedInterface")), + new HashSet<>(names)); + } + + @Test + public void testIgnoresAMissingClassDirectory() { + File missingDir = new File(temporaryFolder.getRoot(), "missing"); + + List names = GetInterfaceNames.collectInterfaceNames(Collections.singletonList(new DataRow(missingDir.getAbsolutePath()))); + + Assert.assertTrue(names.isEmpty()); + } + + private static void copyClassFile(Class clazz, File classesDir) throws URISyntaxException, java.io.IOException { + String relativePath = clazz.getName().replace('.', '/') + ".class"; + File source = new File(clazz.getResource('/' + relativePath).toURI()); + FileUtils.copyFile(source, new File(classesDir, relativePath)); + } +} diff --git a/test-app/runtime/src/main/java/com/tns/DexFactory.java b/test-app/runtime/src/main/java/com/tns/DexFactory.java index a52574536..ff2660c13 100644 --- a/test-app/runtime/src/main/java/com/tns/DexFactory.java +++ b/test-app/runtime/src/main/java/com/tns/DexFactory.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -79,8 +80,9 @@ public class DexFactory { static long totalMultiDexTime = 0; static long totalLoadDexTime = 0; - // Process-wide: every runtime (main and workers) has its own DexFactory. - private static final ArrayList runtimeGeneratedClasses = new ArrayList(); + // Process-wide: every runtime (main and workers) has its own DexFactory, and + // each one resolves the same classes again, so entries are kept unique. + private static final LinkedHashSet runtimeGeneratedClasses = new LinkedHashSet(); /** * The classes this process resolved without a prebuilt class from the APK, @@ -95,10 +97,11 @@ public static String[] getRuntimeGeneratedClasses() { private static void recordRuntimeGeneratedClass(String requestedName, Class loadedClass, boolean isInterface, boolean generated) { String entry = (generated ? "generated" : "cached") + " " + (isInterface ? "interface" : "class") + " " + loadedClass.getName() + " " + requestedName; + boolean added; synchronized (runtimeGeneratedClasses) { - runtimeGeneratedClasses.add(entry); + added = runtimeGeneratedClasses.add(entry); } - if (com.tns.Runtime.isDebuggable()) { + if (added && com.tns.Runtime.isDebuggable()) { Log.i("JS", (generated ? "Generated class at runtime: " : "Loaded runtime-generated class from the dex cache: ") + loadedClass.getName() + " (requested as " + requestedName + ")"); } } From 1b3e85a55cc8f3dd8b2643bb26c1e932e830ba93 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 20:30:54 -0300 Subject: [PATCH 05/14] fix(jsparser): list the members the runtime sees on an implementation object The member names the parser writes for an extend are the overrides the static binding generator writes into the class, and they should be the implementation object's own data properties, which is what the runtime reads when it generates the same class. - Object literals: include quoted keys and skip getters and setters, `__proto__: value`, and keys that cannot name a Java method. - Compiled TypeScript classes: collect only `X.prototype.name = ...` assignments of the class `__extends` declares, including chained and comma-joined ones, instead of every assignment to a member expression. Statics such as `X.newInstance = function () {...}` are no longer listed as overrides, and computed members no longer add `undefined`. - List each name once. - Find the `_super.apply` location in a constructor that starts with `var _this = _super !== null && _super.apply(this, arguments) || this;`, so such classes get a call-site name instead of none. The test app's generated bindings are unchanged. --- .../cases/content_members/app/members.js | 54 ++++++ .../content_members/internal/ts_helpers.js | 175 ++++++++++++++++++ .../tests/specs/ast-parser-tests.spec.js | 36 ++++ .../jsparser/visitors/es5-visitors.js | 175 ++++++++++++------ 4 files changed, 386 insertions(+), 54 deletions(-) create mode 100644 test-app/build-tools/jsparser/tests/cases/content_members/app/members.js create mode 100644 test-app/build-tools/jsparser/tests/cases/content_members/internal/ts_helpers.js diff --git a/test-app/build-tools/jsparser/tests/cases/content_members/app/members.js b/test-app/build-tools/jsparser/tests/cases/content_members/app/members.js new file mode 100644 index 000000000..007cb2492 --- /dev/null +++ b/test-app/build-tools/jsparser/tests/cases/content_members/app/members.js @@ -0,0 +1,54 @@ +var other = { spreadMember: function () {} }; +var computedName = "computedMember"; + +var LiteralMembers = java.lang.Object.extend("LiteralMembers", { + identifierMember: function () {}, + "quotedMember": function () {}, + "quoted-invalid": function () {}, + shorthandMember() {}, + dataMember: 1, + get getterMember() { return 1; }, + set setterMember(value) {}, + [computedName]: function () {}, + ...other, + 42: function () {}, + __proto__: null, + identifierMember: function () {}, + interfaces: [java.lang.Runnable], + run: function () {} +}); + +var PrototypeMembers = /** @class */ (function (_super) { + __extends(PrototypeMembers, _super); + function PrototypeMembers() { + var _this = _super.call(this) || this; + _this.instanceField = null; + return global.__native(_this); + } + PrototypeMembers.newInstance = function () { return new PrototypeMembers(); }; + PrototypeMembers.staticField = 1; + PrototypeMembers.prototype.toString = function () { return "members"; }; + PrototypeMembers.prototype["quotedPrototypeMember"] = function () {}; + PrototypeMembers.prototype.chainedA = PrototypeMembers.prototype.chainedB = function () {}; + PrototypeMembers.prototype.sequenceA = function () {}, PrototypeMembers.prototype.sequenceB = 1; + PrototypeMembers.prototype.toString = function () { return "redefined"; }; + Object.defineProperty(PrototypeMembers.prototype, "accessorMember", { + get: function () { return 1; }, + enumerable: true, + configurable: true + }); + __decorate([someDecorator], PrototypeMembers.prototype, "toString", null); + PrototypeMembers = __decorate([Interfaces([java.lang.Runnable])], PrototypeMembers); + return PrototypeMembers; +}(java.lang.Object)); + +var ApplyInDeclaration = function (_super) { + __extends(ApplyInDeclaration, _super); + function ApplyInDeclaration() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.count = 0; + return _this; + } + ApplyInDeclaration.prototype.toString = function () { return "apply"; }; + return ApplyInDeclaration; +}(java.lang.Object); diff --git a/test-app/build-tools/jsparser/tests/cases/content_members/internal/ts_helpers.js b/test-app/build-tools/jsparser/tests/cases/content_members/internal/ts_helpers.js new file mode 100644 index 000000000..1a0625f45 --- /dev/null +++ b/test-app/build-tools/jsparser/tests/cases/content_members/internal/ts_helpers.js @@ -0,0 +1,175 @@ +(function () { + + var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length; + var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + + if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") { + r = global.Reflect.decorate(decorators, target, key, desc); + } + else { + for (var i = decorators.length - 1; i >= 0; i--) { + if (d = decorators[i]) { + r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + } + } + } + return c > 3 && r && Object.defineProperty(target, key, r), r; + }; + + // For backward compatibility. + var __native = function (thiz) { + // we are setting the __container__ property to the base class when the super method is called + // if the constructor returns the __native(this) call we will use the old implementation + // copying all the properties to the result + // otherwise if we are using the result from the super() method call we won't need such logic + // as thiz already contains the parent properties + // this way we now support both implementations in typescript generated constructors: + // 1: super(); return __native(this); + // 2: return super() || this; + if (thiz.__container__) { + var result = thiz.__proto__; + + for (var prop in thiz) { + if (thiz.hasOwnProperty(prop)) { + thiz.__proto__[prop] = thiz[prop]; + delete thiz[prop]; + } + } + + thiz.constructor = undefined; + thiz.__proto__ = undefined; + Object.freeze(thiz); + Object.preventExtensions(thiz); + return result; + } else { + return thiz; + } + }; + + var __extends = function (Child, Parent) { + var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1); + if (!extendNativeClass) { + __extends_ts(Child, Parent); + return; + } + if (Parent.__isPrototypeImplementationObject) { + throw new Error("Can not extend an already extended native object."); + } + + function extend(thiz) { + var child = thiz.__proto__.__child; + if (!child.__extended) { + var parent = thiz.__proto__.__parent; + child.__extended = parent.extend(child.name, child.prototype, true); + // This will deal with "i instanceof child" + child[Symbol.hasInstance] = function (instance) { + return instance instanceof this.__extended; + } + } + return child.__extended; + }; + + Parent.__activityExtend = function (parent, name, implementationObject) { + __log("__activityExtend called"); + return parent.extend(name, implementationObject); + }; + + Parent.call = function (thiz) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (arguments.length > 1) { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1)))); + } + else { + thiz.__proto__ = new Extended() + } + return thiz.__proto__; + }; + + Parent.apply = function (thiz, args) { + var Extended = extend(thiz); + thiz.__container__ = true; + if (args && args.length > 0) { + thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args))); + } + else { + thiz.__proto__ = new Extended(); + } + return thiz.__proto__; + }; + __extends_ns(Child, Parent); + Child.__isPrototypeImplementationObject = true; + Child.__proto__ = Parent; + Child.prototype.__parent = Parent; + Child.prototype.__child = Child; + } + + var __extends_ts = function (child, parent) { + extendStaticFunctions(child, parent); + assignPrototypeFromParentToChild(parent, child); + }; + + var __extends_ns = function (child, parent) { + if (!parent.extend) { + assignPropertiesFromParentToChild(parent, child); + } + + assignPrototypeFromParentToChild(parent, child); + }; + + var extendStaticFunctions = + Object.setPrototypeOf + || (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; }) + || assignPropertiesFromParentToChild; + + function hasInternalProtoProperty() { + return { __proto__: [] } instanceof Array; + } + + function assignPropertiesFromParentToChild(parent, child) { + for (var property in parent) { + if (parent.hasOwnProperty(property)) { + child[property] = parent[property]; + } + } + } + + function assignPrototypeFromParentToChild(parent, child) { + function __() { + this.constructor = child; + } + + if (parent === null) { + child.prototype = Object.create(null); + } else { + __.prototype = parent.prototype; + child.prototype = new __(); + } + } + + + function JavaProxy(className) { + return function (target) { + var extended = target.extend(className, target.prototype) + extended.name = className; + return extended; + }; + } + + function Interfaces(interfacesArr) { + return function (target) { + if (interfacesArr instanceof Array) { + // attach interfaces: [] to the object + target.prototype.interfaces = interfacesArr; + } + } + } + + Object.defineProperty(global, "__native", { value: __native }); + Object.defineProperty(global, "__extends", { value: __extends }); + Object.defineProperty(global, "__decorate", { value: __decorate }); + + global.JavaProxy = JavaProxy; + global.Interfaces = Interfaces; +})() \ No newline at end of file diff --git a/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js b/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js index a72be6651..729e4c902 100644 --- a/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js +++ b/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js @@ -307,6 +307,42 @@ describe("parser/js_parser tests", function () { }); }); + it("Generated metadata for bindings lists the own data properties the runtime sees", function (done) { + let input = path.normalize(path.join(prefix, "content_members", "app")), + generatedJavaClassesRoot = path.normalize(path.join(prefix, "content_members", "src", "main", "java")); + + clearOutput(); + + execGradle(input, generatedJavaClassesRoot, function (error, stdout, stderr) { + if (error) { + console.error(`exec error: ${error}`); + return done.fail(error); + } + logExecResult(stdout, stderr) + + // JavaClass*file*line*column*NewClass*methods*JavaProxyName*jsFile*interfaces + let rows = {}; + for (let line of fs.readFileSync(sbgBindingOutoutFile, "utf-8").toString().trim().split('\n')) { + let parts = line.trim().split("*"); + rows[parts[4]] = parts; + } + + expect(rows["LiteralMembers"][5]).toBe("identifierMember,quotedMember,shorthandMember,dataMember,run"); + expect(rows["LiteralMembers"][8]).toBe("java.lang.Runnable"); + + expect(rows["PrototypeMembers"][5]).toBe("toString,quotedPrototypeMember,chainedA,chainedB,sequenceA,sequenceB"); + expect(rows["PrototypeMembers"][8]).toBe("java.lang.Runnable"); + + let source = fs.readFileSync(path.join(input, "members.js"), "utf-8").split("\n"); + let applyLine = source.findIndex(line => line.indexOf("_super.apply(this, arguments) || this;") > 0); + expect(rows["ApplyInDeclaration"][5]).toBe("toString"); + expect(rows["ApplyInDeclaration"][2]).toBe(String(applyLine + 1)); + expect(rows["ApplyInDeclaration"][3]).toBe(String(source[applyLine].indexOf("apply(") + 1)); + + done(); + }); + }); + it("Generate valid metadata for bindings where multiple interfaces are implemented using array", function (done) { let input = path.normalize(path.join(prefix, "extends_with_interfaces", "app")); generatedJavaClassesRoot = path.normalize(path.join(prefix, "extends_with_interfaces", "src", "main", "java")); diff --git a/test-app/build-tools/jsparser/visitors/es5-visitors.js b/test-app/build-tools/jsparser/visitors/es5-visitors.js index ca6a8c0ba..8a68f7da4 100644 --- a/test-app/build-tools/jsparser/visitors/es5-visitors.js +++ b/test-app/build-tools/jsparser/visitors/es5-visitors.js @@ -228,16 +228,37 @@ var es5_visitors = (function() { if possibleExpressionStatements[0].expression.right.left.callee.object.name === superVariableIdentifier if the above is valid, then variableRHS = possibleVariableDeclarations[0].expression.right.left */ - function getThisAssignmentSuperCallLineNode(nodes, superIdentifier) { - var matchingNodes = nodes.filter( - (node) => { - return types.isAssignmentExpression(node.expression) && - types.isLogicalExpression(node.expression.right) && - types.isCallExpression(node.expression.right.left) && - node.expression.right.left.callee.object.name === superIdentifier; - }); + function isSuperCall(node, superIdentifier) { + return types.isCallExpression(node) && + types.isMemberExpression(node.callee) && + node.callee.object.name === superIdentifier; + } - return matchingNodes.length > 0 ? matchingNodes[0].expression.right.left : null; + // `_super.call(this) || this` or `_super !== null && _super.apply(this, arguments) || this` + function getSuperCallFromLogicalExpression(node, superIdentifier) { + if (!types.isLogicalExpression(node)) { + return null; + } + var left = node.left; + if (isSuperCall(left, superIdentifier)) { + return left; + } + if (types.isLogicalExpression(left) && isSuperCall(left.right, superIdentifier)) { + return left.right; + } + return null; + } + + function getThisAssignmentSuperCallLineNode(nodes, superIdentifier) { + for (var i = 0; i < nodes.length; i++) { + var expression = nodes[i].expression; + var superCall = types.isAssignmentExpression(expression) && + getSuperCallFromLogicalExpression(expression.right, superIdentifier); + if (superCall) { + return superCall; + } + } + return null; } /** Getting the super.call node from declaration @@ -248,14 +269,13 @@ var es5_visitors = (function() { if the above is valid, then variableRHS = possibleVariableDeclarations[0].declarations[0].init.left */ function getThisDeclarationSuperCallLineNode(nodes, superIdentifier) { - var matchingNodes = nodes.filter( - (node) => { - return types.isLogicalExpression(node.declarations[0].init) && - types.isCallExpression(node.declarations[0].init.left) && - node.declarations[0].init.left.callee.object.name === superIdentifier; - }); - - return matchingNodes.length > 0 ? matchingNodes[0].declarations[0].init.left : null; + for (var i = 0; i < nodes.length; i++) { + var superCall = getSuperCallFromLogicalExpression(nodes[i].declarations[0].init, superIdentifier); + if (superCall) { + return superCall; + } + } + return null; } /** Getting the super.apply node from the return of a class without a constructor @@ -263,23 +283,10 @@ var es5_visitors = (function() { return _super.apply(this, arguments) || this; */ function getReturnSuperCallLineNode(nodes, superIdentifier) { - function isSuperCall(node) { - return types.isCallExpression(node) && - types.isMemberExpression(node.callee) && - node.callee.object.name === superIdentifier; - } - for (var i = 0; i < nodes.length; i++) { - var argument = nodes[i].argument; - if (!types.isLogicalExpression(argument)) { - continue; - } - var left = argument.left; - if (isSuperCall(left)) { - return left; - } - if (types.isLogicalExpression(left) && isSuperCall(left.right)) { - return left.right; + var superCall = getSuperCallFromLogicalExpression(nodes[i].argument, superIdentifier); + if (superCall) { + return superCall; } } return null; @@ -558,18 +565,42 @@ var es5_visitors = (function() { * HELPER METHODS */ - // Returns the Identifier name of a property's key, or null when the property - // is a SpreadElement, has a computed key, or uses a non-Identifier key - // (e.g. StringLiteral, NumericLiteral). Such properties cannot be mapped to - // a Java method binding, so callers should skip them. - function _getIdentifierKeyName(property) { - if (!property || property.computed) { + // The member names the runtime can see as own data properties of an + // implementation object, which is what it names and generates a class from. + var MEMBER_NAME_PATTERN = /^[A-Za-z_$][A-Za-z0-9_$]*$/; + + // Returns the name an object literal property defines as an own data + // property, or null for spreads, computed keys, getters and setters, and + // keys that cannot name a Java method. + function _getMemberKeyName(property) { + if (!property || property.computed || !property.key) { return null; } - if (!property.key || !types.isIdentifier(property.key)) { + if (types.isObjectMethod(property) && property.kind !== "method") { return null; } - return property.key.name; + + var name; + if (types.isIdentifier(property.key)) { + name = property.key.name; + } else if (types.isStringLiteral(property.key)) { + name = property.key.value; + } else { + return null; + } + + // `__proto__: value` sets the prototype instead of defining a property. + if (name === "__proto__" && types.isObjectProperty(property) && !property.shorthand) { + return null; + } + + return MEMBER_NAME_PATTERN.test(name) ? name : null; + } + + function _pushUnique(array, value) { + if (array.indexOf(value) === -1) { + array.push(value); + } } function _getOverriddenMethods(node, config) { @@ -578,11 +609,11 @@ var es5_visitors = (function() { var objectProperties = node.properties; for (var index in objectProperties) { - var keyName = _getIdentifierKeyName(objectProperties[index]); + var keyName = _getMemberKeyName(objectProperties[index]); if (keyName === null) { continue; } - overriddenMethodNames.push(keyName); + _pushUnique(overriddenMethodNames, keyName); } } @@ -617,11 +648,9 @@ var es5_visitors = (function() { will get 'method1' and 'method3' */ for (var index in objectProperties) { - var keyName = _getIdentifierKeyName(objectProperties[index]); - // Skip spreads, computed keys, and non-Identifier keys — they - // cannot be statically mapped to a Java binding. Without this - // guard, valid (non-NS) `.extend({ ...other, foo })` calls in - // bundled vendor code (e.g. Zod schemas) would crash the parser. + var keyName = _getMemberKeyName(objectProperties[index]); + // Without this guard, valid (non-NS) `.extend({ ...other, foo })` + // calls in bundled vendor code (e.g. Zod schemas) would crash the parser. if (keyName === null) { continue; } @@ -638,7 +667,7 @@ var es5_visitors = (function() { implementedInterfaces.push(interfaceName); } } else { - overriddenMethodNames.push(keyName) + _pushUnique(overriddenMethodNames, keyName); } } } @@ -747,20 +776,58 @@ var es5_visitors = (function() { return undefined; } + // The prototype members of the class declared by `__extends(X, _super)`: + // `X.prototype.name = ...` statements, including chained and comma-joined + // ones. Statics, and accessors (`Object.defineProperty(X.prototype, ...)`), + // are not data properties of the prototype the runtime reads. function _getOverriddenMethodsTypescript(path, count) { var overriddenMethods = []; + var extendsCall = path.parentPath && path.parentPath.node; + var classIdentifier = types.isCallExpression(extendsCall) && extendsCall.arguments[0]; + if (!types.isIdentifier(classIdentifier)) { + return overriddenMethods; + } + var className = classIdentifier.name; + + function prototypeMemberName(left) { + if (!types.isMemberExpression(left)) { + return null; + } + var target = left.object; + if (!types.isMemberExpression(target) || target.computed || + !types.isIdentifier(target.object, { name: className }) || + !types.isIdentifier(target.property, { name: "prototype" })) { + return null; + } + var name = null; + if (!left.computed && types.isIdentifier(left.property)) { + name = left.property.name; + } else if (left.computed && types.isStringLiteral(left.property)) { + name = left.property.value; + } + return name !== null && MEMBER_NAME_PATTERN.test(name) ? name : null; + } + + function collect(expression) { + if (types.isSequenceExpression(expression)) { + expression.expressions.forEach(collect); + } else if (types.isAssignmentExpression(expression) && expression.operator === "=") { + var name = prototypeMemberName(expression.left); + if (name !== null) { + _pushUnique(overriddenMethods, name); + } + collect(expression.right); + } + } + var cn = _getParent(path, count) // this pattern follows typescript generated syntax for (var item in cn.node.body) { var ci = cn.node.body[item]; if (types.isExpressionStatement(ci)) { - if (types.isAssignmentExpression(ci.expression)) { - if (ci.expression.left.property) { - overriddenMethods.push(ci.expression.left.property.name) - } - } + collect(ci.expression); } } From 373ad02bda458133ccb682fd714104bd652ecb4a Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 20:31:05 -0300 Subject: [PATCH 06/14] feat(sbg): optionally name bindings after their content With `-PuseContentBindings` (or `project.ext.useContentBindings = true` in App_Resources/Android/app.gradle or before-plugins.gradle), the static binding generator names the class of a JS subclass of a Java class after what it is made of instead of where its extend() call is: com.tns.gen.._[_]h<16 hex digits> The hash is FNV-1a 64 over the base, the sorted implemented interfaces and the sorted member names, with nested types spelled with dots. The class is generated from the same sorted, de-duplicated members and interfaces, so call sites with the same content share one class whichever of them produced it. Interface implementations and @JavaProxy/dotted names keep their names. Without the property nothing changes. Java finds the JS implementation of a class it creates by class name alone, so named extends must not share a class: when more than one call site produces the same named content name, those sites keep their call-site names and the name goes into the collision list of a generated com.tns.gen.ContentKeyedBindings class. The runtime switches to content names when that class is in the app, so the generator and the runtime cannot disagree on the mode. The generator also warns about named extends without a source location, which it cannot check for collisions, and about extends of Fragment classes without a @JavaProxy name, which Android re-creates by class name. runSbg prints the warnings, and the property is one of its inputs. Two bindings generated under one file name are now compared in memory. The file on disk carries the generated-file prologue and was compared with the content without it, so even identical duplicates failed the build. Different contents still fail it, naming both call sites. runtests forwards -PuseContentBindings to the test app build. --- build.gradle | 3 + test-app/app/build.gradle | 25 +++ test-app/build-tools/.gitignore | 1 + .../staticbindinggenerator/Binding.java | 11 + .../staticbindinggenerator/ContentKey.java | 122 +++++++++++ .../staticbindinggenerator/Generator.java | 203 +++++++++++++++--- .../InputParameters.java | 16 ++ .../staticbindinggenerator/Main.java | 15 ++ .../java/androidx/fragment/app/Fragment.java | 6 + .../src/test/java/com/example/MyFragment.java | 4 + .../test/ContentKeyTest.java | 90 ++++++++ .../test/ContentKeyedGeneratorTest.java | 171 +++++++++++++++ test-app/runtests.gradle | 3 + 13 files changed, 645 insertions(+), 25 deletions(-) create mode 100644 test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java create mode 100644 test-app/build-tools/static-binding-generator/src/test/java/androidx/fragment/app/Fragment.java create mode 100644 test-app/build-tools/static-binding-generator/src/test/java/com/example/MyFragment.java create mode 100644 test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java create mode 100644 test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java diff --git a/build.gradle b/build.gradle index f9bd5c264..1718eb62d 100644 --- a/build.gradle +++ b/build.gradle @@ -488,6 +488,9 @@ def getRunTestsBuildArguments = { taskName -> if (project.hasProperty("narrowMetadataFields")) { arguments.add("-PnarrowMetadataFields") } + if (project.hasProperty("useContentBindings")) { + arguments.add("-PuseContentBindings=${project.property('useContentBindings')}") + } arguments += ["--warning-mode", "all"] return arguments } diff --git a/test-app/app/build.gradle b/test-app/app/build.gradle index f7e68d069..ead48359b 100644 --- a/test-app/app/build.gradle +++ b/test-app/app/build.gradle @@ -19,6 +19,7 @@ * -PappPath=[app_path] * -PappResourcesPath=[app_resources_path] * -PskipSbgAppClasses //don't show the app's own Java classes to the static binding generator +* -PuseContentBindings //name the classes of JS subclasses of Java classes after their content */ import groovy.json.JsonSlurper @@ -604,11 +605,24 @@ tasks.register("compileAppClassesForSbg") { } } +// Name the Java classes of JS subclasses of Java classes after what they contain instead +// of where their extend() call is. Off unless the app sets -PuseContentBindings or +// `project.ext.useContentBindings = true` in App_Resources/Android/app.gradle or +// before-plugins.gradle. The runtime follows the class the generator then adds to the app. +def useContentBindings = { -> + if (!project.hasProperty("useContentBindings")) { + return false + } + def value = project.property("useContentBindings").toString() + return value.isEmpty() || value.toBoolean() +} + task runSbg(type: BuildToolTask) { dependsOn "collectAllJars" dependsOn "compileAppClassesForSbg" inputs.files(tasks.named("compileAppClassesForSbg")).withPropertyName("appClasses") inputs.property("sbgAppClassesEnabled", provider { sbgAppClassesEnabled() }) + inputs.property("useContentBindings", provider { useContentBindings() }) def rootPath = "" if (!findProject(':static-binding-generator').is(null)) { rootPath = Paths.get(project(':static-binding-generator').projectDir.path, "build/libs").toString() @@ -634,6 +648,7 @@ task runSbg(type: BuildToolTask) { args paramz def sbgDependenciesFile = file("$BUILD_TOOLS_PATH/$SBG_JAVA_DEPENDENCIES") + def sbgWarningsFile = file("$BUILD_TOOLS_PATH/sbg-warnings.txt") doFirst { new File("$OUTPUT_JAVA_DIR/com/tns/gen").deleteDir() @@ -642,6 +657,16 @@ task runSbg(type: BuildToolTask) { if (sbgAppClassesEnabled() && appClassesDir.isDirectory()) { sbgDependenciesFile.withWriterAppend { it.println(appClassesDir.absolutePath) } } + + if (useContentBindings()) { + args("-content-keyed-bindings") + } + } + doLast { + // The generator's standard output only reaches runSbg.log. + if (sbgWarningsFile.isFile()) { + sbgWarningsFile.eachLine { logger.warn("runSbg: ${it}") } + } } } diff --git a/test-app/build-tools/.gitignore b/test-app/build-tools/.gitignore index b948262cc..55848f83d 100644 --- a/test-app/build-tools/.gitignore +++ b/test-app/build-tools/.gitignore @@ -4,6 +4,7 @@ sbg-js-parsed-files.txt sbg-input-file.txt sbg-output-file.txt sbg-java-dependencies.txt +sbg-warnings.txt mdg-output-dir.txt mdg-java-dependencies.txt sbg-input-output-dirs.txt diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Binding.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Binding.java index fc8697d3a..f2a6a4224 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Binding.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Binding.java @@ -6,11 +6,17 @@ public class Binding { private final File file; private final String content; private final String classname; + private final String source; public Binding(File file, String content, String classname) { + this(file, content, classname, ""); + } + + public Binding(File file, String content, String classname, String source) { this.file = file; this.content = content; this.classname = classname; + this.source = source; } public File getFile() { @@ -25,4 +31,9 @@ public String getClassname() { return classname; } + /** The extend() call site the binding was generated for, for messages. */ + public String getSource() { + return source; + } + } diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java new file mode 100644 index 000000000..6eee9f094 --- /dev/null +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java @@ -0,0 +1,122 @@ +package org.nativescript.staticbindinggenerator; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.TreeSet; +import java.util.regex.Pattern; + +/** + * The build-time half of content-keyed binding names. The runtime computes the + * same key in MetadataNode::CreateContentKey and the same member set in + * CallbackHandlers::CollectContentMemberNames; a disagreement does not fail + * anything, the class is just generated on the device instead. ContentKeyTest + * and the test app's content-keyed binding specs pin the same literal vectors. + */ +public final class ContentKey { + + private static final long FNV_OFFSET_BASIS = 0xcbf29ce484222325L; + private static final long FNV_PRIME = 0x100000001b3L; + + private static final Pattern MEMBER_NAME = Pattern.compile("^[A-Za-z_$][A-Za-z0-9_$]*$"); + + // Put on every implementation object by extend() or by ts_helpers' __extends, + // never by the app. + private static final Set EXCLUDED_MEMBER_NAMES = new HashSet<>(Arrays.asList("constructor", "super", "__parent", "__child")); + + // The runtime sorts std::string, which compares bytes; String.compareTo + // compares UTF-16 units, which orders differently above the BMP. + private static final Comparator UTF8_ORDER = (left, right) -> { + byte[] a = left.getBytes(StandardCharsets.UTF_8); + byte[] b = right.getBytes(StandardCharsets.UTF_8); + int shared = Math.min(a.length, b.length); + for (int i = 0; i < shared; i++) { + int diff = (a[i] & 0xff) - (b[i] & 0xff); + if (diff != 0) { + return diff; + } + } + return a.length - b.length; + }; + + private ContentKey() { + } + + /** + * The members a binding is generated with, from the JS parser's member names: + * sorted, without duplicates and without the names extend() itself adds. + */ + public static List memberNames(String[] parsedNames) { + TreeSet result = new TreeSet<>(UTF8_ORDER); + if (parsedNames != null) { + for (String name : parsedNames) { + if (name != null && MEMBER_NAME.matcher(name).matches() && !EXCLUDED_MEMBER_NAMES.contains(name)) { + result.add(name); + } + } + } + return new ArrayList<>(result); + } + + /** + * Interface names in the order the binding implements them. Ordered and + * de-duplicated by their canonical form, the form the key hashes. + */ + public static List interfaceNames(String[] parsedNames) { + List result = new ArrayList<>(); + Set seen = new HashSet<>(); + if (parsedNames != null) { + for (String name : parsedNames) { + if (name != null && !name.isEmpty() && seen.add(canonicalize(name))) { + result.add(name); + } + } + } + result.sort((left, right) -> UTF8_ORDER.compare(canonicalize(left), canonicalize(right))); + return result; + } + + /** {@code h} followed by 16 lowercase hex digits. */ + public static String of(String baseClassName, Collection interfaceNames, Collection memberNames) { + long hash = FNV_OFFSET_BASIS; + for (byte b : payload(baseClassName, interfaceNames, memberNames).getBytes(StandardCharsets.UTF_8)) { + hash ^= (b & 0xffL); + hash *= FNV_PRIME; + } + return String.format(Locale.ROOT, "h%016x", hash); + } + + /** + * {@code canonical(base)|sorted canonical interfaces|sorted members}, each + * list comma-separated, without empty or duplicate entries. + */ + public static String payload(String baseClassName, Collection interfaceNames, Collection memberNames) { + return canonicalize(baseClassName) + "|" + join(interfaceNames, true) + "|" + join(memberNames, false); + } + + /** + * JS source spells a nested type like a package, `a.B.C`; the runtime holds + * binary names, `a/B$C`. The dotted form is the one both sides can produce. + */ + static String canonicalize(String name) { + return name == null ? "" : name.replace('/', '.').replace('$', '.'); + } + + private static String join(Collection values, boolean canonicalize) { + TreeSet sorted = new TreeSet<>(UTF8_ORDER); + if (values != null) { + for (String value : values) { + if (value != null && !value.isEmpty()) { + sorted.add(canonicalize ? canonicalize(value) : value); + } + } + } + return String.join(",", sorted); + } +} diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java index fe3e6227f..d294f0825 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java @@ -45,17 +45,19 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; -import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeSet; public class Generator { @@ -63,6 +65,9 @@ public class Generator { private static final String DEFAULT_PACKAGE_NAME = "com.tns.gen"; + // Looked up by name in com.tns.Runtime. + public static final String CONTENT_KEYED_BINDINGS_CLASS = "ContentKeyedBindings"; + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); public static final String AUTO_GENERATED_FILE_PROLOGUE = @@ -80,6 +85,13 @@ public class Generator { private final AndroidClassChecker androidClassChecker; private Set nonPublicNestedClasses; + private final boolean contentKeyedBindings; + // Named extends whose content name more than one call site produces; they keep + // their call-site name, and the runtime does the same for every name listed here. + private final Set locationNamedRows = Collections.newSetFromMap(new IdentityHashMap<>()); + private final Set contentNameCollisions = new TreeSet<>(); + private final List warnings = new ArrayList<>(); + public Generator(File outputDir, List libs) throws IOException { this(outputDir, libs, false); } @@ -93,29 +105,62 @@ public Generator(File outputDir, List libs, boolean suppressCallJSMetho this.nonPublicNestedClasses = classesCollection.getNonPublicNestedClasses(); androidClassChecker = new AndroidClassCheckerImpl(classes); this.suppressCallJSMethodExceptions = suppressCallJSMethodExceptions; + this.contentKeyedBindings = InputParameters.getCurrent().getContentKeyedBindings(); } public void writeBindings(String filename) throws IOException, ClassNotFoundException { fileSystemHelper.cleanPreviouslyAutoGeneratedSbgFiles(this.outputDir); Binding[] bindings = generateBindings(filename); - Set writtenFiles = new HashSet(); + Map writtenFiles = new HashMap(); for (Binding b : bindings) { - if (writtenFiles.add(b.getFile())) { - try (PrintStream ps = new PrintStream(b.getFile())) { - ps.append(AUTO_GENERATED_FILE_PROLOGUE); - ps.append(b.getContent()); - } - // A file with that name has already been written - } else { - // Compare text contents for equality - String content = new String(Files.readAllBytes(Paths.get(b.getFile().toString()))); - if (content.equals(b.getContent())) { - System.out.println("Warning: File already exists. This could mean the same code has been parsed more than once from two or more different files. \nFile:" + b.getFile()); - } else { - throw new IOException("File already exists. This may lead to undesired behavior.\nPlease change the name of one of the extended classes.\nFile:" + b.getFile() + " Class: " + b.getClassname()); - } + Binding written = writtenFiles.putIfAbsent(b.getFile(), b); + if (written == null) { + writeGeneratedFile(b.getFile(), b.getContent()); + } else if (!written.getContent().equals(b.getContent())) { + throw new IOException("Two extends generate the class " + b.getFile() + " with different contents, from " + written.getSource() + " and from " + b.getSource() + ".\nPlease change the name of one of the extended classes."); } } + + if (contentKeyedBindings) { + File markerDir = new File(outputDir, DEFAULT_PACKAGE_NAME.replace('.', '/')); + markerDir.mkdirs(); + writeGeneratedFile(new File(markerDir, CONTENT_KEYED_BINDINGS_CLASS + JAVA_EXT), getContentKeyedBindingsSource()); + } + } + + private static void writeGeneratedFile(File file, String content) throws IOException { + try (PrintStream ps = new PrintStream(file)) { + ps.append(AUTO_GENERATED_FILE_PROLOGUE); + ps.append(content); + } + } + + /** + * The runtime names classes after their content only when this class is in + * the app, so the generator and the runtime cannot disagree on the mode. + */ + public String getContentKeyedBindingsSource() { + StringBuilder sb = new StringBuilder(); + sb.append("package ").append(DEFAULT_PACKAGE_NAME).append(";").append(LINE_SEPARATOR).append(LINE_SEPARATOR); + sb.append("public final class ").append(CONTENT_KEYED_BINDINGS_CLASS).append(" {").append(LINE_SEPARATOR); + sb.append(" public static final String[] COLLISIONS = {").append(LINE_SEPARATOR); + for (String name : contentNameCollisions) { + sb.append(" \"").append(name).append("\",").append(LINE_SEPARATOR); + } + sb.append(" };").append(LINE_SEPARATOR).append(LINE_SEPARATOR); + sb.append(" private ").append(CONTENT_KEYED_BINDINGS_CLASS).append("() {").append(LINE_SEPARATOR); + sb.append(" }").append(LINE_SEPARATOR); + sb.append("}").append(LINE_SEPARATOR); + return sb.toString(); + } + + /** Content names that more than one named call site produced, as dotted class names. */ + public Set getContentNameCollisions() { + return Collections.unmodifiableSet(contentNameCollisions); + } + + public List getWarnings() { + return Collections.unmodifiableList(warnings); } public Binding[] generateBindings(String filename) throws IOException, ClassNotFoundException { @@ -148,11 +193,19 @@ public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) String name = ""; Boolean isInterface = clazz.isInterface(); + // The class body must be a function of the content name alone, whichever + // call site sharing the name it is generated for. + boolean contentKeyed = usesContentKeyedName(dataRow, clazz); + List methods = contentKeyed ? ContentKey.memberNames(dataRow.getMethods()) : Arrays.asList(dataRow.getMethods()); + List interfaces = contentKeyed ? ContentKey.interfaceNames(dataRow.getInterfaces()) : Arrays.asList(dataRow.getInterfaces()); + if (hasSpecifiedName) { name = getSimpleClassname(dataRow.getFilename()); } else { if (isInterface) { name = getSimpleClassname(clazz.getClassName()); + } else if (contentKeyed && !locationNamedRows.contains(dataRow)) { + name = getContentClassSimpleName(dataRow, clazz, interfaces, methods); } else { name = getSimpleClassname(clazz.getClassName().replace("$", "_")) + "_"; // Generate a unique identifier that prevents naming collisions @@ -172,14 +225,108 @@ public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) Writer w = new Writer(); - writeBinding(w, dataRow, clazz, packageName, name); + writeBinding(w, dataRow, clazz, packageName, name, methods, interfaces); String classname = dataRow.getFilename(); + String source = describeSite(dataRow); try { String formattedSource = new Formatter().formatSource(w.toString()); - return new Binding(new File(baseDir, normalizedName + JAVA_EXT), formattedSource, classname); + return new Binding(new File(baseDir, normalizedName + JAVA_EXT), formattedSource, classname, source); } catch (FormatterException e) { - return new Binding(new File(baseDir, normalizedName + JAVA_EXT), w.toString(), classname); + return new Binding(new File(baseDir, normalizedName + JAVA_EXT), w.toString(), classname, source); + } + } + + private boolean usesContentKeyedName(DataRow dataRow, JavaClass clazz) { + return contentKeyedBindings && !clazz.isInterface() && dataRow.getFilename().isEmpty(); + } + + /** {@code _[_]h}, with `$` in the base replaced by `_`. */ + private String getContentClassSimpleName(DataRow dataRow, JavaClass clazz, List interfaces, List methods) { + String extendName = dataRow.getNewClassName(); + return getSimpleClassname(clazz.getClassName().replace("$", "_")) + "_" + + (extendName.isEmpty() ? "" : extendName + "_") + + ContentKey.of(clazz.getClassName(), interfaces, methods); + } + + private String getContentClassName(DataRow dataRow, JavaClass clazz) { + List interfaces = ContentKey.interfaceNames(dataRow.getInterfaces()); + List methods = ContentKey.memberNames(dataRow.getMethods()); + return DEFAULT_PACKAGE_NAME + "." + clazz.getPackageName() + "." + getContentClassSimpleName(dataRow, clazz, interfaces, methods); + } + + private static boolean hasLocation(DataRow dataRow) { + return !dataRow.getLine().isEmpty() && !dataRow.getColumn().isEmpty(); + } + + private static String describeSite(DataRow dataRow) { + if (!dataRow.getFilename().isEmpty()) { + return dataRow.getJsFilename(); + } + return dataRow.getFile() + ":" + dataRow.getLine() + ":" + dataRow.getColumn(); + } + + /** + * Android re-creates a fragment from its class name alone, which cannot tell + * apart the call sites that share a content-keyed class. + */ + private boolean isFragment(JavaClass clazz) { + Set visited = new HashSet<>(); + JavaClass current = clazz; + while (current != null && visited.add(current.getClassName())) { + String className = current.getClassName(); + if (className.equals("androidx.fragment.app.Fragment") || className.equals("android.app.Fragment")) { + return true; + } + String superclassName = current.getSuperclassName(); + current = superclassName == null ? null : classes.get(BcelNamingUtil.resolveClassName(superclassName)); + } + return false; + } + + /** + * Decides which named extends keep their call-site name and warns about the + * call sites content-keyed names cannot protect. A named extend's class is + * the only thing Java has to find its JS implementation with when it creates + * an instance itself, so two named call sites must never share a class. + */ + private void planContentKeyedNames(List rows) { + Map> sitesByContentName = new HashMap<>(); + Map> rowsByContentName = new HashMap<>(); + + for (DataRow dataRow : rows) { + JavaClass clazz = classes.get(dataRow.getBaseClassname()); + if (clazz == null || !usesContentKeyedName(dataRow, clazz) || !canClassBeExtended(clazz)) { + continue; + } + + String contentName = getContentClassName(dataRow, clazz); + if (isFragment(clazz)) { + warnings.add("Warning: " + describeSite(dataRow) + " extends Fragment subclass " + clazz.getClassName() + " as " + contentName + ". Android re-creates fragments by class name; give it a unique class name with @JavaProxy."); + } + + if (dataRow.getNewClassName().isEmpty()) { + continue; + } + rowsByContentName.computeIfAbsent(contentName, k -> new ArrayList<>()).add(dataRow); + if (hasLocation(dataRow)) { + sitesByContentName.computeIfAbsent(contentName, k -> new HashSet<>()).add(describeSite(dataRow)); + } else { + warnings.add("Warning: " + dataRow.getFile() + " has an extend named " + dataRow.getNewClassName() + " without a source location; it keeps the class name " + contentName + " even if another extend of that name has the same content."); + } + } + + for (Map.Entry> entry : rowsByContentName.entrySet()) { + Set sites = sitesByContentName.get(entry.getKey()); + if (sites == null || sites.size() < 2) { + continue; + } + contentNameCollisions.add(entry.getKey()); + for (DataRow dataRow : entry.getValue()) { + if (hasLocation(dataRow)) { + locationNamedRows.add(dataRow); + } + } } } @@ -231,6 +378,13 @@ private Binding[] processRows(List rows) throws ClassNotFoundException ArrayList bindings = new ArrayList(); HashSet interfaceNames = new HashSet(); + if (contentKeyedBindings) { + planContentKeyedNames(rows); + for (String warning : warnings) { + System.out.println(warning); + } + } + for (DataRow dataRow : rows) { String classname = dataRow.getBaseClassname(); boolean isJavaExtend = classes.containsKey(classname); @@ -327,9 +481,9 @@ private String generateUniqueFileIdentifier(String filePath) { return identifier.toString(); } - private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String packageName, String name) { + private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String packageName, String name, List methods, List interfaces) { GenericsAwareClassHierarchyParser genericsAwareClassHierarchyParser = new GenericsAwareClassHierarchyParserImpl(new GenericSignatureReader(), classes); - List userImplementedInterfaces = getInterfacesFromCache(Arrays.asList(dataRow.getInterfaces())); + List userImplementedInterfaces = getInterfacesFromCache(interfaces); if (clazz.isInterface()) { userImplementedInterfaces.add(clazz); @@ -343,8 +497,8 @@ private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String pac writeImportsToWriter(w, clazz, packageName); writeClassBeginningToWriter(w, clazz, userImplementedInterfaces, name, dataRow, genView, interfaceGenericHierarchyViews); writeFieldsToWriter(w, clazz); - writeConstructorsToWriter(w, clazz, dataRow, name, genView); - writeMethodsToWriter(w, genView, interfaceGenericHierarchyViews, clazz, Arrays.asList(dataRow.getMethods()), userImplementedInterfaces, packageName); + writeConstructorsToWriter(w, clazz, methods, name, genView); + writeMethodsToWriter(w, genView, interfaceGenericHierarchyViews, clazz, methods, userImplementedInterfaces, packageName); writeClassEndToWriter(w); } @@ -452,7 +606,7 @@ private void writeFieldsToWriter(Writer writer, JavaClass clazz) { fieldsWriter.writeRuntimeTagField(); } - private void writeConstructorsToWriter(Writer writer, JavaClass clazz, DataRow dataRow, String generatedClassName, GenericHierarchyView genericHierarchyView) { + private void writeConstructorsToWriter(Writer writer, JavaClass clazz, List implObjectMethods, String generatedClassName, GenericHierarchyView genericHierarchyView) { boolean isApplicationClass = androidClassChecker.isApplicationClass(clazz); boolean isServiceClass = androidClassChecker.isServiceClass(clazz); boolean isAndroidWorkerClass = androidClassChecker.isAndroidWorkerClass(clazz); @@ -461,7 +615,6 @@ private void writeConstructorsToWriter(Writer writer, JavaClass clazz, DataRow d MethodsWriter methodsWriter = new MethodsWriterImpl(writer, suppressCallJSMethodExceptions, isApplicationClass, isServiceClass, isAndroidWorkerClass); ImplementationObjectChecker implementationObjectChecker = new ImplementationObjectCheckerImpl(); - List implObjectMethods = Arrays.asList(dataRow.getMethods()); boolean hasInitMethod = implementationObjectChecker.hasInitMethod(implObjectMethods); boolean hasInitMethod2 = !isApplicationClass && hasInitMethod; diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/InputParameters.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/InputParameters.java index 816f5e152..2737f6dde 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/InputParameters.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/InputParameters.java @@ -6,15 +6,27 @@ public class InputParameters { private static final String SHOW_DEPRECATION_WARNINGS = "-show-deprecation-warnings"; // if the flag is passed the generation will exit on error private static final String THROW_ON_ERROR = "-throw-on-error"; + // name classes of anonymous and named class extends after their content, not their call site + private static final String CONTENT_KEYED_BINDINGS = "-content-keyed-bindings"; private static InputParameters current = new InputParameters(); private boolean showDeprecationWarnings; private boolean throwOnError; + private boolean contentKeyedBindings; public InputParameters() { this.showDeprecationWarnings = false; this.throwOnError = false; + this.contentKeyedBindings = false; + } + + public void setContentKeyedBindings(boolean value) { + this.contentKeyedBindings = value; + } + + public boolean getContentKeyedBindings() { + return contentKeyedBindings; } public void setShowDeprecationWarnings(boolean value) { @@ -47,6 +59,10 @@ public static void parseCommand(String[] args) { if (commandArg.equals(THROW_ON_ERROR)) { inputParameters.setThrowOnError(true); } + + if (commandArg.equals(CONTENT_KEYED_BINDINGS)) { + inputParameters.setContentKeyedBindings(true); + } } } diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Main.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Main.java index f947b5719..846d6f98d 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Main.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Main.java @@ -13,6 +13,8 @@ import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -23,6 +25,7 @@ public class Main { public static final String SBG_BINDINGS_NAME = "sbg-bindings.txt"; public static final String SBG_JS_PARSED_FILES = "sbg-js-parsed-files.txt"; public static final String SBG_INTERFACE_NAMES = "sbg-interface-names.txt"; + public static final String SBG_WARNINGS = "sbg-warnings.txt"; private static String jsCodeAbsolutePath; private static List inputJsFiles; @@ -53,6 +56,7 @@ public static void main(String[] args) { Generator generator = new Generator(outputDir, rows, isSuppressCallJSMethodExceptionsEnabled()); generator.writeBindings(inputBindingFilename); + writeWarnings(generator.getWarnings()); } catch (Throwable ex) { System.err.println(String.format("Error executing Static Binding Generator: %s", ex.getMessage())); ex.printStackTrace(System.out); @@ -60,6 +64,17 @@ public static void main(String[] args) { } } + /* + * Standard output only reaches the build's log file; the build prints this file. + * */ + private static void writeWarnings(List warnings) throws IOException { + File warningsFile = new File(System.getProperty("user.dir"), SBG_WARNINGS); + Files.deleteIfExists(warningsFile.toPath()); + if (!warnings.isEmpty()) { + Files.write(warningsFile.toPath(), warnings, StandardCharsets.UTF_8); + } + } + /* * Method should traverse all js files from input folder and put the ones that need traversing in another file * */ diff --git a/test-app/build-tools/static-binding-generator/src/test/java/androidx/fragment/app/Fragment.java b/test-app/build-tools/static-binding-generator/src/test/java/androidx/fragment/app/Fragment.java new file mode 100644 index 000000000..22787c307 --- /dev/null +++ b/test-app/build-tools/static-binding-generator/src/test/java/androidx/fragment/app/Fragment.java @@ -0,0 +1,6 @@ +package androidx.fragment.app; + +public class Fragment { + public void onResume() { + } +} diff --git a/test-app/build-tools/static-binding-generator/src/test/java/com/example/MyFragment.java b/test-app/build-tools/static-binding-generator/src/test/java/com/example/MyFragment.java new file mode 100644 index 000000000..40ac5af02 --- /dev/null +++ b/test-app/build-tools/static-binding-generator/src/test/java/com/example/MyFragment.java @@ -0,0 +1,4 @@ +package com.example; + +public class MyFragment extends androidx.fragment.app.Fragment { +} diff --git a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java new file mode 100644 index 000000000..4f75d6224 --- /dev/null +++ b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java @@ -0,0 +1,90 @@ +package org.nativescript.staticbindinggenerator.test; + +import org.junit.Test; +import org.nativescript.staticbindinggenerator.ContentKey; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * The runtime computes the same keys in C++ (MetadataNode::CreateContentKey). + * The vectors marked "paired" are also pinned in the test app's + * testContentKeyedBindings.js, against classes the runtime generates. + */ +public class ContentKeyTest { + + private static List list(String... values) { + return Arrays.asList(values); + } + + private static final List NONE = Collections.emptyList(); + + @Test + public void hashesPayloads() { + assertEquals("com.tns.tests.Button1||echo,toString", ContentKey.payload("com.tns.tests.Button1", list(""), list("toString", "echo"))); + assertEquals("h4b3d4e7b5dcd7eac", ContentKey.of("com.tns.tests.Button1", list(""), list("toString", "echo"))); + assertEquals("h43486683deee2d72", ContentKey.of("java.lang.Object", list("java.lang.Runnable", "java.io.Closeable"), list("run", "close"))); + assertEquals("he2246413e8de25b9", ContentKey.of("android.view.View", NONE, list("onClick"))); + assertEquals("h55d3a3a72d6a9f05", ContentKey.of("com/tns/tests/Button1", list(""), list(""))); + } + + @Test + public void pairedWithTheRuntime() { + assertEquals("he8b99c521d575fca", ContentKey.of("java/lang/Object", NONE, list("toString", "contentKeyPairingPlain"))); + assertEquals("h68c7e45e43205e66", ContentKey.of("com/tns/tests/DummyClass$DummyDerivedClass", NONE, list("toString", "contentKeyPairingNested"))); + assertEquals("java.lang.Object|com.tns.tests.DummyClass.MyInterface,java.lang.Runnable|contentKeyPairingInterfaces,doSomething,echoInt,run", + ContentKey.payload("java/lang/Object", list("java.lang.Runnable", "com.tns.tests.DummyClass$MyInterface"), list("run", "echoInt", "doSomething", "contentKeyPairingInterfaces"))); + assertEquals("ha59a9a5b07d44d2b", ContentKey.of("java/lang/Object", list("java.lang.Runnable", "com.tns.tests.DummyClass$MyInterface"), list("run", "echoInt", "doSomething", "contentKeyPairingInterfaces"))); + assertEquals("h63c03cfe49192d3d", ContentKey.of("java/lang/Object", NONE, list("toString", "contentKeyPairingNamed"))); + assertEquals("hc5ac23ca0a98a3c1", ContentKey.of("java/lang/Object", NONE, list("toString", "contentKeyPairingTs"))); + assertEquals("h86a36c7f58a561ea", ContentKey.of("java/lang/Object", NONE, NONE)); + } + + @Test + public void readsNestedTypesInEitherSpelling() { + assertEquals(ContentKey.of("com.tns.tests.DummyClass.DummyDerivedClass", NONE, list("toString")), + ContentKey.of("com/tns/tests/DummyClass$DummyDerivedClass", NONE, list("toString"))); + + List methods = list("greet", "unpack", "properties", "toString", "formatTo", "update", "addPropertyChangeListener", "pack", "removePropertyChangeListener"); + List dotted = list("java.util.jar.Pack200.Unpacker", "java.util.Formattable", "java.util.Observer", "java.util.jar.Pack200.Packer"); + List binary = list("java/util/jar/Pack200$Unpacker", "java.util.Formattable", "java.util.Observer", "java/util/jar/Pack200$Packer"); + assertEquals("haa054d88a5def37b", ContentKey.of("java.lang.Object", dotted, methods)); + assertEquals("haa054d88a5def37b", ContentKey.of("java.lang.Object", binary, methods)); + } + + @Test + public void ignoresOrderEmptiesAndDuplicates() { + assertEquals(ContentKey.of("android.view.View", list("a.B", "c.D"), list("x", "y")), + ContentKey.of("android.view.View", list("c.D", "", "a.B"), list("y", "x", ""))); + assertEquals("h0c2b7a522f2ece0a", ContentKey.of("java.lang.Object", list("java.lang.Runnable", "java/lang/Runnable"), list("run", "run", "toString"))); + assertEquals(ContentKey.of("java.lang.Object", list("java.lang.Runnable"), list("run", "toString")), + ContentKey.of("java.lang.Object", list("java.lang.Runnable", "java/lang/Runnable"), list("run", "run", "toString"))); + } + + @Test + public void separatesDifferentContent() { + String base = ContentKey.of("android.view.View", NONE, list("onClick")); + assertNotEquals(base, ContentKey.of("android.view.ViewGroup", NONE, list("onClick"))); + assertNotEquals(base, ContentKey.of("android.view.View", NONE, list("onClick", "onTouch"))); + assertNotEquals(base, ContentKey.of("android.view.View", list("a.B"), list("onClick"))); + assertNotEquals(ContentKey.of("android.view.View", list("a.B"), NONE), ContentKey.of("android.view.View", NONE, list("a.B"))); + } + + @Test + public void selectsMemberNames() { + assertEquals(list("$x", "_y", "a1", "onCreate", "toString"), + ContentKey.memberNames(new String[]{"toString", "onCreate", "constructor", "super", "__parent", "__child", "", "a1", "_y", "$x", "toString", "1a", "a-b"})); + assertEquals(NONE, ContentKey.memberNames(new String[]{""})); + assertEquals(NONE, ContentKey.memberNames(null)); + } + + @Test + public void ordersInterfacesByCanonicalName() { + assertEquals(list("a.B$C", "a.B.D", "java.lang.Runnable"), + ContentKey.interfaceNames(new String[]{"java.lang.Runnable", "a.B.D", "", "a.B$C", "java/lang/Runnable"})); + } +} diff --git a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java new file mode 100644 index 000000000..e955c7e6e --- /dev/null +++ b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java @@ -0,0 +1,171 @@ +package org.nativescript.staticbindinggenerator.test; + +import com.example.View; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mdkt.compiler.InMemoryJavaCompiler; +import org.nativescript.staticbindinggenerator.ContentKey; +import org.nativescript.staticbindinggenerator.DataRow; +import org.nativescript.staticbindinggenerator.Generator; +import org.nativescript.staticbindinggenerator.InputParameters; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class ContentKeyedGeneratorTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private String classesDir; + private File outputDir; + + @Before + public void setUp() throws Exception { + URL u = View.class.getResource('/' + View.class.getName().replace('.', '/') + ".class"); + classesDir = new File(u.toURI()).getParentFile().getParentFile().getParentFile().getAbsolutePath(); + outputDir = temp.newFolder("out"); + } + + @After + public void tearDown() { + InputParameters.parseCommand(new String[0]); + } + + private Generator generate(boolean contentKeyed, String... rows) throws Exception { + InputParameters.parseCommand(contentKeyed ? new String[]{"-content-keyed-bindings"} : new String[0]); + File rowsFile = temp.newFile(); + Files.write(rowsFile.toPath(), Arrays.asList(rows), StandardCharsets.UTF_8); + List libs = new ArrayList<>(); + libs.add(new DataRow(classesDir)); + Generator generator = new Generator(outputDir, libs); + generator.writeBindings(rowsFile.getAbsolutePath()); + return generator; + } + + private List generatedFiles(String packageDir) { + File[] files = new File(outputDir, packageDir).listFiles(); + if (files == null) { + return Collections.emptyList(); + } + return Arrays.stream(files).map(File::getName).sorted().collect(Collectors.toList()); + } + + private String read(String path) throws IOException { + return new String(Files.readAllBytes(new File(outputDir, path).toPath()), StandardCharsets.UTF_8); + } + + private static String key(String... members) { + return ContentKey.of("com.example.View", Collections.emptyList(), Arrays.asList(members)); + } + + @Test + public void namesClassExtendsAfterTheirContent() throws Exception { + generate(true, + "com.example.View*tests_a*3*5**createView***", + "com.example.View*tests_a*9*5*Named*createView***", + "com.example.MyInterface*tests_a*12*5**getTile***"); + + List expected = new ArrayList<>(Arrays.asList("MyInterface.java", "View_" + key("createView") + ".java", "View_Named_" + key("createView") + ".java")); + Collections.sort(expected); + Assert.assertEquals(expected, generatedFiles("com/tns/gen/com/example")); + } + + @Test + public void sharesOneClassBetweenCallSitesWithTheSameContent() throws Exception { + generate(true, + "com.example.View*tests_a*3*5**createView,toString***", + "com.example.View*tests_b*7*9**toString,createView,toString,constructor***"); + + String name = "View_" + key("createView", "toString"); + Assert.assertEquals(Collections.singletonList(name + ".java"), generatedFiles("com/tns/gen/com/example")); + + String source = read("com/tns/gen/com/example/" + name + ".java"); + Class compiled = InMemoryJavaCompiler.compile("com.tns.gen.com.example." + name, + source, Arrays.asList("-cp", classesDir)); + Assert.assertNotNull(compiled); + } + + @Test + public void keepsCallSiteNamesForNamedExtendsThatCollide() throws Exception { + Generator generator = generate(true, + "com.example.View*tests_a*10*5*Named*createView***", + "com.example.View*tests_b*12*5*Named*createView***", + "com.example.View*tests_c*1*1*Unique*createView***", + "com.example.View*tests_d***NoLocation*createView***"); + + List files = generatedFiles("com/tns/gen/com/example"); + Assert.assertEquals(4, files.size()); + Assert.assertTrue(files.toString(), files.stream().anyMatch(f -> f.startsWith("View_tests_a_") && f.endsWith("_10_5_Named.java"))); + Assert.assertTrue(files.toString(), files.stream().anyMatch(f -> f.startsWith("View_tests_b_") && f.endsWith("_12_5_Named.java"))); + Assert.assertTrue(files.contains("View_Unique_" + key("createView") + ".java")); + Assert.assertTrue(files.contains("View_NoLocation_" + key("createView") + ".java")); + + String collision = "com.tns.gen.com.example.View_Named_" + key("createView"); + Assert.assertEquals(Collections.singleton(collision), generator.getContentNameCollisions()); + Assert.assertEquals(1, generator.getWarnings().size()); + Assert.assertTrue(generator.getWarnings().get(0), generator.getWarnings().get(0).contains("NoLocation")); + + String marker = read("com/tns/gen/ContentKeyedBindings.java"); + Assert.assertTrue(marker.startsWith(Generator.AUTO_GENERATED_FILE_PROLOGUE)); + Class markerClass = InMemoryJavaCompiler.compile("com.tns.gen.ContentKeyedBindings", marker, Arrays.asList("-cp", classesDir)); + Assert.assertArrayEquals(new String[]{collision}, (String[]) markerClass.getField("COLLISIONS").get(null)); + } + + @Test + public void warnsAboutFragmentsWithoutAJavaProxyName() throws Exception { + Generator generator = generate(true, + "com.example.MyFragment*tests_e*2*3**onResume***", + "com.example.MyFragment*****onResume*com.example.NamedFragment*app/f.js*", + "com.example.View*tests_e*5*3**createView***"); + + Assert.assertEquals(1, generator.getWarnings().size()); + String warning = generator.getWarnings().get(0); + Assert.assertTrue(warning, warning.contains("tests_e:2:3") && warning.contains("@JavaProxy")); + } + + @Test + public void keepsCallSiteNamesWithoutTheOptIn() throws Exception { + Generator generator = generate(false, + "com.example.View*tests_a*3*5**createView***", + "com.example.View*tests_a*9*5*Named*createView***", + "com.example.MyFragment*tests_e*2*3**onResume***"); + + List files = generatedFiles("com/tns/gen/com/example"); + Assert.assertEquals(3, files.size()); + Assert.assertTrue(files.toString(), files.stream().noneMatch(f -> f.contains("_h"))); + Assert.assertFalse(new File(outputDir, "com/tns/gen/ContentKeyedBindings.java").exists()); + Assert.assertTrue(generator.getWarnings().isEmpty()); + Assert.assertTrue(generator.getContentNameCollisions().isEmpty()); + } + + @Test + public void sharesIdenticalDuplicatesAndRejectsDifferentOnes() throws Exception { + generate(false, + "com.example.View*tests_a*3*5**createView***", + "com.example.View*tests_a*3*5**createView***"); + Assert.assertEquals(1, generatedFiles("com/tns/gen/com/example").size()); + + try { + generate(false, + "com.example.View*tests_a*3*5**createView***", + "com.example.View*tests_a*3*5**createView,toString***"); + Assert.fail("expected the generator to reject two different classes with one name"); + } catch (IOException e) { + Assert.assertTrue(e.getMessage(), e.getMessage().contains("tests_a:3:5") && e.getMessage().contains("different contents")); + } + } +} diff --git a/test-app/runtests.gradle b/test-app/runtests.gradle index 445b69eec..f63970154 100644 --- a/test-app/runtests.gradle +++ b/test-app/runtests.gradle @@ -74,6 +74,9 @@ def getBuildArguments = { -> } arguments.add("-PuseKotlin=true") + if (project.hasProperty("useContentBindings")) { + arguments.add("-PuseContentBindings=${project.property('useContentBindings')}") + } // Ordinary apps get the narrowest metadata fields they can; the suite runs on the // wider ones, which nothing else would exercise. -PnarrowMetadataFields runs it on // the ones that ship. From 5545d23c9d4ee446a45e830527a4a89364105c6d Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 20:33:18 -0300 Subject: [PATCH 07/14] feat: look up content-keyed binding names at runtime When the app contains com.tns.gen.ContentKeyedBindings, which the static binding generator adds when it names bindings after their content, an extend() of a Java class (not an interface, not a dotted or @JavaProxy name) asks for the class the generator wrote for its content: com.tns.gen.._[_]h<16 hex digits> The member set is the implementation object's own enumerable data properties whose names can name a Java method, without the ones extend() and __extends add. They are read from property descriptors, so getters do not run, and are also the overrides of a class generated at runtime, so it matches the class the generator would write for that name. The marker class is read once per process, before the first runtime starts, and the mode and collision list stay immutable afterwards. The call-site name stays the identity of an extend() call within a runtime: it keys the extended-constructor cache, so a call site that runs again gets its first constructor and call sites sharing a class keep their own constructors and implementation objects. Instances Java creates by class name get the first call site that registered the class; if more than one did, that is logged once per class. A named extend resolves by its call-site name, as before, when its content name is in the generator's collision list or another call site took it first in this runtime. The test app's specs pass with and without the opt-in. Specs that need a class generated at runtime, or created by Java by class name, give their extends a member no other extend has. --- test-app/app/src/main/assets/app/mainpage.js | 1 + .../app/tests/appModuleBindingsWorker.js | 3 +- .../app/tests/contentKeyedBindingsWorker.js | 44 ++ .../assets/app/tests/contentKeyedFixtures.js | 93 ++++ .../assets/app/tests/testAppModuleBindings.js | 5 +- .../app/tests/testContentKeyedBindings.js | 402 ++++++++++++++++++ .../src/main/assets/app/tests/testMetadata.js | 7 +- .../app/tests/testPrebuiltExtendClasses.js | 7 +- .../app/tests/testRuntimeGenerationReport.js | 7 +- .../runtime/src/main/cpp/CallbackHandlers.cpp | 115 ++++- .../runtime/src/main/cpp/CallbackHandlers.h | 27 +- .../runtime/src/main/cpp/MetadataNode.cpp | 142 ++++++- test-app/runtime/src/main/cpp/MetadataNode.h | 32 ++ .../runtime/src/main/cpp/com_tns_Runtime.cpp | 14 + .../src/main/java/com/tns/Runtime.java | 31 ++ 15 files changed, 887 insertions(+), 43 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/contentKeyedBindingsWorker.js create mode 100644 test-app/app/src/main/assets/app/tests/contentKeyedFixtures.js create mode 100644 test-app/app/src/main/assets/app/tests/testContentKeyedBindings.js diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index 8d3450e47..a444af473 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -76,6 +76,7 @@ require("./tests/testPrebuiltExtendClasses"); require("./tests/testRuntimeGenerationReport"); require("./tests/testInterfaceExtendClasses"); require("./tests/testAppModuleBindings"); +require("./tests/testContentKeyedBindings"); require("./tests/testProxyRegistrationGc"); require("./tests/testInterfaceImplementation"); require("./tests/testPrimitiveCallbackReturns"); diff --git a/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js b/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js index 4e840dd7f..cd6ffdb86 100644 --- a/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js +++ b/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js @@ -12,7 +12,8 @@ try { var Extended = com.tns.tests.Button1[ext]({ toString: function () { return "worker extend"; - } + }, + workerExtendMarker: true }); var extended = new Extended(); result.extendedClass = String(extended.getClass().getName()); diff --git a/test-app/app/src/main/assets/app/tests/contentKeyedBindingsWorker.js b/test-app/app/src/main/assets/app/tests/contentKeyedBindingsWorker.js new file mode 100644 index 000000000..8018ce2b6 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/contentKeyedBindingsWorker.js @@ -0,0 +1,44 @@ +// Entry for testContentKeyedBindings: the same content as call sites of the main +// runtime, one prebuilt, one generated at runtime, and a named one. +var ext = "ex" + "tend"; +var result = {}; + +function javaToString(instance) { + return String(instance.getClass().getMethod("toString", []).invoke(instance, [])); +} + +try { + var Prebuilt = java.lang.Object.extend({ + toString: function () { + return "worker prebuilt"; + }, + workerContentMarker: 1 + }); + var prebuilt = new Prebuilt(); + result.prebuiltClass = String(prebuilt.getClass().getName()); + result.prebuiltString = javaToString(prebuilt); + + var Hidden = java.lang.Object[ext]({ + toString: function () { + return "worker hidden"; + }, + workerHiddenMarker: 1 + }); + var hidden = new Hidden(); + result.hiddenClass = String(hidden.getClass().getName()); + result.hiddenString = javaToString(hidden); + result.hiddenCreatedString = javaToString(hidden.getClass().newInstance()); + + var Owned = java.lang.Object.extend("WorkerOwnedName", { + toString: function () { + return "worker owned"; + } + }); + var owned = new Owned(); + result.ownedClass = String(owned.getClass().getName()); + result.ownedString = javaToString(owned); + result.ownedCreatedString = javaToString(owned.getClass().newInstance()); +} catch (e) { + result.error = String(e && e.stack || e); +} +postMessage(result); diff --git a/test-app/app/src/main/assets/app/tests/contentKeyedFixtures.js b/test-app/app/src/main/assets/app/tests/contentKeyedFixtures.js new file mode 100644 index 000000000..d3e44a736 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/contentKeyedFixtures.js @@ -0,0 +1,93 @@ +// Native classes in the shapes bundlers emit for `@NativeClass` TypeScript and for +// plain JS. The static binding generator must prebuild each of them under the +// class name the runtime asks for. + +var getterCalls = 0; + +function profile(target, key, descriptor) { + return descriptor; +} + +var DecoratedFixture = /** @class */ (function (_super) { + __extends(DecoratedFixture, _super); + function DecoratedFixture() { + var _this = _super.call(this) || this; + _this.backgroundBitmap = null; + return global.__native(_this); + } + DecoratedFixture.newInstance = function () { + return new DecoratedFixture(); + }; + DecoratedFixture.prototype.toString = function () { + return "decorated fixture"; + }; + DecoratedFixture.prototype.run = function () { + this.ran = true; + }; + Object.defineProperty(DecoratedFixture.prototype, "items", { + get: function () { + getterCalls++; + return []; + }, + enumerable: true, + configurable: true + }); + __decorate([profile], DecoratedFixture.prototype, "toString", null); + DecoratedFixture = __decorate([Interfaces([java.lang.Runnable])], DecoratedFixture); + return DecoratedFixture; +}(java.lang.Object)); + +var ApplyFixture = function (_super) { + __extends(ApplyFixture, _super); + function ApplyFixture() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this.count = 0; + return _this; + } + ApplyFixture.prototype.toString = function () { + return "apply fixture"; + }; + ApplyFixture.prototype.applyFixtureMarker = 1; + return ApplyFixture; +}(java.lang.Object); + +var NoConstructorFixture = /** @class */ (function (_super) { + __extends(NoConstructorFixture, _super); + function NoConstructorFixture() { + return _super !== null && _super.apply(this, arguments) || this; + } + NoConstructorFixture.prototype.toString = function () { + return "no constructor fixture"; + }; + NoConstructorFixture.prototype.noConstructorFixtureMarker = 1; + return NoConstructorFixture; +}(java.lang.Object)); + +var QuotedKeysFixture = java.lang.Object.extend({ + "toString": function () { + return "quoted keys fixture"; + }, + "quotedKeysFixtureMarker": 1, + get ignoredGetter() { + getterCalls++; + return 1; + } +}); + +var NestedBaseFixture = com.tns.tests.DummyClass.DummyDerivedClass.extend({ + toString: function () { + return "nested base fixture"; + }, + nestedBaseFixtureMarker: 1 +}); + +exports.fixtures = { + DecoratedFixture: { ctor: DecoratedFixture, toString: "decorated fixture" }, + ApplyFixture: { ctor: ApplyFixture, toString: "apply fixture" }, + NoConstructorFixture: { ctor: NoConstructorFixture, toString: "no constructor fixture" }, + QuotedKeysFixture: { ctor: QuotedKeysFixture, toString: "quoted keys fixture" }, + NestedBaseFixture: { ctor: NestedBaseFixture, toString: "nested base fixture" } +}; +exports.getterCalls = function () { + return getterCalls; +}; diff --git a/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js b/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js index c21477ef6..6b865b298 100644 --- a/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js +++ b/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js @@ -60,10 +60,13 @@ describe("Bindings of app-module types", function () { describe("App-module types generated at runtime", function () { it("generates a hidden extend of an app-module class", function () { + // A member no other extend has keeps a content-keyed name from matching + // the class prebuilt for the extend above. var Extended = com.tns.tests.Button1[ext]({ toString: function () { return "runtime extend"; - } + }, + runtimeExtendMarker: true }); var instance = new Extended(); diff --git a/test-app/app/src/main/assets/app/tests/testContentKeyedBindings.js b/test-app/app/src/main/assets/app/tests/testContentKeyedBindings.js new file mode 100644 index 000000000..697dd4111 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testContentKeyedBindings.js @@ -0,0 +1,402 @@ +// The test app is built and run both with and without -PuseContentBindings; these +// specs pass in both, and differ only where the class name format does. +describe("Content-keyed binding names", function () { + + var contentKeyed = (function () { + try { + java.lang.Class.forName("com.tns.gen.ContentKeyedBindings", false, com.tns.Runtime.class.getClassLoader()); + return true; + } catch (e) { + return false; + } + }()); + console.log("Binding class names are " + (contentKeyed ? "content-keyed" : "call-site-keyed") + " in this build"); + + // A computed member hides an extend call from the static binding generator. + var ext = "ex" + "tend"; + var tsExtends = global["__" + "extends"]; + + function runtimeGenerated(instance) { + var loadedName = String(instance.getClass().getName()); + var lines = com.tns.DexFactory.getRuntimeGeneratedClasses(); + for (var i = 0; i < lines.length; i++) { + if (String(lines[i]).split(" ")[2] === loadedName) { + return true; + } + } + return false; + } + + function className(instance) { + return String(instance.getClass().getName()); + } + + // Runs the class's own toString override through Java. + function javaToString(instance) { + return String(instance.getClass().getMethod("toString", []).invoke(instance, [])); + } + + function createFromJava(instance) { + return instance.getClass().newInstance(); + } + + // Defined when this file loads, in this order: the first of two call sites + // sharing a class is the one Java gets when it creates that class by name. + var SharedFirst = java.lang.Object.extend({ + toString: function () { + return "shared first"; + }, + sharedContentMarker: 1 + }); + var SharedSecond = java.lang.Object.extend({ + toString: function () { + return "shared second"; + }, + sharedContentMarker: 2 + }); + var SharedEdited = java.lang.Object.extend({ + toString: function () { + return "shared edited"; + }, + sharedContentMarker: 3, + editedMember: function () { + } + }); + + var CollidingFirst = java.lang.Object.extend("CollidingName", { + toString: function () { + return "colliding first"; + } + }); + var CollidingSecond = java.lang.Object.extend("CollidingName", { + toString: function () { + return "colliding second"; + } + }); + + var OwnedVisible = java.lang.Object.extend("OwnedName", { + toString: function () { + return "owned visible"; + }, + ownedNameMarker: 1 + }); + var OwnedHidden = java.lang.Object[ext]("OwnedName", { + toString: function () { + return "owned hidden"; + }, + ownedNameMarker: 1 + }); + + it("names a class after its call site or after its content", function () { + var Keyed = java.lang.Object.extend({ + toString: function () { + return "keyed"; + }, + namingFormatMarker: 1 + }); + var instance = new Keyed(); + + expect(javaToString(instance)).toBe("keyed"); + expect(runtimeGenerated(instance)).toBe(false); + if (contentKeyed) { + expect(className(instance)).toMatch(/^com\.tns\.gen\.java\.lang\.Object_h[0-9a-f]{16}$/); + } else { + expect(className(instance)).toMatch(/^com\.tns\.gen\.java\.lang\.Object_tests_testContentKeyedBindings_[0-9a-f]{6}_\d+_\d+_$/); + } + }); + + describe("agrees with the static binding generator's ContentKeyTest", function () { + + // The class of each call site below is generated on the device, so its name + // is computed by the runtime alone. + function expectRuntimeName(instance, suffix) { + expect(runtimeGenerated(instance)).toBe(true); + if (contentKeyed) { + expect(className(instance)).toBe(suffix); + } + } + + it("for a plain base", function () { + var Plain = java.lang.Object[ext]({ + toString: function () { + return "pairing plain"; + }, + contentKeyPairingPlain: true + }); + var instance = new Plain(); + expect(javaToString(instance)).toBe("pairing plain"); + expectRuntimeName(instance, "java.lang.Object_he8b99c521d575fca"); + }); + + it("for a nested base", function () { + var Nested = com.tns.tests.DummyClass.DummyDerivedClass[ext]({ + toString: function () { + return "pairing nested"; + }, + contentKeyPairingNested: true + }); + var instance = new Nested(); + expect(javaToString(instance)).toBe("pairing nested"); + expectRuntimeName(instance, "com.tns.tests.DummyClass_DummyDerivedClass_h68c7e45e43205e66"); + }); + + it("for implemented interfaces", function () { + var calls = []; + var WithInterfaces = java.lang.Object[ext]({ + interfaces: [java.lang.Runnable, com.tns.tests.DummyClass.MyInterface], + run: function () { + calls.push("run"); + }, + echoInt: function (i) { + return i; + }, + doSomething: function () { + }, + contentKeyPairingInterfaces: true + }); + var instance = new WithInterfaces(); + new java.lang.Thread(instance).run(); + expect(calls).toEqual(["run"]); + expectRuntimeName(instance, "java.lang.Object_ha59a9a5b07d44d2b"); + }); + + it("for a named extend", function () { + var Named = java.lang.Object[ext]("PairingNamed", { + toString: function () { + return "pairing named"; + }, + contentKeyPairingNamed: true + }); + var instance = new Named(); + expect(javaToString(instance)).toBe("pairing named"); + expectRuntimeName(instance, "java.lang.Object_PairingNamed_h63c03cfe49192d3d"); + }); + + it("for a TypeScript class", function () { + var PairingTsClass = (function (_super) { + tsExtends(PairingTsClass, _super); + function PairingTsClass() { + var _this = _super.call(this) || this; + return global.__native(_this); + } + PairingTsClass.create = function () { + return new PairingTsClass(); + }; + PairingTsClass.prototype.toString = function () { + return "pairing ts"; + }; + PairingTsClass.prototype.contentKeyPairingTs = true; + return PairingTsClass; + }(java.lang.Object)); + var instance = PairingTsClass.create(); + expect(javaToString(instance)).toBe("pairing ts"); + expectRuntimeName(instance, "java.lang.Object_PairingTsClass_hc5ac23ca0a98a3c1"); + }); + }); + + describe("named extends whose implementation the generator cannot read", function () { + + it("load their own class, not the generator's class without members", function () { + var implementation = { + toString: function () { + return "named variable"; + } + }; + var NamedVariable = java.lang.Object.extend("NamedVariable", implementation); + var instance = new NamedVariable(); + + expect(javaToString(instance)).toBe("named variable"); + expect(runtimeGenerated(instance)).toBe(true); + }); + + it("load their own class even when they have no members", function () { + var emptyImplementation = {}; + var NamedEmptyVariable = java.lang.Object.extend("NamedEmptyVariable", emptyImplementation); + var instance = new NamedEmptyVariable(); + + expect(runtimeGenerated(instance)).toBe(true); + if (contentKeyed) { + expect(className(instance)).toBe("java.lang.Object_NamedEmptyVariable_h86a36c7f58a561ea"); + } + }); + + it("leave the class without members to anonymous extends without members", function () { + var Empty = java.lang.Object.extend({}); + var instance = new Empty(); + + expect(runtimeGenerated(instance)).toBe(false); + if (contentKeyed) { + expect(className(instance)).toBe("com.tns.gen.java.lang.Object_h86a36c7f58a561ea"); + } + }); + }); + + describe("prebuilds the shapes bundlers emit", function () { + var fixtures = require("./contentKeyedFixtures"); + + Object.keys(fixtures.fixtures).forEach(function (name) { + it("for " + name, function () { + var fixture = fixtures.fixtures[name]; + var instance = new fixture.ctor(); + + expect(javaToString(instance)).toBe(fixture.toString); + expect(runtimeGenerated(instance)).toBe(false); + if (contentKeyed) { + expect(className(instance)).toMatch(/_h[0-9a-f]{16}$/); + } + }); + }); + + it("without running getters of the implementation object", function () { + Object.keys(fixtures.fixtures).forEach(function (name) { + new fixtures.fixtures[name].ctor(); + }); + if (contentKeyed) { + expect(fixtures.getterCalls()).toBe(0); + } + }); + + it("with the interfaces from @Interfaces", function () { + var instance = new fixtures.fixtures.DecoratedFixture.ctor(); + new java.lang.Thread(instance).run(); + expect(instance.ran).toBe(true); + }); + }); + + describe("shared classes", function () { + + it("give call sites with the same content one class and their own constructors", function () { + var first = new SharedFirst(); + var second = new SharedSecond(); + + expect(SharedFirst).not.toBe(SharedSecond); + expect(javaToString(first)).toBe("shared first"); + expect(javaToString(second)).toBe("shared second"); + expect(runtimeGenerated(first)).toBe(false); + expect(runtimeGenerated(second)).toBe(false); + if (contentKeyed) { + expect(className(second)).toBe(className(first)); + } else { + expect(className(second)).not.toBe(className(first)); + } + }); + + it("give a call site that runs again its first constructor", function () { + function extendAgain() { + return java.lang.Object.extend({ + toString: function () { + return "extended again"; + }, + sharedContentMarker: 4 + }); + } + var first = extendAgain(); + expect(extendAgain()).toBe(first); + expect(javaToString(new first())).toBe("extended again"); + }); + + it("give a call site with an extra member another class", function () { + var edited = new SharedEdited(); + + expect(javaToString(edited)).toBe("shared edited"); + expect(className(edited)).not.toBe(className(new SharedFirst())); + expect(runtimeGenerated(edited)).toBe(false); + }); + + it("are created from Java with the implementation of the first call site", function () { + var created = createFromJava(new SharedSecond()); + + expect(javaToString(created)).toBe(contentKeyed ? "shared first" : "shared second"); + }); + }); + + describe("named extends", function () { + + it("with the same name and content keep their call-site classes", function () { + var first = new CollidingFirst(); + var second = new CollidingSecond(); + + expect(className(first)).not.toBe(className(second)); + expect(className(first)).toMatch(/_CollidingName$/); + expect(className(second)).toMatch(/_CollidingName$/); + expect(runtimeGenerated(first)).toBe(false); + expect(runtimeGenerated(second)).toBe(false); + expect(javaToString(createFromJava(first))).toBe("colliding first"); + expect(javaToString(createFromJava(second))).toBe("colliding second"); + + if (contentKeyed) { + var collisions = com.tns.gen.ContentKeyedBindings.COLLISIONS; + var listed = []; + for (var i = 0; i < collisions.length; i++) { + listed.push(String(collisions[i])); + } + expect(listed.filter(function (name) { + return /^com\.tns\.gen\.java\.lang\.Object_CollidingName_h[0-9a-f]{16}$/.test(name); + }).length).toBe(1); + } + }); + + it("with the content of a name another call site took first get their call-site class", function () { + var visible = new OwnedVisible(); + var hidden = new OwnedHidden(); + + expect(className(visible)).not.toBe(className(hidden)); + expect(runtimeGenerated(visible)).toBe(false); + expect(runtimeGenerated(hidden)).toBe(true); + expect(javaToString(createFromJava(visible))).toBe("owned visible"); + expect(javaToString(createFromJava(hidden))).toBe("owned hidden"); + if (contentKeyed) { + expect(className(visible)).toMatch(/^com\.tns\.gen\.java\.lang\.Object_OwnedName_h[0-9a-f]{16}$/); + expect(className(hidden)).toMatch(/_tests_testContentKeyedBindings_[0-9a-f]{6}_\d+_\d+_OwnedName$/); + } + }); + }); + + it("resolves and constructs content-keyed classes in a worker", function (done) { + // Same content as the worker's call sites, so the main runtime has the + // classes before the worker asks for them. + var MainPrebuilt = java.lang.Object.extend({ + toString: function () { + return "main prebuilt"; + }, + workerContentMarker: 1 + }); + var MainHidden = java.lang.Object[ext]({ + toString: function () { + return "main hidden"; + }, + workerHiddenMarker: 1 + }); + var MainOwned = java.lang.Object[ext]("WorkerOwnedName", { + toString: function () { + return "main owned"; + } + }); + var mainPrebuilt = new MainPrebuilt(); + var mainHidden = new MainHidden(); + var mainOwned = new MainOwned(); + + var worker = new Worker("./contentKeyedBindingsWorker.js"); + worker.onmessage = function (msg) { + worker.terminate(); + var result = msg.data; + expect(result.error).toBeUndefined(); + expect(result.prebuiltString).toBe("worker prebuilt"); + expect(result.hiddenString).toBe("worker hidden"); + expect(result.hiddenCreatedString).toBe("worker hidden"); + expect(result.ownedString).toBe("worker owned"); + expect(result.ownedCreatedString).toBe("worker owned"); + expect(javaToString(createFromJava(mainOwned))).toBe("main owned"); + expect(runtimeGenerated(mainPrebuilt)).toBe(false); + expect(runtimeGenerated(mainHidden)).toBe(true); + if (contentKeyed) { + expect(result.prebuiltClass).toBe(className(mainPrebuilt)); + expect(result.hiddenClass).toBe(className(mainHidden)); + // Each runtime's first call site keeps a named content-keyed class. + expect(result.ownedClass).toBe(className(mainOwned)); + expect(className(mainOwned)).toMatch(/^com\.tns\.gen\.java\.lang\.Object_WorkerOwnedName_h[0-9a-f]{16}$/); + } + done(); + }; + }); +}); diff --git a/test-app/app/src/main/assets/app/tests/testMetadata.js b/test-app/app/src/main/assets/app/tests/testMetadata.js index 739917953..dee1af2b3 100644 --- a/test-app/app/src/main/assets/app/tests/testMetadata.js +++ b/test-app/app/src/main/assets/app/tests/testMetadata.js @@ -85,8 +85,11 @@ describe("Java object wrappers", function () { expect(function () { return impl.super; }).not.toThrow(); }); + // Java creates the extended instances in this spec and in "give a Java-created extended + // instance ..." by class name, so their extends need a member no other extend has: a + // content-keyed class shared with other call sites gets the first one's implementation. it("leave an extended instance Java creates during a JS construction its own JS object", function () { - var Inner = java.lang.Object.extend({ toString: function () { return "inner"; } }); + var Inner = java.lang.Object.extend({ toString: function () { return "inner"; }, nestedConstructionInner: true }); var Outer = com.tns.tests.NestedConstruction.extend({ toString: function () { return "outer"; } }); var outer = new Outer(new Inner().getClass(), null); @@ -111,7 +114,7 @@ describe("Java object wrappers", function () { }); it("give a Java-created extended instance its class's current prototype", function () { - var Created = java.lang.Object.extend({ toString: function () { return "created"; } }); + var Created = java.lang.Object.extend({ toString: function () { return "created"; }, javaCreatedPrototype: true }); var createdClass = new Created().getClass(); com.tns.tests.JavaCreated.newInstance(createdClass); var replacement = Object.create(Object.getPrototypeOf(Created.prototype)); diff --git a/test-app/app/src/main/assets/app/tests/testPrebuiltExtendClasses.js b/test-app/app/src/main/assets/app/tests/testPrebuiltExtendClasses.js index 9c113e43a..640d3f2e4 100644 --- a/test-app/app/src/main/assets/app/tests/testPrebuiltExtendClasses.js +++ b/test-app/app/src/main/assets/app/tests/testPrebuiltExtendClasses.js @@ -89,12 +89,15 @@ describe("Prebuilt classes of anonymous extends", function () { }); it("caches a class generated at runtime under the name DexFactory looks up", function () { - // A computed member hides the extend from the static binding generator. + // A computed member hides the extend from the static binding generator, and + // a member no other extend has keeps a content-keyed name from matching a + // prebuilt class. var ext = "ex" + "tend"; var RuntimeGenerated = java.lang.Object[ext]({ toString: function () { return "runtime generated"; - } + }, + runtimeGeneratedMarker: true }); var instance = new RuntimeGenerated(); diff --git a/test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js b/test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js index 22e1122f3..66a7d910e 100644 --- a/test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js +++ b/test-app/app/src/main/assets/app/tests/testRuntimeGenerationReport.js @@ -13,12 +13,15 @@ describe("Runtime class generation report", function () { } it("lists a class generated at runtime", function () { - // A computed member hides the extend from the static binding generator. + // A computed member hides the extend from the static binding generator, and + // a member no other extend has keeps a content-keyed name from matching a + // prebuilt class. var ext = "ex" + "tend"; var ReportedObject = java.lang.Object[ext]({ toString: function () { return "reported"; - } + }, + reportedObjectMarker: true }); var instance = new ReportedObject(); expect(instance.toString()).toBe("reported"); diff --git a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp index 92dd9afb4..5dd0b9ce9 100644 --- a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp +++ b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp @@ -9,6 +9,7 @@ #include "JsArgToArrayConverter.h" #include "ArgConverter.h" #include "v8-profiler.h" +#include #include #include #include @@ -141,7 +142,8 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local &j const ArgsWrapper &argWrapper, const Local &implementationObject, bool isInterface, - const std::string &baseClassName) { + const std::string &baseClassName, + const vector *contentMemberNames) { bool success; DEBUG_WRITE("RegisterInstance called for '%s'", fullClassName.c_str()); @@ -157,7 +159,7 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local &j jclass generatedJavaClass = ResolveClass(isolate, baseClassName, fullClassName, implementationObject, - isInterface); + isInterface, contentMemberNames); int javaObjectID = objectManager->GenerateNewObjectID(); @@ -203,7 +205,8 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local &j jclass CallbackHandlers::ResolveClass(Isolate *isolate, const string &baseClassName, const string &fullClassName, - const Local &implementationObject, bool isInterface) { + const Local &implementationObject, bool isInterface, + const vector *contentMemberNames) { JEnv env; jclass globalRefToGeneratedClass = env.CheckForClassInCache(fullClassName); @@ -213,7 +216,11 @@ jclass CallbackHandlers::ResolveClass(Isolate *isolate, const string &baseClassN JniLocalRef javaBaseClassName(env.NewStringUTF(baseClassName.c_str())); JniLocalRef javaFullClassName(env.NewStringUTF(fullClassName.c_str())); - jobjectArray methodOverrides = GetMethodOverrides(env, implementationObject); + // A content-keyed class must get the overrides the static binding generator + // writes for its name, which are not only the function-valued members. + jobjectArray methodOverrides = contentMemberNames != nullptr + ? ToJavaStringArray(env, *contentMemberNames) + : GetMethodOverrides(env, implementationObject); jobjectArray implementedInterfaces = GetImplementedInterfaces(env, implementationObject); @@ -636,14 +643,13 @@ Local CallbackHandlers::CreateJSWrapper(Isolate *isolate, jint javaObjec } -jobjectArray -CallbackHandlers::GetImplementedInterfaces(JEnv &env, const Local &implementationObject) { +vector +CallbackHandlers::CollectImplementedInterfaceNames(Isolate *isolate, const Local &implementationObject) { + vector interfaceNames; if (implementationObject.IsEmpty()) { - return CallbackHandlers::GetJavaStringArray(env, 0); + return interfaceNames; } - vector interfacesToImplement; - auto isolate = v8::Isolate::GetCurrent(); auto context = GetCreationContextOrCurrent(isolate, implementationObject); Local interfacesName = String::NewFromUtf8Literal(isolate, "interfaces"); Local prop; @@ -660,26 +666,95 @@ CallbackHandlers::GetImplementedInterfaces(JEnv &env, const Local &imple if (element->IsFunction()) { auto node = MetadataNode::GetTypeMetadataName(isolate, element); - node = Util::ReplaceAll(node, std::string("/"), std::string(".")); - - jstring value = env.NewStringUTF(node.c_str()); - interfacesToImplement.push_back(value); + interfaceNames.push_back(Util::ReplaceAll(node, std::string("/"), std::string("."))); } } } - int interfacesCount = interfacesToImplement.size(); + return interfaceNames; +} + +jobjectArray +CallbackHandlers::GetImplementedInterfaces(JEnv &env, const Local &implementationObject) { + return ToJavaStringArray(env, CollectImplementedInterfaceNames(v8::Isolate::GetCurrent(), implementationObject)); +} + +jobjectArray CallbackHandlers::ToJavaStringArray(JEnv &env, const vector &values) { + int count = values.size(); + jobjectArray result = CallbackHandlers::GetJavaStringArray(env, count); + for (int i = 0; i < count; i++) { + JniLocalRef value(env.NewStringUTF(values[i].c_str())); + env.SetObjectArrayElement(result, i, (jstring) value); + } + return result; +} - jobjectArray implementedInterfaces = CallbackHandlers::GetJavaStringArray(env, interfacesCount); - for (int i = 0; i < interfacesCount; i++) { - env.SetObjectArrayElement(implementedInterfaces, i, interfacesToImplement[i]); +// Must select the same names as the static binding generator's +// ContentKey.memberNames applied to the JS parser's member list. +static bool IsContentMemberName(const string &name) { + if (name.empty()) { + return false; + } + for (size_t i = 0; i < name.size(); i++) { + char c = name[i]; + bool valid = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || c == '$' || + (i > 0 && c >= '0' && c <= '9'); + if (!valid) { + return false; + } } + return name != "constructor" && name != "super" && name != "__parent" && name != "__child"; +} + +vector +CallbackHandlers::CollectContentMemberNames(Isolate *isolate, const Local &implementationObject) { + vector memberNames; + if (implementationObject.IsEmpty()) { + return memberNames; + } + + auto context = GetCreationContextOrCurrent(isolate, implementationObject); + Local keys; + if (!implementationObject->GetOwnPropertyNames(context, + static_cast(ONLY_ENUMERABLE | SKIP_SYMBOLS), + KeyConversionMode::kConvertToString).ToLocal(&keys)) { + return memberNames; + } + + auto interfacesName = String::NewFromUtf8Literal(isolate, "interfaces"); + auto valueName = String::NewFromUtf8Literal(isolate, "value"); + for (uint32_t i = 0; i < keys->Length(); i++) { + Local key; + if (!keys->Get(context, i).ToLocal(&key) || !key->IsString()) { + continue; + } + string name = ArgConverter::ConvertToString(key.As()); + if (!IsContentMemberName(name)) { + continue; + } + + // Reads the descriptor, not the property: a getter must not run here. + Local descriptor; + if (!implementationObject->GetOwnPropertyDescriptor(context, key.As()).ToLocal(&descriptor) || + !descriptor->IsObject()) { + continue; + } + Local value; + auto descriptorObject = descriptor.As(); + if (!descriptorObject->HasOwnProperty(context, valueName).FromMaybe(false) || + !descriptorObject->Get(context, valueName).ToLocal(&value)) { + continue; + } + if (value->IsArray() && key.As()->StringEquals(interfacesName)) { + continue; + } - for (int i = 0; i < interfacesCount; i++) { - env.DeleteLocalRef(interfacesToImplement[i]); + memberNames.push_back(name); } - return implementedInterfaces; + sort(memberNames.begin(), memberNames.end()); + memberNames.erase(unique(memberNames.begin(), memberNames.end()), memberNames.end()); + return memberNames; } jobjectArray diff --git a/test-app/runtime/src/main/cpp/CallbackHandlers.h b/test-app/runtime/src/main/cpp/CallbackHandlers.h index 43f040dd9..6ec8a39c2 100644 --- a/test-app/runtime/src/main/cpp/CallbackHandlers.h +++ b/test-app/runtime/src/main/cpp/CallbackHandlers.h @@ -40,12 +40,33 @@ namespace tns { const ArgsWrapper &argWrapper, const v8::Local &implementationObject, bool isInterface, - const std::string &baseClassName = std::string()); + const std::string &baseClassName = std::string(), + const std::vector *contentMemberNames = nullptr); + /* + * contentMemberNames, when given, are the method overrides of a class generated + * at runtime, instead of the implementation object's function-valued members. + */ static jclass ResolveClass(v8::Isolate *isolate, const std::string &baseClassName, const std::string &fullClassName, const v8::Local &implementationObject, - bool isInterface); + bool isInterface, + const std::vector *contentMemberNames = nullptr); + + /* + * The interface names the implementation object's `interfaces` array lists, + * dot-separated, as GetImplementedInterfaces passes them to Java. + */ + static std::vector + CollectImplementedInterfaceNames(v8::Isolate *isolate, const v8::Local &implementationObject); + + /* + * The member set a content-keyed class name is derived from: the names of the + * object's own enumerable data properties that can name a Java method, except + * the ones extend() and __extends add, sorted by bytes and without duplicates. + */ + static std::vector + CollectContentMemberNames(v8::Isolate *isolate, const v8::Local &implementationObject); static std::string ResolveClassName(v8::Isolate *isolate, jclass &clazz); @@ -179,6 +200,8 @@ namespace tns { */ static jobjectArray GetJavaStringArray(JEnv &env, int length); + static jobjectArray ToJavaStringArray(JEnv &env, const std::vector &values); + static void validateProvidedArgumentsLength(const v8::FunctionCallbackInfo &args, int expectedSize); static short MAX_JAVA_STRING_ARRAY_LENGTH; diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index e8f7815aa..320ffc42e 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -10,6 +10,7 @@ #include "CallbackHandlers.h" #include "NativeScriptException.h" #include "Runtime.h" +#include #include #include #include @@ -115,6 +116,11 @@ static Local ResolveProxyReceiver(Isolate* isolate, const Local& return object; } +static string DottedClassName(string className) { + std::replace(className.begin(), className.end(), '/', '.'); + return className; +} + static Local ResolveImplementationObject(Isolate* isolate, const Local& implementationObject) { if (!implementationObject->IsProxy()) { return implementationObject; @@ -193,6 +199,17 @@ Local MetadataNode::CreateExtendedJSWrapper(Isolate* isolate, ObjectMana auto cacheData = GetCachedExtendedClassData(isolate, proxyClassName); if (cacheData.node != nullptr) { + auto cache = GetMetadataNodeCache(isolate); + if (!cache->SharedExtendedClasses.empty()) { + auto shared = cache->SharedExtendedClasses.find(proxyClassName); + if (shared != cache->SharedExtendedClasses.end() && !shared->second) { + shared->second = true; + __android_log_print(ANDROID_LOG_WARN, "JS", + "Java created an instance of %s by its class name, but more than one extend() call shares that class; using the implementation of the first one. Give the class a unique name, e.g. with @JavaProxy.", + DottedClassName(proxyClassName).c_str()); + } + } + // Starts on the extended class's initial map, with its prototype, and without // running its constructor callback. auto context = Runtime::GetRuntime(isolate)->GetContext(); @@ -1401,7 +1418,8 @@ void MetadataNode::ExtendedClassConstructorCallback(const v8::FunctionCallbackIn string fullClassName = extData->fullClassName; - bool success = CallbackHandlers::RegisterInstance(isolate, thiz, fullClassName, argWrapper, implementationObject, false, extData->node->m_name); + bool success = CallbackHandlers::RegisterInstance(isolate, thiz, fullClassName, argWrapper, implementationObject, false, extData->node->m_name, + extData->contentMemberNames ? &*extData->contentMemberNames : nullptr); } catch (NativeScriptException& e) { e.ReThrowToV8(); } catch (std::exception e) { @@ -1874,6 +1892,57 @@ string MetadataNode::CreateFullClassName(const std::string& className, const std return fullClassName; } +void MetadataNode::InitContentKeyedBindings(const vector& collisions) { + static std::once_flag once; + std::call_once(once, [&collisions]() { + auto bindings = new ContentKeyedBindings(); + bindings->collisions.insert(collisions.begin(), collisions.end()); + s_contentKeyedBindings.store(bindings, std::memory_order_release); + }); +} + +// JS source spells a nested type like a package, `a.B.C`, and the static binding +// generator reads names from JS source; both separators become '.'. +static string CanonicalTypeName(string name) { + std::replace(name.begin(), name.end(), '/', '.'); + std::replace(name.begin(), name.end(), '$', '.'); + return name; +} + +static string JoinSortedUnique(vector& values) { + values.erase(std::remove(values.begin(), values.end(), string()), values.end()); + std::sort(values.begin(), values.end()); + values.erase(std::unique(values.begin(), values.end()), values.end()); + + string joined; + for (size_t i = 0; i < values.size(); i++) { + if (i > 0) { + joined += ","; + } + joined += values[i]; + } + return joined; +} + +string MetadataNode::CreateContentKey(const string& baseClassName, vector interfaceNames, vector memberNames) { + for (auto& interfaceName : interfaceNames) { + interfaceName = CanonicalTypeName(interfaceName); + } + + string payload = CanonicalTypeName(baseClassName) + "|" + JoinSortedUnique(interfaceNames) + "|" + JoinSortedUnique(memberNames); + + uint64_t hash = 0xcbf29ce484222325ULL; + for (unsigned char c : payload) { + hash ^= c; + hash *= 0x100000001b3ULL; + } + + char key[18]; + snprintf(key, sizeof(key), "h%016llx", static_cast(hash)); + DEBUG_WRITE("CreateContentKey: %s -> %s", payload.c_str(), key); + return key; +} + void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfo& info) { tns::instrumentation::Frame frame; try { @@ -1930,9 +1999,10 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfo(info.Data().As()->Value(v8::kExternalPointerTypeTagDefault)); - DEBUG_WRITE("ExtendsCallMethodHandler: called with %s", ArgConverter::ConvertToString(extendName).c_str()); + string extendNameString = ArgConverter::ConvertToString(extendName); + DEBUG_WRITE("ExtendsCallMethodHandler: called with %s", extendNameString.c_str()); - string extendNameAndLocation = extendLocation + ArgConverter::ConvertToString(extendName); + string extendNameAndLocation = extendLocation + extendNameString; string fullClassName; string baseClassName = node->m_name; if (!hasDot) { @@ -1941,17 +2011,54 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfo()); } - //resolve class (pre-generated or generated runtime from dex generator) uint8_t nodeType = s_metadataReader.GetNodeType(node->m_treeNode); bool isInterface = s_metadataReader.IsNodeTypeInterface(nodeType); - auto clazz = CallbackHandlers::ResolveClass(isolate, baseClassName, fullClassName, implementationObject, isInterface); + + // The call-site name identifies the extend() call within a runtime even when + // the Java class is named after its content and shared with other calls. + const string siteClassName = fullClassName; + auto contentKeyedBindings = s_contentKeyedBindings.load(std::memory_order_acquire); + bool contentKeyed = contentKeyedBindings != nullptr && !hasDot && !isInterface; + string contentClassName; + std::optional> contentMemberNames; + auto cache = GetMetadataNodeCache(isolate); + if (contentKeyed) { + auto cachedData = GetCachedExtendedClassData(isolate, siteClassName); + if (cachedData.extendedCtorFunction != nullptr) { + info.GetReturnValue().Set(Local::New(isolate, *cachedData.extendedCtorFunction)); + return; + } + + contentMemberNames = CallbackHandlers::CollectContentMemberNames(isolate, implementationObject); + auto contentKey = CreateContentKey(baseClassName, CallbackHandlers::CollectImplementedInterfaceNames(isolate, implementationObject), *contentMemberNames); + contentClassName = TNS_PREFIX + CreateFullClassName(baseClassName, extendNameString.empty() ? contentKey : extendNameString + "_" + contentKey); + + // Java finds a named extend's implementation by its class name alone, so a + // named content-keyed class belongs to one call site: the generator's + // collision list names the ones it saw at more than one, and otherwise + // the first call site in this runtime keeps it. + bool useSiteClassName = false; + if (!extendNameString.empty()) { + auto owner = cache->ContentNameOwners.find(contentClassName); + useSiteClassName = (owner != cache->ContentNameOwners.end() && owner->second != siteClassName) || + contentKeyedBindings->collisions.count(DottedClassName(contentClassName)) > 0; + } + if (!useSiteClassName) { + fullClassName = contentClassName; + } + } + + //resolve class (pre-generated or generated runtime from dex generator) + auto clazz = CallbackHandlers::ResolveClass(isolate, baseClassName, fullClassName, implementationObject, isInterface, + contentMemberNames ? &*contentMemberNames : nullptr); auto fullExtendedName = CallbackHandlers::ResolveClassName(isolate, clazz); DEBUG_WRITE("ExtendsCallMethodHandler: extend full name %s", fullClassName.c_str()); // Every anonymous extend of an interface resolves to the interface's one - // class, so keying its constructor by that class would hand each call - // site the implementation object of the first. - const string& extendedCtorKey = (isInterface && !hasDot) ? fullClassName : fullExtendedName; + // class, as do content-keyed extends with the same content, so keying + // their constructor by that class would hand each call site the + // implementation object of the first. + const string& extendedCtorKey = ((isInterface && !hasDot) || contentKeyed) ? siteClassName : fullExtendedName; auto cachedData = GetCachedExtendedClassData(isolate, extendedCtorKey); if (cachedData.extendedCtorFunction != nullptr) { auto cachedExtendedCtorFunc = Local::New(isolate, *cachedData.extendedCtorFunction); @@ -1974,9 +2081,10 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfoGetConstructorFunction(isolate); - auto extendData = External::New(isolate, cache->Own(node, extendNameAndLocation, implementationObject, fullExtendedName), v8::kExternalPointerTypeTagDefault); + auto extendedClassCallbackData = cache->Own(node, extendNameAndLocation, implementationObject, fullExtendedName); + extendedClassCallbackData->contentMemberNames = std::move(contentMemberNames); + auto extendData = External::New(isolate, extendedClassCallbackData, v8::kExternalPointerTypeTagDefault); auto extendFuncTemplate = FunctionTemplate::New(isolate, ExtendedClassConstructorCallback, extendData); extendFuncTemplate->InstanceTemplate()->SetInternalFieldCount(static_cast(ObjectManager::MetadataNodeKeys::END)); @@ -2008,9 +2116,16 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfoExtendedCtorFuncCache.find(fullExtendedName) == cache->ExtendedCtorFuncCache.end()) { - cache->ExtendedCtorFuncCache.emplace(fullExtendedName, ExtendedClassCacheData(extendFunc, extendFuncTemplate->InstanceTemplate(), fullExtendedName, node)); + bool usesContentClassName = contentKeyed && fullClassName == contentClassName; + if (extendedCtorKey != fullExtendedName) { + if (cache->ExtendedCtorFuncCache.find(fullExtendedName) == cache->ExtendedCtorFuncCache.end()) { + cache->ExtendedCtorFuncCache.emplace(fullExtendedName, ExtendedClassCacheData(extendFunc, extendFuncTemplate->InstanceTemplate(), fullExtendedName, node)); + } else if (usesContentClassName) { + cache->SharedExtendedClasses.emplace(fullExtendedName, false); + } + } + if (usesContentClassName && !extendNameString.empty()) { + cache->ContentNameOwners.emplace(contentClassName, siteClassName); } if (frame.check()) { @@ -2507,4 +2622,5 @@ robin_hood::unordered_map MetadataNode::s_name2T robin_hood::unordered_map MetadataNode::s_treeNode2NodeCache; std::mutex MetadataNode::s_nodeCacheMutex; std::atomic MetadataNode::s_profilerEnabled{false}; +std::atomic MetadataNode::s_contentKeyedBindings{nullptr}; diff --git a/test-app/runtime/src/main/cpp/MetadataNode.h b/test-app/runtime/src/main/cpp/MetadataNode.h index 6437b9932..826184039 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.h +++ b/test-app/runtime/src/main/cpp/MetadataNode.h @@ -28,7 +28,9 @@ #include #include #include +#include #include +#include #include #include @@ -78,6 +80,20 @@ class MetadataNode { static MetadataReader* getMetadataReader() { return &s_metadataReader; } + + /* + * Names the classes of JS subclasses of Java classes after their content, in + * every runtime of the process. Called once, before any runtime runs JS, with + * the content-keyed class names (dot-separated) the static binding generator + * saw more than one named extend() produce. + */ + static void InitContentKeyedBindings(const std::vector& collisions); + + /* + * `h` and 16 hex digits of FNV-1a 64 over the canonical payload. Must agree + * with the static binding generator's ContentKey. + */ + static std::string CreateContentKey(const std::string& baseClassName, std::vector interfaceNames, std::vector memberNames); private: struct MethodCallbackData; @@ -200,8 +216,14 @@ class MetadataNode { // Read on every Java method call; GetNodeType takes the reader's state lock. bool m_isInterface; + struct ContentKeyedBindings { + std::unordered_set collisions; + }; + static std::string TNS_PREFIX; static MetadataReader s_metadataReader; + // Written once, before the first extend() of any runtime; never freed. + static std::atomic s_contentKeyedBindings; static robin_hood::unordered_map s_name2NodeCache; static robin_hood::unordered_map s_name2TreeNodeCache; static robin_hood::unordered_map s_treeNode2NodeCache; @@ -260,6 +282,7 @@ class MetadataNode { v8::Persistent* implementationObject; std::string fullClassName; + std::optional> contentMemberNames; }; struct TypeMetadata { @@ -322,6 +345,15 @@ class MetadataNode { robin_hood::unordered_map ExtendedCtorFuncCache; + // A named extend's content-keyed class name -> the call-site class name of + // the extend() call that uses it in this runtime. Java finds a named + // extend's implementation by class name, so no other call site may share it. + robin_hood::unordered_map ContentNameOwners; + + // Content-keyed classes more than one extend() call registered -> whether + // Java creating one by class name was already reported. + robin_hood::unordered_map SharedExtendedClasses; + // The array wrapper template for this runtime. v8::Persistent* ArrayObjectTemplate = nullptr; diff --git a/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp b/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp index c2c09ff3d..c89806e73 100644 --- a/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp +++ b/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp @@ -3,6 +3,7 @@ #include "NativeScriptException.h" #include "CallbackHandlers.h" #include "ArgConverter.h" +#include "MetadataNode.h" #include #include @@ -383,6 +384,19 @@ extern "C" JNIEXPORT void Java_com_tns_Runtime_ResetDateTimeConfigurationCache(J isolate->DateTimeConfigurationChangeNotification(Isolate::TimeZoneDetection::kRedetect); } +extern "C" JNIEXPORT void Java_com_tns_Runtime_initContentKeyedBindings(JNIEnv* _env, jclass clazz, jobjectArray collisions) { + vector names; + jsize length = collisions == nullptr ? 0 : _env->GetArrayLength(collisions); + for (jsize i = 0; i < length; i++) { + auto name = static_cast(_env->GetObjectArrayElement(collisions, i)); + if (name != nullptr) { + names.push_back(ArgConverter::jstringToString(name)); + _env->DeleteLocalRef(name); + } + } + MetadataNode::InitContentKeyedBindings(names); +} + extern "C" JNIEXPORT void Java_com_tns_Runtime_setAdmissionSpinMicros(JNIEnv* _env, jclass clazz, jint micros) { AdmissionGate::s_spinMicros.store(micros, std::memory_order_relaxed); } diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index 75ec2758c..764a8eeb2 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -113,6 +113,33 @@ public static void SetManualInstrumentationMode(String mode) { private static native void ResetDateTimeConfigurationCache(int runtimeId); + private static native void initContentKeyedBindings(String[] collisions); + + /** + * The static binding generator adds com.tns.gen.ContentKeyedBindings to an app + * whose bindings it names after their content, so the runtime names the classes + * it looks up the same way exactly when the generator did. Read once per process, + * before the first runtime runs JS. + */ + private static final class ContentKeyedBindings { + static final boolean ENABLED = init(); + + private static boolean init() { + String[] collisions; + try { + Class marker = Class.forName("com.tns.gen.ContentKeyedBindings", true, Runtime.class.getClassLoader()); + collisions = (String[]) marker.getField("COLLISIONS").get(null); + } catch (ClassNotFoundException e) { + return false; + } catch (ReflectiveOperationException | ClassCastException e) { + android.util.Log.e("JS", "Could not read com.tns.gen.ContentKeyedBindings.COLLISIONS", e); + collisions = new String[0]; + } + initContentKeyedBindings(collisions); + return true; + } + } + /** * Reports an uncaught exception to JS (WHATWG `error` event, then the * `__onUncaughtError` hook). Returns true when an `error` event listener @@ -768,6 +795,10 @@ private void init(Logger logger, String appName, String nativeLibDir, File rootD boolean forceConsoleLog = appConfig.getForceLog() || "timeline".equalsIgnoreCase(appConfig.getProfilingMode()); + if (ContentKeyedBindings.ENABLED && logger.isEnabled()) { + logger.write("Binding class names are content-keyed"); + } + initNativeScript(getRuntimeId(), Module.getApplicationFilesPath(), nativeLibDir, logger.isEnabled(), isDebuggable, appName, appConfig.getV8Flags(), appConfig.getCodeCache(), appConfig.getProfilerOutputDir(), appConfig.getDiscardUncaughtJsExceptions(), appConfig.getUncaughtErrorPolicy(), From 48ecdb73236604735e006c257d504634b2bbb29a Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 21:31:58 -0300 Subject: [PATCH 08/14] feat: optionally implement interfaces with java.lang.reflect.Proxy With "interfaceProxies": true under "android" in the app's package.json, an anonymous interface implementation (new Interface({...}) or Interface.extend({...}) without extra interfaces) whose class is not in the APK gets a java.lang.reflect.Proxy class instead of a class generated at runtime. No dex is generated, written or injected into the app class loader, and no blocking GC precedes the first use; workers need nothing extra. Prebuilt classes are still preferred, and with the option off nothing changes. The proxy behaves like the generated class it replaces: - the interface's abstract methods (including defaults desugared for minSdk 21, which are abstract at runtime) call JS, routed by the owning runtime and object id the handler holds; - equals, hashCode and toString keep identity semantics; - a platform default method runs its Java body through a JNI non-virtual call, without reaching JS; - a result of the wrong type throws ClassCastException, and primitive results are never null. The instance implements NativeScriptRuntimeBound, answered by the handler, so owner routing and argument id hints work unchanged. Native code recognises a proxy class by its constructor signature, cached with the constructor, and constructs it through Runtime.newInterfaceProxy; initInstance is not involved. JS wrappers created for an instance by its class name use the interface's metadata. When Proxy cannot implement the interface, the class is generated as before, with one debug log line. DexFactory.getRuntimeGeneratedClasses() lists proxy classes as "proxy". --- test-app/app/src/main/assets/app/mainpage.js | 2 + test-app/app/src/main/assets/app/package.json | 3 +- .../app/tests/appModuleBindingsWorker.js | 1 + .../assets/app/tests/interfaceProxyWorker.js | 20 + .../assets/app/tests/testAppModuleBindings.js | 15 +- .../app/tests/testInterfaceExtendClasses.js | 17 +- .../assets/app/tests/testInterfaceProxies.js | 417 ++++++++++++++++++ .../InterfaceProxyDefaultMethodFixtures.java | 52 +++ .../com/tns/tests/InterfaceProxyFixtures.java | 209 +++++++++ .../runtime/src/main/cpp/CallbackHandlers.cpp | 10 +- .../runtime/src/main/cpp/CallbackHandlers.h | 2 + .../runtime/src/main/cpp/MetadataNode.cpp | 7 + test-app/runtime/src/main/cpp/MetadataNode.h | 4 + test-app/runtime/src/main/cpp/MethodCache.cpp | 4 + test-app/runtime/src/main/cpp/MethodCache.h | 5 +- .../runtime/src/main/cpp/com_tns_Runtime.cpp | 77 ++++ .../src/main/java/com/tns/AppConfig.java | 14 +- .../src/main/java/com/tns/DexFactory.java | 73 ++- .../java/com/tns/InterfaceProxyHandler.java | 86 ++++ .../src/main/java/com/tns/Runtime.java | 116 ++++- 20 files changed, 1110 insertions(+), 24 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/interfaceProxyWorker.js create mode 100644 test-app/app/src/main/assets/app/tests/testInterfaceProxies.js create mode 100644 test-app/app/src/main/java/com/tns/tests/InterfaceProxyDefaultMethodFixtures.java create mode 100644 test-app/app/src/main/java/com/tns/tests/InterfaceProxyFixtures.java create mode 100644 test-app/runtime/src/main/java/com/tns/InterfaceProxyHandler.java diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index a444af473..ceca27102 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -135,5 +135,7 @@ require('./tests/testEsmHttpLoader'); require('./tests/NapiTests'); require('./tests/NapiCoverageTests'); require("./tests/testConcurrentAccess"); +// Checks the runtime generation report, so it goes after every spec that implements an interface +require("./tests/testInterfaceProxies"); require("./tests/testESModules.mjs"); diff --git a/test-app/app/src/main/assets/app/package.json b/test-app/app/src/main/assets/app/package.json index 2ca148662..035360dbd 100644 --- a/test-app/app/src/main/assets/app/package.json +++ b/test-app/app/src/main/assets/app/package.json @@ -10,7 +10,8 @@ "forceLog": false, "suppressCallJSMethodExceptions": false, "enableLineBreakpoints": false, - "enableMultithreadedJavascript": true + "enableMultithreadedJavascript": true, + "interfaceProxies": false }, "discardUncaughtJsExceptions": false, "security": { diff --git a/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js b/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js index cd6ffdb86..adaa3edf0 100644 --- a/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js +++ b/test-app/app/src/main/assets/app/tests/appModuleBindingsWorker.js @@ -25,6 +25,7 @@ try { } }); result.implementedClass = String(implemented.getClass().getName()); + result.implementedIsProxy = java.lang.reflect.Proxy.isProxyClass(implemented.getClass()); result.implementedCall = callFromJava(implemented, "describe"); } catch (e) { result.error = String(e && e.message || e); diff --git a/test-app/app/src/main/assets/app/tests/interfaceProxyWorker.js b/test-app/app/src/main/assets/app/tests/interfaceProxyWorker.js new file mode 100644 index 000000000..48977108b --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/interfaceProxyWorker.js @@ -0,0 +1,20 @@ +// Entry for testInterfaceProxies: implements an interface the static binding generator +// never sees, and leaves the instance for the main runtime in RuntimeBoundHolder. +var F = com.tns.tests.InterfaceProxyFixtures; +var Target = F["Worker" + "Target"]; +var result = {}; + +try { + var target = new Target({ + describe: function () { + return "worker:" + java.lang.Thread.currentThread().getName(); + } + }); + result.isProxy = java.lang.reflect.Proxy.isProxyClass(target.getClass()); + result.thread = String(java.lang.Thread.currentThread().getName()); + result.fromJava = String(F.describeWorkerTarget(target)); + com.tns.tests.RuntimeBoundHolder.put("interfaceProxyWorkerTarget", target); +} catch (e) { + result.error = String(e && e.message || e); +} +postMessage(result); diff --git a/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js b/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js index 6b865b298..52e24639c 100644 --- a/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js +++ b/test-app/app/src/main/assets/app/tests/testAppModuleBindings.js @@ -1,6 +1,15 @@ describe("Bindings of app-module types", function () { // A computed member hides an extend from the static binding generator. var ext = "ex" + "tend"; + // With this option an interface without a prebuilt class gets a java.lang.reflect.Proxy class. + var proxies = !!require("../package.json").android.interfaceProxies; + + function expectRuntimeInterfaceClass(isProxyClass, className, expectedGeneratedName) { + expect(isProxyClass).toBe(proxies); + if (!proxies) { + expect(className).toBe(expectedGeneratedName); + } + } // Reflection makes Java, not JS, call the implementation. function callFromJava(instance, methodName) { @@ -87,7 +96,7 @@ describe("Bindings of app-module types", function () { }); expect(callFromJava(implemented, "describe")).toBe("runtime new"); - expect(String(implemented.getClass().getName())).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget_RuntimeGenerated"); + expectRuntimeInterfaceClass(java.lang.reflect.Proxy.isProxyClass(implemented.getClass()), String(implemented.getClass().getName()), "com.tns.gen.com.tns.tests.AppModuleBindingTarget_RuntimeGenerated"); var entries = reportEntriesFor(implemented.getClass().getName()); expect(entries.length).toBe(1); expect(entries[0].kind).toBe("interface"); @@ -103,7 +112,7 @@ describe("Bindings of app-module types", function () { var instance = new Extended(); expect(callFromJava(instance, "describe")).toBe("runtime interface extend"); - expect(String(instance.getClass().getName())).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget_ExtendedAtRuntime"); + expectRuntimeInterfaceClass(java.lang.reflect.Proxy.isProxyClass(instance.getClass()), String(instance.getClass().getName()), "com.tns.gen.com.tns.tests.AppModuleBindingTarget_ExtendedAtRuntime"); var entries = reportEntriesFor(instance.getClass().getName()); expect(entries.length).toBe(1); expect(entries[0].kind).toBe("interface"); @@ -116,7 +125,7 @@ describe("Bindings of app-module types", function () { expect(msg.data.error).toBeUndefined(); expect(msg.data.extendedCall).toBe("worker extend"); expect(msg.data.implementedCall).toBe("worker new"); - expect(msg.data.implementedClass).toBe("com.tns.gen.com.tns.tests.AppModuleBindingTarget_WorkerRuntimeGenerated"); + expectRuntimeInterfaceClass(msg.data.implementedIsProxy, msg.data.implementedClass, "com.tns.gen.com.tns.tests.AppModuleBindingTarget_WorkerRuntimeGenerated"); expect(reportEntriesFor(msg.data.extendedClass).length).toBe(1); expect(reportEntriesFor(msg.data.implementedClass).length).toBe(1); done(); diff --git a/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js index ba6ebe4ed..27a743a65 100644 --- a/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js +++ b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js @@ -1,5 +1,7 @@ describe("Anonymous extends of an interface", function () { var ext = "ex" + "tend"; + // With this option an interface without a prebuilt class gets a java.lang.reflect.Proxy class. + var proxies = !!require("../package.json").android.interfaceProxies; // Reflection makes Java, not JS, call the implementation. function callFromJava(instance, methodName) { @@ -86,7 +88,7 @@ describe("Anonymous extends of an interface", function () { expect(String(callFromJava(implemented, "run"))).toBe("implemented"); expect(String(callFromJava(extended, "run"))).toBe("extended"); expect(String(extended.getClass().getName())).toBe(String(implemented.getClass().getName())); - expect(reportEntriesFor("com.tns.gen.java.security.PrivilegedAction").length).toBe(1); + expect(reportEntriesFor(String(implemented.getClass().getName())).length).toBe(1); }); it("work with a generated class before new Interface({...}) of the same interface", function () { @@ -108,7 +110,7 @@ describe("Anonymous extends of an interface", function () { callFromJava(implemented, "close"); expect(calls).toEqual(["extended", "implemented"]); expect(String(implemented.getClass().getName())).toBe(String(extended.getClass().getName())); - expect(reportEntriesFor("com.tns.gen.java.lang.AutoCloseable").length).toBe(1); + expect(reportEntriesFor(String(implemented.getClass().getName())).length).toBe(1); }); it("keep each implementation when new and extend of a prebuilt interface are mixed", function () { @@ -160,7 +162,9 @@ describe("Anonymous extends of an interface", function () { expect(msg.data.error).toBeUndefined(); expect(msg.data.extendedRun).toBe("worker extend"); expect(msg.data.implementedRun).toBe("worker new"); - expect(msg.data.extendedClass).toBe("com.tns.gen.java.security.PrivilegedExceptionAction"); + if (!proxies) { + expect(msg.data.extendedClass).toBe("com.tns.gen.java.security.PrivilegedExceptionAction"); + } expect(msg.data.implementedClass).toBe(msg.data.extendedClass); done(); }; @@ -184,12 +188,13 @@ describe("Anonymous extends of an interface", function () { } }); - expect(String(callFromJava(new Extended(), "run"))).toBe("main extend"); - var entries = reportEntriesFor("com.tns.gen.java.security.PrivilegedExceptionAction").filter(function (entry) { + var extended = new Extended(); + expect(String(callFromJava(extended, "run"))).toBe("main extend"); + var entries = reportEntriesFor(String(extended.getClass().getName())).filter(function (entry) { return entry.requested.indexOf("_tests_testInterfaceExtendClasses_") > 0; }); expect(entries.length).toBe(1); - expect(entries[0].source).toBe("cached"); + expect(entries[0].source).toBe(proxies ? "proxy" : "cached"); done(); }; }); diff --git a/test-app/app/src/main/assets/app/tests/testInterfaceProxies.js b/test-app/app/src/main/assets/app/tests/testInterfaceProxies.js new file mode 100644 index 000000000..54118c150 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testInterfaceProxies.js @@ -0,0 +1,417 @@ +// Interface implementations whose class is not in the APK: a class generated at runtime, +// or a java.lang.reflect.Proxy with the app's android.interfaceProxies option. Both must +// behave the same way when Java calls them. +describe("Interface implementations without a prebuilt class", function () { + var F = com.tns.tests.InterfaceProxyFixtures; + var config = require("../package.json").android; + var proxies = !!config.interfaceProxies; + var multithreaded = !!config.enableMultithreadedJavascript; + var sdk = android.os.Build.VERSION.SDK_INT; + + // Computed members keep the static binding generator from prebuilding these classes. + var ext = "ex" + "tend"; + function hidden(holder, name) { + return holder[name]; + } + + var Values = hidden(F, "Val" + "ues"); + var Identity = hidden(F, "Iden" + "tity"); + + function expectRuntimeClass(instance) { + var cls = instance.getClass(); + expect(java.lang.reflect.Proxy.isProxyClass(cls)).toBe(proxies); + if (!proxies) { + expect(String(cls.getName())).toMatch(/^com\.tns\.gen\./); + } + } + + function reportEntriesFor(loadedName) { + var lines = com.tns.DexFactory.getRuntimeGeneratedClasses(); + var result = []; + for (var i = 0; i < lines.length; i++) { + var fields = String(lines[i]).split(" "); + if (fields[2] === String(loadedName)) { + result.push({ source: fields[0], kind: fields[1], loaded: fields[2], requested: fields[3] }); + } + } + return result; + } + + function collectJava() { + for (var i = 0; i < 3; i++) { + java.lang.Runtime.getRuntime().gc(); + java.lang.System.runFinalization(); + } + } + + it("is a class of the expected kind and listed in the runtime generation report", function () { + var instance = new Identity({ + name: function () { + return "kind"; + } + }); + + expectRuntimeClass(instance); + var entries = reportEntriesFor(instance.getClass().getName()); + expect(entries.length).toBe(1); + expect(entries[0].kind).toBe("interface"); + if (proxies) { + expect(entries[0].source).toBe("proxy"); + } else { + expect(["generated", "cached"]).toContain(entries[0].source); + } + }); + + it("passes every argument and return type between Java and JS", function () { + var ran = false; + var values = new Values({ + booleanValue: function (v) { return v; }, + byteValue: function (v) { return v; }, + charValue: function (v) { return v; }, + shortValue: function (v) { return v; }, + intValue: function (v) { return v; }, + longValue: function (v) { return v; }, + floatValue: function (v) { return v; }, + doubleValue: function (v) { return v; }, + run: function () { ran = true; }, + stringValue: function (v) { return v; }, + objectValue: function (v) { return v; }, + intArray: function (v) { return v; }, + join: function (parts) { + var result = []; + for (var i = 0; i < parts.length; i++) { + result.push(parts[i]); + } + return result.join("+"); + } + }); + + expectRuntimeClass(values); + expect(String(F.callAllValues(values))).toBe("true|-5|x|-300|70000|1099511627776|1.5|-2.25|str|obj|3|a+b+c"); + expect(ran).toBe(true); + }); + + it("gives Java the default value for primitives and null for objects when JS returns nothing", function () { + function nothing() {} + var values = new Values({ + booleanValue: nothing, byteValue: nothing, charValue: nothing, shortValue: nothing, + intValue: nothing, longValue: nothing, floatValue: nothing, doubleValue: nothing, + run: nothing, stringValue: function () { return null; }, objectValue: nothing, + intArray: function () { return null; }, join: nothing + }); + + expect(String(F.callDefaultedValues(values))).toBe("false|0|0|0|0|0|0.0|0.0|null|null|null"); + }); + + it("converts JS return values the way a prebuilt class does", function () { + var current; + function implementation() { + return { + z: function () { return current(); }, + b: function () { return current(); }, + c: function () { return current(); }, + s: function () { return current(); }, + i: function () { return current(); }, + j: function () { return current(); }, + f: function () { return current(); }, + d: function () { return current(); } + }; + } + var prebuilt = new com.tns.tests.PrimitiveReturnTest.Returns(implementation()); + var Hidden = hidden(F, "PrimitiveRe" + "turns"); + var runtime = new Hidden(implementation()); + expectRuntimeClass(runtime); + + var values = [ + function () { return 1; }, function () { return -1.5; }, function () { return 2147483648; }, + function () { return 1e-40; }, function () { return NaN; }, function () { return true; }, + function () { return "a"; }, function () { return null; }, function () { return undefined; }, + function () { return new Number(5); }, function () { return long(5); }, function () { return byte(2); }, + function () { return short(3); }, function () { return char("a"); }, function () { return float(1.5); }, + function () { return double(2); }, function () { return {}; } + ]; + var kinds = ["z", "b", "c", "s", "i", "j", "f", "d"]; + values.forEach(function (value) { + current = value; + kinds.forEach(function (kind) { + var expected = String(com.tns.tests.PrimitiveReturnTest.call(prebuilt, kind)); + var actual = String(com.tns.tests.PrimitiveReturnTest.call(runtime, kind)); + expect(value.toString() + " " + kind + ": " + actual).toBe(value.toString() + " " + kind + ": " + expected); + }); + }); + }); + + it("throws to Java what the JS method throws", function () { + var values = new Values({ + stringValue: function () { + throw new Error("thrown by the implementation"); + } + }); + + var result = String(F.callStringValue(values, "x")); + expect(result).toMatch(/^com\.tns\.NativeScriptException: /); + expect(result).toContain("thrown by the implementation"); + }); + + it("fails a call to an abstract method the JS implementation lacks", function () { + var values = new Values({}); + + var result = String(F.callRun(values)); + expect(result).toMatch(/^com\.tns\.NativeScriptException: /); + expect(result).toContain("java.lang.AbstractMethodError"); + }); + + it("gives equals, hashCode and toString identity semantics, as Java sees them", function () { + var calledFromJava = []; + var implementation = { + name: function () { return "identity"; }, + equals: function () { calledFromJava.push("equals"); return false; }, + hashCode: function () { calledFromJava.push("hashCode"); return 1; }, + toString: function () { calledFromJava.push("toString"); return "js"; } + }; + var first = new Identity(implementation); + var second = new Identity({ name: function () { return "second"; } }); + + expect(F.hashCodeIsIdentity(first)).toBe(true); + expect(F.equalsIsIdentity(first)).toBe(true); + expect(F.toStringIsIdentity(first)).toBe(true); + expect(F.worksAsHashMapKey(first, second)).toBe(true); + expect(calledFromJava).toEqual([]); + }); + + it("hands JS the same object back when Java returns the instance", function () { + var instance = new Identity({ name: function () { return "echo"; } }); + + expect(F.echo(instance) === instance).toBe(true); + expect(F.echo(F.echo(instance)) === instance).toBe(true); + }); + + it("implements an interface that extends another", function () { + var Derived = hidden(F, "Der" + "ived"); + var instance = new Derived({ + base: function () { return "base"; }, + derived: function () { return "derived"; } + }); + + expectRuntimeClass(instance); + expect(String(F.callDerived(instance))).toBe("base|derived"); + }); + + it("implements a generic interface", function () { + var Generic = hidden(F, "Gen" + "eric"); + var instance = new Generic({ + apply: function (value) { return value + "!"; } + }); + + expectRuntimeClass(instance); + expect(String(F.applyGeneric(instance, "generic"))).toBe("generic!"); + }); + + it("implements a Kotlin function type", function () { + var Function1 = hidden(kotlin.jvm.functions, "Func" + "tion1"); + var instance = new Function1({ + invoke: function (value) { return "kotlin " + value; } + }); + + expectRuntimeClass(instance); + expect(String(F.invokeFunction1(instance, "lambda"))).toBe("kotlin lambda"); + }); + + it("keeps each extend's implementation when one interface is extended twice", function () { + var First = Identity[ext]({ name: function () { return "first"; } }); + var Second = Identity[ext]({ name: function () { return "second"; } }); + var first = new First(); + var second = new Second(); + + expect(Second).not.toBe(First); + expectRuntimeClass(first); + expect(String(second.getClass().getName())).toBe(String(first.getClass().getName())); + expect(String(second.getClass().getMethod("name", []).invoke(second, []))).toBe("second"); + expect(String(first.getClass().getMethod("name", []).invoke(first, []))).toBe("first"); + }); + + it("runs a platform default method's Java body, even when JS defines it", function () { + if (sdk < 24) { + return; + } + var D = com.tns.tests.InterfaceProxyDefaultMethodFixtures; + var Iterator = hidden(java.util, "Iter" + "ator"); + var jsRemoveCalls = 0; + function iterator() { + var items = ["a", "b", "c"]; + var index = 0; + return new Iterator({ + hasNext: function () { return index < items.length; }, + next: function () { return items[index++]; }, + remove: function () { jsRemoveCalls++; } + }); + } + + var fromJava = iterator(); + expectRuntimeClass(fromJava); + expect(String(D.drain(fromJava))).toBe("a,b,c"); + expect(String(D.remove(fromJava))).toBe("java.lang.UnsupportedOperationException"); + expect(jsRemoveCalls).toBe(0); + + // JS calling a default the implementation lacks gets the Java body too. + var fromJs = iterator(); + var seen = []; + fromJs.forEachRemaining(new java.util.function.Consumer({ + accept: function (item) { seen.push(item); } + })); + expect(seen).toEqual(["a", "b", "c"]); + + var Predicate = hidden(java.util["func" + "tion"], "Pred" + "icate"); + var predicate = new Predicate({ + test: function (value) { return value === "yes"; }, + negate: function () { throw new Error("JS negate must not run"); } + }); + expect(String(D.negate(predicate, "yes"))).toBe("true|false"); + + // Primitive arguments and results of a default method. + var Spliterator = hidden(java.util, "Split" + "erator"); + var spliterator = new Spliterator({ + tryAdvance: function () { return false; }, + trySplit: function () { return null; }, + estimateSize: function () { return 3; }, + characteristics: function () { return Spliterator.SIZED; }, + getExactSizeIfKnown: function () { throw new Error("JS getExactSizeIfKnown must not run"); } + }); + expect(String(D.spliteratorDefaults(spliterator))).toBe("3|true|false"); + + // OfInt.next() is a default overriding the abstract Iterator.next(), and its + // forEachRemaining(Consumer) overrides Iterator's default. + var OfInt = hidden(java.util.PrimitiveIterator, "Of" + "Int"); + var value = 0; + var ofInt = new OfInt({ + hasNext: function () { return value < 3; }, + nextInt: function () { return ++value; }, + next: function () { throw new Error("JS next must not run"); } + }); + expectRuntimeClass(ofInt); + expect(String(D.next(ofInt))).toBe("1"); + expect(String(D.drain(ofInt))).toBe("2,3"); + }); + + it("sends a default method compiled for minSdk 21 to JS like an abstract one", function () { + // Desugared: at runtime Producer.produce() is abstract and its body lives elsewhere. + var Producer = hidden(com.tns.tests.interfaces.defaultmethods.contract, "Pro" + "ducer"); + + expect(String(F.produce(new Producer({ produce: function () { return "from js"; } })))).toBe("from js"); + expect(String(F.produce(new Producer({})))).toBe("com.tns.NativeScriptException"); + }); + + it("generates a class for an interface a proxy cannot implement", function () { + var Incompatible = hidden(F, "ProxyIncom" + "patible"); + var instance = new Incompatible({ + describe: function () { return "generated"; } + }); + + expect(String(F.describe(instance))).toBe("generated"); + expect(java.lang.reflect.Proxy.isProxyClass(instance.getClass())).toBe(false); + expect(String(instance.getClass().getName())).toBe("com.tns.gen.com.tns.tests.InterfaceProxyFixtures_ProxyIncompatible"); + var entries = reportEntriesFor(instance.getClass().getName()); + expect(entries.length).toBe(1); + expect(["generated", "cached"]).toContain(entries[0].source); + }); + + it("routes a call from another Java thread to the runtime that created the instance", function (done) { + var Reporter = hidden(F, "Thread" + "Reporter"); + var reporter = new Reporter({ + report: function (caller) { + return caller + ">" + java.lang.Thread.currentThread().getName(); + } + }); + expectRuntimeClass(reporter); + + F.startReportOnNewThread(reporter); + var deadline = Date.now() + 10000; + (function poll() { + var report = F.threadReport(); + if (report === null && Date.now() < deadline) { + setTimeout(poll, 10); + return; + } + // With multithreaded JS the call runs on the calling thread under the isolate lock; + // without it, it is posted to this runtime's thread. + expect(String(report)).toBe("InterfaceProxyFixtures-caller>" + (multithreaded ? "InterfaceProxyFixtures-caller" : "main")); + done(); + })(); + }); + + it("works in a worker, and from another runtime that wraps the worker's instance", function (done) { + var Holder = com.tns.tests.RuntimeBoundHolder; + var worker = new Worker("./interfaceProxyWorker.js"); + worker.onmessage = function (msg) { + var data = msg.data; + expect(data.error).toBeUndefined(); + expect(data.isProxy).toBe(proxies); + expect(data.fromJava).toBe("worker:" + data.thread); + + var target = Holder.get("interfaceProxyWorkerTarget"); + var expected = "worker:" + (multithreaded ? "main" : data.thread); + expect(String(F.describeWorkerTarget(target))).toBe(expected); + expect(String(target.describe())).toBe(expected); + + Holder.clear(); + worker.terminate(); + done(); + }; + }); + + it("lets Java collect an instance neither JS nor Java references", function (done) { + var ref = (function () { + return F.weakly(new Identity({ name: function () { return "collectable"; } })); + })(); + + var deadline = Date.now() + 10000; + (function poll() { + gc(); + collectJava(); + if (F.isCleared(ref) || Date.now() > deadline) { + expect(F.isCleared(ref)).toBe(true); + done(); + return; + } + setTimeout(poll, 20); + })(); + }); + + it("keeps the JS implementation of an instance only Java references", function () { + (function () { + var state = "kept"; + F.hold(new Identity({ name: function () { return state; } })); + })(); + + for (var i = 0; i < 3; i++) { + gc(); + collectJava(); + } + + expect(String(F.callHeld())).toBe("kept"); + F.release(); + }); +}); + +// Runs after the other specs in the suite, so it sees what all of them resolved. +describe("Runtime generation report under the interfaceProxies option", function () { + it("lists no class generated for an interface that a proxy could implement", function () { + var lines = Array.prototype.map.call(com.tns.DexFactory.getRuntimeGeneratedClasses(), String); + android.util.Log.i("JS", "Runtime generation report:\n" + lines.join("\n")); + if (!require("../package.json").android.interfaceProxies) { + return; + } + + var generatedInterfaces = lines.filter(function (line) { + var fields = line.split(" "); + return fields[0] !== "proxy" && fields[1] === "interface"; + }); + // Named extends (Interface.extend("a.b.C", {...})) keep their own class, and so does + // the interface the spec above makes incompatible on purpose. + var unexpected = generatedInterfaces.filter(function (line) { + var requested = line.split(" ")[3]; + return requested.indexOf("com.tns.gen.") === 0 && requested.indexOf("InterfaceProxyFixtures$ProxyIncompatible") < 0; + }); + expect(unexpected).toEqual([]); + }); +}); diff --git a/test-app/app/src/main/java/com/tns/tests/InterfaceProxyDefaultMethodFixtures.java b/test-app/app/src/main/java/com/tns/tests/InterfaceProxyDefaultMethodFixtures.java new file mode 100644 index 000000000..789cdaad5 --- /dev/null +++ b/test-app/app/src/main/java/com/tns/tests/InterfaceProxyDefaultMethodFixtures.java @@ -0,0 +1,52 @@ +package com.tns.tests; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.function.Consumer; +import java.util.function.Predicate; + +// Platform default methods for testInterfaceProxies.js, called from Java. API 24+ only: kept +// apart from InterfaceProxyFixtures so that class stays usable on older devices. +public class InterfaceProxyDefaultMethodFixtures { + public static String drain(Iterator it) { + final List seen = new ArrayList<>(); + it.forEachRemaining(new Consumer() { + @Override + public void accept(Object item) { + seen.add(item); + } + }); + return android.text.TextUtils.join(",", seen); + } + + public static String remove(Iterator it) { + try { + it.remove(); + return "removed"; + } catch (Throwable t) { + return t.getClass().getName(); + } + } + + // Through Iterator, as most callers see a PrimitiveIterator.OfInt. + public static String next(Iterator it) { + try { + return String.valueOf(it.next()); + } catch (Throwable t) { + return t.getClass().getName(); + } + } + + @SuppressWarnings("unchecked") + public static String negate(Predicate p, Object value) { + Predicate predicate = (Predicate) p; + return predicate.test(value) + "|" + predicate.negate().test(value); + } + + public static String spliteratorDefaults(Spliterator spliterator) { + return spliterator.getExactSizeIfKnown() + "|" + spliterator.hasCharacteristics(Spliterator.SIZED) + + "|" + spliterator.hasCharacteristics(Spliterator.SORTED); + } +} diff --git a/test-app/app/src/main/java/com/tns/tests/InterfaceProxyFixtures.java b/test-app/app/src/main/java/com/tns/tests/InterfaceProxyFixtures.java new file mode 100644 index 000000000..39a4573ad --- /dev/null +++ b/test-app/app/src/main/java/com/tns/tests/InterfaceProxyFixtures.java @@ -0,0 +1,209 @@ +package com.tns.tests; + +import java.lang.ref.WeakReference; +import java.util.HashMap; + +import kotlin.jvm.functions.Function1; + +// Fixtures for testInterfaceProxies.js. Its specs implement these interfaces only through +// calls hidden from the static binding generator, so none of them is ever prebuilt, and +// make the calls from Java. +public class InterfaceProxyFixtures { + public interface Values { + boolean booleanValue(boolean value); + + byte byteValue(byte value); + + char charValue(char value); + + short shortValue(short value); + + int intValue(int value); + + long longValue(long value); + + float floatValue(float value); + + double doubleValue(double value); + + void run(); + + String stringValue(String value); + + Object objectValue(Object value); + + int[] intArray(int[] value); + + String join(String... parts); + } + + public interface Base { + String base(); + } + + public interface Derived extends Base { + String derived(); + } + + public interface Generic { + T apply(T value); + } + + public interface Identity { + String name(); + } + + public interface ThreadReporter { + String report(String caller); + } + + public interface WorkerTarget { + String describe(); + } + + // A java.lang.reflect.Proxy cannot implement this: its method clashes with the + // runtime's own NativeScriptRuntimeBound.getRuntimeTag__ns(), which returns int. + public interface ProxyIncompatible { + long getRuntimeTag__ns(); + + String describe(); + } + + public static String callAllValues(Values v) { + StringBuilder sb = new StringBuilder(); + sb.append(v.booleanValue(true)).append('|'); + sb.append(v.byteValue((byte) -5)).append('|'); + sb.append(v.charValue('x')).append('|'); + sb.append(v.shortValue((short) -300)).append('|'); + sb.append(v.intValue(70000)).append('|'); + sb.append(v.longValue(1L << 40)).append('|'); + sb.append(v.floatValue(1.5f)).append('|'); + sb.append(v.doubleValue(-2.25)).append('|'); + v.run(); + sb.append(v.stringValue("str")).append('|'); + sb.append(v.objectValue("obj")).append('|'); + int[] ints = v.intArray(new int[]{1, 2, 3}); + sb.append(ints == null ? "null" : String.valueOf(ints.length)).append('|'); + sb.append(v.join("a", "b", "c")); + return sb.toString(); + } + + public static String callDefaultedValues(Values v) { + return v.booleanValue(true) + "|" + v.byteValue((byte) 1) + "|" + (int) v.charValue('x') + "|" + v.shortValue((short) 1) + + "|" + v.intValue(1) + "|" + v.longValue(1) + "|" + v.floatValue(1) + "|" + v.doubleValue(1) + + "|" + v.stringValue("s") + "|" + v.objectValue("o") + "|" + v.intArray(null); + } + + public static String callRun(Values v) { + try { + v.run(); + return "returned"; + } catch (Throwable t) { + return t.getClass().getName() + ": " + t.getMessage(); + } + } + + public static String callStringValue(Values v, String value) { + try { + return v.stringValue(value); + } catch (Throwable t) { + return t.getClass().getName() + ": " + t.getMessage(); + } + } + + public static String callDerived(Derived d) { + Base asBase = d; + return asBase.base() + "|" + d.derived(); + } + + @SuppressWarnings("unchecked") + public static Object applyGeneric(Generic g, Object value) { + return ((Generic) g).apply(value); + } + + @SuppressWarnings("unchecked") + public static Object invokeFunction1(Function1 f, Object value) { + return ((Function1) f).invoke(value); + } + + public static boolean hashCodeIsIdentity(Object o) { + return o.hashCode() == System.identityHashCode(o); + } + + public static boolean equalsIsIdentity(Object o) { + return o.equals(o) && !o.equals(new Object()) && !o.equals(null); + } + + public static boolean toStringIsIdentity(Object o) { + return o.toString().equals(o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o))); + } + + public static boolean worksAsHashMapKey(Object key, Object other) { + HashMap map = new HashMap<>(); + map.put(key, "key"); + map.put(other, "other"); + return "key".equals(map.get(key)) && "other".equals(map.get(other)) && map.size() == 2; + } + + public static Object echo(Object o) { + return o; + } + + public static String produce(com.tns.tests.interfaces.defaultmethods.contract.Producer producer) { + try { + return producer.produce(); + } catch (Throwable t) { + return t.getClass().getName(); + } + } + + public static String describe(ProxyIncompatible target) { + return target.describe(); + } + + public static String describeWorkerTarget(WorkerTarget target) { + return target.describe(); + } + + public interface PrimitiveReturns extends PrimitiveReturnTest.Returns { + } + + private static volatile String threadReport; + + // Returns at once: without multithreaded JS the call waits for the JS thread to go idle. + public static void startReportOnNewThread(final ThreadReporter reporter) { + threadReport = null; + new Thread(new Runnable() { + @Override + public void run() { + threadReport = reporter.report(Thread.currentThread().getName()); + } + }, "InterfaceProxyFixtures-caller").start(); + } + + public static String threadReport() { + return threadReport; + } + + private static Object held; + + public static void hold(Object o) { + held = o; + } + + public static String callHeld() { + return ((Identity) held).name(); + } + + public static void release() { + held = null; + } + + public static WeakReference weakly(Object o) { + return new WeakReference<>(o); + } + + public static boolean isCleared(WeakReference ref) { + return ref.get() == null; + } +} diff --git a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp index 5dd0b9ce9..d29fde683 100644 --- a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp +++ b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp @@ -64,6 +64,10 @@ void CallbackHandlers::Init(Isolate *isolate) { "(Ljava/lang/Object;I)J"); NS_CHECK(REGISTER_CONSTRUCTED_INSTANCE_ID != nullptr); + NEW_INTERFACE_PROXY_ID = env.GetMethodID(RUNTIME_CLASS, "newInterfaceProxy", + "(Ljava/lang/Class;I)Ljava/lang/Object;"); + NS_CHECK(NEW_INTERFACE_PROXY_ID != nullptr); + GET_TYPE_METADATA = env.GetStaticMethodID(RUNTIME_CLASS, "getTypeMetadata", "(Ljava/lang/String;I)[Ljava/lang/String;"); NS_CHECK(GET_TYPE_METADATA != nullptr); @@ -170,7 +174,10 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local &j generatedJavaClass, isInterface); jobject instance; - { + if (mi.isInterfaceProxy) { + instance = env.CallObjectMethod(runtime->GetJavaRuntime(), NEW_INTERFACE_PROXY_ID, + generatedJavaClass, javaObjectID); + } else { ConstructionScope construction(runtime, javaObjectID, generatedJavaClass); if (argWrapper.type == ArgType::Interface) { @@ -2090,6 +2097,7 @@ jfieldID CallbackHandlers::CURRENT_OBJECTID_FIELD_ID = nullptr; jfieldID CallbackHandlers::CURRENT_OBJECTCLASS_FIELD_ID = nullptr; jmethodID CallbackHandlers::RESOLVE_CLASS_METHOD_ID = nullptr; jmethodID CallbackHandlers::REGISTER_CONSTRUCTED_INSTANCE_ID = nullptr; +jmethodID CallbackHandlers::NEW_INTERFACE_PROXY_ID = nullptr; jmethodID CallbackHandlers::GET_TYPE_METADATA = nullptr; jmethodID CallbackHandlers::ENABLE_VERBOSE_LOGGING_METHOD_ID = nullptr; jmethodID CallbackHandlers::DISABLE_VERBOSE_LOGGING_METHOD_ID = nullptr; diff --git a/test-app/runtime/src/main/cpp/CallbackHandlers.h b/test-app/runtime/src/main/cpp/CallbackHandlers.h index 6ec8a39c2..cf308d57c 100644 --- a/test-app/runtime/src/main/cpp/CallbackHandlers.h +++ b/test-app/runtime/src/main/cpp/CallbackHandlers.h @@ -220,6 +220,8 @@ namespace tns { static jmethodID REGISTER_CONSTRUCTED_INSTANCE_ID; + static jmethodID NEW_INTERFACE_PROXY_ID; + static jmethodID GET_TYPE_METADATA; static jmethodID ENABLE_VERBOSE_LOGGING_METHOD_ID; diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index 320ffc42e..8aa8dc0b0 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -258,6 +258,13 @@ MetadataNode* MetadataNode::GetOrCreate(const string& className) { return s_name2NodeCache.emplace(className, node).first->second; } +void MetadataNode::RegisterInterfaceProxyClass(const string& proxyClassName, const string& interfaceName) { + MetadataNode* node = GetOrCreate(interfaceName); + + std::lock_guard lock(s_nodeCacheMutex); + s_name2NodeCache.emplace(proxyClassName, node); +} + MetadataNode* MetadataNode::GetOrCreateInternal(MetadataTreeNode* treeNode) { { std::lock_guard lock(s_nodeCacheMutex); diff --git a/test-app/runtime/src/main/cpp/MetadataNode.h b/test-app/runtime/src/main/cpp/MetadataNode.h index 826184039..73b460851 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.h +++ b/test-app/runtime/src/main/cpp/MetadataNode.h @@ -63,6 +63,10 @@ class MetadataNode { static MetadataNode* GetOrCreate(const std::string& className); + // GetOrCreate(proxyClassName) answers with the interface's node: a proxy class has a + // generated name the metadata cannot describe. + static void RegisterInterfaceProxyClass(const std::string& proxyClassName, const std::string& interfaceName); + static std::string GetTypeMetadataName(v8::Isolate* isolate, v8::Local& value); /* diff --git a/test-app/runtime/src/main/cpp/MethodCache.cpp b/test-app/runtime/src/main/cpp/MethodCache.cpp index 2a73205e8..e2fa1c371 100644 --- a/test-app/runtime/src/main/cpp/MethodCache.cpp +++ b/test-app/runtime/src/main/cpp/MethodCache.cpp @@ -108,6 +108,10 @@ const MethodCache::CacheMethodInfo& MethodCache::ResolveConstructorSignature(con constructor_info.clazz = javaClass; constructor_info.signature = signature; constructor_info.mid = env.GetMethodID(javaClass, "", signature); + // Runtime.resolveConstructorSignature answers this for no Java arguments only for an + // interface proxy; any other class would need a one-argument call to resolve to it. + bool hasJavaArgs = !isInterface && args.Length() > 0; + constructor_info.isInterfaceProxy = !hasJavaArgs && signature == "(Ljava/lang/reflect/InvocationHandler;)V"; std::unique_lock lock(signatureCacheMutex); return s_mthod_ctor_signature_cache.emplace(encoded_ctor_signature, constructor_info).first->second; diff --git a/test-app/runtime/src/main/cpp/MethodCache.h b/test-app/runtime/src/main/cpp/MethodCache.h index 0aac42904..127eff086 100644 --- a/test-app/runtime/src/main/cpp/MethodCache.h +++ b/test-app/runtime/src/main/cpp/MethodCache.h @@ -20,7 +20,7 @@ class MethodCache { struct CacheMethodInfo { CacheMethodInfo() : - retType(MethodReturnType::Unknown), mid(nullptr), clazz(nullptr), isStatic(false) { + retType(MethodReturnType::Unknown), mid(nullptr), clazz(nullptr), isStatic(false), isInterfaceProxy(false) { } std::string signature; std::string returnType; @@ -28,6 +28,9 @@ class MethodCache { jmethodID mid; jclass clazz; bool isStatic; + // A constructor entry for a java.lang.reflect.Proxy class implementing an interface: + // instances come from Runtime.newInterfaceProxy, not from mid. + bool isInterfaceProxy; }; static void Init(); diff --git a/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp b/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp index c89806e73..8358e8812 100644 --- a/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp +++ b/test-app/runtime/src/main/cpp/com_tns_Runtime.cpp @@ -4,7 +4,9 @@ #include "CallbackHandlers.h" #include "ArgConverter.h" #include "MetadataNode.h" +#include #include +#include #include using namespace std; @@ -400,3 +402,78 @@ extern "C" JNIEXPORT void Java_com_tns_Runtime_initContentKeyedBindings(JNIEnv* extern "C" JNIEXPORT void Java_com_tns_Runtime_setAdmissionSpinMicros(JNIEnv* _env, jclass clazz, jint micros) { AdmissionGate::s_spinMicros.store(micros, std::memory_order_relaxed); } + +extern "C" JNIEXPORT void Java_com_tns_Runtime_registerInterfaceProxyClass(JNIEnv* env, jclass, jstring proxyClassName, jstring interfaceName) { + try { + MetadataNode::RegisterInterfaceProxyClass(ArgConverter::jstringToString(proxyClassName), ArgConverter::jstringToString(interfaceName)); + } catch (NativeScriptException& e) { + __android_log_print(ANDROID_LOG_WARN, "JS", "No metadata for the interface of %s: %s", + ArgConverter::jstringToString(proxyClassName).c_str(), e.GetErrorMessage().c_str()); + } +} + +// Raw JNI throughout: an exception the method throws must stay pending, so that it reaches +// the Java caller unchanged. +extern "C" JNIEXPORT jobject Java_com_tns_Runtime_invokeNonvirtualNative(JNIEnv* env, jclass, jobject receiver, jobject method, jclass declaringClass, jstring shorty, + jlongArray primitiveArgs, jobjectArray args, jlongArray primitiveResult) { + jmethodID mid = env->FromReflectedMethod(method); + + const char* chars = env->GetStringUTFChars(shorty, nullptr); + string types(chars); + env->ReleaseStringUTFChars(shorty, chars); + + size_t argc = types.size() - 1; + vector values(argc > 0 ? argc : 1); + vector bits(argc); + if (argc > 0 && primitiveArgs != nullptr) { + env->GetLongArrayRegion(primitiveArgs, 0, static_cast(argc), bits.data()); + } + for (size_t i = 0; i < argc; i++) { + jvalue& value = values[i]; + switch (types[i + 1]) { + case 'Z': value.z = bits[i] != 0 ? JNI_TRUE : JNI_FALSE; break; + case 'B': value.b = static_cast(bits[i]); break; + case 'C': value.c = static_cast(bits[i]); break; + case 'S': value.s = static_cast(bits[i]); break; + case 'I': value.i = static_cast(bits[i]); break; + case 'J': value.j = bits[i]; break; + case 'F': { + auto raw = static_cast(bits[i]); + memcpy(&value.f, &raw, sizeof(value.f)); + break; + } + case 'D': memcpy(&value.d, &bits[i], sizeof(value.d)); break; + default: value.l = env->GetObjectArrayElement(args, static_cast(i)); break; + } + } + + jlong result = 0; + jobject objectResult = nullptr; + switch (types[0]) { + case 'V': env->CallNonvirtualVoidMethodA(receiver, declaringClass, mid, values.data()); break; + case 'Z': result = env->CallNonvirtualBooleanMethodA(receiver, declaringClass, mid, values.data()); break; + case 'B': result = env->CallNonvirtualByteMethodA(receiver, declaringClass, mid, values.data()); break; + case 'C': result = env->CallNonvirtualCharMethodA(receiver, declaringClass, mid, values.data()); break; + case 'S': result = env->CallNonvirtualShortMethodA(receiver, declaringClass, mid, values.data()); break; + case 'I': result = env->CallNonvirtualIntMethodA(receiver, declaringClass, mid, values.data()); break; + case 'J': result = env->CallNonvirtualLongMethodA(receiver, declaringClass, mid, values.data()); break; + case 'F': { + jfloat f = env->CallNonvirtualFloatMethodA(receiver, declaringClass, mid, values.data()); + int32_t raw; + memcpy(&raw, &f, sizeof(raw)); + result = raw; + break; + } + case 'D': { + jdouble d = env->CallNonvirtualDoubleMethodA(receiver, declaringClass, mid, values.data()); + memcpy(&result, &d, sizeof(result)); + break; + } + default: objectResult = env->CallNonvirtualObjectMethodA(receiver, declaringClass, mid, values.data()); break; + } + + if (primitiveResult != nullptr && !env->ExceptionCheck()) { + env->SetLongArrayRegion(primitiveResult, 0, 1, &result); + } + return objectResult; +} diff --git a/test-app/runtime/src/main/java/com/tns/AppConfig.java b/test-app/runtime/src/main/java/com/tns/AppConfig.java index 7d7dcd086..3ec37c7e1 100644 --- a/test-app/runtime/src/main/java/com/tns/AppConfig.java +++ b/test-app/runtime/src/main/java/com/tns/AppConfig.java @@ -23,7 +23,8 @@ protected enum KnownKeys { DiscardUncaughtJsExceptions("discardUncaughtJsExceptions", false), EnableLineBreakpoins("enableLineBreakpoints", false), EnableMultithreadedJavascript("enableMultithreadedJavascript", false), - UncaughtErrorPolicy("uncaughtErrorPolicy", "report"); + UncaughtErrorPolicy("uncaughtErrorPolicy", "report"), + InterfaceProxies("interfaceProxies", false); private final String name; private final Object defaultValue; @@ -142,6 +143,9 @@ public AppConfig(File appDir) { if (androidObject.has(KnownKeys.EnableMultithreadedJavascript.getName())) { values[KnownKeys.EnableMultithreadedJavascript.ordinal()] = androidObject.getBoolean(KnownKeys.EnableMultithreadedJavascript.getName()); } + if (androidObject.has(KnownKeys.InterfaceProxies.getName())) { + values[KnownKeys.InterfaceProxies.ordinal()] = androidObject.getBoolean(KnownKeys.InterfaceProxies.getName()); + } } } } catch (Exception e) { @@ -216,6 +220,14 @@ public boolean getEnableMultithreadedJavascript() { return (boolean)values[KnownKeys.EnableMultithreadedJavascript.ordinal()]; } + /** + * Implement an interface that has no class in the APK with a java.lang.reflect.Proxy + * instead of generating, writing and loading a dex for it. + */ + public boolean getInterfaceProxies() { + return (boolean)values[KnownKeys.InterfaceProxies.ordinal()]; + } + // Security conf /** diff --git a/test-app/runtime/src/main/java/com/tns/DexFactory.java b/test-app/runtime/src/main/java/com/tns/DexFactory.java index ff2660c13..c89738f13 100644 --- a/test-app/runtime/src/main/java/com/tns/DexFactory.java +++ b/test-app/runtime/src/main/java/com/tns/DexFactory.java @@ -21,13 +21,17 @@ import java.io.InvalidClassException; import java.io.OutputStreamWriter; import java.lang.reflect.Array; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.concurrent.ConcurrentHashMap; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -45,19 +49,25 @@ public class DexFactory { private final ClassStorageService classStorageService; private final boolean injectIntoParentClassLoader; + private final boolean useInterfaceProxies; + private ProxyGenerator proxyGenerator; private HashMap> injectedDexClasses = new HashMap>(); + // Process-wide, like the native class cache that hands a resolved class to every runtime. + private static final ConcurrentHashMap, Constructor> interfaceProxyConstructors = new ConcurrentHashMap, Constructor>(); + DexFactory(Logger logger, ClassLoader classLoader, File dexBaseDir, String dexThumb, ClassStorageService classStorageService) { - this(logger, classLoader, dexBaseDir, dexThumb, classStorageService, false); + this(logger, classLoader, dexBaseDir, dexThumb, classStorageService, false, false); } - DexFactory(Logger logger, ClassLoader classLoader, File dexBaseDir, String dexThumb, ClassStorageService classStorageService, boolean injectIntoParentClassLoader) { + DexFactory(Logger logger, ClassLoader classLoader, File dexBaseDir, String dexThumb, ClassStorageService classStorageService, boolean injectIntoParentClassLoader, boolean useInterfaceProxies) { this.logger = logger; this.classLoader = classLoader; this.dexDir = dexBaseDir; this.dexThumb = dexThumb; this.injectIntoParentClassLoader = injectIntoParentClassLoader; + this.useInterfaceProxies = useInterfaceProxies; this.odexDir = new File(this.dexDir, "odex"); this.proxyGenerator = new ProxyGenerator(this.dexDir.getAbsolutePath()); @@ -86,8 +96,9 @@ public class DexFactory { /** * The classes this process resolved without a prebuilt class from the APK, - * one line each: {@code }. - * "cached" means the dex was reused from the on-disk dex cache. + * one line each: {@code }. + * "cached" means the dex was reused from the on-disk dex cache, "proxy" that + * the class is a java.lang.reflect.Proxy class (the interfaceProxies option). */ public static String[] getRuntimeGeneratedClasses() { synchronized (runtimeGeneratedClasses) { @@ -95,14 +106,52 @@ public static String[] getRuntimeGeneratedClasses() { } } - private static void recordRuntimeGeneratedClass(String requestedName, Class loadedClass, boolean isInterface, boolean generated) { - String entry = (generated ? "generated" : "cached") + " " + (isInterface ? "interface" : "class") + " " + loadedClass.getName() + " " + requestedName; + private static void recordRuntimeGeneratedClass(String requestedName, Class loadedClass, boolean isInterface, String source) { + String entry = source + " " + (isInterface ? "interface" : "class") + " " + loadedClass.getName() + " " + requestedName; boolean added; synchronized (runtimeGeneratedClasses) { added = runtimeGeneratedClasses.add(entry); } if (added && com.tns.Runtime.isDebuggable()) { - Log.i("JS", (generated ? "Generated class at runtime: " : "Loaded runtime-generated class from the dex cache: ") + loadedClass.getName() + " (requested as " + requestedName + ")"); + String what = source.equals("generated") ? "Generated class at runtime: " + : source.equals("cached") ? "Loaded runtime-generated class from the dex cache: " + : "Implemented interface with a java.lang.reflect.Proxy: "; + Log.i("JS", what + loadedClass.getName() + " (requested as " + requestedName + ")"); + } + } + + static boolean isInterfaceProxyClass(Class clazz) { + return interfaceProxyConstructors.containsKey(clazz); + } + + static Object newInterfaceProxy(Class proxyClass, InvocationHandler handler) throws ReflectiveOperationException { + return interfaceProxyConstructors.get(proxyClass).newInstance(handler); + } + + /** + * A proxy class for an anonymous implementation of the interface, or null when a + * proxy cannot behave like the class that would be generated for it, in which case + * the class is generated. + */ + private Class getInterfaceProxyClass(String interfaceName, String requestedName) { + try { + Class iface = Class.forName(interfaceName, false, classLoader); + // The proxy class is defined in this loader, which must see both interfaces; the + // boot class loader of platform interfaces cannot see the runtime's. + ClassLoader loader = iface.getClassLoader() == Object.class.getClassLoader() + ? NativeScriptRuntimeBound.class.getClassLoader() + : iface.getClassLoader(); + Class proxyClass = Proxy.getProxyClass(loader, iface, NativeScriptRuntimeBound.class); + if (interfaceProxyConstructors.putIfAbsent(proxyClass, proxyClass.getConstructor(InvocationHandler.class)) == null) { + // A JS wrapper created for an instance by its class name uses the interface's metadata. + Runtime.registerInterfaceProxyClass(proxyClass.getName().replace('.', '/'), iface.getName().replace('.', '/')); + } + classStorageService.storeClass(proxyClass.getName(), proxyClass); + recordRuntimeGeneratedClass(requestedName, proxyClass, true, "proxy"); + return proxyClass; + } catch (Throwable e) { + Log.d("JS", "Generating a class for " + interfaceName + ": a java.lang.reflect.Proxy cannot implement it (" + e + ")"); + return null; } } @@ -153,6 +202,14 @@ public Class resolveClass(String baseClassName, String name, String className classToProxy = this.getClassToProxyName(className); } + if (useInterfaceProxies && isInterface && !isNamedProxy && !hasImplementedInterfaces(implementedInterfaces)) { + Class proxyClass = getInterfaceProxyClass(classToProxy, className); + if (proxyClass != null) { + this.injectedDexClasses.put(originalFullClassName, proxyClass); + return proxyClass; + } + } + // strip the `com.tns.gen` off the base extended class name String desiredDexClassName = this.getClassToProxyName(fullClassName); @@ -231,7 +288,7 @@ public Class resolveClass(String baseClassName, String name, String className classStorageService.storeClass(result.getName(), result); this.injectedDexClasses.put(originalFullClassName, result); - recordRuntimeGeneratedClass(className, result, isInterface, generated); + recordRuntimeGeneratedClass(className, result, isInterface, generated ? "generated" : "cached"); return result; } diff --git a/test-app/runtime/src/main/java/com/tns/InterfaceProxyHandler.java b/test-app/runtime/src/main/java/com/tns/InterfaceProxyHandler.java new file mode 100644 index 000000000..505c2c6c3 --- /dev/null +++ b/test-app/runtime/src/main/java/com/tns/InterfaceProxyHandler.java @@ -0,0 +1,86 @@ +package com.tns; + +import android.os.Build; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + * Behaves like the class the runtime generates for an anonymous interface + * implementation (see DexFactory.resolveClass): only the interface's abstract + * methods reach JS. Object's methods keep identity semantics and a default method + * runs its Java body, even when the JS implementation defines a function of that name. + */ +final class InterfaceProxyHandler implements InvocationHandler { + private final int runtimeId; + private final int objectId; + + InterfaceProxyHandler(int runtimeId, int objectId) { + this.runtimeId = runtimeId; + this.objectId = objectId; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + String name = method.getName(); + + // The owner is fixed at construction, so the setters have nothing to record. + if (method.getDeclaringClass() == NativeScriptRuntimeBound.class) { + if (name.equals("getRuntimeTag__ns")) { + return Runtime.runtimeTagOf(runtimeId); + } else if (name.equals("getObjectId__ns")) { + return objectId; + } + return null; + } + + // Matched by signature: an interface that redeclares one of these, as + // Comparator does equals, is the declaring class of the Method passed here. + int argCount = args == null ? 0 : args.length; + if (argCount == 0) { + if (name.equals("hashCode")) { + return System.identityHashCode(proxy); + } else if (name.equals("toString")) { + return proxy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(proxy)); + } + } else if (argCount == 1 && name.equals("equals") && method.getParameterTypes()[0] == Object.class) { + return proxy == args[0]; + } + + if (Build.VERSION.SDK_INT >= 24 && method.isDefault()) { + return Runtime.invokeInterfaceDefault(proxy, method, args); + } + + Class returnType = method.getReturnType(); + Object result = Runtime.callJSMethodOfOwner(runtimeId, objectId, proxy, name, returnType, false, 0, args); + + // A generated class casts the result to the return type, or to its box for a + // primitive; the proxy would instead widen a box or throw IllegalArgumentException. + if (result != null && returnType != void.class) { + Class expected = returnType.isPrimitive() ? boxOf(returnType) : returnType; + if (returnType.isPrimitive() ? result.getClass() != expected : !expected.isInstance(result)) { + throw new ClassCastException(result.getClass().getName() + " cannot be cast to " + expected.getName()); + } + } + return result; + } + + private static Class boxOf(Class primitive) { + if (primitive == boolean.class) { + return Boolean.class; + } else if (primitive == byte.class) { + return Byte.class; + } else if (primitive == char.class) { + return Character.class; + } else if (primitive == short.class) { + return Short.class; + } else if (primitive == int.class) { + return Integer.class; + } else if (primitive == long.class) { + return Long.class; + } else if (primitive == float.class) { + return Float.class; + } + return Double.class; + } +} diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index 764a8eeb2..91f2349b8 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -546,11 +546,15 @@ private static void recordOwningRuntime(Object instance, int runtimeId, int obje if (bound.getRuntimeTag__ns() == 0) { // Before the tag: readers take the id only once they see a tag. bound.setObjectId__ns(objectId); - bound.setRuntimeTag__ns(runtimeId + 1); + bound.setRuntimeTag__ns(runtimeTagOf(runtimeId)); } } } + static int runtimeTagOf(int runtimeId) { + return runtimeId + 1; + } + /* * The id of the runtime that created the instance, or NO_OWNING_RUNTIME when * nothing has recorded one yet. A returned id may name a runtime that has @@ -781,7 +785,7 @@ private void init(Logger logger, String appName, String nativeLibDir, File rootD this.logger = logger; boolean isMainThread = this.workerId == 0; - this.dexFactory = new DexFactory(logger, classLoader, dexDir, dexThumb, classStorageService, isMainThread); + this.dexFactory = new DexFactory(logger, classLoader, dexDir, dexThumb, classStorageService, isMainThread, appConfig.getInterfaceProxies()); if (logger.isEnabled()) { logger.write("Initializing NativeScript JAVA"); @@ -1120,6 +1124,16 @@ private static String getTypeMetadata(Class clazz) { return ret; } + /* + * Takes the place of the Java constructor call for a class DexFactory.resolveClass + * returned as an interface proxy. The owner and id are fixed in the handler here, so + * nothing goes through initInstance; registerConstructedInstance follows as usual. + */ + @RuntimeCallable + private Object newInterfaceProxy(Class proxyClass, int objectId) throws ReflectiveOperationException { + return DexFactory.newInterfaceProxy(proxyClass, new InterfaceProxyHandler(runtimeId, objectId)); + } + // One upcall for what native needs after constructing an object from JS. @RuntimeCallable private long registerConstructedInstance(Object instance, int objectId) { @@ -1287,6 +1301,10 @@ public static Object callJSMethod(Object javaObject, String methodName, Class objectIdHint = bound.getObjectId__ns(); } } + return callJSMethodOfOwner(owningRuntimeId, objectIdHint, javaObject, methodName, retType, isConstructor, delay, args); + } + + static Object callJSMethodOfOwner(int owningRuntimeId, int objectIdHint, Object javaObject, String methodName, Class retType, boolean isConstructor, long delay, Object[] args) throws NativeScriptException { Runtime runtime; if (owningRuntimeId != NO_OWNING_RUNTIME) { @@ -1427,6 +1445,8 @@ static Class getClassForName(String className) { return classStorageService.retrieveClass(className); } + private static final String INTERFACE_PROXY_CONSTRUCTOR_SIGNATURE = "(Ljava/lang/reflect/InvocationHandler;)V"; + @RuntimeCallable private String resolveConstructorSignature(Class clazz, Object[] args) throws Exception { // Pete: cache stuff here, or in the cpp part @@ -1435,7 +1455,13 @@ private String resolveConstructorSignature(Class clazz, Object[] args) throws logger.write("resolveConstructorSignature: Resolving constructor for class " + clazz.getName()); } - String res = MethodResolver.resolveConstructorSignature(clazz, args); + String res; + if (DexFactory.isInterfaceProxyClass(clazz)) { + // Native reads this signature, for no arguments, as "construct with newInterfaceProxy". + res = (args == null || args.length == 0) ? INTERFACE_PROXY_CONSTRUCTOR_SIGNATURE : null; + } else { + res = MethodResolver.resolveConstructorSignature(clazz, args); + } if (res == null) { throw new Exception("Failed resolving constructor for class \'" + clazz.getName() + "\' with " + (args != null ? args.length : 0) + " parameters. " + FAILED_CTOR_RESOLUTION_MSG); @@ -1689,6 +1715,90 @@ private static Object takePrimitiveResult(Object nativeResult, int returnType, l } } + static native void registerInterfaceProxyClass(String proxyClassName, String interfaceName); + + // shorty: the JNI shorty of the method, return type first; primitive arguments travel as + // raw bits in primitiveArgs, references in args. A primitive result comes back in + // primitiveResult[0] as raw bits. + private static native Object invokeNonvirtualNative(Object receiver, Method method, Class declaringClass, String shorty, long[] primitiveArgs, Object[] args, long[] primitiveResult); + + /* + * Runs an interface's default method on an instance whose class does not override it, + * the way invokespecial would: a virtual call would come straight back to the proxy. + */ + static Object invokeInterfaceDefault(Object receiver, Method method, Object[] args) { + Class[] parameterTypes = method.getParameterTypes(); + Class returnType = method.getReturnType(); + char[] shorty = new char[parameterTypes.length + 1]; + shorty[0] = shortyChar(returnType); + long[] primitiveArgs = parameterTypes.length > 0 ? new long[parameterTypes.length] : null; + for (int i = 0; i < parameterTypes.length; i++) { + shorty[i + 1] = shortyChar(parameterTypes[i]); + if (parameterTypes[i].isPrimitive()) { + primitiveArgs[i] = rawBits(args[i]); + } + } + boolean primitiveReturn = returnType.isPrimitive() && returnType != void.class; + long[] primitiveResult = primitiveReturn ? new long[1] : null; + Object result = invokeNonvirtualNative(receiver, method, method.getDeclaringClass(), new String(shorty), primitiveArgs, args, primitiveResult); + return primitiveReturn ? fromRawBits(returnType, primitiveResult[0]) : result; + } + + private static char shortyChar(Class type) { + if (!type.isPrimitive()) { + return 'L'; + } else if (type == boolean.class) { + return 'Z'; + } else if (type == byte.class) { + return 'B'; + } else if (type == char.class) { + return 'C'; + } else if (type == short.class) { + return 'S'; + } else if (type == int.class) { + return 'I'; + } else if (type == long.class) { + return 'J'; + } else if (type == float.class) { + return 'F'; + } else if (type == double.class) { + return 'D'; + } + return 'V'; + } + + private static long rawBits(Object boxed) { + if (boxed instanceof Boolean) { + return ((Boolean) boxed) ? 1 : 0; + } else if (boxed instanceof Character) { + return (Character) boxed; + } else if (boxed instanceof Float) { + return Float.floatToRawIntBits((Float) boxed); + } else if (boxed instanceof Double) { + return Double.doubleToRawLongBits((Double) boxed); + } + return ((Number) boxed).longValue(); + } + + private static Object fromRawBits(Class type, long bits) { + if (type == boolean.class) { + return bits != 0; + } else if (type == byte.class) { + return (byte) bits; + } else if (type == char.class) { + return (char) bits; + } else if (type == short.class) { + return (short) bits; + } else if (type == int.class) { + return (int) bits; + } else if (type == long.class) { + return bits; + } else if (type == float.class) { + return Float.intBitsToFloat((int) bits); + } + return Double.longBitsToDouble(bits); + } + private static Object defaultPrimitiveValue(Class type) { if (type == boolean.class) { return Boolean.FALSE; From 3a259506260eed2e48bbbb7b670dcc665d8cdc9b Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 22:10:15 -0300 Subject: [PATCH 09/14] fix(sbg): show binding warnings in quiet gradle runs The NativeScript CLI runs gradle with --quiet, which hides the warn level, so the static binding generator's warnings (Fragment classes without a @JavaProxy name, named extends without a source location) never reached a default `ns build`. They are printed at the quiet level instead. They only exist when content-keyed binding names are on. --- test-app/app/build.gradle | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test-app/app/build.gradle b/test-app/app/build.gradle index ead48359b..e2b5fb5da 100644 --- a/test-app/app/build.gradle +++ b/test-app/app/build.gradle @@ -663,9 +663,10 @@ task runSbg(type: BuildToolTask) { } } doLast { - // The generator's standard output only reaches runSbg.log. + // The generator's standard output only reaches runSbg.log, and the + // NativeScript CLI runs gradle with --quiet, which hides the warn level. if (sbgWarningsFile.isFile()) { - sbgWarningsFile.eachLine { logger.warn("runSbg: ${it}") } + sbgWarningsFile.eachLine { logger.quiet("runSbg: ${it}") } } } } From 672ebe97579941ca6c42d05529b10a54b456e590 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 22:41:53 -0300 Subject: [PATCH 10/14] fix(sbg): keep accessor overrides when bindings are not content-keyed The JS parser left object literal getters and setters out of a row's member list in every mode, so without content keys an extend like `Base.extend({ get onClick() { return fn; } })` lost the override its prebuilt class used to get, although the runtime still counts a getter that returns a function as an override. The parser now lists accessors with a `~` prefix. A name defined more than once keeps its first position and the kind of its last definition, as in the object the literal creates. The generator strips the prefix without content keys, and drops prefixed names for every row with them, so content keys keep agreeing with the members the runtime hashes. --- .../cases/content_members/app/members.js | 17 +++++++ .../tests/specs/ast-parser-tests.spec.js | 8 ++- .../jsparser/visitors/es5-visitors.js | 38 ++++++++++---- .../staticbindinggenerator/ContentKey.java | 3 +- .../staticbindinggenerator/DataRow.java | 24 ++++++++- .../staticbindinggenerator/Generator.java | 11 ++++- .../test/ContentKeyTest.java | 1 + .../test/ContentKeyedGeneratorTest.java | 49 +++++++++++++++++++ .../test/DataRowTest.java | 14 ++++-- 9 files changed, 147 insertions(+), 18 deletions(-) diff --git a/test-app/build-tools/jsparser/tests/cases/content_members/app/members.js b/test-app/build-tools/jsparser/tests/cases/content_members/app/members.js index 007cb2492..de1e87aee 100644 --- a/test-app/build-tools/jsparser/tests/cases/content_members/app/members.js +++ b/test-app/build-tools/jsparser/tests/cases/content_members/app/members.js @@ -18,6 +18,23 @@ var LiteralMembers = java.lang.Object.extend("LiteralMembers", { run: function () {} }); +var AccessorMembers = java.lang.Object.extend("AccessorMembers", { + get getOnly() { return 1; }, + set setOnly(value) {}, + get pair() { return 1; }, + set pair(value) {}, + get "quotedAccessor"() { return 1; }, + get accessorThenData() { return 1; }, + accessorThenData: function () {}, + dataThenAccessor: function () {}, + get dataThenAccessor() { return 1; }, + get toString() { return function () { return "accessor"; }; } +}); + +var AccessorInterface = java.lang.Runnable.extend("AccessorInterface", { + get run() { return function () {}; } +}); + var PrototypeMembers = /** @class */ (function (_super) { __extends(PrototypeMembers, _super); function PrototypeMembers() { diff --git a/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js b/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js index 729e4c902..7e57ca8ef 100644 --- a/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js +++ b/test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js @@ -327,9 +327,15 @@ describe("parser/js_parser tests", function () { rows[parts[4]] = parts; } - expect(rows["LiteralMembers"][5]).toBe("identifierMember,quotedMember,shorthandMember,dataMember,run"); + expect(rows["LiteralMembers"][5]).toBe("identifierMember,quotedMember,shorthandMember,dataMember,~getterMember,~setterMember,run"); expect(rows["LiteralMembers"][8]).toBe("java.lang.Runnable"); + // Accessors are marked with `~`; a redefined name keeps its first + // position and the kind of its last definition. + expect(rows["AccessorMembers"][5]).toBe("~getOnly,~setOnly,~pair,~quotedAccessor,accessorThenData,~dataThenAccessor,~toString"); + expect(rows["AccessorInterface"][0]).toBe("java.lang.Runnable"); + expect(rows["AccessorInterface"][5]).toBe("~run"); + expect(rows["PrototypeMembers"][5]).toBe("toString,quotedPrototypeMember,chainedA,chainedB,sequenceA,sequenceB"); expect(rows["PrototypeMembers"][8]).toBe("java.lang.Runnable"); diff --git a/test-app/build-tools/jsparser/visitors/es5-visitors.js b/test-app/build-tools/jsparser/visitors/es5-visitors.js index 8a68f7da4..8def5f912 100644 --- a/test-app/build-tools/jsparser/visitors/es5-visitors.js +++ b/test-app/build-tools/jsparser/visitors/es5-visitors.js @@ -565,20 +565,25 @@ var es5_visitors = (function() { * HELPER METHODS */ - // The member names the runtime can see as own data properties of an + // The member names the runtime can see as own properties of an // implementation object, which is what it names and generates a class from. var MEMBER_NAME_PATTERN = /^[A-Za-z_$][A-Za-z0-9_$]*$/; - // Returns the name an object literal property defines as an own data - // property, or null for spreads, computed keys, getters and setters, and - // keys that cannot name a Java method. + // Prefixes a member an object literal defines as a getter or setter. The + // runtime finds method overrides behind getters but leaves accessors out of + // content keys, so the static binding generator decides per mode. + var ACCESSOR_PREFIX = "~"; + + function _isAccessor(property) { + return types.isObjectMethod(property) && (property.kind === "get" || property.kind === "set"); + } + + // Returns the name an object literal property defines as an own property, + // or null for spreads, computed keys, and keys that cannot name a Java method. function _getMemberKeyName(property) { if (!property || property.computed || !property.key) { return null; } - if (types.isObjectMethod(property) && property.kind !== "method") { - return null; - } var name; if (types.isIdentifier(property.key)) { @@ -603,6 +608,21 @@ var es5_visitors = (function() { } } + // A redefined property keeps its first position and takes the kind of its + // last definition, as in the object the literal creates. + function _addLiteralMember(members, name, isAccessor) { + var index = members.indexOf(name); + if (index === -1) { + index = members.indexOf(ACCESSOR_PREFIX + name); + } + var entry = isAccessor ? ACCESSOR_PREFIX + name : name; + if (index === -1) { + members.push(entry); + } else { + members[index] = entry; + } + } + function _getOverriddenMethods(node, config) { var overriddenMethodNames = []; if (types.isObjectExpression(node)) { @@ -613,7 +633,7 @@ var es5_visitors = (function() { if (keyName === null) { continue; } - _pushUnique(overriddenMethodNames, keyName); + _addLiteralMember(overriddenMethodNames, keyName, _isAccessor(objectProperties[index])); } } @@ -667,7 +687,7 @@ var es5_visitors = (function() { implementedInterfaces.push(interfaceName); } } else { - _pushUnique(overriddenMethodNames, keyName); + _addLiteralMember(overriddenMethodNames, keyName, _isAccessor(objectProperties[index])); } } } diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java index 6eee9f094..a457b2def 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/ContentKey.java @@ -50,7 +50,8 @@ private ContentKey() { /** * The members a binding is generated with, from the JS parser's member names: - * sorted, without duplicates and without the names extend() itself adds. + * sorted, without duplicates, without the names extend() itself adds, and + * without accessors, which the runtime leaves out of the key. */ public static List memberNames(String[] parsedNames) { TreeSet result = new TreeSet<>(UTF8_ORDER); diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/DataRow.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/DataRow.java index 8848ac8b7..d30a2b623 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/DataRow.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/DataRow.java @@ -1,8 +1,12 @@ package org.nativescript.staticbindinggenerator; import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.Set; public class DataRow { + public static final String ACCESSOR_PREFIX = "~"; + private final String DELIMITER = "\\*"; private final int ELEMENT_NUMBER = 9; private final String row; @@ -51,8 +55,24 @@ public String getNewClassName() { return newClassName; } - public String[] getMethods() { - return methods; + /** + * The implementation object's members. The JS parser prefixes those an + * object literal defines as accessors with {@link #ACCESSOR_PREFIX}; they are + * returned as plain names, or left out when {@code includeAccessors} is false. + */ + public String[] getMethods(boolean includeAccessors) { + if (methods == null) { + return null; + } + Set result = new LinkedHashSet<>(methods.length); + for (String method : methods) { + if (!method.startsWith(ACCESSOR_PREFIX)) { + result.add(method); + } else if (includeAccessors) { + result.add(method.substring(ACCESSOR_PREFIX.length())); + } + } + return result.toArray(new String[0]); } public String getFilename() { diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java index d294f0825..8bcec7aa0 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java @@ -196,7 +196,8 @@ public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) // The class body must be a function of the content name alone, whichever // call site sharing the name it is generated for. boolean contentKeyed = usesContentKeyedName(dataRow, clazz); - List methods = contentKeyed ? ContentKey.memberNames(dataRow.getMethods()) : Arrays.asList(dataRow.getMethods()); + String[] parsedMethods = getMemberNames(dataRow); + List methods = contentKeyed ? ContentKey.memberNames(parsedMethods) : Arrays.asList(parsedMethods); List interfaces = contentKeyed ? ContentKey.interfaceNames(dataRow.getInterfaces()) : Arrays.asList(dataRow.getInterfaces()); if (hasSpecifiedName) { @@ -237,6 +238,12 @@ public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) } } + // With content keys on, accessors are never overrides, whichever way a + // row's class is named. + private String[] getMemberNames(DataRow dataRow) { + return dataRow.getMethods(!contentKeyedBindings); + } + private boolean usesContentKeyedName(DataRow dataRow, JavaClass clazz) { return contentKeyedBindings && !clazz.isInterface() && dataRow.getFilename().isEmpty(); } @@ -251,7 +258,7 @@ private String getContentClassSimpleName(DataRow dataRow, JavaClass clazz, List< private String getContentClassName(DataRow dataRow, JavaClass clazz) { List interfaces = ContentKey.interfaceNames(dataRow.getInterfaces()); - List methods = ContentKey.memberNames(dataRow.getMethods()); + List methods = ContentKey.memberNames(getMemberNames(dataRow)); return DEFAULT_PACKAGE_NAME + "." + clazz.getPackageName() + "." + getContentClassSimpleName(dataRow, clazz, interfaces, methods); } diff --git a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java index 4f75d6224..dfbf554a5 100644 --- a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java +++ b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyTest.java @@ -78,6 +78,7 @@ public void separatesDifferentContent() { public void selectsMemberNames() { assertEquals(list("$x", "_y", "a1", "onCreate", "toString"), ContentKey.memberNames(new String[]{"toString", "onCreate", "constructor", "super", "__parent", "__child", "", "a1", "_y", "$x", "toString", "1a", "a-b"})); + assertEquals(list("onClick"), ContentKey.memberNames(new String[]{"~getter", "onClick", "~onClick"})); assertEquals(NONE, ContentKey.memberNames(new String[]{""})); assertEquals(NONE, ContentKey.memberNames(null)); } diff --git a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java index e955c7e6e..51823c92f 100644 --- a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java +++ b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java @@ -19,11 +19,13 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; public class ContentKeyedGeneratorTest { @@ -152,6 +154,53 @@ public void keepsCallSiteNamesWithoutTheOptIn() throws Exception { Assert.assertTrue(generator.getContentNameCollisions().isEmpty()); } + private void assertNoAccessorMarkers() throws IOException { + try (Stream paths = Files.walk(outputDir.toPath())) { + for (Path file : paths.filter(p -> p.toString().endsWith(".java")).collect(Collectors.toList())) { + String source = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); + Assert.assertFalse(file.toString(), source.contains(DataRow.ACCESSOR_PREFIX)); + } + } + } + + @Test + public void overridesAccessorsWithoutTheOptIn() throws Exception { + generate(false, + "com.example.View*tests_a*3*5**~createView,~init***", + "com.example.View*****toString,~createView*com.example.NamedView*app/f.js*", + "com.example.MyInterface*tests_a*12*5**~getTile***"); + assertNoAccessorMarkers(); + + List files = generatedFiles("com/tns/gen/com/example"); + Assert.assertEquals(files.toString(), 2, files.size()); + String anonymous = read("com/tns/gen/com/example/" + files.stream().filter(f -> f.startsWith("View_")).findFirst().get()); + Assert.assertTrue(anonymous, anonymous.contains("createView(")); + Assert.assertTrue(anonymous, anonymous.contains("\"init\"")); + + String named = read("com/example/NamedView.java"); + Assert.assertTrue(named, named.contains("createView(")); + Assert.assertTrue(read("com/tns/gen/com/example/MyInterface.java").contains("getTile(")); + } + + @Test + public void leavesAccessorsOutOfContentKeyedBindings() throws Exception { + generate(true, + "com.example.View*tests_a*3*5**~createView,~init,toString***", + "com.example.View*****toString,~createView*com.example.NamedView*app/f.js*", + "com.example.MyInterface*tests_a*12*5**~getTile***"); + assertNoAccessorMarkers(); + + String name = "View_" + key("toString"); + Assert.assertEquals(Arrays.asList("MyInterface.java", name + ".java"), generatedFiles("com/tns/gen/com/example")); + String anonymous = read("com/tns/gen/com/example/" + name + ".java"); + Assert.assertFalse(anonymous, anonymous.contains("createView(")); + Assert.assertFalse(anonymous, anonymous.contains("\"init\"")); + + Assert.assertFalse(read("com/example/NamedView.java").contains("createView(")); + // An interface's abstract methods are implemented whatever the object defines. + Assert.assertTrue(read("com/tns/gen/com/example/MyInterface.java").contains("getTile(")); + } + @Test public void sharesIdenticalDuplicatesAndRejectsDifferentOnes() throws Exception { generate(false, diff --git a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/DataRowTest.java b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/DataRowTest.java index 5935b95fd..ac532f56e 100644 --- a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/DataRowTest.java +++ b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/DataRowTest.java @@ -19,7 +19,7 @@ public void testParseInterface() throws IOException { Assert.assertEquals(row.getLine(), "54"); Assert.assertEquals(row.getColumn(), "32"); Assert.assertEquals(row.getNewClassName(), "AppClickListener"); - Assert.assertEquals(row.getMethods()[0], "onClick"); + Assert.assertEquals(row.getMethods(true)[0], "onClick"); Assert.assertEquals(row.getFilename(), ""); Assert.assertEquals(row.getJsFilename(), ""); Assert.assertEquals(row.getInterfaces()[0], ""); @@ -36,12 +36,20 @@ public void testParseNamedExtend() throws IOException { Assert.assertEquals(row.getLine(), ""); Assert.assertEquals(row.getColumn(), ""); Assert.assertEquals(row.getNewClassName(), ""); - Assert.assertEquals(row.getMethods()[0], "toString"); + Assert.assertEquals(row.getMethods(true)[0], "toString"); Assert.assertEquals(row.getFilename(), "com.tns.NativeScriptActivity"); Assert.assertEquals(row.getJsFilename(), "MyActivity.js"); Assert.assertEquals(row.getInterfaces()[0], ""); } + @Test + public void readsAccessorMembers() { + DataRow row = new DataRow("com.example.View*tests_a*3*5**createView,~onClick,~createView,toString***"); + + Assert.assertArrayEquals(new String[]{"createView", "onClick", "toString"}, row.getMethods(true)); + Assert.assertArrayEquals(new String[]{"createView", "toString"}, row.getMethods(false)); + } + @Test public void testParseClassWithMultipleInterfaces() throws IOException { List lines = Utils.getDataRowsFromResource("datarow-class-extends-interfaces.txt"); @@ -52,7 +60,7 @@ public void testParseClassWithMultipleInterfaces() throws IOException { Assert.assertEquals(row.getFilename(), "com.tns.ComplexClass"); Assert.assertEquals(row.getJsFilename(), "app.js"); - String[] methods = row.getMethods(); + String[] methods = row.getMethods(true); Assert.assertEquals(6, methods.length); String[] interfaces = row.getInterfaces(); From 7bd8b3bd2e49a37c01dfe95735330c845d38c1d8 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 22:43:10 -0300 Subject: [PATCH 11/14] fix: load the prebuilt class an explicit com.tns.gen name asks for An extend name is told apart from a call-site name by its prefix, so `Interface.extend("com.tns.gen.a.B", {...})` counted as anonymous and was resolved to the interface's shared class without ever probing for a prebuilt class of the requested name. ExtendMethodCallback now passes whether the script named the class, and DexFactory probes that exact name before the shared one. Anonymous extends keep probing only the shared name. --- .../app/tests/testInterfaceExtendClasses.js | 15 +++++++++++++++ .../runtime/src/main/cpp/CallbackHandlers.cpp | 8 +++++--- .../runtime/src/main/cpp/CallbackHandlers.h | 4 +++- test-app/runtime/src/main/cpp/MetadataNode.cpp | 2 +- .../src/main/java/com/tns/ClassResolver.java | 6 +++--- .../src/main/java/com/tns/DexFactory.java | 18 ++++++++++++++---- .../runtime/src/main/java/com/tns/Runtime.java | 4 ++-- 7 files changed, 43 insertions(+), 14 deletions(-) diff --git a/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js index 27a743a65..0b764f2e9 100644 --- a/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js +++ b/test-app/app/src/main/assets/app/tests/testInterfaceExtendClasses.js @@ -30,6 +30,21 @@ describe("Anonymous extends of an interface", function () { expect(reportEntriesFor("com.tns.gen.java.lang.Runnable")).toEqual([]); }); + it("load the prebuilt class a name under com.tns.gen asks for", function () { + var calls = []; + var Extended = java.lang.Runnable.extend("com.tns.gen.tests.ExplicitlyNamedRunnable", { + run: function () { + calls.push("explicit"); + } + }); + var instance = new Extended(); + + callFromJava(instance, "run"); + expect(calls).toEqual(["explicit"]); + expect(instance.getClass().getName()).toBe("com.tns.gen.tests.ExplicitlyNamedRunnable"); + expect(reportEntriesFor("com.tns.gen.tests.ExplicitlyNamedRunnable")).toEqual([]); + }); + it("keep each call site's implementation when they extend the same interface", function () { var calls = []; var First = java.lang.Runnable.extend({ diff --git a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp index d29fde683..769e2da82 100644 --- a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp +++ b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp @@ -51,7 +51,7 @@ void CallbackHandlers::Init(Isolate *isolate) { NS_CHECK(RUNTIME_CLASS != nullptr); RESOLVE_CLASS_METHOD_ID = env.GetMethodID(RUNTIME_CLASS, "resolveClass", - "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;Z)Ljava/lang/Class;"); + "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;ZZ)Ljava/lang/Class;"); NS_CHECK(RESOLVE_CLASS_METHOD_ID != nullptr); CURRENT_OBJECTID_FIELD_ID = env.GetFieldID(RUNTIME_CLASS, "currentObjectId", "I"); @@ -213,7 +213,8 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local &j jclass CallbackHandlers::ResolveClass(Isolate *isolate, const string &baseClassName, const string &fullClassName, const Local &implementationObject, bool isInterface, - const vector *contentMemberNames) { + const vector *contentMemberNames, + bool hasExplicitName) { JEnv env; jclass globalRefToGeneratedClass = env.CheckForClassInCache(fullClassName); @@ -240,7 +241,8 @@ jclass CallbackHandlers::ResolveClass(Isolate *isolate, const string &baseClassN (jstring) javaFullClassName, methodOverrides, implementedInterfaces, - isInterface); + isInterface, + hasExplicitName); globalRefToGeneratedClass = env.InsertClassIntoCache(fullClassName, generatedClass); diff --git a/test-app/runtime/src/main/cpp/CallbackHandlers.h b/test-app/runtime/src/main/cpp/CallbackHandlers.h index cf308d57c..e1c732c23 100644 --- a/test-app/runtime/src/main/cpp/CallbackHandlers.h +++ b/test-app/runtime/src/main/cpp/CallbackHandlers.h @@ -46,12 +46,14 @@ namespace tns { /* * contentMemberNames, when given, are the method overrides of a class generated * at runtime, instead of the implementation object's function-valued members. + * hasExplicitName: fullClassName is the name the script passed to extend(). */ static jclass ResolveClass(v8::Isolate *isolate, const std::string &baseClassName, const std::string &fullClassName, const v8::Local &implementationObject, bool isInterface, - const std::vector *contentMemberNames = nullptr); + const std::vector *contentMemberNames = nullptr, + bool hasExplicitName = false); /* * The interface names the implementation object's `interfaces` array lists, diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index 8aa8dc0b0..f490ef179 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -2057,7 +2057,7 @@ void MetadataNode::ExtendMethodCallback(const v8::FunctionCallbackInfo resolveClass(String baseClassName, String fullClassName, DexFactory dexFactory, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException { + Class resolveClass(String baseClassName, String fullClassName, DexFactory dexFactory, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface, boolean hasExplicitName) throws ClassNotFoundException, IOException { String canonicalClassName = fullClassName.replace('/', '.'); String canonicalBaseClassName = baseClassName.replace('/', '.'); String name = ""; @@ -25,7 +25,7 @@ Class resolveClass(String baseClassName, String fullClassName, DexFactory dex // if binding generate proxy or load pregenerated if (isBindingClass) { - clazz = dexFactory.resolveClass(canonicalBaseClassName, name, className, methodOverrides, implementedInterfaces, isInterface); + clazz = dexFactory.resolveClass(canonicalBaseClassName, name, className, methodOverrides, implementedInterfaces, isInterface, hasExplicitName); } if (clazz == null) { @@ -40,7 +40,7 @@ Class resolveClass(String baseClassName, String fullClassName, DexFactory dex // anonymous extends are supplied. Log.w("JS", "Class " + className + " not precompiled; generating at runtime. Framework references resolve only if dex injection into the app class loader succeeds."); try { - clazz = dexFactory.resolveClass(canonicalBaseClassName, name, className, methodOverrides, implementedInterfaces, isInterface); + clazz = dexFactory.resolveClass(canonicalBaseClassName, name, className, methodOverrides, implementedInterfaces, isInterface, hasExplicitName); } catch (Throwable generationFailure) { // The precise not-found is the actionable error; a failed // generation attempt is its detail, not its replacement. diff --git a/test-app/runtime/src/main/java/com/tns/DexFactory.java b/test-app/runtime/src/main/java/com/tns/DexFactory.java index c89738f13..199d711fd 100644 --- a/test-app/runtime/src/main/java/com/tns/DexFactory.java +++ b/test-app/runtime/src/main/java/com/tns/DexFactory.java @@ -155,7 +155,11 @@ private Class getInterfaceProxyClass(String interfaceName, String requestedNa } } - public Class resolveClass(String baseClassName, String name, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException { + /** + * @param hasExplicitName className is the name the script passed to extend(), which + * may itself start with com.tns.gen. + */ + public Class resolveClass(String baseClassName, String name, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface, boolean hasExplicitName) throws ClassNotFoundException, IOException { String fullClassName = className.replace("$", "_"); // A named proxy (`Base.extend('a.b.C', {...})` / @JavaProxy) asks for @@ -177,10 +181,16 @@ public Class resolveClass(String baseClassName, String name, String className // The static binding generator keeps a `$` from the script's file name in an // anonymous extend's class name; the runtime already replaced the base class's `$`. Class pregeneratedClass = null; - if (!usesSharedInterfaceClass && !fullClassName.equals(className)) { - pregeneratedClass = loadPregeneratedClass(className.replace("-", "_")); + if (!usesSharedInterfaceClass || hasExplicitName) { + String requestedFullClassName = className.replace("$", "_"); + if (!requestedFullClassName.equals(className)) { + pregeneratedClass = loadPregeneratedClass(className.replace("-", "_")); + } + if (pregeneratedClass == null) { + pregeneratedClass = loadPregeneratedClass(requestedFullClassName.replace("-", "_")); + } } - if (pregeneratedClass == null) { + if (pregeneratedClass == null && usesSharedInterfaceClass) { pregeneratedClass = loadPregeneratedClass(fullClassName.replace("-", "_")); } if (pregeneratedClass != null) { diff --git a/test-app/runtime/src/main/java/com/tns/Runtime.java b/test-app/runtime/src/main/java/com/tns/Runtime.java index 91f2349b8..84d03546c 100644 --- a/test-app/runtime/src/main/java/com/tns/Runtime.java +++ b/test-app/runtime/src/main/java/com/tns/Runtime.java @@ -903,8 +903,8 @@ public void run() { } @RuntimeCallable - private Class resolveClass(String baseClassName, String fullClassName, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException { - Class javaClass = classResolver.resolveClass(baseClassName, fullClassName, dexFactory, methodOverrides, implementedInterfaces, isInterface); + private Class resolveClass(String baseClassName, String fullClassName, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface, boolean hasExplicitName) throws ClassNotFoundException, IOException { + Class javaClass = classResolver.resolveClass(baseClassName, fullClassName, dexFactory, methodOverrides, implementedInterfaces, isInterface, hasExplicitName); return javaClass; } From 043ed384d1874a901b36e61a5bd599ffcd73d817 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 22:43:29 -0300 Subject: [PATCH 12/14] fix(DexFactory): register an interface proxy class before publishing it The proxy class's constructor was published before the mapping from its class name to the interface's metadata was registered, so another runtime resolving the same interface in between could create and wrap an instance whose class name had no metadata yet. Register the mapping first on every call that finds no published constructor; registration is idempotent, and the published path stays a single lock-free lookup. --- test-app/runtime/src/main/java/com/tns/DexFactory.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test-app/runtime/src/main/java/com/tns/DexFactory.java b/test-app/runtime/src/main/java/com/tns/DexFactory.java index 199d711fd..561bc60b6 100644 --- a/test-app/runtime/src/main/java/com/tns/DexFactory.java +++ b/test-app/runtime/src/main/java/com/tns/DexFactory.java @@ -142,9 +142,12 @@ private Class getInterfaceProxyClass(String interfaceName, String requestedNa ? NativeScriptRuntimeBound.class.getClassLoader() : iface.getClassLoader(); Class proxyClass = Proxy.getProxyClass(loader, iface, NativeScriptRuntimeBound.class); - if (interfaceProxyConstructors.putIfAbsent(proxyClass, proxyClass.getConstructor(InvocationHandler.class)) == null) { - // A JS wrapper created for an instance by its class name uses the interface's metadata. + if (!interfaceProxyConstructors.containsKey(proxyClass)) { + Constructor constructor = proxyClass.getConstructor(InvocationHandler.class); + // A JS wrapper created for an instance by its class name uses the interface's + // metadata, so the mapping is registered before any runtime can see the class. Runtime.registerInterfaceProxyClass(proxyClass.getName().replace('.', '/'), iface.getName().replace('.', '/')); + interfaceProxyConstructors.putIfAbsent(proxyClass, constructor); } classStorageService.storeClass(proxyClass.getName(), proxyClass); recordRuntimeGeneratedClass(requestedName, proxyClass, true, "proxy"); From a363e53e3f69c8e4a38de8214b5c035422d7f6d1 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 22:43:29 -0300 Subject: [PATCH 13/14] test: give interface proxy polling specs room for their deadlines Two specs poll for up to 10 s, twice Jasmine's default spec timeout, so a slow device timed the spec out before its own deadline could report what it was waiting for. --- .../app/src/main/assets/app/tests/testInterfaceProxies.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-app/app/src/main/assets/app/tests/testInterfaceProxies.js b/test-app/app/src/main/assets/app/tests/testInterfaceProxies.js index 54118c150..14d4b1c14 100644 --- a/test-app/app/src/main/assets/app/tests/testInterfaceProxies.js +++ b/test-app/app/src/main/assets/app/tests/testInterfaceProxies.js @@ -337,7 +337,7 @@ describe("Interface implementations without a prebuilt class", function () { expect(String(report)).toBe("InterfaceProxyFixtures-caller>" + (multithreaded ? "InterfaceProxyFixtures-caller" : "main")); done(); })(); - }); + }, 20000); it("works in a worker, and from another runtime that wraps the worker's instance", function (done) { var Holder = com.tns.tests.RuntimeBoundHolder; @@ -375,7 +375,7 @@ describe("Interface implementations without a prebuilt class", function () { } setTimeout(poll, 20); })(); - }); + }, 20000); it("keeps the JS implementation of an instance only Java references", function () { (function () { From 0f15bb2eaebdcf74f27c6ab5a177d6f08184ca78 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 16 Nov 2026 23:01:34 -0300 Subject: [PATCH 14/14] fix(sbg): leave accessors out of content-keyed classes only With content-keyed binding names on, the generator dropped accessor members from every row. The runtime only does that for the classes it names after their content; for interface implementations and @JavaProxy/dotted names it still counts a getter that returns a function as an override, so those rows keep their accessor overrides in both modes. --- .../staticbindinggenerator/Generator.java | 12 ++++-------- .../test/ContentKeyedGeneratorTest.java | 3 ++- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java index 8bcec7aa0..22ca60a87 100644 --- a/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java +++ b/test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java @@ -196,7 +196,9 @@ public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) // The class body must be a function of the content name alone, whichever // call site sharing the name it is generated for. boolean contentKeyed = usesContentKeyedName(dataRow, clazz); - String[] parsedMethods = getMemberNames(dataRow); + // The runtime leaves accessors out of a content-keyed class only; for every + // other class it still counts a getter that returns a function as an override. + String[] parsedMethods = dataRow.getMethods(!contentKeyed); List methods = contentKeyed ? ContentKey.memberNames(parsedMethods) : Arrays.asList(parsedMethods); List interfaces = contentKeyed ? ContentKey.interfaceNames(dataRow.getInterfaces()) : Arrays.asList(dataRow.getInterfaces()); @@ -238,12 +240,6 @@ public Binding generateBinding(DataRow dataRow, HashSet interfaceNames) } } - // With content keys on, accessors are never overrides, whichever way a - // row's class is named. - private String[] getMemberNames(DataRow dataRow) { - return dataRow.getMethods(!contentKeyedBindings); - } - private boolean usesContentKeyedName(DataRow dataRow, JavaClass clazz) { return contentKeyedBindings && !clazz.isInterface() && dataRow.getFilename().isEmpty(); } @@ -258,7 +254,7 @@ private String getContentClassSimpleName(DataRow dataRow, JavaClass clazz, List< private String getContentClassName(DataRow dataRow, JavaClass clazz) { List interfaces = ContentKey.interfaceNames(dataRow.getInterfaces()); - List methods = ContentKey.memberNames(getMemberNames(dataRow)); + List methods = ContentKey.memberNames(dataRow.getMethods(false)); return DEFAULT_PACKAGE_NAME + "." + clazz.getPackageName() + "." + getContentClassSimpleName(dataRow, clazz, interfaces, methods); } diff --git a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java index 51823c92f..e4fbc5e37 100644 --- a/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java +++ b/test-app/build-tools/static-binding-generator/src/test/java/org/nativescript/staticbindinggenerator/test/ContentKeyedGeneratorTest.java @@ -196,7 +196,8 @@ public void leavesAccessorsOutOfContentKeyedBindings() throws Exception { Assert.assertFalse(anonymous, anonymous.contains("createView(")); Assert.assertFalse(anonymous, anonymous.contains("\"init\"")); - Assert.assertFalse(read("com/example/NamedView.java").contains("createView(")); + // Only content-keyed classes leave accessors out. + Assert.assertTrue(read("com/example/NamedView.java").contains("createView(")); // An interface's abstract methods are implemented whatever the object defines. Assert.assertTrue(read("com/tns/gen/com/example/MyInterface.java").contains("getTile(")); }