From c2c15a1abf634dd2d9669ff3fe3da36c908d54d5 Mon Sep 17 00:00:00 2001
From: Asad Salman <me@asad.co>
Date: Sat, 1 Jul 2017 03:42:43 +0500
Subject: [PATCH] Added Typemaps to handle Callbacks

Removed hacky sed calls for the same purpose

Change-Id: I8d2c475be5d6a076b4be7d2637b7f6345f5daac1
---
 bin/nodejs/Makefile.am        |  2 +-
 bin/nodejs/callback.h         |  8 ++++----
 bin/nodejs/nodejs_interface.i | 17 +++++++++++++----
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/bin/nodejs/Makefile.am b/bin/nodejs/Makefile.am
index 5a25a20e20..ffd5e9c7bd 100644
--- a/bin/nodejs/Makefile.am
+++ b/bin/nodejs/Makefile.am
@@ -3,11 +3,11 @@ include $(top_srcdir)/globals.mk
 BUILT_SOURCES= \
 	ring_wrapper.cpp \
 	build/Makefile \
+	build/Release/dring.node \
 	build/Release/obj.target/dring.node
 
 ring_wrapper.cpp: nodejs_interface.i configurationmanager.i managerimpl.i
 	$(SWIG) -v -c++ -javascript -node -o ring_wrapper.cpp nodejs_interface.i
-	sed -i 's/_wrap_init)/init)/g' ring_wrapper.cpp
 
 build/Makefile: ring_wrapper.cpp binding.gyp
 	node-gyp configure
diff --git a/bin/nodejs/callback.h b/bin/nodejs/callback.h
index 30ef45f3f1..eb564e79d7 100755
--- a/bin/nodejs/callback.h
+++ b/bin/nodejs/callback.h
@@ -59,13 +59,13 @@ void setCallback(const std::string& signal, Local<Function>& func){
     }
 }
 
-void parseCbMap(const Local<Value>& arg){
-    Local<Object> array  = arg->ToObject();
-    Local<Array> props = array->GetOwnPropertyNames();
+void parseCbMap(const Local<Value>& callbackMap){
+    Local<Object> cbAssocArray  = callbackMap->ToObject();
+    Local<Array> props = cbAssocArray->GetOwnPropertyNames();
     for (uint32_t i = 0; i < props->Length(); ++i) {
         const Local<Value> key_local = props->Get(i);
         std::string key = *String::Utf8Value(key_local);
-        Handle<Object> buffer = array->Get(V8_STRING_NEW(key))->ToObject();
+        Handle<Object> buffer = cbAssocArray->Get(V8_STRING_NEW(key))->ToObject();
         Local<Function> func = Local<Function>::Cast(buffer);
         setCallback(key,func);
     }
diff --git a/bin/nodejs/nodejs_interface.i b/bin/nodejs/nodejs_interface.i
index fee5856177..da94ac4911 100644
--- a/bin/nodejs/nodejs_interface.i
+++ b/bin/nodejs/nodejs_interface.i
@@ -106,13 +106,24 @@ namespace std {
 #include "callback.h"
 %}
 
+//typemap for passing Callbacks
+%typemap(in) v8::Local<v8::Function> {
+    $1 = v8::Local<v8::Function>::Cast($input);
+}
+
+//typemap for handling map of functions
+%typemap(in) const v8::Handle<v8::Value>  {
+    $1 = $input;
+}
+
+
 %inline %{
 /* some functions that need to be declared in *_wrap.cpp
  * that are not declared elsewhere in the c++ code
  */
 
-void init(const SwigV8Arguments& args){
-    parseCbMap(args[0]);
+void init(const v8::Handle<v8::Value> &funcMap){
+    parseCbMap(funcMap);
 
     using namespace std::placeholders;
     using std::bind;
@@ -143,8 +154,6 @@ void init(const SwigV8Arguments& args){
 
     DRing::start();
 }
-
-
 %}
 #ifndef SWIG
 /* some bad declarations */
-- 
GitLab