From 7c18364e9a8f35865a93f1bccbf2f796dc0ad8a9 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 19 Jun 2024 17:34:17 +0200 Subject: [PATCH] [quickjs] document ref|unref --- src/ecmascript/quickjs/document.c | 103 ++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/src/ecmascript/quickjs/document.c b/src/ecmascript/quickjs/document.c index 670fe7dd5..7a3df4ebe 100644 --- a/src/ecmascript/quickjs/document.c +++ b/src/ecmascript/quickjs/document.c @@ -95,14 +95,17 @@ js_document_get_property_anchors(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); dom_html_collection *anchors = NULL; dom_exception exc = dom_html_document_get_anchors(doc, &anchors); if (exc != DOM_NO_ERR || !anchors) { + dom_node_unref(doc); return JS_NULL; } JSValue rr = getCollection(ctx, anchors); JS_FreeValue(ctx, rr); + dom_node_unref(doc); RETURN_JS(rr); } @@ -155,12 +158,16 @@ js_document_get_property_body(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_html_element *body = NULL; dom_exception exc = dom_html_document_get_body(doc, &body); if (exc != DOM_NO_ERR || !body) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getElement(ctx, body); } @@ -283,10 +290,13 @@ js_document_get_property_childNodes(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_element *root = NULL; dom_exception exc = dom_document_get_document_element(doc, &root); if (exc != DOM_NO_ERR || !root) { + dom_node_unref(doc); return JS_NULL; } dom_nodelist *nodes = NULL; @@ -294,8 +304,10 @@ js_document_get_property_childNodes(JSContext *ctx, JSValueConst this_val) dom_node_unref(root); if (exc != DOM_NO_ERR || !nodes) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getNodeList(ctx, nodes); } @@ -325,8 +337,11 @@ js_document_get_property_doctype(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_document_type *dtd; dom_document_get_doctype(doc, &dtd); + dom_node_unref(doc); return getDoctype(ctx, dtd); } @@ -345,12 +360,16 @@ js_document_get_property_documentElement(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_html_element *root = NULL; dom_exception exc = dom_document_get_document_element(doc, &root); if (exc != DOM_NO_ERR || !root) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getElement(ctx, root); } @@ -434,14 +453,18 @@ js_document_get_property_forms(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_html_collection *forms = NULL; dom_exception exc = dom_html_document_get_forms(doc, &forms); if (exc != DOM_NO_ERR || !forms) { + dom_node_unref(doc); return JS_NULL; } JSValue rr = getForms(ctx, forms); JS_FreeValue(ctx, rr); + dom_node_unref(doc); RETURN_JS(rr); } @@ -475,14 +498,18 @@ js_document_get_property_images(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_html_collection *images = NULL; dom_exception exc = dom_html_document_get_images(doc, &images); if (exc != DOM_NO_ERR || !images) { + dom_node_unref(doc); return JS_NULL; } JSValue rr = getCollection(ctx, images); JS_FreeValue(ctx, rr); + dom_node_unref(doc); RETURN_JS(rr); } @@ -512,14 +539,17 @@ js_document_get_property_links(JSContext *ctx, JSValueConst this_val) if (!doc) { return JS_NULL; } + dom_node_ref(doc); dom_html_collection *links = NULL; dom_exception exc = dom_html_document_get_links(doc, &links); if (exc != DOM_NO_ERR || !links) { + dom_node_unref(doc); return JS_NULL; } JSValue rr = getCollection(ctx, links); JS_FreeValue(ctx, rr); + dom_node_unref(doc); RETURN_JS(rr); } @@ -1006,8 +1036,10 @@ js_document_addEventListener(JSContext *ctx, JSValueConst this_val, int argc, JS return JS_NULL; } dom_document *doc = doc_private->node; + dom_node_ref(doc); if (argc < 2) { + dom_node_unref(doc); return JS_UNDEFINED; } const char *str; @@ -1015,6 +1047,7 @@ js_document_addEventListener(JSContext *ctx, JSValueConst this_val, int argc, JS str = JS_ToCStringLen(ctx, &len, argv[0]); if (!str) { + dom_node_unref(doc); return JS_EXCEPTION; } char *method = stracpy(str); @@ -1033,12 +1066,14 @@ js_document_addEventListener(JSContext *ctx, JSValueConst this_val, int argc, JS if (JS_VALUE_GET_PTR(l->fun) == JS_VALUE_GET_PTR(fun)) { mem_free(method); + dom_node_unref(doc); return JS_UNDEFINED; } } struct document_listener *n = (struct document_listener *)mem_calloc(1, sizeof(*n)); if (!n) { + dom_node_unref(doc); return JS_UNDEFINED; } n->fun = fun; @@ -1052,6 +1087,7 @@ js_document_addEventListener(JSContext *ctx, JSValueConst this_val, int argc, JS exc = dom_event_listener_create(document_event_handler, doc_private, &doc_private->listener); if (exc != DOM_NO_ERR || !doc_private->listener) { + dom_node_unref(doc); return JS_UNDEFINED; } } @@ -1072,6 +1108,7 @@ ex: dom_string_unref(typ); } dom_event_listener_unref(doc_private->listener); + dom_node_unref(doc); return JS_UNDEFINED; } @@ -1094,15 +1131,26 @@ js_document_dispatchEvent(JSContext *ctx, JSValueConst this_val, int argc, JSVal if (!doc) { return JS_FALSE; } + dom_node_ref(doc); if (argc < 1) { + dom_node_unref(doc); return JS_FALSE; } JSValue eve = argv[0]; dom_event *event = (dom_event *)(JS_GetOpaque(eve, js_event_class_id)); + + if (event) { + dom_event_ref(event); + } bool result = false; dom_exception exc = dom_event_target_dispatch_event(doc, event, &result); + if (event) { + dom_event_unref(event); + } + dom_node_unref(doc); + return JS_NewBool(ctx, result); } @@ -1120,8 +1168,10 @@ js_document_removeEventListener(JSContext *ctx, JSValueConst this_val, int argc, return JS_NULL; } dom_document *doc = (dom_document *)doc_private->node; + dom_node_ref(doc); if (argc < 2) { + dom_node_unref(doc); return JS_UNDEFINED; } const char *str; @@ -1129,12 +1179,14 @@ js_document_removeEventListener(JSContext *ctx, JSValueConst this_val, int argc, str = JS_ToCStringLen(ctx, &len, argv[0]); if (!str) { + dom_node_unref(doc); return JS_EXCEPTION; } char *method = stracpy(str); JS_FreeCString(ctx, str); if (!method) { + dom_node_unref(doc); return JS_EXCEPTION; } JSValue fun = argv[1]; @@ -1159,11 +1211,13 @@ js_document_removeEventListener(JSContext *ctx, JSValueConst this_val, int argc, mem_free_set(&l->typ, NULL); mem_free(l); mem_free(method); + dom_node_unref(doc); return JS_UNDEFINED; } } mem_free(method); + dom_node_unref(doc); return JS_UNDEFINED; } @@ -1185,6 +1239,8 @@ js_document_createComment(JSContext *ctx, JSValueConst this_val, int argc, JSVal if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_string *data = NULL; dom_exception exc; const char *str; @@ -1193,12 +1249,14 @@ js_document_createComment(JSContext *ctx, JSValueConst this_val, int argc, JSVal str = JS_ToCStringLen(ctx, &len, argv[0]); if (!str) { + dom_node_unref(doc); return JS_EXCEPTION; } exc = dom_string_create((const uint8_t *)str, len, &data); JS_FreeCString(ctx, str); if (exc != DOM_NO_ERR || !data) { + dom_node_unref(doc); return JS_NULL; } dom_comment *comment = NULL; @@ -1206,8 +1264,10 @@ js_document_createComment(JSContext *ctx, JSValueConst this_val, int argc, JSVal dom_string_unref(data); if (exc != DOM_NO_ERR || !comment) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getElement(ctx, comment); } @@ -1229,12 +1289,16 @@ js_document_createDocumentFragment(JSContext *ctx, JSValueConst this_val, int ar if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_document_fragment *fragment = NULL; dom_exception exc = dom_document_create_document_fragment(doc, &fragment); if (exc != DOM_NO_ERR || !fragment) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getElement(ctx, fragment); } @@ -1256,6 +1320,8 @@ js_document_createElement(JSContext *ctx, JSValueConst this_val, int argc, JSVal if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_string *tag_name = NULL; dom_exception exc; const char *str; @@ -1264,12 +1330,14 @@ js_document_createElement(JSContext *ctx, JSValueConst this_val, int argc, JSVal str = JS_ToCStringLen(ctx, &len, argv[0]); if (!str) { + dom_node_unref(doc); return JS_EXCEPTION; } exc = dom_string_create((const uint8_t *)str, len, &tag_name); JS_FreeCString(ctx, str); if (exc != DOM_NO_ERR || !tag_name) { + dom_node_unref(doc); return JS_NULL; } dom_element *element = NULL; @@ -1277,8 +1345,10 @@ js_document_createElement(JSContext *ctx, JSValueConst this_val, int argc, JSVal dom_string_unref(tag_name); if (exc != DOM_NO_ERR || !element) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getElement(ctx, element); } @@ -1300,6 +1370,8 @@ js_document_createTextNode(JSContext *ctx, JSValueConst this_val, int argc, JSVa if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_string *data = NULL; dom_exception exc; const char *str; @@ -1308,12 +1380,14 @@ js_document_createTextNode(JSContext *ctx, JSValueConst this_val, int argc, JSVa str = JS_ToCStringLen(ctx, &len, argv[0]); if (!str) { + dom_node_unref(doc); return JS_EXCEPTION; } exc = dom_string_create((const uint8_t *)str, len, &data); JS_FreeCString(ctx, str); if (exc != DOM_NO_ERR || !data) { + dom_node_unref(doc); return JS_NULL; } dom_text *text_node = NULL; @@ -1321,8 +1395,10 @@ js_document_createTextNode(JSContext *ctx, JSValueConst this_val, int argc, JSVa dom_string_unref(data); if (exc != DOM_NO_ERR || !text_node) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getElement(ctx, text_node); } @@ -1344,6 +1420,8 @@ js_document_getElementById(JSContext *ctx, JSValueConst this_val, int argc, JSVa if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_string *id = NULL; dom_exception exc; const char *str; @@ -1352,12 +1430,14 @@ js_document_getElementById(JSContext *ctx, JSValueConst this_val, int argc, JSVa str = JS_ToCStringLen(ctx, &len, argv[0]); if (!str) { + dom_node_unref(doc); return JS_EXCEPTION; } exc = dom_string_create((const uint8_t *)str, len, &id); JS_FreeCString(ctx, str); if (exc != DOM_NO_ERR || !id) { + dom_node_unref(doc); return JS_NULL; } dom_element *element = NULL; @@ -1365,8 +1445,10 @@ js_document_getElementById(JSContext *ctx, JSValueConst this_val, int argc, JSVa dom_string_unref(id); if (exc != DOM_NO_ERR || !element) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); return getElement(ctx, element); } @@ -1482,6 +1564,8 @@ js_document_getElementsByTagName(JSContext *ctx, JSValueConst this_val, int argc if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_string *tagname = NULL; dom_exception exc; const char *str; @@ -1490,12 +1574,14 @@ js_document_getElementsByTagName(JSContext *ctx, JSValueConst this_val, int argc str = JS_ToCStringLen(ctx, &len, argv[0]); if (!str) { + dom_node_unref(doc); return JS_EXCEPTION; } exc = dom_string_create((const uint8_t *)str, len, &tagname); JS_FreeCString(ctx, str); if (exc != DOM_NO_ERR || !tagname) { + dom_node_unref(doc); return JS_NULL; } dom_nodelist *nodes = NULL; @@ -1503,9 +1589,11 @@ js_document_getElementsByTagName(JSContext *ctx, JSValueConst this_val, int argc dom_string_unref(tagname); if (exc != DOM_NO_ERR || !nodes) { + dom_node_unref(doc); return JS_NULL; } JSValue rr = getNodeList(ctx, nodes); + dom_node_unref(doc); RETURN_JS(rr); } @@ -1527,17 +1615,21 @@ js_document_querySelector(JSContext *ctx, JSValueConst this_val, int argc, JSVal if (!doc) { return JS_NULL; } + dom_node_ref(doc); + dom_node *root = NULL; /* root element of document */ /* Get root element */ dom_exception exc = dom_document_get_document_element(doc, &root); if (exc != DOM_NO_ERR) { + dom_node_unref(doc); return JS_NULL; } size_t len; const char *selector = JS_ToCStringLen(ctx, &len, argv[0]); if (!selector) { + dom_node_unref(doc); dom_node_unref(root); return JS_NULL; } @@ -1546,8 +1638,11 @@ js_document_querySelector(JSContext *ctx, JSValueConst this_val, int argc, JSVal dom_node_unref(root); if (!ret) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); + return getElement(ctx, ret); } @@ -1568,17 +1663,20 @@ js_document_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JS if (!doc) { return JS_NULL; } + dom_node_ref(doc); dom_node *doc_root = NULL; /* root element of document */ /* Get root element */ dom_exception exc = dom_document_get_document_element(doc, &doc_root); if (exc != DOM_NO_ERR) { + dom_node_unref(doc); return JS_NULL; } size_t len; const char *selector = JS_ToCStringLen(ctx, &len, argv[0]); if (!selector) { + dom_node_unref(doc); dom_node_unref(doc_root); return JS_NULL; } @@ -1587,6 +1685,7 @@ js_document_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JS exc = dom_string_create((const uint8_t *)"B", 1, &tag_name); if (exc != DOM_NO_ERR || !tag_name) { + dom_node_unref(doc); dom_node_unref(doc_root); JS_FreeCString(ctx, selector); return JS_NULL; @@ -1596,6 +1695,7 @@ js_document_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JS dom_string_unref(tag_name); if (exc != DOM_NO_ERR || !element) { + dom_node_unref(doc); dom_node_unref(doc_root); JS_FreeCString(ctx, selector); return JS_NULL; @@ -1609,8 +1709,11 @@ js_document_querySelectorAll(JSContext *ctx, JSValueConst this_val, int argc, JS dom_node_unref(element); if (exc != DOM_NO_ERR || !nodes) { + dom_node_unref(doc); return JS_NULL; } + dom_node_unref(doc); + return getNodeList(ctx, nodes); }