Truly independent web browser
LibJS: Look through call and apply frames for the legacy caller
Shannon Booth4ed56ec97c6e
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rsindex 608d4956038..4dc0e89b0fa 100644--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs@@ -23,8 +23,8 @@ use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::environment::{Environment, PrivateEnvironment};use crate::layout::execution_context::{ExecutionContext, ScriptOrModule};-use crate::layout::function_object::ecmascript_function_flags;pub use crate::layout::function_object::{EcmascriptFunctionObject, EcmascriptFunctionObjectWithRareData};+use crate::layout::function_object::{RawNativeFunction, ecmascript_function_flags};use crate::layout::member::NullableMember;use crate::layout::object::{IndexedStorageKind, Object};use crate::layout::value::Value;@@ -39,6 +39,7 @@ use crate::runtime::error::ErrorKind;use crate::runtime::error_types::ErrorType;use crate::runtime::function_environment::FunctionEnvironment;use crate::runtime::function_object::{FUNCTION_OBJECT_METHODS, FunctionObject};+use crate::runtime::function_prototype::FunctionPrototype;use crate::runtime::generator_object::{GeneratingFunction, GeneratorObject};use crate::runtime::intrinsics::Intrinsics;use crate::runtime::object::{@@ -49,6 +50,7 @@ use crate::runtime::property_attributes::{Attribute, PropertyAttributes};use crate::runtime::property_descriptor::PropertyDescriptor;use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;+use crate::runtime::reflect_object::ReflectObject;use crate::runtime::shared_function_instance_data::{ClassFieldInitializerName, ClosureCount, ConstructorKind, FunctionKind, SharedFunctionInstanceData, ThisMode,};@@ -168,6 +170,15 @@ pub fn value_as_ecmascript_function_object(value: Value) -> Option<Gc<Ecmascriptas_ecmascript_function_object(value.as_object())}+fn performs_prepare_for_tail_call(vm: &Vm, function: Gc<FunctionObject>) -> bool {+ let Some(function) = function.upcast::<Object>().downcast::<RawNativeFunction>() else {+ return false;+ };+ FunctionPrototype::is_call_function(vm, &function)+ || FunctionPrototype::is_apply_function(vm, &function)+ || ReflectObject::is_apply_function(vm, &function)+}+fn prototype_for_function_kind(realm: Gc<Realm>, kind: FunctionKind) -> Gc<Object> {match kind {FunctionKind::Normal => realm.function_prototype(),@@ -1118,6 +1129,12 @@ impl EcmascriptFunctionObject {return ControlFlow::Continue(());};+ // NB: The spec has these built-ins perform PrepareForTailCall(), which discards their execution context+ // before they call this function. We don't implement it, so skip the frames we keep for them.+ if performs_prepare_for_tail_call(vm, function) {+ return ControlFlow::Continue(());+ }+caller = as_ecmascript_function_object(function);ControlFlow::Break(())});diff --git a/Libraries/LibJS/Rust/src/runtime/function_prototype.rs b/Libraries/LibJS/Rust/src/runtime/function_prototype.rsindex a365c383efd..bd6529b9b5f 100644--- a/Libraries/LibJS/Rust/src/runtime/function_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/function_prototype.rs@@ -92,7 +92,11 @@ define_object_class!(FunctionPrototype, extends: [FunctionObject, Object], methopub(crate) static APPLY_FUNCTION: RawNativeFunctionPointer = raw_native!(FunctionPrototype::apply);pub(crate) static CALL_FUNCTION: RawNativeFunctionPointer = raw_native!(FunctionPrototype::call);-fn is_native_function(vm: &Vm, function: &RawNativeFunction, native_function: RawNativeFunctionPointer) -> bool {+pub(crate) fn is_native_function(+ vm: &Vm,+ function: &RawNativeFunction,+ native_function: RawNativeFunctionPointer,+) -> bool {let address = |pointer: RawNativeFunctionPointer| pointer.map(|pointer| pointer as usize);address(function.native_function(vm)) == address(native_function)}diff --git a/Libraries/LibJS/Rust/src/runtime/reflect_object.rs b/Libraries/LibJS/Rust/src/runtime/reflect_object.rsindex 553471e5aac..f67bc9c5cd3 100644--- a/Libraries/LibJS/Rust/src/runtime/reflect_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/reflect_object.rs@@ -9,13 +9,16 @@ use libjs_runtime_macros::Trace;use crate::gc::class::{GcCell, define_cell};use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;+use crate::layout::function_object::RawNativeFunction;use crate::layout::object::Object;use crate::layout::value::Value;+use crate::layout_forward::RawNativeFunctionPointer;use crate::runtime::abstract_operations::{call_function_object, construct, create_list_from_array_like};use crate::runtime::array::Array;use crate::runtime::completion::ThrowCompletionOr;use crate::runtime::error::ErrorKind;use crate::runtime::error_types::ErrorType;+use crate::runtime::function_prototype::is_native_function;use crate::runtime::native_function::raw_native;use crate::runtime::object::{MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, PropertyLookupPhase, define_object_class,@@ -37,7 +40,14 @@ define_object_class!(ReflectObject, extends: [Object], methods: {..ORDINARY_OBJECT_METHODS});+static APPLY_FUNCTION: RawNativeFunctionPointer = raw_native!(ReflectObject::apply);+impl ReflectObject {+ /// Whether the function is Reflect.apply (of any realm).+ pub fn is_apply_function(vm: &Vm, function: &RawNativeFunction) -> bool {+ is_native_function(vm, function, APPLY_FUNCTION)+ }+pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<ReflectObject> {realm.create_object(vm, |_| ReflectObject {base: Object::new_with_prototype(@@ -55,7 +65,7 @@ impl ReflectObject {let define_native_function = |property_key: &PropertyKey, function, length| {object.define_native_function(vm, realm, property_key, function, length, attr, None);};- define_native_function(&names.apply, raw_native!(ReflectObject::apply), 3);+ define_native_function(&names.apply, APPLY_FUNCTION, 3);define_native_function(&names.construct, raw_native!(ReflectObject::construct), 2);define_native_function(&names.defineProperty, raw_native!(ReflectObject::define_property), 3);define_native_function(&names.deleteProperty, raw_native!(ReflectObject::delete_property), 2);diff --git a/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.js b/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.jsindex ec65f6e64f4..36ee930be9e 100644--- a/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.js+++ b/Tests/LibJS/Runtime/functions/function-legacy-caller-arguments.js@@ -23,6 +23,45 @@ test("legacy caller property exposes only ordinary sloppy callers", () => {expect([1].map(getCaller)[0]).toBeNull();});+test("legacy caller property looks through Function.prototype.call, apply and Reflect.apply", () => {+ function getCaller() {+ return getCaller.caller;+ }++ function viaCall() {+ return getCaller.call(null);+ }++ function viaApply() {+ return getCaller.apply(null, []);+ }++ function viaReflectApply() {+ return Reflect.apply(getCaller, null, []);+ }++ function viaCallOfCall() {+ return Function.prototype.call.call(getCaller, null);+ }++ function viaApplyOfReflectApply() {+ return Reflect.apply.apply(null, [getCaller, null, []]);+ }++ function strictViaCall() {+ "use strict";+ return getCaller.call(null);+ }++ expect(viaCall()).toBe(viaCall);+ expect(viaApply()).toBe(viaApply);+ expect(viaReflectApply()).toBe(viaReflectApply);+ 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", () => {function getArguments(a, b) {return getArguments.arguments;