* 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 "ecmascript/base/builtins_base.h"
#include "ecmascript/intl/locale_helper.h"
#include "ecmascript/object_factory-inl.h"
namespace panda::ecmascript {
void JSSegments::SetIcuBreakIterator(JSThread *thread, const JSHandle<JSSegments> &segments,
icu::BreakIterator* icuBreakIterator, const NativePointerCallback &callback)
{
EcmaVM *ecmaVm = thread->GetEcmaVM();
ObjectFactory *factory = ecmaVm->GetFactory();
ASSERT(icuBreakIterator != nullptr);
JSTaggedValue data = segments->GetIcuField(thread);
if (data.IsJSNativePointer()) {
JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
native->ResetExternalPointer(thread, 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 NativePointerCallback &callback)
{
EcmaVM *ecmaVm = thread->GetEcmaVM();
ObjectFactory *factory = ecmaVm->GetFactory();
ASSERT(icuUnicodeString != nullptr);
JSTaggedValue data = segments->GetUnicodeString(thread);
if (data.IsJSNativePointer()) {
JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
native->ResetExternalPointer(thread, 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(thread);
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();
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSFunction> segmentsCtor(env->GetSegmentsFunction());
JSHandle<JSSegments> segments(factory->NewJSObjectByConstructor(segmentsCtor));
icu::BreakIterator* icuBreakIterator = segmenter->GetIcuBreakIterator(thread)->clone();
SetIcuBreakIterator(thread, segments, icuBreakIterator, JSSegments::FreeIcuBreakIterator);
segments->SetGranularity(segmenter->GetGranularity());
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(thread);
int32_t len = unicodeString->length();
if (index < 0 || index >= len) {
return JSTaggedValue::Undefined();
}
int32_t n = static_cast<int32_t>(index);
n = unicodeString->getChar32Start(n);
icu::BreakIterator* breakIterator = segments->GetIcuBreakIterator(thread);
int32_t startIndex = breakIterator->isBoundary(n) ? n : breakIterator->preceding(n);
int32_t endIndex = breakIterator->following(n);
return CreateSegmentDataObject(thread, segments->GetGranularity(), breakIterator,
JSHandle<EcmaString>(thread, segments->GetSegmentsString(thread)),
*unicodeString, startIndex, endIndex).GetTaggedValue();
}
bool CurrentSegmentIsWordLike(icu::BreakIterator* textBreakIterator)
{
int32_t currentSegmentRuleStatus = textBreakIterator->getRuleStatus();
return (currentSegmentRuleStatus >= UBRK_WORD_NUMBER &&
currentSegmentRuleStatus < UBRK_WORD_NUMBER_LIMIT) ||
(currentSegmentRuleStatus >= UBRK_WORD_LETTER &&
currentSegmentRuleStatus < UBRK_WORD_LETTER_LIMIT) ||
(currentSegmentRuleStatus >= UBRK_WORD_KANA &&
currentSegmentRuleStatus < UBRK_WORD_KANA_LIMIT) ||
(currentSegmentRuleStatus >= UBRK_WORD_IDEO &&
currentSegmentRuleStatus < UBRK_WORD_IDEO_LIMIT);
}
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)
{
ASSERT(startIndex >= 0);
ASSERT(endIndex <= unicodeString.length());
ASSERT(startIndex < endIndex);
auto ecmaVm = thread->GetEcmaVM();
JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
ObjectFactory *factory = ecmaVm->GetFactory();
JSHandle<JSFunction> ctor(env->GetObjectFunction());
JSHandle<JSObject> result(factory->NewJSObjectByConstructor(ctor));
JSHandle<EcmaString> segment =
intl::LocaleHelper::UStringToString(thread, unicodeString, startIndex, endIndex);
auto globalConst = thread->GlobalConstants();
JSHandle<JSTaggedValue> segmentKey = globalConst->GetHandledSegmentString();
JSObject::CreateDataPropertyOrThrow(thread, result, segmentKey, JSHandle<JSTaggedValue>::Cast(segment));
JSHandle<JSTaggedValue> indexKey = globalConst->GetHandledIndexString();
JSObject::CreateDataPropertyOrThrow(thread, result, indexKey, JSHandle<JSTaggedValue>(thread,
base::BuiltinsBase::GetTaggedInt(startIndex)));
JSHandle<JSTaggedValue> inputKey = globalConst->GetHandledInputString();
JSObject::CreateDataPropertyOrThrow(thread, result, inputKey, JSHandle<JSTaggedValue>::Cast(inputString));
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)));
}
return result;
}
}