recently read a lot of related documents; but there is one place that has been puzzled. About Node calling the CPP callback function:
the official documents are as follows:
CPP section:
// addon.cc
-sharpinclude <node.h>
namespace demo {
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::String;
using v8::Value;
void RunCallback(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Function> cb = Local<Function>::Cast(args[0]);
const unsigned argc = 1;
Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "hello world") };
cb->Call(Null(isolate), argc, argv);
}
void Init(Local<Object> exports, Local<Object> module) {
NODE_SET_METHOD(module, "exports", RunCallback);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} // namespace demo
js
// test.js
const addon = require("./build/Release/addon");
addon((msg) => {
console.log(msg);
// Prints: "hello world"
});
you can see that although the js in this demo is function, it is executed by Synchronize in the CPP module; and if Synchronize executes, I have also called it, but the actual scenario is definitely not this kind of requirement. Instead, it is called back to js; through the callback function passed by the js module through the callback function passed by the js module after receiving a certain message in an asynchronous event monitor of the js module.
what has been done so far:
encapsulates a CPP class, and export, can call instance methods of this class with js;
two attributes of the .h file:
Isolate *onPlayNoteIsolate;
Local <Function> onPlayNote;
.cpp file
// 1:
void MIDIDeviceHelperBridge::setOnPlayNote(const FunctionCallbackInfo<Value>& args) {
MIDIDeviceHelperBridge *obj = ObjectWrap::Unwrap<MIDIDeviceHelperBridge>(args.Holder());
Local<Function> callback = Local<Function>::Cast(args[1]);
obj->onPlayNote = callback;
obj->onPlayNoteIsolate = args.GetIsolate();
}
// 2:
void MIDIDeviceHelperBridge::ReceiveMsg(DWORD Msg, DWORD TimeStamp) {
// this1obj
}
my requirement now is that the callback function of the js module obtained in method 1 is stored in obj or wherever the CPP module can access, and then after receiving the message in method 2, the message is passed to the js module through the callback of the js module obtained by method 1;
I tried to store callback and isolate, in obj in method 1, and then access callback and isolate, through this in method 2, but after receiving the message, the program crashed. I found that there was no problem with isolate, but callback was released. Later, I also tried to change the Local < Function > onPlayNote;
type to Persistent < Function > onPlayNote; and Handle < Function > onPlayNote; also crashed after receiving the message.
now I would like to ask if there are any great gods who have done similar functions.
=
modify part of the problem after a few hours:
1:Persistent <Function> onPlayNote;Persistent;
2:ReceiveMsgjsjsjs;
3:ReceiveMsgMIDIUSBwindowsmidiapiReceiveMsgReceiveMsgjs;
=