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_segmenter.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/intl/locale_helper.h"
194514f5e3Sopenharmony_ci#include "ecmascript/js_segments.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
224514f5e3Sopenharmony_ci// 18.1.1 Intl.Segmenter ( [ locales [ , options ] ] )
234514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSegmenter::SegmenterConstructor(EcmaRuntimeCallInfo *argv)
244514f5e3Sopenharmony_ci{
254514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
264514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Segmenter, Constructor);
274514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
284514f5e3Sopenharmony_ci    EcmaVM *ecmaVm = thread->GetEcmaVM();
294514f5e3Sopenharmony_ci    ObjectFactory *factory = ecmaVm->GetFactory();
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ci    // 1. If NewTarget is undefined, throw a TypeError exception.
324514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
334514f5e3Sopenharmony_ci    if (newTarget->IsUndefined()) {
344514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
354514f5e3Sopenharmony_ci    }
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci    // 2. Let internalSlotsList be « [[InitializedSegmenter]], [[Locale]], [[SegmenterGranularity]] ».
384514f5e3Sopenharmony_ci    // 3. Let segmenter be ? OrdinaryCreateFromConstructor(NewTarget, "%Segmenter.prototype%", internalSlotsList).
394514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
404514f5e3Sopenharmony_ci    JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
414514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
424514f5e3Sopenharmony_ci    JSHandle<JSSegmenter> segmenter = JSHandle<JSSegmenter>::Cast(newObject);
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci    // 3. Perform ? InitializeSegmenter(segmenter, locales, options).
454514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
464514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
474514f5e3Sopenharmony_ci    JSSegmenter::InitializeSegmenter(thread, segmenter, locales, options);
484514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
494514f5e3Sopenharmony_ci    return segmenter.GetTaggedValue();
504514f5e3Sopenharmony_ci}
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci// 18.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] )
534514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSegmenter::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
544514f5e3Sopenharmony_ci{
554514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
564514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Segmenter, SupportedLocalesOf);
574514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    // 1. Let availableLocales be %Segmenter%.[[AvailableLocales]].
604514f5e3Sopenharmony_ci    JSHandle<TaggedArray> availableLocales = JSSegmenter::GetAvailableLocales(thread);
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci    // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
634514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
644514f5e3Sopenharmony_ci    JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
654514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
684514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
694514f5e3Sopenharmony_ci    JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
704514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
714514f5e3Sopenharmony_ci    return result.GetTaggedValue();
724514f5e3Sopenharmony_ci}
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ci// 18.3.3 Intl.Segmenter.prototype.segment ( string )
754514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSegmenter::Segment(EcmaRuntimeCallInfo *argv)
764514f5e3Sopenharmony_ci{
774514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
784514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Segmenter, Segment);
794514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    // 1. Let segmenter be the this value.
824514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> thisValue = GetThis(argv);
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    // 2. Perform ? RequireInternalSlot(segmenter, [[InitializedSegmenter]]).
854514f5e3Sopenharmony_ci    if (!thisValue->IsJSSegmenter()) {
864514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "this is not segmenter object", JSTaggedValue::Exception());
874514f5e3Sopenharmony_ci    }
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci    // 3. Let string be ? ToString(string).
904514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> stringValue = GetCallArg(argv, 0);
914514f5e3Sopenharmony_ci    JSHandle<EcmaString> string = JSTaggedValue::ToString(thread, stringValue);
924514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
934514f5e3Sopenharmony_ci    // 4. Return ! CreateSegmentsObject(segmenter, string).
944514f5e3Sopenharmony_ci    JSHandle<JSSegments> segments =
954514f5e3Sopenharmony_ci        JSSegments::CreateSegmentsObject(thread, JSHandle<JSSegmenter>::Cast(thisValue), string);
964514f5e3Sopenharmony_ci    return segments.GetTaggedValue();
974514f5e3Sopenharmony_ci}
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci// 18.3.4 Intl.Segmenter.prototype.resolvedOptions ( )
1004514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSegmenter::ResolvedOptions(EcmaRuntimeCallInfo *argv)
1014514f5e3Sopenharmony_ci{
1024514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1034514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Segmenter, ResolvedOptions);
1044514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    // 1. Let segmenter be the this value.
1074514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> thisValue = GetThis(argv);
1084514f5e3Sopenharmony_ci
1094514f5e3Sopenharmony_ci    // 2. Perform ? RequireInternalSlot(segmenter, [[InitializedSegmenter]]).
1104514f5e3Sopenharmony_ci    if (!thisValue->IsJSSegmenter()) {
1114514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "this is not segmenter object", JSTaggedValue::Exception());
1124514f5e3Sopenharmony_ci    }
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_ci    // 3. Let options be OrdinaryObjectCreate(%Object.prototype%).
1154514f5e3Sopenharmony_ci    auto ecmaVm = thread->GetEcmaVM();
1164514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
1174514f5e3Sopenharmony_ci    ObjectFactory *factory = ecmaVm->GetFactory();
1184514f5e3Sopenharmony_ci    JSHandle<JSFunction> ctor(env->GetObjectFunction());
1194514f5e3Sopenharmony_ci    JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci    // 4. perform resolvedOptions
1224514f5e3Sopenharmony_ci    JSHandle<JSSegmenter> segmenter = JSHandle<JSSegmenter>::Cast(thisValue);
1234514f5e3Sopenharmony_ci    JSSegmenter::ResolvedOptions(thread, segmenter, options);
1244514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1254514f5e3Sopenharmony_ci    // 5. Return options.
1264514f5e3Sopenharmony_ci    return options.GetTaggedValue();
1274514f5e3Sopenharmony_ci}
1284514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins