diff --git a/doc/callback.md b/doc/callback.md index 60719352..f7af0bfd 100644 --- a/doc/callback.md +++ b/doc/callback.md @@ -49,9 +49,10 @@ class Callback { v8::Local argv[], AsyncResource* async_resource) const; - // Legacy versions. Use the versions that accept an async_resource instead + // Deprecated versions. Use the versions that accept an async_resource instead // as they run the callback in the correct async context as specified by the - // resource. + // resource. If you want to call a synchronous JS function (i.e. on a + // non-empty JS stack), you can use Nan::Call instead. v8::Local operator()(v8::Local target, int argc = 0, v8::Local argv[] = 0) const; diff --git a/doc/maybe_types.md b/doc/maybe_types.md index 5baa2cfb..6609f15a 100644 --- a/doc/maybe_types.md +++ b/doc/maybe_types.md @@ -122,12 +122,16 @@ template Nan::Maybe Nan::Just(const T &t); ### Nan::Call() -A helper method for calling [`v8::Function#Call()`](https://v8docs.nodesource.com/io.js-3.3/d5/d54/classv8_1_1_function.html#a468a89f737af0612db10132799c827c0) in a way compatible across supported versions of V8. +A helper method for calling a synchronous [`v8::Function#Call()`](https://v8docs.nodesource.com/io.js-3.3/d5/d54/classv8_1_1_function.html#a468a89f737af0612db10132799c827c0) in a way compatible across supported versions of V8. + +For asynchronous callbacks, use Nan::Callback::Call along with an AsyncResource. Signature: ```c++ Nan::MaybeLocal Nan::Call(v8::Local fun, v8::Local recv, int argc, v8::Local argv[]); +Nan::MaybeLocal Nan::Call(const Nan::Callback& callback, v8::Local recv, + int argc, v8::Local argv[]); ``` diff --git a/nan.h b/nan.h index 14d236c6..cd3e9527 100644 --- a/nan.h +++ b/nan.h @@ -1482,14 +1482,14 @@ class Callback { inline v8::Local operator*() const { return GetFunction(); } - inline v8::Local operator()( + NAN_DEPRECATED inline v8::Local operator()( v8::Local target , int argc = 0 , v8::Local argv[] = 0) const { return this->Call(target, argc, argv); } - inline v8::Local operator()( + NAN_DEPRECATED inline v8::Local operator()( int argc = 0 , v8::Local argv[] = 0) const { return this->Call(argc, argv); @@ -1531,7 +1531,11 @@ class Callback { return handle_.IsEmpty(); } - inline v8::Local + // Deprecated: For async callbacks Use the versions that accept an + // AsyncResource. If this callback does not correspond to an async resource, + // that is, it is a synchronous function call on a non-empty JS stack, you + // should Nan::Call instead. + NAN_DEPRECATED inline v8::Local Call(v8::Local target , int argc , v8::Local argv[]) const { @@ -1543,7 +1547,11 @@ class Callback { #endif } - inline v8::Local + // Deprecated: For async callbacks Use the versions that accept an + // AsyncResource. If this callback does not correspond to an async resource, + // that is, it is a synchronous function call on a non-empty JS stack, you + // should Nan::Call instead. + NAN_DEPRECATED inline v8::Local Call(int argc, v8::Local argv[]) const { #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION v8::Isolate *isolate = v8::Isolate::GetCurrent(); @@ -1650,6 +1658,14 @@ class Callback { #endif }; +inline MaybeLocal Call( + const Nan::Callback& callback + , v8::Local recv + , int argc + , v8::Local argv[]) { + return Call(*callback, recv, argc, argv); +} + /* abstract */ class AsyncWorker { public: explicit AsyncWorker(Callback *callback_, diff --git a/test/cpp/asyncprogressqueueworker.cpp b/test/cpp/asyncprogressqueueworker.cpp index f6b276ea..d36c7806 100644 --- a/test/cpp/asyncprogressqueueworker.cpp +++ b/test/cpp/asyncprogressqueueworker.cpp @@ -35,7 +35,7 @@ class ProgressQueueWorker : public AsyncProgressQueueWorker { v8::Local argv[] = { New(*reinterpret_cast(const_cast(data))) }; - progress->Call(1, argv); + progress->Call(1, argv, async_resource); } private: diff --git a/test/cpp/asyncprogressqueueworkerstream.cpp b/test/cpp/asyncprogressqueueworkerstream.cpp index b6f57458..dac34f9b 100644 --- a/test/cpp/asyncprogressqueueworkerstream.cpp +++ b/test/cpp/asyncprogressqueueworkerstream.cpp @@ -54,7 +54,7 @@ class ProgressQueueWorker : public AsyncProgressQueueWorker { New(data->data)); v8::Local argv[] = { obj }; - progress->Call(1, argv); + progress->Call(1, argv, this->async_resource); } private: diff --git a/test/cpp/asyncprogressworker.cpp b/test/cpp/asyncprogressworker.cpp index 2e030b12..c9539f9b 100644 --- a/test/cpp/asyncprogressworker.cpp +++ b/test/cpp/asyncprogressworker.cpp @@ -41,7 +41,7 @@ class ProgressWorker : public AsyncProgressWorker { v8::Local argv[] = { New(*reinterpret_cast(const_cast(data))) }; - progress->Call(1, argv); + progress->Call(1, argv, async_resource); } private: diff --git a/test/cpp/asyncprogressworkersignal.cpp b/test/cpp/asyncprogressworkersignal.cpp index 6563128d..183bdb8e 100644 --- a/test/cpp/asyncprogressworkersignal.cpp +++ b/test/cpp/asyncprogressworkersignal.cpp @@ -39,7 +39,7 @@ class ProgressWorker : public AsyncProgressWorker { HandleScope scope; v8::Local arg = New(data == NULL && count == 0); - progress->Call(1, &arg); + progress->Call(1, &arg, async_resource); } private: diff --git a/test/cpp/asyncprogressworkerstream.cpp b/test/cpp/asyncprogressworkerstream.cpp index d50459ee..5ca2a696 100644 --- a/test/cpp/asyncprogressworkerstream.cpp +++ b/test/cpp/asyncprogressworkerstream.cpp @@ -61,7 +61,7 @@ class ProgressWorker : public AsyncProgressWorkerBase { New(data->data)); v8::Local argv[] = { obj }; - progress->Call(1, argv); + progress->Call(1, argv, this->async_resource); } private: diff --git a/test/cpp/bufferworkerpersistent.cpp b/test/cpp/bufferworkerpersistent.cpp index a1200551..e1a38377 100644 --- a/test/cpp/bufferworkerpersistent.cpp +++ b/test/cpp/bufferworkerpersistent.cpp @@ -37,13 +37,13 @@ class BufferWorker : public AsyncWorker { HandleScope scope; v8::Local handle = GetFromPersistent("buffer"); - callback->Call(1, &handle); + callback->Call(1, &handle, async_resource); handle = GetFromPersistent(New("puffer").ToLocalChecked()); - callback->Call(1, &handle); + callback->Call(1, &handle, async_resource); handle = GetFromPersistent(0u); - callback->Call(1, &handle); + callback->Call(1, &handle, async_resource); } private: diff --git a/test/cpp/nancallback.cpp b/test/cpp/nancallback.cpp index c6311cb3..23e13ad6 100644 --- a/test/cpp/nancallback.cpp +++ b/test/cpp/nancallback.cpp @@ -11,17 +11,20 @@ using namespace Nan; // NOLINT(build/namespaces) NAN_METHOD(GlobalContext) { - Callback(To(info[0]).ToLocalChecked()).Call(0, NULL); + AsyncResource resource("nan:test.nancallback"); + Callback(To(info[0]).ToLocalChecked()).Call(0, NULL, &resource); } NAN_METHOD(SpecificContext) { + AsyncResource resource("nan:test.nancallback"); Callback cb(To(info[0]).ToLocalChecked()); - cb.Call(GetCurrentContext()->Global(), 0, NULL); + cb.Call(GetCurrentContext()->Global(), 0, NULL, &resource); } NAN_METHOD(CustomReceiver) { + AsyncResource resource("nan:test.nancallback"); Callback cb(To(info[0]).ToLocalChecked()); - cb.Call(To(info[1]).ToLocalChecked(), 0, NULL); + cb.Call(To(info[1]).ToLocalChecked(), 0, NULL, &resource); } NAN_METHOD(CompareCallbacks) { @@ -38,7 +41,8 @@ NAN_METHOD(CallDirect) { } NAN_METHOD(CallAsFunction) { - Callback(To(info[0]).ToLocalChecked())(); + AsyncResource resource("nan:test.nancallback"); + Callback(To(info[0]).ToLocalChecked())(&resource); } NAN_METHOD(ResetUnset) {