Skip to content

Commit 50cb2a2

Browse files
committed
fix: jsparser fixes and support for primjs in sbg
1 parent cf6502e commit 50cb2a2

File tree

11 files changed

+474
-112
lines changed

11 files changed

+474
-112
lines changed

test-app/app/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ task runSbg(type: BuildToolTask) {
516516
paramz.add("-show-deprecation-warnings")
517517
}
518518

519+
if (ns_engine == "PRIMJS") {
520+
paramz.add("-line-column-primjs")
521+
}
522+
519523
setOutputs outLogger
520524

521525
args paramz

test-app/build-tools/buildMetadata.log

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ Added Properties 54
22
Ignored Properties 2 duplicates.
33
Added Methods 81320
44
Ignored Methods 3300 duplicates.
5-
Added Fields 54583
6-
Ignored Fields 16676 duplicates.
5+
Added Fields 54602
6+
Ignored Fields 16678 duplicates.

test-app/build-tools/jsparser/js_parser.js

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const showErrorsAndWarnings =
1717
process.argv &&
1818
process.argv.includes("enableErrorLogging"));
1919

20+
const lineColumnPrimJS = process.argv.includes("-line-column-primjs");
21+
2022
const loggingSettings = {
2123
logPath: require("path").join(__dirname, "logs", "i.txt"),
2224
strategy: "console",
@@ -279,6 +281,7 @@ const visitAst = function (path, data, err) {
279281
.substring(inputDir.length + 1)
280282
.replace(/[\\]/g, "/"),
281283
interfaceNames: interfaceNames,
284+
isPrimJS: lineColumnPrimJS
282285
};
283286
es5_visitors.es5Visitor(path, decoratorConfig);
284287
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Bundle = android.os.Bundle;
2+
3+
@JavaProxy('org.nativescript.MyCustomActivity')
4+
export class TestActivity extends android.app.Activity {
5+
static readonly TEST1: string = "my_test";
6+
public onCreate(savedInstanceState?: Bundle): void {
7+
super.onCreate(savedInstanceState);
8+
console.log(TestActivity.TEST1);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
var __swc_crazy_long_line_swc_inherit_polyfill = {
3+
_: () => {}
4+
}
5+
var _Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__ = {
6+
_: () => {}
7+
}
8+
9+
var _swc_helpers_possible_constructor_return__WEBPACK_IMPORTED_MODULE_4__ = {
10+
_: () => {}
11+
}
12+
13+
var _swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__ = {
14+
_: () => {}
15+
}
16+
17+
var _swc_helpers_create_class__WEBPACK_IMPORTED_MODULE_5__ = {
18+
_: () => {}
19+
}
20+
21+
var _swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__ = {
22+
__decorate: () => {}
23+
}
24+
25+
var _swc_helpers_define_property__WEBPACK_IMPORTED_MODULE_9__ = {
26+
_: () => {}
27+
}
28+
var _swc_helpers_get__WEBPACK_IMPORTED_MODULE_7__ = {
29+
_: () => {}
30+
}
31+
32+
var _swc_helpers_get_prototype_of__WEBPACK_IMPORTED_MODULE_8__ = {
33+
_: () => {}
34+
}
35+
36+
var _android_app_Activity;
37+
var TestActivity = /*#__PURE__*/ function(_superClass) {
38+
"use strict";
39+
(0,__swc_crazy_long_line_swc_inherit_polyfill._)(TestActivity, _superClass);
40+
function TestActivity() {
41+
(0,_swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__._)(this, TestActivity);
42+
return (0,_Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__._)(this, TestActivity, arguments);
43+
}
44+
(0,_swc_helpers_create_class__WEBPACK_IMPORTED_MODULE_5__._)(TestActivity, [
45+
{
46+
key: "onCreate",
47+
value: function onCreate(savedInstanceState) {
48+
(0,_swc_helpers_get__WEBPACK_IMPORTED_MODULE_7__._)((0,_swc_helpers_get_prototype_of__WEBPACK_IMPORTED_MODULE_8__._)(TestActivity.prototype), "onCreate", this).call(this, savedInstanceState);
49+
console.log(TestActivity.TEST1);
50+
}
51+
}
52+
]);
53+
return TestActivity;
54+
}(_android_app_Activity = android.app.Activity);
55+
(0,_swc_helpers_define_property__WEBPACK_IMPORTED_MODULE_9__._)(TestActivity, "TEST1", "my_test");
56+
TestActivity = (0,_swc_helpers_ts_decorate__WEBPACK_IMPORTED_MODULE_6__.__decorate)([
57+
JavaProxy('org.nativescript.MyCustomActivity')
58+
], TestActivity);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
(function () {
2+
3+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
4+
var c = arguments.length;
5+
var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
6+
7+
if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") {
8+
r = global.Reflect.decorate(decorators, target, key, desc);
9+
}
10+
else {
11+
for (var i = decorators.length - 1; i >= 0; i--) {
12+
if (d = decorators[i]) {
13+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
14+
}
15+
}
16+
}
17+
return c > 3 && r && Object.defineProperty(target, key, r), r;
18+
};
19+
20+
// For backward compatibility.
21+
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;
30+
if (thiz.__container__) {
31+
var result = thiz.__proto__;
32+
33+
for (var prop in thiz) {
34+
if (thiz.hasOwnProperty(prop)) {
35+
thiz.__proto__[prop] = thiz[prop];
36+
delete thiz[prop];
37+
}
38+
}
39+
40+
thiz.constructor = undefined;
41+
thiz.__proto__ = undefined;
42+
Object.freeze(thiz);
43+
Object.preventExtensions(thiz);
44+
return result;
45+
} else {
46+
return thiz;
47+
}
48+
};
49+
50+
var __extends = function (Child, Parent) {
51+
var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1);
52+
if (!extendNativeClass) {
53+
__extends_ts(Child, Parent);
54+
return;
55+
}
56+
if (Parent.__isPrototypeImplementationObject) {
57+
throw new Error("Can not extend an already extended native object.");
58+
}
59+
60+
function extend(thiz) {
61+
var child = thiz.__proto__.__child;
62+
if (!child.__extended) {
63+
var parent = thiz.__proto__.__parent;
64+
child.__extended = parent.extend(child.name, child.prototype, true);
65+
// This will deal with "i instanceof child"
66+
child[Symbol.hasInstance] = function (instance) {
67+
return instance instanceof this.__extended;
68+
}
69+
}
70+
return child.__extended;
71+
};
72+
73+
Parent.__activityExtend = function (parent, name, implementationObject) {
74+
__log("__activityExtend called");
75+
return parent.extend(name, implementationObject);
76+
};
77+
78+
Parent.call = function (thiz) {
79+
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__;
88+
};
89+
90+
Parent.apply = function (thiz, args) {
91+
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(args)));
95+
}
96+
else {
97+
thiz.__proto__ = new Extended();
98+
}
99+
return thiz.__proto__;
100+
};
101+
__extends_ns(Child, Parent);
102+
Child.__isPrototypeImplementationObject = true;
103+
Child.__proto__ = Parent;
104+
Child.prototype.__parent = Parent;
105+
Child.prototype.__child = Child;
106+
}
107+
108+
var __extends_ts = function (child, parent) {
109+
extendStaticFunctions(child, parent);
110+
assignPrototypeFromParentToChild(parent, child);
111+
};
112+
113+
var __extends_ns = function (child, parent) {
114+
if (!parent.extend) {
115+
assignPropertiesFromParentToChild(parent, child);
116+
}
117+
118+
assignPrototypeFromParentToChild(parent, child);
119+
};
120+
121+
var extendStaticFunctions =
122+
Object.setPrototypeOf
123+
|| (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; })
124+
|| assignPropertiesFromParentToChild;
125+
126+
function hasInternalProtoProperty() {
127+
return { __proto__: [] } instanceof Array;
128+
}
129+
130+
function assignPropertiesFromParentToChild(parent, child) {
131+
for (var property in parent) {
132+
if (parent.hasOwnProperty(property)) {
133+
child[property] = parent[property];
134+
}
135+
}
136+
}
137+
138+
function assignPrototypeFromParentToChild(parent, child) {
139+
function __() {
140+
this.constructor = child;
141+
}
142+
143+
if (parent === null) {
144+
child.prototype = Object.create(null);
145+
} else {
146+
__.prototype = parent.prototype;
147+
child.prototype = new __();
148+
}
149+
}
150+
151+
152+
function JavaProxy(className) {
153+
return function (target) {
154+
var extended = target.extend(className, target.prototype)
155+
extended.name = className;
156+
return extended;
157+
};
158+
}
159+
160+
function Interfaces(interfacesArr) {
161+
return function (target) {
162+
if (interfacesArr instanceof Array) {
163+
// attach interfaces: [] to the object
164+
target.prototype.interfaces = interfacesArr;
165+
}
166+
}
167+
}
168+
169+
Object.defineProperty(global, "__native", { value: __native });
170+
Object.defineProperty(global, "__extends", { value: __extends });
171+
Object.defineProperty(global, "__decorate", { value: __decorate });
172+
173+
global.JavaProxy = JavaProxy;
174+
global.Interfaces = Interfaces;
175+
})()

