changeset 675:c643af2095c5

Keep and retrieve respective js object to/from property store
author Thinker K.F. Li <thinker@branda.to>
date Fri, 06 Aug 2010 00:56:26 +0800
parents 5c8387fd123e
children f264b50c469c
files nodejs/mbfly_njs.cc nodejs/mbfly_njs.h nodejs/observer.cc nodejs/observer.m4 nodejs/shapes.cc
diffstat 5 files changed, 68 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/mbfly_njs.cc	Fri Aug 06 00:50:40 2010 +0800
+++ b/nodejs/mbfly_njs.cc	Fri Aug 06 00:56:26 2010 +0800
@@ -28,11 +28,16 @@
 static void
 xnjsmb_coord_mod(Handle<Object> mbrt, Handle<Value> ret) {
     Handle<Object> ret_obj = ret->ToObject();
+    Persistent<Object> *ret_obj_hdl;
     coord_t *coord;
 
     SET(ret_obj, "mbrt", mbrt);
     coord = (coord_t *)UNWRAP(ret_obj);
-    mb_prop_set(&coord->obj.props, PROP_JSOBJ, *ret_obj);
+    /* Keep associated js object in property store for retrieving,
+     * later, without create new js object.
+     */
+    ret_obj_hdl = new Persistent<Object>(ret_obj);
+    mb_prop_set(&coord->obj.props, PROP_JSOBJ, ret_obj_hdl);
 }
 
 #define xnjsmb_auto_coord_new export_xnjsmb_auto_coord_new
--- a/nodejs/mbfly_njs.h	Fri Aug 06 00:50:40 2010 +0800
+++ b/nodejs/mbfly_njs.h	Fri Aug 06 00:56:26 2010 +0800
@@ -12,6 +12,12 @@
 	exc = v8::Exception::Error(v8::String::New(x));		\
 	return v8::ThrowException(exc);				\
     } while(0)
+#define THROW_noret(x)						\
+    do {							\
+	v8::Handle<v8::Value> exc;				\
+	exc = v8::Exception::Error(v8::String::New(x));		\
+	v8::ThrowException(exc);				\
+    } while(0)
 #define UNWRAP(o) v8::External::Unwrap((o)->GetInternalField(0))
 #define WRAP(o, v) (o)->SetInternalField(0, v8::External::Wrap(v))
 #define SET(o, n, v) (o)->Set(v8::String::New(n), v)
--- a/nodejs/observer.cc	Fri Aug 06 00:50:40 2010 +0800
+++ b/nodejs/observer.cc	Fri Aug 06 00:56:26 2010 +0800
@@ -45,6 +45,39 @@
     delete data;
 }
 
+static Handle<Value>
+xnjsmb_event_tgt_getter(Handle<Object> self, event_t *evt, const char **err) {
+    Persistent<Object> *hdl;
+    
+    hdl = (Persistent<Object> *)
+	mb_prop_get(&((mb_obj_t *)evt->tgt)->props,
+		    PROP_JSOBJ);
+    return Local<Object>::New(*hdl);
+}
+
+static void
+xnjsmb_event_tgt_setter(Handle<Object> self, event_t *evt,
+			Handle<Value> value, const char **err) {
+    *err = "Not implemented";
+}
+
+static Handle<Value>
+xnjsmb_event_cur_tgt_getter(Handle<Object> self, event_t *evt,
+			    const char **err) {
+    Persistent<Object> *hdl;
+    
+    hdl = (Persistent<Object> *)
+	mb_prop_get(&((mb_obj_t *)evt->cur_tgt)->props,
+		    PROP_JSOBJ);
+    return Local<Object>::New(*hdl);
+}
+
+static void
+xnjsmb_event_cur_tgt_setter(Handle<Object> self, event_t *evt,
+			    Handle<Value> value, const char **err) {
+    *err = "Not implemented";
+}
+
 /* This is the part of the code generated by gen_v8_binding.m4 */
 #include "observer-inc.h"
 
--- a/nodejs/observer.m4	Fri Aug 06 00:50:40 2010 +0800
+++ b/nodejs/observer.m4	Fri Aug 06 00:56:26 2010 +0800
@@ -11,5 +11,8 @@
 	       (OBJ([observer], [observer], [observer_t])), 1, [])])
 
 STRUCT([event], [event_t],
-       [INT([type]), OBJ([tgt], [subject], [subject_t]),
-        OBJ([cur_tgt], [subject], [subject_t]), INT([flags])], [])
+       [INT([type]),
+        ACCESSOR([tgt], [xnjsmb_event_tgt_getter], [xnjsmb_event_tgt_setter]),
+        ACCESSOR([cur_tgt], [xnjsmb_event_cur_tgt_getter],
+	    [xnjsmb_event_cur_tgt_setter]),
+	INT([flags])], [])
--- a/nodejs/shapes.cc	Fri Aug 06 00:50:40 2010 +0800
+++ b/nodejs/shapes.cc	Fri Aug 06 00:56:26 2010 +0800
@@ -125,6 +125,7 @@
     redraw_man_t *rdman;
     Handle<Object> self = args.This(); // path object
     Handle<Object> rt;
+    Persistent<Object> *self_hdl;
     char *dstr;
     int argc;
 
@@ -145,7 +146,11 @@
 
     WRAP(self, sh);
     SET(self, "mbrt", rt);
-    mb_prop_set(&sh->obj.props, PROP_JSOBJ, *self);
+    /* Keep associated js object in property store for retrieving,
+     * later, without create new js object.
+     */
+    self_hdl = new Persistent<Object>(self);
+    mb_prop_set(&sh->obj.props, PROP_JSOBJ, self_hdl);
 
     return Null();
 }
@@ -212,6 +217,7 @@
     int argc = args.Length();
     Handle<Object> self = args.This();
     Handle<Object> rt;
+    Persistent<Object> *self_hdl;
     float x, y;
     char *data;
     redraw_man_t *rdman;
@@ -236,7 +242,11 @@
 
     WRAP(self, stext);
     SET(self, "mbrt", rt);
-    mb_prop_set(&stext->obj.props, PROP_JSOBJ, *self);
+    /* Keep associated js object in property store for retrieving,
+     * later, without create new js object.
+     */
+    self_hdl = new Persistent<Object>(self);
+    mb_prop_set(&stext->obj.props, PROP_JSOBJ, self_hdl);
 
     return Null();
 }
@@ -349,6 +359,7 @@
     int argc = args.Length();
     Handle<Object> self = args.This();
     Handle<Object> rt;
+    Persistent<Object> *self_hdl;
     float x, y;
     float w, h;
     redraw_man_t *rdman;
@@ -372,7 +383,11 @@
 
     WRAP(self, img);
     SET(self, "mbrt", rt);
-    mb_prop_set(&img->obj.props, PROP_JSOBJ, *self);
+    /* Keep associated js object in property store for retrieving,
+     * later, without create new js object.
+     */
+    self_hdl = new Persistent<Object>(self);
+    mb_prop_set(&img->obj.props, PROP_JSOBJ, self_hdl);
 
     return Null();
 }