Skip to content

Commit 670dd1a

Browse files
committed
Refactor th_helpers.js and fix 'extend' method handling
1 parent 8ebffcb commit 670dd1a

File tree

10 files changed

+1394
-857
lines changed

10 files changed

+1394
-857
lines changed

test-app/app/src/main/assets/app/tests/extendedClassesTests.js

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe("Tests extended classes ", function () {
22

3-
it("Instance_with_no_extension_shouldnt_use_previously_defined_implementation_object", function () {
3+
it("Instance with no extension shouldn't use previously defined implementation object", function () {
44
var MyButton = com.tns.tests.Button1.extend({
55
toString: function () {
66
return "overriden toString method of chronometer instance";
@@ -21,8 +21,56 @@ describe("Tests extended classes ", function () {
2121
expect(labelToString).not.toBe(labelToString1);
2222
expect(labelgetIMAGE_ID_PROP).not.toBe(labelgetIMAGE_ID_PROP1);
2323
});
24+
25+
it("Having a class with static method named 'extend' and overriding it in a child class shouldn't crash the app", function (){
26+
27+
/* JS below the comment is generated from the following TS code
28+
29+
class Base{
30+
static extend(){
31+
return "expectedValue";
32+
}
33+
}
34+
35+
class Child extends Base{}
36+
37+
const superProto = Object.getPrototypeOf(Child.prototype)
38+
const Super = superProto.constructor;
39+
Super.extend();
40+
41+
var child = Object.create(Child);
42+
child.extend();
43+
Child.extend();
44+
45+
*/
46+
47+
var Base = /** @class */ (function () {
48+
function Base() {
49+
}
50+
Base.extend = function () {
51+
return "expectedValue";
52+
};
53+
return Base;
54+
}());
55+
var Child = /** @class */ (function (_super) {
56+
__extends(Child, _super);
57+
function Child() {
58+
return _super !== null && _super.apply(this, arguments) || this;
59+
}
60+
return Child;
61+
}(Base));
62+
63+
var superProto = Object.getPrototypeOf(Child.prototype);
64+
var Super = superProto.constructor;
65+
expect(Super.extend()).toBe("expectedValue");
66+
67+
var child = Object.create(Child);
68+
expect(child.extend()).toBe("expectedValue");
69+
70+
expect(Child.extend()).toBe("expectedValue");
71+
});
2472

25-
it("Instance_with_extension_shouldnt_use_previously_defined_implementation_object", function () {
73+
it("Instance with extension shouldn't use previously defined implementation object", function () {
2674

2775
var MyButton = com.tns.tests.Button1.extend({
2876
toString: function () {
@@ -53,7 +101,7 @@ describe("Tests extended classes ", function () {
53101
expect(labelgetIMAGE_ID_PROP).not.toBe(labelgetIMAGE_ID_PROP1);
54102
});
55103

56-
it("Newly_created_instances_should_behave_the_same_and_not_use_previously_defined_implementation_objects", function () {
104+
it("Newly created instances should behave the same and not use previously defined implementation objects", function () {
57105

58106
var button1 = new com.tns.tests.Button1();
59107
var labelgetIMAGE_ID_PROP1 = button1.getIMAGE_ID_PROP();
@@ -74,7 +122,7 @@ describe("Tests extended classes ", function () {
74122
expect(labelgetIMAGE_ID_PROP1).toBe(labelgetIMAGE_ID_PROP2);
75123
});
76124

77-
it("should not crash with no exception when incorrectly calling extended class constructor", function () {
125+
it("Should not crash with no exception when incorrectly calling extended class constructor", function () {
78126
let MyObj = java.lang.Object.extend({
79127
toString: () => { return "It's MyObj" }
80128
});

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

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
11
(function() {
2-
var __extends_ns = function (d, b) {
3-
if (!b.extend) {
4-
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
5-
}
6-
7-
function __() { this.constructor = d; }
8-
__.prototype = b.prototype;
9-
d.prototype = new __();
10-
};
11-
12-
var extendStatics = Object.setPrototypeOf ||
13-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
14-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
15-
16-
var __extends_ts = function (d, b) {
17-
extendStatics(d, b);
18-
function __() { this.constructor = d; }
19-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20-
};
21-
222

233
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
244
var c = arguments.length;
@@ -70,7 +50,8 @@
7050
};
7151

7252
var __extends = function(Child, Parent) {
73-
var extendNativeClass = !!Parent.extend && /native/.test(Parent.extend.toString());
53+
const NATIVE_CODE_REGEX = /\{\s*\[native code\]\s*\}/g;
54+
var extendNativeClass = !!Parent.extend && NATIVE_CODE_REGEX.test(Parent.extend.toString());
7455
if (!extendNativeClass) {
7556
__extends_ts(Child, Parent);
7657
return;
@@ -131,6 +112,50 @@
131112
Child.prototype.__child = Child;
132113
}
133114

115+
var __extends_ts = function (child, parent) {
116+
extendStaticFunctions(child, parent);
117+
assignPrototypeFromParentToChild(parent, child);
118+
};
119+
120+
var __extends_ns = function (child, parent) {
121+
if (!parent.extend) {
122+
assignPropertiesFromParentToChild(parent, child);
123+
}
124+
125+
assignPrototypeFromParentToChild(parent, child);
126+
};
127+
128+
var extendStaticFunctions =
129+
Object.setPrototypeOf
130+
|| (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; })
131+
|| assignPropertiesFromParentToChild;
132+
133+
function hasInternalProtoProperty(){
134+
return { __proto__: [] } instanceof Array;
135+
}
136+
137+
function assignPropertiesFromParentToChild(parent, child){
138+
for (var property in parent){
139+
if (parent.hasOwnProperty(property)) {
140+
child[property] = parent[property];
141+
}
142+
}
143+
}
144+
145+
function assignPrototypeFromParentToChild(parent, child){
146+
function __() {
147+
this.constructor = child;
148+
}
149+
150+
if(parent === null){
151+
child.prototype = Object.create(null);
152+
} else {
153+
__.prototype = parent.prototype;
154+
child.prototype = new __();
155+
}
156+
}
157+
158+
134159
function JavaProxy(className) {
135160
return function (target) {
136161
var extended = target.extend(className, target.prototype)

0 commit comments

Comments
 (0)