Skip to content

Commit f8cd9ee

Browse files
committed
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.
1 parent d8abff2 commit f8cd9ee

3 files changed

Lines changed: 191 additions & 26 deletions

File tree

‎test-app/app/src/main/assets/app/mainpage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ require("./tests/exceptionHandlingTests");
5858
require("./tests/discardedExceptionsTest");
5959
require("./tests/dispatchCallbacksOnUiThreadTests");
6060
require("./tests/stringConversionTests");
61+
require("./tests/testTypeScriptExtendedConstruction");
6162
require("./tests/testsForTypescript");
6263
require("./tests/testGC");
6364
require("./tests/testWrapperLifetime");
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
describe("TypeScript-extended native class construction", function () {
2+
var Holder = com.tns.tests.WrapperHolder;
3+
var Button1 = com.tns.tests.Button1;
4+
5+
function collectBoth() {
6+
for (var k = 0; k < 2; k++) {
7+
gc();
8+
java.lang.System.nanoTime();
9+
for (var i = 0; i < 3; i++) {
10+
java.lang.Runtime.getRuntime().gc();
11+
java.lang.System.runFinalization();
12+
}
13+
gc();
14+
java.lang.System.nanoTime();
15+
}
16+
}
17+
18+
afterEach(function () {
19+
Holder.held = null;
20+
});
21+
22+
// TypeScript's ES5 output for
23+
// class EchoButton extends com.tns.tests.Button1 {
24+
// label = "L" + value; count = 0;
25+
// constructor(value) { super(value); }
26+
// echo(s) { ... super.echo(s) ... } describe() { ... } static kind = "echo";
27+
// }
28+
var EchoButton = (function (_super) {
29+
__extends(EchoButton, _super);
30+
function EchoButton(value) {
31+
var _this = _super.call(this, value) || this;
32+
_this.label = "L" + value;
33+
_this.count = 0;
34+
return global.__native(_this);
35+
}
36+
EchoButton.prototype.echo = function (s) {
37+
this.count++;
38+
this.lastEcho = s;
39+
return this.label + ":" + _super.prototype.echo.call(this, s);
40+
};
41+
EchoButton.prototype.describe = function () {
42+
return this.label + "/" + this.count;
43+
};
44+
EchoButton.prototype.superToString = function () {
45+
return this.super.toString();
46+
};
47+
EchoButton.kind = "echo";
48+
return EchoButton;
49+
})(com.tns.tests.Button1);
50+
51+
it("is an instance of its class and of every Java class above it", function () {
52+
var button = new EchoButton(1);
53+
expect(button instanceof EchoButton).toBe(true);
54+
expect(button instanceof Button1).toBe(true);
55+
expect(button instanceof com.tns.tests.ButtonBase).toBe(true);
56+
expect(button instanceof java.lang.Object).toBe(true);
57+
expect(new Button1() instanceof EchoButton).toBe(false);
58+
});
59+
60+
it("shares one prototype chain between instances, with the class prototype on it", function () {
61+
var a = new EchoButton(1);
62+
var b = new EchoButton(2);
63+
var proto = Object.getPrototypeOf(a);
64+
expect(Object.getPrototypeOf(b)).toBe(proto);
65+
expect(Object.getPrototypeOf(proto)).toBe(EchoButton.prototype);
66+
expect(Object.getPrototypeOf(EchoButton.prototype)).toBe(Button1.prototype);
67+
expect(a.constructor.prototype).toBe(proto);
68+
expect(a instanceof a.constructor).toBe(true);
69+
});
70+
71+
it("keeps the constructor's field initializers as own properties of the instance", function () {
72+
var button = new EchoButton(3);
73+
expect(Object.keys(button).sort()).toEqual(["count", "label"]);
74+
expect(button.label).toBe("L3");
75+
expect(button.count).toBe(0);
76+
});
77+
78+
it("keeps the state of each instance apart", function () {
79+
var a = new EchoButton(1);
80+
var b = new EchoButton(2);
81+
a.extra = "a";
82+
a.triggerEcho("x");
83+
expect(b.extra).toBeUndefined();
84+
expect(a.count).toBe(1);
85+
expect(b.count).toBe(0);
86+
expect(b.describe()).toBe("L2/0");
87+
});
88+
89+
it("runs the JS override when Java calls the method, with the instance as this", function () {
90+
var button = new EchoButton(4);
91+
expect(button.triggerEcho("hi")).toBe("L4:hi");
92+
expect(button.count).toBe(1);
93+
expect(button.lastEcho).toBe("hi");
94+
expect(button.describe()).toBe("L4/1");
95+
});
96+
97+
it("calls the Java implementation through super", function () {
98+
var button = new EchoButton(5);
99+
expect(button.echo("s")).toBe("L5:s");
100+
var label = button.superToString();
101+
expect(label.indexOf("com.tns.tests.Button1_")).not.toBe(-1);
102+
});
103+
104+
it("calls inherited Java methods and passes the instance to Java and back", function () {
105+
var button = new EchoButton(6);
106+
expect(button.getIMAGE_ID_PROP()).toBe("image id prop");
107+
var list = new java.util.ArrayList();
108+
list.add(button);
109+
expect(list.get(0)).toBe(button);
110+
});
111+
112+
it("keeps the class's static members and inherits the Java ones", function () {
113+
expect(EchoButton.kind).toBe("echo");
114+
expect(EchoButton.SGetStaticImageId()).toBe("static image id");
115+
});
116+
117+
it("keeps the prototype chain and the expandos of a Java-held instance after a collection", function () {
118+
var proto = (function () {
119+
var button = new EchoButton(7);
120+
button.expando = "kept";
121+
Holder.held = button;
122+
return Object.getPrototypeOf(button);
123+
})();
124+
collectBoth();
125+
126+
var button = Holder.held;
127+
expect(button.triggerEcho("r")).toBe("L7:r");
128+
expect(Object.getPrototypeOf(button)).toBe(proto);
129+
expect(button instanceof EchoButton).toBe(true);
130+
expect(button.expando).toBe("kept");
131+
expect(button.describe()).toBe("L7/1");
132+
});
133+
134+
describe("when the constructor forwards its arguments with apply", function () {
135+
// TypeScript's output for a class without a constructor, and for `super(...args)`.
136+
var DefaultButton = (function (_super) {
137+
__extends(DefaultButton, _super);
138+
function DefaultButton() {
139+
return _super !== null && _super.apply(this, arguments) || this;
140+
}
141+
DefaultButton.prototype.echo = function (s) {
142+
return "default:" + s;
143+
};
144+
return DefaultButton;
145+
})(com.tns.tests.Button1);
146+
var SpreadButton = (function (_super) {
147+
__extends(SpreadButton, _super);
148+
function SpreadButton(value) {
149+
var _this = _super.apply(this, [value]) || this;
150+
_this.value = value;
151+
return _this;
152+
}
153+
SpreadButton.prototype.echo = function (s) {
154+
return this.value + ":" + s;
155+
};
156+
return SpreadButton;
157+
})(com.tns.tests.Button1);
158+
159+
it("constructs the instance with the forwarded arguments", function () {
160+
var plain = new DefaultButton();
161+
expect(plain instanceof DefaultButton).toBe(true);
162+
expect(plain.triggerEcho("a")).toBe("default:a");
163+
164+
var spread = new SpreadButton(3);
165+
expect(spread instanceof SpreadButton).toBe(true);
166+
expect(Object.keys(spread)).toEqual(["value"]);
167+
expect(spread.triggerEcho("b")).toBe("3:b");
168+
});
169+
});
170+
});

