The Task:
I call a callback from C++ that return a v8::Promise (so an async function). Now I want to find out if the promise was resolved.
For this example here, I want to check from JS if the promise is resolved. But "just beeing informed in the C++ addon" would be fine.
The Problem:
I failed to create a persistent Promise object in C++. I works while I am still in the event loop. But when I get into the event loop again later on, the object is empty.
The code:
JS test code
// create an object in the addon
var OB = require('./build/Debug/objectwraphandle.node')
var obj = new OB.MyObject(42)
// just an async wait function
async function asyncFunc1(y) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve('asyncFunc1: DONE'), 2000);
});
}
// pass the async function as callback to the addon and call it
obj.callAsyncFunction(asyncFunc1);
// ask the object, it the promise is already resolved.
// this should switch from 1 (Pending) to 0 (Resolved) after 2 seconds
console.log(" resolved? : " + obj.isPromiseResolved());
// but this core-dumps with:
// "FATAL ERROR: v8::Promise::Cast Could not convert to promise"
Now the C++ side (by the way - no need to say I am neither C++ nor JS guru) (original taken from NAN - Native Abstractions for Node.js). For reproducing, I put the full code here. Important are the functions CallAsyncFunction and IsPromiseResolved.
/*********************************************************************
* NAN - Native Abstractions for Node.js
* Copyright (c) 2018 NAN contributors
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
*
* and now here: used as test basis by Gerrit Prange
********************************************************************/
#include <iostream>
#include <nan.h>
using namespace Nan;
class MyObject : public ObjectWrap {
public:
static NAN_MODULE_INIT(Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// these are the two functions in question
SetPrototypeMethod(tpl, "callAsyncFunction", CallAsyncFunction);
SetPrototypeMethod(tpl, "isPromiseResolved", IsPromiseResolved);
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Set(target, Nan::New("MyObject").ToLocalChecked(),
Nan::GetFunction(tpl).ToLocalChecked());
}
private:
explicit MyObject(double value = 0) : value_(value) {}
~MyObject() {}
// here: the promise is stored as persistent object
Nan::Persistent<v8::Promise> *persistentPromise;
static NAN_METHOD(New) {
if (info.IsConstructCall()) {
double value = info[0]->IsUndefined() ? 0 : Nan::To<double>(info[0]).FromJust();
MyObject *obj = new MyObject(value);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {info[0]};
v8::Local<v8::Function> cons = Nan::New(constructor());
info.GetReturnValue().Set(
Nan::NewInstance(cons, argc, argv).ToLocalChecked());
}
}
/* we get a callback function (async function),
* call this callback and get a promise returned
*/
static NAN_METHOD(CallAsyncFunction) {
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { Nan::New("hello world").ToLocalChecked() };
Callback cb(To<v8::Function>(info[0]).ToLocalChecked());
// call the callback - and get a Promise
Nan::MaybeLocal<v8::Value> promiseReturnValue = (*cb)->Call(GetCurrentContext()->Global(), argc, argv);
// check if the promise is already resolved. (should not be in this example!)
v8::Handle<v8::Promise> promiseReturnObject = v8::Handle<v8::Promise>::Cast ( promiseReturnValue.ToLocalChecked() );
v8::Promise::PromiseState promiseState = promiseReturnObject->State();
std::cout << " state: " << promiseState << std::endl;
// make the callback persistent
Nan::Persistent<v8::Promise> persistentPromiseReturnObject(promiseReturnObject);
obj->persistentPromise = &persistentPromiseReturnObject;
}
/* check if the callback is already resolved and return the state
*/
static NAN_METHOD(IsPromiseResolved) {
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
v8::Local<v8::Context> context = v8::Isolate::GetCurrent()->GetCurrentContext();
// get the persistent callback and convert it into a local object
v8::Local<v8::Object> objectToCheckPromise = Nan::New ( *obj->persistentPromise );
// THE LINE BELOW IS THE PROBLEM! actually, persiting does not seem to work.
v8::Local<v8::Promise> promiseObject = v8::Local<v8::Promise>::Cast ( objectToCheckPromise );
// get the promises state
v8::Promise::PromiseState promiseState = promiseObject->State();
// and return the state
std::cout << " in IsPromiseResolved state: " << promiseState << std::endl;
info.GetReturnValue().Set(Nan::New(promiseState));
}
static inline Persistent<v8::Function> & constructor() {
static Persistent<v8::Function> my_constructor;
return my_constructor;
}
double value_;
};
NODE_MODULE(objectwraphandle, MyObject::Init)
The actual error I get is:
FATAL ERROR: v8::Promise::Cast Could not convert to promise
1: node::Abort() [node]
2: 0x121a2cc [node]
3: v8::Utils::ReportApiFailure(char const*, char const*) [node]
4: v8::Promise::Cast(v8::Value*) [/home/gpr/projects/own/nodejs/jsFromC_NAN_PromiseWait/build/Debug/objectwraphandle.node]
5: v8::Local<v8::Promise> v8::Local<v8::Promise>::Cast<v8::Object>(v8::Local<v8::Object>) [/home/gpr/projects/own/nodejs/jsFromC_NAN_PromiseWait/build/Debug/objectwraphandle.node]
6: MyObject::IsPromiseResolved(Nan::FunctionCallbackInfo<v8::Value> const&) [/home/gpr/projects/own/nodejs/jsFromC_NAN_PromiseWait/build/Debug/objectwraphandle.node]
7: 0x7fb4dd8889dc [/home/gpr/projects/own/nodejs/jsFromC_NAN_PromiseWait/build/Debug/objectwraphandle.node]
8: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [node]
9: 0xb9043c [node]
10: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [node]
Sorry, this is a long post. (But I wanted to put the code here - if anyone wants to try this out.)
You can store Promises in persistent handles. Your code should look something like:
I don't know if there's ever a valid reason to take the address of or dereference a v8 type as an embedder (looking at your callback invocation and how you were setting the persistent handle). There are API methods that do these things safely.
BTW,
Handle
is deprecated;Local
is its replacement.