Skip to content

Commit 53753ce

Browse files
committed
fix(runtime): pass a TypeScript constructor's arguments to the Java constructor
TypeScript compiles a class without a constructor to `_super.apply(this, arguments) || this`. The helpers concatenated the arguments object into the argument list instead of its elements, so the Java constructor received a single Arguments object and creating the instance with any argument threw "Cannot marshal JavaScript argument".
1 parent 2cb821d commit 53753ce

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎test-app/app/src/main/assets/app/tests/testsForTypescript.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,27 @@ describe("Tests typescript", function () {
235235
expect(isInstanceOf).toEqual(true);
236236
});
237237

238+
it("When_a_typescript_class_without_a_constructor_is_created_with_arguments_it_should_pass_them_to_the_java_constructor", function () {
239+
240+
// TypeScript's output for `class NamedThread extends java.lang.Thread {}`.
241+
var NamedThread = (function (_super) {
242+
__extends(NamedThread, _super);
243+
function NamedThread() {
244+
return _super !== null && _super.apply(this, arguments) || this;
245+
}
246+
NamedThread.prototype.run = function () {
247+
};
248+
return NamedThread;
249+
})(java.lang.Thread);
250+
251+
var thread = new NamedThread("named");
252+
expect(thread instanceof NamedThread).toBe(true);
253+
expect(thread.getName()).toBe("named");
254+
255+
var unnamed = new NamedThread();
256+
expect(unnamed instanceof NamedThread).toBe(true);
257+
});
258+
238259
it("When_creating_a_typescript_instance_it_should_support_member_access", function () {
239260

240261
__log("TEST: When_creating_a_typescript_instance_it_should_support_member_access");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
var Extended = extend(thiz);
9292
thiz.__container__ = true;
9393
if (args && args.length > 0) {
94-
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
94+
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(args))));
9595
}
9696
else {
9797
thiz.__proto__ = new Extended();

0 commit comments

Comments
 (0)