changeset 562:1b6402f07cd4 Android_Skia

Make root coord availabe for Javascript code
author Thinker K.F. Li <thinker@branda.to>
date Mon, 07 Jun 2010 14:45:01 +0800
parents a3c13c2a4792
children bc207070e3d5
files nodejs/coord.cc nodejs/mbfly_njs.cc nodejs/mbfly_njs.h nodejs/testcase.js
diffstat 4 files changed, 64 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/coord.cc	Mon Jun 07 12:12:03 2010 +0800
+++ b/nodejs/coord.cc	Mon Jun 07 14:45:01 2010 +0800
@@ -71,6 +71,28 @@
     coord_obj_temp->SetInternalFieldCount(1);
 }
 
+static Handle<Object>
+xnjsmb_coord_new_jsobj(coord_t *coord, Handle<Object> parent_obj,
+		       Handle<Object> js_rt) {
+    Handle<Object> coord_obj;
+    static int init_temp = 0;
+    
+    if(!init_temp) {
+	xnjsmb_init_temp();
+	init_temp = 1;
+    }
+
+    coord_obj = coord_obj_temp->NewInstance();
+    ASSERT(coord_obj != NULL);
+    WRAP(coord_obj, coord);
+
+    if(!parent_obj.IsEmpty())
+	SET(coord_obj, "parent", parent_obj);
+    SET(coord_obj, "mbrt", js_rt);
+
+    return coord_obj;
+}
+
 /*! \brief Create a coord object associated with the rdman of the runtime.
  *
  * Two internal fields, coord and rdman.
@@ -84,12 +106,6 @@
     redraw_man_t *rdman;
     coord_t *coord, *parent = NULL;
     int argc;
-    static int init_temp = 0;
-
-    if(!init_temp) {
-	xnjsmb_init_temp();
-	init_temp = 1;
-    }
 
     argc = args.Length();
     if(argc > 1)
@@ -104,19 +120,29 @@
 	parent = (coord_t *)UNWRAP(parent_obj);
     }
     
-    /*
-     * Set Javascript object
-     */
     coord = rdman_coord_new(rdman, parent);
     ASSERT(coord != NULL);
+    coord_obj = xnjsmb_coord_new_jsobj(coord, parent_obj, js_rt);
     
-    coord_obj = coord_obj_temp->NewInstance();
-    ASSERT(coord_obj != NULL);
-    WRAP(coord_obj, coord);
-
-    if(parent != NULL)
-	SET(coord_obj, "parent", parent_obj);
-    SET(coord_obj, "mbrt", js_rt);
-
     return coord_obj;
 }
+
+/*! \brief Initialize Javascript object for root coord of a runtime.
+ *
+ * \param js_rt is the runtime object to create the root object for.
+ *
+ * After the function, js_rt.root is the object for root coord in
+ * Javascript.
+ */
+void
+xnjsmb_coord_mkroot(Handle<Object> js_rt) {
+    redraw_man_t *rdman;
+    coord_t *root;
+    Handle<Object> obj;
+    
+    rdman = xnjsmb_rt_rdman(js_rt);
+    root = rdman_get_root(rdman);
+    obj = xnjsmb_coord_new_jsobj(root, Handle<Object>(NULL), js_rt);
+
+    SET(js_rt, "root", obj);
+}
--- a/nodejs/mbfly_njs.cc	Mon Jun 07 12:12:03 2010 +0800
+++ b/nodejs/mbfly_njs.cc	Mon Jun 07 14:45:01 2010 +0800
@@ -50,6 +50,7 @@
 
     self = args.This();
     WRAP(self, rt);
+    xnjsmb_coord_mkroot(self);
     
     X_njs_MB_init_handle_connection(rt);
 
@@ -62,6 +63,8 @@
 
 /* @} */
 
+/*! \brief Get rdman associated with the runtime.
+ */
 redraw_man_t *
 xnjsmb_rt_rdman(Handle<Object> mbrt) {
     HandleScope scope;
@@ -84,17 +87,24 @@
 extern "C" void
 init(Handle<Object> target) {
     HandleScope scope;
-    Handle<FunctionTemplate> func;
-    Handle<ObjectTemplate> rt_obj_temp;
+    Handle<FunctionTemplate> func, mb_rt_func;
+    Handle<ObjectTemplate> rt_instance_temp, rt_proto_temp;
 
     func = FunctionTemplate::New(hello_func);
     target->Set(String::New("Hello"), func->GetFunction());
 
-    func = FunctionTemplate::New(xnjsmb_new);
-    target->Set(String::New("mb_rt"), func->GetFunction());
-    rt_obj_temp = func->PrototypeTemplate();
-    rt_obj_temp->SetInternalFieldCount(1);
+    /*
+     * Initialize template for MadButterfly runtime objects.
+     */
+    mb_rt_func = FunctionTemplate::New(xnjsmb_new);
+    mb_rt_func->SetClassName(String::New("mb_rt"));
     
+    rt_instance_temp = mb_rt_func->InstanceTemplate();
+    rt_instance_temp->SetInternalFieldCount(1);
+    
+    rt_proto_temp = mb_rt_func->PrototypeTemplate();
     func = FunctionTemplate::New(xnjsmb_coord_new);
-    SET(rt_obj_temp, "coord_new", func);
+    SET(rt_proto_temp, "coord_new", func);
+    
+    target->Set(String::New("mb_rt"), mb_rt_func->GetFunction());    
 }
--- a/nodejs/mbfly_njs.h	Mon Jun 07 12:12:03 2010 +0800
+++ b/nodejs/mbfly_njs.h	Mon Jun 07 14:45:01 2010 +0800
@@ -18,6 +18,9 @@
 #define GET(o, n) (o)->Get(v8::String::New(n))
 
 redraw_man_t *xnjsmb_rt_rdman(v8::Handle<v8::Object> mbrt);
+
+/* From coord.cc */
 v8::Handle<v8::Value> xnjsmb_coord_new(const v8::Arguments &args);
+void xnjsmb_coord_mkroot(v8::Handle<v8::Object> js_rt);
 
 #endif /* __MBFLY_NJS_H_ */
--- a/nodejs/testcase.js	Mon Jun 07 12:12:03 2010 +0800
+++ b/nodejs/testcase.js	Mon Jun 07 14:45:01 2010 +0800
@@ -4,5 +4,6 @@
 sys.puts(r);
 
 var mb_rt = new mbfly.mb_rt(":0.0", 300, 200);
+sys.puts(mb_rt.root);
 
 setTimeout(function() { sys.puts("timeout"); }, 1000);