14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "builtins_segments.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/js_segment_iterator.h"
194514f5e3Sopenharmony_ci#include "ecmascript/js_segments.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
224514f5e3Sopenharmony_ci// %SegmentsPrototype%.containing ( index )
234514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSegments::Containing(EcmaRuntimeCallInfo *argv)
244514f5e3Sopenharmony_ci{
254514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
264514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Segments, Containing);
274514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ci    // 1. Let segments be the this value.
304514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> thisValue = GetThis(argv);
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci    // 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
334514f5e3Sopenharmony_ci    if (!thisValue->IsJSSegments()) {
344514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Segments object", JSTaggedValue::Exception());
354514f5e3Sopenharmony_ci    }
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci    // 6. Let n be ? ToIntegerOrInfinity(index).
384514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> indexTag = GetCallArg(argv, 0);
394514f5e3Sopenharmony_ci    JSTaggedNumber indexVal = JSTaggedValue::ToInteger(thread, indexTag);
404514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
414514f5e3Sopenharmony_ci    JSHandle<JSSegments> segments = JSHandle<JSSegments>::Cast(thisValue);
424514f5e3Sopenharmony_ci    return JSSegments::Containing(thread, segments, indexVal.GetNumber());
434514f5e3Sopenharmony_ci}
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci// %SegmentsPrototype% [ @@iterator ] ( )
464514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSegments::GetSegmentIterator(EcmaRuntimeCallInfo *argv)
474514f5e3Sopenharmony_ci{
484514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
494514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Segments, GetSegmentIterator);
504514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
514514f5e3Sopenharmony_ci    // 1. Let segments be the this value.
524514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> thisValue = GetThis(argv);
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci    // 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
554514f5e3Sopenharmony_ci    if (!thisValue->IsJSSegments()) {
564514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Segments object", JSTaggedValue::Exception());
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci    JSHandle<JSSegments> segments = JSHandle<JSSegments>::Cast(thisValue);
594514f5e3Sopenharmony_ci    // 3. Let segmenter be segments.[[SegmentsSegmenter]].
604514f5e3Sopenharmony_ci    // 4. Let string be segments.[[SegmentsString]].
614514f5e3Sopenharmony_ci    JSHandle<EcmaString> string(thread, segments->GetSegmentsString());
624514f5e3Sopenharmony_ci    // 5. Return ! CreateSegmentIterator(segmenter, string).
634514f5e3Sopenharmony_ci    return JSSegmentIterator::CreateSegmentIterator(thread, segments->GetIcuBreakIterator(), string,
644514f5e3Sopenharmony_ci                                                    segments->GetGranularity()).GetTaggedValue();
654514f5e3Sopenharmony_ci}
664514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins