]> git.kaiwu.me - quickjs.git/commitdiff
Fix Function.prototype.toString for a revoked proxy (bardrop)
authorFabrice Bellard <fabrice@bellard.org>
Sat, 21 Nov 2026 16:07:34 +0000 (17:07 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Sat, 21 Nov 2026 16:07:34 +0000 (17:07 +0100)
quickjs.c
tests/test_builtin.js

index 1544e4f33d9483d4596e2a2c198284253d942e06..7d48a7dca1e06db36ae790ffdc1a70f620b847fe 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -42613,7 +42613,10 @@ static JSValue js_function_toString(JSContext *ctx, JSValueConst this_val,
             break;
         }
         suff = "() {\n    [native code]\n}";
-        name = JS_GetProperty(ctx, this_val, JS_ATOM_name);
+        if (p->class_id == JS_CLASS_PROXY)
+            name = JS_UNDEFINED;
+        else
+            name = JS_GetProperty(ctx, this_val, JS_ATOM_name);
         if (JS_IsUndefined(name))
             name = JS_AtomToString(ctx, JS_ATOM_empty_string);
         return JS_ConcatString3(ctx, pref, name, suff);
index 7fcc4bd34d8c3dfe550edd0979608c89d57aee75..9c647d16757b54cb409a1a004b677800fb389490 100644 (file)
@@ -119,6 +119,20 @@ function test_function()
     g = constructor1.bind(null, 1);
     r = new g();
     assert(r.x, 1);
+
+    // a callable proxy stays callable after revoke, so toString works
+    var native = "function () {\n    [native code]\n}";
+    g = Proxy.revocable(function f() {}, {});
+    g.revoke();
+    assert(Function.prototype.toString.call(g.proxy), native);
+    // and it does not read "name" through the proxy
+    var keys = [];
+    g = new Proxy(function f() {}, {
+        get(t, k) { keys.push(k); return Reflect.get(t, k); }
+    });
+    assert(Function.prototype.toString.call(g), native);
+    assert(keys.length, 0);
+    assert_throws(TypeError, () => Function.prototype.toString.call(new Proxy({}, {})));
 }
 
 function test()