From adfb0b084a08ee9a161ec93a96f15ea8942ad474 Mon Sep 17 00:00:00 2001 From: Haled Odat Date: Tue, 11 Apr 2023 05:46:37 +0200 Subject: [PATCH] Implement `trimLeft` and `trimRight` --- boa_engine/src/builtins/string/mod.rs | 42 +++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/boa_engine/src/builtins/string/mod.rs b/boa_engine/src/builtins/string/mod.rs index e3bd98019d5..a1c50541a87 100644 --- a/boa_engine/src/builtins/string/mod.rs +++ b/boa_engine/src/builtins/string/mod.rs @@ -70,9 +70,37 @@ impl IntrinsicObject for String { let symbol_iterator = JsSymbol::iterator(); + let trim_start = BuiltInBuilder::new(realm) + .callable(Self::trim_start) + .length(0) + .name("trimStart") + .build(); + + let trim_end = BuiltInBuilder::new(realm) + .callable(Self::trim_end) + .length(0) + .name("trimEnd") + .build(); + + #[cfg(feature = "annex-b")] + let trim_left = trim_start.clone(); + + #[cfg(feature = "annex-b")] + let trim_right = trim_end.clone(); + let attribute = Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT; let builder = BuiltInBuilder::from_standard_constructor::(realm) .property(utf16!("length"), 0, attribute) + .property( + utf16!("trimStart"), + trim_start, + Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, + ) + .property( + utf16!("trimEnd"), + trim_end, + Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, + ) .static_method(Self::raw, "raw", 1) .static_method(Self::from_char_code, "fromCharCode", 1) .static_method(Self::from_code_point, "fromCodePoint", 1) @@ -90,12 +118,10 @@ impl IntrinsicObject for String { .method(Self::last_index_of, "lastIndexOf", 1) .method(Self::locale_compare, "localeCompare", 1) .method(Self::r#match, "match", 1) - .method(Self::normalize, "normalize", 1) + .method(Self::normalize, "normalize", 0) .method(Self::pad_end, "padEnd", 1) .method(Self::pad_start, "padStart", 1) .method(Self::trim, "trim", 0) - .method(Self::trim_start, "trimStart", 0) - .method(Self::trim_end, "trimEnd", 0) .method(Self::to_lowercase, "toLowerCase", 0) .method(Self::to_uppercase, "toUpperCase", 0) .method(Self::substring, "substring", 2) @@ -112,6 +138,16 @@ impl IntrinsicObject for String { #[cfg(feature = "annex-b")] { builder + .property( + utf16!("trimLeft"), + trim_left, + Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, + ) + .property( + utf16!("trimRight"), + trim_right, + Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE, + ) .method(Self::anchor, "anchor", 1) .method(Self::big, "big", 0) .method(Self::blink, "blink", 0)