Truly independent web browser
LibJS: Implement the legacy constructor mode of Intl formats
Andreas Klingb087ec1a79f1
diff --git a/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_constructor.rs b/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_constructor.rsindex bd532129e60..69a3ca898ba 100644--- a/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_constructor.rs+++ b/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_constructor.rs@@ -29,12 +29,15 @@ use crate::runtime::intl::abstract_operations::{use crate::runtime::intl::date_time_format::{DateTimeFormat, adjust_date_time_style_format, for_each_calendar_field, get_date_time_format,};+use crate::runtime::intl::intl::Intl;use crate::runtime::intrinsics::Intrinsics;use crate::runtime::native_function::{NativeFunction, define_native_function_class, raw_native};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::temporal::iso8601::{SubMinutePrecision, parse_utc_offset};+use crate::runtime::value::ordinary_has_instance;use crate::unicode::date_time_format::{self as unicode, CalendarPattern, CalendarPatternField, CalendarPatternFieldValue, HourCycle,};@@ -98,11 +101,19 @@ impl DateTimeFormatConstructor {// 11.1.1 Intl.DateTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.datetimeformatfn call(function: &NativeFunction, vm: &Vm) -> ThrowCompletionOr<Value> {+ let this_value = vm.this_value();+// 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.- Ok(Value::from_object(Self::construct(- function,+ let date_time_format = Self::construct(function, vm, function.as_function_object_gc())?;++ // 3. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then+ // a. Let this be the this value.+ // b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this).+ Ok(Value::from_object(chain_date_time_format(vm,- function.as_function_object_gc(),+ date_time_format,+ Value::UNDEFINED,+ this_value,)?))}@@ -125,6 +136,7 @@ impl DateTimeFormatConstructor {// 3. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then// a. Let this be the this value.// b. Return ? ChainDateTimeFormat(dateTimeFormat, NewTarget, this).+ // NB: ChainDateTimeFormat returns dateTimeFormat when NewTarget is not undefined, so only [[Call]] performs it.// 4. Return dateTimeFormat.Ok(date_time_format.upcast())@@ -145,6 +157,37 @@ impl DateTimeFormatConstructor {}}+// 11.1.1.1 ChainDateTimeFormat ( dateTimeFormat, newTarget, this ), https://tc39.es/ecma402/#sec-chaindatetimeformat+fn chain_date_time_format(+ vm: &Vm,+ date_time_format: Gc<Object>,+ new_target: Value,+ this_value: Value,+) -> ThrowCompletionOr<Gc<Object>> {+ let realm = vm.current_realm().expect("a constructor runs in a realm");+ let constructor = Value::from_object(realm.intrinsics().intl_date_time_format_constructor(vm));++ // 1. If newTarget is undefined and ? OrdinaryHasInstance(%Intl.DateTimeFormat%, this) is true, then+ if new_target.is_undefined() && ordinary_has_instance(vm, this_value, constructor)?.to_boolean() {+ // a. Perform ? DefinePropertyOrThrow(this, %Intl%.[[FallbackSymbol]], PropertyDescriptor { [[Value]]: dateTimeFormat, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).+ let this_object = this_value.as_object();+ let mut descriptor = PropertyDescriptor {+ value: Some(Value::from_object(date_time_format)),+ writable: Some(false),+ enumerable: Some(false),+ configurable: Some(false),+ ..Default::default()+ };+ this_object.define_property_or_throw(vm, &Intl::fallback_symbol(vm, realm), &mut descriptor)?;++ // b. Return this.+ return Ok(this_object);+ }++ // 2. Return dateTimeFormat.+ Ok(date_time_format)+}+#[derive(Clone, Copy, Debug, PartialEq, Eq)]pub enum OptionRequired {Any,diff --git a/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_prototype.rs b/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_prototype.rsindex e9fe4bb3dba..d14640f8669 100644--- a/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/intl/date_time_format_prototype.rs@@ -20,6 +20,7 @@ use crate::runtime::intl::date_time_format::{format_date_time_range_to_parts, format_date_time_to_parts, to_date_time_formattable,};use crate::runtime::intl::date_time_format_function::DateTimeFormatFunction;+use crate::runtime::intl::intl::Intl;use crate::runtime::native_function::raw_native;use crate::runtime::object::{MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, define_object_class};use crate::runtime::primitive_string::PrimitiveString;@@ -27,6 +28,7 @@ use crate::runtime::property_attributes::{Attribute, PropertyAttributes};use crate::runtime::property_key::PropertyKey;use crate::runtime::prototype_object::typed_this_object;use crate::runtime::realm::Realm;+use crate::runtime::value::ordinary_has_instance;use crate::unicode::date_time_format::{self as unicode, CalendarPatternFieldValue, HourCycle};/// 11.3 Properties of the Intl.DateTimeFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-datetimeformat-prototype-object@@ -45,6 +47,42 @@ fn typed_this_date_time_format(vm: &Vm) -> ThrowCompletionOr<Gc<DateTimeFormat>>typed_this_object::<DateTimeFormat>(vm, "Intl.DateTimeFormat")}+// 11.5.13 UnwrapDateTimeFormat ( dtf ), https://tc39.es/ecma402/#sec-unwrapdatetimeformat+fn unwrap_date_time_format(vm: &Vm, value: Value) -> ThrowCompletionOr<Value> {+ // 1. If dtf is not an Object, throw a TypeError exception.+ if !value.is_object() {+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObject, &[&value]);+ }++ // 2. If dtf does not have an [[InitializedDateTimeFormat]] internal slot and ? OrdinaryHasInstance(%Intl.DateTimeFormat%, dtf) is true, then+ if value.as_object().downcast::<DateTimeFormat>().is_none() {+ let realm = vm.current_realm().expect("a built-in function runs in a realm");+ let constructor = Value::from_object(realm.intrinsics().intl_date_time_format_constructor(vm));+ if ordinary_has_instance(vm, value, constructor)?.to_boolean() {+ // a. Return ? Get(dtf, %Intl%.[[FallbackSymbol]]).+ return value.as_object().get(vm, &Intl::fallback_symbol(vm, realm));+ }+ }++ // 3. Return dtf.+ Ok(value)+}++/// The this value, unwrapped with UnwrapDateTimeFormat, which must have an [[InitializedDateTimeFormat]] internal slot.+fn unwrapped_this_date_time_format(vm: &Vm) -> ThrowCompletionOr<Gc<DateTimeFormat>> {+ let value = unwrap_date_time_format(vm, vm.this_value())?;+ if value.is_object()+ && let Some(date_time_format) = value.as_object().downcast::<DateTimeFormat>()+ {+ return Ok(date_time_format);+ }+ vm.throw_completion(+ ErrorKind::TypeError,+ ErrorType::NotAnObjectOfType,+ &[&"Intl.DateTimeFormat"],+ )+}+impl DateTimeFormatPrototype {pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<DateTimeFormatPrototype> {realm.create_object(@@ -128,7 +166,7 @@ impl DateTimeFormatPrototype {// 2. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then// a. Set dtf to ? UnwrapDateTimeFormat(dtf).// 3. Perform ? RequireInternalSlot(dtf, [[InitializedDateTimeFormat]]).- let date_time_format = typed_this_date_time_format(vm)?;+ let date_time_format = unwrapped_this_date_time_format(vm)?;// 4. Let options be OrdinaryObjectCreate(%Object.prototype%).let options = Object::create(vm, realm, Some(realm.object_prototype()));@@ -220,7 +258,7 @@ impl DateTimeFormatPrototype {// 2. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then// a. Set dtf to ? UnwrapDateTimeFormat(dtf).// 3. Perform ? RequireInternalSlot(dtf, [[InitializedDateTimeFormat]]).- let date_time_format = typed_this_date_time_format(vm)?;+ let date_time_format = unwrapped_this_date_time_format(vm)?;// 4. If dtf.[[BoundFormat]] is undefined, thenif date_time_format.bound_format().is_none() {diff --git a/Libraries/LibJS/Rust/src/runtime/intl/intl.rs b/Libraries/LibJS/Rust/src/runtime/intl/intl.rsindex 796bb0a922d..03abb95c97b 100644--- a/Libraries/LibJS/Rust/src/runtime/intl/intl.rs+++ b/Libraries/LibJS/Rust/src/runtime/intl/intl.rs@@ -27,6 +27,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::symbol::{Kind as SymbolKind, Symbol};use crate::unicode::intl as unicode;use crate::utf16::Utf16View;@@ -35,6 +36,7 @@ use crate::utf16::Utf16View;#[derive(Trace)]pub struct Intl {base: Object,+ fallback_symbol: Gc<Symbol>, // [[FallbackSymbol]]}define_object_class!(Intl, extends: [Object], methods: {@@ -53,10 +55,27 @@ impl Intl {realm.object_prototype(),MayInterfereWithIndexedPropertyAccess::No,),+ // The Intl object has a [[FallbackSymbol]] internal slot, which is a new %Symbol% in the current realm+ // with the [[Description]] "IntlLegacyConstructedSymbol".+ fallback_symbol: Symbol::create(+ vm,+ Some(Utf16String::from_utf8("IntlLegacyConstructedSymbol")),+ SymbolKind::Unique,+ ),},)}+ /// %Intl%.[[FallbackSymbol]] of the given realm.+ pub fn fallback_symbol(vm: &Vm, realm: Gc<Realm>) -> PropertyKey {+ let intl = realm+ .intrinsics()+ .intl_object(vm)+ .downcast::<Intl>()+ .expect("%Intl% is an Intl object");+ PropertyKey::from(intl.fallback_symbol)+ }+fn initialize(object: &Object, vm: &Vm, realm: Gc<Realm>) {let names = &vm.names;diff --git a/Libraries/LibJS/Rust/src/runtime/intl/number_format_constructor.rs b/Libraries/LibJS/Rust/src/runtime/intl/number_format_constructor.rsindex dafe85eadd3..5d669dd7141 100644--- a/Libraries/LibJS/Rust/src/runtime/intl/number_format_constructor.rs+++ b/Libraries/LibJS/Rust/src/runtime/intl/number_format_constructor.rs@@ -22,11 +22,14 @@ use crate::runtime::intl::abstract_operations::{filter_locales, get_boolean_or_string_number_format_option, get_number_option, is_well_formed_currency_code,is_well_formed_unit_identifier, resolve_options, throw_option_is_not_valid_value,};+use crate::runtime::intl::intl::Intl;use crate::runtime::intl::number_format::{ComputedRoundingPriority, NumberFormat, NumberFormatBase, currency_digits};use crate::runtime::intrinsics::Intrinsics;use crate::runtime::native_function::{NativeFunction, define_native_function_class, raw_native};use crate::runtime::property_attributes::{Attribute, PropertyAttributes};+use crate::runtime::property_descriptor::PropertyDescriptor;use crate::runtime::realm::Realm;+use crate::runtime::value::ordinary_has_instance;use crate::unicode::number_format::{self as unicode, Notation, NumberFormatStyle, RoundingType};use crate::utf16::Utf16View;@@ -102,9 +105,21 @@ impl NumberFormatConstructor {// 16.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformatfn call(function: &NativeFunction, vm: &Vm) -> ThrowCompletionOr<Value> {+ let this_value = vm.this_value();+// 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.let number_format = Self::construct(function, vm, function.as_function_object_gc())?;- Ok(Value::from_object(number_format))++ // 26. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then+ // a. Let this be the this value.+ // b. Return ? ChainNumberFormat(numberFormat, NewTarget, this).+ // NB: ChainNumberFormat returns numberFormat when NewTarget is not undefined, so only [[Call]] performs it.+ Ok(Value::from_object(chain_number_format(+ vm,+ number_format,+ Value::UNDEFINED,+ this_value,+ )?))}// 16.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat@@ -293,6 +308,37 @@ impl NumberFormatConstructor {}}+// 16.1.1.1 ChainNumberFormat ( numberFormat, newTarget, this ), https://tc39.es/ecma402/#sec-chainnumberformat+fn chain_number_format(+ vm: &Vm,+ number_format: Gc<Object>,+ new_target: Value,+ this_value: Value,+) -> ThrowCompletionOr<Gc<Object>> {+ let realm = vm.current_realm().expect("a constructor runs in a realm");+ let constructor = Value::from_object(realm.intrinsics().intl_number_format_constructor(vm));++ // 1. If newTarget is undefined and ? OrdinaryHasInstance(%Intl.NumberFormat%, this) is true, then+ if new_target.is_undefined() && ordinary_has_instance(vm, this_value, constructor)?.to_boolean() {+ // a. Perform ? DefinePropertyOrThrow(this, %Intl%.[[FallbackSymbol]], PropertyDescriptor { [[Value]]: numberFormat, [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }).+ let this_object = this_value.as_object();+ let mut descriptor = PropertyDescriptor {+ value: Some(Value::from_object(number_format)),+ writable: Some(false),+ enumerable: Some(false),+ configurable: Some(false),+ ..Default::default()+ };+ this_object.define_property_or_throw(vm, &Intl::fallback_symbol(vm, realm), &mut descriptor)?;++ // b. Return this.+ return Ok(this_object);+ }++ // 2. Return numberFormat.+ Ok(number_format)+}+// 16.1.2 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/ecma402/#sec-setnfdigitoptionspub fn set_number_format_digit_options(vm: &Vm,diff --git a/Libraries/LibJS/Rust/src/runtime/intl/number_format_prototype.rs b/Libraries/LibJS/Rust/src/runtime/intl/number_format_prototype.rsindex 8c3cbf26406..33973df7b8c 100644--- a/Libraries/LibJS/Rust/src/runtime/intl/number_format_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/intl/number_format_prototype.rs@@ -14,6 +14,7 @@ use crate::layout::value::Value;use crate::runtime::completion::{Must, ThrowCompletionOr};use crate::runtime::error::ErrorKind;use crate::runtime::error_types::ErrorType;+use crate::runtime::intl::intl::Intl;use crate::runtime::intl::number_format::{NumberFormat, format_numeric_range, format_numeric_range_to_parts, format_numeric_to_parts,to_intl_mathematical_value,@@ -26,6 +27,7 @@ use crate::runtime::property_attributes::{Attribute, PropertyAttributes};use crate::runtime::property_key::PropertyKey;use crate::runtime::prototype_object::typed_this_object;use crate::runtime::realm::Realm;+use crate::runtime::value::ordinary_has_instance;/// 16.3 Properties of the Intl.NumberFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-numberformat-prototype-object#[repr(C)]@@ -43,6 +45,42 @@ fn typed_this_number_format(vm: &Vm) -> ThrowCompletionOr<Gc<NumberFormat>> {typed_this_object::<NumberFormat>(vm, "Intl.NumberFormat")}+// 16.5.9 UnwrapNumberFormat ( nf ), https://tc39.es/ecma402/#sec-unwrapnumberformat+fn unwrap_number_format(vm: &Vm, value: Value) -> ThrowCompletionOr<Value> {+ // 1. If nf is not an Object, throw a TypeError exception.+ if !value.is_object() {+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObject, &[&value]);+ }++ // 2. If nf does not have an [[InitializedNumberFormat]] internal slot and ? OrdinaryHasInstance(%Intl.NumberFormat%, nf) is true, then+ if value.as_object().downcast::<NumberFormat>().is_none() {+ let realm = vm.current_realm().expect("a built-in function runs in a realm");+ let constructor = Value::from_object(realm.intrinsics().intl_number_format_constructor(vm));+ if ordinary_has_instance(vm, value, constructor)?.to_boolean() {+ // a. Return ? Get(nf, %Intl%.[[FallbackSymbol]]).+ return value.as_object().get(vm, &Intl::fallback_symbol(vm, realm));+ }+ }++ // 3. Return nf.+ Ok(value)+}++/// The this value, unwrapped with UnwrapNumberFormat, which must have an [[InitializedNumberFormat]] internal slot.+fn unwrapped_this_number_format(vm: &Vm) -> ThrowCompletionOr<Gc<NumberFormat>> {+ let value = unwrap_number_format(vm, vm.this_value())?;+ if value.is_object()+ && let Some(number_format) = value.as_object().downcast::<NumberFormat>()+ {+ return Ok(number_format);+ }+ vm.throw_completion(+ ErrorKind::TypeError,+ ErrorType::NotAnObjectOfType,+ &[&"Intl.NumberFormat"],+ )+}+impl NumberFormatPrototype {pub fn create(vm: &Vm, realm: Gc<Realm>) -> Gc<NumberFormatPrototype> {realm.create_object(@@ -126,7 +164,7 @@ impl NumberFormatPrototype {// 2. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then// a. Set nf to ? UnwrapNumberFormat(nf).// 3. Perform ? RequireInternalSlot(nf, [[InitializedNumberFormat]]).- let number_format = typed_this_number_format(vm)?;+ let number_format = unwrapped_this_number_format(vm)?;// 4. Let options be OrdinaryObjectCreate(%Object.prototype%).let options = Object::create(vm, realm, Some(realm.object_prototype()));@@ -231,7 +269,7 @@ impl NumberFormatPrototype {// 2. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then// a. Set nf to ? UnwrapNumberFormat(nf).// 3. Perform ? RequireInternalSlot(nf, [[InitializedNumberFormat]]).- let number_format = typed_this_number_format(vm)?;+ let number_format = unwrapped_this_number_format(vm)?;// 4. If nf.[[BoundFormat]] is undefined, thenif number_format.bound_format().is_none() {