# HG changeset patch # User Thinker K.F. Li # Date 1282718628 -28800 # Node ID 4ccb0553e80499985ca3fc3e41c8687993e64ca8 # Parent 6a988e23ad2a63014e7ce1c63edf8cc301bf3a00 Refactor code of free C objects into xnjsmb_coord_free_subtree diff -r 6a988e23ad2a -r 4ccb0553e804 nodejs/coord.cc --- a/nodejs/coord.cc Wed Aug 25 14:23:06 2010 +0800 +++ b/nodejs/coord.cc Wed Aug 25 14:43:48 2010 +0800 @@ -68,26 +68,44 @@ * \sa \ref jsgc */ static void -xnjsmb_coord_invalidate_subtree(Handle self) { +xnjsmb_coord_invalidate_subtree(coord_t *coord) { Persistent *child_hdl; Persistent *mem_hdl; - coord_t *coord, *child, *last_child; - Handle js_rt; - redraw_man_t *rdman; - shape_t *mem, *last_mem; + coord_t *child; + shape_t *mem; Handle _false = Boolean::New(0); - int r; - if(!GET(self, "valid")->ToBoolean()->Value()) /* Invalidated object */ - return; + /* Invalidate all coords in the subtree */ + FOR_COORDS_PREORDER(coord, child) { + child_hdl = (Persistent *)mb_prop_get(&child->obj.props, + PROP_JSOBJ); + SET(*child_hdl, "valid", _false); + WRAP(*child_hdl, NULL); + + /* Invalidate members of a coord */ + FOR_COORD_SHAPES(child, mem) { + mem_hdl = (Persistent *)mb_prop_get(&mem->obj.props, + PROP_JSOBJ); + SET(*mem_hdl, "valid", _false); + WRAP(*mem_hdl, NULL); + } + } +} + +/*! \brief Free C objects for coords and shapes in a subtree. + * + * \param self is the object of the root of subtree. + * + * \sa \ref jsgc + */ +static void +xnjsmb_coord_free_subtree(redraw_man_t *rdman, coord_t *coord) { + coord_t *child, *last_child; + shape_t *mem, *last_mem; + int r; - js_rt = GET(self, "mbrt")->ToObject(); - rdman = xnjsmb_rt_rdman(js_rt); - - coord = (coord_t *)UNWRAP(self); rdman_coord_changed(rdman, coord); - /* Invalidate all coords in the subtree */ last_child = NULL; FOR_COORDS_POSTORDER(coord, child) { if(last_child != NULL) { @@ -95,13 +113,8 @@ if(r != OK) THROW_noret("Unknown error"); } - - child_hdl = (Persistent *)mb_prop_get(&child->obj.props, - PROP_JSOBJ); - SET(*child_hdl, "valid", _false); - WRAP(*child_hdl, NULL); - /* Invalidate members of a coord */ + /* Free members of a coord */ last_mem = NULL; FOR_COORD_SHAPES(child, mem) { if(last_mem != NULL) { @@ -110,11 +123,6 @@ THROW_noret("Unknown error"); } - mem_hdl = (Persistent *)mb_prop_get(&mem->obj.props, - PROP_JSOBJ); - SET(*mem_hdl, "valid", _false); - WRAP(*mem_hdl, NULL); - last_mem = mem; } if(last_mem != NULL) { @@ -197,8 +205,18 @@ } static void -xnjsmb_coord_remove(coord_t *coord, Handle self, const char **err) { - xnjsmb_coord_invalidate_subtree(self); +xnjsmb_coord_remove(coord_t *coord, Handle self) { + Handle js_rt; + redraw_man_t *rdman; + + if(!GET(self, "valid")->ToBoolean()->Value()) /* Invalidated object */ + THROW_noret("Invalid object"); + + js_rt = GET(self, "mbrt")->ToObject(); + rdman = xnjsmb_rt_rdman(js_rt); + + xnjsmb_coord_invalidate_subtree(coord); + xnjsmb_coord_free_subtree(rdman, coord); } #include "coord-inc.h" diff -r 6a988e23ad2a -r 4ccb0553e804 nodejs/coord.m4 --- a/nodejs/coord.m4 Wed Aug 25 14:23:06 2010 +0800 +++ b/nodejs/coord.m4 Wed Aug 25 14:43:48 2010 +0800 @@ -2,7 +2,7 @@ STRUCT([coord], [coord_t], [], [METHOD([add_shape], [xnjsmb_coord_add_shape], (SELF, OBJ([shape], [shape], [shape_t]), ERR), 1, []), - METHOD([remove], [xnjsmb_coord_remove], (SELF, ERR), 0, [])], + METHOD([remove], [xnjsmb_coord_remove], (SELF), 0, [])], ((GET_INDEX, (coord_get_index, NUMBER)), (SET_INDEX, (coord_set_index, NUMBER)), ([STMOD], [xnjsmb_coord_mod])))