Mercurial > MadButterfly
changeset 683:7685c57e29d0
Migrate JS shapes binding to gen_v8_binding.m4
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 07 Aug 2010 18:27:53 +0800 |
parents | 4a38e571cfce |
children | b346e4699e55 |
files | nodejs/mbfly_njs.cc nodejs/mbfly_njs.h nodejs/mbfly_njs.m4 nodejs/shapes.cc nodejs/shapes.m4 nodejs/wscript |
diffstat | 6 files changed, 137 insertions(+), 395 deletions(-) [+] |
line wrap: on
line diff
--- a/nodejs/mbfly_njs.cc Sat Aug 07 18:26:19 2010 +0800 +++ b/nodejs/mbfly_njs.cc Sat Aug 07 18:27:53 2010 +0800 @@ -26,7 +26,7 @@ } static void -xnjsmb_coord_new_mod(Handle<Object> mbrt, Handle<Value> ret) { +xnjsmb_mb_rt_objs_mod(Handle<Object> mbrt, Handle<Value> ret) { Handle<Object> ret_obj = ret->ToObject(); SET(ret_obj, "mbrt", mbrt); @@ -77,6 +77,10 @@ * @{ */ +#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 + #include "mbfly_njs-inc.h" /* @} */
--- a/nodejs/mbfly_njs.h Sat Aug 07 18:26:19 2010 +0800 +++ b/nodejs/mbfly_njs.h Sat Aug 07 18:27:53 2010 +0800 @@ -4,6 +4,7 @@ #include <v8.h> extern "C" { #include <mb.h> +#include "X_supp_njs.h" } #define THROW(x) \ @@ -17,6 +18,7 @@ v8::Handle<v8::Value> exc; \ exc = v8::Exception::Error(v8::String::New(x)); \ v8::ThrowException(exc); \ + return; \ } while(0) #define UNWRAP(o) v8::External::Unwrap((o)->GetInternalField(0)) #define WRAP(o, v) (o)->SetInternalField(0, v8::External::Wrap(v)) @@ -31,6 +33,14 @@ /* From shapes.cc */ void xnjsmb_shapes_init_mb_rt_temp(v8::Handle<v8::FunctionTemplate> rt_temp); +shape_t *xnjsmb_path_new(njs_runtime_t *rt, const char *d); +shape_t *xnjsmb_stext_new(njs_runtime_t *rt, const char *txt, + float x, float y); +shape_t *xnjsmb_image_new(njs_runtime_t *rt, float x, float y, + float w, float h); +v8::Handle<v8::Value> export_xnjsmb_auto_path_new(shape_t *sh); +v8::Handle<v8::Value> export_xnjsmb_auto_stext_new(shape_t *sh); +v8::Handle<v8::Value> export_xnjsmb_auto_image_new(shape_t *sh); /* From paints.cc */ void xnjsmb_paints_init_mb_rt_temp(v8::Handle<v8::FunctionTemplate> rt_temp);
--- a/nodejs/mbfly_njs.m4 Sat Aug 07 18:26:19 2010 +0800 +++ b/nodejs/mbfly_njs.m4 Sat Aug 07 18:27:53 2010 +0800 @@ -4,8 +4,19 @@ STRUCT([mb_rt], [njs_runtime_t], [], [METHOD([coord_new], [xnjsmb_coord_new], (OBJ([parent], [coord], [coord_t]), ERR), 1, - [OBJ([coord], [coord_t])], (([MOD], xnjsmb_coord_new_mod))), + [OBJ([coord], [coord_t])], + (([MOD], [xnjsmb_mb_rt_objs_mod]))), METHOD([redraw_changed], [xnjsmb_redraw_changed], (), 0, []), - METHOD([redraw_all], [xnjsmb_redraw_all], (), 0, [])], + METHOD([redraw_all], [xnjsmb_redraw_all], (), 0, []), + METHOD([path_new], [xnjsmb_path_new], (STR(txt)), 1, + [OBJ([path], [shape_t])], (([MOD], [xnjsmb_mb_rt_objs_mod]))), + METHOD([stext_new], [xnjsmb_stext_new], + (STR(txt), NUMBER(x), NUMBER(y)), 3, + [OBJ([stext], [shape_t])], + (([MOD], [xnjsmb_mb_rt_objs_mod]))), + 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])))], ((CTOR, ([_X_njs_MB_new], (SELF, STR(display_name), INT(width), INT(height)), 3)))dnl )
--- a/nodejs/shapes.cc Sat Aug 07 18:26:19 2010 +0800 +++ b/nodejs/shapes.cc Sat Aug 07 18:27:53 2010 +0800 @@ -1,6 +1,8 @@ #include <v8.h> #include "mbfly_njs.h" +#include <string.h> + extern "C" { #include <mb.h> } @@ -11,72 +13,58 @@ using namespace v8; -/*! \defgroup shape_temp Templates for shape and derivations. +/*! \defgroup xnjsmb_shapes Implement binding of shapes. + * \ingroup xnjsmb * * @{ */ -static Persistent<FunctionTemplate> xnjsmb_shape_temp; - -static Handle<Value> -xnjsmb_shape_show(const Arguments &args) { - shape_t *sh; - Handle<Object> self; - - self = args.This(); - sh = (shape_t *)UNWRAP(self); - sh_show(sh); +static void +xnjsmb_sh_stext_set_style(shape_t *sh, Handle<Value> blks, const char **err) { + Array *blksobj; + Array *blkobj; + mb_style_blk_t *mb_blks; + int nblks; + int r; + int i; - return Null(); + blksobj = Array::Cast(*blks); + nblks = blksobj->Length(); + mb_blks = new mb_style_blk_t[nblks]; + for(i = 0; i < nblks; i++) { + blkobj = Array::Cast(*blksobj->Get(i)); + mb_blks[i].n_chars = blkobj->Get(0)->ToInt32()->Value(); + mb_blks[i].face = (mb_font_face_t *)UNWRAP(blkobj->Get(1)->ToObject()); + mb_blks[i].font_sz = blkobj->Get(2)->ToNumber()->Value(); + } + + r = sh_stext_set_style(sh, mb_blks, nblks); + if(r != 0) { + *err = "Unknown error"; + return; + } + + delete mb_blks; } static Handle<Value> -xnjsmb_shape_hide(const Arguments &args) { - shape_t *sh; - Handle<Object> self; +xnjsmb_shape_stroke_width_get(Handle<Object> self, shape_t *sh, + const char **err) { + float stroke_width; - self = args.This(); - sh = (shape_t *)UNWRAP(self); - sh_hide(sh); - - return Null(); + stroke_width = sh_get_stroke_width(sh); + return Number::New(stroke_width); } -/*! \brief Get stroke width of a shape. - */ -static Handle<Value> -xnjsmb_shape_stroke_width_getter(Local<String> property, - const AccessorInfo &info) { - Handle<Object> self = info.This(); - shape_t *sh; - float w; - Handle<Value> w_val; - - sh = (shape_t *)UNWRAP(self); - w = sh_get_stroke_width(sh); - w_val = Number::New(w); +static void +xnjsmb_shape_stroke_width_set(Handle<Object> self, shape_t *sh, + Handle<Value> value, const char **err) { + float stroke_width; + Handle<Object> rt; + redraw_man_t *rdman; - return w_val; -} - -/*! \brief Set stroke width of a shape. - */ -static void -xnjsmb_shape_stroke_width_setter(Local<String> property, - Local<Value> value, - const AccessorInfo &info) { - Handle<Object> self = info.This(); - Handle<Object> rt; - shape_t *sh; - redraw_man_t *rdman; - float w; - Handle<Number> w_num; + stroke_width = value->Int32Value(); + sh_set_stroke_width(sh, stroke_width); - sh = (shape_t *)UNWRAP(self); - w_num = value->ToNumber(); - w = w_num->Value(); - - sh_set_stroke_width(sh, w); - /* * Mark changed. */ @@ -88,358 +76,77 @@ rdman_shape_changed(rdman, sh); } -static void -xnjsmb_init_shape_temp(void) { - Handle<FunctionTemplate> temp; - Handle<ObjectTemplate> proto_temp; - Handle<FunctionTemplate> method_temp; - - temp = FunctionTemplate::New(); - temp->SetClassName(String::New("shape")); - xnjsmb_shape_temp = Persistent<FunctionTemplate>::New(temp); - proto_temp = temp->PrototypeTemplate(); - - method_temp = FunctionTemplate::New(xnjsmb_shape_show); - SET(proto_temp, "show", method_temp); - method_temp = FunctionTemplate::New(xnjsmb_shape_hide); - SET(proto_temp, "hide", method_temp); - - proto_temp->SetAccessor(String::New("stroke_width"), - xnjsmb_shape_stroke_width_getter, - xnjsmb_shape_stroke_width_setter); -} - /* @} */ -/*! \defgroup path_temp Templates for path objects. +#include "shapes-inc.h" + +/*! \defgroup xnjsmb_shapes_export Export functions of creating objects. + * \ingroup xnjsmb_shapes * * @{ */ -static Persistent<FunctionTemplate> xnjsmb_path_temp; - -/*! \brief Callback of constructor of path objects for Javascript. - */ -static Handle<Value> -xnjsmb_shape_path(const Arguments &args) { - shape_t *sh; - redraw_man_t *rdman; - Handle<Object> self = args.This(); // path object - Handle<Object> rt; - Persistent<Object> *self_hdl; - char *dstr; - int argc; - - argc = args.Length(); - if(argc != 2) - THROW("Invalid number of arugments (!= 1)"); - if(!args[0]->IsString()) - THROW("Invalid argument type (should be a string)"); - if(!args[1]->IsObject()) - THROW("Invalid argument type (should be an object)"); - - String::Utf8Value dutf8(args[0]->ToString()); - dstr = *dutf8; - - rt = args[1]->ToObject(); - rdman = xnjsmb_rt_rdman(rt); - sh = rdman_shape_path_new(rdman, dstr); - - WRAP(self, sh); - SET(self, "mbrt", rt); - /* 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(); +Handle<Value> +export_xnjsmb_auto_path_new(shape_t *sh) { + return xnjsmb_auto_path_new(sh); } -/*! \brief Initial function template for constructor of path objects. - */ -static void -xnjsmb_init_path_temp(void) { - Handle<FunctionTemplate> temp; - Handle<ObjectTemplate> inst_temp; - - temp = FunctionTemplate::New(xnjsmb_shape_path); - temp->Inherit(xnjsmb_shape_temp); - temp->SetClassName(String::New("path")); - - inst_temp = temp->InstanceTemplate(); - inst_temp->SetInternalFieldCount(1); - - xnjsmb_path_temp = Persistent<FunctionTemplate>::New(temp); +Handle<Value> +export_xnjsmb_auto_stext_new(shape_t *sh) { + return xnjsmb_auto_stext_new(sh); } -/*! \brief Callback function of mb_rt.path_new(). - */ -static Handle<Value> -xnjsmb_shape_path_new(const Arguments &args) { - HandleScope scope; - Handle<Object> self = args.This(); // runtime object - Handle<Object> path_obj; - Handle<Value> path_args[2]; - int argc; - - argc = args.Length(); - if(argc != 1) - THROW("Invalid number of arugments (!= 1)"); - if(!args[0]->IsString()) - THROW("Invalid argument type (shoud be a string)"); - - path_args[0] = args[0]; - path_args[1] = self; - - path_obj = xnjsmb_path_temp->GetFunction()->NewInstance(2, path_args); - - scope.Close(path_obj); - return path_obj; +Handle<Value> +export_xnjsmb_auto_image_new(shape_t *sh) { + return xnjsmb_auto_image_new(sh); } /* @} */ -/*! \defgroup stext_path Template for stext objects. +/*! \defgroup xnjsmb_shapes_wraps Wrap functions for shape objects. + * \ingroup xnjsmb_shapes + * + * These functions are used by methods of mb_rt to wrap shape objects + * as Javascript objects. * * @{ */ - -/*! \brief Constructor for stext objects. - * - * 4 arguments - * \param rt is a runtime object. - * \param data is a text to be showed. - * \param x is postion in x-axis. - * \param y is position in y-axis. - */ -static Handle<Value> -xnjsmb_shape_stext(const Arguments &args) { - int argc = args.Length(); - Handle<Object> self = args.This(); - Handle<Object> rt; - Persistent<Object> *self_hdl; - float x, y; - char *data; +shape_t * +xnjsmb_path_new(njs_runtime_t *rt, const char *d) { redraw_man_t *rdman; - shape_t *stext; + shape_t *sh; - if(argc != 4) - THROW("Invalid number of arguments (!= 4)"); - if(!args[0]->IsObject() || !args[1]->IsString() || - !args[2]->IsNumber() || !args[3]->IsNumber()) - THROW("Invalid argument type"); - - rt = args[0]->ToObject(); - String::Utf8Value data_utf8(args[1]); - data = *data_utf8; - x = args[2]->ToNumber()->Value(); - y = args[3]->ToNumber()->Value(); - - rdman = xnjsmb_rt_rdman(rt); - ASSERT(rdman != NULL); - - stext = rdman_shape_stext_new(rdman, data, x, y); - - WRAP(self, stext); - SET(self, "mbrt", rt); - /* 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(); -} - -static Persistent<FunctionTemplate> xnjsmb_shape_stext_temp; - -/*! \brief Create a stext and return it. - */ -static Handle<Value> -xnjsmb_shape_stext_new(const Arguments &args) { - HandleScope scope; - int argc = args.Length(); - Handle<Object> self = args.This(); - Handle<Value> stext_args[4]; - Handle<Object> stext_obj; - Handle<Function> func; - - if(argc != 3) - THROW("Invalid number of arguments (!= 3)"); - - stext_args[0] = self; - stext_args[1] = args[0]; - stext_args[2] = args[1]; - stext_args[3] = args[2]; - - func = xnjsmb_shape_stext_temp->GetFunction(); - stext_obj = func->NewInstance(4, stext_args); - ASSERT(stext_obj != NULL); - - scope.Close(stext_obj); - return stext_obj; + rdman = X_njs_MB_rdman(rt); + sh = rdman_shape_path_new(rdman, d); + + return sh; } -/*! \brief Setup style blocks for a stext. - * - * It defines font style and size for blocks of text message. - * - * \param blks is a list (n_char, face, font size) tuples. - */ -static Handle<Value> -xnjsmb_shape_stext_set_style(const Arguments &args) { - HandleScope scope; - int argc = args.Length(); - Handle<Object> self = args.This(); +shape_t * +xnjsmb_stext_new(njs_runtime_t *rt, const char *txt, float x, float y) { + redraw_man_t *rdman; shape_t *sh; - Array *blksobj; - Array *blkobj; - mb_style_blk_t *blks; - int nblks; - int i; - int r; - - if(argc != 1) - THROW("Invalid number of arguments (!= 1)"); - if(!args[0]->IsArray()) - THROW("Invalid type of the argument"); - blksobj = Array::Cast(*args[0]); - nblks = blksobj->Length(); - blks = new mb_style_blk_t[nblks]; - for(i = 0; i < nblks; i++) { - blkobj = Array::Cast(*blksobj->Get(i)); - blks[i].n_chars = blkobj->Get(0)->ToInt32()->Value(); - blks[i].face = (mb_font_face_t *)UNWRAP(blkobj->Get(1)->ToObject()); - blks[i].font_sz = blkobj->Get(2)->ToNumber()->Value(); - } - - sh = (shape_t *)UNWRAP(self); - r = sh_stext_set_style(sh, blks, nblks); - if(r != 0) - THROW("Unknown error"); - - delete blks; - - return Null(); -} + rdman = X_njs_MB_rdman(rt); + sh = rdman_shape_stext_new(rdman, txt, x, y); -/*! \brief Initialize function template for stext objects. - */ -static void -xnjsmb_init_stext_temp(void) { - Handle<FunctionTemplate> func_temp; - Handle<FunctionTemplate> meth_temp; - Handle<ObjectTemplate> inst_temp; - Handle<ObjectTemplate> proto_temp; - - func_temp = FunctionTemplate::New(xnjsmb_shape_stext); - func_temp->Inherit(xnjsmb_shape_temp); - func_temp->SetClassName(String::New("stext")); - - inst_temp = func_temp->InstanceTemplate(); - inst_temp->SetInternalFieldCount(1); - - proto_temp = func_temp->PrototypeTemplate(); - meth_temp = FunctionTemplate::New(xnjsmb_shape_stext_set_style); - SET(proto_temp, "set_style", meth_temp); - - xnjsmb_shape_stext_temp = Persistent<FunctionTemplate>::New(func_temp); + return sh; } -/* @} */ - -/*! \defgroup image_temp Templates for image objects. - * - * @{ - */ - -static Handle<Value> -xnjsmb_shape_image(const Arguments &args) { - int argc = args.Length(); - Handle<Object> self = args.This(); - Handle<Object> rt; - Persistent<Object> *self_hdl; - float x, y; - float w, h; +shape_t * +xnjsmb_image_new(njs_runtime_t *rt, float x, float y, float w, float h) { redraw_man_t *rdman; - shape_t *img; - - 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(); - x = args[1]->ToNumber()->Value(); - y = args[2]->ToNumber()->Value(); - w = args[3]->ToNumber()->Value(); - h = args[4]->ToNumber()->Value(); - - rdman = xnjsmb_rt_rdman(rt); - img = rdman_shape_image_new(rdman, x, y, w, h); - - WRAP(self, img); - SET(self, "mbrt", rt); - /* 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); + shape_t *sh; - return Null(); -} - -static Persistent<FunctionTemplate> xnjsmb_shape_image_temp; - -static Handle<Value> -xnjsmb_shape_image_new(const Arguments &args) { - HandleScope scope; - int argc = args.Length(); - Handle<Object> self = args.This(); - Handle<Value> img_args[5]; - Handle<Object> img_obj; - Handle<Function> func; - - if(argc != 4) - THROW("Invalid number of arguments (!= 3)"); - - img_args[0] = self; - img_args[1] = args[0]; - img_args[2] = args[1]; - img_args[3] = args[2]; - img_args[4] = args[3]; + rdman = X_njs_MB_rdman(rt); + sh = rdman_shape_image_new(rdman, x, y, w, h); - func = xnjsmb_shape_image_temp->GetFunction(); - img_obj = func->NewInstance(5, img_args); - ASSERT(img_obj != NULL); - - scope.Close(img_obj); - return img_obj; -} - -static void -xnjsmb_init_image_temp() { - Handle<FunctionTemplate> func_temp; - Handle<FunctionTemplate> meth_temp; - Handle<ObjectTemplate> inst_temp; - Handle<ObjectTemplate> proto_temp; - - func_temp = FunctionTemplate::New(xnjsmb_shape_image); - func_temp->Inherit(xnjsmb_shape_temp); - func_temp->SetClassName(String::New("image")); - - inst_temp = func_temp->InstanceTemplate(); - inst_temp->SetInternalFieldCount(1); - - xnjsmb_shape_image_temp = Persistent<FunctionTemplate>::New(func_temp); + return sh; } /* @} */ /*! \brief Set properties of template of mb_rt. + * \ingroup xnjsmb_shapes */ void xnjsmb_shapes_init_mb_rt_temp(Handle<FunctionTemplate> rt_temp) { @@ -450,21 +157,11 @@ static int temp_init_flag = 0; if(temp_init_flag == 0) { - xnjsmb_init_shape_temp(); - xnjsmb_init_path_temp(); - xnjsmb_init_stext_temp(); - xnjsmb_init_image_temp(); + xnjsmb_auto_shape_init(); + xnjsmb_auto_path_init(); + xnjsmb_auto_stext_init(); + xnjsmb_auto_image_init(); temp_init_flag = 1; } - - rt_proto_temp = rt_temp->PrototypeTemplate(); - - path_new_temp = FunctionTemplate::New(xnjsmb_shape_path_new); - SET(rt_proto_temp, "path_new", path_new_temp); - - stext_new_temp = FunctionTemplate::New(xnjsmb_shape_stext_new); - SET(rt_proto_temp, "stext_new", stext_new_temp); - - image_new_temp = FunctionTemplate::New(xnjsmb_shape_image_new); - SET(rt_proto_temp, "image_new", image_new_temp); + return; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nodejs/shapes.m4 Sat Aug 07 18:27:53 2010 +0800 @@ -0,0 +1,18 @@ +define([PROJ_PREFIX], [xnjsmb_auto_])dnl +dnl +STRUCT([shape], [shape_t], + [ACCESSOR([stroke_width], + [xnjsmb_shape_stroke_width_get], + [xnjsmb_shape_stroke_width_set])], + [METHOD([show], [sh_show], (), 0, []), + METHOD([hide], [sh_hide], (), 0, [])]) + +STRUCT([path], [shape_t], [], [], (([INHERIT], [shape]))) + +STRUCT([stext], [shape_t], [], + [METHOD([set_text], [sh_stext_set_text], (STR([txt])), 1, []), + METHOD([set_style], [xnjsmb_sh_stext_set_style], + (ARRAY([blks]), ERR), 1, [])], + (([INHERIT], [shape]))) + +STRUCT([image], [shape_t], [], [], (([INHERIT], [shape])))
--- a/nodejs/wscript Sat Aug 07 18:26:19 2010 +0800 +++ b/nodejs/wscript Sat Aug 07 18:27:53 2010 +0800 @@ -30,15 +30,17 @@ 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']) obj = conf.new_task_gen('cxx', 'shlib', 'node_addon') obj.target = 'mbfly' - obj.source = 'shapes.cc paints.cc font.cc ' + \ - 'image_ldr.cc' - obj.add_objects = 'X_supp_njs.o observer.o coord.o mbfly_njs.o' + 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.staticlib = 'mbfly' - for src in 'observer.cc coord.cc mbfly_njs.cc'.split(): + for src in 'observer.cc coord.cc mbfly_njs.cc shapes.cc'.split(): obj = conf.new_task_gen('cxx', 'shlib', 'node_addon') obj.target = src[:-3] + '.o' obj.source = src