Skip to content

Commit

Permalink
Deprecate legacy Callback::Call
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Feb 13, 2018
1 parent ae82fb2 commit c82a3af
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ 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.
v8::Local<v8::Value> operator()(v8::Local<v8::Object> target,
Expand Down
12 changes: 8 additions & 4 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1479,17 +1479,19 @@ class Callback {
return !operator==(other);
}

// TODO(ofrobots): This is dangerous as it allows a call without async
// context. In a semver-major consider disallowing this.
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 @@ -1523,6 +1525,8 @@ class Callback {
handle_.Reset();
}

// TODO(ofrobots): This is dangerous as it allows a call without async
// context. In a semver-major consider disallowing this.
inline v8::Local<v8::Function> GetFunction() const {
return New(handle_);
}
Expand All @@ -1531,7 +1535,7 @@ class Callback {
return handle_.IsEmpty();
}

inline v8::Local<v8::Value>
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,7 @@ class Callback {
#endif
}

inline v8::Local<v8::Value>
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
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

0 comments on commit c82a3af

Please sign in to comment.