I'm currently building an app (Electron) and i need to connect it with a c++ library. I have done most of the binding using the NodeJS c++ addons, but i'm missing an important part that is related with receiving the events generated by the c++ library on my Javascript code.
void Event1(int64_t id)
{
ArrayBufferAllocator allocator;
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
Isolate* isolate = Isolate::New(create_params);
{
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
v8::Local<v8::Value> argv[1] = {
Nan::New("OnWillPlayTrack").ToLocalChecked(), // event name
};
Nan::MakeCallback(isolate->GetCurrentContext()->Global(),"emit", 1, argv);
}
isolate->Dispose();
}
The Event1 is being called by the c++ lib, that has nothing to do with V8, and now I want to fire an event to JavaScript, back to Node (EventEmitter?). I think that the biggest problem is that most of the v8 functions now need an Isolate, and most of the examples found throughout the web are pretty old.
The Event1 code crashes at MakeCallBack with:
Thread 24 Crashed:: App
0 libnode.dylib 0x000000010a81e35b v8::String::NewFromOneByte(v8::Isolate*, unsigned char const*, v8::String::NewStringType, int) + 43
1 libnode.dylib 0x000000010a4b042f node::OneByteString(v8::Isolate*, char const*, int) + 15
2 libnode.dylib 0x000000010a4ba1b2 node::MakeCallback(node::Environment*, v8::Local<v8::Object>, char const*, int, v8::Local<v8::Value>*) + 50
3 libnode.dylib 0x000000010a4ba240 node::MakeCallback(v8::Isolate*, v8::Local<v8::Object>, char const*, int, v8::Local<v8::Value>*) + 96
4 addon.node 0x0000000110e62e1f Event1(long long) + 291 (v8.h:6721)
Any help will be greatly appreciated!
EventEmitterfrom javascript within the c++ addon scope requires a specific procedure that I have explained in my answer below. Multithreading within a v8 c++ addon is an entirely different problem.