From 93d1ab715b6efeab9679ce995ca369331d24fd88 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 13 Sep 2024 17:20:41 -0700 Subject: [PATCH 1/2] Add `JS_SetPropertyFunctionList2()` --- quickjs.c | 19 +++++++++++++++++++ quickjs.h | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/quickjs.c b/quickjs.c index 642ae3429..9c72fd3b3 100644 --- a/quickjs.c +++ b/quickjs.c @@ -36998,6 +36998,25 @@ void JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj, } } +int JS_SetPropertyFunctionList2(JSContext *ctx, JSValueConst obj, + const JSCFunctionListEntry *tab, int len) +{ + int i; + + for (i = 0; i < len; i++) { + const JSCFunctionListEntry *e = &tab[i]; + JSAtom atom = find_atom(ctx, e->name); + if (atom == JS_ATOM_NULL) + return -1; + if (JS_InstantiateFunctionListItem(ctx, obj, atom, e) < 0) { + JS_FreeAtom(ctx, atom); + return -1; + } + JS_FreeAtom(ctx, atom); + } + return 0; +} + int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m, const JSCFunctionListEntry *tab, int len) { diff --git a/quickjs.h b/quickjs.h index edc7b47b3..5fd65da64 100644 --- a/quickjs.h +++ b/quickjs.h @@ -1060,6 +1060,10 @@ typedef struct JSCFunctionListEntry { void JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj, const JSCFunctionListEntry *tab, int len); +/* same as JS_SetPropertyFunctionList() returns -1 if exception, 0 if OK */ +int JS_SetPropertyFunctionList2(JSContext *ctx, JSValueConst obj, + const JSCFunctionListEntry *tab, + int len); /* C module definition */ From 752a3cac22e29416faa496a6e8320188d75b8c00 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 13 Sep 2024 17:09:17 -0700 Subject: [PATCH 2/2] Fix error handling in `JS_InstantiateFunctionListItem()` --- quickjs.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/quickjs.c b/quickjs.c index 9c72fd3b3..b824319ab 100644 --- a/quickjs.c +++ b/quickjs.c @@ -36892,6 +36892,7 @@ static JSValue JS_InstantiateFunctionListItem2(JSContext *ctx, JSObject *p, return val; } +/* return -1 if exception, 0 if OK */ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj, JSAtom atom, const JSCFunctionListEntry *e) @@ -36934,8 +36935,9 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj, /* Function.prototype[Symbol.hasInstance] is not writable nor configurable */ prop_flags = 0; } - JS_DefineAutoInitProperty(ctx, obj, atom, JS_AUTOINIT_ID_PROP, - (void *)e, prop_flags); + if (JS_DefineAutoInitProperty(ctx, obj, atom, JS_AUTOINIT_ID_PROP, + (void *)e, prop_flags) < 0) + return -1; return 0; case JS_DEF_CGETSET: /* XXX: use autoinit again ? */ case JS_DEF_CGETSET_MAGIC: @@ -36949,6 +36951,8 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj, getter = JS_NewCFunction2(ctx, e->u.getset.get.generic, buf, 0, e->def_type == JS_DEF_CGETSET_MAGIC ? JS_CFUNC_getter_magic : JS_CFUNC_getter, e->magic); + if (JS_IsException(getter)) + return -1; } setter = JS_UNDEFINED; if (e->u.getset.set.generic) { @@ -36956,8 +36960,13 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj, setter = JS_NewCFunction2(ctx, e->u.getset.set.generic, buf, 1, e->def_type == JS_DEF_CGETSET_MAGIC ? JS_CFUNC_setter_magic : JS_CFUNC_setter, e->magic); + if (JS_IsException(setter)) { + JS_FreeValue(ctx, getter); + return -1; + } } - JS_DefinePropertyGetSet(ctx, obj, atom, getter, setter, prop_flags); + if (JS_DefinePropertyGetSet(ctx, obj, atom, getter, setter, prop_flags) < 0) + return -1; return 0; } break; @@ -36975,13 +36984,17 @@ static int JS_InstantiateFunctionListItem(JSContext *ctx, JSValueConst obj, break; case JS_DEF_PROP_STRING: case JS_DEF_OBJECT: - JS_DefineAutoInitProperty(ctx, obj, atom, JS_AUTOINIT_ID_PROP, - (void *)e, prop_flags); + if (JS_DefineAutoInitProperty(ctx, obj, atom, JS_AUTOINIT_ID_PROP, + (void *)e, prop_flags) < 0) + return -1; return 0; default: abort(); } - JS_DefinePropertyValue(ctx, obj, atom, val, prop_flags); + if (JS_DefinePropertyValue(ctx, obj, atom, val, prop_flags) < 0) { + JS_FreeValue(ctx, val); + return -1; + } return 0; }