From 95f45b7327f4734d4dec106d2190d4c95dbeb374 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Fri, 19 Jul 2024 21:01:27 +0200 Subject: [PATCH] [quickjs] memleaks --- src/document/libdom/doc.c | 7 +++---- src/document/libdom/renderer2.c | 8 ++++---- src/ecmascript/ecmascript.c | 4 ++-- src/ecmascript/libdom/parse.c | 2 +- src/ecmascript/quickjs/collection.c | 1 + src/ecmascript/quickjs/document.c | 16 ++++++++-------- src/ecmascript/quickjs/element.c | 4 +++- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/document/libdom/doc.c b/src/document/libdom/doc.c index b020bf5e..70ea0835 100644 --- a/src/document/libdom/doc.c +++ b/src/document/libdom/doc.c @@ -79,11 +79,11 @@ document_parse_text(const char *charset, const char *data, size_t length) dom_hubbub_parser *parser = NULL; dom_hubbub_error error; dom_hubbub_parser_params params; - dom_document *doc; + dom_document *doc = NULL; params.enc = charset; params.fix_enc = true; - params.enable_script = false; + params.enable_script = true; params.msg = NULL; params.script = NULL; params.ctx = NULL; @@ -118,7 +118,6 @@ document_parse_text(const char *charset, const char *data, size_t length) return doc; } - void * document_parse(struct document *document, struct string *source) { @@ -136,7 +135,7 @@ free_document(void *doc) if (!doc) { return; } - dom_node *ddd = (dom_node *)doc; + dom_html_document *ddd = (dom_html_document *)doc; dom_node_unref(ddd); } diff --git a/src/document/libdom/renderer2.c b/src/document/libdom/renderer2.c index e76199a0..753a423f 100644 --- a/src/document/libdom/renderer2.c +++ b/src/document/libdom/renderer2.c @@ -483,7 +483,7 @@ void debug_dump_xhtml(void *d) { #ifdef ECMASCRIPT_DEBUG - dom_document *doc = (dom_document *)d; + dom_html_document *doc = (dom_html_document *)d; if (!doc) { return; @@ -525,7 +525,7 @@ void dump_xhtml(struct cache_entry *cached, struct document *document, int parse) { dom_exception exc; /* returned by libdom functions */ - dom_document *doc = NULL; /* document, loaded into libdom */ + dom_html_document *doc = NULL; /* document, loaded into libdom */ dom_node *root = NULL; /* root element of document */ void *mapa = NULL; void *mapa_rev = NULL; @@ -652,7 +652,7 @@ void walk2(struct document *document) { dom_exception exc; /* returned by libdom functions */ - dom_document *doc = NULL; /* document, loaded into libdom */ + dom_html_document *doc = NULL; /* document, loaded into libdom */ dom_node *root = NULL; /* root element of document */ if (!document->dom) { @@ -799,7 +799,7 @@ void try_to_color(struct terminal *term, struct el_box *box, struct document *document, int vx, int vy) { dom_exception exc; /* returned by libdom functions */ - dom_document *doc = NULL; /* document, loaded into libdom */ + dom_html_document *doc = NULL; /* document, loaded into libdom */ dom_node *root = NULL; /* root element of document */ if (!document->dom) { diff --git a/src/ecmascript/ecmascript.c b/src/ecmascript/ecmascript.c index dfb91d61..5f488cbb 100644 --- a/src/ecmascript/ecmascript.c +++ b/src/ecmascript/ecmascript.c @@ -317,7 +317,7 @@ check_for_rerender(struct ecmascript_interpreter *interpreter, const char* text) struct cache_entry *cached = document->cached; #ifdef CONFIG_ECMASCRIPT_SMJS if (interpreter->document_obj) { - dom_document *doc = JS::GetMaybePtrFromReservedSlot((JSObject *)interpreter->document_obj, 0); + dom_html_document *doc = JS::GetMaybePtrFromReservedSlot((JSObject *)interpreter->document_obj, 0); if (doc) { document->dom = doc; @@ -326,7 +326,7 @@ check_for_rerender(struct ecmascript_interpreter *interpreter, const char* text) #endif #ifdef CONFIG_QUICKJS if (JS_IsObject(interpreter->document_obj)) { - dom_document *doc = js_doc_getopaque(interpreter->document_obj); + dom_html_document *doc = js_doc_getopaque(interpreter->document_obj); if (doc) { document->dom = doc; diff --git a/src/ecmascript/libdom/parse.c b/src/ecmascript/libdom/parse.c index 4622a988..b4ad1099 100644 --- a/src/ecmascript/libdom/parse.c +++ b/src/ecmascript/libdom/parse.c @@ -21,7 +21,7 @@ void el_insert_before(struct document *document, void *element, struct string *source) { - dom_document *doc = (dom_document *)document->dom; + dom_html_document *doc = (dom_html_document *)document->dom; dom_node *node = (dom_node *)element; dom_string *text = NULL; dom_exception exc = dom_string_create((const unsigned char *)source->source, source->length, &text); diff --git a/src/ecmascript/quickjs/collection.c b/src/ecmascript/quickjs/collection.c index 5278eb12..d850f835 100644 --- a/src/ecmascript/quickjs/collection.c +++ b/src/ecmascript/quickjs/collection.c @@ -59,6 +59,7 @@ void js_htmlColection_finalizer(JSRuntime *rt, JSValue val) dom_html_collection *ns = (dom_html_collection *)(js_htmlCollection_GetOpaque(val)); if (ns) { + attr_erase_from_map_str(map_collections, ns); dom_html_collection_unref(ns); } } diff --git a/src/ecmascript/quickjs/document.c b/src/ecmascript/quickjs/document.c index 2d254ccf..5039fce1 100644 --- a/src/ecmascript/quickjs/document.c +++ b/src/ecmascript/quickjs/document.c @@ -347,7 +347,7 @@ js_document_get_property_doctype(JSContext *ctx, JSValueConst this_val) #endif REF_JS(this_val); - dom_document *doc = (struct dom_document *)js_doc_getopaque(this_val); + dom_html_document *doc = (struct dom_html_document *)js_doc_getopaque(this_val); if (!doc) { return JS_NULL; @@ -1042,7 +1042,7 @@ js_document_addEventListener(JSContext *ctx, JSValueConst this_val, int argc, JS if (!doc_private) { return JS_NULL; } - dom_document *doc = doc_private->node; + dom_html_document *doc = doc_private->node; if (argc < 2) { return JS_UNDEFINED; @@ -1126,7 +1126,7 @@ js_document_dispatchEvent(JSContext *ctx, JSValueConst this_val, int argc, JSVal if (!doc_private) { return JS_FALSE; } - dom_document *doc = doc_private->node; + dom_html_document *doc = doc_private->node; if (!doc) { return JS_FALSE; @@ -1167,7 +1167,7 @@ js_document_removeEventListener(JSContext *ctx, JSValueConst this_val, int argc, if (!doc_private) { return JS_NULL; } - dom_document *doc = (dom_document *)doc_private->node; + dom_html_document *doc = (dom_html_document *)doc_private->node; dom_node_ref(doc); if (argc < 2) { @@ -1282,7 +1282,7 @@ js_document_createDocumentFragment(JSContext *ctx, JSValueConst this_val, int ar if (argc != 0) { return JS_FALSE; } - dom_document *doc = (struct dom_document *)js_doc_getopaque(this_val); + dom_html_document *doc = (struct dom_html_document *)js_doc_getopaque(this_val); if (!doc) { return JS_NULL; @@ -1312,7 +1312,7 @@ js_document_createElement(JSContext *ctx, JSValueConst this_val, int argc, JSVal if (argc != 1) { return JS_FALSE; } - dom_document *doc = (struct dom_document *)js_doc_getopaque(this_val); + dom_html_document *doc = (struct dom_html_document *)js_doc_getopaque(this_val); if (!doc) { return JS_NULL; @@ -1361,7 +1361,7 @@ js_document_createTextNode(JSContext *ctx, JSValueConst this_val, int argc, JSVa if (argc != 1) { return JS_FALSE; } - dom_document *doc = (struct dom_document *)js_doc_getopaque(this_val); + dom_html_document *doc = (struct dom_html_document *)js_doc_getopaque(this_val); if (!doc) { return JS_NULL; @@ -1538,7 +1538,7 @@ js_document_getElementsByTagName(JSContext *ctx, JSValueConst this_val, int argc if (argc != 1) { return JS_FALSE; } - dom_document *doc = (struct dom_document *)js_doc_getopaque(this_val); + dom_html_document *doc = (struct dom_html_document *)js_doc_getopaque(this_val); if (!doc) { return JS_NULL; diff --git a/src/ecmascript/quickjs/element.c b/src/ecmascript/quickjs/element.c index 61c5a1c8..3ed225ee 100644 --- a/src/ecmascript/quickjs/element.c +++ b/src/ecmascript/quickjs/element.c @@ -3620,6 +3620,9 @@ void js_element_finalizer(JSRuntime *rt, JSValue val) } if (el) { + void *old_node_data = NULL; + dom_node_set_user_data(el, corestring_dom___ns_key_html_content_data, NULL, js_html_document_user_data_handler, + (void *) &old_node_data); dom_node_unref(el); } @@ -3717,7 +3720,6 @@ getElement(JSContext *ctx, void *node) } init_list(el_private->listeners); el_private->node = node; - dom_node_ref((dom_node *)node); struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); el_private->interpreter = interpreter;