From 06e148c2d7d5be9665f88d96454c3508e21719a0 Mon Sep 17 00:00:00 2001 From: raub Date: Mon, 14 May 2018 11:08:24 +0300 Subject: [PATCH] :art: Experimental new CPP on() --- include/event-emitter.hpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/include/event-emitter.hpp b/include/event-emitter.hpp index 3931d02..9325765 100644 --- a/include/event-emitter.hpp +++ b/include/event-emitter.hpp @@ -122,21 +122,19 @@ public: // C++ side on() method - void on(const std::string &name, V8_VAR_VAL that, const std::string &method) { + void on(const std::string &name, V8_VAR_FUNC cb) { - V8_VAR_STR code = JS_STR( - "((emitter, name, that, method) => emitter.on(name, that[method]))" - ); + V8_VAR_OBJ me = handle(); - V8_VAR_FUNC connector = V8_VAR_FUNC::Cast(v8::Script::Compile(code)->Run()); - Nan::Callback connectorCb(connector); + V8_VAR_VAL onVal = Nan::Get(me, JS_STR("on")).ToLocalChecked(); + V8_VAR_FUNC onFunc = V8_VAR_FUNC::Cast(onVal); - V8_VAR_OBJ emitter = Nan::New(); - this->Wrap(emitter); + Nan::Callback onCb(onFunc); - V8_VAR_VAL argv[] = { emitter, JS_STR(name.c_str()), that, JS_STR(method.c_str()) }; + V8_VAR_VAL argv[] = { JS_STR(name.c_str()), cb }; Nan::AsyncResource async("EventEmitter::cpp_on()"); - connectorCb.Call(4, argv, &async); + + onCb.Call(me, 2, argv, &async); } @@ -214,7 +212,7 @@ private: REQ_OBJ_ARG(0, event); - if (!event->Has(JS_STR("type"))) { + if ( ! event->Has(JS_STR("type")) ) { return Nan::ThrowError("Event must have the `type` property."); } @@ -307,6 +305,7 @@ private: } + static inline void _reportListeners( const std::string &name, int numListeners, @@ -325,9 +324,7 @@ private: V8_VAR_STR code = JS_STR( "(new Error()).stack.split('\\n').slice(2).join('\\n')" ); - V8_VAR_STR stack = V8_VAR_STR::Cast( - v8::Script::Compile(code)->Run() - ); + V8_VAR_STR stack = V8_VAR_STR::Cast(v8::Script::Compile(code)->Run()); Nan::Utf8String stackStr(stack); msg += *stackStr; @@ -335,6 +332,7 @@ private: } + static inline void _addListener( const Nan::FunctionCallbackInfo &info, const std::string &name,