Skip to content

Commit be664ca

Browse files
authored
Add JS_NewProxy (#1263)
Repackage js_proxy_constructor as a public API that downstream projects can use. Fixes: #1261
1 parent e0746cc commit be664ca

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

quickjs.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48695,23 +48695,19 @@ static const JSClassExoticMethods js_proxy_exotic_methods = {
4869548695
.set_property = js_proxy_set,
4869648696
};
4869748697

48698-
static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val,
48699-
int argc, JSValueConst *argv)
48698+
JSValue JS_NewProxy(JSContext *ctx, JSValueConst target, JSValueConst handler)
4870048699
{
48701-
JSValueConst target, handler;
48702-
JSValue obj;
4870348700
JSProxyData *s;
48701+
JSValue obj;
4870448702

48705-
target = argv[0];
48706-
handler = argv[1];
4870748703
if (JS_VALUE_GET_TAG(target) != JS_TAG_OBJECT ||
48708-
JS_VALUE_GET_TAG(handler) != JS_TAG_OBJECT)
48704+
JS_VALUE_GET_TAG(handler) != JS_TAG_OBJECT) {
4870948705
return JS_ThrowTypeErrorNotAnObject(ctx);
48710-
48706+
}
4871148707
obj = JS_NewObjectProtoClass(ctx, JS_NULL, JS_CLASS_PROXY);
4871248708
if (JS_IsException(obj))
4871348709
return obj;
48714-
s = js_malloc(ctx, sizeof(JSProxyData));
48710+
s = js_malloc(ctx, sizeof(*s));
4871548711
if (!s) {
4871648712
JS_FreeValue(ctx, obj);
4871748713
return JS_EXCEPTION;
@@ -48725,6 +48721,12 @@ static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val,
4872548721
return obj;
4872648722
}
4872748723

48724+
static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val,
48725+
int argc, JSValueConst *argv)
48726+
{
48727+
return JS_NewProxy(ctx, argv[0], argv[1]);
48728+
}
48729+
4872848730
static JSValue js_proxy_revoke(JSContext *ctx, JSValueConst this_val,
4872948731
int argc, JSValueConst *argv, int magic,
4873048732
JSValueConst *func_data)

quickjs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,8 @@ JS_EXTERN bool JS_IsArray(JSValueConst val);
862862
JS_EXTERN bool JS_IsProxy(JSValueConst val);
863863
JS_EXTERN JSValue JS_GetProxyTarget(JSContext *ctx, JSValueConst proxy);
864864
JS_EXTERN JSValue JS_GetProxyHandler(JSContext *ctx, JSValueConst proxy);
865+
JS_EXTERN JSValue JS_NewProxy(JSContext *ctx, JSValueConst target,
866+
JSValueConst handler);
865867

866868
JS_EXTERN JSValue JS_NewDate(JSContext *ctx, double epoch_ms);
867869
JS_EXTERN bool JS_IsDate(JSValueConst v);

0 commit comments

Comments
 (0)