Truly independent web browser
LibJS: Convert String objects to primitives without calling valueOf
Andreas Klingcdbb007cfb81
diff --git a/Libraries/LibJS/Rust/src/runtime/realm.rs b/Libraries/LibJS/Rust/src/runtime/realm.rsindex ea0cd836a17..8d76917dd55 100644--- a/Libraries/LibJS/Rust/src/runtime/realm.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm.rs@@ -44,6 +44,9 @@ pub struct RealmStorage {/// Whether %Array.prototype% and %Object.prototype% have no @@isConcatSpreadable (see/// `runtime::realm_watchpoints`).pub(crate) array_prototypes_without_is_concat_spreadable: WatchedCondition,+ /// Whether ToPrimitive of a String object that inherits from %String.prototype% gives its [[StringData]] (see+ /// `runtime::realm_watchpoints`).+ pub(crate) string_objects_convert_to_their_string_data: WatchedCondition,}define_cell!(Realm, Other);@@ -63,6 +66,7 @@ unsafe impl Trace for Realm {self.storage.array_prototypes_without_is_concat_spreadable.trace(visitor);+ self.storage.string_objects_convert_to_their_string_data.trace(visitor);self.array_iterator_next_watchpoint_set.trace(visitor);self.array_iterator_return_watchpoint_set.trace(visitor);}diff --git a/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs b/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rsindex 4d11f5e605f..c60eda89770 100644--- a/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs@@ -26,6 +26,10 @@//! - `array_prototypes_without_is_concat_spreadable`: %Array.prototype% and %Object.prototype% have no//! @@isConcatSpreadable, and %Array.prototype% inherits from %Object.prototype%, so that IsConcatSpreadable of an//! array of the realm without an own one is true. It depends on the sets that fire when either object changes.+//! - `string_objects_convert_to_their_string_data`: %String.prototype% has its builtin `valueOf` and `toString`, and+//! neither it nor %Object.prototype%, which it inherits from, has a @@toPrimitive, so that ToPrimitive of a String+//! object that inherits from %String.prototype% without own such properties gives its [[StringData]]. It depends on+//! the sets that fire when either object changes, and when the values of those properties change.//!//! A change of an intrinsic fires the sets that depend on it, also one that leaves what they stand for in place, such//! as a new property of %Object.prototype%. The next query of a fired set checks whether what it stands for still@@ -40,6 +44,7 @@ use crate::layout::object::Object;use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;use crate::runtime::regexp_prototype::RegExpPrototype;+use crate::runtime::string_prototype::StringPrototype;use crate::runtime::watchpoint_set::WatchpointSet;/// Starts watching the intrinsics of a realm whose intrinsics were just created. Until then, changes of the watched@@ -345,3 +350,44 @@ fn watch_array_prototypes_without_is_concat_spreadable(vm: &Vm, realm: Gc<Realm>}watch_intrinsics(vm, &[array_prototype, object_prototype], &[])}++/// Whether ToPrimitive of a String object of the realm that inherits from %String.prototype% without own `valueOf`,+/// `toString` or @@toPrimitive gives its [[StringData]].+pub fn string_objects_convert_to_their_string_data(vm: &Vm, realm: Gc<Realm>) -> bool {+ realm.storage.string_objects_convert_to_their_string_data.holds(+ vm,+ realm,+ watch_string_objects_convert_to_their_string_data,+ )+}++/// The set that stays valid while ToPrimitive of a String object of the realm that inherits from %String.prototype%+/// without own `valueOf`, `toString` or @@toPrimitive gives its [[StringData]], if it does.+fn watch_string_objects_convert_to_their_string_data(vm: &Vm, realm: Gc<Realm>) -> Option<Gc<WatchpointSet>> {+ let string_prototype = realm.string_prototype(vm);+ let object_prototype = realm.object_prototype();+ let to_primitive_key = PropertyKey::from(vm.well_known_symbols().to_primitive);+ // NB: An accessor property holds an accessor, which is not a function.+ let own_property_value = |key: &PropertyKey| {+ let metadata = string_prototype.shape().lookup(key)?;+ Some(string_prototype.get_direct(metadata.offset))+ };+ // NB: %Object.prototype% has an immutable prototype.+ if string_prototype.shape().prototype() != Some(object_prototype)+ || string_prototype.shape().lookup(&to_primitive_key).is_some()+ || object_prototype.shape().lookup(&to_primitive_key).is_some()+ || !own_property_value(&vm.names.valueOf).is_some_and(|value| StringPrototype::is_value_of_function(vm, value))+ || !own_property_value(&vm.names.toString)+ .is_some_and(|value| StringPrototype::is_to_string_function(vm, value))+ {+ return None;+ }+ watch_intrinsics(+ vm,+ &[string_prototype, object_prototype],+ &[+ (string_prototype, vm.names.valueOf.clone()),+ (string_prototype, vm.names.toString.clone()),+ ],+ )+}diff --git a/Libraries/LibJS/Rust/src/runtime/string_object.rs b/Libraries/LibJS/Rust/src/runtime/string_object.rsindex 66456b9453b..19382b297bb 100644--- a/Libraries/LibJS/Rust/src/runtime/string_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/string_object.rs@@ -24,6 +24,7 @@ use crate::runtime::property_attributes::PropertyAttributes;use crate::runtime::property_descriptor::PropertyDescriptor;use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;+use crate::runtime::realm_watchpoints;/// A String exotic object, whose [[StringData]] is `string`.#[repr(C)]@@ -90,6 +91,31 @@ impl StringObject {self.string}+ /// The [[StringData]] of `object`, if it is a String object whose ToPrimitive gives that without running code: it+ /// inherits from %String.prototype% of the running realm, has no own `valueOf`, `toString` or @@toPrimitive, and+ /// the realm still has the builtin conversion methods (see `realm_watchpoints`).+ pub fn string_data_if_it_converts_to_it(vm: &Vm, object: &Object) -> Option<Gc<PrimitiveString>> {+ if !core::ptr::eq(object.class(), Self::CLASS) || object.has_unimplemented_properties() {+ return None;+ }+ let realm = vm.current_realm()?;+ let shape = object.shape();+ // NB: Every String object has an own "length", so one with no other own property has none of the others.+ let has_own_conversion_method = shape.property_count() > 1+ && (shape.lookup(&vm.names.valueOf).is_some()+ || shape.lookup(&vm.names.toString).is_some()+ || shape+ .lookup(&PropertyKey::from(vm.well_known_symbols().to_primitive))+ .is_some());+ if has_own_conversion_method+ || object.prototype() != Some(realm.string_prototype(vm))+ || !realm_watchpoints::string_objects_convert_to_their_string_data(vm, realm)+ {+ return None;+ }+ Some(as_string_object(object).string)+ }+pub(crate) fn initialize(object: &Object, vm: &Vm, _realm: Gc<Realm>) {let string_object = as_string_object(object);object.define_direct_property(diff --git a/Libraries/LibJS/Rust/src/runtime/string_prototype.rs b/Libraries/LibJS/Rust/src/runtime/string_prototype.rsindex e4d7ae6e217..2bc08fe6260 100644--- a/Libraries/LibJS/Rust/src/runtime/string_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/string_prototype.rs@@ -33,6 +33,7 @@ use crate::runtime::primitive_string::PrimitiveString;use crate::runtime::property_attributes::{Attribute, PropertyAttributes};use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;+use crate::runtime::regexp_constructor::is_raw_native_function_running;use crate::runtime::regexp_object::regexp_create;use crate::runtime::regexp_prototype::RegExpPrototype;use crate::runtime::string_iterator::StringIterator;@@ -222,7 +223,23 @@ pub(crate) static TRIM_FUNCTION: RawNativeFunctionPointer = raw_native!(StringPrpub(crate) static TRIM_START_FUNCTION: RawNativeFunctionPointer = raw_native!(StringPrototype::trim_start);pub(crate) static TRIM_END_FUNCTION: RawNativeFunctionPointer = raw_native!(StringPrototype::trim_end);+/// %String.prototype.toString%, which is_to_string_function() looks for.+static TO_STRING_FUNCTION: RawNativeFunctionPointer = raw_native!(StringPrototype::to_string);++/// %String.prototype.valueOf%, which is_value_of_function() looks for.+static VALUE_OF_FUNCTION: RawNativeFunctionPointer = raw_native!(StringPrototype::value_of);+impl StringPrototype {+ /// Whether `value` is %String.prototype.toString% of some realm.+ pub fn is_to_string_function(vm: &Vm, value: Value) -> bool {+ value.is_function() && is_raw_native_function_running(vm, value.as_function(), TO_STRING_FUNCTION)+ }++ /// Whether `value` is %String.prototype.valueOf% of some realm.+ pub fn is_value_of_function(vm: &Vm, value: Value) -> bool {+ value.is_function() && is_raw_native_function_running(vm, value.as_function(), VALUE_OF_FUNCTION)+ }+/// `S.indexOf(searchString, position)` of the string `S`, a string/// `searchString` and an integral `position`, which runs no code.pub fn index_of_string(string: Gc<PrimitiveString>, search_string: Gc<PrimitiveString>, position: f64) -> Value {@@ -336,7 +353,7 @@ impl StringPrototype {None,);define(&names.toLowerCase, TO_LOWERCASE_FUNCTION, 0, None);- define(&names.toString, raw_native!(StringPrototype::to_string), 0, None);+ define(&names.toString, TO_STRING_FUNCTION, 0, None);define(&names.toUpperCase, TO_UPPERCASE_FUNCTION, 0, None);define(&names.toWellFormed,@@ -347,7 +364,7 @@ impl StringPrototype {define(&names.trim, TRIM_FUNCTION, 0, None);define(&names.trimEnd, TRIM_END_FUNCTION, 0, None);define(&names.trimStart, TRIM_START_FUNCTION, 0, None);- define(&names.valueOf, raw_native!(StringPrototype::value_of), 0, None);+ define(&names.valueOf, VALUE_OF_FUNCTION, 0, None);define(&PropertyKey::from(vm.well_known_symbols().iterator),raw_native!(StringPrototype::symbol_iterator),diff --git a/Libraries/LibJS/Rust/src/runtime/value.rs b/Libraries/LibJS/Rust/src/runtime/value.rsindex 21d7bad8dc1..8d2b6f65d1b 100644--- a/Libraries/LibJS/Rust/src/runtime/value.rs+++ b/Libraries/LibJS/Rust/src/runtime/value.rs@@ -419,6 +419,10 @@ impl Value {{return Ok(Value::from_string(string));}+ // OPTIMIZATION: A String object with the builtin conversion methods converts to its [[StringData]].+ if let Some(string) = StringObject::string_data_if_it_converts_to_it(vm, &object) {+ return Ok(Value::from_string(string));+ }// a. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).let exotic_to_primitive = Self::finish_get_method(