Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Bundle = android.os.Bundle;

@JavaProxy('org.nativescript.MyCustomActivity')
export class TestActivity extends android.app.Activity {
static readonly TEST1: string = "my_test";
public onCreate(savedInstanceState?: Bundle): void {
super.onCreate(savedInstanceState);
console.log(TestActivity.TEST1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var TestActivity = (function (_super) {
__extends(TestActivity, _super);
function TestActivity() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestActivity_1 = TestActivity;
TestActivity.prototype.onCreate = function (savedInstanceState) {
_super.prototype.onCreate.call(this, savedInstanceState);
console.log(TestActivity_1.TEST1);
};
var TestActivity_1;
TestActivity.TEST1 = "my_test";
TestActivity = TestActivity_1 = __decorate([
JavaProxy('org.nativescript.MyCustomActivity')
], TestActivity);
return TestActivity;
}(android.app.Activity));
exports.TestActivity = TestActivity;
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
(function() {
var __extends_ts = function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length;
var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;

if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") {
r = global.Reflect.decorate(decorators, target, key, desc);
}
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;
}
}
}
return c > 3 && r && Object.defineProperty(target, key, r), r;
};

var __native = function(thiz) {
var result = thiz.__proto__;

for (var prop in thiz)
{
if (thiz.hasOwnProperty(prop))
{
thiz.__proto__[prop] = thiz[prop];
delete thiz[prop];
}
}

thiz.constructor = undefined;
thiz.__proto__ = undefined;
Object.freeze(thiz);
Object.preventExtensions(thiz)
return result;
};

var __extends = function(Child, Parent) {

if (Parent.extend) {
if (Parent.__isPrototypeImplementationObject) {
throw new Error("Can not extend an already extended native object.");
}

function extend(child, parent) {
__log("TS extend called");
if (!child.__extended) {
child.__extended = parent.extend(child.name, child.prototype);
}

return child.__extended;
};

Parent.__activityExtend = function(parent, name, implementationObject) {
__log("__activityExtend called");
return parent.extend(name, implementationObject);
};

Parent.call = function(thiz) {
var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent);
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();
}
};

Parent.apply = function(thiz, args) {
var Extended = extend(thiz.__proto__.__child, thiz.__proto__.__parent);
if (args && args.length > 0)
{
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
}
else
{
thiz.__proto__ = new Extended();
}
};
}

__extends_ts(Child, Parent);


if (Parent.extend) {
Child.__isPrototypeImplementationObject = true;
Child.__proto__ = Parent;
Child.prototype.__parent = Parent;
Child.prototype.__child = Child;
}
}

function JavaProxy(className) {
return function (target) {
var extended = target.extend(className, target.prototype)
extended.name = className;
return extended;
};
}

function Interfaces(interfacesArr) {
return function (target) {
if(interfacesArr instanceof Array) {
// attach interfaces: [] to the object
target.prototype.interfaces = interfacesArr;
}
}
}

global.__native = __native;
global.__extends = __extends;
global.__decorate = __decorate;
global.JavaProxy = JavaProxy;
global.Interfaces = Interfaces;
})()
36 changes: 36 additions & 0 deletions test-app/build-tools/jsparser/tests/specs/ast-parser-tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,42 @@ describe("parser/js_parser tests", function () {
});
});

it("Generated metadata for bindings should return proper JavaClass name when there is a static property and double assignment", function (done) {
let input = path.normalize(path.join(prefix, "decorated_double_assignment_ts", "app")),
generatedJavaClassesRoot = path.normalize(path.join(prefix, "decorated_double_assignment_ts", "src", "main", "java"));

const newClassNames = [
"org.nativescript.MyCustomActivity",
];

clearOutput();

execGradle(input, generatedJavaClassesRoot, function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}

logExecResult(stdout, stderr)

let bindingsContent = fs.readFileSync(sbgBindingOutoutFile, "utf-8").toString().trim().split('\n');

expect(bindingsContent.length).toBe(1);


for (let line of bindingsContent) {
var lineParts = line.split("*");
var tsExtendsPart = lineParts[1];
expect(tsExtendsPart).toBeFalsy();

var newClassNamePart = lineParts[6];
expect(newClassNames).toContain(newClassNamePart);
}

done();
});
});

it("Generate valid metadata for bindings where multiple interfaces are implemented using array", function (done) {
let input = path.normalize(path.join(prefix, "extends_with_interfaces", "app"));
generatedJavaClassesRoot = path.normalize(path.join(prefix, "extends_with_interfaces", "src", "main", "java"));
Expand Down
25 changes: 19 additions & 6 deletions test-app/build-tools/jsparser/visitors/es5-visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ var es5_visitors = (function() {
if (isDecorateStatement(ci)) {
// returns the node of the decorate (node.expression.right.callee)
// __decorate([..])
return ci.expression.right.arguments[0].elements;
return getRightExpression(ci.expression).arguments[0].elements;
}
}

Expand All @@ -319,20 +319,33 @@ var es5_visitors = (function() {
if (isDecorateStatement(sibling)) {
// returns the node of the decorate (node.expression.right.callee)
// __decorate([..])
return sibling.expression.right.arguments[0].elements;
return getRightExpression(sibling.expression).arguments[0].elements;
}
}

return null;
}

function getRightExpression(expression) {
if(!expression) {
return null;
}
var rightExpression = expression.right;
// if the right expression is a new assignment, get the right expression from that assignment
while (types.isAssignmentExpression(rightExpression)) {
rightExpression = rightExpression.right;
}
return rightExpression;
}

function isDecorateStatement(node) {
var rightExpression = getRightExpression(node.expression);
return types.isExpressionStatement(node) &&
types.isAssignmentExpression(node.expression) &&
node.expression.right.callee &&
node.expression.right.callee.name === "__decorate" &&
node.expression.right.arguments &&
types.isArrayExpression(node.expression.right.arguments[0])
rightExpression.callee &&
rightExpression.callee.name === "__decorate" &&
rightExpression.arguments &&
types.isArrayExpression(rightExpression.arguments[0])
}

/*
Expand Down