1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "builtins_segments.h"
17 
18 #include "ecmascript/js_segment_iterator.h"
19 #include "ecmascript/js_segments.h"
20 
21 namespace panda::ecmascript::builtins {
22 // %SegmentsPrototype%.containing ( index )
Containing(EcmaRuntimeCallInfo *argv)23 JSTaggedValue BuiltinsSegments::Containing(EcmaRuntimeCallInfo *argv)
24 {
25     JSThread *thread = argv->GetThread();
26     BUILTINS_API_TRACE(thread, Segments, Containing);
27     [[maybe_unused]] EcmaHandleScope scope(thread);
28 
29     // 1. Let segments be the this value.
30     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
31 
32     // 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
33     if (!thisValue->IsJSSegments()) {
34         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Segments object", JSTaggedValue::Exception());
35     }
36 
37     // 6. Let n be ? ToIntegerOrInfinity(index).
38     JSHandle<JSTaggedValue> indexTag = GetCallArg(argv, 0);
39     JSTaggedNumber indexVal = JSTaggedValue::ToInteger(thread, indexTag);
40     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
41     JSHandle<JSSegments> segments = JSHandle<JSSegments>::Cast(thisValue);
42     return JSSegments::Containing(thread, segments, indexVal.GetNumber());
43 }
44 
45 // %SegmentsPrototype% [ @@iterator ] ( )
GetSegmentIterator(EcmaRuntimeCallInfo *argv)46 JSTaggedValue BuiltinsSegments::GetSegmentIterator(EcmaRuntimeCallInfo *argv)
47 {
48     JSThread *thread = argv->GetThread();
49     BUILTINS_API_TRACE(thread, Segments, GetSegmentIterator);
50     [[maybe_unused]] EcmaHandleScope handleScope(thread);
51     // 1. Let segments be the this value.
52     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
53 
54     // 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
55     if (!thisValue->IsJSSegments()) {
56         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Segments object", JSTaggedValue::Exception());
57     }
58     JSHandle<JSSegments> segments = JSHandle<JSSegments>::Cast(thisValue);
59     // 3. Let segmenter be segments.[[SegmentsSegmenter]].
60     // 4. Let string be segments.[[SegmentsString]].
61     JSHandle<EcmaString> string(thread, segments->GetSegmentsString());
62     // 5. Return ! CreateSegmentIterator(segmenter, string).
63     return JSSegmentIterator::CreateSegmentIterator(thread, segments->GetIcuBreakIterator(), string,
64                                                     segments->GetGranularity()).GetTaggedValue();
65 }
66 }  // namespace panda::ecmascript::builtins