diff --git a/lib/loader/index.js b/lib/loader/index.js index 224c674d..847f7e8b 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -265,7 +265,7 @@ function demangle(exports, baseModule) { let classElem = curr[className]; if (typeof classElem === "undefined" || !classElem.prototype) { let ctor = function(...args) { - return ctor.wrap(ctor.prototype.constructor(...args)); + return ctor.wrap(ctor.prototype.constructor(0, ...args)); }; ctor.prototype = {}; ctor.wrap = function(thisValue) { @@ -289,7 +289,16 @@ function demangle(exports, baseModule) { }); } } else { - curr[name] = wrapFunction(elem, setargc); + if (name === 'constructor') { + curr[name] = wrapFunction(elem, setargc); + } else { // for methods + Object.defineProperty(curr, name, { + value: function (...args) { + setargc(args.length); + return elem(this.this, ...args); + } + }); + } } } else { if (/^(get|set):/.test(name)) { diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index 917668ef..133dda77 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -60,5 +60,14 @@ assert.strictEqual(fn(2), 4); ptr = module.newFunction(module.varadd); 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 -module.dotrace(42); +module.dotrace(42); \ No newline at end of file