c++ - Using a node.js persistent handle -


i passing instance of javascript class node.js c++ addon.

let addon = require('boxruntime_addon'); import { mybox } "./testbox1"; let mytestbox = new mybox(addon);  addon.registerbox(mytestbox); 

the testbox1 javascript object has instance data;

import { boxinterface } "./boxinterface"; export class mybox implements boxinterface {      nomsgs : number;      msgin(msg : string) {         console.log("mybox::msgin() : " + msg);         ++this.nomsgs;         console.log("number of msgs = " + this.nomsgs);     }   constructor(runtime : any) {      console.log("constructor: " + runtime);     console.log(runtime);     mybox.runtime = runtime;      this.nomsgs = 0; } 

in c++ code, in registerbox() method create v8::uniquepersistent reference passed in box object.

later use presistent reference call msgin() method on box instance, , expected nomsgs instance value increment each time.

but doesn't work. nan each time, if not getting same instance of mytextbox each time.

can explain what's going on here? here c++ code registerbox.

void registercallback(const functioncallbackinfo<value>& args) {     isolate* isolate = args.getisolate();     local<object> target = args[0]->toobject();     local<v8::string> methodname = string::newfromutf8(isolate,"msgin");     local<v8::value> methodptr = target->get(methodname);     if (!methodptr->isfunction()) {         std::cout << "*** error registering box, missing msgin() method." << std::endl;     }     else {         uniquepersistent<v8::object> cb(isolate,target);         callbackbox = std::move(cb);     }     } 


Comments