changeset 687:da12923a789a

Migrate paints.cc to use gen_v8_binding.m4
author Thinker K.F. Li <thinker@branda.to>
date Sat, 07 Aug 2010 21:37:57 +0800
parents ddce24029561
children cadebc13be2f
files nodejs/mbfly_njs.cc nodejs/mbfly_njs.h nodejs/mbfly_njs.m4 nodejs/paints.cc nodejs/paints.m4 nodejs/shapes.cc nodejs/wscript
diffstat 7 files changed, 122 insertions(+), 242 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/mbfly_njs.cc	Sat Aug 07 19:43:17 2010 +0800
+++ b/nodejs/mbfly_njs.cc	Sat Aug 07 21:37:57 2010 +0800
@@ -82,9 +82,17 @@
  * @{
  */
 
+/*
+ * Redirect following function to respective exported version from
+ * other modules.  Since gen_v8_binding.m4 make all functions static,
+ * we need a exported version to call them indrectly from other
+ * modules.
+ */
 #define xnjsmb_auto_path_new export_xnjsmb_auto_path_new
 #define xnjsmb_auto_stext_new export_xnjsmb_auto_stext_new
 #define xnjsmb_auto_image_new export_xnjsmb_auto_image_new
+#define xnjsmb_auto_paint_color_new export_xnjsmb_auto_paint_color_new
+#define xnjsmb_auto_paint_image_new export_xnjsmb_auto_paint_image_new
 
 #include "mbfly_njs-inc.h"
 
--- a/nodejs/mbfly_njs.h	Sat Aug 07 19:43:17 2010 +0800
+++ b/nodejs/mbfly_njs.h	Sat Aug 07 21:37:57 2010 +0800
@@ -44,6 +44,13 @@
 
 /* From paints.cc */
 void xnjsmb_paints_init_mb_rt_temp(v8::Handle<v8::FunctionTemplate> rt_temp);
+paint_t *xnjsmb_paint_color_new(njs_runtime_t *rt,
+				float r, float g, float b, float a,
+				const char **err);
+paint_t *xnjsmb_paint_image_new(njs_runtime_t *rt, mb_img_data_t *img,
+				const char **err);
+v8::Handle<v8::Value> export_xnjsmb_auto_paint_color_new(paint_t *paint);
+v8::Handle<v8::Value> export_xnjsmb_auto_paint_image_new(paint_t *paint);
 
 /* From font.cc */
 void xnjsmb_font_init_mb_rt_temp(v8::Handle<v8::FunctionTemplate> mb_rt_temp);
--- a/nodejs/mbfly_njs.m4	Sat Aug 07 19:43:17 2010 +0800
+++ b/nodejs/mbfly_njs.m4	Sat Aug 07 21:37:57 2010 +0800
@@ -17,6 +17,14 @@
 	METHOD([image_new], [xnjsmb_image_new],
 	       (NUMBER(x), NUMBER(y), NUMBER(w), NUMBER(h)), 4,
 	       [OBJ([image], [shape_t])],
+	       (([MOD], [xnjsmb_mb_rt_objs_mod]))),
+	METHOD([paint_color_new], [xnjsmb_paint_color_new],
+	       (NUMBER(r), NUMBER(g), NUMBER(b), NUMBER(a), ERR), 4,
+	       [OBJ([paint_color], [paint_t])],
+	       (([MOD], [xnjsmb_mb_rt_objs_mod]))),
+	METHOD([paint_image_new], [xnjsmb_paint_image_new],
+	       (OBJ([img], [img_data], [mb_img_data_t]), ERR), 1,
+	       [OBJ([paint_image], [paint_t])],
 	       (([MOD], [xnjsmb_mb_rt_objs_mod])))],
 	((CTOR, ([_X_njs_MB_new], (SELF, STR(display_name), INT(width), INT(height)), 3)))dnl
 )
