diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-01-30 20:50:23 +0200 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2022-01-31 21:05:04 +0200 |
commit | 96af50bbbaa0b70b5614952e7c16ae91defb2348 (patch) | |
tree | 70af317ae6b4d9366fd07e5a7c105df0ad1cf512 /Userland/Libraries | |
parent | cea6c81c7738ead1d2b95a25c42ccb2b5f5f11e7 (diff) | |
download | serenity-96af50bbbaa0b70b5614952e7c16ae91defb2348.zip |
LibJS: Implement the Intl CreateSegmentDataObject AO
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Intl/Segmenter.cpp | 49 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Intl/Segmenter.h | 1 |
3 files changed, 51 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 32873d98f8..a261f00a63 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -291,6 +291,7 @@ namespace JS { P(isoNanosecond) \ P(isoSecond) \ P(isoYear) \ + P(isWordLike) \ P(italics) \ P(join) \ P(keyFor) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.cpp b/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.cpp index 8571c9501c..3bccf69645 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.cpp @@ -6,6 +6,7 @@ #include <AK/BinarySearch.h> #include <AK/Utf16View.h> +#include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Intl/Segmenter.h> #include <LibUnicode/CharacterTypes.h> @@ -43,6 +44,54 @@ StringView Segmenter::segmenter_granularity_string() const } } +// 18.7.1 CreateSegmentDataObject ( segmenter, string, startIndex, endIndex ), https://tc39.es/ecma402/#sec-createsegmentdataobject +Object* create_segment_data_object(GlobalObject& global_object, Segmenter const& segmenter, Utf16View const& string, double start_index, double end_index) +{ + auto& vm = global_object.vm(); + + // 1. Let len be the length of string. + auto length = string.length_in_code_units(); + + // 2. Assert: startIndex ≥ 0. + VERIFY(start_index >= 0); + + // 3. Assert: endIndex ≤ len. + VERIFY(end_index <= length); + + // 4. Assert: startIndex < endIndex. + VERIFY(start_index < end_index); + + // 5. Let result be ! OrdinaryObjectCreate(%Object.prototype%). + auto* result = Object::create(global_object, global_object.object_prototype()); + + // 6. Let segment be the substring of string from startIndex to endIndex. + auto segment = string.substring_view(start_index, end_index - start_index); + + // 7. Perform ! CreateDataPropertyOrThrow(result, "segment", segment). + MUST(result->create_data_property_or_throw(vm.names.segment, js_string(vm, segment))); + + // 8. Perform ! CreateDataPropertyOrThrow(result, "index", 𝔽(startIndex)). + MUST(result->create_data_property_or_throw(vm.names.index, Value(start_index))); + + // 9. Perform ! CreateDataPropertyOrThrow(result, "input", string). + MUST(result->create_data_property_or_throw(vm.names.input, js_string(vm, string))); + + // 10. Let granularity be segmenter.[[SegmenterGranularity]]. + auto granularity = segmenter.segmenter_granularity(); + + // 11. If granularity is "word", then + if (granularity == Segmenter::SegmenterGranularity::Word) { + // a. Let isWordLike be a Boolean value indicating whether the segment in string is "word-like" according to locale segmenter.[[Locale]]. + // TODO + + // b. Perform ! CreateDataPropertyOrThrow(result, "isWordLike", isWordLike). + MUST(result->create_data_property_or_throw(vm.names.isWordLike, Value(false))); + } + + // 12. Return result. + return result; +} + // 18.8.1 FindBoundary ( segmenter, string, startIndex, direction ), https://tc39.es/ecma402/#sec-findboundary double find_boundary(Segmenter const& segmenter, Utf16View const& string, double start_index, Direction direction, Optional<Vector<size_t>>& boundaries_cache) { diff --git a/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.h b/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.h index 652a395ea2..0c466e76d3 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/Segmenter.h @@ -36,6 +36,7 @@ private: SegmenterGranularity m_segmenter_granularity { SegmenterGranularity::Grapheme }; // [[SegmenterGranularity]] }; +Object* create_segment_data_object(GlobalObject&, Segmenter const&, Utf16View const&, double start_index, double end_index); enum class Direction { Before, After, |