From bd74ef18f83ed6916d3a6c537cc70a8eec0ca07b Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 10 Sep 2017 05:06:34 +0200 Subject: [PATCH] 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: https://github.com/ayojs/ayo/pull/82 --- src/async-wrap-inl.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/async-wrap-inl.h b/src/async-wrap-inl.h index ed5a0c0d27..4fcfe2e757 100644 --- a/src/async-wrap-inl.h +++ b/src/async-wrap-inl.h @@ -50,8 +50,13 @@ inline v8::MaybeLocal AsyncWrap::MakeCallback( const v8::Local symbol, int argc, v8::Local* argv) { - v8::Local cb_v = object()->Get(symbol); - CHECK(cb_v->IsFunction()); + v8::Local cb_v; + if (!object()->Get(object()->CreationContext(), symbol).ToLocal(&cb_v)) + return v8::MaybeLocal(); + if (!cb_v->IsFunction()) { + env()->ThrowError("callback must be a function"); + return v8::MaybeLocal(); + } return MakeCallback(cb_v.As(), argc, argv); } @@ -60,8 +65,13 @@ inline v8::MaybeLocal AsyncWrap::MakeCallback( uint32_t index, int argc, v8::Local* argv) { - v8::Local cb_v = object()->Get(index); - CHECK(cb_v->IsFunction()); + v8::Local cb_v; + if (!object()->Get(object()->CreationContext(), index).ToLocal(&cb_v)) + return v8::MaybeLocal(); + if (!cb_v->IsFunction()) { + env()->ThrowError("callback must be a function"); + return v8::MaybeLocal(); + } return MakeCallback(cb_v.As(), argc, argv); }