Issue #1952 - m-c 1440468: Proxied functions can't be passed to Function.prototype.toString.call()

This commit is contained in:
Martok 2022-07-01 17:36:42 +02:00 committed by roytam1
commit 887e6d0ab3
4 changed files with 11 additions and 15 deletions

View file

@ -3,12 +3,7 @@ var functionProxy = new Proxy(function() {}, {});
assertEq(Object.prototype.toString.call(objectProxy), '[object Object]');
assertEq(Object.prototype.toString.call(functionProxy), '[object Function]');
try {
Function.prototype.toString.call(functionProxy);
assertEq(true, false);
} catch (e) {
assertEq(!!/incompatible/.exec(e), true);
}
try {
Function.prototype.toString.call(objectProxy);
assertEq(true, false);

View file

@ -1,10 +1,10 @@
load(libdir + 'asserts.js');
// Function.prototype.toString doesn't accept ES6 proxies.
var nativeCode = "function () {\n [native code]\n}";
var proxy = new Proxy(function() {}, {});
assertThrowsInstanceOf(() => Function.prototype.toString.call(proxy), TypeError);
assertEq(Function.prototype.toString.call(proxy), nativeCode);
var o = Proxy.revocable(function() {}, {});
assertThrowsInstanceOf(() => Function.prototype.toString.call(o.proxy), TypeError);
assertEq(Function.prototype.toString.call(o.proxy), nativeCode);
o.revoke();
assertThrowsInstanceOf(() => Function.prototype.toString.call(o.proxy), TypeError);
assertEq(Function.prototype.toString.call(o.proxy), nativeCode);

View file

@ -319,8 +319,9 @@ BaseProxyHandler::fun_toString(JSContext* cx, HandleObject proxy, bool isToSourc
{
if (proxy->isCallable())
return JS_NewStringCopyZ(cx, "function () {\n [native code]\n}");
RootedValue v(cx, ObjectValue(*proxy));
ReportIsNotFunction(cx, v);
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO,
js_Function_str, js_toString_str, "object");
return nullptr;
}

View file

@ -1259,9 +1259,9 @@ ScriptedProxyHandler::className(JSContext* cx, HandleObject proxy) const
JSString*
ScriptedProxyHandler::fun_toString(JSContext* cx, HandleObject proxy, bool isToSource) const
{
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO,
js_Function_str, js_toString_str, "object");
return nullptr;
// The BaseProxyHandler has the desired behavior: Throw for non-callable,
// otherwise return [native code].
return BaseProxyHandler::fun_toString(cx, proxy, isToSource);
}
bool