# HG changeset patch # User Thinker K.F. Li # Date 1302687249 -28800 # Node ID 0cb89e20482412c42627db2b0258d8b0d02936fc # Parent 67282421b0ac148a4a6e930dbdef221d84d77260 Implement tree-traveling for JS diff -r 67282421b0ac -r 0cb89e204824 nodejs/coord.cc --- a/nodejs/coord.cc Wed Apr 13 17:27:55 2011 +0800 +++ b/nodejs/coord.cc Wed Apr 13 17:34:09 2011 +0800 @@ -18,6 +18,16 @@ #define OK 0 +#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_rect_new export_xnjsmb_auto_rect_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 +#define xnjsmb_auto_paint_linear_new export_xnjsmb_auto_paint_linear_new +#define xnjsmb_auto_paint_radial_new export_xnjsmb_auto_paint_radial_new + + /*! \page jsgc How to Manage Life-cycle of Objects for Javascript. * * The life-cycle of MadButterfly ojects are simple. A object is live @@ -341,6 +351,94 @@ return Number::New(opacity); } +#define COORD_NEXT_SIBLING(child) STAILQ_NEXT(coord_t, sibling, (child)) + +static mb_obj_t * +_coord_get_nth_child_member(coord_t *coord, int idx) { + geo_t *geo; + coord_t *child; + int member_idx = 0; + int cnt = 0; + + child = STAILQ_HEAD(coord->children); + FOR_COORD_MEMBERS(coord, geo) { + while(child != NULL && child->before_pmem == member_idx) { + if(cnt == member_idx) + return (mb_obj_t *)child; + cnt++; + child = COORD_NEXT_SIBLING(child); + } + + if(cnt == member_idx) + return (mb_obj_t *)geo->shape; + cnt++; + } + + return NULL; +} + +static Handle xnjsmb_auto_coord_new(coord_t *data); + +static Handle +_mb_obj_to_value(Handle parent, mb_obj_t *mbobj) { + Handle val; + Handle obj; + Handle mbrt; + Handle mbrt_o; + Persistent *hdl; + + hdl = (Persistent *)mb_prop_get(&mbobj->props, + PROP_JSOBJ); + if(hdl) + return *hdl; + + switch(mbobj->obj_type) { + case MBO_COORD: + val = xnjsmb_auto_coord_new((coord_t *)mbobj); + break; + + case MBO_PATH: + val = xnjsmb_auto_path_new((shape_t *)mbobj); + break; + + case MBO_RECT: + val = xnjsmb_auto_rect_new((shape_t *)mbobj); + break; + + case MBO_IMAGE: + val = xnjsmb_auto_image_new((shape_t *)mbobj); + break; + + case MBO_STEXT: + val = xnjsmb_auto_stext_new((shape_t *)mbobj); + break; + } + ASSERT(val != NULL); + + mbrt = GET(parent, "mbrt"); + obj = val->ToObject(); + mbrt_o = mbrt->ToObject(); + SET(obj, "mbrt", mbrt_o); + + return val; +} + +static Handle +xnjsmb_coord_get_child(coord_t *coord, Handle self, + int idx, const char **err) { + Handle child; + mb_obj_t *child_mbobj; + + child_mbobj = _coord_get_nth_child_member(coord, idx); + if(child_mbobj == NULL) { + *err = "invalid index value for children"; + return Handle(NULL); + } + child = _mb_obj_to_value(self, child_mbobj); + + return child; +} + #include "coord-inc.h" /*! \brief This function used by \ref xnjsmb_mb_rt to wrap coord object. diff -r 67282421b0ac -r 0cb89e204824 nodejs/mbfly_njs.cc --- a/nodejs/mbfly_njs.cc Wed Apr 13 17:27:55 2011 +0800 +++ b/nodejs/mbfly_njs.cc Wed Apr 13 17:34:09 2011 +0800 @@ -40,8 +40,6 @@ SET(ret_obj, "mbrt", mbrt); } -#define xnjsmb_auto_coord_new export_xnjsmb_auto_coord_new - static void xnjsmb_redraw_changed(njs_runtime_t *rt) { redraw_man_t *rdman; @@ -121,6 +119,7 @@ * we need a exported version to call them indrectly from other * modules. */ +#define xnjsmb_auto_coord_new export_xnjsmb_auto_coord_new #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