Skip to content

Truly independent web browser

LibJS: Expose sloppy arrow and method callers through fn.caller

Shannon Booth3da673b9b4cc
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
index 4dc0e89b0fa..4bb42b7b61a 100644
--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
@@ -1140,9 +1140,12 @@ impl EcmascriptFunctionObject {
});
// NB: https://tc39.es/ecma262/#sec-forbidden-extensions keeps own "caller" properties from exposing strict
- // functions. The accessor on %Function.prototype% holds to the same rule.
+ // functions. The accessor on %Function.prototype% holds to the same rule, and also hides generator and
+ // async functions.
match caller {
- Some(caller) if caller.supports_legacy_caller_or_arguments() => Value::from_object(caller),
+ Some(caller) if caller.kind() == FunctionKind::Normal && !caller.is_strict_mode() => {
+ Value::from_object(caller)
+ }
_ => Value::NULL,
}
}
diff --git a/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.js b/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.js
index 36ee930be9e..5a5ce4c3b10 100644
--- a/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.js
+++ b/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.js
@@ -1,4 +1,4 @@
-test("legacy caller property exposes only ordinary sloppy callers", () => {
+test("legacy caller property exposes only sloppy non-generator, non-async callers", () => {
function getCaller() {
return getCaller.caller;
}
@@ -7,22 +7,55 @@ test("legacy caller property exposes only ordinary sloppy callers", () => {
return getCaller();
}
+ const arrow = () => getCaller();
+
+ const object = {
+ method() {
+ return getCaller();
+ },
+ get getter() {
+ return getCaller();
+ },
+ };
+
function strict() {
"use strict";
return getCaller();
}
+ class C {
+ method() {
+ return getCaller();
+ }
+ }
+
function* generator() {
yield getCaller();
}
- expect(getCaller()).toBeNull();
expect(ordinary()).toBe(ordinary);
+ expect(arrow()).toBe(arrow);
+ expect(object.method()).toBe(object.method);
+ expect(object.getter).toBe(Object.getOwnPropertyDescriptor(object, "getter").get);
expect(strict()).toBeNull();
+ expect(new C().method()).toBeNull();
expect(generator().next().value).toBeNull();
expect([1].map(getCaller)[0]).toBeNull();
});
+test("legacy caller property hides async callers", () => {
+ function getCaller() {
+ return getCaller.caller;
+ }
+
+ let result;
+ async function asyncFunction() {
+ result = getCaller();
+ }
+ asyncFunction();
+ expect(result).toBeNull();
+});
+
test("legacy caller property looks through Function.prototype.call, apply and Reflect.apply", () => {
function getCaller() {
return getCaller.caller;
@@ -59,7 +92,6 @@ test("legacy caller property looks through Function.prototype.call, apply and Re
expect(viaCallOfCall()).toBe(viaCallOfCall);
expect(viaApplyOfReflectApply()).toBe(viaApplyOfReflectApply);
expect(strictViaCall()).toBeNull();
- expect(getCaller.call(null)).toBeNull();
});
test("legacy arguments property exposes the active arguments object", () => {
@@ -208,8 +240,12 @@ test("assigning to legacy caller and arguments does nothing", () => {
return [ordinary.caller, ordinary.arguments];
}
- const [caller, argumentsObject] = ordinary(3);
- expect(caller).toBeNull();
+ function outer() {
+ return ordinary(3);
+ }
+
+ const [caller, argumentsObject] = outer();
+ expect(caller).toBe(outer);
expect(argumentsObject[0]).toBe(3);
expect(Object.hasOwn(ordinary, "caller")).toBeFalse();
expect(Object.hasOwn(ordinary, "arguments")).toBeFalse();