Skip to content

Commit 429435c

Browse files
kazupondcodeIO
authored andcommitted
Improve demangling of class methods when using the loader (AssemblyScript#363)
1 parent 934f05e commit 429435c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/loader/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function demangle(exports, baseModule) {
265265
let classElem = curr[className];
266266
if (typeof classElem === "undefined" || !classElem.prototype) {
267267
let ctor = function(...args) {
268-
return ctor.wrap(ctor.prototype.constructor(...args));
268+
return ctor.wrap(ctor.prototype.constructor(0, ...args));
269269
};
270270
ctor.prototype = {};
271271
ctor.wrap = function(thisValue) {
@@ -289,7 +289,16 @@ function demangle(exports, baseModule) {
289289
});
290290
}
291291
} else {
292-
curr[name] = wrapFunction(elem, setargc);
292+
if (name === 'constructor') {
293+
curr[name] = wrapFunction(elem, setargc);
294+
} else { // for methods
295+
Object.defineProperty(curr, name, {
296+
value: function (...args) {
297+
setargc(args.length);
298+
return elem(this.this, ...args);
299+
}
300+
});
301+
}
293302
}
294303
} else {
295304
if (/^(get|set):/.test(name)) {

lib/loader/tests/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,14 @@ assert.strictEqual(fn(2), 4);
6060
ptr = module.newFunction(module.varadd);
6161
assert.strictEqual(module.calladd(ptr, 2, 3), 5);
6262

63+
// should be able to use a class
64+
var car = new module.Car(5);
65+
assert.strictEqual(car.numDoors, 5);
66+
assert.strictEqual(car.isDoorsOpen, 0);
67+
car.openDoors();
68+
assert.strictEqual(car.isDoorsOpen, 1);
69+
car.closeDoors();
70+
assert.strictEqual(car.isDoorsOpen, 0);
71+
6372
// should be able to use trace
64-
module.dotrace(42);
73+
module.dotrace(42);

0 commit comments

Comments
 (0)