--- a/nodejs/paints.cc	Sat Aug 07 19:43:17 2010 +0800
+++ b/nodejs/paints.cc	Sat Aug 07 21:37:57 2010 +0800
@@ -18,261 +18,104 @@
  * @{
  */
 
-/*! \brief Fill a shape with the paint.
- */
-static Handle<Value>
-xnjsmb_paint_fill(const Arguments &args) {
-    int argc = args.Length();
-    Handle<Object> self = args.This();
-    Handle<Object> sh_obj;
-    Handle<Object> rt;
-    Handle<Value> rt_val;
-    paint_t *paint;
-    shape_t *sh;
+static void
+xnjsmb_paint_fill(paint_t *paint, Handle<Object> self, shape_t *sh) {
+    Handle<Value> rt_v;
+    Handle<Object> rt_o;
     redraw_man_t *rdman;
 
-    if(argc != 1)
-	THROW("Invalid number of arguments (!= 1)");
-    if(!args[0]->IsObject())
-	THROW("Invalid argument type (shape)");
-
-    paint = (paint_t *)UNWRAP(self);
-    
-    sh_obj = args[0]->ToObject();
-    sh = (shape_t *)UNWRAP(sh_obj);
-
-    rt_val = GET(self, "mbrt");
-    rt = rt_val->ToObject();
-    rdman = xnjsmb_rt_rdman(rt);
+    rt_v = GET(self, "mbrt");
+    rt_o = rt_v->ToObject();
+    rdman = xnjsmb_rt_rdman(rt_o);
     
     rdman_paint_fill(rdman, paint, sh);
     
     if(sh_get_coord(sh))
 	rdman_shape_changed(rdman, sh);
-    
-    return Null();
 }
 
-/*! \brief Stroke a shape with the paint.
- */
-static Handle<Value>
-xnjsmb_paint_stroke(const Arguments &args) {
-    int argc = args.Length();
-    Handle<Object> self = args.This();
-    Handle<Object> sh_obj;
-    Handle<Object> rt;
-    Handle<Value> rt_val, sh_val;
-    paint_t *paint;
-    shape_t *sh;
+static void
+xnjsmb_paint_stroke(paint_t *paint, Handle<Object> self, shape_t *sh) {
+    Handle<Value> rt_v;
+    Handle<Object> rt_o;
     redraw_man_t *rdman;
 
-    if(argc != 1)
-	THROW("Invalid number of arguments (!= 1)");
-    if(!args[0]->IsObject())
-	THROW("Invalid argument type (shape)");
-
-    paint = (paint_t *)UNWRAP(self);
-    
-    sh_val = args[0];
-    sh_obj = sh_val->ToObject();
-    sh = (shape_t *)UNWRAP(sh_obj);
-
-    rt_val = GET(self, "mbrt");
-    rt = rt_val->ToObject();
-    rdman = xnjsmb_rt_rdman(rt);
+    rt_v = GET(self, "mbrt");
+    rt_o = rt_v->ToObject();
+    rdman = xnjsmb_rt_rdman(rt_o);
     
     rdman_paint_stroke(rdman, paint, sh);
     
     if(sh_get_coord(sh))
 	rdman_shape_changed(rdman, sh);
-    
-    return Null();
 }
 
-/*! \brief Constructor of color paint_color_t object for Javascript.
- */
-static Handle<Value>
-xnjsmb_paint_color(const Arguments &args) {
-    int argc = args.Length();
-    Handle<Object> self = args.This();
-    Handle<Object> rt;
-    redraw_man_t *rdman;
-    paint_t *paint;
-    float r, g, b, a;
-
-    if(argc != 5)
-	THROW("Invalid number of arguments (!= 5)");
-    if(!args[0]->IsObject() || !args[1]->IsNumber() ||
-       !args[2]->IsNumber() || !args[3]->IsNumber() ||
-       !args[4]->IsNumber())
-	THROW("Invalid argument type");
-
-    rt = args[0]->ToObject();
-    r = args[1]->ToNumber()->Value();
-    g = args[2]->ToNumber()->Value();
-    b = args[3]->ToNumber()->Value();
-    a = args[4]->ToNumber()->Value();
-
-    rdman = xnjsmb_rt_rdman(rt);
-    paint = rdman_paint_color_new(rdman, r, g, b, a);
-    ASSERT(sh != NULL);
-
-    WRAP(self, paint);
-    SET(self, "mbrt", rt);
+#include "paints-inc.h"
 
-    return Null();
-}
-
-static Persistent<FunctionTemplate> xnjsmb_paint_temp;
-static Persistent<FunctionTemplate> xnjsmb_paint_color_temp;
-
-/*! \brief Create and return a paint_color object.
+/*! \defgroup xnjsmb_paints_cons Constructor of paints
+ *
+ * @{
  */
-static Handle<Value>
-xnjsmb_paint_color_new(const Arguments &args) {
-    HandleScope scope;
-    Handle<Object> rt = args.This();
-    Handle<Object> paint_color_obj;
-    Handle<Function> paint_color_func;
-    Handle<Value> pc_args[5];
-    int argc;
-    int i;
+paint_t *
+xnjsmb_paint_color_new(njs_runtime_t *rt,
+		       float r, float g, float b, float a,
+		       const char **err) {
+    paint_t *paint;
+    redraw_man_t *rdman;
 
-    argc = args.Length();
-    if(argc != 4)
-	THROW("Invalid number of arguments (r, g, b, a)");
-    for(i = 0; i < 4; i++)
-	if(!args[i]->IsNumber())
-	    THROW("Invalid argument type");
-    
-    pc_args[0] = rt;
-    pc_args[1] = args[0];	// r
-    pc_args[2] = args[1];	// g
-    pc_args[3] = args[2];	// b
-    pc_args[4] = args[3];	// a
-    paint_color_func = xnjsmb_paint_color_temp->GetFunction();
-    paint_color_obj = paint_color_func->NewInstance(5, pc_args);
+    rdman = X_njs_MB_rdman(rt);
+    paint = rdman_paint_color_new(rdman, r, g, b, a);
+    if(paint == NULL) {
+	*err = "can not allocate a paint_color_t";
+	return NULL;
+    }
 
-    scope.Close(paint_color_obj);
-    return paint_color_obj;
+    return paint;
 }
 
-static Persistent<FunctionTemplate> xnjsmb_paint_color_new_temp;
-
-/*! \brief Constructor of paint_image_t objects for Javascript.
- */
-static Handle<Value>
-xnjsmb_paint_image(const Arguments &args) {
-    int argc = args.Length();
-    Handle<Object> rt;
-    Handle<Object> self = args.This();
-    Handle<Object> img_obj;
+paint_t *
+xnjsmb_paint_image_new(njs_runtime_t *rt, mb_img_data_t *img,
+		       const char **err) {
+    paint_t *paint;
     redraw_man_t *rdman;
-    mb_img_data_t *img;
-    paint_t *paint;
 
-    if(argc != 2)
-	THROW("Invalid number of arguments (!= 2)");
-    if(!args[0]->IsObject() || !args[1]->IsObject())
-	THROW("Invalid argument type");
-    
-    rt = args[0]->ToObject();
-    img_obj = args[1]->ToObject();
-    
-    rdman = xnjsmb_rt_rdman(rt);
-    img = (mb_img_data_t *)UNWRAP(img_obj);
+    rdman = X_njs_MB_rdman(rt);
+    paint = rdman_paint_image_new(rdman, img);
+    if(paint == NULL) {
+	*err = "can not allocate a paint_image_t";
+	return NULL;
+    }
 
-    paint = rdman_paint_image_new(rdman, img);
-    ASSERT(paint != NULL);
-    
-    WRAP(self, paint);
-    SET(self, "mbrt", rt);
-    
-    return Null();
+    return paint;
 }
 
-static Persistent<FunctionTemplate> xnjsmb_paint_image_temp;
-
-/*! \brief Create and return a paint_image object.
- */
-static Handle<Value>
-xnjsmb_paint_image_new(const Arguments &args) {
-    int argc = args.Length();
-    HandleScope scope;
-    Handle<Object> rt = args.This();
-    Handle<Object> paint_image_obj;
-    Handle<Value> pi_args[2];
-    Handle<Function> paint_image_func;
+/* @} */
 
-    if(argc != 1)
-	THROW("Invalid number of arguments (!= 2)");
-    if(!args[0]->IsObject())
-	THROW("Invalid argument type");
-
-    pi_args[0] = rt;
-    pi_args[1] = args[0];	// image
-    paint_image_func = xnjsmb_paint_image_temp->GetFunction();
-    paint_image_obj = paint_image_func->NewInstance(2, pi_args);
-
-    scope.Close(paint_image_obj);
-    return paint_image_obj;
+/*! \defgroup xnjsmb_paints_export Exported wrapper maker for paints
+ *
+ * These functions are used by MB runtime to wrap C paints to JS
+ * objects.
+ *
+ * @{
+ */
+Handle<Value>
+export_xnjsmb_auto_paint_color_new(paint_t *paint) {
+    Handle<Value> ret;
+    
+    ret = xnjsmb_auto_paint_color_new(paint);
+    
+    return ret;
 }
 
-static Persistent<FunctionTemplate> xnjsmb_paint_image_new_temp;
-
-/*! \brief Create templates for paint types.
- *
- * This function is only called one time for every execution.
- */
-static void
-xnjsmb_init_paints(void) {
-    Handle<FunctionTemplate> temp, meth;
-    Handle<ObjectTemplate> inst_temp;
-    Handle<ObjectTemplate> proto_temp;
-    
-    /*
-     * Base type of paint types.
-     */
-    temp = FunctionTemplate::New();
-    xnjsmb_paint_temp = Persistent<FunctionTemplate>::New(temp);
-    xnjsmb_paint_temp->SetClassName(String::New("paint"));
+Handle<Value>
+export_xnjsmb_auto_paint_image_new(paint_t *paint) {
+    Handle<Value> ret;
     
-    meth = FunctionTemplate::New(xnjsmb_paint_fill);
-    proto_temp = xnjsmb_paint_temp->PrototypeTemplate();
-    SET(proto_temp, "fill", meth);
-
-    meth = FunctionTemplate::New(xnjsmb_paint_stroke);
-    proto_temp = xnjsmb_paint_temp->PrototypeTemplate();
-    SET(proto_temp, "stroke", meth);
-
-    /*
-     * Paint color
-     */
-    temp = FunctionTemplate::New(xnjsmb_paint_color);
-    xnjsmb_paint_color_temp = Persistent<FunctionTemplate>::New(temp);
-    xnjsmb_paint_color_temp->SetClassName(String::New("paint_color"));
-    xnjsmb_paint_color_temp->Inherit(xnjsmb_paint_temp);
-
-    inst_temp = xnjsmb_paint_color_temp->InstanceTemplate();
-    inst_temp->SetInternalFieldCount(1);
-
-    temp = FunctionTemplate::New(xnjsmb_paint_color_new);
-    xnjsmb_paint_color_new_temp = Persistent<FunctionTemplate>::New(temp);
+    ret = xnjsmb_auto_paint_image_new(paint);
     
-    /*
-     * Paint image
-     */
-    temp = FunctionTemplate::New(xnjsmb_paint_image);
-    xnjsmb_paint_image_temp = Persistent<FunctionTemplate>::New(temp);
-    xnjsmb_paint_image_temp->SetClassName(String::New("paint_image"));
-    xnjsmb_paint_image_temp->Inherit(xnjsmb_paint_temp);
-    
-    inst_temp = xnjsmb_paint_image_temp->InstanceTemplate();
-    inst_temp->SetInternalFieldCount(1);
-
-    temp = FunctionTemplate::New(xnjsmb_paint_image_new);
-    xnjsmb_paint_image_new_temp = Persistent<FunctionTemplate>::New(temp);
+    return ret;
 }
+/* @} */
 
 /*! \brief Initialize paints for mbfly.
  *
@@ -284,13 +127,18 @@
     Handle<ObjectTemplate> rt_proto_temp;
 
     if(!init_flag) {
-	xnjsmb_init_paints();
+	xnjsmb_auto_paint_init();
+	xnjsmb_auto_paint_color_init();
+	xnjsmb_auto_paint_image_init();
+	
+	/* xnjsmb_init_paints(); */
 	init_flag = 1;
     }
-
+    /*
     rt_proto_temp = rt_temp->PrototypeTemplate();
     SET(rt_proto_temp, "paint_color_new", xnjsmb_paint_color_new_temp);
     SET(rt_proto_temp, "paint_image_new", xnjsmb_paint_image_new_temp);
+    */
 }
 
 /* @} */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/paints.m4	Sat Aug 07 21:37:57 2010 +0800
@@ -0,0 +1,15 @@
+define([PROJ_PREFIX], [xnjsmb_auto_])dnl
+dnl
+STRUCT([paint], [paint_t], [],
+       [METHOD([fill], [xnjsmb_paint_fill],
+               (SELF, OBJ([sh], [shape], [shape_t])), 1, []),
+        METHOD([stroke], [xnjsmb_paint_stroke],
+	       (SELF, OBJ([sh], [shape], [shape_t])), 1, [])])
+
+STRUCT([paint_color], [paint_t], [],
+       [],
+       (([INHERIT], [paint])))
+
+STRUCT([paint_image], [paint_t], [],
+       [],
+       (([INHERIT], [paint])))
--- a/nodejs/shapes.cc	Sat Aug 07 19:43:17 2010 +0800
+++ b/nodejs/shapes.cc	Sat Aug 07 21:37:57 2010 +0800
@@ -80,8 +80,11 @@
 
 #include "shapes-inc.h"
 
-/*! \defgroup xnjsmb_shapes_export Export functions of creating objects.
+/*! \defgroup xnjsmb_shapes_wraps Exported wrapper makers for shapes
  * \ingroup xnjsmb_shapes
+ * 
+ * These functions are used by methods of mb_rt to wrap shape objects
+ * as Javascript objects.
  *
  * @{
  */
@@ -102,11 +105,8 @@
 
 /* @} */
 
-/*! \defgroup xnjsmb_shapes_wraps Wrap functions for shape objects.
+/*! \defgroup xnjsmb_shapes_cons Constructor of shapes
  * \ingroup xnjsmb_shapes
- * 
- * These functions are used by methods of mb_rt to wrap shape objects
- * as Javascript objects.
  *
  * @{
  */
--- a/nodejs/wscript	Sat Aug 07 19:43:17 2010 +0800
+++ b/nodejs/wscript	Sat Aug 07 21:37:57 2010 +0800
@@ -21,26 +21,20 @@
 def build(conf):
     import Utils
     
-    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'])
-    conf(rule='m4 -I ${SRCDIR}/../tools gen_v8_binding.m4 ${SRC} > ${TGT}',
-         source='mbfly_njs.m4', target='mbfly_njs-inc.h',
-         name='mbfly_njs-inc', shell=True, always=True, before=['cxx'])
-    conf(rule='m4 -I ${SRCDIR}/../tools gen_v8_binding.m4 ${SRC} > ${TGT}',
-         source='shapes.m4', target='shapes-inc.h',
-         name='shapes-inc', shell=True, always=True, before=['cxx'])
+    for m in 'observer coord mbfly_njs shapes paints'.split():
+        conf(rule='m4 -I ${SRCDIR}/../tools gen_v8_binding.m4 ${SRC} > ${TGT}',
+             source=m+'.m4', target=m+'-inc.h',
+             name=m+'-inc', shell=True, always=True, before=['cxx'])
+        pass
 
     obj = conf.new_task_gen('cxx', 'shlib', 'node_addon')
     obj.target = 'mbfly'
-    obj.source = 'paints.cc font.cc image_ldr.cc'
-    obj.add_objects = 'X_supp_njs.o observer.o coord.o mbfly_njs.o shapes.o'
+    obj.source = 'font.cc image_ldr.cc'
+    obj.add_objects = 'X_supp_njs.o observer.o coord.o mbfly_njs.o ' + \
+        'shapes.o paints.o'
     obj.staticlib = 'mbfly'
 
-    for src in 'observer.cc coord.cc mbfly_njs.cc shapes.cc'.split():
+    for src in 'observer.cc coord.cc mbfly_njs.cc shapes.cc paints.cc'.split():
         obj = conf.new_task_gen('cxx', 'shlib', 'node_addon')
         obj.target = src[:-3] + '.o'
         obj.source = src