comparison nodejs/coord.cc @ 741:d8764f10e141

Remove a coord from the tree in JS
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 25 Aug 2010 10:40:30 +0800
parents 4916c3a3fe3c
children 24038e7a365b
comparison
equal deleted inserted replaced
740:00a8c8a9e157 741:d8764f10e141
39 * An invalid object is the one with NULL internal field and obj.valid 39 * An invalid object is the one with NULL internal field and obj.valid
40 * == false. The binding of MadButterfly hold a reference to every 40 * == false. The binding of MadButterfly hold a reference to every
41 * object added to the tree of a mbrt (runtime object), and remove the 41 * object added to the tree of a mbrt (runtime object), and remove the
42 * reference and invalidate it when it being removed. 42 * reference and invalidate it when it being removed.
43 * 43 *
44 * The binding also hold a weak reference to every object, it free the 44 * For coords, they are always attached to the tree when it is valid.
45 * object when the callback of the weak reference being called and it 45 * So, binding hold a persistent reference to it. The reference is
46 * being valid. If the object is invalid, the callback does nothing. 46 * purged when a coord being removed from the tree and being
47 * invalidated.
48 *
49 * For any shape, it is not attached to the tree at begining, but is
50 * attached to a tree laterly, or is collected by GC. The binding
51 * hold a weak reference for a new shape, and upgrade to a strong
52 * reference when the shape being added to the tree.
47 */ 53 */
48 54
49 using namespace v8; 55 using namespace v8;
50 56
51 /*! \defgroup xnjsmb_coord JS binding for coord objects. 57 /*! \defgroup xnjsmb_coord JS binding for coord objects.
52 * \ingroup xnjsmb 58 * \ingroup xnjsmb
53 * 59 *
54 * @{ 60 * @{
55 */ 61 */
62 /*! \brief Invalidate JS objects for coords and shapes in a subtree.
63 *
64 * \param self is the object of the root of subtree.
65 *
66 * \sa \ref jsgc
67 */
68 static void
69 xnjsmb_coord_invalidate_subtree(Handle<Object> self) {
70 Handle<Object> *child_hdl;
71 Handle<Object> *mem_hdl;
72 redraw_man_t *rdman;
73 coord_t *coord, *child;
74 shape_t *mem;
75 Handle<Value> _false = Boolean::New(0);
76
77 if(!GET(self, "valid")->ToBoolean()->Value()) /* Invalidated object */
78 return;
79
80 coord = (coord_t *)UNWRAP(self);
81
82 /* Invalidate all coords in the subtree */
83 FOR_COORDS_PREORDER(coord, child) {
84 child_hdl = (Handle<Object> *)mb_prop_get(&child->obj.props,
85 PROP_JSOBJ);
86 child = (coord_t *)UNWRAP(*child_hdl);
87 WRAP(*child_hdl, NULL);
88 SET(*child_hdl, "valid", _false);
89
90 /* Invalidate members of a coord */
91 FOR_COORD_SHAPES(child, mem) {
92 mem_hdl = (Handle<Object> *)mb_prop_get(&mem->obj.props,
93 PROP_JSOBJ);
94 WRAP(*mem_hdl, NULL);
95 SET(*mem_hdl, "valid", _false);
96 }
97 }
98 }
99
56 static void 100 static void
57 xnjsmb_coord_mod(Handle<Object> self, coord_t *coord) { 101 xnjsmb_coord_mod(Handle<Object> self, coord_t *coord) {
58 Persistent<Object> *self_hdl; 102 Persistent<Object> *self_hdl;
59 subject_t *subject; 103 subject_t *subject;
60 Handle<Value> subject_o; 104 Handle<Value> subject_o;
66 mb_prop_set(&coord->obj.props, PROP_JSOBJ, self_hdl); 110 mb_prop_set(&coord->obj.props, PROP_JSOBJ, self_hdl);
67 111
68 subject = coord->mouse_event; 112 subject = coord->mouse_event;
69 subject_o = export_xnjsmb_auto_subject_new(subject); 113 subject_o = export_xnjsmb_auto_subject_new(subject);
70 SET(self, "mouse_event", subject_o); 114 SET(self, "mouse_event", subject_o);
115 SET(self, "valid", Boolean::New(1));
71 } 116 }
72 117
73 static float 118 static float
74 coord_get_index(coord_t *coord, Handle<Object> self, int idx, 119 coord_get_index(coord_t *coord, Handle<Object> self, int idx,
75 const char **err) { 120 const char **err) {
113 r = rdman_add_shape(rdman, shape, coord); 158 r = rdman_add_shape(rdman, shape, coord);
114 if(r != 0) 159 if(r != 0)
115 *err = "Unknown error"; 160 *err = "Unknown error";
116 } 161 }
117 162
163 static void
164 xnjsmb_coord_remove(coord_t *coord, Handle<Object> self, const char **err) {
165 Handle<Object> js_rt;
166 redraw_man_t *rdman;
167
168 js_rt = GET(self, "mbrt")->ToObject();
169 rdman = xnjsmb_rt_rdman(js_rt);
170
171 xnjsmb_coord_invalidate_subtree(self);
172
173 /* Free all coords and shapes in the subtree */
174 rdman_coord_free(rdman, coord);
175 }
176
118 #include "coord-inc.h" 177 #include "coord-inc.h"
119 178
120 /*! \brief This function used by \ref xnjsmb_mb_rt to wrap coord object. 179 /*! \brief This function used by \ref xnjsmb_mb_rt to wrap coord object.
121 */ 180 */
122 Handle<Value> export_xnjsmb_auto_coord_new(coord_t *coord) { 181 Handle<Value> export_xnjsmb_auto_coord_new(coord_t *coord) {