Skip to content

Commit 99de9a1

Browse files
authored
fix: don't use prebuilt classes that mismatch JS (#2155)
Since #2101, anonymous extends and TypeScript native classes load the class the static binding generator built for their call site as-is, so anything the JS parser missed made a broken class. - The parser resolves interfaces listed through local aliases, destructuring, alias chains, aliased packages and constant arrays, for @Interfaces and extend's interfaces. - Interfaces it cannot name print a warning and get no prebuilt class, and so do anonymous classes with spread or computed members; the runtime generates them. - Classes named after their call site list the parser's members in members__ns. - DexFactory checks an anonymous class's prebuilt class against the interfaces and functions the script defines, and on a mismatch warns and generates the class at runtime under a name of its own.
1 parent 15cd5e5 commit 99de9a1

12 files changed

Lines changed: 849 additions & 39 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
@@ -84,6 +84,7 @@ require("./tests/testTypedArrayArgs");
8484
require("./tests/dex-interface-implementation");
8585
require("./tests/testClassForNameDiscovery");
8686
require("./tests/testPrebuiltExtendClasses");
87+
require("./tests/testPrebuiltInterfaceAliases");
8788
require("./tests/testRuntimeGenerationReport");
8889
require("./tests/testInterfaceExtendClasses");
8990
require("./tests/testAppModuleBindings");
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
describe("Interfaces of prebuilt classes", function () {
2+
var RunnableAlias = java.lang.Runnable;
3+
4+
// With -PuseContentBindings a class the generator saw only part of asks for another name,
5+
// so no prebuilt class answers it and nothing is rejected.
6+
var contentKeyed = (function () {
7+
try {
8+
java.lang.Class.forName("com.tns.gen.ContentKeyedBindings", false, com.tns.Runtime.class.getClassLoader());
9+
return true;
10+
} catch (e) {
11+
return false;
12+
}
13+
}());
14+
15+
function expectRejectedPrebuilt(instance, className, reason) {
16+
// A content-keyed name may also be the name of another class with the same content.
17+
if (contentKeyed) {
18+
return;
19+
}
20+
var name = String(instance.getClass().getName());
21+
expect(runtimeGeneratedEntries(name).length).toBe(1);
22+
expect(name).toMatch(new RegExp("_" + className + "_runtime$"));
23+
var prebuiltName = "com.tns.gen." + name.replace(/_runtime$/, "");
24+
expect(com.tns.tests.LogcatUtil.countJsWarnings(prebuiltName, reason)).toBe(1);
25+
}
26+
27+
function runtimeGeneratedEntries(loadedName) {
28+
var lines = com.tns.DexFactory.getRuntimeGeneratedClasses();
29+
var result = [];
30+
for (var i = 0; i < lines.length; i++) {
31+
var fields = String(lines[i]).split(" ");
32+
if (fields[2] === loadedName) {
33+
result.push({ source: fields[0], loaded: fields[2], requested: fields[3] });
34+
}
35+
}
36+
return result;
37+
}
38+
39+
function runFromJava(instance) {
40+
java.util.concurrent.Executors.callable(instance).call();
41+
}
42+
43+
it("loads a class whose interface is listed through a local alias from the APK", function () {
44+
var runs = 0;
45+
var Aliased = (function (_super) {
46+
__extends(Aliased, _super);
47+
function Aliased() {
48+
var _this = _super.call(this) || this;
49+
return global.__native(_this);
50+
}
51+
Aliased.prototype.run = function () {
52+
runs++;
53+
};
54+
Aliased = __decorate([
55+
Interfaces([RunnableAlias])
56+
], Aliased);
57+
return Aliased;
58+
}(java.lang.Object));
59+
var instance = new Aliased();
60+
61+
expect(java.lang.Runnable.class.isAssignableFrom(instance.getClass())).toBe(true);
62+
runFromJava(instance);
63+
expect(runs).toBe(1);
64+
expect(runtimeGeneratedEntries(instance.getClass().getName())).toEqual([]);
65+
});
66+
67+
it("generates a class whose interface the static binding generator cannot name at runtime", function () {
68+
function runnableType() {
69+
return java.lang.Runnable;
70+
}
71+
var runs = 0;
72+
var Unnamed = (function (_super) {
73+
__extends(Unnamed, _super);
74+
function Unnamed() {
75+
var _this = _super.call(this) || this;
76+
return global.__native(_this);
77+
}
78+
Unnamed.prototype.run = function () {
79+
runs++;
80+
};
81+
Unnamed = __decorate([
82+
Interfaces([runnableType()])
83+
], Unnamed);
84+
return Unnamed;
85+
}(java.lang.Object));
86+
var instance = new Unnamed();
87+
88+
expect(java.lang.Runnable.class.isAssignableFrom(instance.getClass())).toBe(true);
89+
runFromJava(instance);
90+
expect(runs).toBe(1);
91+
expect(runtimeGeneratedEntries(instance.getClass().getName()).length).toBe(1);
92+
});
93+
94+
it("generates a class in place of a prebuilt one built without a method added after the class body, and warns", function () {
95+
var LateMember = (function (_super) {
96+
__extends(LateMember, _super);
97+
function LateMember() {
98+
var _this = _super.call(this) || this;
99+
return global.__native(_this);
100+
}
101+
LateMember.prototype.run = function () {
102+
};
103+
return LateMember;
104+
}(java.lang.Object));
105+
LateMember.prototype.toString = function () {
106+
return "late member";
107+
};
108+
var instance = new LateMember();
109+
110+
expect(String(java.lang.String.valueOf(instance))).toBe("late member");
111+
expectRejectedPrebuilt(instance, "LateMember", "was built without toString");
112+
});
113+
114+
it("generates a class in place of a prebuilt one that lacks a listed interface, and warns", function () {
115+
var runs = 0;
116+
var LateInterface = (function (_super) {
117+
__extends(LateInterface, _super);
118+
function LateInterface() {
119+
var _this = _super.call(this) || this;
120+
return global.__native(_this);
121+
}
122+
LateInterface.prototype.run = function () {
123+
runs++;
124+
};
125+
return LateInterface;
126+
}(java.lang.Object));
127+
// The generator builds the class from the class body, without the interface.
128+
LateInterface.prototype.interfaces = [java.lang.Runnable];
129+
var instance = new LateInterface();
130+
131+
expect(java.lang.Runnable.class.isAssignableFrom(instance.getClass())).toBe(true);
132+
runFromJava(instance);
133+
expect(runs).toBe(1);
134+
135+
// Java creates an instance by the generated class alone.
136+
runFromJava(com.tns.tests.JavaCreated.newInstance(instance.getClass()));
137+
expect(runs).toBe(2);
138+
139+
expectRejectedPrebuilt(instance, "LateInterface", "does not implement java.lang.Runnable");
140+
});
141+
});

‎test-app/build-tools/jsparser/tests/cases/content_members/app/members.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ var LiteralMembers = java.lang.Object.extend("LiteralMembers", {
99
dataMember: 1,
1010
get getterMember() { return 1; },
1111
set setterMember(value) {},
12-
[computedName]: function () {},
13-
...other,
1412
42: function () {},
1513
__proto__: null,
1614
identifierMember: function () {},
1715
interfaces: [java.lang.Runnable],
1816
run: function () {}
1917
});
2018

19+
var UnnamedMembers = java.lang.Object.extend("UnnamedMembers", {
20+
[computedName]: function () {},
21+
...other,
22+
run: function () {}
23+
});
24+
2125
var AccessorMembers = java.lang.Object.extend("AccessorMembers", {
2226
get getOnly() { return 1; },
2327
set setOnly(value) {},
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var __extends = (this && this.__extends) || function (d, b) {
2+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3+
function __() { this.constructor = d; }
4+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
5+
};
6+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
7+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9+
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;
10+
return c > 3 && r && Object.defineProperty(target, key, r), r;
11+
};
12+
const Observer = java.util.Observer;
13+
var util = java.util;
14+
var Formattable = util.Formattable;
15+
const Runnable = java.lang.Runnable;
16+
var _imported__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
17+
18+
var AliasedObject = (function (_super) {
19+
__extends(AliasedObject, _super);
20+
function AliasedObject() {
21+
var _this = _super.call(this) || this;
22+
return global.__native(_this);
23+
}
24+
AliasedObject_1 = AliasedObject;
25+
AliasedObject.prototype.update = function () {
26+
};
27+
AliasedObject.prototype.formatTo = function () {
28+
};
29+
var AliasedObject_1;
30+
AliasedObject = AliasedObject_1 = __decorate([
31+
Interfaces([Observer, Formattable])
32+
], AliasedObject);
33+
return AliasedObject;
34+
}(java.lang.Object));
35+
36+
var ImportedInterfaceObject = (function (_super) {
37+
__extends(ImportedInterfaceObject, _super);
38+
function ImportedInterfaceObject() {
39+
var _this = _super.call(this) || this;
40+
return global.__native(_this);
41+
}
42+
ImportedInterfaceObject.prototype.update = function () {
43+
};
44+
ImportedInterfaceObject = __decorate([
45+
Interfaces([_imported__WEBPACK_IMPORTED_MODULE_0__.Observer])
46+
], ImportedInterfaceObject);
47+
return ImportedInterfaceObject;
48+
}(java.lang.Object));
49+
50+
var AliasedExtend = java.lang.Object.extend({
51+
run: function () {
52+
},
53+
interfaces: [Runnable]
54+
});
55+
56+
var NamedImportedInterfaceObject = (function (_super) {
57+
__extends(NamedImportedInterfaceObject, _super);
58+
function NamedImportedInterfaceObject() {
59+
var _this = _super.call(this) || this;
60+
return global.__native(_this);
61+
}
62+
NamedImportedInterfaceObject.prototype.update = function () {
63+
};
64+
NamedImportedInterfaceObject = __decorate([
65+
JavaProxy("org.nativescript.aliases.NamedImported"),
66+
Interfaces([_imported__WEBPACK_IMPORTED_MODULE_0__.Observer])
67+
], NamedImportedInterfaceObject);
68+
return NamedImportedInterfaceObject;
69+
}(java.lang.Object));
70+
71+
var NamedCalledExtend = java.lang.Object.extend("org.nativescript.aliases.NamedCalled", {
72+
run: function () {
73+
},
74+
interfaces: getInterfaces()
75+
});
76+
77+
var mixin = { hashCode: function () { return 1; } };
78+
var SpreadExtend = java.lang.Object.extend({
79+
...mixin,
80+
toString: function () {
81+
}
82+
});
83+
84+
var memberName = "equals";
85+
var ComputedExtend = java.lang.Object.extend({
86+
[memberName]: function () {
87+
},
88+
finalize: function () {
89+
}
90+
});
91+
92+
var ComputedMember = (function (_super) {
93+
__extends(ComputedMember, _super);
94+
function ComputedMember() {
95+
var _this = _super.call(this) || this;
96+
return global.__native(_this);
97+
}
98+
ComputedMember.prototype.update = function () {
99+
};
100+
ComputedMember.prototype[memberName] = function () {
101+
};
102+
return ComputedMember;
103+
}(java.lang.Object));
104+
105+
const { Closeable } = java.io;
106+
const { Observer: ObserverMember } = java.util;
107+
const { Formattable: DefaultedFormattable = null } = java.util;
108+
109+
var DestructuredObject = (function (_super) {
110+
__extends(DestructuredObject, _super);
111+
function DestructuredObject() {
112+
var _this = _super.call(this) || this;
113+
return global.__native(_this);
114+
}
115+
DestructuredObject.prototype.close = function () {
116+
};
117+
DestructuredObject = __decorate([
118+
Interfaces([Closeable, ObserverMember])
119+
], DestructuredObject);
120+
return DestructuredObject;
121+
}(java.lang.Object));
122+
123+
var DefaultedDestructuredObject = (function (_super) {
124+
__extends(DefaultedDestructuredObject, _super);
125+
function DefaultedDestructuredObject() {
126+
var _this = _super.call(this) || this;
127+
return global.__native(_this);
128+
}
129+
DefaultedDestructuredObject.prototype.formatTo = function () {
130+
};
131+
DefaultedDestructuredObject = __decorate([
132+
Interfaces([DefaultedFormattable])
133+
], DefaultedDestructuredObject);
134+
return DefaultedDestructuredObject;
135+
}(java.lang.Object));

0 commit comments

Comments
 (0)