Improve demangling of class methods when using the loader (#363)

This commit is contained in:
kazuya kawaguchi 2018-12-08 01:20:28 +09:00 committed by Daniel Wirtz
parent 934f05eed9
commit 429435c5b1
2 changed files with 21 additions and 3 deletions

View File

@ -265,7 +265,7 @@ function demangle(exports, baseModule) {
let classElem = curr[className]; let classElem = curr[className];
if (typeof classElem === "undefined" || !classElem.prototype) { if (typeof classElem === "undefined" || !classElem.prototype) {
let ctor = function(...args) { let ctor = function(...args) {
return ctor.wrap(ctor.prototype.constructor(...args)); return ctor.wrap(ctor.prototype.constructor(0, ...args));
}; };
ctor.prototype = {}; ctor.prototype = {};
ctor.wrap = function(thisValue) { ctor.wrap = function(thisValue) {
@ -289,7 +289,16 @@ function demangle(exports, baseModule) {
}); });
} }
} else { } else {
if (name === 'constructor') {
curr[name] = wrapFunction(elem, setargc); curr[name] = wrapFunction(elem, setargc);
} else { // for methods
Object.defineProperty(curr, name, {
value: function (...args) {
setargc(args.length);
return elem(this.this, ...args);
}
});
}
} }
} else { } else {
if (/^(get|set):/.test(name)) { if (/^(get|set):/.test(name)) {

View File

@ -60,5 +60,14 @@ assert.strictEqual(fn(2), 4);
ptr = module.newFunction(module.varadd); ptr = module.newFunction(module.varadd);
assert.strictEqual(module.calladd(ptr, 2, 3), 5); assert.strictEqual(module.calladd(ptr, 2, 3), 5);
// should be able to use a class
var car = new module.Car(5);
assert.strictEqual(car.numDoors, 5);
assert.strictEqual(car.isDoorsOpen, 0);
car.openDoors();
assert.strictEqual(car.isDoorsOpen, 1);
car.closeDoors();
assert.strictEqual(car.isDoorsOpen, 0);
// should be able to use trace // should be able to use trace
module.dotrace(42); module.dotrace(42);