Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate old Callback::Call #733

Merged
merged 4 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class Callback {
v8::Local<v8::Value> 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<v8::Value> operator()(v8::Local<v8::Object> target,
int argc = 0,
v8::Local<v8::Value> argv[] = 0) const;
Expand Down
6 changes: 5 additions & 1 deletion doc/maybe_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,16 @@ template<typename T> Nan::Maybe<T> Nan::Just(const T &t);
<a name="api_nan_call"></a>
### 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<v8::Value> Nan::Call(v8::Local<v8::Function> fun, v8::Local<v8::Object> recv, int argc, v8::Local<v8::Value> argv[]);
Nan::MaybeLocal<v8::Value> Nan::Call(const Nan::Callback& callback, v8::Local<v8::Object> recv,
int argc, v8::Local<v8::Value> argv[]);
```


Expand Down
24 changes: 20 additions & 4 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1482,14 +1482,14 @@ class Callback {
inline
v8::Local<v8::Function> operator*() const { return GetFunction(); }

inline v8::Local<v8::Value> operator()(
NAN_DEPRECATED inline v8::Local<v8::Value> operator()(
v8::Local<v8::Object> target
, int argc = 0
, v8::Local<v8::Value> argv[] = 0) const {
return this->Call(target, argc, argv);
}

inline v8::Local<v8::Value> operator()(
NAN_DEPRECATED inline v8::Local<v8::Value> operator()(
int argc = 0
, v8::Local<v8::Value> argv[] = 0) const {
return this->Call(argc, argv);
Expand Down Expand Up @@ -1531,7 +1531,11 @@ class Callback {
return handle_.IsEmpty();
}

inline v8::Local<v8::Value>
// 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<v8::Value>
Call(v8::Local<v8::Object> target
, int argc
, v8::Local<v8::Value> argv[]) const {
Expand All @@ -1543,7 +1547,11 @@ class Callback {
#endif
}

inline v8::Local<v8::Value>
// 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<v8::Value>
Call(int argc, v8::Local<v8::Value> argv[]) const {
#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
v8::Isolate *isolate = v8::Isolate::GetCurrent();
Expand Down Expand Up @@ -1650,6 +1658,14 @@ class Callback {
#endif
};

inline MaybeLocal<v8::Value> Call(
const Nan::Callback& callback
, v8::Local<v8::Object> recv
, int argc
, v8::Local<v8::Value> argv[]) {
return Call(*callback, recv, argc, argv);
}

/* abstract */ class AsyncWorker {
public:
explicit AsyncWorker(Callback *callback_,
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/asyncprogressqueueworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProgressQueueWorker : public AsyncProgressQueueWorker<char> {
v8::Local<v8::Value> argv[] = {
New<v8::Integer>(*reinterpret_cast<int*>(const_cast<char*>(data)))
};
progress->Call(1, argv);
progress->Call(1, argv, async_resource);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/asyncprogressqueueworkerstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ProgressQueueWorker : public AsyncProgressQueueWorker<T> {
New<v8::Integer>(data->data));

v8::Local<v8::Value> argv[] = { obj };
progress->Call(1, argv);
progress->Call(1, argv, this->async_resource);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/asyncprogressworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ProgressWorker : public AsyncProgressWorker {
v8::Local<v8::Value> argv[] = {
New<v8::Integer>(*reinterpret_cast<int*>(const_cast<char*>(data)))
};
progress->Call(1, argv);
progress->Call(1, argv, async_resource);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/asyncprogressworkersignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ProgressWorker : public AsyncProgressWorker {
HandleScope scope;

v8::Local<v8::Value> arg = New<v8::Boolean>(data == NULL && count == 0);
progress->Call(1, &arg);
progress->Call(1, &arg, async_resource);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion test/cpp/asyncprogressworkerstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ProgressWorker : public AsyncProgressWorkerBase<T> {
New<v8::Integer>(data->data));

v8::Local<v8::Value> argv[] = { obj };
progress->Call(1, argv);
progress->Call(1, argv, this->async_resource);
}

private:
Expand Down
6 changes: 3 additions & 3 deletions test/cpp/bufferworkerpersistent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class BufferWorker : public AsyncWorker {
HandleScope scope;

v8::Local<v8::Value> 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:
Expand Down
12 changes: 8 additions & 4 deletions test/cpp/nancallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
using namespace Nan; // NOLINT(build/namespaces)

NAN_METHOD(GlobalContext) {
Callback(To<v8::Function>(info[0]).ToLocalChecked()).Call(0, NULL);
AsyncResource resource("nan:test.nancallback");
Callback(To<v8::Function>(info[0]).ToLocalChecked()).Call(0, NULL, &resource);
}

NAN_METHOD(SpecificContext) {
AsyncResource resource("nan:test.nancallback");
Callback cb(To<v8::Function>(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<v8::Function>(info[0]).ToLocalChecked());
cb.Call(To<v8::Object>(info[1]).ToLocalChecked(), 0, NULL);
cb.Call(To<v8::Object>(info[1]).ToLocalChecked(), 0, NULL, &resource);
}

NAN_METHOD(CompareCallbacks) {
Expand All @@ -38,7 +41,8 @@ NAN_METHOD(CallDirect) {
}

NAN_METHOD(CallAsFunction) {
Callback(To<v8::Function>(info[0]).ToLocalChecked())();
AsyncResource resource("nan:test.nancallback");
Callback(To<v8::Function>(info[0]).ToLocalChecked())(&resource);
}

NAN_METHOD(ResetUnset) {
Expand Down