Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm里的对象实例, 外部call async method的的时候this指向不对 #55

Closed
lljxx1 opened this issue Mar 13, 2020 · 5 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@lljxx1
Copy link

lljxx1 commented Mar 13, 2020

class A {
    constructor(a) {
        this.a = a;
    }
    print() {
        console.log(this.a)
    }
}

exports.A  = A

const instance = new vm.exports.A('test')
instance.print()
@lljxx1 lljxx1 changed the title vm vm里的对象实例, 外部call metho的的时候this指向不对 Mar 13, 2020
@lljxx1 lljxx1 changed the title vm里的对象实例, 外部call metho的的时候this指向不对 vm里的对象实例, 外部call method的的时候this指向不对 Mar 13, 2020
@lljxx1
Copy link
Author

lljxx1 commented Mar 13, 2020

更正一下,是async function会这样

@lljxx1
Copy link
Author

lljxx1 commented Mar 13, 2020

const  Sval = require('sval');
const options = {
    // ECMA Version of the code (5 | 6 | 7 | 8 | 9 | 10 | 2015 | 2016 | 2017 | 2018 | 2019)
    ecmaVer: 2019,
    // Whether the code runs in a sandbox
    sandBox: true
};


const interpreter = new Sval(options);
const sanbox = {
    console: console,
};

const code = `class A {
    constructor(a) {
        this.a = a;
    }
    print() {
        return new Promise((resolve, rject) => {
            resolve(this.a)
        })
    }

    async printB() {
       return this.a
    }
}

exports.A  = A`

interpreter.run(code);
const instance = new interpreter.exports.A('test');
console.log('instance', instance);

(async () => {
    try {
        console.log('print', await instance.print())
        console.log('printB', await instance.printB())
    } catch (e) {
        console.log(e)
    }
})();

@lljxx1
Copy link
Author

lljxx1 commented Mar 13, 2020

看来qucik fix只能wrap个 new Promise,还好有热更新

@Siubaak Siubaak added the bug Something isn't working label Mar 13, 2020
@Siubaak Siubaak self-assigned this Mar 13, 2020
@lljxx1 lljxx1 changed the title vm里的对象实例, 外部call method的的时候this指向不对 vm里的对象实例, 外部call async method的的时候this指向不对 Mar 13, 2020
@Siubaak Siubaak closed this as completed in f567e79 May 4, 2020
@Siubaak
Copy link
Owner

Siubaak commented May 4, 2020

@lljxx1 0.4.7已fix

@lljxx1
Copy link
Author

lljxx1 commented Jun 11, 2020

@lljxx1 0.4.7已fix

收到

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants