# HG changeset patch # User Thinker K.F. Li # Date 1275893101 -28800 # Node ID 1b6402f07cd4a60bf9ec9a5016d5415b7392db11 # Parent a3c13c2a4792344415e6c6e9e8539b58cb55c130 Make root coord availabe for Javascript code diff -r a3c13c2a4792 -r 1b6402f07cd4 nodejs/coord.cc --- a/nodejs/coord.cc Mon Jun 07 12:12:03 2010 +0800 +++ b/nodejs/coord.cc Mon Jun 07 14:45:01 2010 +0800 @@ -71,6 +71,28 @@ coord_obj_temp->SetInternalFieldCount(1); } +static Handle +xnjsmb_coord_new_jsobj(coord_t *coord, Handle parent_obj, + Handle js_rt) { + Handle 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; +} + /*! \brief Create a coord object associated with the rdman of the runtime. * * Two internal fields, coord and rdman. @@ -84,12 +106,6 @@ redraw_man_t *rdman; coord_t *coord, *parent = NULL; int argc; - static int init_temp = 0; - - if(!init_temp) { - xnjsmb_init_temp(); - init_temp = 1; - } argc = args.Length(); if(argc > 1) @@ -104,19 +120,29 @@ parent = (coord_t *)UNWRAP(parent_obj); } - /* - * Set Javascript object - */ coord = rdman_coord_new(rdman, parent); ASSERT(coord != NULL); + coord_obj = xnjsmb_coord_new_jsobj(coord, parent_obj, js_rt); - coord_obj = coord_obj_temp->NewInstance(); - ASSERT(coord_obj != NULL); - WRAP(coord_obj, coord); - - if(parent != NULL) - SET(coord_obj, "parent", parent_obj); - SET(coord_obj, "mbrt", js_rt); - return coord_obj; } + +/*! \brief Initialize Javascript object for root coord of a runtime. + * + * \param js_rt is the runtime object to create the root object for. + * + * After the function, js_rt.root is the object for root coord in + * Javascript. + */ +void +xnjsmb_coord_mkroot(Handle js_rt) { + redraw_man_t *rdman; + coord_t *root; + Handle obj; + + rdman = xnjsmb_rt_rdman(js_rt); + root = rdman_get_root(rdman); + obj = xnjsmb_coord_new_jsobj(root, Handle(NULL), js_rt); + + SET(js_rt, "root", obj); +} diff -r a3c13c2a4792 -r 1b6402f07cd4 nodejs/mbfly_njs.cc --- a/nodejs/mbfly_njs.cc Mon Jun 07 12:12:03 2010 +0800 +++ b/nodejs/mbfly_njs.cc Mon Jun 07 14:45:01 2010 +0800 @@ -50,6 +50,7 @@ self = args.This(); WRAP(self, rt); + xnjsmb_coord_mkroot(self); X_njs_MB_init_handle_connection(rt); @@ -62,6 +63,8 @@ /* @} */ +/*! \brief Get rdman associated with the runtime. + */ redraw_man_t * xnjsmb_rt_rdman(Handle mbrt) { HandleScope scope; @@ -84,17 +87,24 @@ extern "C" void init(Handle target) { HandleScope scope; - Handle func; - Handle rt_obj_temp; + Handle func, mb_rt_func; + Handle rt_instance_temp, rt_proto_temp; func = FunctionTemplate::New(hello_func); target->Set(String::New("Hello"), func->GetFunction()); - func = FunctionTemplate::New(xnjsmb_new); - target->Set(String::New("mb_rt"), func->GetFunction()); - rt_obj_temp = func->PrototypeTemplate(); - rt_obj_temp->SetInternalFieldCount(1); + /* + * Initialize template for MadButterfly runtime objects. + */ + mb_rt_func = FunctionTemplate::New(xnjsmb_new); + mb_rt_func->SetClassName(String::New("mb_rt")); + rt_instance_temp = mb_rt_func->InstanceTemplate(); + rt_instance_temp->SetInternalFieldCount(1); + + rt_proto_temp = mb_rt_func->PrototypeTemplate(); func = FunctionTemplate::New(xnjsmb_coord_new); - SET(rt_obj_temp, "coord_new", func); + SET(rt_proto_temp, "coord_new", func); + + target->Set(String::New("mb_rt"), mb_rt_func->GetFunction()); } diff -r a3c13c2a4792 -r 1b6402f07cd4 nodejs/mbfly_njs.h --- a/nodejs/mbfly_njs.h Mon Jun 07 12:12:03 2010 +0800 +++ b/nodejs/mbfly_njs.h Mon Jun 07 14:45:01 2010 +0800 @@ -18,6 +18,9 @@ #define GET(o, n) (o)->Get(v8::String::New(n)) redraw_man_t *xnjsmb_rt_rdman(v8::Handle mbrt); + +/* From coord.cc */ v8::Handle xnjsmb_coord_new(const v8::Arguments &args); +void xnjsmb_coord_mkroot(v8::Handle js_rt); #endif /* __MBFLY_NJS_H_ */ diff -r a3c13c2a4792 -r 1b6402f07cd4 nodejs/testcase.js --- a/nodejs/testcase.js Mon Jun 07 12:12:03 2010 +0800 +++ b/nodejs/testcase.js Mon Jun 07 14:45:01 2010 +0800 @@ -4,5 +4,6 @@ sys.puts(r); var mb_rt = new mbfly.mb_rt(":0.0", 300, 200); +sys.puts(mb_rt.root); setTimeout(function() { sys.puts("timeout"); }, 1000);