From 1fd1454e80a7c41df965331966446123d2df9554 Mon Sep 17 00:00:00 2001 From: xliu Date: Mon, 1 Nov 2021 20:31:30 +0800 Subject: [PATCH] add arraylist and deque Signed-off-by: xliu Change-Id: Ib5da64aa33c08cc573d6d26dfd2414978e5d40d2 --- container/BUILD.gn | 190 ++++++++++++++++++ container/arraylist/js_arraylist.ts | 25 +++ .../arraylist/native_module_arraylist.cpp | 78 +++++++ container/build_ts_js.py | 52 +++++ container/deque/js_deque.ts | 25 +++ container/deque/native_module_deque.cpp | 68 +++++++ container/tsconfig.json | 15 ++ ohos.build | 3 +- 8 files changed, 455 insertions(+), 1 deletion(-) create mode 100755 container/BUILD.gn create mode 100644 container/arraylist/js_arraylist.ts create mode 100755 container/arraylist/native_module_arraylist.cpp create mode 100755 container/build_ts_js.py create mode 100644 container/deque/js_deque.ts create mode 100755 container/deque/native_module_deque.cpp create mode 100644 container/tsconfig.json diff --git a/container/BUILD.gn b/container/BUILD.gn new file mode 100755 index 0000000..a8979c4 --- /dev/null +++ b/container/BUILD.gn @@ -0,0 +1,190 @@ +# 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. + +import("//ark/ts2abc/ts2panda/ts2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") +import("//foundation/ace/ace_engine/ace_config.gni") + +# arraylist compile .ts to .js. +action("build_arraylist_ts") { + script = "//base/compileruntime/js_util_module/container/build_ts_js.py" + args = [ + "--dst-file", + rebase_path(target_out_dir + "/js_arraylist.js"), + "--src-file", + rebase_path("./out/arraylist/js_arraylist.js"), + ] + depfile = "$target_gen_dir/$target_name.d" + outputs = [ target_out_dir + "/js_arraylist.js" ] +} + +base_output_path = get_label_info(":js_arraylist", "target_out_dir") +js_arraylist_obj_path = base_output_path + "/arraylist.o" +gen_js_obj("js_arraylist") { + input = "$target_out_dir/js_arraylist.js" + output = js_arraylist_obj_path + dep = ":build_arraylist_ts" +} + +# compile .js to .abc. +action("gen_arraylist_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path(target_out_dir + "/js_arraylist.js"), + "--dst-file", + rebase_path(target_out_dir + "/arraylist.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ + ":build_arraylist_ts", + "//ark/ts2abc/ts2panda:ark_ts2abc_build", + ] + + inputs = [ target_out_dir + "/js_arraylist.js" ] + outputs = [ target_out_dir + "/arraylist.abc" ] +} + +abc_output_path = get_label_info(":arraylist_abc", "target_out_dir") +arraylist_abc_obj_path = abc_output_path + "/arraylist_abc.o" +gen_js_obj("arraylist_abc") { + input = "$target_out_dir/arraylist.abc" + output = arraylist_abc_obj_path + dep = ":gen_arraylist_abc" +} + +ohos_shared_library("arraylist") { + include_dirs = [ + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/compileruntime/js_util_module/container/arraylist", + ] + + sources = [ "arraylist/native_module_arraylist.cpp" ] + + deps = [ + ":arraylist_abc", + ":js_arraylist", + "//base/compileruntime/js_util_module/container/:js_arraylist", + "//foundation/ace/napi/:ace_napi", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + subsystem_name = "ccruntime" + part_name = "jsapi_util" + + relative_install_dir = "module" +} + +# deque compile .ts to .js. +action("build_deque_ts") { + script = "//base/compileruntime/js_util_module/container/build_ts_js.py" + args = [ + "--dst-file", + rebase_path(target_out_dir + "/js_deque.js"), + "--src-file", + rebase_path("./out/deque/js_deque.js"), + ] + depfile = "$target_gen_dir/$target_name.d" + outputs = [ target_out_dir + "/js_deque.js" ] +} + +base_output_path = get_label_info(":js_deque", "target_out_dir") +js_deque_obj_path = base_output_path + "/deque.o" +gen_js_obj("js_deque") { + input = "$target_out_dir/js_deque.js" + output = js_deque_obj_path + dep = ":build_deque_ts" +} + +# compile .js to .abc. +action("gen_deque_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path(target_out_dir + "/js_deque.js"), + "--dst-file", + rebase_path(target_out_dir + "/deque.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ + ":build_deque_ts", + "//ark/ts2abc/ts2panda:ark_ts2abc_build", + ] + + inputs = [ target_out_dir + "/js_deque.js" ] + outputs = [ target_out_dir + "/deque.abc" ] +} + +abc_output_path = get_label_info(":deque_abc", "target_out_dir") +deque_abc_obj_path = abc_output_path + "/deque_abc.o" +gen_js_obj("deque_abc") { + input = "$target_out_dir/deque.abc" + output = deque_abc_obj_path + dep = ":gen_deque_abc" +} + +ohos_shared_library("deque") { + include_dirs = [ + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/compileruntime/js_util_module/container/deque", + ] + + sources = [ "deque/native_module_deque.cpp" ] + + deps = [ + ":deque_abc", + ":js_deque", + "//base/compileruntime/js_util_module/container/:js_deque", + "//foundation/ace/napi/:ace_napi", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + subsystem_name = "ccruntime" + part_name = "jsapi_util" + + relative_install_dir = "module" +} + +group("container_packages") { + deps = [ + ":arraylist", + ":deque", + ] +} diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts new file mode 100644 index 0000000..57552fb --- /dev/null +++ b/container/arraylist/js_arraylist.ts @@ -0,0 +1,25 @@ +/* + * 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. + */ + +class ArrayList { + constructor() { + } +} + +export default { + ArrayList : ArrayList +} + + diff --git a/container/arraylist/native_module_arraylist.cpp b/container/arraylist/native_module_arraylist.cpp new file mode 100755 index 0000000..12d77fd --- /dev/null +++ b/container/arraylist/native_module_arraylist.cpp @@ -0,0 +1,78 @@ + /* + * 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 "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_arraylist_js_start[]; +extern const char _binary_js_arraylist_js_end[]; +extern const char _binary_arraylist_abc_start[]; +extern const char _binary_arraylist_abc_end[]; + +namespace OHOS::Util { +static napi_value ArrayListInit(napi_env env, napi_value exports) +{ + // const char *arrayListClassName = "ArrayList"; + // napi_value arrayListClass = nullptr; + // static napi_property_descriptor arrayListDesc[] = { + // }; + // NAPI_CALL(env, napi_define_class(env, arrayListClassName, strlen(arrayListClassName), ArrayListConstructor, + // nullptr, sizeof(arrayListDesc) / sizeof(arrayListDesc[0]), arrayListDesc, + // &arrayListClass)); + // static napi_property_descriptor desc[] = { + // DECLARE_NAPI_PROPERTY("ArrayList", arrayListClass) + // }; + // NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_arraylist_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_arraylist_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_arraylist_js_end - _binary_js_arraylist_js_start; + } +} +extern "C" +__attribute__((visibility("default"))) void NAPI_arraylist_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_arraylist_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_arraylist_abc_end - _binary_arraylist_abc_start; + } +} + +static napi_module arrayListModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ArrayListInit, + .nm_modname = "ArrayList", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__ ((constructor)) void RegisterModule() +{ + napi_module_register(&arrayListModule); +} +} // namespace diff --git a/container/build_ts_js.py b/container/build_ts_js.py new file mode 100755 index 0000000..cff0af4 --- /dev/null +++ b/container/build_ts_js.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# 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. +import os +import platform +import argparse +import subprocess + +def run_command(cmd): + print(" ".join(cmd)) + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, universal_newlines=True) + out, err = proc.communicate() + if out != "": + print(out) + exit(1) + +if __name__ == '__main__': + + build_path = os.path.abspath(os.path.join(os.getcwd(), "../..")) + os.chdir("%s/base/compileruntime/js_util_module/container/" % build_path) + + parser = argparse.ArgumentParser() + parser.add_argument('--dst-file', + help='the converted target file') + parser.add_argument('--src-file', + help='the converted src file') + input_arguments = parser.parse_args() + + node = '../../../../prebuilts/build-tools/common/nodejs/\ +node-v12.18.4-linux-x64/bin/node' + tsc = '../../../../ark/ts2abc/ts2panda/node_modules/typescript/bin/tsc' + cmd = [node, tsc] + run_command(cmd) + + cmd = ['cp', "-r", input_arguments.src_file, input_arguments.dst_file] + run_command(cmd) + + cmd = ['rm', "-rf", './out'] + run_command(cmd) + exit(0) diff --git a/container/deque/js_deque.ts b/container/deque/js_deque.ts new file mode 100644 index 0000000..d26ef18 --- /dev/null +++ b/container/deque/js_deque.ts @@ -0,0 +1,25 @@ +/* + * 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. + */ + +class Deque { + constructor() { + } +} + +export default { + Deque : Deque +} + + diff --git a/container/deque/native_module_deque.cpp b/container/deque/native_module_deque.cpp new file mode 100755 index 0000000..a9c839a --- /dev/null +++ b/container/deque/native_module_deque.cpp @@ -0,0 +1,68 @@ + /* + * 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 "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +extern const char _binary_js_deque_js_start[]; +extern const char _binary_js_deque_js_end[]; +extern const char _binary_deque_abc_start[]; +extern const char _binary_deque_abc_end[]; + +namespace OHOS::Util { +static napi_value DequeInit(napi_env env, napi_value exports) +{ + return exports; +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_deque_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_js_deque_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_deque_js_end - _binary_js_deque_js_start; + } +} + +extern "C" +__attribute__((visibility("default"))) void NAPI_deque_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_deque_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_deque_abc_end - _binary_deque_abc_start; + } +} + +static napi_module dequeModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = DequeInit, + .nm_modname = "Deque", + .nm_priv = ((void*)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__ ((constructor)) void RegisterModule() +{ + napi_module_register(&dequeModule); +} +} // namespace diff --git a/container/tsconfig.json b/container/tsconfig.json new file mode 100644 index 0000000..c6076fd --- /dev/null +++ b/container/tsconfig.json @@ -0,0 +1,15 @@ +{ +"compilerOptions": { + "target": "es6", + "module": "es6", + "rootDir": "./", + //"outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./out", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + } +} + diff --git a/ohos.build b/ohos.build index e5737bd..c14bd95 100755 --- a/ohos.build +++ b/ohos.build @@ -7,7 +7,8 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_util_module/util:util_packages" + "//base/compileruntime/js_util_module/util:util_packages", + "//base/compileruntime/js_util_module/container:container_packages" ], "inner_kits": [ ], -- Gitee