diff --git a/src/evaluate/helper.ts b/src/evaluate/helper.ts index 8622e17..85fd2be 100644 --- a/src/evaluate/helper.ts +++ b/src/evaluate/helper.ts @@ -175,7 +175,7 @@ export function createFunc( /**/ if (node.async && node.generator) { func = function (): AsyncIterator { - const iterator = tmpFunc.apply(void 0, arguments) + const iterator = tmpFunc.apply(this, arguments) let last: Promise = Promise.resolve() let hasCatch = false const run = (opts: runAsyncOptions) => @@ -198,7 +198,7 @@ export function createFunc( return asyncIterator } } else if (node.async) { - func = function () { return runAsync(tmpFunc.apply(void 0, arguments)) } + func = function () { return runAsync(tmpFunc.apply(this, arguments)) } } else { func = tmpFunc } diff --git a/tests/class.test.ts b/tests/class.test.ts index 89888e8..2172ff9 100644 --- a/tests/class.test.ts +++ b/tests/class.test.ts @@ -474,6 +474,24 @@ describe('testing src/index.ts', () => { expect(interpreter.exports.target).toEqual(interpreter.exports.actual) }) + it('should support async method', () => { + const interpreter = new Sval() + interpreter.run(` + class Foo { + constructor() { + this.a = 1 + } + async getA() { + return this.a + } + } + exports.Foo = Foo + `) + + const instance = new interpreter.exports.Foo(); + expect(instance.getA()).resolves.toBe(1); + }) + it('should support property accessing between parent and child class', () => { const interpreter = new Sval() interpreter.run(`