From f8cd9eed3e172fcc72c9fda3ff16399491eaebcb Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 05:11:14 -0300 Subject: [PATCH] perf(runtime): keep TypeScript-extended instances out of prototype mode The TypeScript helpers made each native instance the prototype of the constructor's own `this`. V8 gives every prototype a map of its own, so each field store in the constructor missed its inline cache and copied the map, and the instance kept a map no other instance shared. The instance is no longer linked to the constructor's `this`; __native finds it under a symbol instead. TypeScript 2+ output, which uses the instance `super()` returns, behaves as before. Output from older compilers, whose constructors keep working on their own `this` until __native, can no longer reach the instance through it. Construction also uses Reflect.construct instead of a bound function. --- test-app/app/src/main/assets/app/mainpage.js | 1 + .../testTypeScriptExtendedConstruction.js | 170 ++++++++++++++++++ .../src/main/assets/internal/ts_helpers.js | 46 +++-- 3 files changed, 191 insertions(+), 26 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/testTypeScriptExtendedConstruction.js diff --git a/test-app/app/src/main/assets/app/mainpage.js b/test-app/app/src/main/assets/app/mainpage.js index c7a2abf70..6a9d28837 100644 --- a/test-app/app/src/main/assets/app/mainpage.js +++ b/test-app/app/src/main/assets/app/mainpage.js @@ -58,6 +58,7 @@ require("./tests/exceptionHandlingTests"); require("./tests/discardedExceptionsTest"); require("./tests/dispatchCallbacksOnUiThreadTests"); require("./tests/stringConversionTests"); +require("./tests/testTypeScriptExtendedConstruction"); require("./tests/testsForTypescript"); require("./tests/testGC"); require("./tests/testWrapperLifetime"); diff --git a/test-app/app/src/main/assets/app/tests/testTypeScriptExtendedConstruction.js b/test-app/app/src/main/assets/app/tests/testTypeScriptExtendedConstruction.js new file mode 100644 index 000000000..527bec193 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/testTypeScriptExtendedConstruction.js @@ -0,0 +1,170 @@ +describe("TypeScript-extended native class construction", function () { + var Holder = com.tns.tests.WrapperHolder; + var Button1 = com.tns.tests.Button1; + + function collectBoth() { + for (var k = 0; k < 2; k++) { + gc(); + java.lang.System.nanoTime(); + for (var i = 0; i < 3; i++) { + java.lang.Runtime.getRuntime().gc(); + java.lang.System.runFinalization(); + } + gc(); + java.lang.System.nanoTime(); + } + } + + afterEach(function () { + Holder.held = null; + }); + + // TypeScript's ES5 output for + // class EchoButton extends com.tns.tests.Button1 { + // label = "L" + value; count = 0; + // constructor(value) { super(value); } + // echo(s) { ... super.echo(s) ... } describe() { ... } static kind = "echo"; + // } + var EchoButton = (function (_super) { + __extends(EchoButton, _super); + function EchoButton(value) { + var _this = _super.call(this, value) || this; + _this.label = "L" + value; + _this.count = 0; + return global.__native(_this); + } + EchoButton.prototype.echo = function (s) { + this.count++; + this.lastEcho = s; + return this.label + ":" + _super.prototype.echo.call(this, s); + }; + EchoButton.prototype.describe = function () { + return this.label + "/" + this.count; + }; + EchoButton.prototype.superToString = function () { + return this.super.toString(); + }; + EchoButton.kind = "echo"; + return EchoButton; + })(com.tns.tests.Button1); + + it("is an instance of its class and of every Java class above it", function () { + var button = new EchoButton(1); + expect(button instanceof EchoButton).toBe(true); + expect(button instanceof Button1).toBe(true); + expect(button instanceof com.tns.tests.ButtonBase).toBe(true); + expect(button instanceof java.lang.Object).toBe(true); + expect(new Button1() instanceof EchoButton).toBe(false); + }); + + it("shares one prototype chain between instances, with the class prototype on it", function () { + var a = new EchoButton(1); + var b = new EchoButton(2); + var proto = Object.getPrototypeOf(a); + expect(Object.getPrototypeOf(b)).toBe(proto); + expect(Object.getPrototypeOf(proto)).toBe(EchoButton.prototype); + expect(Object.getPrototypeOf(EchoButton.prototype)).toBe(Button1.prototype); + expect(a.constructor.prototype).toBe(proto); + expect(a instanceof a.constructor).toBe(true); + }); + + it("keeps the constructor's field initializers as own properties of the instance", function () { + var button = new EchoButton(3); + expect(Object.keys(button).sort()).toEqual(["count", "label"]); + expect(button.label).toBe("L3"); + expect(button.count).toBe(0); + }); + + it("keeps the state of each instance apart", function () { + var a = new EchoButton(1); + var b = new EchoButton(2); + a.extra = "a"; + a.triggerEcho("x"); + expect(b.extra).toBeUndefined(); + expect(a.count).toBe(1); + expect(b.count).toBe(0); + expect(b.describe()).toBe("L2/0"); + }); + + it("runs the JS override when Java calls the method, with the instance as this", function () { + var button = new EchoButton(4); + expect(button.triggerEcho("hi")).toBe("L4:hi"); + expect(button.count).toBe(1); + expect(button.lastEcho).toBe("hi"); + expect(button.describe()).toBe("L4/1"); + }); + + it("calls the Java implementation through super", function () { + var button = new EchoButton(5); + expect(button.echo("s")).toBe("L5:s"); + var label = button.superToString(); + expect(label.indexOf("com.tns.tests.Button1_")).not.toBe(-1); + }); + + it("calls inherited Java methods and passes the instance to Java and back", function () { + var button = new EchoButton(6); + expect(button.getIMAGE_ID_PROP()).toBe("image id prop"); + var list = new java.util.ArrayList(); + list.add(button); + expect(list.get(0)).toBe(button); + }); + + it("keeps the class's static members and inherits the Java ones", function () { + expect(EchoButton.kind).toBe("echo"); + expect(EchoButton.SGetStaticImageId()).toBe("static image id"); + }); + + it("keeps the prototype chain and the expandos of a Java-held instance after a collection", function () { + var proto = (function () { + var button = new EchoButton(7); + button.expando = "kept"; + Holder.held = button; + return Object.getPrototypeOf(button); + })(); + collectBoth(); + + var button = Holder.held; + expect(button.triggerEcho("r")).toBe("L7:r"); + expect(Object.getPrototypeOf(button)).toBe(proto); + expect(button instanceof EchoButton).toBe(true); + expect(button.expando).toBe("kept"); + expect(button.describe()).toBe("L7/1"); + }); + + describe("when the constructor forwards its arguments with apply", function () { + // TypeScript's output for a class without a constructor, and for `super(...args)`. + var DefaultButton = (function (_super) { + __extends(DefaultButton, _super); + function DefaultButton() { + return _super !== null && _super.apply(this, arguments) || this; + } + DefaultButton.prototype.echo = function (s) { + return "default:" + s; + }; + return DefaultButton; + })(com.tns.tests.Button1); + var SpreadButton = (function (_super) { + __extends(SpreadButton, _super); + function SpreadButton(value) { + var _this = _super.apply(this, [value]) || this; + _this.value = value; + return _this; + } + SpreadButton.prototype.echo = function (s) { + return this.value + ":" + s; + }; + return SpreadButton; + })(com.tns.tests.Button1); + + it("constructs the instance with the forwarded arguments", function () { + var plain = new DefaultButton(); + expect(plain instanceof DefaultButton).toBe(true); + expect(plain.triggerEcho("a")).toBe("default:a"); + + var spread = new SpreadButton(3); + expect(spread instanceof SpreadButton).toBe(true); + expect(Object.keys(spread)).toEqual(["value"]); + expect(spread.triggerEcho("b")).toBe("3:b"); + }); + }); +}); diff --git a/test-app/app/src/main/assets/internal/ts_helpers.js b/test-app/app/src/main/assets/internal/ts_helpers.js index 5f187f94c..413956c06 100644 --- a/test-app/app/src/main/assets/internal/ts_helpers.js +++ b/test-app/app/src/main/assets/internal/ts_helpers.js @@ -19,20 +19,15 @@ // 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; + // TypeScript 2+ constructors pass the instance `super()` returned, which this returns as is. + // One compiled earlier sets its fields on its own `this` and passes that instead; this moves + // them to the instance. if (thiz.__container__) { - var result = thiz.__proto__; + var result = thiz[nativeInstanceKey]; for (var prop in thiz) { if (thiz.hasOwnProperty(prop)) { - thiz.__proto__[prop] = thiz[prop]; + result[prop] = thiz[prop]; delete thiz[prop]; } } @@ -47,6 +42,17 @@ } }; + var nativeInstanceKey = Symbol("nativeInstance"); + + // Never make the native instance a prototype, not even of the constructor's own `this`: + // V8 gives a prototype a map of its own, so every store on the instance, such as each + // field initializer, would miss. + function linkContainer(thiz, instance) { + thiz.__container__ = true; + thiz[nativeInstanceKey] = instance; + return instance; + } + var __extends = function (Child, Parent) { var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1); if (!extendNativeClass) { @@ -77,26 +83,14 @@ 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__; + var instance = arguments.length > 1 ? Reflect.construct(Extended, Array.prototype.slice.call(arguments, 1)) : new Extended(); + return linkContainer(thiz, instance); }; 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(Array.prototype.slice.call(args)))); - } - else { - thiz.__proto__ = new Extended(); - } - return thiz.__proto__; + var instance = args && args.length > 0 ? Reflect.construct(Extended, Array.prototype.slice.call(args)) : new Extended(); + return linkContainer(thiz, instance); }; __extends_ns(Child, Parent); Child.__isPrototypeImplementationObject = true;