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
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require("./tests/exceptionHandlingTests");
require("./tests/discardedExceptionsTest");
require("./tests/dispatchCallbacksOnUiThreadTests");
require("./tests/stringConversionTests");
require("./tests/testTypeScriptExtendedConstruction");
require("./tests/testsForTypescript");
require("./tests/testGC");
require("./tests/testWrapperLifetime");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
describe("TypeScript-extended native class construction", function () {
var Holder = com.tns.tests.WrapperHolder;
var Button1 = com.tns.tests.Button1;

function collectBoth() {
for (var k = 0; k < 2; k++) {
gc();
java.lang.System.nanoTime();
for (var i = 0; i < 3; i++) {
java.lang.Runtime.getRuntime().gc();
java.lang.System.runFinalization();
}
gc();
java.lang.System.nanoTime();
}
}

afterEach(function () {
Holder.held = null;
});

// TypeScript's ES5 output for
// class EchoButton extends com.tns.tests.Button1 {
// label = "L" + value; count = 0;
// constructor(value) { super(value); }
// echo(s) { ... super.echo(s) ... } describe() { ... } static kind = "echo";
// }
var EchoButton = (function (_super) {
__extends(EchoButton, _super);
function EchoButton(value) {
var _this = _super.call(this, value) || this;
_this.label = "L" + value;
_this.count = 0;
return global.__native(_this);
}
EchoButton.prototype.echo = function (s) {
this.count++;
this.lastEcho = s;
return this.label + ":" + _super.prototype.echo.call(this, s);
};
EchoButton.prototype.describe = function () {
return this.label + "/" + this.count;
};
EchoButton.prototype.superToString = function () {
return this.super.toString();
};
EchoButton.kind = "echo";
return EchoButton;
})(com.tns.tests.Button1);

it("is an instance of its class and of every Java class above it", function () {
var button = new EchoButton(1);
expect(button instanceof EchoButton).toBe(true);
expect(button instanceof Button1).toBe(true);
expect(button instanceof com.tns.tests.ButtonBase).toBe(true);
expect(button instanceof java.lang.Object).toBe(true);
expect(new Button1() instanceof EchoButton).toBe(false);
});

it("shares one prototype chain between instances, with the class prototype on it", function () {
var a = new EchoButton(1);
var b = new EchoButton(2);
var proto = Object.getPrototypeOf(a);
expect(Object.getPrototypeOf(b)).toBe(proto);
expect(Object.getPrototypeOf(proto)).toBe(EchoButton.prototype);
expect(Object.getPrototypeOf(EchoButton.prototype)).toBe(Button1.prototype);
expect(a.constructor.prototype).toBe(proto);
expect(a instanceof a.constructor).toBe(true);
});

it("keeps the constructor's field initializers as own properties of the instance", function () {
var button = new EchoButton(3);
expect(Object.keys(button).sort()).toEqual(["count", "label"]);
expect(button.label).toBe("L3");
expect(button.count).toBe(0);
});

it("keeps the state of each instance apart", function () {
var a = new EchoButton(1);
var b = new EchoButton(2);
a.extra = "a";
a.triggerEcho("x");
expect(b.extra).toBeUndefined();
expect(a.count).toBe(1);
expect(b.count).toBe(0);
expect(b.describe()).toBe("L2/0");
});

it("runs the JS override when Java calls the method, with the instance as this", function () {
var button = new EchoButton(4);
expect(button.triggerEcho("hi")).toBe("L4:hi");
expect(button.count).toBe(1);
expect(button.lastEcho).toBe("hi");
expect(button.describe()).toBe("L4/1");
});

it("calls the Java implementation through super", function () {
var button = new EchoButton(5);
expect(button.echo("s")).toBe("L5:s");
var label = button.superToString();
expect(label.indexOf("com.tns.tests.Button1_")).not.toBe(-1);
});

it("calls inherited Java methods and passes the instance to Java and back", function () {
var button = new EchoButton(6);
expect(button.getIMAGE_ID_PROP()).toBe("image id prop");
var list = new java.util.ArrayList();
list.add(button);
expect(list.get(0)).toBe(button);
});

it("keeps the class's static members and inherits the Java ones", function () {
expect(EchoButton.kind).toBe("echo");
expect(EchoButton.SGetStaticImageId()).toBe("static image id");
});

it("keeps the prototype chain and the expandos of a Java-held instance after a collection", function () {
var proto = (function () {
var button = new EchoButton(7);
button.expando = "kept";
Holder.held = button;
return Object.getPrototypeOf(button);
})();
collectBoth();

var button = Holder.held;
expect(button.triggerEcho("r")).toBe("L7:r");
expect(Object.getPrototypeOf(button)).toBe(proto);
expect(button instanceof EchoButton).toBe(true);
expect(button.expando).toBe("kept");
expect(button.describe()).toBe("L7/1");
});

describe("when the constructor forwards its arguments with apply", function () {
// TypeScript's output for a class without a constructor, and for `super(...args)`.
var DefaultButton = (function (_super) {
__extends(DefaultButton, _super);
function DefaultButton() {
return _super !== null && _super.apply(this, arguments) || this;
}
DefaultButton.prototype.echo = function (s) {
return "default:" + s;
};
return DefaultButton;
})(com.tns.tests.Button1);
var SpreadButton = (function (_super) {
__extends(SpreadButton, _super);
function SpreadButton(value) {
var _this = _super.apply(this, [value]) || this;
_this.value = value;
return _this;
}
SpreadButton.prototype.echo = function (s) {
return this.value + ":" + s;
};
return SpreadButton;
})(com.tns.tests.Button1);

it("constructs the instance with the forwarded arguments", function () {
var plain = new DefaultButton();
expect(plain instanceof DefaultButton).toBe(true);
expect(plain.triggerEcho("a")).toBe("default:a");

var spread = new SpreadButton(3);
expect(spread instanceof SpreadButton).toBe(true);
expect(Object.keys(spread)).toEqual(["value"]);
expect(spread.triggerEcho("b")).toBe("3:b");
});
});
});
46 changes: 20 additions & 26 deletions test-app/app/src/main/assets/internal/ts_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@

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

