From 44432660eae01defa807837c58b37db9f9ce00fd Mon Sep 17 00:00:00 2001 From: blagoev Date: Sun, 7 May 2017 02:02:05 +0300 Subject: [PATCH] Add MakeCallback method abstraction Needed for node to kick next tick properly. JSC uses regular call --- src/js_types.hpp | 1 + src/jsc/jsc_function.hpp | 5 +++++ src/node/node_function.hpp | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/js_types.hpp b/src/js_types.hpp index 8a5a10be..1c6a3aec 100644 --- a/src/js_types.hpp +++ b/src/js_types.hpp @@ -151,6 +151,7 @@ struct Function { using ObjectType = typename T::Object; using ValueType = typename T::Value; + static ValueType callback(ContextType, const FunctionType &, const ObjectType &, size_t, const ValueType[]); static ValueType call(ContextType, const FunctionType &, const ObjectType &, size_t, const ValueType[]); template static ValueType call(ContextType ctx, const FunctionType &function, const ObjectType &this_object, const ValueType (&arguments)[N]) diff --git a/src/jsc/jsc_function.hpp b/src/jsc/jsc_function.hpp index 866c973f..da3f2a3d 100644 --- a/src/jsc/jsc_function.hpp +++ b/src/jsc/jsc_function.hpp @@ -33,6 +33,11 @@ inline JSValueRef jsc::Function::call(JSContextRef ctx, const JSObjectRef &funct return result; } +template<> +inline JSValueRef jsc::Function::callback(JSContextRef ctx, const JSObjectRef &function, const JSObjectRef &this_object, size_t argc, const JSValueRef arguments[]) { + return jsc::Function::call(ctx, function, this_object, argc, arguments); +} + template<> inline JSObjectRef jsc::Function::construct(JSContextRef ctx, const JSObjectRef &function, size_t argc, const JSValueRef arguments[]) { JSValueRef exception = nullptr; diff --git a/src/node/node_function.hpp b/src/node/node_function.hpp index 39e90720..cb07959a 100644 --- a/src/node/node_function.hpp +++ b/src/node/node_function.hpp @@ -36,6 +36,19 @@ inline v8::Local node::Function::call(v8::Isolate* isolate, const v8: return result.ToLocalChecked(); } +template<> +inline v8::Local node::Function::callback(v8::Isolate* isolate, const v8::Local &function, const v8::Local &this_object, size_t argc, const v8::Local arguments[]) { + Nan::TryCatch trycatch; + + auto recv = this_object.IsEmpty() ? isolate->GetCurrentContext()->Global() : this_object; + auto result = Nan::MakeCallback(recv, function, (int)argc, const_cast*>(arguments)); + + if (trycatch.HasCaught()) { + throw node::Exception(isolate, trycatch.Exception()); + } + return result; +} + template<> inline v8::Local node::Function::construct(v8::Isolate* isolate, const v8::Local &function, size_t argc, const v8::Local arguments[]) { Nan::TryCatch trycatch;