diff nodejs/paints.cc @ 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 b346e4699e55
children 0b98bdc53215
line wrap: on
line diff
--- 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);
+    */
 }
 
 /* @} */