Skip to content

Commit bd74ef1

Browse files
committed
async_wrap: update utility methods for flexibility
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
1 parent 607316c commit bd74ef1

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/async-wrap-inl.h

+14-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
5050
const v8::Local<v8::String> symbol,
5151
int argc,
5252
v8::Local<v8::Value>* argv) {
53-
v8::Local<v8::Value> cb_v = object()->Get(symbol);
54-
CHECK(cb_v->IsFunction());
53+
v8::Local<v8::Value> cb_v;
54+
if (!object()->Get(object()->CreationContext(), symbol).ToLocal(&cb_v))
55+
return v8::MaybeLocal<v8::Value>();
56+
if (!cb_v->IsFunction()) {
57+
env()->ThrowError("callback must be a function");
58+
return v8::MaybeLocal<v8::Value>();
59+
}
5560
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
5661
}
5762

@@ -60,8 +65,13 @@ inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
6065
uint32_t index,
6166
int argc,
6267
v8::Local<v8::Value>* argv) {
63-
v8::Local<v8::Value> cb_v = object()->Get(index);
64-
CHECK(cb_v->IsFunction());
68+
v8::Local<v8::Value> cb_v;
69+
if (!object()->Get(object()->CreationContext(), index).ToLocal(&cb_v))
70+
return v8::MaybeLocal<v8::Value>();
71+
if (!cb_v->IsFunction()) {
72+
env()->ThrowError("callback must be a function");
73+
return v8::MaybeLocal<v8::Value>();
74+
}
6575
return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
6676
}
6777

0 commit comments

Comments
 (0)