Skip to content

Commit

Permalink
async_wrap: update utility methods for flexibility
Browse files Browse the repository at this point in the history
This makes some of the internal `AsyncWrap::MakeCallback()`
utility wrappers throw rather than crash the process when
the requested method is not present on the `this` object.

Doing so makes it easier for future code to expose C++
objects directly to userland, where JS code can overwrite
or delete such methods.

PR-URL: ayojs#82
  • Loading branch information
addaleax committed Sep 26, 2017
1 parent 607316c commit bd74ef1
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
const v8::Local<v8::String> symbol,
int argc,
v8::Local<v8::Value>* argv) {
v8::Local<v8::Value> cb_v = object()->Get(symbol);
CHECK(cb_v->IsFunction());
v8::Local<v8::Value> cb_v;
if (!object()->Get(object()->CreationContext(), symbol).ToLocal(&cb_v))
return v8::MaybeLocal<v8::Value>();
if (!cb_v->IsFunction()) {
env()->ThrowError("callback must be a function");
return v8::MaybeLocal<v8::Value>();
}
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
}

Expand All @@ -60,8 +65,13 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
uint32_t index,
int argc,
v8::Local<v8::Value>* argv) {
v8::Local<v8::Value> cb_v = object()->Get(index);
CHECK(cb_v->IsFunction());
v8::Local<v8::Value> cb_v;
if (!object()->Get(object()->CreationContext(), index).ToLocal(&cb_v))
return v8::MaybeLocal<v8::Value>();
if (!cb_v->IsFunction()) {
env()->ThrowError("callback must be a function");
return v8::MaybeLocal<v8::Value>();
}
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
}

Expand Down

0 comments on commit bd74ef1

Please sign in to comment.