for (var prop in thiz) {
if (thiz.hasOwnProperty(prop)) {
thiz.__proto__[prop] = thiz[prop];
result[prop] = thiz[prop];
delete thiz[prop];
}
}
Expand All @@ -47,6 +42,17 @@
}
};

var nativeInstanceKey = Symbol("nativeInstance");

// Never make the native instance a prototype, not even of the constructor's own `this`:
// V8 gives a prototype a map of its own, so every store on the instance, such as each
// field initializer, would miss.
function linkContainer(thiz, instance) {
thiz.__container__ = true;
thiz[nativeInstanceKey] = instance;
return instance;
}

var __extends = function (Child, Parent) {
var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1);
if (!extendNativeClass) {
Expand Down Expand Up @@ -77,26 +83,14 @@

Parent.call = function (thiz) {
var Extended = extend(thiz);
thiz.__container__ = true;
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()
}
return thiz.__proto__;
var instance = arguments.length > 1 ? Reflect.construct(Extended, Array.prototype.slice.call(arguments, 1)) : new Extended();
return linkContainer(thiz, instance);
};

Parent.apply = function (thiz, args) {
var Extended = extend(thiz);
thiz.__container__ = true;
if (args && args.length > 0) {
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(args))));
}
else {
thiz.__proto__ = new Extended();
}
return thiz.__proto__;
var instance = args && args.length > 0 ? Reflect.construct(Extended, Array.prototype.slice.call(args)) : new Extended();
return linkContainer(thiz, instance);
};
__extends_ns(Child, Parent);
Child.__isPrototypeImplementationObject = true;
Expand Down