Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Intl.PluralRules #3298

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ intl = [
"dep:sys-locale",
"dep:yoke",
"dep:zerofrom",
"dep:fixed_decimal",
]

fuzz = ["boa_ast/arbitrary", "boa_interner/arbitrary"]
Expand Down Expand Up @@ -93,6 +94,7 @@ writeable = { version = "0.5.2", optional = true }
yoke = { version = "0.7.1", optional = true }
zerofrom = { version = "0.1.2", optional = true }
sys-locale = { version = "0.3.1", optional = true }
fixed_decimal = { version = "0.5.4", features = ["ryu"], optional = true}

[dev-dependencies]
criterion = "0.5.1"
Expand Down
6 changes: 4 additions & 2 deletions boa_engine/src/builtins/intl/collator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use icu_locid::{
use icu_provider::DataLocale;

use crate::{
builtins::{BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
builtins::{
options::get_option, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
},
context::{
intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
BoaProvider,
Expand All @@ -30,7 +32,7 @@ use crate::{

use super::{
locale::{canonicalize_locale_list, resolve_locale, supported_locales, validate_extension},
options::{coerce_options_to_object, get_option, IntlOptions, LocaleMatcher},
options::{coerce_options_to_object, IntlOptions, LocaleMatcher},
Service,
};

Expand Down
24 changes: 20 additions & 4 deletions boa_engine/src/builtins/intl/collator/options.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::str::FromStr;

use icu_collator::{CaseLevel, Strength};
use icu_collator::{CaseFirst, CaseLevel, Strength};

use crate::builtins::intl::options::OptionTypeParsable;
use crate::{
builtins::options::{OptionType, ParsableOptionType},
Context, JsNativeError, JsResult, JsValue,
};

#[derive(Debug, Clone, Copy)]
pub(crate) enum Sensitivity {
Expand Down Expand Up @@ -47,7 +50,7 @@ impl FromStr for Sensitivity {
}
}

impl OptionTypeParsable for Sensitivity {}
impl ParsableOptionType for Sensitivity {}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) enum Usage {
Expand Down Expand Up @@ -77,4 +80,17 @@ impl FromStr for Usage {
}
}

impl OptionTypeParsable for Usage {}
impl ParsableOptionType for Usage {}

impl OptionType for CaseFirst {
fn from_value(value: JsValue, context: &mut Context<'_>) -> JsResult<Self> {
match value.to_string(context)?.to_std_string_escaped().as_str() {
"upper" => Ok(Self::UpperFirst),
"lower" => Ok(Self::LowerFirst),
"false" => Ok(Self::Off),
_ => Err(JsNativeError::range()
.with_message("provided string was not `upper`, `lower` or `false`")
.into()),
}
}
}
6 changes: 3 additions & 3 deletions boa_engine/src/builtins/intl/date_time_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//! [spec]: https://tc39.es/ecma402/#datetimeformat-objects

use crate::{
builtins::{BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
builtins::{
options::OptionType, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
error::JsNativeError,
js_string,
Expand All @@ -22,8 +24,6 @@ use boa_gc::{Finalize, Trace};
use boa_profiler::Profiler;
use icu_datetime::options::preferences::HourCycle;

use super::options::OptionType;

impl OptionType for HourCycle {
fn from_value(value: JsValue, context: &mut Context<'_>) -> JsResult<Self> {
match value.to_string(context)?.to_std_string_escaped().as_str() {
Expand Down
7 changes: 5 additions & 2 deletions boa_engine/src/builtins/intl/list_format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use icu_locid::Locale;
use icu_provider::DataLocale;

use crate::{
builtins::{Array, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
builtins::{
options::{get_option, get_options_object},
Array, BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
object::{internal_methods::get_prototype_from_constructor, JsObject, ObjectData},
property::Attribute,
Expand All @@ -18,7 +21,7 @@ use crate::{

use super::{
locale::{canonicalize_locale_list, resolve_locale, supported_locales},
options::{get_option, get_options_object, IntlOptions, LocaleMatcher},
options::{IntlOptions, LocaleMatcher},
Service,
};

Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/intl/list_format/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str::FromStr;
use icu_list::ListLength;

use crate::{
builtins::intl::options::{OptionType, OptionTypeParsable},
builtins::options::{OptionType, ParsableOptionType},
Context, JsNativeError, JsResult, JsValue,
};

Expand Down Expand Up @@ -37,7 +37,7 @@ impl FromStr for ListFormatType {
}
}

impl OptionTypeParsable for ListFormatType {}
impl ParsableOptionType for ListFormatType {}

impl OptionType for ListLength {
fn from_value(value: JsValue, context: &mut Context<'_>) -> JsResult<Self> {
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/intl/locale/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{realm::Realm, string::utf16};
use crate::{builtins::options::get_option, realm::Realm, string::utf16};
use boa_profiler::Profiler;
use icu_collator::CaseFirst;
use icu_datetime::options::preferences::HourCycle;
Expand Down Expand Up @@ -26,7 +26,7 @@ use crate::{
Context, JsArgs, JsNativeError, JsResult, JsString, JsValue,
};

use super::options::{coerce_options_to_object, get_option};
use super::options::coerce_options_to_object;

#[derive(Debug, Clone)]
pub(crate) struct Locale;
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/intl/locale/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use icu_locid::extensions::unicode::Value;

use crate::{builtins::intl::options::OptionType, Context, JsNativeError};
use crate::{builtins::options::OptionType, Context, JsNativeError};

impl OptionType for Value {
fn from_value(value: crate::JsValue, context: &mut Context<'_>) -> crate::JsResult<Self> {
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/builtins/intl/locale/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
builtins::{
intl::{
options::{coerce_options_to_object, get_option, IntlOptions, LocaleMatcher},
options::{coerce_options_to_object, IntlOptions, LocaleMatcher},
Service,
},
options::get_option,
Array,
},
context::{icu::Icu, BoaProvider},
Expand Down
13 changes: 12 additions & 1 deletion boa_engine/src/builtins/intl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ pub(crate) mod collator;
pub(crate) mod date_time_format;
pub(crate) mod list_format;
pub(crate) mod locale;
pub(crate) mod number_format;
pub(crate) mod plural_rules;
pub(crate) mod segmenter;

pub(crate) use self::{
collator::Collator, date_time_format::DateTimeFormat, list_format::ListFormat, locale::Locale,
segmenter::Segmenter,
plural_rules::PluralRules, segmenter::Segmenter,
};

mod options;
Expand Down Expand Up @@ -73,6 +75,15 @@ impl IntrinsicObject for Intl {
realm.intrinsics().constructors().segmenter().constructor(),
Segmenter::ATTRIBUTE,
)
.static_property(
PluralRules::NAME,
realm
.intrinsics()
.constructors()
.plural_rules()
.constructor(),
PluralRules::ATTRIBUTE,
)
.static_property(
DateTimeFormat::NAME,
realm
Expand Down
4 changes: 4 additions & 0 deletions boa_engine/src/builtins/intl/number_format/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod options;
mod utils;
pub(crate) use options::*;
pub(crate) use utils::*;
Loading
Loading