From d8794b5aecc36c4505fc4f67f1d27d0a869891b5 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sat, 6 May 2023 10:03:26 +0200 Subject: [PATCH] [libdom] spidermonkey finalizers --- src/ecmascript/ecmascript.cpp | 10 ---------- src/ecmascript/libdom/parse.c | 10 ++++++++++ src/ecmascript/libdom/spidermonkey/attr.cpp | 5 +++++ .../libdom/spidermonkey/attributes.cpp | 6 ++++++ .../libdom/spidermonkey/collection.cpp | 5 +++++ .../libdom/spidermonkey/document.cpp | 19 +++++++++++++++++- .../libdom/spidermonkey/element.cpp | 20 ++++++++++++++++++- .../libdom/spidermonkey/nodelist.cpp | 5 +++++ 8 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/ecmascript/ecmascript.cpp b/src/ecmascript/ecmascript.cpp index ff4325891..9c1b752e6 100644 --- a/src/ecmascript/ecmascript.cpp +++ b/src/ecmascript/ecmascript.cpp @@ -806,16 +806,6 @@ static struct module *ecmascript_modules[] = { NULL, }; -void -free_document(void *doc) -{ - if (!doc) { - return; - } -// xmlpp::Document *docu = static_cast(doc); -// delete docu; -} - static void delayed_goto(void *data) { diff --git a/src/ecmascript/libdom/parse.c b/src/ecmascript/libdom/parse.c index b4f85b6ac..38e79772c 100644 --- a/src/ecmascript/libdom/parse.c +++ b/src/ecmascript/libdom/parse.c @@ -79,6 +79,16 @@ document_parse(struct document *document) return document_parse_text(f->data, f->length); } +void +free_document(void *doc) +{ + if (!doc) { + return; + } + dom_node *ddd = (dom_node *)doc; + dom_node_unref(ddd); +} + void el_insert_before(struct document *document, void *element, struct string *source) { diff --git a/src/ecmascript/libdom/spidermonkey/attr.cpp b/src/ecmascript/libdom/spidermonkey/attr.cpp index e6e69a1be..20af6233d 100644 --- a/src/ecmascript/libdom/spidermonkey/attr.cpp +++ b/src/ecmascript/libdom/spidermonkey/attr.cpp @@ -57,6 +57,11 @@ static void attr_finalize(JS::GCContext *op, JSObject *obj) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif + dom_attr *attr = (dom_attr *)JS::GetMaybePtrFromReservedSlot(obj, 0); + + if (attr) { + dom_node_unref(attr); + } } diff --git a/src/ecmascript/libdom/spidermonkey/attributes.cpp b/src/ecmascript/libdom/spidermonkey/attributes.cpp index 1c965ee4b..1accdfa3c 100644 --- a/src/ecmascript/libdom/spidermonkey/attributes.cpp +++ b/src/ecmascript/libdom/spidermonkey/attributes.cpp @@ -62,6 +62,12 @@ static void attributes_finalize(JS::GCContext *op, JSObject *obj) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif + dom_namednodemap *attrs = (dom_namednodemap *)JS::GetMaybePtrFromReservedSlot(obj, 0); + + if (attrs) { + dom_namednodemap_unref(attrs); + } + } JSClassOps attributes_ops = { diff --git a/src/ecmascript/libdom/spidermonkey/collection.cpp b/src/ecmascript/libdom/spidermonkey/collection.cpp index d9598feea..839766b88 100644 --- a/src/ecmascript/libdom/spidermonkey/collection.cpp +++ b/src/ecmascript/libdom/spidermonkey/collection.cpp @@ -63,6 +63,11 @@ static void htmlCollection_finalize(JS::GCContext *op, JSObject *obj) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif + dom_html_collection *ns = JS::GetMaybePtrFromReservedSlot(obj, 0); + + if (ns) { + dom_html_collection_unref(ns); + } } diff --git a/src/ecmascript/libdom/spidermonkey/document.cpp b/src/ecmascript/libdom/spidermonkey/document.cpp index 5acba400c..12de7eb25 100644 --- a/src/ecmascript/libdom/spidermonkey/document.cpp +++ b/src/ecmascript/libdom/spidermonkey/document.cpp @@ -67,6 +67,11 @@ static void document_finalize(JS::GCContext *op, JSObject *obj) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif + dom_document *doc = JS::GetMaybePtrFromReservedSlot(obj, 0); + + if (doc) { + dom_node_unref(doc); + } } JSClassOps document_ops = { @@ -1763,6 +1768,18 @@ document_querySelectorAll(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static void doctype_finalize(JS::GCContext *op, JSObject *obj) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + dom_document_type *dtd = JS::GetMaybePtrFromReservedSlot(obj, 0); + + if (dtd) { + dom_node_unref(dtd); + } +} + JSClassOps doctype_ops = { nullptr, // addProperty nullptr, // deleteProperty @@ -1770,7 +1787,7 @@ JSClassOps doctype_ops = { nullptr, // newEnumerate nullptr, // resolve nullptr, // mayResolve - nullptr, // finalize + doctype_finalize, // finalize nullptr, // call nullptr, // construct JS_GlobalObjectTraceHook diff --git a/src/ecmascript/libdom/spidermonkey/element.cpp b/src/ecmascript/libdom/spidermonkey/element.cpp index 9971ab37d..a320b86ad 100644 --- a/src/ecmascript/libdom/spidermonkey/element.cpp +++ b/src/ecmascript/libdom/spidermonkey/element.cpp @@ -108,6 +108,8 @@ struct element_private { JS::RootedObject thisval; }; +static std::map map_privates; + static void element_finalize(JS::GCContext *op, JSObject *obj); JSClassOps element_ops = { @@ -166,6 +168,23 @@ static void element_finalize(JS::GCContext *op, JSObject *obj) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif + dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot(obj, 0); + struct element_private *el_private = JS::GetMaybePtrFromReservedSlot(obj, 1); + + if (el_private) { + map_privates.erase(el); + + struct listener *l; + + foreach(l, el_private->listeners) { + mem_free_set(&l->typ, NULL); + } + free_list(el_private->listeners); + mem_free(el_private); + } + if (el) { + dom_node_unref(el); + } } static bool @@ -4032,7 +4051,6 @@ element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval) return true; } -static std::map map_privates; JSObject * getElement(JSContext *ctx, void *node) diff --git a/src/ecmascript/libdom/spidermonkey/nodelist.cpp b/src/ecmascript/libdom/spidermonkey/nodelist.cpp index 160929ee9..0f66b3589 100644 --- a/src/ecmascript/libdom/spidermonkey/nodelist.cpp +++ b/src/ecmascript/libdom/spidermonkey/nodelist.cpp @@ -60,6 +60,11 @@ static void nodeList_finalize(JS::GCContext *op, JSObject *obj) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif + dom_nodelist *nl = JS::GetMaybePtrFromReservedSlot(obj, 0); + + if (nl) { + dom_nodelist_unref(nl); + } } JSClassOps nodeList_ops = {