mirror of
https://gitee.com/openharmony/arkcompiler_ets_runtime
synced 2024-11-24 02:30:03 +00:00
!5916 支持ECMA402(国际化)规范2022Intl.Segmenter
Merge pull request !5916 from 吴文璐/for_segmenter
This commit is contained in:
commit
503bc46f22
6
BUILD.gn
6
BUILD.gn
@ -241,6 +241,9 @@ if (enable_ark_intl) {
|
||||
"ecmascript/builtins/builtins_number_format.cpp",
|
||||
"ecmascript/builtins/builtins_plural_rules.cpp",
|
||||
"ecmascript/builtins/builtins_relative_time_format.cpp",
|
||||
"ecmascript/builtins/builtins_segment_iterator.cpp",
|
||||
"ecmascript/builtins/builtins_segmenter.cpp",
|
||||
"ecmascript/builtins/builtins_segments.cpp",
|
||||
"ecmascript/js_collator.cpp",
|
||||
"ecmascript/js_date_time_format.cpp",
|
||||
"ecmascript/js_displaynames.cpp",
|
||||
@ -249,6 +252,9 @@ if (enable_ark_intl) {
|
||||
"ecmascript/js_number_format.cpp",
|
||||
"ecmascript/js_plural_rules.cpp",
|
||||
"ecmascript/js_relative_time_format.cpp",
|
||||
"ecmascript/js_segment_iterator.cpp",
|
||||
"ecmascript/js_segmenter.cpp",
|
||||
"ecmascript/js_segments.cpp",
|
||||
]
|
||||
} else {
|
||||
if (target_os != "android" && target_os != "ios") {
|
||||
|
@ -108,6 +108,9 @@
|
||||
#include "ecmascript/builtins/builtins_number_format.h"
|
||||
#include "ecmascript/builtins/builtins_plural_rules.h"
|
||||
#include "ecmascript/builtins/builtins_relative_time_format.h"
|
||||
#include "ecmascript/builtins/builtins_segmenter.h"
|
||||
#include "ecmascript/builtins/builtins_segments.h"
|
||||
#include "ecmascript/builtins/builtins_segment_iterator.h"
|
||||
#include "ecmascript/js_collator.h"
|
||||
#include "ecmascript/js_date_time_format.h"
|
||||
#include "ecmascript/js_displaynames.h"
|
||||
@ -116,6 +119,9 @@
|
||||
#include "ecmascript/js_number_format.h"
|
||||
#include "ecmascript/js_plural_rules.h"
|
||||
#include "ecmascript/js_relative_time_format.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
#endif
|
||||
|
||||
#include "ohos/init_data.h"
|
||||
@ -180,6 +186,9 @@ using NumberFormat = builtins::BuiltinsNumberFormat;
|
||||
using Collator = builtins::BuiltinsCollator;
|
||||
using PluralRules = builtins::BuiltinsPluralRules;
|
||||
using DisplayNames = builtins::BuiltinsDisplayNames;
|
||||
using Segmenter = builtins::BuiltinsSegmenter;
|
||||
using Segments = builtins::BuiltinsSegments;
|
||||
using SegmentIterator = builtins::BuiltinsSegmentIterator;
|
||||
using ListFormat = builtins::BuiltinsListFormat;
|
||||
#endif
|
||||
using BuiltinsCjsModule = builtins::BuiltinsCjsModule;
|
||||
@ -467,6 +476,8 @@ void Builtins::Initialize(const JSHandle<GlobalEnv> &env, JSThread *thread, bool
|
||||
LazyInitializePluralRules(env);
|
||||
LazyInitializeDisplayNames(env);
|
||||
LazyInitializeListFormat(env);
|
||||
LazyInitializeSegments(env);
|
||||
LazyInitializeSegmenter(env);
|
||||
} else {
|
||||
InitializeLocale(env);
|
||||
InitializeDateTimeFormat(env);
|
||||
@ -476,6 +487,8 @@ void Builtins::Initialize(const JSHandle<GlobalEnv> &env, JSThread *thread, bool
|
||||
InitializePluralRules(env);
|
||||
InitializeDisplayNames(env);
|
||||
InitializeListFormat(env);
|
||||
InitializeSegments(env);
|
||||
InitializeSegmenter(env);
|
||||
}
|
||||
#endif
|
||||
InitializeModuleNamespace(env, objFuncClass);
|
||||
@ -1849,6 +1862,9 @@ void Builtins::InitializeIterator(const JSHandle<GlobalEnv> &env, const JSHandle
|
||||
InitializeArrayIterator(env, iteratorFuncClass);
|
||||
InitializeStringIterator(env, iteratorFuncClass);
|
||||
InitializeRegexpIterator(env, iteratorFuncClass);
|
||||
#ifdef ARK_SUPPORT_INTL
|
||||
InitializeSegmentIterator(env, iteratorFuncClass);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Builtins::InitializeAsyncIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
|
||||
@ -3468,6 +3484,87 @@ void Builtins::InitializeListFormat(const JSHandle<GlobalEnv> &env)
|
||||
// 13.4.5 Intl.ListFormat.prototype.resolvedOptions()
|
||||
SetFunction(env, lfPrototype, "resolvedOptions", ListFormat::ResolvedOptions, FunctionLength::ZERO);
|
||||
}
|
||||
|
||||
void Builtins::InitializeSegmenter(const JSHandle<GlobalEnv> &env)
|
||||
{
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread_);
|
||||
// Segmenter.prototype
|
||||
JSHandle<JSFunction> objFun(env->GetObjectFunction());
|
||||
JSHandle<JSObject> sgPrototype = factory_->NewJSObjectByConstructor(objFun);
|
||||
JSHandle<JSTaggedValue> sgPrototypeValue(sgPrototype);
|
||||
|
||||
// Segmenter.prototype_or_hclass
|
||||
JSHandle<JSHClass> sgFuncInstanceHClass =
|
||||
factory_->NewEcmaHClass(JSSegmenter::SIZE, JSType::JS_SEGMENTER, sgPrototypeValue);
|
||||
|
||||
// Segmenter = new Function()
|
||||
// 18.3.1 Intl.Segmenter.prototype.constructor
|
||||
JSHandle<JSObject> sgFunction(NewIntlConstructor(env, sgPrototype, Segmenter::SegmenterConstructor,
|
||||
"Segmenter", FunctionLength::ZERO));
|
||||
JSFunction::SetFunctionPrototype(thread_,
|
||||
JSHandle<JSFunction>(sgFunction), sgFuncInstanceHClass.GetTaggedValue());
|
||||
|
||||
// 18.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] )
|
||||
SetFunction(env, sgFunction, "supportedLocalesOf", Segmenter::SupportedLocalesOf, FunctionLength::ONE);
|
||||
|
||||
// Segmenter.prototype method
|
||||
// 18.3.2 Intl.Segmenter.prototype [ @@toStringTag ]
|
||||
SetStringTagSymbol(env, sgPrototype, "Intl.Segmenter");
|
||||
env->SetSegmenterFunction(thread_, sgFunction);
|
||||
|
||||
// 18.3.4 Intl.Segmenter.prototype.resolvedOptions ( )
|
||||
SetFunction(env, sgPrototype, "resolvedOptions", Segmenter::ResolvedOptions, FunctionLength::ZERO);
|
||||
|
||||
// 18.3.3 Intl.Segmenter.prototype.segment ( string )
|
||||
SetFunction(env, sgPrototype, "segment", Segmenter::Segment, FunctionLength::ONE);
|
||||
}
|
||||
|
||||
void Builtins::InitializeSegments(const JSHandle<GlobalEnv> &env)
|
||||
{
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread_);
|
||||
// Segments.prototype
|
||||
JSHandle<JSFunction> objFun(env->GetObjectFunction());
|
||||
JSHandle<JSObject> segmentsPrototype = factory_->NewJSObjectByConstructor(objFun);
|
||||
JSHandle<JSTaggedValue> segmentsPrototypeValue(segmentsPrototype);
|
||||
|
||||
// Segments.prototype_or_hclass
|
||||
JSHandle<JSHClass> segmentsFuncInstanceHClass =
|
||||
factory_->NewEcmaHClass(JSSegments::SIZE, JSType::JS_SEGMENTS, segmentsPrototypeValue);
|
||||
|
||||
JSHandle<JSFunction> segmentsFunction(
|
||||
factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR));
|
||||
JSFunction::SetFunctionPrototype(thread_,
|
||||
JSHandle<JSFunction>(segmentsFunction), segmentsFuncInstanceHClass.GetTaggedValue());
|
||||
|
||||
env->SetSegmentsFunction(thread_, segmentsFunction);
|
||||
|
||||
// %SegmentsPrototype%.containing ( index )
|
||||
SetFunction(env, segmentsPrototype, "containing", Segments::Containing, FunctionLength::ONE);
|
||||
SetAndReturnFunctionAtSymbol(env, segmentsPrototype,
|
||||
env->GetIteratorSymbol(), "[Symbol.iterator]", Segments::GetSegmentIterator, FunctionLength::ZERO);
|
||||
}
|
||||
|
||||
void Builtins::InitializeSegmentIterator(const JSHandle<GlobalEnv> &env,
|
||||
const JSHandle<JSHClass> &iteratorFuncClass) const
|
||||
{
|
||||
// SegmentIterator.prototype
|
||||
JSHandle<JSObject> segIterPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass));
|
||||
|
||||
// SegmentIterator.prototype_or_hclass
|
||||
JSHandle<JSHClass> segIterFuncInstanceHClass = factory_->NewEcmaHClass(
|
||||
JSSegmentIterator::SIZE, JSType::JS_SEGMENT_ITERATOR, JSHandle<JSTaggedValue>(segIterPrototype));
|
||||
|
||||
JSHandle<JSFunction> segIterFunction(
|
||||
factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR));
|
||||
JSFunction::SetFunctionPrototype(thread_,
|
||||
JSHandle<JSFunction>(segIterFunction), segIterFuncInstanceHClass.GetTaggedValue());
|
||||
|
||||
SetFunction(env, segIterPrototype, "next", SegmentIterator::Next, FunctionLength::ZERO);
|
||||
SetStringTagSymbol(env, segIterPrototype, "Segmenter String Iterator");
|
||||
|
||||
env->SetSegmentIterator(thread_, segIterFunction);
|
||||
env->SetSegmentIteratorPrototype(thread_, segIterPrototype);
|
||||
}
|
||||
#endif // #ifdef ARK_SUPPORT_INTL
|
||||
|
||||
JSHandle<JSObject> Builtins::InitializeArkTools(const JSHandle<GlobalEnv> &env) const
|
||||
|
@ -159,6 +159,8 @@ private:
|
||||
void InitializePluralRules(const JSHandle<GlobalEnv> &env);
|
||||
void InitializeDisplayNames(const JSHandle<GlobalEnv> &env);
|
||||
void InitializeListFormat(const JSHandle<GlobalEnv> &env);
|
||||
void InitializeSegmenter(const JSHandle<GlobalEnv> &env);
|
||||
void InitializeSegments(const JSHandle<GlobalEnv> &env);
|
||||
|
||||
void LazyInitializeLocale(const JSHandle<GlobalEnv> &env) const;
|
||||
void LazyInitializeDateTimeFormat(const JSHandle<GlobalEnv> &env) const;
|
||||
@ -168,6 +170,8 @@ private:
|
||||
void LazyInitializePluralRules(const JSHandle<GlobalEnv> &env) const;
|
||||
void LazyInitializeDisplayNames(const JSHandle<GlobalEnv> &env) const;
|
||||
void LazyInitializeListFormat(const JSHandle<GlobalEnv> &env) const;
|
||||
void LazyInitializeSegmenter(const JSHandle<GlobalEnv> &env) const;
|
||||
void LazyInitializeSegments(const JSHandle<GlobalEnv> &env) const;
|
||||
|
||||
void GeneralUpdateError(ErrorParameter *error, EcmaEntrypoint constructor, EcmaEntrypoint method,
|
||||
std::string_view name, JSType type) const;
|
||||
@ -205,6 +209,8 @@ private:
|
||||
|
||||
void InitializeStringIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &iteratorFuncClass) const;
|
||||
|
||||
void InitializeSegmentIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &iteratorFuncClass) const;
|
||||
|
||||
void InitializeAsyncFromSyncIterator(const JSHandle<GlobalEnv> &env,
|
||||
const JSHandle<JSHClass> &iteratorFuncClass) const;
|
||||
|
||||
|
@ -45,6 +45,8 @@
|
||||
V(Collator) \
|
||||
V(PluralRules) \
|
||||
V(DisplayNames) \
|
||||
V(Segmenter) \
|
||||
V(Segments) \
|
||||
V(ListFormat) \
|
||||
|
||||
namespace panda::ecmascript::builtins {
|
||||
@ -80,6 +82,8 @@ public:
|
||||
static JSTaggedValue Collator(JSThread *thread, const JSHandle<JSObject> &obj);
|
||||
static JSTaggedValue PluralRules(JSThread *thread, const JSHandle<JSObject> &obj);
|
||||
static JSTaggedValue DisplayNames(JSThread *thread, const JSHandle<JSObject> &obj);
|
||||
static JSTaggedValue Segmenter(JSThread *thread, const JSHandle<JSObject> &obj);
|
||||
static JSTaggedValue Segments(JSThread *thread, const JSHandle<JSObject> &obj);
|
||||
static JSTaggedValue ListFormat(JSThread *thread, const JSHandle<JSObject> &obj);
|
||||
#endif
|
||||
|
||||
|
46
ecmascript/builtins/builtins_segment_iterator.cpp
Normal file
46
ecmascript/builtins/builtins_segment_iterator.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "builtins_segment_iterator.h"
|
||||
|
||||
#include "ecmascript/intl/locale_helper.h"
|
||||
#include "ecmascript/ecma_vm.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/js_intl.h"
|
||||
#include "ecmascript/js_locale.h"
|
||||
#include "ecmascript/js_object.h"
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
#include "ecmascript/object_factory.h"
|
||||
|
||||
namespace panda::ecmascript::builtins {
|
||||
// %SegmentIteratorPrototype%.next ( )
|
||||
JSTaggedValue BuiltinsSegmentIterator::Next(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
JSThread *thread = argv->GetThread();
|
||||
BUILTINS_API_TRACE(thread, SegmentIterator, Next);
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread);
|
||||
|
||||
// 1. Let iterator be the this value.
|
||||
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
|
||||
|
||||
// 2. Perform ? RequireInternalSlot(iterator, [[IteratingSegmenter]]).
|
||||
if (!thisValue->IsJSSegmentIterator()) {
|
||||
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not SegmentIterator object", JSTaggedValue::Exception());
|
||||
}
|
||||
|
||||
JSHandle<JSSegmentIterator> iterator = JSHandle<JSSegmentIterator>::Cast(thisValue);
|
||||
return JSSegmentIterator::Next(thread, iterator);
|
||||
}
|
||||
} // namespace panda::ecmascript::builtins
|
28
ecmascript/builtins/builtins_segment_iterator.h
Normal file
28
ecmascript/builtins/builtins_segment_iterator.h
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ECMASCRIPT_BUILTINS_BUILTINS_SEGMENT_ITERATOR_H
|
||||
#define ECMASCRIPT_BUILTINS_BUILTINS_SEGMENT_ITERATOR_H
|
||||
|
||||
#include "ecmascript/base/builtins_base.h"
|
||||
|
||||
namespace panda::ecmascript::builtins {
|
||||
class BuiltinsSegmentIterator : public base::BuiltinsBase {
|
||||
public:
|
||||
// 18.6.2(1) %SegmentIteratorPrototype%.next ( )
|
||||
static JSTaggedValue Next(EcmaRuntimeCallInfo *argv);
|
||||
};
|
||||
} // namespace panda::ecmascript::builtins
|
||||
#endif // ECMASCRIPT_BUILTINS_BUILTINS_SEGMENT_ITERATOR_H
|
135
ecmascript/builtins/builtins_segmenter.cpp
Normal file
135
ecmascript/builtins/builtins_segmenter.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "builtins_segmenter.h"
|
||||
|
||||
#include "ecmascript/intl/locale_helper.h"
|
||||
#include "ecmascript/ecma_vm.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/js_intl.h"
|
||||
#include "ecmascript/js_locale.h"
|
||||
#include "ecmascript/js_object.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/object_factory.h"
|
||||
|
||||
namespace panda::ecmascript::builtins {
|
||||
// 18.1.1 Intl.Segmenter ( [ locales [ , options ] ] )
|
||||
JSTaggedValue BuiltinsSegmenter::SegmenterConstructor(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
JSThread *thread = argv->GetThread();
|
||||
BUILTINS_API_TRACE(thread, Segmenter, Constructor);
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread);
|
||||
EcmaVM *ecmaVm = thread->GetEcmaVM();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
|
||||
// 1. If NewTarget is undefined, throw a TypeError exception.
|
||||
JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
|
||||
if (newTarget->IsUndefined()) {
|
||||
THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
|
||||
}
|
||||
|
||||
// 2. Let internalSlotsList be « [[InitializedSegmenter]], [[Locale]], [[SegmenterGranularity]] ».
|
||||
// 3. Let segmenter be ? OrdinaryCreateFromConstructor(NewTarget, "%Segmenter.prototype%", internalSlotsList).
|
||||
JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
|
||||
JSHandle<JSSegmenter> segmenter =
|
||||
JSHandle<JSSegmenter>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget));
|
||||
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
||||
|
||||
// 3. Perform ? InitializeSegmenter(segmenter, locales, options).
|
||||
JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
|
||||
JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
|
||||
JSSegmenter::InitializeSegmenter(thread, segmenter, locales, options);
|
||||
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
||||
return segmenter.GetTaggedValue();
|
||||
}
|
||||
|
||||
// 18.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] )
|
||||
JSTaggedValue BuiltinsSegmenter::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
JSThread *thread = argv->GetThread();
|
||||
BUILTINS_API_TRACE(thread, Segmenter, SupportedLocalesOf);
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread);
|
||||
|
||||
// 1. Let availableLocales be %Segmenter%.[[AvailableLocales]].
|
||||
JSHandle<TaggedArray> availableLocales = JSSegmenter::GetAvailableLocales(thread);
|
||||
|
||||
// 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
|
||||
JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
|
||||
JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
|
||||
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
||||
|
||||
// 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
|
||||
JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
|
||||
JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
|
||||
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
||||
return result.GetTaggedValue();
|
||||
}
|
||||
|
||||
// 18.3.3 Intl.Segmenter.prototype.segment ( string )
|
||||
JSTaggedValue BuiltinsSegmenter::Segment(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
JSThread *thread = argv->GetThread();
|
||||
BUILTINS_API_TRACE(thread, Segmenter, Segment);
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread);
|
||||
|
||||
// 1. Let segmenter be the this value.
|
||||
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
|
||||
|
||||
// 2. Perform ? RequireInternalSlot(segmenter, [[InitializedSegmenter]]).
|
||||
if (!thisValue->IsJSSegmenter()) {
|
||||
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not segmenter object", JSTaggedValue::Exception());
|
||||
}
|
||||
|
||||
// 3. Let string be ? ToString(string).
|
||||
JSHandle<JSTaggedValue> stringValue = GetCallArg(argv, 0);
|
||||
JSHandle<EcmaString> string = JSTaggedValue::ToString(thread, stringValue);
|
||||
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
||||
// 4. Return ! CreateSegmentsObject(segmenter, string).
|
||||
JSHandle<JSSegments> segments =
|
||||
JSSegments::CreateSegmentsObject(thread, JSHandle<JSSegmenter>::Cast(thisValue), string);
|
||||
return segments.GetTaggedValue();
|
||||
}
|
||||
|
||||
// 18.3.4 Intl.Segmenter.prototype.resolvedOptions ( )
|
||||
JSTaggedValue BuiltinsSegmenter::ResolvedOptions(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
JSThread *thread = argv->GetThread();
|
||||
BUILTINS_API_TRACE(thread, Segmenter, ResolvedOptions);
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread);
|
||||
|
||||
// 1. Let segmenter be the this value.
|
||||
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
|
||||
|
||||
// 2. Perform ? RequireInternalSlot(segmenter, [[InitializedSegmenter]]).
|
||||
if (!thisValue->IsJSSegmenter()) {
|
||||
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not segmenter object", JSTaggedValue::Exception());
|
||||
}
|
||||
|
||||
// 3. Let options be OrdinaryObjectCreate(%Object.prototype%).
|
||||
auto ecmaVm = thread->GetEcmaVM();
|
||||
JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
JSHandle<JSFunction> ctor(env->GetObjectFunction());
|
||||
JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
|
||||
|
||||
// 4. perform resolvedOptions
|
||||
JSHandle<JSSegmenter> segmenter = JSHandle<JSSegmenter>::Cast(thisValue);
|
||||
JSSegmenter::ResolvedOptions(thread, segmenter, options);
|
||||
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
||||
// 5. Return options.
|
||||
return options.GetTaggedValue();
|
||||
}
|
||||
} // namespace panda::ecmascript::builtins
|
37
ecmascript/builtins/builtins_segmenter.h
Normal file
37
ecmascript/builtins/builtins_segmenter.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ECMASCRIPT_BUILTINS_BUILTINS_SEGMENTER_H
|
||||
#define ECMASCRIPT_BUILTINS_BUILTINS_SEGMENTER_H
|
||||
|
||||
#include "ecmascript/base/builtins_base.h"
|
||||
|
||||
namespace panda::ecmascript::builtins {
|
||||
class BuiltinsSegmenter : public base::BuiltinsBase {
|
||||
public:
|
||||
// 18.1.1 Intl.Segmenter ( [ locales [ , options ] ] )
|
||||
static JSTaggedValue SegmenterConstructor(EcmaRuntimeCallInfo *argv);
|
||||
|
||||
// 18.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] )
|
||||
static JSTaggedValue SupportedLocalesOf(EcmaRuntimeCallInfo *argv);
|
||||
|
||||
// 18.3.3 Intl.Segmenter.prototype.segment ( string )
|
||||
static JSTaggedValue Segment(EcmaRuntimeCallInfo *argv);
|
||||
|
||||
// 18.3.4 Intl.Segmenter.prototype.resolvedOptions ( )
|
||||
static JSTaggedValue ResolvedOptions(EcmaRuntimeCallInfo *argv);
|
||||
};
|
||||
} // namespace panda::ecmascript::builtins
|
||||
#endif // ECMASCRIPT_BUILTINS_BUILTINS_SEGMENTER_H
|
73
ecmascript/builtins/builtins_segments.cpp
Normal file
73
ecmascript/builtins/builtins_segments.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "builtins_segments.h"
|
||||
|
||||
#include "ecmascript/intl/locale_helper.h"
|
||||
#include "ecmascript/ecma_vm.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/js_intl.h"
|
||||
#include "ecmascript/js_locale.h"
|
||||
#include "ecmascript/js_object.h"
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/object_factory.h"
|
||||
|
||||
namespace panda::ecmascript::builtins {
|
||||
// %SegmentsPrototype%.containing ( index )
|
||||
JSTaggedValue BuiltinsSegments::Containing(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
JSThread *thread = argv->GetThread();
|
||||
BUILTINS_API_TRACE(thread, Segments, Containing);
|
||||
[[maybe_unused]] EcmaHandleScope scope(thread);
|
||||
|
||||
// 1. Let segments be the this value.
|
||||
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
|
||||
|
||||
// 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
|
||||
if (!thisValue->IsJSSegments()) {
|
||||
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Segments object", JSTaggedValue::Exception());
|
||||
}
|
||||
|
||||
// 6. Let n be ? ToIntegerOrInfinity(index).
|
||||
JSHandle<JSTaggedValue> indexTag = GetCallArg(argv, 0);
|
||||
JSTaggedNumber indexVal = JSTaggedValue::ToInteger(thread, indexTag);
|
||||
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
||||
JSHandle<JSSegments> segments = JSHandle<JSSegments>::Cast(thisValue);
|
||||
return JSSegments::Containing(thread, segments, indexVal.GetNumber());
|
||||
}
|
||||
|
||||
// %SegmentsPrototype% [ @@iterator ] ( )
|
||||
JSTaggedValue BuiltinsSegments::GetSegmentIterator(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
JSThread *thread = argv->GetThread();
|
||||
BUILTINS_API_TRACE(thread, Segments, GetSegmentIterator);
|
||||
[[maybe_unused]] EcmaHandleScope handleScope(thread);
|
||||
// 1. Let segments be the this value.
|
||||
JSHandle<JSTaggedValue> thisValue = GetThis(argv);
|
||||
|
||||
// 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
|
||||
if (!thisValue->IsJSSegments()) {
|
||||
THROW_TYPE_ERROR_AND_RETURN(thread, "this is not Segments object", JSTaggedValue::Exception());
|
||||
}
|
||||
JSHandle<JSSegments> segments = JSHandle<JSSegments>::Cast(thisValue);
|
||||
// 3. Let segmenter be segments.[[SegmentsSegmenter]].
|
||||
// 4. Let string be segments.[[SegmentsString]].
|
||||
JSHandle<EcmaString> string(thread, segments->GetSegmentsString());
|
||||
// 5. Return ! CreateSegmentIterator(segmenter, string).
|
||||
return JSSegmentIterator::CreateSegmentIterator(thread, segments->GetIcuBreakIterator(), string,
|
||||
segments->GetGranularity()).GetTaggedValue();
|
||||
}
|
||||
} // namespace panda::ecmascript::builtins
|
31
ecmascript/builtins/builtins_segments.h
Normal file
31
ecmascript/builtins/builtins_segments.h
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ECMASCRIPT_BUILTINS_BUILTINS_SEGMENTS_H
|
||||
#define ECMASCRIPT_BUILTINS_BUILTINS_SEGMENTS_H
|
||||
|
||||
#include "ecmascript/base/builtins_base.h"
|
||||
|
||||
namespace panda::ecmascript::builtins {
|
||||
class BuiltinsSegments : public base::BuiltinsBase {
|
||||
public:
|
||||
// 18.5.2(1) %SegmentsPrototype%.containing ( index )
|
||||
static JSTaggedValue Containing(EcmaRuntimeCallInfo *argv);
|
||||
|
||||
// 18.5.2(2) %SegmentsPrototype% [ @@iterator ] ( )
|
||||
static JSTaggedValue GetSegmentIterator(EcmaRuntimeCallInfo *argv);
|
||||
};
|
||||
} // namespace panda::ecmascript::builtins
|
||||
#endif // ECMASCRIPT_BUILTINS_BUILTINS_SEGMENTS_H
|
@ -1959,7 +1959,7 @@ JSTaggedValue BuiltinsString::SubStr(EcmaRuntimeCallInfo *argv)
|
||||
JSTaggedValue BuiltinsString::At(EcmaRuntimeCallInfo *argv)
|
||||
{
|
||||
ASSERT(argv);
|
||||
BUILTINS_API_TRACE(argv->GetThread(), String, Substring);
|
||||
BUILTINS_API_TRACE(argv->GetThread(), String, At);
|
||||
JSThread *thread = argv->GetThread();
|
||||
[[maybe_unused]] EcmaHandleScope handleScope(thread);
|
||||
|
||||
|
@ -391,6 +391,31 @@ host_unittest_action("BuiltinsInternational_015_Test") {
|
||||
deps += hiviewdfx_deps
|
||||
}
|
||||
|
||||
host_unittest_action("BuiltinsInternational_016_Test") {
|
||||
module_out_path = module_output_path
|
||||
|
||||
sources = [
|
||||
# test file
|
||||
"builtins_segmenter_test.cpp",
|
||||
]
|
||||
|
||||
configs = [
|
||||
"../../../:ecma_test_config",
|
||||
"../../../:icu_path_test_config",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"$ark_third_party_root/icu/icu4c:shared_icui18n",
|
||||
"$ark_third_party_root/icu/icu4c:shared_icuuc",
|
||||
"../../../:libark_jsruntime_test",
|
||||
sdk_libc_secshared_dep,
|
||||
]
|
||||
|
||||
# hiviewdfx libraries
|
||||
external_deps = hiviewdfx_ext_deps
|
||||
deps += hiviewdfx_deps
|
||||
}
|
||||
|
||||
host_unittest_action("BuiltinsNatural_001_Test") {
|
||||
module_out_path = module_output_path
|
||||
|
||||
@ -590,6 +615,7 @@ group("unittest") {
|
||||
":BuiltinsInternational_013_Test",
|
||||
":BuiltinsInternational_014_Test",
|
||||
":BuiltinsInternational_015_Test",
|
||||
":BuiltinsInternational_016_Test",
|
||||
":BuiltinsNatural_001_Test",
|
||||
":BuiltinsNatural_002_Test",
|
||||
":BuiltinsNatural_003_Test",
|
||||
@ -620,6 +646,7 @@ group("host_unittest") {
|
||||
":BuiltinsInternational_013_TestAction",
|
||||
":BuiltinsInternational_014_TestAction",
|
||||
":BuiltinsInternational_015_TestAction",
|
||||
":BuiltinsInternational_016_TestAction",
|
||||
":BuiltinsNatural_001_TestAction",
|
||||
":BuiltinsNatural_002_TestAction",
|
||||
":BuiltinsNatural_003_TestAction",
|
||||
@ -645,6 +672,7 @@ group("host_unittest") {
|
||||
":BuiltinsInternational_013_TestAction",
|
||||
":BuiltinsInternational_014_TestAction",
|
||||
":BuiltinsInternational_015_TestAction",
|
||||
":BuiltinsInternational_016_TestAction",
|
||||
":BuiltinsNatural_001_TestAction",
|
||||
":BuiltinsNatural_002_TestAction",
|
||||
":BuiltinsNatural_003_TestAction",
|
||||
|
392
ecmascript/builtins/tests/builtins_segmenter_test.cpp
Normal file
392
ecmascript/builtins/tests/builtins_segmenter_test.cpp
Normal file
@ -0,0 +1,392 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ecmascript/builtins/builtins_segmenter.h"
|
||||
#include "ecmascript/builtins/builtins_segments.h"
|
||||
#include "ecmascript/builtins/builtins_segment_iterator.h"
|
||||
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/tests/test_helper.h"
|
||||
|
||||
using namespace panda::ecmascript;
|
||||
using namespace panda::ecmascript::builtins;
|
||||
namespace panda::test {
|
||||
class BuiltinsSegmenterTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
GTEST_LOG_(INFO) << "SetUpTestCase";
|
||||
}
|
||||
|
||||
static void TearDownTestCase()
|
||||
{
|
||||
GTEST_LOG_(INFO) << "TearDownCase";
|
||||
}
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
JSRuntimeOptions options;
|
||||
#if PANDA_TARGET_LINUX
|
||||
// for consistency requirement, use ohos_icu4j/data/icudt67l.dat as icu-data-path
|
||||
options.SetIcuDataPath(ICU_PATH);
|
||||
#endif
|
||||
options.SetEnableForceGC(true);
|
||||
instance = JSNApi::CreateEcmaVM(options);
|
||||
instance->SetEnableForceGC(true);
|
||||
ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
|
||||
thread = instance->GetJSThread();
|
||||
scope = new EcmaHandleScope(thread);
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
TestHelper::DestroyEcmaVMWithScope(instance, scope);
|
||||
}
|
||||
|
||||
EcmaVM *instance {nullptr};
|
||||
EcmaHandleScope *scope {nullptr};
|
||||
JSThread *thread {nullptr};
|
||||
};
|
||||
|
||||
static JSTaggedValue JSSegmenterCreateWithLocaleTest(JSThread *thread, JSHandle<JSTaggedValue> &locale)
|
||||
{
|
||||
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
||||
JSHandle<JSFunction> newTarget(env->GetSegmenterFunction());
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 6); // 6 means 1 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue result = BuiltinsSegmenter::SegmenterConstructor(ecmaRuntimeCallInfo);
|
||||
EXPECT_TRUE(result.IsJSSegmenter());
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSTaggedValue JSSegmenterCreateWithLocaleAndOptionsTest(JSThread *thread, JSHandle<JSTaggedValue> &locale,
|
||||
JSHandle<JSTaggedValue> &granularity)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
||||
JSHandle<JSFunction> newTarget(env->GetSegmenterFunction());
|
||||
JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
|
||||
|
||||
JSHandle<JSTaggedValue> granularityKey = thread->GlobalConstants()->GetHandledGranularityString();
|
||||
JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
|
||||
JSObject::SetProperty(thread, optionsObj, granularityKey, granularity);
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8); // 8 means 2 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue result = BuiltinsSegmenter::SegmenterConstructor(ecmaRuntimeCallInfo);
|
||||
EXPECT_TRUE(result.IsJSSegmenter());
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
return result;
|
||||
}
|
||||
|
||||
static JSTaggedValue JSSegmentsCreateTest(JSThread *thread, JSHandle<JSTaggedValue> &locale,
|
||||
JSHandle<JSTaggedValue> &granularity, JSHandle<JSTaggedValue> &stringValue)
|
||||
{
|
||||
JSHandle<JSSegmenter> jsSegmenter =
|
||||
JSHandle<JSSegmenter>(thread, JSSegmenterCreateWithLocaleAndOptionsTest(thread, locale, granularity));
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(jsSegmenter.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue segments = BuiltinsSegmenter::Segment(ecmaRuntimeCallInfo);
|
||||
EXPECT_TRUE(segments.IsJSSegments());
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
return segments;
|
||||
}
|
||||
|
||||
// new Intl.Segmenter ( [ locales [ , options ] ] )
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, SegmenterConstructor)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
||||
JSHandle<JSFunction> newTarget(env->GetSegmenterFunction());
|
||||
|
||||
JSHandle<JSTaggedValue> localesString(factory->NewFromASCII("en-US"));
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 6); // 6 means 1 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, localesString.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue result = BuiltinsSegmenter::SegmenterConstructor(ecmaRuntimeCallInfo);
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
|
||||
EXPECT_TRUE(result.IsJSSegmenter());
|
||||
}
|
||||
|
||||
// Intl.Segmenter.prototype.segment ( string )
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, segment)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-cn"));
|
||||
JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
|
||||
JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("这句话是中文"));
|
||||
JSHandle<JSSegmenter> jsSegmenter =
|
||||
JSHandle<JSSegmenter>(thread, JSSegmenterCreateWithLocaleAndOptionsTest(thread, locale, granularity));
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(jsSegmenter.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue result = BuiltinsSegmenter::Segment(ecmaRuntimeCallInfo);
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
EXPECT_TRUE(result.IsJSSegments());
|
||||
}
|
||||
|
||||
// SupportedLocalesOf("lookup")
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, SupportedLocalesOf)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
||||
JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
|
||||
|
||||
JSHandle<JSTaggedValue> localeMatcherKey = thread->GlobalConstants()->GetHandledLocaleMatcherString();
|
||||
JSHandle<JSTaggedValue> localeMatcherValue(factory->NewFromASCII("lookup"));
|
||||
JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
|
||||
JSObject::SetProperty(thread, optionsObj, localeMatcherKey, localeMatcherValue);
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("id-u-co-pinyin-de-ID"));
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); // 8 means 2 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue resultArr = BuiltinsSegmenter::SupportedLocalesOf(ecmaRuntimeCallInfo);
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
|
||||
JSHandle<JSArray> resultHandle(thread, resultArr);
|
||||
JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
|
||||
EXPECT_EQ(elements->GetLength(), 1U);
|
||||
JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
|
||||
EXPECT_STREQ("id-u-co-pinyin-de-id", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
|
||||
}
|
||||
|
||||
// Intl.Segmenter.prototype.resolvedOptions
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, ResolvedOptions)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("de-DE"));
|
||||
JSHandle<JSSegmenter> jsSegmenter =
|
||||
JSHandle<JSSegmenter>(thread, JSSegmenterCreateWithLocaleTest(thread, locale));
|
||||
auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(jsSegmenter.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue result = BuiltinsSegmenter::ResolvedOptions(ecmaRuntimeCallInfo);
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
|
||||
JSHandle<JSTaggedValue> resultObj =
|
||||
JSHandle<JSTaggedValue>(thread, JSTaggedValue(static_cast<JSTaggedType>(result.GetRawData())));
|
||||
// judge whether the properties of the object are the same as those of jsdatetimeformat tag
|
||||
JSHandle<JSTaggedValue> localeKey = globalConst->GetHandledLocaleString();
|
||||
EXPECT_EQ(JSTaggedValue::SameValue(
|
||||
JSObject::GetProperty(thread, resultObj, localeKey).GetValue(), locale), true);
|
||||
JSHandle<JSTaggedValue> granularityKey = globalConst->GetHandledGranularityString();
|
||||
JSHandle<JSTaggedValue> defaultGranularityValue(factory->NewFromASCII("grapheme"));
|
||||
EXPECT_EQ(JSTaggedValue::SameValue(
|
||||
JSObject::GetProperty(thread, resultObj, granularityKey).GetValue(), defaultGranularityValue), true);
|
||||
}
|
||||
|
||||
// %SegmentsPrototype%.containing ( index )
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, SegmentsPrototypeContaining_001)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-cn"));
|
||||
JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("sentence"));
|
||||
JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("这句话是中文。这句还是中文!"));
|
||||
JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(segments.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(3)));
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSHandle<JSTaggedValue> result(thread, BuiltinsSegments::Containing(ecmaRuntimeCallInfo));
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
EXPECT_TRUE(result->IsJSObject());
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
JSHandle<JSTaggedValue> segmentKey = globalConst->GetHandledSegmentString();
|
||||
JSHandle<JSTaggedValue> indexKey = globalConst->GetHandledIndexString();
|
||||
JSHandle<JSTaggedValue> inputKey = globalConst->GetHandledInputString();
|
||||
JSHandle<JSTaggedValue> isWordLikeKey = globalConst->GetHandledIsWordLikeString();
|
||||
JSHandle<JSTaggedValue> segmentValue(JSObject::GetProperty(thread, result, segmentKey).GetValue());
|
||||
JSHandle<JSTaggedValue> indexValue(JSObject::GetProperty(thread, result, indexKey).GetValue());
|
||||
JSHandle<JSTaggedValue> inputValue(JSObject::GetProperty(thread, result, inputKey).GetValue());
|
||||
JSHandle<JSTaggedValue> isWordLikeValue(JSObject::GetProperty(thread, result, isWordLikeKey).GetValue());
|
||||
EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(segmentValue)).ToCString().c_str(),
|
||||
"这句话是中文。");
|
||||
EXPECT_EQ(indexValue->GetRawData(), JSTaggedValue(0).GetRawData());
|
||||
EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(inputValue)).ToCString().c_str(),
|
||||
"这句话是中文。这句还是中文!");
|
||||
EXPECT_TRUE(isWordLikeValue->IsUndefined());
|
||||
}
|
||||
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, SegmentsPrototypeContaining_002)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
|
||||
JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
|
||||
JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure"));
|
||||
JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(segments.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(10)));
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSHandle<JSTaggedValue> result(thread, BuiltinsSegments::Containing(ecmaRuntimeCallInfo));
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
EXPECT_TRUE(result->IsJSObject());
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
JSHandle<JSTaggedValue> segmentKey = globalConst->GetHandledSegmentString();
|
||||
JSHandle<JSTaggedValue> indexKey = globalConst->GetHandledIndexString();
|
||||
JSHandle<JSTaggedValue> inputKey = globalConst->GetHandledInputString();
|
||||
JSHandle<JSTaggedValue> isWordLikeKey = globalConst->GetHandledIsWordLikeString();
|
||||
JSHandle<JSTaggedValue> segmentValue(JSObject::GetProperty(thread, result, segmentKey).GetValue());
|
||||
JSHandle<JSTaggedValue> indexValue(JSObject::GetProperty(thread, result, indexKey).GetValue());
|
||||
JSHandle<JSTaggedValue> inputValue(JSObject::GetProperty(thread, result, inputKey).GetValue());
|
||||
JSHandle<JSTaggedValue> isWordLikeValue(JSObject::GetProperty(thread, result, isWordLikeKey).GetValue());
|
||||
EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(segmentValue)).ToCString().c_str(),
|
||||
"joie");
|
||||
EXPECT_EQ(indexValue->GetRawData(), JSTaggedValue(7).GetRawData());
|
||||
EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(inputValue)).ToCString().c_str(),
|
||||
"Que ma joie demeure");
|
||||
EXPECT_EQ(isWordLikeValue->GetRawData(), JSTaggedValue::True().GetRawData());
|
||||
}
|
||||
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, SegmentsPrototypeContaining_003)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
|
||||
JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
|
||||
JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure"));
|
||||
JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(segments.GetTaggedValue());
|
||||
ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(-10)));
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSHandle<JSTaggedValue> result(thread, BuiltinsSegments::Containing(ecmaRuntimeCallInfo));
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
EXPECT_TRUE(result->IsUndefined());
|
||||
}
|
||||
|
||||
// %SegmentsPrototype% [ @@iterator ] ( )
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, GetSegmentIterator)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
|
||||
JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
|
||||
JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure"));
|
||||
JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); // 4 means 0 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(segments.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSTaggedValue result = BuiltinsSegments::GetSegmentIterator(ecmaRuntimeCallInfo);
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
EXPECT_TRUE(result.IsJSSegmentIterator());
|
||||
}
|
||||
|
||||
// %SegmentIteratorPrototype%.next ( )
|
||||
HWTEST_F_L0(BuiltinsSegmenterTest, SegmentIteratorNext)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
|
||||
JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("sentence"));
|
||||
JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure."));
|
||||
JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
|
||||
|
||||
auto ecmaRuntimeCallInfo =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); // 4 means 0 call args
|
||||
ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo->SetThis(segments.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
|
||||
JSHandle<JSTaggedValue> iterator(thread, BuiltinsSegments::GetSegmentIterator(ecmaRuntimeCallInfo));
|
||||
TestHelper::TearDownFrame(thread, prev);
|
||||
EXPECT_TRUE(iterator->IsJSSegmentIterator());
|
||||
|
||||
auto ecmaRuntimeCallInfo1 =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); // 4 means 0 call args
|
||||
ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo1->SetThis(iterator.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
|
||||
JSHandle<JSTaggedValue> result1(thread, BuiltinsSegmentIterator::Next(ecmaRuntimeCallInfo1));
|
||||
TestHelper::TearDownFrame(thread, prev1);
|
||||
EXPECT_TRUE(result1->IsJSObject());
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
JSHandle<JSTaggedValue> valueKey = globalConst->GetHandledValueString();
|
||||
JSHandle<JSTaggedValue> doneKey = globalConst->GetHandledDoneString();
|
||||
JSHandle<JSTaggedValue> value1(JSObject::GetProperty(thread, result1, valueKey).GetValue());
|
||||
JSHandle<JSTaggedValue> done1(JSObject::GetProperty(thread, result1, doneKey).GetValue());
|
||||
EXPECT_TRUE(value1->IsJSObject());
|
||||
JSHandle<JSTaggedValue> segmentKey = globalConst->GetHandledSegmentString();
|
||||
JSHandle<JSTaggedValue> segmentValue(JSObject::GetProperty(thread, value1, segmentKey).GetValue());
|
||||
EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(segmentValue)).ToCString().c_str(),
|
||||
"Que ma joie demeure.");
|
||||
EXPECT_FALSE(done1->ToBoolean());
|
||||
|
||||
auto ecmaRuntimeCallInfo2 =
|
||||
TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); // 4 means 0 call args
|
||||
ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
|
||||
ecmaRuntimeCallInfo2->SetThis(iterator.GetTaggedValue());
|
||||
|
||||
[[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
|
||||
JSHandle<JSTaggedValue> result2(thread, BuiltinsSegmentIterator::Next(ecmaRuntimeCallInfo2));
|
||||
TestHelper::TearDownFrame(thread, prev2);
|
||||
EXPECT_TRUE(result2->IsJSObject());
|
||||
JSHandle<JSTaggedValue> value2(JSObject::GetProperty(thread, result2, valueKey).GetValue());
|
||||
JSHandle<JSTaggedValue> done2(JSObject::GetProperty(thread, result2, doneKey).GetValue());
|
||||
EXPECT_TRUE(value2->IsUndefined());
|
||||
EXPECT_TRUE(done2->ToBoolean());
|
||||
}
|
||||
}
|
@ -450,6 +450,12 @@ CString *HeapSnapshot::GenerateNodeName(TaggedObject *entry)
|
||||
return GetString("JSPluralRules");
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
return GetString("JSDisplayNames");
|
||||
case JSType::JS_SEGMENTER:
|
||||
return GetString("JSSegmenter");
|
||||
case JSType::JS_SEGMENTS:
|
||||
return GetString("JSSegments");
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
return GetString("JSSegmentIterator");
|
||||
case JSType::JS_LIST_FORMAT:
|
||||
return GetString("JSListFormat");
|
||||
case JSType::JS_GENERATOR_OBJECT:
|
||||
|
@ -125,6 +125,9 @@
|
||||
#include "ecmascript/js_intl.h"
|
||||
#include "ecmascript/js_locale.h"
|
||||
#include "ecmascript/js_relative_time_format.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
#endif
|
||||
|
||||
namespace panda::ecmascript {
|
||||
@ -355,6 +358,12 @@ CString JSHClass::DumpJSType(JSType type)
|
||||
return "JSPluralRules";
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
return "JSDisplayNames";
|
||||
case JSType::JS_SEGMENTER:
|
||||
return "JSSegmenter";
|
||||
case JSType::JS_SEGMENTS:
|
||||
return "JSSegments";
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
return "JSSegmentIterator";
|
||||
case JSType::JS_LIST_FORMAT:
|
||||
return "JSListFormat";
|
||||
case JSType::JS_GENERATOR_OBJECT:
|
||||
@ -1012,6 +1021,15 @@ static void DumpObject(TaggedObject *obj, std::ostream &os)
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
JSDisplayNames::Cast(obj)->Dump(os);
|
||||
break;
|
||||
case JSType::JS_SEGMENTER:
|
||||
JSSegmenter::Cast(obj)->Dump(os);
|
||||
break;
|
||||
case JSType::JS_SEGMENTS:
|
||||
JSSegments::Cast(obj)->Dump(os);
|
||||
break;
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
JSSegmentIterator::Cast(obj)->Dump(os);
|
||||
break;
|
||||
case JSType::JS_LIST_FORMAT:
|
||||
JSListFormat::Cast(obj)->Dump(os);
|
||||
break;
|
||||
@ -1024,6 +1042,9 @@ static void DumpObject(TaggedObject *obj, std::ostream &os)
|
||||
case JSType::JS_COLLATOR:
|
||||
case JSType::JS_PLURAL_RULES:
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
case JSType::JS_SEGMENTER:
|
||||
case JSType::JS_SEGMENTS:
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
case JSType::JS_LIST_FORMAT:
|
||||
break;
|
||||
#endif
|
||||
@ -3205,6 +3226,51 @@ void JSDisplayNames::Dump(std::ostream &os) const
|
||||
JSObject::Dump(os);
|
||||
}
|
||||
|
||||
void JSSegmenter::Dump(std::ostream &os) const
|
||||
{
|
||||
os << " - Locale: ";
|
||||
GetLocale().Dump(os);
|
||||
os << "\n";
|
||||
os << " - Granularity: "<< static_cast<int>(GetGranularity());
|
||||
os << "\n";
|
||||
os << " - IcuField: ";
|
||||
GetIcuField().Dump(os);
|
||||
os << "\n";
|
||||
JSObject::Dump(os);
|
||||
}
|
||||
|
||||
void JSSegments::Dump(std::ostream &os) const
|
||||
{
|
||||
os << " - SegmentsString: ";
|
||||
GetSegmentsString().Dump(os);
|
||||
os << "\n";
|
||||
os << " - UnicodeString: ";
|
||||
GetUnicodeString().Dump(os);
|
||||
os << "\n";
|
||||
os << " - Granularity: "<< static_cast<int>(GetGranularity());
|
||||
os << "\n";
|
||||
os << " - IcuField: ";
|
||||
GetIcuField().Dump(os);
|
||||
os << "\n";
|
||||
JSObject::Dump(os);
|
||||
}
|
||||
|
||||
void JSSegmentIterator::Dump(std::ostream &os) const
|
||||
{
|
||||
os << " - IteratedString: ";
|
||||
GetIteratedString().Dump(os);
|
||||
os << "\n";
|
||||
os << " - UnicodeString: ";
|
||||
GetUnicodeString().Dump(os);
|
||||
os << "\n";
|
||||
os << " - Granularity: "<< static_cast<int>(GetGranularity());
|
||||
os << "\n";
|
||||
os << " - IcuField: ";
|
||||
GetIcuField().Dump(os);
|
||||
os << "\n";
|
||||
JSObject::Dump(os);
|
||||
}
|
||||
|
||||
void JSListFormat::Dump(std::ostream &os) const
|
||||
{
|
||||
os << " - Locale: ";
|
||||
@ -4185,6 +4251,15 @@ static void DumpObject(TaggedObject *obj, std::vector<Reference> &vec, bool isVm
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
JSDisplayNames::Cast(obj)->DumpForSnapshot(vec);
|
||||
return;
|
||||
case JSType::JS_SEGMENTER:
|
||||
JSSegmenter::Cast(obj)->DumpForSnapshot(vec);
|
||||
return;
|
||||
case JSType::JS_SEGMENTS:
|
||||
JSSegments::Cast(obj)->DumpForSnapshot(vec);
|
||||
return;
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
JSSegmentIterator::Cast(obj)->DumpForSnapshot(vec);
|
||||
return;
|
||||
case JSType::JS_LIST_FORMAT:
|
||||
JSListFormat::Cast(obj)->DumpForSnapshot(vec);
|
||||
return;
|
||||
@ -4197,6 +4272,9 @@ static void DumpObject(TaggedObject *obj, std::vector<Reference> &vec, bool isVm
|
||||
case JSType::JS_COLLATOR:
|
||||
case JSType::JS_PLURAL_RULES:
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
case JSType::JS_SEGMENTER:
|
||||
case JSType::JS_SEGMENTS:
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
case JSType::JS_LIST_FORMAT:
|
||||
return;
|
||||
#endif
|
||||
@ -5555,6 +5633,32 @@ void JSDisplayNames::DumpForSnapshot(std::vector<Reference> &vec) const
|
||||
JSObject::DumpForSnapshot(vec);
|
||||
}
|
||||
|
||||
void JSSegmenter::DumpForSnapshot(std::vector<Reference> &vec) const
|
||||
{
|
||||
vec.emplace_back(CString("Locale"), GetLocale());
|
||||
vec.emplace_back(CString("Granularity"), JSTaggedValue(static_cast<int>(GetGranularity())));
|
||||
vec.emplace_back(CString("IcuField"), GetIcuField());
|
||||
JSObject::DumpForSnapshot(vec);
|
||||
}
|
||||
|
||||
void JSSegments::DumpForSnapshot(std::vector<Reference> &vec) const
|
||||
{
|
||||
vec.emplace_back(CString("SegmentsString"), GetSegmentsString());
|
||||
vec.emplace_back(CString("UnicodeString"), GetUnicodeString());
|
||||
vec.emplace_back(CString("Granularity"), JSTaggedValue(static_cast<int>(GetGranularity())));
|
||||
vec.emplace_back(CString("IcuField"), GetIcuField());
|
||||
JSObject::DumpForSnapshot(vec);
|
||||
}
|
||||
|
||||
void JSSegmentIterator::DumpForSnapshot(std::vector<Reference> &vec) const
|
||||
{
|
||||
vec.emplace_back(CString("IteratedString"), GetIteratedString());
|
||||
vec.emplace_back(CString("UnicodeString"), GetUnicodeString());
|
||||
vec.emplace_back(CString("Granularity"), JSTaggedValue(static_cast<int>(GetGranularity())));
|
||||
vec.emplace_back(CString("IcuField"), GetIcuField());
|
||||
JSObject::DumpForSnapshot(vec);
|
||||
}
|
||||
|
||||
void JSListFormat::DumpForSnapshot(std::vector<Reference> &vec) const
|
||||
{
|
||||
vec.emplace_back(CString("Locale"), GetLocale());
|
||||
|
@ -278,6 +278,12 @@ class ObjectFactory;
|
||||
V(NumericString, NUMERIC_STRING_CLASS_INDEX, "numeric") \
|
||||
V(NumberingSystemString, NUMBERING_SYSTEM_STRING_CLASS_INDEX, "numberingSystem") \
|
||||
V(TypeString, TYPE_STRING_INDEX, "type") \
|
||||
V(GranularityString, GRANULARITY_STRING_INDEX, "granularity") \
|
||||
V(GraphemeString, GRAPHEME_STRING_INDEX, "grapheme") \
|
||||
V(WordString, WORD_STRING_INDEX, "word") \
|
||||
V(SentenceString, SENTENCE_STRING_INDEX, "sentence") \
|
||||
V(SegmentString, SEGMENT_STRING_INDEX, "segment") \
|
||||
V(IsWordLikeString, ISWORDLIKE_STRING_INDEX, "isWordLike") \
|
||||
V(LocaleMatcherString, LOCALE_MATCHER_STRING_INDEX, "localeMatcher") \
|
||||
V(FormatMatcherString, FORMAT_MATCHER_STRING_INDEX, "formatMatcher") \
|
||||
V(Hour12String, HOUR12_STRING_INDEX, "hour12") \
|
||||
|
@ -115,6 +115,10 @@
|
||||
V(JSTaggedValue, CollatorFunction, COLLATOR_FUNCTION_INDEX) \
|
||||
V(JSTaggedValue, PluralRulesFunction, PLURAL_RULES_FUNCTION_INDEX) \
|
||||
V(JSTaggedValue, DisplayNamesFunction, DISPLAY_NAMES_FUNCTION_INDEX) \
|
||||
V(JSTaggedValue, SegmenterFunction, SEGMENTER_FUNCTION_INDEX) \
|
||||
V(JSTaggedValue, SegmentsFunction, SEGMENTS_FUNCTION_INDEX) \
|
||||
V(JSTaggedValue, SegmentIterator, SEGMENT_ITERATOR_INDEX) \
|
||||
V(JSTaggedValue, SegmentIteratorPrototype, SEGMENT_ITERATOR_PROTOTYPE_INDEX) \
|
||||
V(JSTaggedValue, ListFormatFunction, LIST_FORMAT_FUNCTION_INDEX) \
|
||||
V(JSTaggedValue, RegExpFunction, REGEXP_FUNCTION_INDEX) \
|
||||
V(JSTaggedValue, RegExpExecFunction, REGEXP_EXEC_FUNCTION_INDEX) \
|
||||
|
@ -140,6 +140,7 @@ struct Reference;
|
||||
JS_API_LINKED_LIST_ITERATOR, /* ///////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_API_LIST_ITERATOR, /* ///////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_ARRAY_ITERATOR, /* ///////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_SEGMENT_ITERATOR, /* /////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_STRING_ITERATOR, /* ///////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_INTL, /* ///////////////////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_LOCALE, /* /////////////////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
@ -150,6 +151,8 @@ struct Reference;
|
||||
JS_PLURAL_RULES, /* ///////////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_DISPLAYNAMES, /* ///////////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_LIST_FORMAT, /* ///////////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_SEGMENTER, /* //////////////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_SEGMENTS, /* /////////////////////////////////////////////////////////////////////////// ///////-PADDING */ \
|
||||
\
|
||||
JS_ARRAY_BUFFER, /* ///////////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
JS_SHARED_ARRAY_BUFFER, /* ////////////////////////////////////////////////////////////////////////-PADDING */ \
|
||||
@ -979,6 +982,21 @@ public:
|
||||
return GetObjectType() == JSType::JS_DISPLAYNAMES;
|
||||
}
|
||||
|
||||
inline bool IsJSSegmenter() const
|
||||
{
|
||||
return GetObjectType() == JSType::JS_SEGMENTER;
|
||||
}
|
||||
|
||||
inline bool IsJSSegments() const
|
||||
{
|
||||
return GetObjectType() == JSType::JS_SEGMENTS;
|
||||
}
|
||||
|
||||
inline bool IsJSSegmentIterator() const
|
||||
{
|
||||
return GetObjectType() == JSType::JS_SEGMENT_ITERATOR;
|
||||
}
|
||||
|
||||
inline bool IsJSListFormat() const
|
||||
{
|
||||
return GetObjectType() == JSType::JS_LIST_FORMAT;
|
||||
|
124
ecmascript/js_segment_iterator.cpp
Normal file
124
ecmascript/js_segment_iterator.cpp
Normal file
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "ecmascript/base/builtins_base.h"
|
||||
#include "ecmascript/intl/locale_helper.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/global_env_constants.h"
|
||||
#include "ecmascript/js_iterator.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/object_factory-inl.h"
|
||||
|
||||
#include "unicode/errorcode.h"
|
||||
#include "unicode/locdspnm.h"
|
||||
#include "unicode/locid.h"
|
||||
#include "unicode/udisplaycontext.h"
|
||||
#include "unicode/uloc.h"
|
||||
#include "unicode/unistr.h"
|
||||
#include "unicode/uscript.h"
|
||||
#include "unicode/ustring.h"
|
||||
|
||||
namespace panda::ecmascript {
|
||||
|
||||
void JSSegmentIterator::SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator,
|
||||
icu::BreakIterator* icuBreakIterator, const DeleteEntryPoint &callback)
|
||||
{
|
||||
EcmaVM *ecmaVm = thread->GetEcmaVM();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
|
||||
ASSERT(icuBreakIterator != nullptr);
|
||||
JSTaggedValue data = iterator->GetIcuField();
|
||||
if (data.IsJSNativePointer()) {
|
||||
JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
|
||||
native->ResetExternalPointer(icuBreakIterator);
|
||||
return;
|
||||
}
|
||||
JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(icuBreakIterator, callback);
|
||||
iterator->SetIcuField(thread, pointer.GetTaggedValue());
|
||||
}
|
||||
|
||||
void JSSegmentIterator::SetUString(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator,
|
||||
icu::UnicodeString* icuUnicodeString, const DeleteEntryPoint &callback)
|
||||
{
|
||||
EcmaVM *ecmaVm = thread->GetEcmaVM();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
|
||||
ASSERT(icuUnicodeString != nullptr);
|
||||
JSTaggedValue data = iterator->GetUnicodeString();
|
||||
if (data.IsJSNativePointer()) {
|
||||
JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
|
||||
native->ResetExternalPointer(icuUnicodeString);
|
||||
return;
|
||||
}
|
||||
JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(icuUnicodeString, callback);
|
||||
iterator->SetUnicodeString(thread, pointer.GetTaggedValue());
|
||||
}
|
||||
|
||||
JSHandle<JSSegmentIterator> JSSegmentIterator::CreateSegmentIterator(JSThread *thread,
|
||||
icu::BreakIterator* icuBreakIterator, const JSHandle<EcmaString> &string, GranularityOption granularity)
|
||||
{
|
||||
// 1. Let internalSlotsList be « [[IteratingSegmenter]], [[IteratedString]],
|
||||
// [[IteratedStringNextSegmentCodeUnitIndex]] ».
|
||||
// 2. Let iterator be OrdinaryObjectCreate(%SegmentIteratorPrototype%, internalSlotsList).
|
||||
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
||||
JSHandle<JSFunction> segIterCtor(env->GetSegmentIterator());
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
JSHandle<JSSegmentIterator> iterator(factory->NewJSObjectByConstructor(segIterCtor));
|
||||
icuBreakIterator = icuBreakIterator->clone();
|
||||
// 5. Set iterator.[[IteratedStringNextSegmentCodeUnitIndex]] to 0.
|
||||
icuBreakIterator->first();
|
||||
icu::UnicodeString* uString = new icu::UnicodeString();
|
||||
icuBreakIterator->getText().getText(*uString);
|
||||
// 3. Set iterator.[[IteratingSegmenter]] to segmenter.
|
||||
SetIcuBreakIterator(thread, iterator, icuBreakIterator, JSSegmentIterator::FreeIcuBreakIterator);
|
||||
iterator->SetGranularity(granularity);
|
||||
// 4. Set iterator.[[IteratedString]] to string.
|
||||
iterator->SetIteratedString(thread, string);
|
||||
SetUString(thread, iterator, uString, JSSegmentIterator::FreeUString);
|
||||
// 6. Return iterator.
|
||||
return iterator;
|
||||
}
|
||||
|
||||
JSTaggedValue JSSegmentIterator::Next(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator)
|
||||
{
|
||||
icu::BreakIterator* icuBreakIterator = iterator->GetIcuBreakIterator();
|
||||
// 5. Let startIndex be iterator.[[IteratedStringNextSegmentCodeUnitIndex]].
|
||||
int32_t startIndex = icuBreakIterator->current();
|
||||
// 6. Let endIndex be ! FindBoundary(segmenter, string, startIndex, after).
|
||||
int32_t endIndex = icuBreakIterator->next();
|
||||
// 7. If endIndex is not finite, then
|
||||
if (endIndex == icu::BreakIterator::DONE) {
|
||||
// a. Return CreateIterResultObject(undefined, true).
|
||||
JSHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
|
||||
return JSIterator::CreateIterResultObject(thread, result, true).GetTaggedValue();
|
||||
}
|
||||
|
||||
// 8. Set iterator.[[IteratedStringNextSegmentCodeUnitIndex]] to endIndex.
|
||||
// 9. Let segmentData be ! CreateSegmentDataObject(segmenter, string, startIndex, endIndex).
|
||||
icu::UnicodeString unicodeString;
|
||||
icuBreakIterator->getText().getText(unicodeString);
|
||||
JSHandle<JSObject> segmentData = JSSegments::CreateSegmentDataObject(thread, iterator->GetGranularity(),
|
||||
icuBreakIterator, JSHandle<EcmaString>(thread, iterator->GetIteratedString()),
|
||||
unicodeString, startIndex, endIndex);
|
||||
|
||||
// 10. Return CreateIterResultObject(segmentData, false).
|
||||
return JSIterator::CreateIterResultObject(thread, JSHandle<JSTaggedValue>::Cast(segmentData), false)
|
||||
.GetTaggedValue();
|
||||
}
|
||||
} // namespace panda::ecmascript
|
100
ecmascript/js_segment_iterator.h
Normal file
100
ecmascript/js_segment_iterator.h
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ECMASCRIPT_JS_SEGMENT_ITERATOR_H
|
||||
#define ECMASCRIPT_JS_SEGMENT_ITERATOR_H
|
||||
|
||||
#include "ecmascript/ecma_string.h"
|
||||
#include "ecmascript/ecma_vm.h"
|
||||
#include "ecmascript/ecma_macros.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/js_hclass.h"
|
||||
#include "ecmascript/js_intl.h"
|
||||
#include "ecmascript/js_locale.h"
|
||||
#include "ecmascript/js_object.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_tagged_value.h"
|
||||
#include "ecmascript/object_factory.h"
|
||||
|
||||
#include "unicode/locdspnm.h"
|
||||
|
||||
namespace panda::ecmascript {
|
||||
class JSSegmentIterator : public JSObject {
|
||||
public:
|
||||
CAST_CHECK(JSSegmentIterator, IsJSSegmentIterator);
|
||||
|
||||
static constexpr size_t ICU_FIELD_OFFSET = JSObject::SIZE;
|
||||
ACCESSORS(IcuField, ICU_FIELD_OFFSET, SEGMENTS_STRING_OFFSET)
|
||||
ACCESSORS(IteratedString, SEGMENTS_STRING_OFFSET, UNICODE_STRING_OFFSET)
|
||||
ACCESSORS(UnicodeString, UNICODE_STRING_OFFSET, BIT_FIELD_OFFSET)
|
||||
ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
|
||||
DEFINE_ALIGN_SIZE(LAST_OFFSET);
|
||||
|
||||
// define BitField
|
||||
static constexpr size_t GRANULARITY_BITS = 3;
|
||||
FIRST_BIT_FIELD(BitField, Granularity, GranularityOption, GRANULARITY_BITS)
|
||||
|
||||
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
|
||||
DECL_DUMP()
|
||||
|
||||
// 18.6.1 CreateSegmentIterator ( segmenter, string )
|
||||
static JSHandle<JSSegmentIterator> CreateSegmentIterator(JSThread *thread, icu::BreakIterator* icuBreakIterator,
|
||||
const JSHandle<EcmaString> &string,
|
||||
GranularityOption granularity);
|
||||
|
||||
// 18.6.2(1) SegmentIteratorPrototype.next ( )
|
||||
static JSTaggedValue Next(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator);
|
||||
|
||||
// Get icu break iterator from icu field
|
||||
icu::BreakIterator *GetIcuBreakIterator() const
|
||||
{
|
||||
ASSERT(GetIcuField().IsJSNativePointer());
|
||||
auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
|
||||
return reinterpret_cast<icu::BreakIterator *>(result);
|
||||
}
|
||||
|
||||
static void FreeIcuBreakIterator(void *pointer, [[maybe_unused]] void* hint)
|
||||
{
|
||||
if (pointer == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto icuBreakIterator = reinterpret_cast<icu::BreakIterator *>(pointer);
|
||||
delete icuBreakIterator;
|
||||
}
|
||||
|
||||
static void SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator,
|
||||
icu::BreakIterator* icuBreakIterator, const DeleteEntryPoint &callback);
|
||||
|
||||
static void FreeUString(void *pointer, [[maybe_unused]] void* hint)
|
||||
{
|
||||
if (pointer == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto unicodeString = reinterpret_cast<icu::UnicodeString *>(pointer);
|
||||
delete unicodeString;
|
||||
}
|
||||
|
||||
static void SetUString(JSThread *thread, const JSHandle<JSSegmentIterator> &iterator,
|
||||
icu::UnicodeString* icuUnicodeString, const DeleteEntryPoint &callback);
|
||||
|
||||
icu::UnicodeString *GetUString() const
|
||||
{
|
||||
ASSERT(GetUnicodeString().IsJSNativePointer());
|
||||
auto result = JSNativePointer::Cast(GetUnicodeString().GetTaggedObject())->GetExternalPointer();
|
||||
return reinterpret_cast<icu::UnicodeString *>(result);
|
||||
}
|
||||
};
|
||||
} // namespace panda::ecmascript
|
||||
#endif // ECMASCRIPT_JS_SEGMENT_ITERATOR_H
|
195
ecmascript/js_segmenter.cpp
Normal file
195
ecmascript/js_segmenter.cpp
Normal file
@ -0,0 +1,195 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "ecmascript/intl/locale_helper.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/global_env_constants.h"
|
||||
#include "ecmascript/object_factory-inl.h"
|
||||
|
||||
#include "unicode/errorcode.h"
|
||||
#include "unicode/locdspnm.h"
|
||||
#include "unicode/locid.h"
|
||||
#include "unicode/udisplaycontext.h"
|
||||
#include "unicode/uloc.h"
|
||||
#include "unicode/unistr.h"
|
||||
#include "unicode/uscript.h"
|
||||
#include "unicode/ustring.h"
|
||||
|
||||
namespace panda::ecmascript {
|
||||
|
||||
void JSSegmenter::SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
|
||||
icu::BreakIterator* icuBreakIterator, const DeleteEntryPoint &callback)
|
||||
{
|
||||
EcmaVM *ecmaVm = thread->GetEcmaVM();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
|
||||
ASSERT(icuBreakIterator != nullptr);
|
||||
JSTaggedValue data = segmenter->GetIcuField();
|
||||
if (data.IsJSNativePointer()) {
|
||||
JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
|
||||
native->ResetExternalPointer(icuBreakIterator);
|
||||
return;
|
||||
}
|
||||
JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(icuBreakIterator, callback);
|
||||
segmenter->SetIcuField(thread, pointer.GetTaggedValue());
|
||||
}
|
||||
|
||||
JSHandle<TaggedArray> JSSegmenter::GetAvailableLocales(JSThread *thread)
|
||||
{
|
||||
std::vector<std::string> availableStringLocales = intl::LocaleHelper::GetAvailableLocales(thread, nullptr, nullptr);
|
||||
JSHandle<TaggedArray> availableLocales = JSLocale::ConstructLocaleList(thread, availableStringLocales);
|
||||
return availableLocales;
|
||||
}
|
||||
|
||||
void JSSegmenter::InitializeIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
|
||||
const icu::Locale &icuLocale, GranularityOption granularity)
|
||||
{
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
std::unique_ptr<icu::BreakIterator> icuBreakIterator;
|
||||
|
||||
switch (granularity) {
|
||||
case GranularityOption::GRAPHEME:
|
||||
icuBreakIterator.reset(icu::BreakIterator::createCharacterInstance(icuLocale, status));
|
||||
break;
|
||||
case GranularityOption::WORD:
|
||||
icuBreakIterator.reset(icu::BreakIterator::createWordInstance(icuLocale, status));
|
||||
break;
|
||||
case GranularityOption::SENTENCE:
|
||||
icuBreakIterator.reset(icu::BreakIterator::createSentenceInstance(icuLocale, status));
|
||||
break;
|
||||
default:
|
||||
LOG_ECMA(FATAL) << "this branch is unreachable";
|
||||
UNREACHABLE();
|
||||
}
|
||||
if (U_FAILURE(status) || icuBreakIterator == nullptr) {
|
||||
if (status == UErrorCode::U_MISSING_RESOURCE_ERROR) {
|
||||
THROW_ERROR(thread, ErrorType::REFERENCE_ERROR, "can not find icu data resources");
|
||||
}
|
||||
THROW_ERROR(thread, ErrorType::RANGE_ERROR, "create icu::BreakIterator failed");
|
||||
}
|
||||
|
||||
SetIcuBreakIterator(thread, segmenter, icuBreakIterator.release(), JSSegmenter::FreeIcuBreakIterator);
|
||||
}
|
||||
|
||||
JSHandle<JSSegmenter> JSSegmenter::InitializeSegmenter(JSThread *thread,
|
||||
const JSHandle<JSSegmenter> &segmenter,
|
||||
const JSHandle<JSTaggedValue> &locales,
|
||||
const JSHandle<JSTaggedValue> &options)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
// 4. Let requestedLocales be ? CanonicalizeLocaleList(locales).
|
||||
JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
|
||||
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSSegmenter, thread);
|
||||
|
||||
// 5. Let options be ? GetOptionsObject(options).
|
||||
JSHandle<JSObject> optionsObject;
|
||||
if (options->IsUndefined()) {
|
||||
optionsObject = factory->CreateNullJSObject();
|
||||
} else {
|
||||
optionsObject = JSTaggedValue::ToObject(thread, options);
|
||||
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSSegmenter, thread);
|
||||
}
|
||||
|
||||
// 6. Let opt be a new Record.
|
||||
// 7. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
|
||||
JSHandle<JSTaggedValue> property = globalConst->GetHandledLocaleMatcherString();
|
||||
auto matcher = JSLocale::GetOptionOfString<LocaleMatcherOption>(
|
||||
thread, optionsObject, property, {LocaleMatcherOption::LOOKUP, LocaleMatcherOption::BEST_FIT},
|
||||
{"lookup", "best fit"}, LocaleMatcherOption::BEST_FIT);
|
||||
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSSegmenter, thread);
|
||||
|
||||
// 9. Let localeData be %Segmenter%.[[LocaleData]].
|
||||
// 10. Let r be ResolveLocale(%Segmenter%.[[AvailableLocales]], requestedLocales, opt,
|
||||
// %Segmenter%.[[RelevantExtensionKeys]], localeData).
|
||||
JSHandle<TaggedArray> availableLocales;
|
||||
if (requestedLocales->GetLength() == 0) {
|
||||
availableLocales = factory->EmptyArray();
|
||||
} else {
|
||||
availableLocales = JSSegmenter::GetAvailableLocales(thread);
|
||||
}
|
||||
std::set<std::string> relevantExtensionKeys {""};
|
||||
ResolvedLocale r =
|
||||
JSLocale::ResolveLocale(thread, availableLocales, requestedLocales, matcher, relevantExtensionKeys);
|
||||
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSSegmenter, thread);
|
||||
|
||||
// 11. Set segmenter.[[Locale]] to r.[[locale]].
|
||||
icu::Locale icuLocale = r.localeData;
|
||||
JSHandle<EcmaString> localeStr = intl::LocaleHelper::ToLanguageTag(thread, icuLocale);
|
||||
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSSegmenter, thread);
|
||||
segmenter->SetLocale(thread, localeStr.GetTaggedValue());
|
||||
|
||||
// 12. Let granularity be ? GetOption(options, "granularity", string, « "grapheme", "word", "sentence" »,
|
||||
// "grapheme").
|
||||
property = globalConst->GetHandledGranularityString();
|
||||
auto granularity = JSLocale::GetOptionOfString<GranularityOption>(thread, optionsObject, property,
|
||||
{GranularityOption::GRAPHEME,
|
||||
GranularityOption::WORD,
|
||||
GranularityOption::SENTENCE},
|
||||
{"grapheme", "word", "sentence"},
|
||||
GranularityOption::GRAPHEME);
|
||||
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSSegmenter, thread);
|
||||
|
||||
// 13. Set segmenter.[[SegmenterGranularity]] to granularity.
|
||||
segmenter->SetGranularity(granularity);
|
||||
InitializeIcuBreakIterator(thread, segmenter, icuLocale, granularity);
|
||||
// 14. Return segmenter.
|
||||
return segmenter;
|
||||
}
|
||||
|
||||
JSHandle<JSTaggedValue> JSSegmenter::GranularityOptionToEcmaString(JSThread *thread, GranularityOption granularity)
|
||||
{
|
||||
JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
switch (granularity) {
|
||||
case GranularityOption::GRAPHEME:
|
||||
result.Update(globalConst->GetHandledGraphemeString().GetTaggedValue());
|
||||
break;
|
||||
case GranularityOption::WORD:
|
||||
result.Update(globalConst->GetHandledWordString().GetTaggedValue());
|
||||
break;
|
||||
case GranularityOption::SENTENCE:
|
||||
result.Update(globalConst->GetHandledSentenceString().GetTaggedValue());
|
||||
break;
|
||||
default:
|
||||
LOG_ECMA(FATAL) << "this branch is unreachable";
|
||||
UNREACHABLE();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void JSSegmenter::ResolvedOptions(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
|
||||
const JSHandle<JSObject> &options)
|
||||
{
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
|
||||
// [[Locale]]
|
||||
JSHandle<JSTaggedValue> propertyKey = globalConst->GetHandledLocaleString();
|
||||
JSHandle<JSTaggedValue> locale(thread, segmenter->GetLocale());
|
||||
JSObject::CreateDataPropertyOrThrow(thread, options, propertyKey, locale);
|
||||
RETURN_IF_ABRUPT_COMPLETION(thread);
|
||||
|
||||
// [[SegmenterGranularity]]
|
||||
GranularityOption granularity = segmenter->GetGranularity();
|
||||
propertyKey = globalConst->GetHandledGranularityString();
|
||||
JSHandle<JSTaggedValue> granularityString = GranularityOptionToEcmaString(thread, granularity);
|
||||
JSObject::CreateDataPropertyOrThrow(thread, options, propertyKey, granularityString);
|
||||
RETURN_IF_ABRUPT_COMPLETION(thread);
|
||||
}
|
||||
} // namespace panda::ecmascript
|
93
ecmascript/js_segmenter.h
Normal file
93
ecmascript/js_segmenter.h
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ECMASCRIPT_JS_SEGMENTER_H
|
||||
#define ECMASCRIPT_JS_SEGMENTER_H
|
||||
|
||||
#include "ecmascript/ecma_string.h"
|
||||
#include "ecmascript/ecma_vm.h"
|
||||
#include "ecmascript/ecma_macros.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/js_hclass.h"
|
||||
#include "ecmascript/js_intl.h"
|
||||
#include "ecmascript/js_locale.h"
|
||||
#include "ecmascript/js_object.h"
|
||||
#include "ecmascript/js_tagged_value.h"
|
||||
#include "ecmascript/object_factory.h"
|
||||
|
||||
#include "unicode/locdspnm.h"
|
||||
|
||||
namespace panda::ecmascript {
|
||||
enum class GranularityOption : uint8_t {
|
||||
GRAPHEME = 0x01,
|
||||
WORD,
|
||||
SENTENCE,
|
||||
EXCEPTION
|
||||
};
|
||||
|
||||
class JSSegmenter : public JSObject {
|
||||
public:
|
||||
CAST_CHECK(JSSegmenter, IsJSSegmenter);
|
||||
|
||||
static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
|
||||
ACCESSORS(Locale, LOCALE_OFFSET, ICU_FIELD_OFFSET)
|
||||
ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
|
||||
ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
|
||||
DEFINE_ALIGN_SIZE(LAST_OFFSET);
|
||||
|
||||
// define BitField
|
||||
static constexpr size_t GRANULARITY_BITS = 3;
|
||||
FIRST_BIT_FIELD(BitField, Granularity, GranularityOption, GRANULARITY_BITS)
|
||||
|
||||
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
|
||||
DECL_DUMP()
|
||||
|
||||
static JSHandle<JSSegmenter> InitializeSegmenter(JSThread *thread,
|
||||
const JSHandle<JSSegmenter> &segmenter,
|
||||
const JSHandle<JSTaggedValue> &locales,
|
||||
const JSHandle<JSTaggedValue> &options);
|
||||
// Get icu break iterator from icu field
|
||||
icu::BreakIterator *GetIcuBreakIterator() const
|
||||
{
|
||||
ASSERT(GetIcuField().IsJSNativePointer());
|
||||
auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
|
||||
return reinterpret_cast<icu::BreakIterator *>(result);
|
||||
}
|
||||
|
||||
static void FreeIcuBreakIterator(void *pointer, [[maybe_unused]] void* hint)
|
||||
{
|
||||
if (pointer == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto icuBreakIterator = reinterpret_cast<icu::BreakIterator *>(pointer);
|
||||
delete icuBreakIterator;
|
||||
}
|
||||
|
||||
static void SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
|
||||
icu::BreakIterator* icuBreakIterator, const DeleteEntryPoint &callback);
|
||||
|
||||
static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
|
||||
|
||||
static void ResolvedOptions(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
|
||||
const JSHandle<JSObject> &options);
|
||||
|
||||
static JSHandle<JSTaggedValue> GranularityOptionToEcmaString(JSThread *thread, GranularityOption granularity);
|
||||
|
||||
private:
|
||||
static void InitializeIcuBreakIterator(JSThread *thread, const JSHandle<JSSegmenter> &segmenter,
|
||||
const icu::Locale &icuLocale, GranularityOption granularity);
|
||||
};
|
||||
} // namespace panda::ecmascript
|
||||
#endif // ECMASCRIPT_JS_SEGMENTER_H
|
182
ecmascript/js_segments.cpp
Normal file
182
ecmascript/js_segments.cpp
Normal file
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "ecmascript/js_segments.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "ecmascript/base/builtins_base.h"
|
||||
#include "ecmascript/intl/locale_helper.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/global_env_constants.h"
|
||||
#include "ecmascript/object_factory-inl.h"
|
||||
|
||||
#include "unicode/errorcode.h"
|
||||
#include "unicode/locdspnm.h"
|
||||
#include "unicode/locid.h"
|
||||
#include "unicode/udisplaycontext.h"
|
||||
#include "unicode/uloc.h"
|
||||
#include "unicode/unistr.h"
|
||||
#include "unicode/uscript.h"
|
||||
#include "unicode/ustring.h"
|
||||
|
||||
namespace panda::ecmascript {
|
||||
|
||||
void JSSegments::SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegments> &segments,
|
||||
icu::BreakIterator* icuBreakIterator, const DeleteEntryPoint &callback)
|
||||
{
|
||||
EcmaVM *ecmaVm = thread->GetEcmaVM();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
|
||||
ASSERT(icuBreakIterator != nullptr);
|
||||
JSTaggedValue data = segments->GetIcuField();
|
||||
if (data.IsJSNativePointer()) {
|
||||
JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
|
||||
native->ResetExternalPointer(icuBreakIterator);
|
||||
return;
|
||||
}
|
||||
JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(icuBreakIterator, callback);
|
||||
segments->SetIcuField(thread, pointer.GetTaggedValue());
|
||||
}
|
||||
|
||||
void JSSegments::SetUString(JSThread *thread, const JSHandle<JSSegments> &segments,
|
||||
icu::UnicodeString* icuUnicodeString, const DeleteEntryPoint &callback)
|
||||
{
|
||||
EcmaVM *ecmaVm = thread->GetEcmaVM();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
|
||||
ASSERT(icuUnicodeString != nullptr);
|
||||
JSTaggedValue data = segments->GetUnicodeString();
|
||||
if (data.IsJSNativePointer()) {
|
||||
JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
|
||||
native->ResetExternalPointer(icuUnicodeString);
|
||||
return;
|
||||
}
|
||||
JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(icuUnicodeString, callback);
|
||||
segments->SetUnicodeString(thread, pointer.GetTaggedValue());
|
||||
}
|
||||
|
||||
void SetTextToBreakIterator(JSThread *thread, const JSHandle<JSSegments> &segments,
|
||||
JSHandle<EcmaString> text, icu::BreakIterator* breakIterator)
|
||||
{
|
||||
std::u16string u16str = EcmaStringAccessor(text).ToU16String();
|
||||
icu::UnicodeString src(u16str.data(), u16str.size());
|
||||
icu::UnicodeString* uText = static_cast<icu::UnicodeString*>(src.clone());
|
||||
breakIterator->setText(*uText);
|
||||
JSSegments::SetUString(thread, segments, uText, JSSegments::FreeUString);
|
||||
}
|
||||
|
||||
JSHandle<JSSegments> JSSegments::CreateSegmentsObject(JSThread *thread,
|
||||
const JSHandle<JSSegmenter> &segmenter,
|
||||
const JSHandle<EcmaString> &string)
|
||||
{
|
||||
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
||||
// 1. Let internalSlotsList be « [[SegmentsSegmenter]], [[SegmentsString]] ».
|
||||
// 2. Let segments be OrdinaryObjectCreate(%SegmentsPrototype%, internalSlotsList).
|
||||
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
||||
JSHandle<JSFunction> segmentsCtor(env->GetSegmentsFunction());
|
||||
JSHandle<JSSegments> segments(factory->NewJSObjectByConstructor(segmentsCtor));
|
||||
// 3. Set segments.[[SegmentsSegmenter]] to segmenter.
|
||||
icu::BreakIterator* icuBreakIterator = segmenter->GetIcuBreakIterator()->clone();
|
||||
SetIcuBreakIterator(thread, segments, icuBreakIterator, JSSegments::FreeIcuBreakIterator);
|
||||
segments->SetGranularity(segmenter->GetGranularity());
|
||||
// 4. Set segments.[[SegmentsString]] to string.
|
||||
segments->SetSegmentsString(thread, string);
|
||||
SetTextToBreakIterator(thread, segments, string, icuBreakIterator);
|
||||
return segments;
|
||||
}
|
||||
|
||||
JSTaggedValue JSSegments::Containing(JSThread *thread, const JSHandle<JSSegments> &segments, double index)
|
||||
{
|
||||
icu::UnicodeString* unicodeString = segments->GetUString();
|
||||
// 5. Let len be the length of string.
|
||||
int32_t len = unicodeString->length();
|
||||
// 7. If n < 0 or n ≥ len, return undefined.
|
||||
if (index < 0 || index >= len) {
|
||||
return JSTaggedValue::Undefined();
|
||||
}
|
||||
int32_t n = static_cast<int32_t>(index);
|
||||
// n may point to the surrogate tail- adjust it back to the lead.
|
||||
n = unicodeString->getChar32Start(n);
|
||||
icu::BreakIterator* breakIterator = segments->GetIcuBreakIterator();
|
||||
// 8. Let startIndex be ! FindBoundary(segmenter, string, n, before).
|
||||
int32_t startIndex = breakIterator->isBoundary(n) ? n : breakIterator->preceding(n);
|
||||
// 9. Let endIndex be ! FindBoundary(segmenter, string, n, after).
|
||||
int32_t endIndex = breakIterator->following(n);
|
||||
// 10. Return ! CreateSegmentDataObject(segmenter, string, startIndex, endIndex).
|
||||
return CreateSegmentDataObject(thread, segments->GetGranularity(), breakIterator,
|
||||
JSHandle<EcmaString>(thread, segments->GetSegmentsString()),
|
||||
*unicodeString, startIndex, endIndex).GetTaggedValue();
|
||||
}
|
||||
|
||||
bool CurrentSegmentIsWordLike(icu::BreakIterator* breakIterator)
|
||||
{
|
||||
int32_t rule_status = breakIterator->getRuleStatus();
|
||||
return (rule_status >= UBRK_WORD_NUMBER &&
|
||||
rule_status < UBRK_WORD_NUMBER_LIMIT) ||
|
||||
(rule_status >= UBRK_WORD_LETTER &&
|
||||
rule_status < UBRK_WORD_LETTER_LIMIT) ||
|
||||
(rule_status >= UBRK_WORD_KANA &&
|
||||
rule_status < UBRK_WORD_KANA_LIMIT) ||
|
||||
(rule_status >= UBRK_WORD_IDEO && rule_status < UBRK_WORD_IDEO_LIMIT);
|
||||
}
|
||||
|
||||
// 18.7.1 CreateSegmentDataObject ( segmenter, string, startIndex, endIndex )
|
||||
JSHandle<JSObject> JSSegments::CreateSegmentDataObject(JSThread *thread, GranularityOption granularity,
|
||||
icu::BreakIterator* breakIterator, const JSHandle<EcmaString> &inputString,
|
||||
const icu::UnicodeString& unicodeString, int32_t startIndex, int32_t endIndex)
|
||||
{
|
||||
// 1. Let len be the length of string.
|
||||
// 2. Assert: startIndex ≥ 0.
|
||||
ASSERT(startIndex >= 0);
|
||||
// 3. Assert: endIndex ≤ len.
|
||||
ASSERT(endIndex <= unicodeString.length());
|
||||
// 4. Assert: startIndex < endIndex.
|
||||
ASSERT(startIndex < endIndex);
|
||||
// 5. Let result be OrdinaryObjectCreate(%Object.prototype%).
|
||||
auto ecmaVm = thread->GetEcmaVM();
|
||||
JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
|
||||
ObjectFactory *factory = ecmaVm->GetFactory();
|
||||
JSHandle<JSFunction> ctor(env->GetObjectFunction());
|
||||
JSHandle<JSObject> result(factory->NewJSObjectByConstructor(ctor));
|
||||
// 6. Let segment be the substring of string from startIndex to endIndex.
|
||||
JSHandle<EcmaString> segment =
|
||||
intl::LocaleHelper::UStringToString(thread, unicodeString, startIndex, endIndex);
|
||||
auto globalConst = thread->GlobalConstants();
|
||||
// 7. Perform ! CreateDataPropertyOrThrow(result, "segment", segment).
|
||||
JSHandle<JSTaggedValue> segmentKey = globalConst->GetHandledSegmentString();
|
||||
JSObject::CreateDataPropertyOrThrow(thread, result, segmentKey, JSHandle<JSTaggedValue>::Cast(segment));
|
||||
// 8. Perform ! CreateDataPropertyOrThrow(result, "index", 𝔽(startIndex)).
|
||||
JSHandle<JSTaggedValue> indexKey = globalConst->GetHandledIndexString();
|
||||
JSObject::CreateDataPropertyOrThrow(thread, result, indexKey, JSHandle<JSTaggedValue>(thread,
|
||||
base::BuiltinsBase::GetTaggedInt(startIndex)));
|
||||
// 9. Perform ! CreateDataPropertyOrThrow(result, "input", string).
|
||||
JSHandle<JSTaggedValue> inputKey = globalConst->GetHandledInputString();
|
||||
JSObject::CreateDataPropertyOrThrow(thread, result, inputKey, JSHandle<JSTaggedValue>::Cast(inputString));
|
||||
// 10. Let granularity be segmenter.[[SegmenterGranularity]].
|
||||
// 11. If granularity is "word", then
|
||||
// a. Let isWordLike be a Boolean value indicating whether the segment in string is "word-like"
|
||||
// according to locale segmenter.[[Locale]].
|
||||
// b. Perform ! CreateDataPropertyOrThrow(result, "isWordLike", isWordLike).
|
||||
if (granularity == GranularityOption::WORD) {
|
||||
bool isWordLike = CurrentSegmentIsWordLike(breakIterator);
|
||||
JSHandle<JSTaggedValue> isWordLikeKey = globalConst->GetHandledIsWordLikeString();
|
||||
JSObject::CreateDataPropertyOrThrow(thread, result, isWordLikeKey, JSHandle<JSTaggedValue>(thread,
|
||||
base::BuiltinsBase::GetTaggedBoolean(isWordLike)));
|
||||
}
|
||||
// 12. Return result.
|
||||
return result;
|
||||
}
|
||||
} // namespace panda::ecmascript
|
103
ecmascript/js_segments.h
Normal file
103
ecmascript/js_segments.h
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef ECMASCRIPT_JS_SEGMENTS_H
|
||||
#define ECMASCRIPT_JS_SEGMENTS_H
|
||||
|
||||
#include "ecmascript/ecma_string.h"
|
||||
#include "ecmascript/ecma_vm.h"
|
||||
#include "ecmascript/ecma_macros.h"
|
||||
#include "ecmascript/global_env.h"
|
||||
#include "ecmascript/js_hclass.h"
|
||||
#include "ecmascript/js_intl.h"
|
||||
#include "ecmascript/js_locale.h"
|
||||
#include "ecmascript/js_object.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_tagged_value.h"
|
||||
#include "ecmascript/object_factory.h"
|
||||
|
||||
#include "unicode/locdspnm.h"
|
||||
|
||||
namespace panda::ecmascript {
|
||||
class JSSegments : public JSObject {
|
||||
public:
|
||||
CAST_CHECK(JSSegments, IsJSSegments);
|
||||
|
||||
static constexpr size_t ICU_FIELD_OFFSET = JSObject::SIZE;
|
||||
ACCESSORS(IcuField, ICU_FIELD_OFFSET, SEGMENTS_STRING_OFFSET)
|
||||
ACCESSORS(SegmentsString, SEGMENTS_STRING_OFFSET, UNICODE_STRING_OFFSET)
|
||||
ACCESSORS(UnicodeString, UNICODE_STRING_OFFSET, BIT_FIELD_OFFSET)
|
||||
ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
|
||||
DEFINE_ALIGN_SIZE(LAST_OFFSET);
|
||||
|
||||
// define BitField
|
||||
static constexpr size_t GRANULARITY_BITS = 3;
|
||||
FIRST_BIT_FIELD(BitField, Granularity, GranularityOption, GRANULARITY_BITS)
|
||||
|
||||
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
|
||||
DECL_DUMP()
|
||||
|
||||
// 18.5.1 CreateSegmentsObject ( segmenter, string )
|
||||
static JSHandle<JSSegments> CreateSegmentsObject(JSThread *thread,
|
||||
const JSHandle<JSSegmenter> &segmenter,
|
||||
const JSHandle<EcmaString> &string);
|
||||
// Get icu break iterator from icu field
|
||||
icu::BreakIterator *GetIcuBreakIterator() const
|
||||
{
|
||||
ASSERT(GetIcuField().IsJSNativePointer());
|
||||
auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
|
||||
return reinterpret_cast<icu::BreakIterator *>(result);
|
||||
}
|
||||
|
||||
static void FreeIcuBreakIterator(void *pointer, [[maybe_unused]] void* hint)
|
||||
{
|
||||
if (pointer == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto icuBreakIterator = reinterpret_cast<icu::BreakIterator *>(pointer);
|
||||
delete icuBreakIterator;
|
||||
}
|
||||
|
||||
static void SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegments> &segments,
|
||||
icu::BreakIterator* icuBreakIterator, const DeleteEntryPoint &callback);
|
||||
|
||||
static void FreeUString(void *pointer, [[maybe_unused]] void* hint)
|
||||
{
|
||||
if (pointer == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto unicodeString = reinterpret_cast<icu::UnicodeString *>(pointer);
|
||||
delete unicodeString;
|
||||
}
|
||||
|
||||
static void SetUString(JSThread *thread, const JSHandle<JSSegments> &segments,
|
||||
icu::UnicodeString* icuUnicodeString, const DeleteEntryPoint &callback);
|
||||
|
||||
icu::UnicodeString *GetUString() const
|
||||
{
|
||||
ASSERT(GetUnicodeString().IsJSNativePointer());
|
||||
auto result = JSNativePointer::Cast(GetUnicodeString().GetTaggedObject())->GetExternalPointer();
|
||||
return reinterpret_cast<icu::UnicodeString *>(result);
|
||||
}
|
||||
|
||||
static JSTaggedValue Containing(JSThread *thread, const JSHandle<JSSegments> &segments, double index);
|
||||
|
||||
// 18.7.1 CreateSegmentDataObject ( segmenter, string, startIndex, endIndex )
|
||||
static JSHandle<JSObject> CreateSegmentDataObject(JSThread *thread, GranularityOption granularity,
|
||||
icu::BreakIterator* breakIterator, const JSHandle<EcmaString> &inputString,
|
||||
const icu::UnicodeString& unicodeString, int32_t startIndex, int32_t endIndex);
|
||||
};
|
||||
} // namespace panda::ecmascript
|
||||
#endif // ECMASCRIPT_JS_SEGMENTS_H
|
@ -713,6 +713,21 @@ inline bool JSTaggedValue::IsJSDisplayNames() const
|
||||
return IsHeapObject() && GetTaggedObject()->GetClass()->IsJSDisplayNames();
|
||||
}
|
||||
|
||||
inline bool JSTaggedValue::IsJSSegmenter() const
|
||||
{
|
||||
return IsHeapObject() && GetTaggedObject()->GetClass()->IsJSSegmenter();
|
||||
}
|
||||
|
||||
inline bool JSTaggedValue::IsJSSegments() const
|
||||
{
|
||||
return IsHeapObject() && GetTaggedObject()->GetClass()->IsJSSegments();
|
||||
}
|
||||
|
||||
inline bool JSTaggedValue::IsJSSegmentIterator() const
|
||||
{
|
||||
return IsHeapObject() && GetTaggedObject()->GetClass()->IsJSSegmentIterator();
|
||||
}
|
||||
|
||||
inline bool JSTaggedValue::IsJSListFormat() const
|
||||
{
|
||||
return IsHeapObject() && GetTaggedObject()->GetClass()->IsJSListFormat();
|
||||
|
@ -576,6 +576,9 @@ public:
|
||||
bool IsJSCollator() const;
|
||||
bool IsJSPluralRules() const;
|
||||
bool IsJSDisplayNames() const;
|
||||
bool IsJSSegmenter() const;
|
||||
bool IsJSSegments() const;
|
||||
bool IsJSSegmentIterator() const;
|
||||
bool IsJSListFormat() const;
|
||||
bool IsMethod() const;
|
||||
bool IsClassLiteral() const;
|
||||
|
@ -86,6 +86,9 @@
|
||||
#include "ecmascript/js_regexp.h"
|
||||
#include "ecmascript/js_regexp_iterator.h"
|
||||
#include "ecmascript/js_relative_time_format.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
#include "ecmascript/js_set.h"
|
||||
#include "ecmascript/js_displaynames.h"
|
||||
#include "ecmascript/js_list_format.h"
|
||||
@ -513,6 +516,15 @@ public:
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
JSDisplayNames::Cast(object)->VisitRangeSlot<visitType>(visitor);
|
||||
break;
|
||||
case JSType::JS_SEGMENTER:
|
||||
JSSegmenter::Cast(object)->VisitRangeSlot<visitType>(visitor);
|
||||
break;
|
||||
case JSType::JS_SEGMENTS:
|
||||
JSSegments::Cast(object)->VisitRangeSlot<visitType>(visitor);
|
||||
break;
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
JSSegmentIterator::Cast(object)->VisitRangeSlot<visitType>(visitor);
|
||||
break;
|
||||
case JSType::JS_LIST_FORMAT:
|
||||
JSListFormat::Cast(object)->VisitRangeSlot<visitType>(visitor);
|
||||
break;
|
||||
|
@ -137,6 +137,9 @@
|
||||
#include "ecmascript/js_number_format.h"
|
||||
#include "ecmascript/js_plural_rules.h"
|
||||
#include "ecmascript/js_relative_time_format.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
#endif
|
||||
namespace panda::ecmascript {
|
||||
using Error = builtins::BuiltinsError;
|
||||
@ -1115,6 +1118,26 @@ void ObjectFactory::InitializeJSObject(const JSHandle<JSObject> &obj, const JSHa
|
||||
JSDisplayNames::Cast(*obj)->SetIcuLDN(thread_, JSTaggedValue::Undefined());
|
||||
break;
|
||||
}
|
||||
case JSType::JS_SEGMENTER: {
|
||||
JSSegmenter::Cast(*obj)->SetLocale(thread_, JSTaggedValue::Undefined());
|
||||
JSSegmenter::Cast(*obj)->SetGranularity(GranularityOption::EXCEPTION);
|
||||
JSSegmenter::Cast(*obj)->SetIcuField(thread_, JSTaggedValue::Undefined());
|
||||
break;
|
||||
}
|
||||
case JSType::JS_SEGMENTS: {
|
||||
JSSegments::Cast(*obj)->SetIcuField(thread_, JSTaggedValue::Undefined());
|
||||
JSSegments::Cast(*obj)->SetSegmentsString(thread_, JSTaggedValue::Undefined());
|
||||
JSSegments::Cast(*obj)->SetUnicodeString(thread_, JSTaggedValue::Undefined());
|
||||
JSSegments::Cast(*obj)->SetGranularity(GranularityOption::EXCEPTION);
|
||||
break;
|
||||
}
|
||||
case JSType::JS_SEGMENT_ITERATOR: {
|
||||
JSSegmentIterator::Cast(*obj)->SetIcuField(thread_, JSTaggedValue::Undefined());
|
||||
JSSegmentIterator::Cast(*obj)->SetIteratedString(thread_, JSTaggedValue::Undefined());
|
||||
JSSegmentIterator::Cast(*obj)->SetUnicodeString(thread_, JSTaggedValue::Undefined());
|
||||
JSSegmentIterator::Cast(*obj)->SetGranularity(GranularityOption::EXCEPTION);
|
||||
break;
|
||||
}
|
||||
case JSType::JS_LIST_FORMAT: {
|
||||
JSListFormat::Cast(*obj)->SetLocale(thread_, JSTaggedValue::Undefined());
|
||||
JSListFormat::Cast(*obj)->SetType(ListTypeOption::EXCEPTION);
|
||||
@ -1131,6 +1154,9 @@ void ObjectFactory::InitializeJSObject(const JSHandle<JSObject> &obj, const JSHa
|
||||
case JSType::JS_COLLATOR:
|
||||
case JSType::JS_PLURAL_RULES:
|
||||
case JSType::JS_DISPLAYNAMES:
|
||||
case JSType::JS_SEGMENTER:
|
||||
case JSType::JS_SEGMENTS:
|
||||
case JSType::JS_SEGMENT_ITERATOR:
|
||||
case JSType::JS_LIST_FORMAT: {
|
||||
break;
|
||||
}
|
||||
|
@ -390,6 +390,13 @@ namespace panda::ecmascript {
|
||||
V(DisplayNames, SupportedLocalesOf) \
|
||||
V(DisplayNames, Of) \
|
||||
V(DisplayNames, ResolvedOptions) \
|
||||
V(Segmenter, Constructor) \
|
||||
V(Segmenter, SupportedLocalesOf) \
|
||||
V(Segmenter, ResolvedOptions) \
|
||||
V(Segmenter, Segment) \
|
||||
V(Segments, Containing) \
|
||||
V(Segments, GetSegmentIterator) \
|
||||
V(SegmentIterator, Next) \
|
||||
V(Error, ErrorConstructor) \
|
||||
V(Error, ErrorToString) \
|
||||
V(Error, RangeErrorConstructor) \
|
||||
|
@ -114,6 +114,9 @@
|
||||
#include "ecmascript/builtins/builtins_number_format.h"
|
||||
#include "ecmascript/builtins/builtins_plural_rules.h"
|
||||
#include "ecmascript/builtins/builtins_relative_time_format.h"
|
||||
#include "ecmascript/builtins/builtins_segmenter.h"
|
||||
#include "ecmascript/builtins/builtins_segments.h"
|
||||
#include "ecmascript/builtins/builtins_segment_iterator.h"
|
||||
#endif
|
||||
|
||||
namespace panda::ecmascript {
|
||||
@ -192,6 +195,8 @@ using NumberFormat = builtins::BuiltinsNumberFormat;
|
||||
using RelativeTimeFormat = builtins::BuiltinsRelativeTimeFormat;
|
||||
using Collator = builtins::BuiltinsCollator;
|
||||
using PluralRules = builtins::BuiltinsPluralRules;
|
||||
using Segmenter = builtins::BuiltinsSegmenter;
|
||||
using Segments = builtins::BuiltinsSegments;
|
||||
#endif
|
||||
|
||||
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
|
||||
@ -728,6 +733,11 @@ static uintptr_t g_nativeTable[] = {
|
||||
reinterpret_cast<uintptr_t>(ListFormat::Format),
|
||||
reinterpret_cast<uintptr_t>(ListFormat::FormatToParts),
|
||||
reinterpret_cast<uintptr_t>(ListFormat::ResolvedOptions),
|
||||
reinterpret_cast<uintptr_t>(Segmenter::SegmenterConstructor),
|
||||
reinterpret_cast<uintptr_t>(Segmenter::SupportedLocalesOf),
|
||||
reinterpret_cast<uintptr_t>(Segmenter::ResolvedOptions),
|
||||
reinterpret_cast<uintptr_t>(Segmenter::Segment),
|
||||
reinterpret_cast<uintptr_t>(Segments::Containing),
|
||||
#endif
|
||||
|
||||
// non ECMA standard jsapi containers.
|
||||
|
@ -91,6 +91,9 @@
|
||||
#include "ecmascript/js_regexp.h"
|
||||
#include "ecmascript/js_regexp_iterator.h"
|
||||
#include "ecmascript/js_relative_time_format.h"
|
||||
#include "ecmascript/js_segmenter.h"
|
||||
#include "ecmascript/js_segments.h"
|
||||
#include "ecmascript/js_segment_iterator.h"
|
||||
#include "ecmascript/js_set.h"
|
||||
#include "ecmascript/js_set_iterator.h"
|
||||
#include "ecmascript/js_string_iterator.h"
|
||||
@ -730,6 +733,21 @@ HWTEST_F_L0(EcmaDumpTest, HeapProfileDump)
|
||||
NEW_OBJECT_AND_DUMP(JSDisplayNames, JS_DISPLAYNAMES);
|
||||
break;
|
||||
}
|
||||
case JSType::JS_SEGMENTER: {
|
||||
CHECK_DUMP_FIELDS(JSObject::SIZE, JSSegmenter::SIZE, 3U);
|
||||
NEW_OBJECT_AND_DUMP(JSSegmenter, JS_SEGMENTER);
|
||||
break;
|
||||
}
|
||||
case JSType::JS_SEGMENTS: {
|
||||
CHECK_DUMP_FIELDS(JSObject::SIZE, JSSegments::SIZE, 4U);
|
||||
NEW_OBJECT_AND_DUMP(JSSegments, JS_SEGMENTS);
|
||||
break;
|
||||
}
|
||||
case JSType::JS_SEGMENT_ITERATOR: {
|
||||
CHECK_DUMP_FIELDS(JSObject::SIZE, JSSegmentIterator::SIZE, 4U);
|
||||
NEW_OBJECT_AND_DUMP(JSSegmentIterator, JS_SEGMENTS);
|
||||
break;
|
||||
}
|
||||
case JSType::JS_LIST_FORMAT: {
|
||||
CHECK_DUMP_FIELDS(JSObject::SIZE, JSListFormat::SIZE, 3U);
|
||||
NEW_OBJECT_AND_DUMP(JSListFormat, JS_LIST_FORMAT);
|
||||
|
@ -153,6 +153,11 @@
|
||||
<option name="push" value="test/test/libark_jsruntime_test.so -> /data/test" src="out"/>
|
||||
</preparer>
|
||||
</target>
|
||||
<target name="BuiltinsInternational_016_Test">
|
||||
<preparer>
|
||||
<option name="push" value="test/test/libark_jsruntime_test.so -> /data/test" src="out"/>
|
||||
</preparer>
|
||||
</target>
|
||||
<target name="BuiltinsNatural_001_Test">
|
||||
<preparer>
|
||||
<option name="push" value="test/test/libark_jsruntime_test.so -> /data/test" src="out"/>
|
||||
|
Loading…
Reference in New Issue
Block a user