1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[spidermonkey] Free strings allocated in get_elements_by_class_name

This commit is contained in:
Witold Filipczyk 2024-10-22 16:09:49 +02:00
parent 14c9eff83c
commit 88fdf42ade
3 changed files with 38 additions and 8 deletions

View File

@ -27,6 +27,7 @@
#include "document/libdom/corestrings.h"
#include "document/view.h"
#include "js/ecmascript.h"
#include "js/ecmascript-c.h"
#include "js/spidermonkey/collection.h"
#include "js/spidermonkey/element.h"
#include "intl/libintl.h"
@ -56,7 +57,8 @@
enum {
SLOT_NODE = 0,
SLOT_ITEM_NAMEDITEM,
SLOT_ARRAY
SLOT_ARRAY,
SLOT_WAS_CLASS_NAME
};
static bool htmlCollection2_item(JSContext *ctx, unsigned int argc, JS::Value *rval);
@ -69,12 +71,26 @@ static void htmlCollection_finalize(JS::GCContext *op, JSObject *obj)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
bool was_class_name = JS::GetReservedSlot(obj, SLOT_WAS_CLASS_NAME).toBoolean();
if (was_class_name) {
struct el_dom_html_collection *ns = JS::GetMaybePtrFromReservedSlot<el_dom_html_collection>(obj, SLOT_NODE);
if (ns) {
if (ns->refcnt > 0) {
free_el_dom_collection(ns->ctx);
ns->ctx = NULL;
dom_html_collection_unref((dom_html_collection *)ns);
}
}
} else {
dom_html_collection *ns = JS::GetMaybePtrFromReservedSlot<dom_html_collection>(obj, SLOT_NODE);
if (ns) {
dom_html_collection_unref(ns);
}
}
}
static void htmlCollection2_finalize(JS::GCContext *op, JSObject *obj)
{
@ -192,7 +208,7 @@ js::ObjectOps col_obj_ops = {
JSClass htmlCollection_class = {
"htmlCollection",
JSCLASS_HAS_RESERVED_SLOTS(3),
JSCLASS_HAS_RESERVED_SLOTS(4),
&htmlCollection_ops,
.oOps = &col_obj_ops
};
@ -506,8 +522,8 @@ htmlCollection_set_items(JSContext *ctx, JS::HandleObject hobj, void *node)
return true;
}
JSObject *
getCollection(JSContext *ctx, void *node)
static JSObject *
getCollection_common(JSContext *ctx, void *node, bool was_class_name)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
@ -545,6 +561,19 @@ getCollection(JSContext *ctx, void *node)
JS::SetReservedSlot(el, SLOT_NODE, JS::PrivateValue(node));
JS::SetReservedSlot(el, SLOT_ITEM_NAMEDITEM, JS::ObjectValue(*col));
JS::SetReservedSlot(el, SLOT_ARRAY, JS::ObjectValue(*arr));
JS::SetReservedSlot(el, SLOT_WAS_CLASS_NAME, JS::BooleanValue(was_class_name));
return el;
}
JSObject *
getCollection(JSContext *ctx, void *node)
{
return getCollection_common(ctx, node, false);
}
JSObject *
getCollection2(JSContext *ctx, void *node)
{
return getCollection_common(ctx, node, true);
}

View File

@ -4,5 +4,6 @@
#include "js/spidermonkey/util.h"
JSObject *getCollection(JSContext *ctx, void *node);
JSObject *getCollection2(JSContext *ctx, void *node);
#endif

View File

@ -2004,7 +2004,7 @@ document_getElementsByClassName(JSContext *ctx, unsigned int argc, JS::Value *vp
args.rval().setNull();
return true;
}
JSObject *obj = getCollection(ctx, col);
JSObject *obj = getCollection2(ctx, col);
args.rval().setObject(*obj);
return true;