diff --git a/quickjs.c b/quickjs.c index 475232c..879d071 100644 --- a/quickjs.c +++ b/quickjs.c @@ -37874,6 +37874,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) @@ -37916,8 +37917,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: @@ -37931,6 +37933,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) { @@ -37938,8 +37942,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; @@ -37957,13 +37966,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; } @@ -37980,6 +37993,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 c92afbc..4687478 100644 --- a/quickjs.h +++ b/quickjs.h @@ -1116,6 +1116,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 */