diff --git a/BUILD.gn b/BUILD.gn
index 843cbd002576528020e1879e2e446a9fa37c4e1a..5a91102056dae060cc325d8b29ba833764583f3f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import("//ark/js_runtime/js_runtime_config.gni")
import("//build/ohos.gni")
config("ace_napi_quickjs_config") {
@@ -22,6 +23,14 @@ config("ace_napi_quickjs_config") {
]
}
+config("ace_napi_ark_config") {
+ include_dirs = [
+ "//foundation/ace/napi",
+ "//third_party/libuv/include",
+ "//utils/native/base/include",
+ ]
+}
+
config("ace_napi_config") {
include_dirs = [
"//foundation/ace/napi",
@@ -59,8 +68,6 @@ ohos_shared_library("ace_napi") {
"//utils/native/base:utilsecurec",
]
- cflags_cc = [ "-Wno-missing-braces" ]
-
if (is_standard_system) {
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
}
@@ -103,10 +110,60 @@ ohos_shared_library("ace_napi_quickjs") {
"//third_party/quickjs:qjs",
]
- cflags_cc = [ "-Wno-missing-braces" ]
+ if (is_standard_system) {
+ external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
+ }
+
+ subsystem_name = "ace"
+ part_name = "napi"
+}
+
+ohos_shared_library("ace_napi_ark") {
+ public_configs = [ ":ace_napi_ark_config" ]
+
+ configs = [ "//ark/js_runtime:ark_jsruntime_public_config" ]
+
+ include_dirs = [
+ "//foundation/ace/napi",
+ "//foundation/ace/napi/native_engine",
+ "//foundation/ace/napi/native_engine/impl/ark",
+ "//third_party/libuv/include",
+ "//utils/native/base/include",
+ ]
+
+ defines = [ "USE_ARK_ENGINE" ]
+
+ if (target_cpu == "arm64") {
+ defines += [ "APP_USE_ARM64" ]
+ } else if (target_cpu == "arm") {
+ defines += [ "APP_USE_ARM" ]
+ }
+
+ sources = [
+ "native_engine/impl/ark/ark_native_deferred.cpp",
+ "native_engine/impl/ark/ark_native_engine.cpp",
+ "native_engine/impl/ark/ark_native_reference.cpp",
+ "native_engine/impl/ark/native_value/ark_native_array.cpp",
+ "native_engine/impl/ark/native_value/ark_native_array_buffer.cpp",
+ "native_engine/impl/ark/native_value/ark_native_boolean.cpp",
+ "native_engine/impl/ark/native_value/ark_native_data_view.cpp",
+ "native_engine/impl/ark/native_value/ark_native_external.cpp",
+ "native_engine/impl/ark/native_value/ark_native_function.cpp",
+ "native_engine/impl/ark/native_value/ark_native_number.cpp",
+ "native_engine/impl/ark/native_value/ark_native_object.cpp",
+ "native_engine/impl/ark/native_value/ark_native_string.cpp",
+ "native_engine/impl/ark/native_value/ark_native_typed_array.cpp",
+ "native_engine/impl/ark/native_value/ark_native_value.cpp",
+ ]
+ deps = [
+ ":ace_napi",
+ "//ark/js_runtime:libark_jsruntime",
+ ]
if (is_standard_system) {
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
+ } else {
+ external_deps = [ "hilog:libhilog" ]
}
subsystem_name = "ace"
@@ -116,6 +173,7 @@ ohos_shared_library("ace_napi_quickjs") {
group("napi_packages") {
deps = [
":ace_napi",
+ ":ace_napi_ark",
":ace_napi_quickjs",
]
}
diff --git a/OAT.xml b/OAT.xml
deleted file mode 100644
index aab51654dbaf0ae983539b5028d40257e8b27f97..0000000000000000000000000000000000000000
--- a/OAT.xml
+++ /dev/null
@@ -1,65 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/interfaces/kits/napi/native_api.h b/interfaces/kits/napi/native_api.h
index ee4dea417fa0e84c0d78d3e76eadee2d4e10333e..b1dc7bd6593127cb29d02009354bd8b97602386a 100644
--- a/interfaces/kits/napi/native_api.h
+++ b/interfaces/kits/napi/native_api.h
@@ -16,9 +16,10 @@
#ifndef FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_API_H
#define FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_API_H
+#include
#include "native_common.h"
-#include
+#include "js_native_api.h"
DEPRECATED napi_status napi_create_string_utf16(napi_env env, const char16_t* str, size_t length, napi_value* result);
DEPRECATED napi_status napi_get_value_string_utf16(napi_env env,
@@ -28,10 +29,12 @@ DEPRECATED napi_status napi_get_value_string_utf16(napi_env env,
size_t* result);
DEPRECATED napi_status napi_adjust_external_memory(napi_env env, int64_t change_in_bytes, int64_t* adjusted_value);
napi_status napi_is_callable(napi_env env, napi_value value, bool* result);
- napi_status napi_create_runtime(napi_env env, napi_env* result_env);
- napi_status napi_serialize(napi_env env, napi_value object, napi_value transfer_list, napi_value* result);
- napi_status napi_deserialize(napi_env env, napi_value recorder, napi_value* object);
- napi_status napi_delete_serialization_data(napi_env env, napi_value value);
- napi_status napi_get_exception_info_for_worker(napi_env env, napi_value obj);
+napi_status napi_create_runtime(napi_env env, napi_env* result_env);
+napi_status napi_serialize(napi_env env, napi_value object, napi_value transfer_list, napi_value* result);
+napi_status napi_deserialize(napi_env env, napi_value recorder, napi_value* object);
+napi_status napi_delete_serialization_data(napi_env env, napi_value value);
+napi_status napi_get_exception_info_for_worker(napi_env env, napi_value obj);
+napi_status napi_get_jsEngine(napi_env env, void** pEngine);
+napi_status napi_run_buffer_script(napi_env env, std::vector& buffer, napi_value* result);
#endif /* FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_API_H */
diff --git a/interfaces/kits/napi/native_node_api.h b/interfaces/kits/napi/native_node_api.h
index 22aa47df361c2db5d6c64d385085e7f2d61ec43b..00c6868e95d647d28db40f22ab4374127dd19782 100644
--- a/interfaces/kits/napi/native_node_api.h
+++ b/interfaces/kits/napi/native_node_api.h
@@ -18,7 +18,7 @@
#include "native_common.h"
-#include
+#include "node_api.h"
DEPRECATED napi_status napi_async_init(napi_env env,
napi_value async_resource,
diff --git a/module_manager/native_module_manager.cpp b/module_manager/native_module_manager.cpp
index b7317d1638c5f927c5718bef0cdab44fc75ae8b3..9d64de4ca76a77a977d6ccdb549226ec04331d41 100644
--- a/module_manager/native_module_manager.cpp
+++ b/module_manager/native_module_manager.cpp
@@ -23,12 +23,17 @@
#include
#include
+namespace {
+constexpr static int32_t NATIVE_PATH_NUMBER = 2;
+} // namespace
+
NativeModuleManager NativeModuleManager::instance_;
NativeModuleManager::NativeModuleManager()
{
firstNativeModule_ = nullptr;
lastNativeModule_ = nullptr;
+ appLibPath_ = nullptr;
pthread_mutex_init(&mutex_, nullptr);
}
@@ -42,6 +47,9 @@ NativeModuleManager::~NativeModuleManager()
firstNativeModule_ = nativeModule;
}
firstNativeModule_ = lastNativeModule_ = nullptr;
+ if (appLibPath_) {
+ delete[] appLibPath_;
+ }
pthread_mutex_destroy(&mutex_);
}
@@ -60,9 +68,18 @@ void NativeModuleManager::Register(NativeModule* nativeModule)
if (firstNativeModule_ == lastNativeModule_ && lastNativeModule_ == nullptr) {
firstNativeModule_ = new NativeModule();
+ if (firstNativeModule_ == nullptr) {
+ HILOG_ERROR("first NativeModule create failed");
+ return;
+ }
lastNativeModule_ = firstNativeModule_;
} else {
- lastNativeModule_->next = new NativeModule();
+ auto next = new NativeModule();
+ if (next == nullptr) {
+ HILOG_ERROR("next NativeModule create failed");
+ return;
+ }
+ lastNativeModule_->next = next;
lastNativeModule_ = lastNativeModule_->next;
}
@@ -74,8 +91,27 @@ void NativeModuleManager::Register(NativeModule* nativeModule)
lastNativeModule_->next = nullptr;
}
-NativeModule* NativeModuleManager::LoadNativeModule(const char* moduleName, const char* path, bool internal)
+void NativeModuleManager::SetAppLibPath(const char* appLibPath)
+{
+ char* tmp = new char[PATH_MAX];
+ errno_t err = EOK;
+ err = memset_s(tmp, PATH_MAX, 0, PATH_MAX);
+ if (err != EOK) {
+ delete[] tmp;
+ return;
+ }
+ err = strcpy_s(tmp, PATH_MAX, appLibPath);
+ if (err != EOK) {
+ delete[] tmp;
+ return;
+ }
+ appLibPath_ = tmp;
+}
+
+NativeModule* NativeModuleManager::LoadNativeModule(const char* moduleName,
+ const char* path, bool isAppModule, bool internal, bool isArk)
{
+ HILOG_ERROR("Kee LoadNativeModule");
if (moduleName == nullptr) {
HILOG_ERROR("moduleName value is null");
return nullptr;
@@ -89,7 +125,7 @@ NativeModule* NativeModuleManager::LoadNativeModule(const char* moduleName, cons
NativeModule* nativeModule = FindNativeModuleByCache(moduleName);
if (nativeModule == nullptr) {
HILOG_INFO("not in cache: moduleName: %{public}s", moduleName);
- nativeModule = FindNativeModuleByDisk(moduleName, internal, path);
+ nativeModule = FindNativeModuleByDisk(moduleName, internal, isAppModule, isArk);
}
if (pthread_mutex_unlock(&mutex_) != 0) {
@@ -101,7 +137,7 @@ NativeModule* NativeModuleManager::LoadNativeModule(const char* moduleName, cons
}
bool NativeModuleManager::GetNativeModulePath(
- const char* moduleName, const char* path, char* nativeModulePath, int32_t pathLength) const
+ const char* moduleName, const bool isAppModule, char nativeModulePath[][PATH_MAX], int32_t pathLength) const
{
const char* soPostfix = ".so";
#ifdef _ARM64_
@@ -110,8 +146,8 @@ bool NativeModuleManager::GetNativeModulePath(
const char* sysPrefix = "/system/lib/module";
#endif
const char* prefix = nullptr;
- if (path) {
- prefix = path;
+ if (isAppModule && appLibPath_) {
+ prefix = appLibPath_;
} else {
prefix = sysPrefix;
}
@@ -129,7 +165,7 @@ bool NativeModuleManager::GetNativeModulePath(
int32_t lengthOfPostfix = strlen(soPostfix);
if ((lengthOfModuleName > lengthOfPostfix) &&
(strcmp(dupModuleName + lengthOfModuleName - lengthOfPostfix, soPostfix) == 0)) {
- if (sprintf_s(nativeModulePath, pathLength, "%s/%s", prefix, dupModuleName) == -1) {
+ if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s", prefix, dupModuleName) == -1) {
return false;
}
return true;
@@ -137,8 +173,17 @@ bool NativeModuleManager::GetNativeModulePath(
char* lastDot = strrchr(dupModuleName, '.');
if (lastDot == nullptr) {
- if (sprintf_s(nativeModulePath, pathLength, "%s/lib%s.z.so", prefix, dupModuleName) == -1) {
- return false;
+ if (strcmp(prefix, sysPrefix) == 0) {
+ if (sprintf_s(nativeModulePath[0], pathLength, "%s/lib%s.z.so", prefix, dupModuleName) == -1) {
+ return false;
+ }
+ if (sprintf_s(nativeModulePath[1], pathLength, "%s/lib%s_napi.z.so", prefix, dupModuleName) == -1) {
+ return false;
+ }
+ } else {
+ if (sprintf_s(nativeModulePath[0], pathLength, "%s/lib%s.so", prefix, dupModuleName) == -1) {
+ return false;
+ }
}
} else {
char* afterDot = lastDot + 1;
@@ -152,36 +197,71 @@ bool NativeModuleManager::GetNativeModulePath(
*(dupModuleName + i) = '/';
}
}
- if (sprintf_s(nativeModulePath, pathLength, "%s/%s/lib%s.z.so", prefix, dupModuleName, afterDot) == -1) {
- return false;
+ if (strcmp(prefix, sysPrefix) == 0) {
+ if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s/lib%s.z.so",
+ prefix, dupModuleName, afterDot) == -1) {
+ return false;
+ }
+ if (sprintf_s(nativeModulePath[1], pathLength, "%s/%s/lib%s_napi.z.so",
+ prefix, dupModuleName, afterDot) == -1) {
+ return false;
+ }
+ } else {
+ if (sprintf_s(nativeModulePath[0], pathLength, "%s/%s/lib%s.so", prefix, dupModuleName, afterDot) == -1) {
+ return false;
+ }
}
}
return true;
}
-using NAPIGetJSCode = void (*)(const char** buf, int* bufLen);
-NativeModule* NativeModuleManager::FindNativeModuleByDisk(const char* moduleName, bool internal, const char* path)
+void* NativeModuleManager::LoadLibrary(const char* path) const
{
- char nativeModulePath[PATH_MAX] = { 0 };
- if (!GetNativeModulePath(moduleName, path, nativeModulePath, sizeof(nativeModulePath))) {
- HILOG_ERROR("get module filed");
+ if (strlen(path) == 0) {
+ HILOG_ERROR("primary module path is empty");
return nullptr;
}
+ void* lib = dlopen(path, RTLD_LAZY);
+ if (lib == nullptr) {
+ HILOG_ERROR("dlopen failed: %{public}s", dlerror());
+ }
+ return lib;
+}
- HILOG_INFO("get module path: %{public}s", nativeModulePath);
- if (strlen(nativeModulePath) <= 0) {
+using NAPIGetJSCode = void (*)(const char** buf, int* bufLen);
+NativeModule* NativeModuleManager::FindNativeModuleByDisk(const char* moduleName, bool internal, const bool isAppModule,
+ bool isArk)
+{
+ char nativeModulePath[NATIVE_PATH_NUMBER][PATH_MAX] = { { 0 }, { 0 } };
+ if (!GetNativeModulePath(moduleName, isAppModule, nativeModulePath, PATH_MAX)) {
+ HILOG_ERROR("get module filed");
return nullptr;
}
- void* lib = dlopen(nativeModulePath, RTLD_LAZY);
+
+ // load primary module path first
+ char* loadPath = nativeModulePath[0];
+ HILOG_INFO("get primary module path: %{public}s", loadPath);
+ void* lib = LoadLibrary(loadPath);
if (lib == nullptr) {
- HILOG_ERROR("dlopen failed: %{public}s", dlerror());
- return nullptr;
+ loadPath = nativeModulePath[1];
+ HILOG_WARN("primary module path load failed, try to load secondary module path: %{public}s", loadPath);
+ lib = LoadLibrary(loadPath);
+ if (lib == nullptr) {
+ HILOG_ERROR("secondary module path load failed, load native module failed");
+ return nullptr;
+ }
}
if (!internal) {
char symbol[PATH_MAX] = { 0 };
- if (sprintf_s(symbol, sizeof(symbol), "NAPI_%s_GetJSCode", moduleName) == -1) {
- return nullptr;
+ if (!isArk) {
+ if (sprintf_s(symbol, sizeof(symbol), "NAPI_%s_GetJSCode", moduleName) == -1) {
+ return nullptr;
+ }
+ } else {
+ if (sprintf_s(symbol, sizeof(symbol), "NAPI_%s_GetABCCode", moduleName) == -1) {
+ return nullptr;
+ }
}
auto getJSCode = reinterpret_cast(dlsym(lib, symbol));
if (getJSCode != nullptr) {
@@ -194,7 +274,7 @@ NativeModule* NativeModuleManager::FindNativeModuleByDisk(const char* moduleName
lastNativeModule_->jsCodeLen = bufLen;
}
} else {
- HILOG_INFO("no %{public}s in %{public}s", symbol, nativeModulePath);
+ HILOG_INFO("ignore: no %{public}s in %{public}s", symbol, loadPath);
}
}
@@ -205,7 +285,7 @@ NativeModule* NativeModuleManager::FindNativeModuleByCache(const char* moduleNam
{
NativeModule* result = nullptr;
for (NativeModule* temp = firstNativeModule_; temp != nullptr; temp = temp->next) {
- if (!strcmp(temp->name, moduleName)) {
+ if (!strcasecmp(temp->name, moduleName)) {
result = temp;
break;
}
diff --git a/module_manager/native_module_manager.h b/module_manager/native_module_manager.h
index 3d24196fec059669886ed01a13e09c2e478c9a2e..2195e5f9ae2ff0e39e828fefaaca387701050399 100644
--- a/module_manager/native_module_manager.h
+++ b/module_manager/native_module_manager.h
@@ -19,6 +19,8 @@
#include
#include
+#define PATH_MAX 4096
+
class NativeValue;
class NativeEngine;
@@ -42,19 +44,23 @@ public:
static unsigned long Release();
void Register(NativeModule* nativeModule);
- NativeModule* LoadNativeModule(const char* moduleName, const char* path, bool internal = false);
+ void SetAppLibPath(const char* appLibPath);
+ NativeModule* LoadNativeModule(const char* moduleName, const char* path, bool isAppModule, bool internal = false,
+ bool isArk = false);
private:
NativeModuleManager();
virtual ~NativeModuleManager();
bool GetNativeModulePath(
- const char* moduleName, const char* path, char* nativeModulePath, int32_t pathLength) const;
- NativeModule* FindNativeModuleByDisk(const char* moduleName, bool internal, const char* path);
+ const char* moduleName, const bool isAppModule, char nativeModulePath[][PATH_MAX], int32_t pathLength) const;
+ NativeModule* FindNativeModuleByDisk(const char* moduleName, bool internal, const bool isAppModule, bool isArk);
NativeModule* FindNativeModuleByCache(const char* moduleName) const;
+ void* LoadLibrary(const char* path) const;
NativeModule* firstNativeModule_;
NativeModule* lastNativeModule_;
+ char* appLibPath_;
static NativeModuleManager instance_;
pthread_mutex_t mutex_;
diff --git a/native_engine/impl/ark/ark_headers.h b/native_engine/impl/ark/ark_headers.h
new file mode 100644
index 0000000000000000000000000000000000000000..eb2deb663d04c40fb1eda65cbe2b92e8a9fb9a61
--- /dev/null
+++ b/native_engine/impl/ark/ark_headers.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_ARK_HEADERS_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_ARK_HEADERS_H
+
+// #include "ecmascript/ecma_vm.h"
+#include "ecmascript/napi/include/jsnapi.h"
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_ARK_HEADERS_H */
+
diff --git a/native_engine/impl/ark/ark_native_deferred.cpp b/native_engine/impl/ark/ark_native_deferred.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..8674b4717300e62e73954c12938194301d5273bb
--- /dev/null
+++ b/native_engine/impl/ark/ark_native_deferred.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_engine.h"
+
+#include "ark_native_deferred.h"
+
+using panda::Global;
+using panda::Local;
+using panda::JSValueRef;
+ArkNativeDeferred::ArkNativeDeferred(ArkNativeEngine* engine, Local deferred)
+ : engine_(engine), deferred_(engine->GetEcmaVm(), deferred)
+{
+}
+
+ArkNativeDeferred::~ArkNativeDeferred()
+{
+ // Addr of Global stored in ArkNativeDeferred should be released.
+ deferred_.FreeGlobalHandleAddr();
+}
+
+void ArkNativeDeferred::Resolve(NativeValue* data)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = *data;
+ deferred_->Resolve(vm, value.ToLocal(vm));
+}
+
+void ArkNativeDeferred::Reject(NativeValue* reason)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = *reason;
+ deferred_->Reject(vm, value.ToLocal(vm));
+}
\ No newline at end of file
diff --git a/native_engine/impl/ark/ark_native_deferred.h b/native_engine/impl/ark/ark_native_deferred.h
new file mode 100644
index 0000000000000000000000000000000000000000..f9f23e9fe38fe60347cc9455380948698016cad3
--- /dev/null
+++ b/native_engine/impl/ark/ark_native_deferred.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_DEFERRED_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_DEFERRED_H
+
+#include "ark_headers.h"
+
+#include "native_engine/native_deferred.h"
+
+using panda::PromiseCapabilityRef;
+class ArkNativeDeferred : public NativeDeferred {
+public:
+ ArkNativeDeferred(ArkNativeEngine* engine, Local deferred);
+ ~ArkNativeDeferred();
+ void Resolve(NativeValue* data) override;
+ void Reject(NativeValue* reason) override;
+
+private:
+ ArkNativeEngine* engine_;
+ panda::Global deferred_;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_DEFERRED_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/ark_native_engine.cpp b/native_engine/impl/ark/ark_native_engine.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..21c8801cd0cb435e05f9749eba23ed70362963bf
--- /dev/null
+++ b/native_engine/impl/ark/ark_native_engine.cpp
@@ -0,0 +1,574 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_engine.h"
+
+#include "ark_native_deferred.h"
+#include "ark_native_reference.h"
+
+#include "native_engine/native_property.h"
+
+#include "native_value/ark_native_array.h"
+#include "native_value/ark_native_array_buffer.h"
+#include "native_value/ark_native_boolean.h"
+#include "native_value/ark_native_data_view.h"
+#include "native_value/ark_native_external.h"
+#include "native_value/ark_native_function.h"
+#include "native_value/ark_native_number.h"
+#include "native_value/ark_native_object.h"
+#include "native_value/ark_native_string.h"
+#include "native_value/ark_native_typed_array.h"
+
+#include "securec.h"
+#include "utils/log.h"
+
+using panda::ObjectRef;
+using panda::StringRef;
+using panda::FunctionRef;
+using panda::PrimitiveRef;
+using panda::JSValueRef;
+using panda::ArrayBufferRef;
+using panda::TypedArrayRef;
+using panda::PromiseCapabilityRef;
+using panda::NativePointerRef;
+using panda::SymbolRef;
+
+static constexpr auto PANDA_MAIN_FUNCTION = "_GLOBAL::func_main_0";
+
+ArkNativeEngine::ArkNativeEngine(EcmaVM* vm, void* jsEngine) : NativeEngine(jsEngine), vm_(vm), topScope_(vm)
+{
+ Local requireName = StringRef::NewFromUtf8(vm, "requireNapi");
+ Local requireInternalName = StringRef::NewFromUtf8(vm, "requireInternal");
+ void* requireData = static_cast(this);
+
+ Local requireNapi =
+ FunctionRef::New(
+ vm,
+ [](EcmaVM *ecmaVm, Local thisObj,
+ const Local argv[], // NOLINTNEXTLINE(modernize-avoid-c-arrays)
+ int32_t length, void *data) -> Local {
+ panda::EscapeLocalScope scope(ecmaVm);
+ NativeModuleManager* moduleManager = NativeModuleManager::GetInstance();
+ ArkNativeEngine* engine = static_cast(data);
+ Local moduleName(argv[0]);
+ NativeModule* module =
+ moduleManager->LoadNativeModule(moduleName->ToString().c_str(), nullptr, false, false, true);
+ Global exports(ecmaVm, JSValueRef::Undefined(ecmaVm));
+ if (module != nullptr) {
+ if (module->jsCode != nullptr) {
+ HILOG_INFO("load js code");
+ NativeValue* script = engine->CreateString(module->jsCode, module->jsCodeLen);
+ char fileName[PATH_MAX] = { 0 };
+ const char* name = module->name;
+ if (sprintf_s(fileName, sizeof(fileName), "lib%s.z.so/%s.js", name, name) == -1) {
+ HILOG_ERROR("sprintf_s file name failed");
+ return scope.Escape(exports.ToLocal(ecmaVm));
+ }
+ HILOG_DEBUG("load js code from %{public}s", fileName);
+ NativeValue* exportObject = engine->LoadModule(script, fileName);
+ if (exportObject == nullptr) {
+ HILOG_ERROR("load module failed");
+ return scope.Escape(exports.ToLocal(ecmaVm));
+ } else {
+ exports = *exportObject;
+ }
+ } else if (module->registerCallback != nullptr) {
+ NativeValue* exportObject = engine->CreateObject();
+ module->registerCallback(engine, exportObject);
+ exports = *exportObject;
+ } else {
+ HILOG_ERROR("init module failed");
+ return scope.Escape(exports.ToLocal(ecmaVm));
+ }
+ }
+ return scope.Escape(exports.ToLocal(ecmaVm));
+ },
+ requireData);
+
+ Local requireInternal =
+ FunctionRef::New(
+ vm,
+ [](EcmaVM *ecmaVm, Local thisObj,
+ const Local argv[], // NOLINTNEXTLINE(modernize-avoid-c-arrays)
+ int32_t length, void *data) -> Local {
+ panda::EscapeLocalScope scope(ecmaVm);
+ NativeModuleManager* moduleManager = NativeModuleManager::GetInstance();
+ ArkNativeEngine* engine = static_cast(data);
+ Local moduleName(argv[0]);
+ NativeModule* module = moduleManager->LoadNativeModule(moduleName->ToString().c_str(), nullptr, false);
+ Global exports;
+ if (module != nullptr) {
+ NativeValue* exportObject = engine->CreateObject();
+ module->registerCallback(engine, exportObject);
+ exports = *exportObject;
+ }
+ return scope.Escape(exports.ToLocal(ecmaVm));
+ },
+ requireData);
+
+ Local global = panda::JSNApi::GetGlobalObject(vm);
+ global->Set(vm, requireName, requireNapi);
+ global->Set(vm, requireInternalName, requireInternal);
+}
+
+ArkNativeEngine::~ArkNativeEngine() {}
+
+void ArkNativeEngine::Loop(LoopMode mode, bool needSync)
+{
+ LocalScope scope(vm_);
+ NativeEngine::Loop(mode, needSync);
+ // panda::JSNApi::ExecutePendingJob(vm_);
+}
+
+NativeValue* ArkNativeEngine::GetGlobal()
+{
+ Local value = panda::JSNApi::GetGlobalObject(vm_);
+ return ArkValueToNativeValue(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateNull()
+{
+ Local value = JSValueRef::Null(vm_);
+ return new ArkNativeValue(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateUndefined()
+{
+ Local value = JSValueRef::Undefined(vm_);
+ return new ArkNativeValue(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateBoolean(bool value)
+{
+ return new ArkNativeBoolean(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateNumber(int32_t value)
+{
+ return new ArkNativeNumber(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateNumber(uint32_t value)
+{
+ return new ArkNativeNumber(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateNumber(int64_t value)
+{
+ return new ArkNativeNumber(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateNumber(double value)
+{
+ return new ArkNativeNumber(this, value);
+}
+
+NativeValue* ArkNativeEngine::CreateString(const char* value, size_t length)
+{
+ return new ArkNativeString(this, value, length);
+}
+
+NativeValue* ArkNativeEngine::CreateSymbol(NativeValue* value)
+{
+ LocalScope scope(vm_);
+ Global str = *value;
+ Local symbol = SymbolRef::New(vm_, str.ToLocal(vm_));
+ return new ArkNativeValue(this, symbol);
+}
+
+NativeValue* ArkNativeEngine::CreateExternal(void* value, NativeFinalize callback, void* hint)
+{
+ return new ArkNativeExternal(this, value, callback, hint);
+}
+
+NativeValue* ArkNativeEngine::CreateObject()
+{
+ return new ArkNativeObject(this);
+}
+
+NativeValue* ArkNativeEngine::CreateFunction(const char* name, size_t length, NativeCallback cb, void* value)
+{
+ return new ArkNativeFunction(this, name, length, cb, value);
+}
+
+NativeValue* ArkNativeEngine::CreateArray(size_t length)
+{
+ return new ArkNativeArray(this, length);
+}
+
+NativeValue* ArkNativeEngine::CreateArrayBuffer(void** value, size_t length)
+{
+ return new ArkNativeArrayBuffer(this, (uint8_t**)value, length);
+}
+
+NativeValue* ArkNativeEngine::CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint)
+{
+ return new ArkNativeArrayBuffer(this, (uint8_t*)value, length, cb, hint);
+}
+
+NativeValue* ArkNativeEngine::CreateTypedArray(NativeTypedArrayType type,
+ NativeValue* value,
+ size_t length,
+ size_t offset)
+{
+ LocalScope scope(vm_);
+ Global globalBuffer = *value;
+ Local buffer = globalBuffer.ToLocal(vm_);
+ Local typedArray(JSValueRef::Undefined(vm_));
+
+ switch (type) {
+ case NATIVE_INT8_ARRAY:
+ typedArray = panda::Int8ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_UINT8_ARRAY:
+ typedArray = panda::Uint8ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_UINT8_CLAMPED_ARRAY:
+ typedArray = panda::Uint8ClampedArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_INT16_ARRAY:
+ typedArray = panda::Int16ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_UINT16_ARRAY:
+ typedArray = panda::Uint16ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_INT32_ARRAY:
+ typedArray = panda::Int32ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_UINT32_ARRAY:
+ typedArray = panda::Uint32ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_FLOAT32_ARRAY:
+ typedArray = panda::Float32ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_FLOAT64_ARRAY:
+ typedArray = panda::Float64ArrayRef::New(vm_, buffer, offset, length);
+ break;
+ case NATIVE_BIGINT64_ARRAY:
+ // not support yet
+ return nullptr;
+ case NATIVE_BIGUINT64_ARRAY:
+ // not support yet
+ return nullptr;
+ default:
+ return nullptr;
+ }
+ return new ArkNativeTypedArray(this, typedArray);
+}
+
+NativeValue* ArkNativeEngine::CreateDataView(NativeValue* value, size_t length, size_t offset)
+{
+ return new ArkNativeDataView(this, value, length, offset);
+}
+
+NativeValue* ArkNativeEngine::CreatePromise(NativeDeferred** deferred)
+{
+ LocalScope scope(vm_);
+ Local capability = PromiseCapabilityRef::New(vm_);
+ *deferred = new ArkNativeDeferred(this, capability);
+
+ return new ArkNativeValue(this, capability->GetPromise(vm_));
+}
+
+NativeValue* ArkNativeEngine::CreateError(NativeValue* code, NativeValue* message)
+{
+ LocalScope scope(vm_);
+ Local errorVal = panda::Exception::Error(vm_, *message);
+ if (code != nullptr) {
+ Local codeKey = StringRef::NewFromUtf8(vm_, "code");
+ Local errorObj(errorVal);
+ errorObj->Set(vm_, codeKey, *code);
+ }
+ return ArkValueToNativeValue(this, errorVal);
+}
+
+NativeValue* ArkNativeEngine::CallFunction(NativeValue* thisVar,
+ NativeValue* function,
+ NativeValue* const* argv,
+ size_t argc)
+{
+ if (function == nullptr) {
+ return nullptr;
+ }
+ LocalScope scope(vm_);
+ Global thisObj = (thisVar != nullptr) ? *thisVar : Global(vm_, JSValueRef::Undefined(vm_));
+ Global funcObj = *function;
+
+ std::vector> args;
+ args.reserve(argc);
+ for (size_t i = 0; i < argc; i++) {
+ if (argv[i] != nullptr) {
+ Global arg = *argv[i];
+ args.emplace_back(arg.ToLocal(vm_));
+ } else {
+ args.emplace_back(JSValueRef::Undefined(vm_));
+ }
+ }
+
+ Local value = funcObj->Call(vm_, thisObj.ToLocal(vm_), args.data(), argc);
+ Local excep = panda::JSNApi::GetUncaughtException(vm_);
+ if (!excep.IsNull()) {
+ Local exceptionMsg = excep->ToString(vm_);
+ exceptionStr_ = exceptionMsg->ToString();
+ }
+
+ return ArkValueToNativeValue(this, value);
+}
+
+NativeValue* ArkNativeEngine::RunScript(NativeValue* script)
+{
+ // not support yet
+ return nullptr;
+}
+
+NativeValue* ArkNativeEngine::DefineClass(const char* name,
+ NativeCallback callback,
+ void* data,
+ const NativePropertyDescriptor* properties,
+ size_t length)
+{
+ LocalScope scope(vm_);
+ auto classConstructor = new ArkNativeFunction(this, name, callback, data);
+ auto classPrototype = classConstructor->GetFunctionPrototype();
+
+ for (size_t i = 0; i < length; i++) {
+ if (properties[i].attributes & NATIVE_STATIC) {
+ classConstructor->DefineProperty(properties[i]);
+ } else {
+ if (classPrototype == nullptr) {
+ HILOG_ERROR("ArkNativeEngine::Class's prototype is null");
+ continue;
+ }
+ static_cast(classPrototype)->DefineProperty(properties[i]);
+ }
+ }
+
+ return classConstructor;
+}
+
+NativeValue* ArkNativeEngine::CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc)
+{
+ if (constructor == nullptr) {
+ return nullptr;
+ }
+ LocalScope scope(vm_);
+ Global value = *constructor;
+
+ std::vector> args;
+ args.reserve(argc);
+ for (size_t i = 0; i < argc; i++) {
+ if (argv[i] != nullptr) {
+ Global arg = *argv[i];
+ args.emplace_back(arg.ToLocal(vm_));
+ } else {
+ args.emplace_back(JSValueRef::Undefined(vm_));
+ }
+ }
+
+ Local instance = value->Constructor(vm_, args.data(), argc);
+
+ return ArkValueToNativeValue(this, instance);
+}
+
+NativeReference* ArkNativeEngine::CreateReference(NativeValue* value, uint32_t initialRefcount)
+{
+ return new ArkNativeReference(this, value, initialRefcount);
+}
+
+bool ArkNativeEngine::Throw(NativeValue* error)
+{
+ LocalScope scope(vm_);
+ Global errorVal = *error;
+ panda::JSNApi::ThrowException(vm_, errorVal.ToLocal(vm_));
+ lastException_ = error;
+ return true;
+}
+
+bool ArkNativeEngine::Throw(NativeErrorType type, const char* code, const char* message)
+{
+ LocalScope scope(vm_);
+ Local error(JSValueRef::Undefined(vm_));
+ switch (type) {
+ case NATIVE_COMMON_ERROR:
+ error = panda::Exception::Error(vm_, StringRef::NewFromUtf8(vm_, message));
+ break;
+ case NATIVE_TYPE_ERROR:
+ error = panda::Exception::TypeError(vm_, StringRef::NewFromUtf8(vm_, message));
+ break;
+ case NATIVE_RANGE_ERROR:
+ error = panda::Exception::RangeError(vm_, StringRef::NewFromUtf8(vm_, message));
+ break;
+ default:
+ return false;
+ }
+ if (code != nullptr) {
+ Local codeKey = StringRef::NewFromUtf8(vm_, "code");
+ Local codeValue = StringRef::NewFromUtf8(vm_, code);
+ Local errorObj(error);
+ errorObj->Set(vm_, codeKey, codeValue);
+ }
+
+ panda::JSNApi::ThrowException(vm_, error);
+ lastException_ = ArkValueToNativeValue(this, error);
+ return true;
+}
+
+void* ArkNativeEngine::CreateRuntime()
+{
+ panda::RuntimeOption option;
+ option.SetGcType(panda::RuntimeOption::GC_TYPE::GEN_GC);
+ const int64_t poolSize = 0x1000000;
+ option.SetGcPoolSize(poolSize);
+ // option.SetPandaStdFile("pandastdlib/arkstdlib.abc");
+ option.SetLogLevel(panda::RuntimeOption::LOG_LEVEL::ERROR);
+ option.SetDebuggerLibraryPath("");
+ EcmaVM* vm = panda::JSNApi::CreateJSVM(option);
+ if (vm == nullptr) {
+ return nullptr;
+ }
+
+ ArkNativeEngine* arkEngine = new ArkNativeEngine(vm, this);
+ return reinterpret_cast(arkEngine);
+}
+
+NativeValue* ArkNativeEngine::Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer)
+{
+ const panda::ecmascript::EcmaVM* vm = reinterpret_cast(context)->GetEcmaVm();
+ Local arkValue = *value;
+ Local arkTransfer = *transfer;
+ void* result = panda::JSNApi::SerializeValue(vm, arkValue, arkTransfer);
+ return reinterpret_cast(result);
+}
+
+NativeValue* ArkNativeEngine::Deserialize(NativeEngine* context, NativeValue* recorder)
+{
+ const panda::ecmascript::EcmaVM* vm = reinterpret_cast(context)->GetEcmaVm();
+ Local result = panda::JSNApi::DeserializeValue(vm, recorder);
+ return ArkValueToNativeValue(this, result);
+}
+
+ExceptionInfo* ArkNativeEngine::GetExceptionForWorker() const
+{
+ if (exceptionStr_.empty()) {
+ HILOG_ERROR("worker:: exception is null");
+ return nullptr;
+ }
+
+ ExceptionInfo* exceptionInfo = new ExceptionInfo();
+ int msgLength = exceptionStr_.length();
+ char* exceptionMessage = new char[msgLength + 1] { 0 };
+ if (memcpy_s(exceptionMessage, msgLength, exceptionStr_.c_str(), msgLength) != EOK) {
+ HILOG_ERROR("worker:: memcpy_s error");
+ delete exceptionInfo;
+ delete[] exceptionMessage;
+ return nullptr;
+ }
+ exceptionInfo->message_ = exceptionMessage;
+ // need add colno, lineno when ark exception support
+ return exceptionInfo;
+}
+
+void ArkNativeEngine::DeleteSerializationData(NativeValue* value) const
+{
+ void* data = reinterpret_cast(value);
+ panda::JSNApi::DeleteSerializationData(data);
+}
+
+NativeValue* ArkNativeEngine::RunBufferScript(std::vector& buffer)
+{
+ Local entryPoint = StringRef::NewFromUtf8(vm_, PANDA_MAIN_FUNCTION);
+ // panda::JSNApi::EnableUserUncaughtErrorHandler(vm_);
+ panda::JSExecutionScope executionScope(vm_);
+ bool ret = panda::JSNApi::Execute(vm_, buffer.data(), buffer.size(), entryPoint);
+
+ Local excep = panda::JSNApi::GetUncaughtException(vm_);
+ if (!excep.IsNull()) {
+ Local exceptionMsg = excep->ToString(vm_);
+ exceptionStr_ = exceptionMsg->ToString();
+ }
+
+ if (!ret) {
+ return nullptr;
+ }
+ return CreateUndefined();
+}
+
+NativeValue* ArkNativeEngine::LoadModule(NativeValue* str, const std::string& fileName)
+{
+ HILOG_DEBUG("ArkNativeEngine::LoadModule start");
+ if (str == nullptr || fileName.empty()) {
+ HILOG_ERROR("fileName is nullptr or source code is nullptr");
+ return nullptr;
+ }
+
+ Local source = *str;
+ int32_t len = source->Length();
+ char* buffer = new char[len];
+ source->WriteUtf8(buffer, len);
+ HILOG_DEBUG("Ark::LoadModule buffer = %{public}s", buffer);
+
+ bool res = JSNApi::ExecuteModuleFromBuffer(vm_, buffer, len, fileName);
+ if (!res) {
+ HILOG_ERROR("Execute module failed");
+ delete[] buffer;
+ return nullptr;
+ }
+
+ Local exportObj = JSNApi::GetExportObject(vm_, fileName, "default");
+ if (exportObj->IsNull()) {
+ HILOG_ERROR("Get export object failed");
+ delete[] buffer;
+ return nullptr;
+ }
+
+ delete[] buffer;
+ HILOG_DEBUG("ArkNativeEngine::LoadModule end");
+ return ArkValueToNativeValue(this, exportObj);
+}
+
+NativeValue* ArkNativeEngine::ArkValueToNativeValue(ArkNativeEngine* engine, Local value)
+{
+ NativeValue* result = nullptr;
+ if (value->IsNull() || value->IsUndefined() || value->IsSymbol() || value->IsPromise()) {
+ result = new ArkNativeValue(engine, value);
+ } else if (value->IsNumber()) {
+ result = new ArkNativeNumber(engine, value);
+ } else if (value->IsString()) {
+ result = new ArkNativeString(engine, value);
+ } else if (value->IsArray(engine->GetEcmaVm())) {
+ result = new ArkNativeArray(engine, value);
+ } else if (value->IsFunction()) {
+ result = new ArkNativeFunction(engine, value);
+ } else if (value->IsArrayBuffer()) {
+ result = new ArkNativeArrayBuffer(engine, value);
+ } else if (value->IsDataView()) {
+ result = new ArkNativeDataView(engine, value);
+ } else if (value->IsTypedArray()) {
+ result = new ArkNativeTypedArray(engine, value);
+ } else if (value->IsNativeObject() || value->IsNativePointer()) {
+ result = new ArkNativeExternal(engine, value);
+ } else if (value->IsObject()) {
+ result = new ArkNativeObject(engine, value);
+ } else if (value->IsBoolean()) {
+ result = new ArkNativeBoolean(engine, value);
+ }
+ return result;
+}
+
+NativeValue* ArkNativeEngine::ValueToNativeValue(JSValueWrapper& value)
+{
+ Global arkValue = value;
+ return ArkValueToNativeValue(this, arkValue.ToLocal(vm_));
+}
diff --git a/native_engine/impl/ark/ark_native_engine.h b/native_engine/impl/ark/ark_native_engine.h
new file mode 100644
index 0000000000000000000000000000000000000000..0efbb561a1adfa0a654064377af77290da68ac83
--- /dev/null
+++ b/native_engine/impl/ark/ark_native_engine.h
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H
+
+#include "ark_headers.h"
+#include "ecmascript/napi/include/jsnapi.h"
+#include "native_engine/native_engine.h"
+
+using panda::ecmascript::EcmaVM;
+using panda::Local;
+using panda::LocalScope;
+using panda::JSValueRef;
+using panda::JSNApi;
+
+class SerializationData {
+public:
+ SerializationData() : data_(nullptr), size_(0) {}
+ ~SerializationData() = default;
+
+ uint8_t* GetData() const
+ {
+ return data_.get();
+ }
+ size_t GetSize() const
+ {
+ return size_;
+ }
+
+private:
+ struct DataDeleter {
+ void operator()(uint8_t* p) const
+ {
+ free(p);
+ }
+ };
+
+ std::unique_ptr data_;
+ size_t size_;
+};
+
+class ArkNativeEngine : public NativeEngine {
+public:
+ // ArkNativeEngine constructor
+ ArkNativeEngine(EcmaVM* vm, void* jsEngine);
+ // ArkNativeEngine destructor
+ ~ArkNativeEngine() override;
+
+ const EcmaVM* GetEcmaVm() const
+ {
+ return vm_;
+ }
+
+ void Loop(LoopMode mode, bool needSync = false) override;
+
+ // Get global native object value
+ NativeValue* GetGlobal() override;
+ // Create native null value
+ NativeValue* CreateNull() override;
+ // Create native undefined value
+ NativeValue* CreateUndefined() override;
+ // Create native boolean value
+ NativeValue* CreateBoolean(bool value) override;
+ // Create number value by int32_t
+ NativeValue* CreateNumber(int32_t value) override;
+ // Create number value by uint32_t
+ NativeValue* CreateNumber(uint32_t value) override;
+ // Create native number value by int64_t
+ NativeValue* CreateNumber(int64_t value) override;
+ // Create native number value by double
+ NativeValue* CreateNumber(double value) override;
+ // Create native string value by const char pointer
+ NativeValue* CreateString(const char* value, size_t length) override;
+ // Create native symbol value
+ NativeValue* CreateSymbol(NativeValue* value) override;
+ // Create native value of external pointer
+ NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint) override;
+ // Create native object value
+ NativeValue* CreateObject() override;
+ // Create native function value
+ NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override;
+ // Create native array value
+ NativeValue* CreateArray(size_t length) override;
+ // Create native array buffer value
+ NativeValue* CreateArrayBuffer(void** value, size_t length) override;
+ // Create native array buffer value of external
+ NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override;
+ // Create native typed array value
+ NativeValue* CreateTypedArray(NativeTypedArrayType type,
+ NativeValue* value,
+ size_t length,
+ size_t offset) override;
+ // Create native data view value
+ NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override;
+ // Create native promise value
+ NativeValue* CreatePromise(NativeDeferred** deferred) override;
+ // Create native error value
+ NativeValue* CreateError(NativeValue* code, NativeValue* message) override;
+ // Call function
+ NativeValue* CallFunction(NativeValue* thisVar,
+ NativeValue* function,
+ NativeValue* const* argv,
+ size_t argc) override;
+ // Run script
+ NativeValue* RunScript(NativeValue* script) override;
+ // Run buffer script
+ NativeValue* RunBufferScript(std::vector& buffer) override;
+ // Define native class
+ NativeValue* DefineClass(const char* name,
+ NativeCallback callback,
+ void* data,
+ const NativePropertyDescriptor* properties,
+ size_t length) override;
+ // Create instance by defined class
+ NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override;
+
+ // Create native reference
+ NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount) override;
+ // Throw exception
+ bool Throw(NativeValue* error) override;
+ // Throw exception
+ bool Throw(NativeErrorType type, const char* code, const char* message) override;
+
+ void* CreateRuntime() override;
+ NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override;
+ NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override;
+ void DeleteSerializationData(NativeValue* value) const override;
+ ExceptionInfo* GetExceptionForWorker() const override;
+ NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override;
+
+ static NativeValue* ArkValueToNativeValue(ArkNativeEngine* engine, Local value);
+
+ NativeValue* ValueToNativeValue(JSValueWrapper& value) override;
+
+private:
+ EcmaVM* vm_ = nullptr;
+ std::string exceptionStr_;
+ panda::LocalScope topScope_;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H */
diff --git a/native_engine/impl/ark/ark_native_reference.cpp b/native_engine/impl/ark/ark_native_reference.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a9cec0d904ca77c60f023047b268296edbac0ec0
--- /dev/null
+++ b/native_engine/impl/ark/ark_native_reference.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_engine.h"
+
+#include "ark_native_reference.h"
+
+#include "utils/log.h"
+
+ArkNativeReference::ArkNativeReference(ArkNativeEngine* engine, NativeValue* value, uint32_t initialRefcount)
+ : engine_(engine),
+ value_(Global(engine->GetEcmaVm(), JSValueRef::Undefined(engine->GetEcmaVm()))),
+ refCount_(initialRefcount)
+{
+ ASSERT(initialRefcount != 0);
+ Global oldValue = *value;
+ auto vm = engine->GetEcmaVm();
+ Global newValue(vm, oldValue.ToLocal(vm));
+ value_ = newValue;
+}
+
+ArkNativeReference::~ArkNativeReference()
+{
+ if (refCount_ != 0) {
+ refCount_ = 0;
+ // Addr of Global stored in ArkNativeReference should be released.
+ value_.FreeGlobalHandleAddr();
+ }
+}
+
+uint32_t ArkNativeReference::Ref()
+{
+ if (refCount_ != 0) {
+ ++refCount_;
+ }
+ return refCount_;
+}
+
+uint32_t ArkNativeReference::Unref()
+{
+ if (refCount_ == 1) {
+ refCount_ = 0;
+ value_.FreeGlobalHandleAddr();
+ } else if (refCount_ > 0) {
+ --refCount_;
+ }
+ return refCount_;
+}
+
+NativeValue* ArkNativeReference::Get()
+{
+ if (refCount_ == 0) {
+ return nullptr;
+ }
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Local value = value_.ToLocal(vm);
+ return ArkNativeEngine::ArkValueToNativeValue(engine_, value);
+}
+
+ArkNativeReference::operator NativeValue*()
+{
+ return Get();
+}
\ No newline at end of file
diff --git a/native_engine/impl/ark/ark_native_reference.h b/native_engine/impl/ark/ark_native_reference.h
new file mode 100644
index 0000000000000000000000000000000000000000..91d34a356e6b5925c0cdb21e7d9f756057ba0132
--- /dev/null
+++ b/native_engine/impl/ark/ark_native_reference.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H
+
+#include "native_engine/native_value.h"
+
+#include "native_engine/native_reference.h"
+
+class ArkNativeEngine;
+
+using panda::Global;
+using panda::JSValueRef;
+using panda::Local;
+
+class ArkNativeReference : public NativeReference {
+public:
+ ArkNativeReference(ArkNativeEngine* engine, NativeValue* value, uint32_t initialRefcount);
+ ~ArkNativeReference() override;
+
+ uint32_t Ref() override;
+ uint32_t Unref() override;
+ NativeValue* Get() override;
+ operator NativeValue*() override;
+
+private:
+ ArkNativeEngine* engine_;
+ Global value_;
+ uint32_t refCount_;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_array.cpp b/native_engine/impl/ark/native_value/ark_native_array.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..5123971b2e7c54c374150b1d6aa5a1f69caf2c8f
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_array.cpp
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_array.h"
+
+using panda::ObjectRef;
+using panda::ArrayRef;
+ArkNativeArray::ArkNativeArray(ArkNativeEngine* engine, Local value) : ArkNativeObject(engine, value) {}
+
+ArkNativeArray::ArkNativeArray(ArkNativeEngine* engine, uint32_t length)
+ : ArkNativeArray(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = ArrayRef::New(vm, length);
+ value_ = Global(vm, object);
+}
+
+ArkNativeArray::~ArkNativeArray() {}
+
+void* ArkNativeArray::GetInterface(int interfaceId)
+{
+ return (NativeArray::INTERFACE_ID == interfaceId) ? (NativeArray*)this
+ : ArkNativeObject::GetInterface(interfaceId);
+}
+
+bool ArkNativeArray::SetElement(uint32_t index, NativeValue* value)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ Global val = *value;
+ return obj->Set(vm, index, val.ToLocal(vm));
+}
+
+NativeValue* ArkNativeArray::GetElement(uint32_t index)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ auto val = obj->Get(vm, index);
+ return ArkNativeEngine::ArkValueToNativeValue(engine_, val);
+}
+
+bool ArkNativeArray::HasElement(uint32_t index)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ return obj->Has(vm, index);
+}
+
+bool ArkNativeArray::DeleteElement(uint32_t index)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ return obj->Delete(vm, index);
+}
+
+uint32_t ArkNativeArray::GetLength()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ return obj->Length(vm);
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_array.h b/native_engine/impl/ark/native_value/ark_native_array.h
new file mode 100644
index 0000000000000000000000000000000000000000..e0783dc67e647e055f045df9d56a6fc8f4c50ca0
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_array.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_ARRAY_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_ARRAY_H
+
+#include "ark_native_object.h"
+
+class ArkNativeArray : public ArkNativeObject, public NativeArray {
+public:
+ explicit ArkNativeArray(ArkNativeEngine* engine, Local value);
+ explicit ArkNativeArray(ArkNativeEngine* engine, uint32_t length);
+ ~ArkNativeArray() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ bool SetElement(uint32_t index, NativeValue* value) override;
+ NativeValue* GetElement(uint32_t index) override;
+ bool HasElement(uint32_t index) override;
+ bool DeleteElement(uint32_t index) override;
+
+ uint32_t GetLength() override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_ARRAY_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_array_buffer.cpp b/native_engine/impl/ark/native_value/ark_native_array_buffer.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..621862f9ba0f39f8116d32b10608f9c8fcfb4c75
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_array_buffer.cpp
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_array_buffer.h"
+
+using panda::ArrayBufferRef;
+ArkNativeArrayBuffer::ArkNativeArrayBuffer(ArkNativeEngine* engine, Local value)
+ : ArkNativeObject(engine, value)
+{
+}
+
+ArkNativeArrayBuffer::ArkNativeArrayBuffer(ArkNativeEngine* engine, uint8_t** value, size_t length)
+ : ArkNativeArrayBuffer(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ value_ = Global(vm, ArrayBufferRef::New(vm, length));
+ if (value != nullptr) {
+ Global obj = value_;
+ *value = reinterpret_cast(obj->GetBuffer());
+ }
+}
+
+ArkNativeArrayBuffer::ArkNativeArrayBuffer(ArkNativeEngine* engine,
+ uint8_t* value,
+ size_t length,
+ NativeFinalize cb,
+ void* hint)
+ : ArkNativeArrayBuffer(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+
+ NativeObjectInfo* cbinfo = NativeObjectInfo::CreateNewInstance();
+ cbinfo->engine = engine_;
+ cbinfo->callback = cb;
+ cbinfo->hint = hint;
+
+ Local object = ArrayBufferRef::New(vm, value, length,
+ [](void* data, void* info) {
+ auto externalInfo = reinterpret_cast(info);
+ auto engine = externalInfo->engine;
+ auto callback = externalInfo->callback;
+ auto hint = externalInfo->hint;
+ callback(engine, data, hint);
+ delete externalInfo;
+ },
+ cbinfo);
+
+ value_ = Global(vm, object);
+}
+
+ArkNativeArrayBuffer::~ArkNativeArrayBuffer() {}
+
+void* ArkNativeArrayBuffer::GetInterface(int interfaceId)
+{
+ return (NativeArrayBuffer::INTERFACE_ID == interfaceId) ? (NativeArrayBuffer*)this
+ : ArkNativeObject::GetInterface(interfaceId);
+}
+
+void* ArkNativeArrayBuffer::GetBuffer()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global v = value_;
+ return v->GetBuffer();
+}
+
+size_t ArkNativeArrayBuffer::GetLength()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global v = value_;
+ return v->ByteLength(vm);
+}
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_array_buffer.h b/native_engine/impl/ark/native_value/ark_native_array_buffer.h
new file mode 100644
index 0000000000000000000000000000000000000000..c4c68b78a6f7ce94293ec786f8d9e998d418fe13
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_array_buffer.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_ARRAY_BUFFER_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_ARRAY_BUFFER_H
+
+#include "ark_native_object.h"
+
+class ArkNativeArrayBuffer : public ArkNativeObject, public NativeArrayBuffer {
+public:
+ ArkNativeArrayBuffer(ArkNativeEngine* engine, Local value);
+ ArkNativeArrayBuffer(ArkNativeEngine* engine, uint8_t** data, size_t length);
+ ArkNativeArrayBuffer(ArkNativeEngine* engine, uint8_t* data, size_t length, NativeFinalize cb, void* hint);
+ ~ArkNativeArrayBuffer() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ void* GetBuffer() override;
+ size_t GetLength() override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_ARRAY_BUFFER_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_boolean.cpp b/native_engine/impl/ark/native_value/ark_native_boolean.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..2ada298bc51b043b4d17c9eddec371c54b96731a
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_boolean.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_boolean.h"
+
+using panda::BooleanRef;
+ArkNativeBoolean::ArkNativeBoolean(ArkNativeEngine* engine, Local value) : ArkNativeValue(engine, value) {}
+
+ArkNativeBoolean::ArkNativeBoolean(ArkNativeEngine* engine, bool value)
+ : ArkNativeBoolean(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = BooleanRef::New(vm, value);
+ value_ = Global(vm, object);
+}
+
+ArkNativeBoolean::~ArkNativeBoolean() {}
+
+void* ArkNativeBoolean::GetInterface(int interfaceId)
+{
+ return (NativeBoolean::INTERFACE_ID == interfaceId) ? (NativeBoolean*)this : nullptr;
+}
+
+ArkNativeBoolean::operator bool()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ return value->Value();
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_boolean.h b/native_engine/impl/ark/native_value/ark_native_boolean.h
new file mode 100644
index 0000000000000000000000000000000000000000..a2596e1e645549eb334585538ec6822edfd31eb3
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_boolean.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_BOOLEAN_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_BOOLEAN_H
+
+#include "ark_native_value.h"
+
+class ArkNativeBoolean : public ArkNativeValue, public NativeBoolean {
+public:
+ ArkNativeBoolean(ArkNativeEngine* engine, Local value);
+ ArkNativeBoolean(ArkNativeEngine* engine, bool value);
+ ~ArkNativeBoolean() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ operator bool() override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_BOOLEAN_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_data_view.cpp b/native_engine/impl/ark/native_value/ark_native_data_view.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..f7e9f3c211cce22fbb32578c77d5566f8188fede
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_data_view.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_data_view.h"
+#include "ark_native_array_buffer.h"
+
+using panda::DataViewRef;
+using panda::ArrayBufferRef;
+ArkNativeDataView::ArkNativeDataView(ArkNativeEngine* engine, Local value) : ArkNativeObject(engine, value)
+{
+}
+
+ArkNativeDataView::ArkNativeDataView(ArkNativeEngine* engine, NativeValue* value, size_t length, size_t offset)
+ : ArkNativeDataView(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Global arraybuffer = *value;
+ Local dataView = DataViewRef::New(vm, arraybuffer.ToLocal(vm), offset, length);
+ value_ = Global(vm, dataView);
+}
+
+ArkNativeDataView::~ArkNativeDataView() {}
+
+void* ArkNativeDataView::GetInterface(int interfaceId)
+{
+ return (NativeDataView::INTERFACE_ID == interfaceId) ? (NativeDataView*)this
+ : ArkNativeObject::GetInterface(interfaceId);
+}
+
+void* ArkNativeDataView::GetBuffer()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return value->GetArrayBuffer(vm)->GetBuffer();
+}
+
+size_t ArkNativeDataView::GetLength()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return value->ByteLength();
+}
+
+NativeValue* ArkNativeDataView::GetArrayBuffer()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return new ArkNativeArrayBuffer(engine_, value->GetArrayBuffer(vm));
+}
+
+size_t ArkNativeDataView::GetOffset()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return value->ByteOffset();
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_data_view.h b/native_engine/impl/ark/native_value/ark_native_data_view.h
new file mode 100644
index 0000000000000000000000000000000000000000..7af9487b52e799ae46077a5e9101a831f0975551
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_data_view.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_DATA_VIEW_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_DATA_VIEW_H
+
+#include "ark_native_object.h"
+
+class ArkNativeDataView : public ArkNativeObject, public NativeDataView {
+public:
+ ArkNativeDataView(ArkNativeEngine* engine, Local value);
+ ArkNativeDataView(ArkNativeEngine* engine, NativeValue* value, size_t length, size_t offset);
+ ~ArkNativeDataView() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ void* GetBuffer() override;
+ size_t GetLength() override;
+ NativeValue* GetArrayBuffer() override;
+ size_t GetOffset() override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_DATA_VIEW_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_external.cpp b/native_engine/impl/ark/native_value/ark_native_external.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..c55511aac8da35066112b92e4b186842f22015dd
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_external.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_external.h"
+#include "ark_native_reference.h"
+
+using panda::NativePointerRef;
+
+ArkNativeExternal::ArkNativeExternal(ArkNativeEngine* engine, void* value, NativeFinalize callback, void* hint)
+ : ArkNativeExternal(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+
+ NativeObjectInfo* info = NativeObjectInfo::CreateNewInstance();
+ info->engine = engine;
+ info->nativeObject = nullptr;
+ info->callback = callback;
+ info->hint = hint;
+
+ Local object = NativePointerRef::New(vm, value, ArkExternalDeleterCallback, info);
+ value_ = Global(vm, object);
+}
+
+ArkNativeExternal::ArkNativeExternal(ArkNativeEngine* engine, Local value)
+ : ArkNativeValue(engine, value) {}
+
+ArkNativeExternal::~ArkNativeExternal()
+{
+}
+
+void* ArkNativeExternal::GetInterface(int interfaceId)
+{
+ return (NativeExternal::INTERFACE_ID == interfaceId) ? (NativeExternal*)this : nullptr;
+}
+
+ArkNativeExternal::operator void*()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ return value->Value();
+}
+
+// static
+void ArkNativeExternal::ArkExternalDeleterCallback(void* data, void* info)
+{
+ auto externalInfo = reinterpret_cast(info);
+ auto engine = externalInfo->engine;
+ auto callback = externalInfo->callback;
+ auto hint = externalInfo->hint;
+ callback(engine, data, hint);
+ delete externalInfo;
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_external.h b/native_engine/impl/ark/native_value/ark_native_external.h
new file mode 100644
index 0000000000000000000000000000000000000000..b7859f1334ac5878e4cfba7c9997bc15a491e4a1
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_external.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_EXTERNAL_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_EXTERNAL_H
+
+#include "ark_native_value.h"
+
+class ArkNativeExternal : public ArkNativeValue, public NativeExternal {
+public:
+ ArkNativeExternal(ArkNativeEngine* engine, void* value, NativeFinalize callback, void* hint);
+ ArkNativeExternal(ArkNativeEngine* engine, Local value);
+ ~ArkNativeExternal() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ operator void*() override;
+
+private:
+ static void ArkExternalDeleterCallback(void* data, void* info);
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_EXTERNAL_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_function.cpp b/native_engine/impl/ark/native_value/ark_native_function.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a44162cf88bd6ca247635794530a946f3b9f2771
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_function.cpp
@@ -0,0 +1,232 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_function.h"
+
+#include "utils/log.h"
+
+using panda::ArrayRef;
+using panda::NativePointerRef;
+using panda::FunctionRef;
+using panda::StringRef;
+ArkNativeFunction::ArkNativeFunction(ArkNativeEngine* engine, Local value) : ArkNativeObject(engine, value)
+{
+}
+
+// common function
+ArkNativeFunction::ArkNativeFunction(ArkNativeEngine* engine,
+ const char* name,
+ size_t length,
+ NativeCallback cb,
+ void* value)
+ : ArkNativeFunction(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = const_cast(engine->GetEcmaVm());
+ LocalScope scope(vm);
+
+ NativeFunctionInfo* funcInfo = NativeFunctionInfo::CreateNewInstance();
+ funcInfo->engine = engine;
+ funcInfo->callback = cb;
+ funcInfo->data = value;
+
+ Local fn = FunctionRef::New(vm, NativeFunctionCallBack,
+ [](void* data, void* hint) {
+ auto info = reinterpret_cast(data);
+ if (info != nullptr) {
+ delete info;
+ }
+ },
+ reinterpret_cast(funcInfo));
+ Local fnName = StringRef::NewFromUtf8(vm, name);
+ fn->SetName(vm, fnName);
+
+ Global globalFn(vm, fn);
+ value_ = globalFn;
+}
+
+
+// class function
+ArkNativeFunction::ArkNativeFunction(ArkNativeEngine* engine,
+ const char* name,
+ NativeCallback cb,
+ void* value)
+ : ArkNativeFunction(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = const_cast(engine->GetEcmaVm());
+ LocalScope scope(vm);
+
+ NativeFunctionInfo* funcInfo = NativeFunctionInfo::CreateNewInstance();
+ funcInfo->engine = engine;
+ funcInfo->callback = cb;
+ funcInfo->data = value;
+
+ Local fn = FunctionRef::NewClassFunction(vm, NativeClassFunctionCallBack,
+ [](void* data, void* hint) {
+ auto info = reinterpret_cast(data);
+ if (info != nullptr) {
+ delete info;
+ }
+ },
+ reinterpret_cast(funcInfo));
+ Local fnName = StringRef::NewFromUtf8(vm, name);
+ fn->SetName(vm, fnName);
+
+ Global globalFn(vm, fn);
+ value_ = globalFn;
+}
+
+ArkNativeFunction::~ArkNativeFunction()
+{
+}
+
+void* ArkNativeFunction::GetInterface(int interfaceId)
+{
+ return (NativeFunction::INTERFACE_ID == interfaceId) ? (NativeFunction*)this
+ : ArkNativeObject::GetInterface(interfaceId);
+}
+
+Local ArkNativeFunction::NativeFunctionCallBack(EcmaVM* vm,
+ Local thisObj,
+ const Local argv[],
+ int32_t length,
+ void* data)
+{
+ panda::EscapeLocalScope scope(vm);
+ auto info = reinterpret_cast(data);
+ auto engine = reinterpret_cast(info->engine);
+ auto cb = info->callback;
+ if (engine == nullptr) {
+ HILOG_ERROR("native engine is null");
+ return JSValueRef::Undefined(vm);
+ }
+
+ NativeCallbackInfo cbInfo = { 0 };
+ NativeScopeManager* scopeManager = engine->GetScopeManager();
+ if (scopeManager == nullptr) {
+ HILOG_ERROR("scope manager is null");
+ return JSValueRef::Undefined(vm);
+ }
+ NativeScope* nativeScope = scopeManager->Open();
+ cbInfo.thisVar = ArkNativeEngine::ArkValueToNativeValue(engine, thisObj);
+ cbInfo.function = nullptr;
+ cbInfo.argc = length;
+ cbInfo.argv = nullptr;
+ cbInfo.functionInfo = info;
+ if (cbInfo.argc > 0) {
+ cbInfo.argv = new NativeValue* [cbInfo.argc];
+ for (size_t i = 0; i < cbInfo.argc; i++) {
+ cbInfo.argv[i] = ArkNativeEngine::ArkValueToNativeValue(engine, argv[i]);
+ }
+ }
+
+ NativeValue* result = nullptr;
+ if (cb != nullptr) {
+ result = cb(engine, &cbInfo);
+ }
+
+ if (cbInfo.argv != nullptr) {
+ delete[] cbInfo.argv;
+ cbInfo.argv = nullptr;
+ }
+
+ Global ret(vm, JSValueRef::Undefined(vm));
+ if (result == nullptr) {
+ if (engine->IsExceptionPending()) {
+ NativeValue* error = engine->GetAndClearLastException();
+ if (error != nullptr) {
+ ret = *error;
+ }
+ } else {
+ ret = Global(vm, JSValueRef::Undefined(vm));
+ }
+ } else {
+ ret = *result;
+ }
+ auto localRet = ret.ToLocal(vm);
+ scopeManager->Close(nativeScope);
+ return scope.Escape(localRet);
+}
+
+Local ArkNativeFunction::NativeClassFunctionCallBack(EcmaVM* vm,
+ Local function,
+ Local newTarget,
+ const Local argv[],
+ int32_t length,
+ void* data)
+{
+ panda::EscapeLocalScope scope(vm);
+ auto info = reinterpret_cast(data);
+ auto engine = reinterpret_cast(info->engine);
+ auto cb = info->callback;
+ if (engine == nullptr) {
+ HILOG_ERROR("native engine is null");
+ return JSValueRef::Undefined(vm);
+ }
+
+ NativeCallbackInfo cbInfo = { 0 };
+ NativeScopeManager* scopeManager = engine->GetScopeManager();
+ if (scopeManager == nullptr) {
+ HILOG_ERROR("scope manager is null");
+ return JSValueRef::Undefined(vm);
+ }
+ NativeScope* nativeScope = scopeManager->Open();
+ cbInfo.thisVar = ArkNativeEngine::ArkValueToNativeValue(engine, function);
+ cbInfo.function = ArkNativeEngine::ArkValueToNativeValue(engine, newTarget);
+ cbInfo.argc = length;
+ cbInfo.argv = nullptr;
+ cbInfo.functionInfo = info;
+ if (cbInfo.argc > 0) {
+ cbInfo.argv = new NativeValue* [cbInfo.argc];
+ for (size_t i = 0; i < cbInfo.argc; i++) {
+ cbInfo.argv[i] = ArkNativeEngine::ArkValueToNativeValue(engine, argv[i]);
+ }
+ }
+
+ NativeValue* result = nullptr;
+ if (cb != nullptr) {
+ result = cb(engine, &cbInfo);
+ }
+
+ if (cbInfo.argv != nullptr) {
+ delete[] cbInfo.argv;
+ cbInfo.argv = nullptr;
+ }
+
+ Global ret(vm, JSValueRef::Undefined(vm));
+ if (result == nullptr) {
+ if (engine->IsExceptionPending()) {
+ NativeValue* error = engine->GetAndClearLastException();
+ if (error != nullptr) {
+ ret = *error;
+ }
+ } else {
+ ret = Global(vm, JSValueRef::Undefined(vm));
+ }
+ } else {
+ ret = *result;
+ }
+ auto localRet = ret.ToLocal(vm);
+ scopeManager->Close(nativeScope);
+ return scope.Escape(localRet);
+}
+
+NativeValue* ArkNativeFunction::GetFunctionPrototype()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global func = value_;
+ Local prototype = func->GetFunctionPrototype(vm);
+ return ArkNativeEngine::ArkValueToNativeValue(engine_, prototype);
+}
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_function.h b/native_engine/impl/ark/native_value/ark_native_function.h
new file mode 100644
index 0000000000000000000000000000000000000000..a97986aefb5484f36f6364126e7e494c7261d402
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_function.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_FUNCTION_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_FUNCTION_H
+
+#include "ark_native_object.h"
+
+class ArkNativeFunction : public ArkNativeObject, public NativeFunction {
+public:
+ ArkNativeFunction(ArkNativeEngine* engine, Local value);
+ ArkNativeFunction(ArkNativeEngine* engine, const char* name, size_t length, NativeCallback cb, void* value);
+ ArkNativeFunction(ArkNativeEngine* engine, const char* name, NativeCallback cb, void* value); // Used for class
+ ~ArkNativeFunction() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ NativeValue* GetFunctionPrototype();
+
+private:
+ static Local NativeFunctionCallBack(EcmaVM* vm,
+ Local thisObj,
+ const Local argv[],
+ int32_t length,
+ void* data);
+ static Local NativeClassFunctionCallBack(EcmaVM* vm,
+ Local function,
+ Local newTarget,
+ const Local argv[],
+ int32_t length,
+ void* data);
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_FUNCTION_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_number.cpp b/native_engine/impl/ark/native_value/ark_native_number.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..93a06b1c0a7de140aa38406b82b9b3bca7353ccf
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_number.cpp
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_number.h"
+
+#include
+
+using panda::NumberRef;
+ArkNativeNumber::ArkNativeNumber(ArkNativeEngine* engine, Local value) : ArkNativeValue(engine, value) {}
+
+ArkNativeNumber::ArkNativeNumber(ArkNativeEngine* engine, int32_t value)
+ : ArkNativeNumber(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = NumberRef::New(vm, value);
+ value_ = Global(vm, object);
+}
+
+ArkNativeNumber::ArkNativeNumber(ArkNativeEngine* engine, uint32_t value)
+ : ArkNativeNumber(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = NumberRef::New(vm, value);
+ value_ = Global(vm, object);
+}
+
+ArkNativeNumber::ArkNativeNumber(ArkNativeEngine* engine, int64_t value)
+ : ArkNativeNumber(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = NumberRef::New(vm, value);
+ value_ = Global(vm, object);
+}
+
+ArkNativeNumber::ArkNativeNumber(ArkNativeEngine* engine, double value)
+ : ArkNativeNumber(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = NumberRef::New(vm, value);
+ value_ = Global(vm, object);
+}
+
+ArkNativeNumber::~ArkNativeNumber() {}
+
+void* ArkNativeNumber::GetInterface(int interfaceId)
+{
+ return (NativeNumber::INTERFACE_ID == interfaceId) ? (NativeNumber*)this : nullptr;
+}
+
+ArkNativeNumber::operator int32_t()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ return value->Int32Value(vm);
+}
+
+ArkNativeNumber::operator uint32_t()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ return value->Uint32Value(vm);
+}
+
+ArkNativeNumber::operator int64_t()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ return value->IntegerValue(vm);
+}
+
+ArkNativeNumber::operator double()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ Local number(value.ToLocal(vm));
+ return number->Value();
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_number.h b/native_engine/impl/ark/native_value/ark_native_number.h
new file mode 100644
index 0000000000000000000000000000000000000000..4d8ea056c8b363f719d608eec7fe138a2d4837cb
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_number.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_NUMBER_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_NUMBER_H
+
+#include "ark_native_value.h"
+
+class ArkNativeNumber : public ArkNativeValue, public NativeNumber {
+public:
+ ArkNativeNumber(ArkNativeEngine* engine, Local value);
+ ArkNativeNumber(ArkNativeEngine* engine, int32_t value);
+ ArkNativeNumber(ArkNativeEngine* engine, uint32_t value);
+ ArkNativeNumber(ArkNativeEngine* engine, int64_t value);
+ ArkNativeNumber(ArkNativeEngine* engine, double value);
+ ~ArkNativeNumber() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ operator int32_t() override;
+ operator uint32_t() override;
+ operator int64_t() override;
+ operator double() override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_NUMBER_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_object.cpp b/native_engine/impl/ark/native_value/ark_native_object.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..ef67b3a7de5aec5d606069c4080430ae8aba3f2a
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_object.cpp
@@ -0,0 +1,271 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_object.h"
+
+#include "native_engine/native_property.h"
+#include "ark_headers.h"
+#include "ark_native_array.h"
+#include "ark_native_external.h"
+#include "ark_native_function.h"
+#include "ark_native_reference.h"
+
+#include "utils/log.h"
+
+using panda::ObjectRef;
+using panda::StringRef;
+using panda::NativePointerRef;
+using panda::ArrayRef;
+using panda::PropertyAttribute;
+ArkNativeObject::ArkNativeObject(ArkNativeEngine* engine)
+ : ArkNativeObject(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = ObjectRef::New(vm);
+ value_ = Global(vm, object);
+}
+
+ArkNativeObject::ArkNativeObject(ArkNativeEngine* engine, Local value) : ArkNativeValue(engine, value) {}
+
+ArkNativeObject::~ArkNativeObject() {}
+
+void* ArkNativeObject::GetInterface(int interfaceId)
+{
+ return (NativeObject::INTERFACE_ID == interfaceId) ? (NativeObject*)this : nullptr;
+}
+
+void ArkNativeObject::SetNativePointer(void* pointer, NativeFinalize cb, void* hint)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ NativeObjectInfo* objInfo = NativeObjectInfo::CreateNewInstance();
+ objInfo->engine = engine_;
+ objInfo->nativeObject = nullptr;
+ objInfo->callback = cb;
+ objInfo->hint = hint;
+
+ Local object = NativePointerRef::New(vm, pointer,
+ [](void* data, void* info) {
+ auto externalInfo = reinterpret_cast(info);
+ auto engine = externalInfo->engine;
+ auto callback = externalInfo->callback;
+ auto hint = externalInfo->hint;
+ callback(engine, data, hint);
+ delete externalInfo;
+ },
+ objInfo);
+
+ Local key = StringRef::NewFromUtf8(vm, "_napiwrapper");
+ bool has = value->Has(vm, key);
+ if (!has) {
+ value->Set(vm, key, object);
+ }
+}
+
+void* ArkNativeObject::GetNativePointer()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ Local key = StringRef::NewFromUtf8(vm, "_napiwrapper");
+ Local val = value->Get(vm, key);
+ void* result = nullptr;
+ if (val->IsNativeObject() || val->IsNativePointer()) {
+ Local ext(val);
+ result = ext->Value();
+ }
+ return result;
+}
+
+NativeValue* ArkNativeObject::GetPropertyNames()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global val = value_;
+ Local arrayVal = val->GetOwnEnumerablePropertyNames(vm);
+
+ return new ArkNativeArray(engine_, arrayVal);
+}
+
+NativeValue* ArkNativeObject::GetPrototype()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ Local val = obj->GetPrototype(vm);
+
+ return ArkNativeEngine::ArkValueToNativeValue(engine_, val);
+}
+
+bool ArkNativeObject::DefineProperty(NativePropertyDescriptor propertyDescriptor)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ bool result = false;
+ Local propertyName = StringRef::NewFromUtf8(vm, propertyDescriptor.utf8name);
+
+ bool writable = (propertyDescriptor.attributes & NATIVE_WRITABLE) != 0;
+ bool enumable = (propertyDescriptor.attributes & NATIVE_ENUMERABLE) != 0;
+ bool configable = (propertyDescriptor.attributes & NATIVE_CONFIGURABLE) != 0;
+
+ NativeScopeManager* scopeManager = engine_->GetScopeManager();
+ if (scopeManager == nullptr) {
+ HILOG_ERROR("scope manager is null");
+ return false;
+ }
+ NativeScope* nativeScope = scopeManager->Open();
+ if (propertyDescriptor.getter != nullptr || propertyDescriptor.setter != nullptr) {
+ Global localGetter(vm, JSValueRef::Undefined(vm));
+ Global localSetter(vm, JSValueRef::Undefined(vm));
+
+ if (propertyDescriptor.getter != nullptr) {
+ NativeValue* getter =
+ new ArkNativeFunction(engine_, "getter", 0, propertyDescriptor.getter, propertyDescriptor.data);
+ localGetter = *getter;
+ }
+ if (propertyDescriptor.setter != nullptr) {
+ NativeValue* setter =
+ new ArkNativeFunction(engine_, "setter", 0, propertyDescriptor.setter, propertyDescriptor.data);
+ localSetter = *setter;
+ }
+
+ PropertyAttribute attr(JSValueRef::Undefined(engine_->GetEcmaVm()), false, enumable, configable);
+ result = obj->SetAccessorProperty(vm, propertyName, localGetter.ToLocal(vm), localSetter.ToLocal(vm), attr);
+ } else if (propertyDescriptor.method != nullptr) {
+ NativeValue* cb = new ArkNativeFunction(engine_, propertyDescriptor.utf8name, 0, propertyDescriptor.method,
+ propertyDescriptor.data);
+ Global globalCb = *cb;
+ PropertyAttribute attr(globalCb.ToLocal(vm), writable, enumable, configable);
+ result = obj->DefineProperty(vm, propertyName, attr);
+ } else {
+ Global value = *(propertyDescriptor.value);
+
+ PropertyAttribute attr(value.ToLocal(vm), writable, enumable, configable);
+ result = obj->DefineProperty(vm, propertyName, attr);
+ }
+ scopeManager->Close(nativeScope);
+ return result;
+}
+
+bool ArkNativeObject::SetProperty(NativeValue* key, NativeValue* value)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ Global k = *key;
+ Global val = *value;
+
+ return obj->Set(vm, k.ToLocal(vm), val.ToLocal(vm));
+}
+
+NativeValue* ArkNativeObject::GetProperty(NativeValue* key)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global k = *key;
+ Global obj = value_;
+
+ Local val = obj->Get(vm, k.ToLocal(vm));
+ return ArkNativeEngine::ArkValueToNativeValue(engine_, val);
+}
+
+bool ArkNativeObject::HasProperty(NativeValue* key)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global obj = value_;
+ Global k = *key;
+
+ return obj->Has(vm, k.ToLocal(vm));
+}
+
+bool ArkNativeObject::DeleteProperty(NativeValue* key)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global k = *key;
+ Global obj = value_;
+
+ return obj->Delete(vm, k.ToLocal(vm));
+}
+
+bool ArkNativeObject::SetProperty(const char* name, NativeValue* value)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+
+ Global obj = value_;
+ Local key = StringRef::NewFromUtf8(vm, name);
+ Global val = *value;
+
+ return obj->Set(vm, key, val.ToLocal(vm));
+}
+
+NativeValue* ArkNativeObject::GetProperty(const char* name)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+
+ Local key = StringRef::NewFromUtf8(vm, name);
+ Global obj = value_;
+ Local val = obj->Get(vm, key);
+ return ArkNativeEngine::ArkValueToNativeValue(engine_, val);
+}
+
+bool ArkNativeObject::HasProperty(const char* name)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+
+ Local key = StringRef::NewFromUtf8(vm, name);
+ Global obj = value_;
+
+ return obj->Has(vm, key);
+}
+
+bool ArkNativeObject::DeleteProperty(const char* name)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+
+ Local key = StringRef::NewFromUtf8(vm, name);
+ Global obj = value_;
+
+ return obj->Delete(vm, key);
+}
+
+bool ArkNativeObject::SetPrivateProperty(const char* name, NativeValue* value)
+{
+ return false;
+}
+
+NativeValue* ArkNativeObject::GetPrivateProperty(const char* name)
+{
+ return nullptr;
+}
+
+bool ArkNativeObject::HasPrivateProperty(const char* name)
+{
+ return false;
+}
+
+bool ArkNativeObject::DeletePrivateProperty(const char* name)
+{
+ return false;
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_object.h b/native_engine/impl/ark/native_value/ark_native_object.h
new file mode 100644
index 0000000000000000000000000000000000000000..6350b9fb681c1082c3de7ea8ecaaeb7ac093aa36
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_object.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_OBJECT_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_OBJECT_H
+
+#include "ark_native_value.h"
+
+class ArkNativeObject : public ArkNativeValue, public NativeObject {
+public:
+ explicit ArkNativeObject(ArkNativeEngine* engine);
+ ArkNativeObject(ArkNativeEngine* engine, Local value);
+ ~ArkNativeObject() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ void SetNativePointer(void* pointer, NativeFinalize cb, void* hint) override;
+ void* GetNativePointer() override;
+
+ NativeValue* GetPropertyNames() override;
+
+ NativeValue* GetPrototype() override;
+
+ bool DefineProperty(NativePropertyDescriptor propertyDescriptor) override;
+
+ bool SetProperty(NativeValue* key, NativeValue* value) override;
+ NativeValue* GetProperty(NativeValue* key) override;
+ bool HasProperty(NativeValue* key) override;
+ bool DeleteProperty(NativeValue* key) override;
+
+ bool SetProperty(const char* name, NativeValue* value) override;
+ NativeValue* GetProperty(const char* name) override;
+ bool HasProperty(const char* name) override;
+ bool DeleteProperty(const char* name) override;
+
+ bool SetPrivateProperty(const char* name, NativeValue* value) override;
+ NativeValue* GetPrivateProperty(const char* name) override;
+ bool HasPrivateProperty(const char* name) override;
+ bool DeletePrivateProperty(const char* name) override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_OBJECT_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_string.cpp b/native_engine/impl/ark/native_value/ark_native_string.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..0c5fb959a1286d0846dcfdccc65ad93161909564
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_string.cpp
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_string.h"
+#include "securec.h"
+#include "utils/log.h"
+
+#include
+#include
+#include
+#include
+
+using panda::StringRef;
+ArkNativeString::ArkNativeString(ArkNativeEngine* engine, const char* value, size_t length)
+ : ArkNativeString(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine->GetEcmaVm();
+ LocalScope scope(vm);
+ Local object = StringRef::NewFromUtf8(vm, value, length);
+ value_ = Global(vm, object);
+}
+ArkNativeString::ArkNativeString(ArkNativeEngine* engine, Local value) : ArkNativeValue(engine, value) {}
+
+ArkNativeString::~ArkNativeString() {}
+
+void* ArkNativeString::GetInterface(int interfaceId)
+{
+ return (NativeString::INTERFACE_ID == interfaceId) ? (NativeString*)this : nullptr;
+}
+
+void ArkNativeString::GetCString(char* buffer, size_t size, size_t* length)
+{
+ if (length == nullptr) {
+ return;
+ }
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global val = value_;
+ if (buffer == nullptr) {
+ *length = val->Length();
+ } else if (size != 0) {
+ int copied = val->WriteUtf8(buffer, size) - 1;
+ buffer[copied] = '\0';
+ *length = copied;
+ } else {
+ *length = 0;
+ }
+}
+
+size_t ArkNativeString::GetLength()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ return value->Length();
+}
+
+size_t ArkNativeString::EncodeWriteUtf8(char* buffer, size_t bufferSize, int32_t* nchars)
+{
+ if (buffer == nullptr || nchars == nullptr) {
+ HILOG_ERROR("buffer is null or nchars is null");
+ return 0;
+ }
+
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global val = value_;
+ std::string valString = val->ToString();
+ std::u16string src = std::wstring_convert, char16_t>{}.from_bytes(valString);
+ char* oldLocale = setlocale(LC_CTYPE, "");
+ setlocale(LC_CTYPE, "en_US.UTF-8");
+
+ mbstate_t state;
+ size_t pos = 0;
+ size_t writableSize = bufferSize;
+ char u8Char[4] = {};
+ uint32_t i = 0;
+ for (; i < src.length(); i++) {
+ size_t rc = c16rtomb(u8Char, src.at(i), &state);
+ if (rc == -1 || rc > writableSize) {
+ break;
+ }
+
+ int ret = memcpy_s((buffer + pos), writableSize, u8Char, rc);
+ if (ret != EOK) {
+ HILOG_ERROR("memcpy_s failed");
+ break;
+ }
+ writableSize -= rc;
+ pos += rc;
+ }
+
+ setlocale(LC_CTYPE, oldLocale);
+ *nchars = i;
+ buffer[bufferSize] = '\0';
+ HILOG_DEBUG("EncodeWriteUtf8 the result of buffer: %{public}s", buffer);
+ return pos;
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_string.h b/native_engine/impl/ark/native_value/ark_native_string.h
new file mode 100644
index 0000000000000000000000000000000000000000..e81101717057c9ec32192d2d71c184b04f333a74
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_string.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_STRING_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_STRING_H
+
+#include "ark_native_value.h"
+
+class ArkNativeString : public ArkNativeValue, public NativeString {
+public:
+ ArkNativeString(ArkNativeEngine* engine, const char* value, size_t length);
+ ArkNativeString(ArkNativeEngine* engine, Local value);
+ ~ArkNativeString() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ void GetCString(char* buffer, size_t size, size_t* length) override;
+ size_t GetLength() override;
+ size_t EncodeWriteUtf8(char* buffer, size_t bufferSize, int32_t* nchars) override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_STRING_H */
diff --git a/native_engine/impl/ark/native_value/ark_native_typed_array.cpp b/native_engine/impl/ark/native_value/ark_native_typed_array.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a8207724d87e038900825d4791f8b6d5253e2fd4
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_typed_array.cpp
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_typed_array.h"
+
+ArkNativeTypedArray::ArkNativeTypedArray(ArkNativeEngine* engine, Local value)
+ : ArkNativeObject(engine, value)
+{
+}
+
+ArkNativeTypedArray::ArkNativeTypedArray(ArkNativeEngine* engine,
+ NativeTypedArrayType type,
+ NativeValue* value,
+ size_t length,
+ size_t offset)
+ : ArkNativeTypedArray(engine, JSValueRef::Undefined(engine->GetEcmaVm()))
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+
+ Global globalValue = *value;
+ Local localValue = globalValue.ToLocal(vm);
+ Local buffer(localValue);
+
+ Local typedArray(JSValueRef::Undefined(vm));
+ switch (type) {
+ case NATIVE_INT8_ARRAY:
+ typedArray = panda::Int8ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_UINT8_ARRAY:
+ typedArray = panda::Uint8ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_UINT8_CLAMPED_ARRAY:
+ typedArray = panda::Uint8ClampedArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_INT16_ARRAY:
+ typedArray = panda::Int16ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_UINT16_ARRAY:
+ typedArray = panda::Uint16ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_INT32_ARRAY:
+ typedArray = panda::Int32ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_UINT32_ARRAY:
+ typedArray = panda::Uint32ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_FLOAT32_ARRAY:
+ typedArray = panda::Float32ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_FLOAT64_ARRAY:
+ typedArray = panda::Float64ArrayRef::New(vm, buffer, offset, length);
+ break;
+ case NATIVE_BIGINT64_ARRAY:
+ // not support yet
+ break;
+ case NATIVE_BIGUINT64_ARRAY:
+ // not support yet
+ break;
+ default:;
+ }
+ Global globalTypedArray(vm, typedArray);
+ value_ = globalTypedArray;
+}
+
+ArkNativeTypedArray::~ArkNativeTypedArray() {}
+
+void* ArkNativeTypedArray::GetInterface(int interfaceId)
+{
+ return (NativeTypedArray::INTERFACE_ID == interfaceId) ? (NativeTypedArray*)this
+ : ArkNativeObject::GetInterface(interfaceId);
+}
+
+NativeTypedArrayType ArkNativeTypedArray::GetTypedArrayType()
+{
+ Global value = value_;
+ NativeTypedArrayType type = NATIVE_INT8_ARRAY;
+ if (value->IsInt8Array()) {
+ type = NATIVE_INT8_ARRAY;
+ } else if (value->IsUint8Array()) {
+ type = NATIVE_UINT8_ARRAY;
+ } else if (value->IsUint8ClampedArray()) {
+ type = NATIVE_UINT8_CLAMPED_ARRAY;
+ } else if (value->IsInt16Array()) {
+ type = NATIVE_INT16_ARRAY;
+ } else if (value->IsUint16Array()) {
+ type = NATIVE_UINT16_ARRAY;
+ } else if (value->IsInt32Array()) {
+ type = NATIVE_INT32_ARRAY;
+ } else if (value->IsUint32Array()) {
+ type = NATIVE_UINT32_ARRAY;
+ } else if (value->IsFloat32Array()) {
+ type = NATIVE_FLOAT32_ARRAY;
+ } else if (value->IsFloat64Array()) {
+ type = NATIVE_FLOAT64_ARRAY;
+ }
+ return type;
+}
+
+size_t ArkNativeTypedArray::GetLength()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return value->ByteLength(vm);
+}
+
+NativeValue* ArkNativeTypedArray::GetArrayBuffer()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return ArkNativeEngine::ArkValueToNativeValue(engine_, value->GetArrayBuffer(vm));
+}
+
+void* ArkNativeTypedArray::GetData()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return value->GetArrayBuffer(vm)->GetBuffer();
+}
+
+size_t ArkNativeTypedArray::GetOffset()
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+
+ return value->ByteOffset(vm);
+}
diff --git a/native_engine/impl/ark/native_value/ark_native_typed_array.h b/native_engine/impl/ark/native_value/ark_native_typed_array.h
new file mode 100644
index 0000000000000000000000000000000000000000..71c332768eb8192af87f8ae74e8e2cca5ded9df0
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_typed_array.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_TYPED_ARRAY_H
+#define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_TYPED_ARRAY_H
+
+#include "ark_native_object.h"
+
+using panda::TypedArrayRef;
+using panda::ArrayBufferRef;
+class ArkNativeTypedArray : public ArkNativeObject, public NativeTypedArray {
+public:
+ ArkNativeTypedArray(ArkNativeEngine* engine, Local value);
+ ArkNativeTypedArray(ArkNativeEngine* engine,
+ NativeTypedArrayType type,
+ NativeValue* value,
+ size_t length,
+ size_t offset);
+ ~ArkNativeTypedArray() override;
+
+ void* GetInterface(int interfaceId) override;
+
+ NativeTypedArrayType GetTypedArrayType() override;
+ size_t GetLength() override;
+ NativeValue* GetArrayBuffer() override;
+ void* GetData() override;
+ size_t GetOffset() override;
+};
+
+#endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_NATIVE_VALUE_ARK_NATIVE_TYPED_ARRAY_H */
\ No newline at end of file
diff --git a/native_engine/impl/ark/native_value/ark_native_value.cpp b/native_engine/impl/ark/native_value/ark_native_value.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..5ad065362392288e7737043fd60ef72e5fc5ea90
--- /dev/null
+++ b/native_engine/impl/ark/native_value/ark_native_value.cpp
@@ -0,0 +1,179 @@
+/*
+ * Copyright (c) 2021 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 "ark_native_value.h"
+
+ArkNativeValue::ArkNativeValue(ArkNativeEngine* engine, Local value)
+{
+ engine_ = engine;
+ Global globalValue(engine->GetEcmaVm(), value);
+ value_ = globalValue;
+
+ NativeScopeManager* scopeManager = engine_->GetScopeManager();
+ if (scopeManager != nullptr) {
+ scopeManager->CreateHandle(this);
+ }
+}
+
+ArkNativeValue::~ArkNativeValue()
+{
+ // Addr of Global stored in ArkNativeValue should be released.
+ Global oldValue = value_;
+ oldValue.FreeGlobalHandleAddr();
+}
+
+void* ArkNativeValue::GetInterface(int interfaceId)
+{
+ return nullptr;
+}
+
+void ArkNativeValue::UpdateValue(Local value)
+{
+ auto vm = engine_->GetEcmaVm();
+ Global oldValue = value_;
+ oldValue.FreeGlobalHandleAddr();
+
+ Global newValue(vm, value);
+ value_ = newValue;
+}
+
+NativeValueType ArkNativeValue::TypeOf()
+{
+ Global value = value_;
+ NativeValueType result;
+
+ // TODO: optimize it
+ if (value->IsNumber()) {
+ result = NATIVE_NUMBER;
+ } else if (value->IsString()) {
+ result = NATIVE_STRING;
+ } else if (value->IsFunction()) {
+ result = NATIVE_FUNCTION;
+ } else if (value->IsNativeObject() || value->IsNativePointer()) {
+ result = NATIVE_EXTERNAL;
+ } else if (value->IsNull()) {
+ result = NATIVE_NULL;
+ } else if (value->IsBoolean()) {
+ result = NATIVE_BOOLEAN;
+ } else if (value->IsUndefined()) {
+ result = NATIVE_UNDEFINED;
+ } else if (value->IsSymbol()) {
+ result = NATIVE_SYMBOL;
+ } else if (value->IsObject()) {
+ result = NATIVE_OBJECT;
+ } else {
+ result = NATIVE_UNDEFINED;
+ }
+
+ return result;
+}
+
+bool ArkNativeValue::InstanceOf(NativeValue* obj)
+{
+ auto vm = engine_->GetEcmaVm();
+ LocalScope scope(vm);
+ Global value = value_;
+ Global