From 1ff1c9c0012e0b96fa1ba5a86e13ee18b9136be0 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 5 Dec 2025 23:30:34 +0100 Subject: [PATCH] Add JS_NewProxy Repackage js_proxy_constructor as a public API that downstream projects can use. Fixes: https://github.com/quickjs-ng/quickjs/issues/1261 --- quickjs.c | 20 +++++++++++--------- quickjs.h | 2 ++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/quickjs.c b/quickjs.c index 5a15e00eb..6c1874840 100644 --- a/quickjs.c +++ b/quickjs.c @@ -48695,23 +48695,19 @@ static const JSClassExoticMethods js_proxy_exotic_methods = { .set_property = js_proxy_set, }; -static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val, - int argc, JSValueConst *argv) +JSValue JS_NewProxy(JSContext *ctx, JSValueConst target, JSValueConst handler) { - JSValueConst target, handler; - JSValue obj; JSProxyData *s; + JSValue obj; - target = argv[0]; - handler = argv[1]; if (JS_VALUE_GET_TAG(target) != JS_TAG_OBJECT || - JS_VALUE_GET_TAG(handler) != JS_TAG_OBJECT) + JS_VALUE_GET_TAG(handler) != JS_TAG_OBJECT) { return JS_ThrowTypeErrorNotAnObject(ctx); - + } obj = JS_NewObjectProtoClass(ctx, JS_NULL, JS_CLASS_PROXY); if (JS_IsException(obj)) return obj; - s = js_malloc(ctx, sizeof(JSProxyData)); + s = js_malloc(ctx, sizeof(*s)); if (!s) { JS_FreeValue(ctx, obj); return JS_EXCEPTION; @@ -48725,6 +48721,12 @@ static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val, return obj; } +static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val, + int argc, JSValueConst *argv) +{ + return JS_NewProxy(ctx, argv[0], argv[1]); +} + static JSValue js_proxy_revoke(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, JSValueConst *func_data) diff --git a/quickjs.h b/quickjs.h index b1ab008d9..c6dd0f4b6 100644 --- a/quickjs.h +++ b/quickjs.h @@ -862,6 +862,8 @@ JS_EXTERN bool JS_IsArray(JSValueConst val); JS_EXTERN bool JS_IsProxy(JSValueConst val); JS_EXTERN JSValue JS_GetProxyTarget(JSContext *ctx, JSValueConst proxy); JS_EXTERN JSValue JS_GetProxyHandler(JSContext *ctx, JSValueConst proxy); +JS_EXTERN JSValue JS_NewProxy(JSContext *ctx, JSValueConst target, + JSValueConst handler); JS_EXTERN JSValue JS_NewDate(JSContext *ctx, double epoch_ms); JS_EXTERN bool JS_IsDate(JSValueConst v);