# HG changeset patch # User Thinker K.F. Li # Date 1275921471 -28800 # Node ID c0bc604489132ed2d3fad981e913f50f9138270b # Parent 0cd1511272d2690cf781667c8e64f8e42af08b14 Constructor for path objects in Javascript domain diff -r 0cd1511272d2 -r c0bc60448913 nodejs/mbfly_njs.cc --- a/nodejs/mbfly_njs.cc Mon Jun 07 19:18:20 2010 +0800 +++ b/nodejs/mbfly_njs.cc Mon Jun 07 22:37:51 2010 +0800 @@ -105,6 +105,8 @@ rt_proto_temp = mb_rt_func->PrototypeTemplate(); func = FunctionTemplate::New(xnjsmb_coord_new); SET(rt_proto_temp, "coord_new", func); + + xnjsmb_shapes_init_mb_rt_temp(mb_rt_func); target->Set(String::New("mb_rt"), mb_rt_func->GetFunction()); } diff -r 0cd1511272d2 -r c0bc60448913 nodejs/mbfly_njs.h --- a/nodejs/mbfly_njs.h Mon Jun 07 19:18:20 2010 +0800 +++ b/nodejs/mbfly_njs.h Mon Jun 07 22:37:51 2010 +0800 @@ -23,4 +23,8 @@ v8::Handle xnjsmb_coord_new(const v8::Arguments &args); void xnjsmb_coord_mkroot(v8::Handle js_rt); +/* From shapes.cc */ +void xnjsmb_shapes_init_mb_rt_temp(v8::Handle rt_temp); + + #endif /* __MBFLY_NJS_H_ */ diff -r 0cd1511272d2 -r c0bc60448913 nodejs/shapes.cc --- a/nodejs/shapes.cc Mon Jun 07 19:18:20 2010 +0800 +++ b/nodejs/shapes.cc Mon Jun 07 22:37:51 2010 +0800 @@ -7,7 +7,7 @@ using namespace v8; -/*! \defgroup shape_temp Templates for shape and derive. +/*! \defgroup shape_temp Templates for shape and derivations. * * @{ */ @@ -56,3 +56,103 @@ /* @} */ +/*! \defgroup path_temp Templates for path objects. + * + * @{ + */ +static Persistent xnjsmb_path_temp; + +/*! \brief Callback of constructor of path objects for Javascript. + */ +static Handle +xnjsmb_shape_path(const Arguments &args) { + shape_t *sh; + redraw_man_t *rdman; + Handle self = args.This(); // path object + Handle rt; + 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); + + return Null(); +} + +/*! \brief Initial function template for constructor of path objects. + */ +static void +xnjsmb_init_path_temp(void) { + Handle temp; + Handle 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::New(temp); +} + +/*! \brief Callback function of mb_rt.path_new(). + */ +static Handle +xnjsmb_shape_path_new(const Arguments &args) { + HandleScope scope; + Handle self = args.This(); // runtime object + Handle path_obj; + Handle 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; +} + +/* @} */ + +/*! \brief Set properties of template of mb_rt. + */ +void +xnjsmb_shapes_init_mb_rt_temp(Handle rt_temp) { + Handle path_new_temp; + Handle rt_proto_temp; + static int temp_init_flag = 0; + + if(temp_init_flag == 0) { + xnjsmb_init_shape_temp(); + xnjsmb_init_path_temp(); + 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); +} diff -r 0cd1511272d2 -r c0bc60448913 nodejs/testcase.js --- a/nodejs/testcase.js Mon Jun 07 19:18:20 2010 +0800 +++ b/nodejs/testcase.js Mon Jun 07 22:37:51 2010 +0800 @@ -11,4 +11,8 @@ sys.puts("coord matrix: " + [coord[0], coord[1], coord[2], coord[3], coord[4], coord[5]]); +sys.puts(mb_rt.path_new); +var path = mb_rt.path_new("m 100,100 L 200,200"); +sys.puts(path); + setTimeout(function() { sys.puts("timeout"); }, 1000);