Skip to content

Commit

Permalink
add more Nan::Call overloads (#751)
Browse files Browse the repository at this point in the history
* add more Nan::Call overloads

Presently we use MakeCallback throughout the codebase even in cases
where it doesn't make sense (sync calls). Add some more convenience
overloads for Nan::Call to allow such uses to be migrated over to
Nan::Call instead.

In lieu of coverage, change the Tap reverse bindings to use Nan::Call
to perform synchornous calls. This change exposes a timing issue in
one of the tests – fix it by properly calling Tap's t.end().
  • Loading branch information
ofrobots authored and kkoopa committed Mar 12, 2018
1 parent 19f3027 commit 8584e63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
37 changes: 34 additions & 3 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,32 @@ inline MaybeLocal<v8::Value> Call(
#endif
}

inline MaybeLocal<v8::Value> Call(
v8::Local<v8::String> symbol
, v8::Local<v8::Object> recv
, int argc
, v8::Local<v8::Value> argv[]) {
EscapableHandleScope scope;
v8::Local<v8::Value> fn_v =
Get(recv, symbol).FromMaybe(v8::Local<v8::Value>());
if (fn_v.IsEmpty() || !fn_v->IsFunction()) return v8::Local<v8::Value>();
v8::Local<v8::Function> fn = fn_v.As<v8::Function>();
return scope.Escape(
Call(fn, recv, argc, argv).FromMaybe(v8::Local<v8::Value>()));
}

inline MaybeLocal<v8::Value> Call(
const char* method
, v8::Local<v8::Object> recv
, int argc
, v8::Local<v8::Value> argv[]) {
EscapableHandleScope scope;
v8::Local<v8::String> method_string =
New<v8::String>(method).ToLocalChecked();
return scope.Escape(
Call(method_string, recv, argc, argv).FromMaybe(v8::Local<v8::Value>()));
}

/* abstract */ class AsyncWorker {
public:
explicit AsyncWorker(Callback *callback_,
Expand Down Expand Up @@ -2605,22 +2631,27 @@ struct Tap {
inline void plan(int i) {
HandleScope scope;
v8::Local<v8::Value> arg = New(i);
MakeCallback(New(t_), "plan", 1, &arg);
Call("plan", New(t_), 1, &arg);
}

inline void ok(bool isOk, const char *msg = NULL) {
HandleScope scope;
v8::Local<v8::Value> args[2];
args[0] = New(isOk);
if (msg) args[1] = New(msg).ToLocalChecked();
MakeCallback(New(t_), "ok", msg ? 2 : 1, args);
Call("ok", New(t_), msg ? 2 : 1, args);
}

inline void pass(const char * msg = NULL) {
HandleScope scope;
v8::Local<v8::Value> hmsg;
if (msg) hmsg = New(msg).ToLocalChecked();
MakeCallback(New(t_), "pass", msg ? 1 : 0, &hmsg);
Call("pass", New(t_), msg ? 1 : 0, &hmsg);
}

inline void end() {
HandleScope scope;
Call("end", New(t_), 0, NULL);
}

private:
Expand Down
1 change: 1 addition & 0 deletions test/cpp/threadlocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TlsTest : public AsyncWorker {
t->ok(res[j].ok, res[j].msg);
nauv_key_delete(&tls_key);
t->ok(_(NULL == ErrorMessage()));
t->end();
delete t;
}

Expand Down

0 comments on commit 8584e63

Please sign in to comment.