static int js_atomic_destroy(duk_context* ctx) { if (!duk_is_object(ctx, 0)) return 0; Object* obj = js_to_class_instance<Object>(ctx, 0, 0); if (!obj) return 0; if (obj->GetType() == Node::GetTypeStatic()) { Node* node = (Node*) obj; js_atomic_destroy_node(node, ctx, true); return 0; } if (obj->GetType() == Scene::GetTypeStatic()) { Scene* scene = (Scene*) obj; js_atomic_destroy_scene(scene, ctx); return 0; } else if (obj->GetType() == JSComponent::GetTypeStatic()) { // FIXME: want to be able to destroy a single component assert(0); JSComponent* component = (JSComponent*) obj; component->UnsubscribeFromAllEvents(); component->Remove(); return 0; } return 0; }
static void js_atomic_destroy_node(Node* node, duk_context* ctx, bool root = false) { if (root) { PODVector<Node*> children; node->GetChildren(children, true); for (unsigned i = 0; i < children.Size(); i++) { if (children.At(i)->JSGetHeapPtr()) js_atomic_destroy_node(children.At(i), ctx); } } const Vector<SharedPtr<Component> >& components = node->GetComponents(); for (unsigned i = 0; i < components.Size(); i++) { Component* component = components[i]; if (component->GetType() == JSComponent::GetTypeStatic()) { JSComponent* jscomponent = (JSComponent*) component; jscomponent->SetDestroyed(); } component->UnsubscribeFromAllEvents(); } node->RemoveAllComponents(); node->UnsubscribeFromAllEvents(); node->Remove(); }
static int js_atomic_destroy(duk_context* ctx) { if (!duk_is_object(ctx, 0)) return 0; Object* obj = js_to_class_instance<Object>(ctx, 0, 0); if (!obj) return 0; JSVM* vm = JSVM::GetJSVM(ctx); if (obj->GetType() == Node::GetTypeStatic()) { Node* node = (Node*) obj; if (node->JSGetHeapPtr()) { int top = duk_get_top(ctx); duk_push_global_stash(ctx); duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_INDEX_NODE_REGISTRY); duk_push_pointer(ctx, node->JSGetHeapPtr()); duk_del_prop(ctx, -2); duk_pop_2(ctx); assert(top = duk_get_top(ctx)); } const Vector<SharedPtr<Component> >& components = node->GetComponents(); for (unsigned i = 0; i < components.Size(); i++) { Component* component = components[i]; if (component->GetType() == JSComponent::GetTypeStatic()) { JSComponent* jscomponent = (JSComponent*) component; jscomponent->SetDestroyed(); } component->UnsubscribeFromAllEvents(); } node->RemoveAllComponents(); node->UnsubscribeFromAllEvents(); assert(node->Refs() >= 2); node->Remove(); return 0; } else if (obj->GetType() == JSComponent::GetTypeStatic()) { assert(0); JSComponent* component = (JSComponent*) obj; component->UnsubscribeFromAllEvents(); component->Remove(); return 0; } return 0; }