‎test-app/app/src/main/assets/internal/ts_helpers.js‎

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@
1919

2020
// For backward compatibility.
2121
var __native = function (thiz) {
22-
// we are setting the __container__ property to the base class when the super method is called
23-
// if the constructor returns the __native(this) call we will use the old implementation
24-
// copying all the properties to the result
25-
// otherwise if we are using the result from the super() method call we won't need such logic
26-
// as thiz already contains the parent properties
27-
// this way we now support both implementations in typescript generated constructors:
28-
// 1: super(); return __native(this);
29-
// 2: return super() || this;
22+
// TypeScript 2+ constructors pass the instance `super()` returned, which this returns as is.
23+
// One compiled earlier sets its fields on its own `this` and passes that instead; this moves
24+
// them to the instance.
3025
if (thiz.__container__) {
31-
var result = thiz.__proto__;
26+
var result = thiz[nativeInstanceKey];
3227

3328
for (var prop in thiz) {
3429
if (thiz.hasOwnProperty(prop)) {
35-
thiz.__proto__[prop] = thiz[prop];
30+
result[prop] = thiz[prop];
3631
delete thiz[prop];
3732
}
3833
}
@@ -47,6 +42,17 @@
4742
}
4843
};
4944

45+
var nativeInstanceKey = Symbol("nativeInstance");
46+
47+
// Never make the native instance a prototype, not even of the constructor's own `this`:
48+
// V8 gives a prototype a map of its own, so every store on the instance, such as each
49+
// field initializer, would miss.
50+
function linkContainer(thiz, instance) {
51+
thiz.__container__ = true;
52+
thiz[nativeInstanceKey] = instance;
53+
return instance;
54+
}
55+
5056
var __extends = function (Child, Parent) {
5157
var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1);
5258
if (!extendNativeClass) {
@@ -77,26 +83,14 @@
7783

7884
Parent.call = function (thiz) {
7985
var Extended = extend(thiz);
80-
thiz.__container__ = true;
81-
if (arguments.length > 1) {
82-
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
83-
}
84-
else {
85-
thiz.__proto__ = new Extended()
86-
}
87-
return thiz.__proto__;
86+
var instance = arguments.length > 1 ? Reflect.construct(Extended, Array.prototype.slice.call(arguments, 1)) : new Extended();
87+
return linkContainer(thiz, instance);
8888
};
8989

9090
Parent.apply = function (thiz, args) {
9191
var Extended = extend(thiz);
92-
thiz.__container__ = true;
93-
if (args && args.length > 0) {
94-
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(args))));
95-
}
96-
else {
97-
thiz.__proto__ = new Extended();
98-
}
99-
return thiz.__proto__;
92+
var instance = args && args.length > 0 ? Reflect.construct(Extended, Array.prototype.slice.call(args)) : new Extended();
93+
return linkContainer(thiz, instance);
10094
};
10195
__extends_ns(Child, Parent);
10296
Child.__isPrototypeImplementationObject = true;

0 commit comments

Comments
 (0)