changeset 661:90c7726bc953

Replace part code of coord.cc by the code generated by generator
author Thinker K.F. Li <thinker@branda.to>
date Sun, 01 Aug 2010 20:18:59 +0800
parents dde4e3cc9529
children 689e15edbf72
files nodejs/coord.cc nodejs/coord.m4 nodejs/wscript
diffstat 3 files changed, 57 insertions(+), 106 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/coord.cc	Sun Aug 01 20:17:39 2010 +0800
+++ b/nodejs/coord.cc	Sun Aug 01 20:18:59 2010 +0800
@@ -16,126 +16,52 @@
 
 using namespace v8;
 
-static Handle<Value>
-xnjsmb_coord_get_index(uint32_t index, const AccessorInfo &info) {
-    HandleScope scope;
-    Handle<Object> self;
-    coord_t *coord;
-    co_aix v;
+static float
+coord_get_index(coord_t *coord, Handle<Object> self, int idx,
+		const char **err) {
+    if(idx < 0 || idx >= 6) {
+        *err = "Invalid index: out of range";
+        return 0;
+    }
 
-    if(index < 0 || index >= 6)
-	THROW("Invalid index");
-    
-    self = info.This();
-    coord = (coord_t *)UNWRAP(self);
-    v = coord_get_matrix(coord)[index];
-
-    return Number::New(v);
+    return coord_get_matrix(coord)[idx];
 }
 
-static Handle<Value>
-xnjsmb_coord_set_index(uint32_t index, Local<Value> value,
-		       const AccessorInfo &info) {
-    
-    HandleScope scope;
-    Handle<Object> self;
+static float
+coord_set_index(coord_t *coord, Handle<Object> self,
+		int idx, float v, const char **err) {
     Handle<Object> js_rt;
     redraw_man_t *rdman;
-    coord_t *coord;
-    co_aix v;
+    
+    if(idx < 0 || idx >= 6) {
+        *err = "Invalid index: out of range";
+        return 0;
+    }
 
-    if(index < 0 || index >= 6)
-	THROW("Invalid Index");
-    if(!value->IsNumber())
-	THROW("Invalid value");
-
-    self = info.This();
-    coord = (coord_t *)UNWRAP(self);
-    v = value->NumberValue();
-    coord_get_matrix(coord)[index] = v;
-
+    coord_get_matrix(coord)[idx] = v;
+    
     js_rt = GET(self, "mbrt")->ToObject();
     rdman = xnjsmb_rt_rdman(js_rt);
     rdman_coord_changed(rdman, coord);
 
-    return value;
+    return v;
 }
 
-/*! \brief Callback functio to add a shape to a coord in Javascript.
- *
- * coord.add_shape(shape)
- */
-static Handle<Value>
-xnjsmb_coord_add_shape(const Arguments &args) {
-    int argc = args.Length();
-    Handle<Object> self = args.This();
-    Handle<Object> shape_obj;
-    Handle<Object> rt_obj;
-    Handle<Value> rt_val;
+static void
+xnjsmb_coord_add_shape(coord_t *coord, Handle<Object> self,
+			shape_t *shape, const char **err) {
+    Handle<Object> js_rt;
     redraw_man_t *rdman;
-    coord_t *coord;
-    shape_t *sh;
     int r;
-
-    if(argc != 1)
-	THROW("Invalid number of arguments (!= 1)");
     
-    shape_obj = args[0]->ToObject();
-    sh = (shape_t *)UNWRAP(shape_obj);
-    ASSERT(sh != NULL);
-    
-    coord = (coord_t *)UNWRAP(self);
-    ASSERT(coord != NULL);
-
-    rt_val = GET(self, "mbrt");
-    rt_obj = rt_val->ToObject();
-    rdman = xnjsmb_rt_rdman(rt_obj);
-    
-    r = rdman_add_shape(rdman, sh, coord);
+    js_rt = GET(self, "mbrt")->ToObject();
+    rdman = xnjsmb_rt_rdman(js_rt);
+    r = rdman_add_shape(rdman, shape, coord);
     if(r != 0)
-	THROW("Unknown error");
-
-    return Null();
+	*err = "Unknown error";
 }
 
-static Persistent<ObjectTemplate> coord_obj_temp;
-
-static void
-xnjsmb_init_temp(void) {
-    Handle<FunctionTemplate> add_shape_temp;
-    
-    coord_obj_temp = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
-    coord_obj_temp->SetIndexedPropertyHandler(xnjsmb_coord_get_index,
-					      xnjsmb_coord_set_index);
-    coord_obj_temp->SetInternalFieldCount(1);
-
-    add_shape_temp = FunctionTemplate::New(xnjsmb_coord_add_shape);
-    SET(coord_obj_temp, "add_shape", add_shape_temp);
-}
-
-/*! \brief Create and initialize a Javascript object for a coord.
- */
-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;
-}
+#include "coord-inc.h"
 
 /*! \brief Create a coord object associated with the rdman of the runtime.
  *
@@ -166,7 +92,11 @@
     
     coord = rdman_coord_new(rdman, parent);
     ASSERT(coord != NULL);
-    coord_obj = xnjsmb_coord_new_jsobj(coord, parent_obj, js_rt);
+    
+    coord_obj = xnjsmb_auto_coord_new(coord).As<Object>();
+    if(!parent_obj.IsEmpty())
+	SET(coord_obj, "parent", parent_obj);
+    SET(coord_obj, "mbrt", js_rt);
 
     scope.Close(coord_obj);
     
@@ -185,10 +115,17 @@
     redraw_man_t *rdman;
     coord_t *root;
     Handle<Object> obj;
+    static int init_flag = 0;
+
+    if(!init_flag) {
+	xnjsmb_auto_coord_init();
+	init_flag = 1;
+    }
     
     rdman = xnjsmb_rt_rdman(js_rt);
     root = rdman_get_root(rdman);
-    obj = xnjsmb_coord_new_jsobj(root, Handle<Object>(NULL), js_rt);
+    obj = xnjsmb_auto_coord_new(root).As<Object>();
+    SET(obj, "mbrt", js_rt);
 
     SET(js_rt, "root", obj);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/coord.m4	Sun Aug 01 20:18:59 2010 +0800
@@ -0,0 +1,6 @@
+define([PROJ_PREFIX], [xnjsmb_auto_])dnl
+STRUCT([coord], [coord_t], [],
+	[METHOD([add_shape], [xnjsmb_coord_add_shape],
+		(SELF, OBJ([shape], [shape_t]), ERR), 1, [])],
+	((GET_INDEX, (coord_get_index, NUMBER)),
+	 (SET_INDEX, (coord_set_index, NUMBER))))
--- a/nodejs/wscript	Sun Aug 01 20:17:39 2010 +0800
+++ b/nodejs/wscript	Sun Aug 01 20:18:59 2010 +0800
@@ -24,12 +24,15 @@
     conf(rule='m4 -I ${SRCDIR}/../tools gen_v8_binding.m4 ${SRC} > ${TGT}',
          source='observer.m4', target='observer-inc.h',
          name='observer-inc', shell=True, always=True, before=['cxx'])
+    conf(rule='m4 -I ${SRCDIR}/../tools gen_v8_binding.m4 ${SRC} > ${TGT}',
+         source='coord.m4', target='coord-inc.h',
+         name='coord-inc', shell=True, always=True, before=['cxx'])
 
     obj = conf.new_task_gen('cxx', 'shlib', 'node_addon')
     obj.target = 'mbfly'
-    obj.source = 'mbfly_njs.cc coord.cc shapes.cc paints.cc font.cc ' + \
+    obj.source = 'mbfly_njs.cc shapes.cc paints.cc font.cc ' + \
 	'image_ldr.cc'
-    obj.add_objects = 'X_supp_njs.o observer.o'
+    obj.add_objects = 'X_supp_njs.o observer.o coord.o'
     obj.staticlib = 'mbfly'
 
     obj = conf.new_task_gen('cc', 'shlib', 'node_addon')
@@ -40,4 +43,9 @@
     obj.target = 'observer.o'
     obj.source = 'observer.cc'
     obj.includes = '.'
+    
+    obj = conf.new_task_gen('cxx', 'shlib', 'node_addon')
+    obj.target = 'coord.o'
+    obj.source = 'coord.cc'
+    obj.includes = '.'
     pass