test-app/build-tools/jsparser/tests/cases/extends_with_interfaces_ts_swc/app/normal_ts_extends.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ var ClickListener = /*#__PURE__*/ function(_java_lang_Object) {
2727
function ClickListener() {
2828
(0,_swc_helpers_class_call_check__WEBPACK_IMPORTED_MODULE_2__._)(this, ClickListener);
2929
var _this;
30-
_this = (0,_Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__._)(this, ClickListener);
30+
_this = (0,_Users_ammarahmed_Downloads_ns_vue_rspack_nativescript_rspack_dist_helpers_swc_call_super_polyfill_js__WEBPACK_IMPORTED_MODULE_0__._)(this,
31+
ClickListener);
3132
// necessary when extending TypeScript constructors
3233
return (0,_swc_helpers_possible_constructor_return__WEBPACK_IMPORTED_MODULE_4__._)(_this, global.__native(_this));
3334
}

test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ function execGradle(inputPath, generatedJavaClassesRoot, callback) {
1515
}
1616

1717
function logExecResult(stdout, stderr) {
18-
// console.log(`stdout: ${stdout}`);
1918
if (stderr) {
2019
console.log(`stderr: ${stderr}`);
2120
}
@@ -174,7 +173,6 @@ describe("parser/js_parser tests", function () {
174173
clearOutput();
175174

176175
execGradle(input, generatedJavaClassesRoot, function (error, stdout, stderr) {
177-
console.log(error, stdout, stderr);
178176
if (error) {
179177
console.error(`exec error: ${error}`);
180178
return done.fail(error);
@@ -214,7 +212,6 @@ describe("parser/js_parser tests", function () {
214212
clearOutput();
215213

216214
execGradle(input, generatedJavaClassesRoot, function (error, stdout, stderr) {
217-
console.log(stdout);
218215
if (error) {
219216
console.error(`exec error: ${error}`);
220217
return done.fail(error);
@@ -251,7 +248,6 @@ describe("parser/js_parser tests", function () {
251248
clearOutput();
252249

253250
execGradle(input, generatedJavaClassesRoot, function (error, stdout, stderr) {
254-
console.log(stdout);
255251
if (error) {
256252
console.error(`exec error: ${error}`);
257253
return done.fail(error);
@@ -264,7 +260,6 @@ describe("parser/js_parser tests", function () {
264260
let bindings = bindingsContent[0].split('*');
265261
let implInterfacesStr = bindings[bindings.length - 1];
266262

267-
console.log(bindings)
268263

269264
expect(implInterfacesStr).toBeDefined();
270265
expect(implInterfacesStr.length).toBeGreaterThan(0);
@@ -290,7 +285,6 @@ describe("parser/js_parser tests", function () {
290285
clearOutput();
291286

292287
execGradle(input, generatedJavaClassesRoot, function (error, stdout, stderr) {
293-
console.log(stdout);
294288
if (error) {
295289
console.error(`exec error: ${error}`);
296290
return done.fail(error);
@@ -303,7 +297,6 @@ describe("parser/js_parser tests", function () {
303297
let bindings = bindingsContent[0].split('*');
304298
let implInterfacesStr = bindings[bindings.length - 1];
305299

306-
console.log(bindings)
307300

308301
expect(implInterfacesStr).toBeDefined();
309302
expect(implInterfacesStr.length).toBeGreaterThan(0);
@@ -382,6 +375,44 @@ describe("parser/js_parser tests", function () {
382375
expect(bindingsContent.length).toBe(1);
383376

384377

378+
379+
for (let line of bindingsContent) {
380+
var lineParts = line.split("*");
381+
var tsExtendsPart = lineParts[1];
382+
expect(tsExtendsPart).toBeFalsy();
383+
384+
var newClassNamePart = lineParts[6];
385+
expect(newClassNames).toContain(newClassNamePart);
386+
}
387+
388+
done();
389+
});
390+
});
391+
392+
it("SWC: Generated metadata for bindings should return proper JavaClass name when there is a static property and double assignment", function (done) {
393+
let input = path.normalize(path.join(prefix, "decorated_double_assignment_ts_swc", "app")),
394+
generatedJavaClassesRoot = path.normalize(path.join(prefix, "decorated_double_assignment_ts_swc", "src", "main", "java"));
395+
396+
const newClassNames = [
397+
"org.nativescript.MyCustomActivity",
398+
];
399+
400+
clearOutput();
401+
402+
execGradle(input, generatedJavaClassesRoot, function (error, stdout, stderr) {
403+
if (error) {
404+
console.error(`exec error: ${error}`);
405+
return done.fail(error);
406+
}
407+
408+
logExecResult(stdout, stderr)
409+
410+
let bindingsContent = fs.readFileSync(sbgBindingOutoutFile, "utf-8").toString().trim().split('\n');
411+
412+
expect(bindingsContent.length).toBe(1);
413+
414+
415+
385416
for (let line of bindingsContent) {
386417
var lineParts = line.split("*");
387418
var tsExtendsPart = lineParts[1];

0 commit comments

Comments
 (0)