node-addon-api Вызов js Метод объекта не выполнен - PullRequest
1 голос
/ 04 апреля 2019

Я передаю объект javascript в код c ++, код c ++ вызывает метод этого объекта «readdata1», но второй метод объекта (readdata2) не вызвал;

консоль сообщает, что readdata2 не определена;

JS код:

    const testfunc = require('./index').testfunc;

    class reader{
        constructor(){
            this.readdata2=function(){
                console.log('js:readdata2')
                return 222
            }
            this.readdata1=function (){
                console.log('js:readdata1')
                let ret = this.readdata2()
                return ret;
            }
        }
    }

    let n = testfunc(new reader());
    console.log('n=',n)

c ++ код:

    Napi::Value testfunc(const Napi::CallbackInfo &info) {
    LOG("C++:testfunc()!!");
    Object r = info[0].As<Napi::Object>();
    return r.Get("readdata1").As<Function>().Call({});
    }

выход:

C++:testfunc()!!
js:readdata1
/Users/chentingfeng/cpp_proj/decodeDLL/test.js:17
let n = testfunc(new reader());
        ^

TypeError: Cannot read property 'readdata2' of undefined
    at readdata1 (/Users/chentingfeng/cpp_proj/decodeDLL/test.js:11:28)
    at Object.<anonymous> (/Users/chentingfeng/cpp_proj/decodeDLL/test.js:17:9)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
npm ERR! Test failed.  See above for more details.

Process finished with exit code 1
...