1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 23:27:35 +00:00

LibJS: Implement Intl %SegmentsPrototype%.containing

This commit is contained in:
Idan Horowitz 2022-01-30 21:12:42 +02:00
parent 96af50bbba
commit 366468f1de
4 changed files with 79 additions and 1 deletions

View file

@ -4,9 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Utf16View.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/SegmentIterator.h>
#include <LibJS/Runtime/Intl/Segments.h>
#include <LibJS/Runtime/Intl/SegmentsPrototype.h>
namespace JS::Intl {
@ -25,6 +25,40 @@ void SegmentsPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(*vm.well_known_symbol_iterator(), symbol_iterator, 0, attr);
define_native_function(vm.names.containing, containing, 1, attr);
}
// 18.5.2.1 %SegmentsPrototype%.containing ( index ), https://tc39.es/ecma402/#sec-%segmentsprototype%.containing
JS_DEFINE_NATIVE_FUNCTION(SegmentsPrototype::containing)
{
// 1. Let segments be the this value.
// 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
auto* segments = TRY(typed_this_object(global_object));
// 3. Let segmenter be segments.[[SegmentsSegmenter]].
auto const& segmenter = segments->segments_segmenter();
// 4. Let string be segments.[[SegmentsString]].
auto string = segments->segments_string();
// 5. Let len be the length of string.
auto length = string.length_in_code_units();
// 6. Let n be ? ToIntegerOrInfinity(index).
auto n = TRY(vm.argument(0).to_integer_or_infinity(global_object));
// 7. If n < 0 or n ≥ len, return undefined.
if (n < 0 || n >= length)
return js_undefined();
// 8. Let startIndex be ! FindBoundary(segmenter, string, n, before).
auto start_index = find_boundary(segmenter, string, n, Direction::Before, segments->boundaries_cache());
// 9. Let endIndex be ! FindBoundary(segmenter, string, n, after).
auto end_index = find_boundary(segmenter, string, n, Direction::After, segments->boundaries_cache());
// 10. Return ! CreateSegmentDataObject(segmenter, string, startIndex, endIndex).
return create_segment_data_object(global_object, segmenter, string, start_index, end_index);
}
// 18.5.2.2 %SegmentsPrototype% [ @@iterator ] ( ), https://tc39.es/ecma402/#sec-%segmentsprototype%-@@iterator

View file

@ -20,6 +20,7 @@ public:
virtual ~SegmentsPrototype() override = default;
private:
JS_DECLARE_NATIVE_FUNCTION(containing);
JS_DECLARE_NATIVE_FUNCTION(symbol_iterator);
};