From 20f253caa1eab853f30213d015b6faaf3c44398c Mon Sep 17 00:00:00 2001 From: Ilya Trubachev Date: Sun, 25 Sep 2022 17:40:25 +0300 Subject: [PATCH 1/2] fix icu data init Signed-off-by: Ilya Trubachev --- runtime/CMakeLists.txt | 3 +++ runtime/builtins.cpp | 2 +- runtime/init_icu.h | 25 ++++++++++++++++++++ runtime/js_locale.h | 2 +- runtime/templates/init_icu.cpp.in | 39 +++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 runtime/init_icu.h create mode 100644 runtime/templates/init_icu.cpp.in diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index f167d53f6..bd3a99285 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -27,7 +27,10 @@ endif() set(ECMA_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") file(MAKE_DIRECTORY "${ECMA_GEN_DIR}") +configure_file(${ECMA_SRC_DIR}/templates/init_icu.cpp.in ${ECMA_GEN_DIR}/init_icu.cpp) + set(ECMASCRIPT_SOURCES + ${ECMA_GEN_DIR}/init_icu.cpp ${ECMA_SRC_DIR}/bridge/ecma_bridge_helpers.cpp ${ECMA_SRC_DIR}/builtins/builtins_collator.cpp ${ECMA_SRC_DIR}/builtins/builtins_date_time_format.cpp diff --git a/runtime/builtins.cpp b/runtime/builtins.cpp index 99131ff35..270d73065 100644 --- a/runtime/builtins.cpp +++ b/runtime/builtins.cpp @@ -16,6 +16,7 @@ #include "plugins/ecmascript/runtime/builtins.h" #include "ecma_vm.h" +#include "init_icu.h" #include "plugins/ecmascript/runtime/base/error_type.h" #include "plugins/ecmascript/runtime/base/number_helper.h" #include "plugins/ecmascript/runtime/builtins/builtins_ark_tools.h" @@ -96,7 +97,6 @@ #include "plugins/ecmascript/runtime/js_finalization_registry.h" #include "plugins/ecmascript/runtime/mem/mem.h" #include "plugins/ecmascript/runtime/object_factory.h" -#include "ohos/init_data.h" namespace panda::ecmascript { using Number = builtins::BuiltinsNumber; diff --git a/runtime/init_icu.h b/runtime/init_icu.h new file mode 100644 index 000000000..bd98ac545 --- /dev/null +++ b/runtime/init_icu.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 Huawei Device 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 INIT_HW_DATA_H +#define INIT_HW_DATA_H + +#include "common/unicode/putil.h" + +/** + * convienient method to set icu data file directory to HW_DIRECTORY); + */ +void SetHwIcuDirectory(); +#endif diff --git a/runtime/js_locale.h b/runtime/js_locale.h index d3eba38bd..15c088540 100644 --- a/runtime/js_locale.h +++ b/runtime/js_locale.h @@ -16,10 +16,10 @@ #ifndef ECMASCRIPT_JSLOCALE_H #define ECMASCRIPT_JSLOCALE_H +#include "init_icu.h" #include "plugins/ecmascript/runtime/ecma_macros.h" #include "plugins/ecmascript/runtime/js_array.h" #include "plugins/ecmascript/runtime/js_object.h" -#include "ohos/init_data.h" #include "unicode/basictz.h" #include "unicode/brkiter.h" #include "unicode/calendar.h" diff --git a/runtime/templates/init_icu.cpp.in b/runtime/templates/init_icu.cpp.in new file mode 100644 index 000000000..04ea53697 --- /dev/null +++ b/runtime/templates/init_icu.cpp.in @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device 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 "init_icu.h" + +#include +#include + +namespace { +// NOLINTNEXTLINE(fuchsia-statically-constructed-objects) +std::string g_hwDirectory = std::string("@PANDA_THIRD_PARTY_SOURCES_DIR@") + std::string("/icu/icu4c/source/data/out/tmp"); +int status = 0; +std::mutex dataMutex; +} // namespace + +/** + * set icu data files to hwDirectory + */ +void SetHwIcuDirectory() +{ + std::lock_guard lock(dataMutex); + if (status != 0) { + return; + } + u_setDataDirectory(g_hwDirectory.c_str()); + status = 1; +} -- Gitee From 8b67e664f5414f7a9f3e4fee92d5a668be5d6faf Mon Sep 17 00:00:00 2001 From: Ilya Trubachev Date: Thu, 29 Sep 2022 18:34:24 +0300 Subject: [PATCH 2/2] support surrogate pairs in icu and update icu version Signed-off-by: Ilya Trubachev --- BUILD.gn | 11 ++++++++ runtime/CMakeLists.txt | 8 ++++-- runtime/base/utf_helper.cpp | 5 ++++ runtime/base/utf_helper.h | 2 ++ runtime/init_icu.cpp | 17 +++++++++++++ runtime/init_icu.h | 6 ++--- runtime/interpreter/slow_runtime_stub.cpp | 20 ++++++++++++--- runtime/runtime_sources.gn | 1 + .../{init_icu.cpp.in => init_icu_gen.cpp.erb} | 11 +++----- runtime/templates/substitute_icu_path.rb | 25 +++++++++++++++++++ subproject_sources.gn | 1 + 11 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 runtime/init_icu.cpp rename runtime/templates/{init_icu.cpp.in => init_icu_gen.cpp.erb} (78%) create mode 100755 runtime/templates/substitute_icu_path.rb diff --git a/BUILD.gn b/BUILD.gn index 4d54e4bbc..d5150242c 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -80,6 +80,17 @@ action("ecmastblib_inline_h") { ] } +# Substitute ICU path +action("substitute_icu_path") { + script = "runtime/templates/substitute_icu_path.rb" + outputs = [ "$target_gen_dir/init_icu_gen.cpp" ] + args = [ + rebase_path("runtime/templates/init_icu_gen.cpp.erb"), + rebase_path("$target_gen_dir/init_icu_gen.cpp"), + rebase_path("$ark_root/ark-third-party"), + ] +} + ark_gen_file("ecma_intrinsics_gen_arkruntime") { template_file = "$ark_root/plugins/ecmascript/runtime/templates/intrinsics_gen.cpp.erb" diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index bd3a99285..56b25a893 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -27,10 +27,13 @@ endif() set(ECMA_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") file(MAKE_DIRECTORY "${ECMA_GEN_DIR}") -configure_file(${ECMA_SRC_DIR}/templates/init_icu.cpp.in ${ECMA_GEN_DIR}/init_icu.cpp) +add_custom_target(init_icu_gen_cpp + COMMAND ${ECMA_SRC_DIR}/templates/substitute_icu_path.rb ${ECMA_SRC_DIR}/templates/init_icu_gen.cpp.erb ${ECMA_GEN_DIR}/init_icu_gen.cpp ${PANDA_THIRD_PARTY_SOURCES_DIR} + DEPENDS ${ECMA_SRC_DIR}/templates/init_icu_gen.cpp.erb) + +add_dependencies(arkruntime_static init_icu_gen_cpp) set(ECMASCRIPT_SOURCES - ${ECMA_GEN_DIR}/init_icu.cpp ${ECMA_SRC_DIR}/bridge/ecma_bridge_helpers.cpp ${ECMA_SRC_DIR}/builtins/builtins_collator.cpp ${ECMA_SRC_DIR}/builtins/builtins_date_time_format.cpp @@ -112,6 +115,7 @@ set(ECMASCRIPT_SOURCES ${ECMA_SRC_DIR}/ic/ic_runtime_stub.cpp ${ECMA_SRC_DIR}/ic/property_box.cpp ${ECMA_SRC_DIR}/ic/proto_change_details.cpp + ${ECMA_SRC_DIR}/init_icu.cpp ${ECMA_SRC_DIR}/internal_call_params.cpp ${ECMA_SRC_DIR}/interpreter/slow_runtime_helper.cpp ${ECMA_SRC_DIR}/interpreter/slow_runtime_stub.cpp diff --git a/runtime/base/utf_helper.cpp b/runtime/base/utf_helper.cpp index bf5a8f682..b79260ce2 100644 --- a/runtime/base/utf_helper.cpp +++ b/runtime/base/utf_helper.cpp @@ -226,4 +226,9 @@ size_t ConvertRegionUtf8ToUtf16(const uint8_t *utf8In, uint16_t *utf16Out, size_ { return utf::ConvertRegionMUtf8ToUtf16(utf8In, utf16Out, utf8Len, utf16Len, start); } + +bool IsUTF16SurrogatePair(const uint16_t lead) +{ + return lead >= DECODE_LEAD_LOW && lead <= DECODE_LEAD_HIGH; +} } // namespace panda::ecmascript::base::utf_helper diff --git a/runtime/base/utf_helper.h b/runtime/base/utf_helper.h index 71271254a..5798e2c9a 100644 --- a/runtime/base/utf_helper.h +++ b/runtime/base/utf_helper.h @@ -75,6 +75,8 @@ size_t Utf8ToUtf16Size(const uint8_t *utf8, size_t utf8Len); size_t ConvertRegionUtf8ToUtf16(const uint8_t *utf8In, uint16_t *utf16Out, size_t utf8Len, size_t utf16Len, size_t start); +bool IsUTF16SurrogatePair(uint16_t lead); + static inline uint32_t CombineTwoU16(uint16_t d0, uint16_t d1) { uint32_t codePoint = d0 - utf::HI_SURROGATE_MIN; diff --git a/runtime/init_icu.cpp b/runtime/init_icu.cpp new file mode 100644 index 000000000..368342f7e --- /dev/null +++ b/runtime/init_icu.cpp @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2022 Huawei Device 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 "init_icu.h" +#include "init_icu_gen.cpp" diff --git a/runtime/init_icu.h b/runtime/init_icu.h index bd98ac545..0ac3b594c 100644 --- a/runtime/init_icu.h +++ b/runtime/init_icu.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef INIT_HW_DATA_H -#define INIT_HW_DATA_H +#ifndef RUNTIME_INIT_ICU_H +#define RUNTIME_INIT_ICU_H #include "common/unicode/putil.h" @@ -22,4 +22,4 @@ * convienient method to set icu data file directory to HW_DIRECTORY); */ void SetHwIcuDirectory(); -#endif +#endif // RUNTIME_INIT_ICU_H diff --git a/runtime/interpreter/slow_runtime_stub.cpp b/runtime/interpreter/slow_runtime_stub.cpp index 4149507aa..64bfe2a84 100644 --- a/runtime/interpreter/slow_runtime_stub.cpp +++ b/runtime/interpreter/slow_runtime_stub.cpp @@ -16,6 +16,7 @@ #include "plugins/ecmascript/runtime/interpreter/slow_runtime_stub.h" #include "plugins/ecmascript/runtime/base/number_helper.h" +#include "plugins/ecmascript/runtime/base/utf_helper.h" #include "plugins/ecmascript/runtime/builtins/builtins_regexp.h" #include "plugins/ecmascript/runtime/class_linker/program_object-inl.h" #include "plugins/ecmascript/runtime/ecma_module.h" @@ -1601,13 +1602,24 @@ JSTaggedValue SlowRuntimeStub::StArraySpread(JSThread *thread, JSTaggedValue dst RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); uint32_t dst_len = index_handle.GetTaggedValue().GetInt(); uint32_t str_len = src_string->GetLength(); - for (uint32_t i = 0; i < str_len; i++) { + uint32_t prop_counter = 0; + for (uint32_t i = 0; i < str_len; i++, prop_counter++) { uint16_t res = src_string->At(i); - JSHandle strValue(factory->NewFromUtf16Literal(&res, 1)); - JSTaggedValue::SetProperty(thread, dst_handle, dst_len + i, strValue, true); + if (UNLIKELY(base::utf_helper::IsUTF16SurrogatePair(res))) { + std::array res_surrogate_pair {}; + res_surrogate_pair[0] = src_string->At(i); + res_surrogate_pair[1] = src_string->At(i + 1); + JSHandle strValue_surrogate_pair( + factory->NewFromUtf16Literal(res_surrogate_pair.data(), 2)); + JSTaggedValue::SetProperty(thread, dst_handle, dst_len + prop_counter, strValue_surrogate_pair, true); + i++; + } else { + JSHandle strValue(factory->NewFromUtf16Literal(&res, 1)); + JSTaggedValue::SetProperty(thread, dst_handle, dst_len + prop_counter, strValue, true); + } RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); } - return JSTaggedValue(dst_len + str_len); + return JSTaggedValue(dst_len + prop_counter); } JSHandle iter; diff --git a/runtime/runtime_sources.gn b/runtime/runtime_sources.gn index eb742b0e2..1d47ca330 100644 --- a/runtime/runtime_sources.gn +++ b/runtime/runtime_sources.gn @@ -143,6 +143,7 @@ srcs = [ "class_info_extractor.cpp", "compiler/ecmascript_runtime_interface.cpp", "tooling/pt_ecmascript_extension.cpp", + "init_icu.cpp" ] # Should be files deleted??? diff --git a/runtime/templates/init_icu.cpp.in b/runtime/templates/init_icu_gen.cpp.erb similarity index 78% rename from runtime/templates/init_icu.cpp.in rename to runtime/templates/init_icu_gen.cpp.erb index 04ea53697..d15582bac 100644 --- a/runtime/templates/init_icu.cpp.in +++ b/runtime/templates/init_icu_gen.cpp.erb @@ -18,18 +18,15 @@ #include #include -namespace { -// NOLINTNEXTLINE(fuchsia-statically-constructed-objects) -std::string g_hwDirectory = std::string("@PANDA_THIRD_PARTY_SOURCES_DIR@") + std::string("/icu/icu4c/source/data/out/tmp"); -int status = 0; -std::mutex dataMutex; -} // namespace - /** * set icu data files to hwDirectory */ void SetHwIcuDirectory() { + static int status = 0; + static std::mutex dataMutex; + std::string g_hwDirectory = std::string("<%= PANDA_THIRD_PARTY_SOURCES_DIR %>") + std::string("/icu/ohos_icu4j/data"); + std::lock_guard lock(dataMutex); if (status != 0) { return; diff --git a/runtime/templates/substitute_icu_path.rb b/runtime/templates/substitute_icu_path.rb new file mode 100755 index 000000000..950522d82 --- /dev/null +++ b/runtime/templates/substitute_icu_path.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby +# Copyright (c) 2021-2022 Huawei Device 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. + +# Huawei Technologies Co.,Ltd. + +require 'erb' + +template = File.read(File.expand_path(ARGV[0])) +output = File.open(File.expand_path(ARGV[1]), 'w') +PANDA_THIRD_PARTY_SOURCES_DIR = ARGV[2] + +t = ERB.new(template, 0, "%<>") +output.write(t.result) +output.close diff --git a/subproject_sources.gn b/subproject_sources.gn index d35c75fc8..c081de6b9 100644 --- a/subproject_sources.gn +++ b/subproject_sources.gn @@ -25,6 +25,7 @@ irtoc_plugins = "irtoc_scripts/irtoc_plugins.gn" arkruntime_header_sub_deps = [ ":ecma_intrinsics_gen_arkruntime", ":ecmastblib_inline_h", + ":substitute_icu_path", ] arkcompiler_deps = [ ":isa_gen_ecma_compiler_ecmascript_inst_builder_gen_cpp", -- Gitee