ref: 9f5bc0ff812c2ad550396d3506e5f1328bbcce70
parent: 9bd3edcbcd1e63118a2f2311e9b8306e1f7dcc6b
author: Tor Andersson <tor.andersson@artifex.com>
date: Thu Aug 10 13:13:37 EDT 2023
Finalize user data if object allocation fails.
--- a/jsvalue.c
+++ b/jsvalue.c
@@ -481,7 +481,15 @@
void js_newcfunctionx(js_State *J, js_CFunction cfun, const char *name, int length, void *data, js_Finalize finalize)
{
- js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype);
+ js_Object *obj;
+
+ if (js_try(J)) {
+ if (finalize)
+ finalize(J, data);
+ js_throw(J);
+ }
+
+ obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype);
obj->u.c.name = name;
obj->u.c.function = cfun;
obj->u.c.constructor = NULL;
@@ -488,6 +496,9 @@
obj->u.c.length = length;
obj->u.c.data = data;
obj->u.c.finalize = finalize;
+
+ js_endtry(J);
+
js_pushobject(J, obj);
{
js_pushnumber(J, length);
@@ -534,6 +545,12 @@
prototype = js_toobject(J, -1);
js_pop(J, 1);
+ if (js_try(J)) {
+ if (finalize)
+ finalize(J, data);
+ js_throw(J);
+ }
+
obj = jsV_newobject(J, JS_CUSERDATA, prototype);
obj->u.user.tag = tag;
obj->u.user.data = data;
@@ -541,6 +558,9 @@
obj->u.user.put = put;
obj->u.user.delete = delete;
obj->u.user.finalize = finalize;
+
+ js_endtry(J);
+
js_pushobject(J, obj);
}