From 668d1c01bdead0a3f9bdee7f89b93c2ab3f20320 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Sun, 26 Sep 2021 20:37:50 +0800 Subject: [PATCH 1/3] Signed-off-by: lifansheng On branch OpenHarmony-3.0-LTS Your branch is up to date with 'origin/OpenHarmony-3.0-LTS'. Changes to be committed: modified: convertxml/BUILD.gn modified: convertxml/native_module_convertxml.cpp modified: uri/BUILD.gn modified: uri/native_module_uri.cpp modified: url/BUILD.gn modified: url/native_module_url.cpp --- convertxml/BUILD.gn | 36 +++++++++++++++++++++++++ convertxml/native_module_convertxml.cpp | 12 +++++++++ uri/BUILD.gn | 35 +++++++++++++++++++++++- uri/native_module_uri.cpp | 12 +++++++++ url/BUILD.gn | 35 +++++++++++++++++++++++- url/native_module_url.cpp | 26 +++++++++++++----- 6 files changed, 147 insertions(+), 9 deletions(-) diff --git a/convertxml/BUILD.gn b/convertxml/BUILD.gn index a2ac7c53..2eb33223 100755 --- a/convertxml/BUILD.gn +++ b/convertxml/BUILD.gn @@ -11,16 +11,51 @@ # 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") + +# compile .js to .abc. +action("gen_convertxml_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path( + "//base/compileruntime/js_api_module/convertxml/js_convertxml.js"), + "--dst-file", + rebase_path(target_out_dir + "/convertxml.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build" ] + + inputs = [ "//base/compileruntime/js_api_module/convertxml/js_convertxml.js" ] + outputs = [ target_out_dir + "/convertxml.abc" ] +} base_output_path = get_label_info(":js_convertxml", "target_out_dir") js_xml_obj_path = base_output_path + "/convertxml.o" + gen_js_obj("js_convertxml") { input = "//base/compileruntime/js_api_module/convertxml/js_convertxml.js" output = js_xml_obj_path } +abc_output_path = get_label_info(":convertxml_abc", "target_out_dir") +convertxml_abc_obj_path = abc_output_path + "/convertxml_abc.o" +gen_js_obj("convertxml_abc") { + input = "$target_out_dir/convertxml.abc" + output = convertxml_abc_obj_path + dep = ":gen_convertxml_abc" +} + ohos_shared_library("convertxml") { include_dirs = [ "//third_party/icu/icu4c/source/common", @@ -36,6 +71,7 @@ ohos_shared_library("convertxml") { ] deps = [ + ":convertxml_abc", ":js_convertxml", "//base/compileruntime/js_api_module/convertxml/:js_convertxml", "//foundation/ace/napi/:ace_napi", diff --git a/convertxml/native_module_convertxml.cpp b/convertxml/native_module_convertxml.cpp index cfca087e..05ca3943 100755 --- a/convertxml/native_module_convertxml.cpp +++ b/convertxml/native_module_convertxml.cpp @@ -19,6 +19,8 @@ extern const char _binary_js_convertxml_js_start[]; extern const char _binary_js_convertxml_js_end[]; +extern const char _binary_convertxml_abc_start[]; +extern const char _binary_convertxml_abc_end[]; static napi_value ConvertXmlConstructor(napi_env env, napi_callback_info info) { @@ -95,6 +97,16 @@ __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start; } } +extern "C" +__attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_convertxml_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start; + } +} static napi_module ConvertXmlModule = { .nm_version = 1, diff --git a/uri/BUILD.gn b/uri/BUILD.gn index 0249e7a3..b0b2ca30 100755 --- a/uri/BUILD.gn +++ b/uri/BUILD.gn @@ -10,9 +10,41 @@ # 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") + +# compile .js to .abc. +action("gen_uri_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path("//base/compileruntime/js_api_module/uri/js_uri.js"), + "--dst-file", + rebase_path(target_out_dir + "/uri.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build"] + + inputs = [ "//base/compileruntime/js_api_module/uri/js_uri.js" ] + outputs = [ target_out_dir + "/uri.abc" ] +} + +abc_output_path = get_label_info(":uri_abc", "target_out_dir") +uri_abc_obj_path = abc_output_path + "/uri_abc.o" +gen_js_obj("uri_abc") { + input = "$target_out_dir/uri.abc" + output = uri_abc_obj_path + dep = ":gen_uri_abc" +} base_output_path = get_label_info(":js_uri", "target_out_dir") js_uri_obj_path = base_output_path + "/uri.o" @@ -35,6 +67,7 @@ ohos_shared_library("uri") { ] deps = [ + ":uri_abc", ":js_uri", "//base/compileruntime/js_api_module/uri/:js_uri", "//foundation/ace/napi/:ace_napi", diff --git a/uri/native_module_uri.cpp b/uri/native_module_uri.cpp index c0ee394a..54320f86 100755 --- a/uri/native_module_uri.cpp +++ b/uri/native_module_uri.cpp @@ -21,6 +21,8 @@ extern const char _binary_js_uri_js_start[]; extern const char _binary_js_uri_js_end[]; +extern const char _binary_uri_abc_start[]; +extern const char _binary_uri_abc_end[]; namespace OHOS::Uri { static napi_value UriConstructor(napi_env env, napi_callback_info info) { @@ -283,6 +285,16 @@ namespace OHOS::Uri { *bufLen = _binary_js_uri_js_end - _binary_js_uri_js_start; } } + extern "C" + __attribute__((visibility("default"))) void NAPI_uri_GetABCCode(const char** buf, int* buflen) + { + if (buf != nullptr) { + *buf = _binary_uri_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_uri_abc_end - _binary_uri_abc_start; + } + } static napi_module UriModule = { .nm_version = 1, diff --git a/url/BUILD.gn b/url/BUILD.gn index 5e8d16fa..0a65834b 100755 --- a/url/BUILD.gn +++ b/url/BUILD.gn @@ -10,9 +10,41 @@ # 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") + +# compile .js to .abc. +action("gen_url_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path("//base/compileruntime/js_api_module/url/js_url.js"), + "--dst-file", + rebase_path(target_out_dir + "/url.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build"] + + inputs = [ "//base/compileruntime/js_api_module/url/js_url.js" ] + outputs = [ target_out_dir + "/url.abc" ] +} + +abc_output_path = get_label_info(":url_abc", "target_out_dir") +url_abc_obj_path = abc_output_path + "/url_abc.o" +gen_js_obj("url_abc") { + input = "$target_out_dir/url.abc" + output = url_abc_obj_path + dep = ":gen_url_abc" +} base_output_path = get_label_info(":js_url", "target_out_dir") js_url_obj_path = base_output_path + "/url.o" @@ -35,6 +67,7 @@ ohos_shared_library("url") { ] deps = [ + ":url_abc", ":js_url", "//base/compileruntime/js_api_module/url/:js_url", "//foundation/ace/napi/:ace_napi", diff --git a/url/native_module_url.cpp b/url/native_module_url.cpp index 61497a76..e80ab108 100755 --- a/url/native_module_url.cpp +++ b/url/native_module_url.cpp @@ -20,6 +20,8 @@ extern const char _binary_js_url_js_start[]; extern const char _binary_js_url_js_end[]; +extern const char _binary_url_abc_start[]; +extern const char _binary_url_abc_end[]; namespace OHOS::Url { static void UrlStructor(napi_env &env, napi_callback_info &info, URL *&object) { @@ -29,8 +31,8 @@ namespace OHOS::Url { void *data = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, &data); napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - napi_valuetype valuetype1 = napi_null; - napi_valuetype valuetype2 = napi_null; + napi_valuetype valuetype1; + napi_valuetype valuetype2; std::string input = ""; napi_typeof(env, argv[0], &valuetype1); if (valuetype1 == napi_string) { @@ -80,7 +82,7 @@ namespace OHOS::Url { if (argc == 1) { std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); - napi_valuetype valuetype = napi_null; + napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); if (valuetype == napi_string) { char *type = nullptr; @@ -102,9 +104,9 @@ namespace OHOS::Url { napi_wrap( env, thisVar, object, [](napi_env env, void *data, void *hint) { - auto obj = (URL*)data; - if (obj != nullptr) { - delete obj; + auto object = (URL*)data; + if (object != nullptr) { + delete object; } }, nullptr, nullptr); @@ -884,7 +886,7 @@ namespace OHOS::Url { DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6), }; NAPI_CALL(env, napi_define_class(env, urlClassName, strlen(urlClassName), UrlConstructor, - nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass)); + nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass)); static napi_property_descriptor desc[] = { DECLARE_NAPI_PROPERTY("Url", urlClass) }; @@ -914,6 +916,16 @@ namespace OHOS::Url { *bufLen = _binary_js_url_js_end - _binary_js_url_js_start; } } + extern "C" + __attribute__((visibility("default"))) void NAPI_url_GetABCCode(const char** buf, int* buflen) + { + if (buf != nullptr) { + *buf = _binary_url_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_url_abc_end - _binary_url_abc_start; + } + } static napi_module UrlModule = { .nm_version = 1, -- Gitee From e3cb8d090a6c242f80f5f935b2b37c4b4ae4f8d9 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Sun, 26 Sep 2021 20:48:12 +0800 Subject: [PATCH 2/3] Signed-off-by: lifansheng On branch OpenHarmony-3.0-LTS Your branch is up to date with 'origin/OpenHarmony-3.0-LTS'. Changes to be committed: modified: convertxml/BUILD.gn modified: uri/BUILD.gn new file: uri/build_ts_js.py modified: uri/js_uri.cpp modified: uri/js_uri.h modified: uri/native_module_uri.cpp renamed: uri/js_uri.js -> uri/src/js_uri.ts new file: uri/tsconfig.json modified: url/BUILD.gn --- convertxml/BUILD.gn | 3 +- uri/BUILD.gn | 30 +++-- uri/build_ts_js.py | 21 ++++ uri/js_uri.cpp | 24 ++-- uri/js_uri.h | 16 +-- uri/native_module_uri.cpp | 34 +++-- uri/{js_uri.js => src/js_uri.ts} | 205 +++++++++++++++---------------- uri/tsconfig.json | 14 +++ url/BUILD.gn | 1 - 9 files changed, 192 insertions(+), 156 deletions(-) create mode 100644 uri/build_ts_js.py rename uri/{js_uri.js => src/js_uri.ts} (85%) mode change 100755 => 100644 create mode 100644 uri/tsconfig.json diff --git a/convertxml/BUILD.gn b/convertxml/BUILD.gn index 2eb33223..ebace873 100755 --- a/convertxml/BUILD.gn +++ b/convertxml/BUILD.gn @@ -23,8 +23,7 @@ action("gen_convertxml_abc") { args = [ "--src-js", - rebase_path( - "//base/compileruntime/js_api_module/convertxml/js_convertxml.js"), + rebase_path("//base/compileruntime/js_api_module/convertxml/js_convertxml.js"), "--dst-file", rebase_path(target_out_dir + "/convertxml.abc"), "--node", diff --git a/uri/BUILD.gn b/uri/BUILD.gn index b0b2ca30..be1eeae5 100755 --- a/uri/BUILD.gn +++ b/uri/BUILD.gn @@ -15,6 +15,22 @@ import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") import("//foundation/ace/ace_engine/ace_config.gni") +# compile .ts to .js. +action("build_ts_js") { + script = "//base/compileruntime/js_api_module/uri/build_ts_js.py" + depfile = "$target_gen_dir/$target_name.d" + outputs = [target_out_dir + "/js_uri.js" + ] +} + +base_output_path = get_label_info(":js_uri", "target_out_dir") +js_uri_obj_path = base_output_path + "/uri.o" +gen_js_obj("js_uri") { + input = "$target_out_dir/js_uri.js" + output = js_uri_obj_path + dep = ":build_ts_js" +} + # compile .js to .abc. action("gen_uri_abc") { visibility = [ ":*" ] @@ -22,7 +38,7 @@ action("gen_uri_abc") { args = [ "--src-js", - rebase_path("//base/compileruntime/js_api_module/uri/js_uri.js"), + rebase_path(target_out_dir + "/js_uri.js"), "--dst-file", rebase_path(target_out_dir + "/uri.abc"), "--node", @@ -32,9 +48,10 @@ action("gen_uri_abc") { "--node-modules", rebase_path("${node_modules}"), ] - deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build"] + deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build" , + ":build_ts_js" ] - inputs = [ "//base/compileruntime/js_api_module/uri/js_uri.js" ] + inputs = [ target_out_dir+"/js_uri.js" ] outputs = [ target_out_dir + "/uri.abc" ] } @@ -46,13 +63,6 @@ gen_js_obj("uri_abc") { dep = ":gen_uri_abc" } -base_output_path = get_label_info(":js_uri", "target_out_dir") -js_uri_obj_path = base_output_path + "/uri.o" -gen_js_obj("js_uri") { - input = "//base/compileruntime/js_api_module/uri/js_uri.js" - output = js_uri_obj_path -} - ohos_shared_library("uri") { include_dirs = [ "//third_party/icu/icu4c/source/common", diff --git a/uri/build_ts_js.py b/uri/build_ts_js.py new file mode 100644 index 00000000..27024303 --- /dev/null +++ b/uri/build_ts_js.py @@ -0,0 +1,21 @@ +#!/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 + +if __name__ == '__main__': + + build_path = os.path.abspath(os.path.join(os.getcwd(), "../..")) + os.chdir("%s/base/compileruntime/js_api_module/uri" % build_path) + os.system('../../../../developtools/ace-ets2bundle/compiler/node_modules/typescript/bin/tsc') \ No newline at end of file diff --git a/uri/js_uri.cpp b/uri/js_uri.cpp index 34452121..b6e7de17 100755 --- a/uri/js_uri.cpp +++ b/uri/js_uri.cpp @@ -15,15 +15,15 @@ #include "js_uri.h" #include "utils/log.h" namespace OHOS::Uri { - std::bitset g_ruleAlpha; - std::bitset g_ruleScheme; - std::bitset g_ruleUrlc; - std::bitset g_rulePath; - std::bitset g_ruleUserInfo; - std::bitset g_ruleScope; - std::bitset g_ruleDigit; - std::bitset g_rulePort; - void Uri::PreliminaryWork() const + std::bitset g_ruleAlpha; + std::bitset g_ruleScheme; + std::bitset g_ruleUrlc; + std::bitset g_rulePath; + std::bitset g_ruleUserInfo; + std::bitset g_ruleScope; + std::bitset g_ruleDigit; + std::bitset g_rulePort; + void Uri::PreliminaryWork() { std::string digitAggregate = "0123456789"; for (size_t i = 0; i < digitAggregate.size(); ++i) { @@ -129,11 +129,11 @@ namespace OHOS::Uri { } } - bool Uri::CheckCharacter(std::string data, std::bitset rule, bool flag) const + bool Uri::CheckCharacter(std::string data, std::bitset rule, bool flag) { size_t dataLen = data.size(); for (size_t i = 0; i < dataLen; ++i) { - if (data[i] >= 0 && data[i] < 128) { // 128:Maximum value of char + if (data[i] >= 0 && data[i] < 128) { // 128:Number of ASCII characters bool isLegal = rule.test(data[i]); if (!isLegal) { return false; @@ -255,7 +255,7 @@ namespace OHOS::Uri { } } else { // ipv4 - if (!isLawfulProt || !AnalysisIPV4()) { + if (!isLawfulProt | !AnalysisIPV4()) { uriData_.port = -1; uriData_.host = ""; uriData_.userInfo = ""; diff --git a/uri/js_uri.h b/uri/js_uri.h index 16365f83..923be07f 100755 --- a/uri/js_uri.h +++ b/uri/js_uri.h @@ -16,17 +16,17 @@ #ifndef COMPILERUNTIME_JS_API_URI_H #define COMPILERUNTIME_JS_API_URI_H -#include -#include -#include #include #include +#include +#include +#include +#include #include "napi/native_api.h" #include "napi/native_node_api.h" - namespace OHOS::Uri { constexpr int MAX_BIT_SIZE = 128; - struct UriData { + struct uri_data { int port = -1; std::string scheme = ""; std::string userInfo = ""; @@ -59,7 +59,7 @@ namespace OHOS::Uri { std::string GetQuery() const; std::string GetFragment() const; private: - void PreliminaryWork() const; + void PreliminaryWork(); void AnalysisUri(); void SpecialPath(); void AnalysisFragment(size_t pos); @@ -70,13 +70,13 @@ namespace OHOS::Uri { void AnalysisUserInfo(size_t pos); void AnalysisIPV6(); - bool CheckCharacter(std::string data, std::bitset rule, bool flag) const; + bool CheckCharacter(std::string data, std::bitset rule, bool flag); bool AnalysisPort(size_t pos); bool AnalysisIPV4(); std::string Split(std::string path) const; private: - UriData uriData_; + uri_data uriData_; std::string data_; std::string inputUri_; std::string errStr_; diff --git a/uri/native_module_uri.cpp b/uri/native_module_uri.cpp index 54320f86..91581781 100755 --- a/uri/native_module_uri.cpp +++ b/uri/native_module_uri.cpp @@ -13,17 +13,17 @@ * limitations under the License. */ - -#include "js_uri.h" -#include "utils/log.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "js_uri.h" +#include "utils/log.h" extern const char _binary_js_uri_js_start[]; extern const char _binary_js_uri_js_end[]; extern const char _binary_uri_abc_start[]; extern const char _binary_uri_abc_end[]; namespace OHOS::Uri { + napi_value g_uriClass = nullptr; static napi_value UriConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -36,24 +36,20 @@ namespace OHOS::Uri { NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); if (valuetype == napi_string) { char *type = nullptr; - std::string input = ""; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - input = type; - delete[] type; - } - object = new Uri(env, input); + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + object = new Uri(env, type); + delete[] type; } else { napi_throw_error(env, nullptr, "parameter type is error"); } NAPI_CALL(env, napi_wrap(env, thisVar, object, [](napi_env env, void *data, void *hint) { - auto obj = (Uri*)data; - if (obj != nullptr) { - delete obj; + auto object = (Uri*)data; + if (object != nullptr) { + delete object; } }, nullptr, nullptr)); return thisVar; @@ -66,9 +62,11 @@ namespace OHOS::Uri { Uri *muri = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); std::string normalizeUri = muri->Normalize(); + size_t argc = 1; + napi_value args[1] = { 0 }; napi_value result = nullptr; - size_t tempLen = normalizeUri.size(); - NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), tempLen, &result)); + NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), normalizeUri.size(), args)); + NAPI_CALL(env, napi_new_instance(env, g_uriClass, argc, args, &result)); return result; } @@ -266,7 +264,8 @@ namespace OHOS::Uri { DECLARE_NAPI_GETTER("isFailed", IsFailed), }; NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor, - nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); + nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); + g_uriClass = uriClass; static napi_property_descriptor desc[] = { DECLARE_NAPI_PROPERTY("Uri", uriClass) }; @@ -295,7 +294,6 @@ namespace OHOS::Uri { *buflen = _binary_uri_abc_end - _binary_uri_abc_start; } } - static napi_module UriModule = { .nm_version = 1, .nm_flags = 0, diff --git a/uri/js_uri.js b/uri/src/js_uri.ts old mode 100755 new mode 100644 similarity index 85% rename from uri/js_uri.js rename to uri/src/js_uri.ts index d2603eef..f94da8a9 --- a/uri/js_uri.js +++ b/uri/src/js_uri.ts @@ -1,105 +1,100 @@ -/* - * 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. - */ - -'use strict'; -const uri = requireInternal("uri"); - -class URI { - constructor(input) { - if (typeof input !== 'string' || input.length === 0) { - throw new Error("input type err"); - } - this.uricalss = new uri.Uri(input); - let errStr = this.uricalss.isFailed; - if (errStr.length !== 0) { - throw new Error(errStr); - } - } - toString() { - return toAscllString(this.uricalss.toString()); - } - - equals(other) { - return this.uricalss.equals(other.uricalss); - } - - isAbsolute() { - return this.uricalss.isAbsolute(); - } - - normalize() { - let uriStr = this.uricalss.normalize(); - return createNewUri(uriStr); - } - - get scheme() { - return this.uricalss.scheme; - } - - get authority() { - return this.uricalss.authority; - } - - get ssp() { - return this.uricalss.ssp; - } - - get userinfo() { - return this.uricalss.userinfo; - } - - get host() { - return this.uricalss.host; - } - - get port() { - return this.uricalss.port; - } - - get path() { - return this.uricalss.path; - } - - get query() { - return this.uricalss.query; - } - - get fragment() { - return this.uricalss.fragment; - } - -} - -function toAscllString(uriStr) { - if (uriStr.indexOf('[') !== -1) { - let arr = uriStr.split("["); - let brr = arr[1].split("]"); - arr[1] = '[' + brr[0] + ']'; - arr[2] = brr[1]; - arr[0] = encodeURI(arr[0]); - arr[2] = encodeURI(arr[2]); - return arr.join(''); - } else { - return encodeURI(uriStr); - } -} - -function createNewUri(uriStr) { - return new URI(uriStr); -} - -export default { - URI: URI, -} +/* + * 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. + */ +declare function requireInternal(s : string) : any; +const uri = requireInternal("uri"); + +class URI { + uricalss : any + constructor(input:any) { + if (typeof input !== 'string' || input.length === 0) { + throw new Error("input type err"); + } + this.uricalss = new uri.Uri(input); + let errStr = this.uricalss.isFailed; + if (errStr.length !== 0) { + throw new Error(errStr); + } + } + toString() { + return toAscllString(this.uricalss.toString()); + } + + equals(other:any) { + return this.uricalss.equals(other.uricalss); + } + + isAbsolute() { + return this.uricalss.isAbsolute(); + } + + normalize() { + return this.uricalss.normalize(); + } + + get scheme() { + return this.uricalss.scheme; + } + + get authority() { + return this.uricalss.authority; + } + + get ssp() { + return this.uricalss.ssp; + } + + get userinfo() { + return this.uricalss.userinfo; + } + + get host() { + return this.uricalss.host; + } + + get port() { + return this.uricalss.port; + } + + get path() { + return this.uricalss.path; + } + + get query() { + return this.uricalss.query; + } + + get fragment() { + return this.uricalss.fragment; + } + +} + +function toAscllString(uriStr:any) { + if (uriStr.indexOf('[') !== -1) { + let arr = uriStr.split("["); + let brr = arr[1].split("]"); + arr[1] = '[' + brr[0] + ']'; + arr[2] = brr[1]; + arr[0] = encodeURI(arr[0]); + arr[2] = encodeURI(arr[2]); + return arr.join(''); + } else { + return encodeURI(uriStr); + } +} + +export default { + URI: URI, +} diff --git a/uri/tsconfig.json b/uri/tsconfig.json new file mode 100644 index 00000000..f8212040 --- /dev/null +++ b/uri/tsconfig.json @@ -0,0 +1,14 @@ +{ +"compilerOptions": { + "target": "es6", + "module": "es6", + "rootDir": "./src", + //"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/ohos-arm-release/obj/base/compileruntime/js_api_module/uri/", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + } +} diff --git a/url/BUILD.gn b/url/BUILD.gn index 0a65834b..920d024a 100755 --- a/url/BUILD.gn +++ b/url/BUILD.gn @@ -67,7 +67,6 @@ ohos_shared_library("url") { ] deps = [ - ":url_abc", ":js_url", "//base/compileruntime/js_api_module/url/:js_url", "//foundation/ace/napi/:ace_napi", -- Gitee From dab2e6f6deb482527b887380fd4b91a672516db1 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Sun, 26 Sep 2021 22:23:30 +0800 Subject: [PATCH 3/3] Signed-off-by: lifansheng On branch OpenHarmony-3.0-LTS Your branch is up to date with 'origin/OpenHarmony-3.0-LTS'. Changes to be committed: modified: uri/BUILD.gn deleted: uri/build_ts_js.py modified: uri/js_uri.cpp modified: uri/js_uri.h renamed: uri/src/js_uri.ts -> uri/js_uri.js modified: uri/native_module_uri.cpp deleted: uri/tsconfig.json modified: url/BUILD.gn modified: url/native_module_url.cpp --- uri/BUILD.gn | 33 +++++++++++-------------------- uri/build_ts_js.py | 21 -------------------- uri/js_uri.cpp | 24 +++++++++++----------- uri/js_uri.h | 16 +++++++-------- uri/{src/js_uri.ts => js_uri.js} | 17 ++++++++++------ uri/native_module_uri.cpp | 34 +++++++++++++++++--------------- uri/tsconfig.json | 14 ------------- url/BUILD.gn | 3 ++- url/native_module_url.cpp | 15 +++++++------- 9 files changed, 69 insertions(+), 108 deletions(-) delete mode 100644 uri/build_ts_js.py rename uri/{src/js_uri.ts => js_uri.js} (89%) delete mode 100644 uri/tsconfig.json diff --git a/uri/BUILD.gn b/uri/BUILD.gn index be1eeae5..8d84a14e 100755 --- a/uri/BUILD.gn +++ b/uri/BUILD.gn @@ -15,22 +15,6 @@ import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") import("//foundation/ace/ace_engine/ace_config.gni") -# compile .ts to .js. -action("build_ts_js") { - script = "//base/compileruntime/js_api_module/uri/build_ts_js.py" - depfile = "$target_gen_dir/$target_name.d" - outputs = [target_out_dir + "/js_uri.js" - ] -} - -base_output_path = get_label_info(":js_uri", "target_out_dir") -js_uri_obj_path = base_output_path + "/uri.o" -gen_js_obj("js_uri") { - input = "$target_out_dir/js_uri.js" - output = js_uri_obj_path - dep = ":build_ts_js" -} - # compile .js to .abc. action("gen_uri_abc") { visibility = [ ":*" ] @@ -38,7 +22,7 @@ action("gen_uri_abc") { args = [ "--src-js", - rebase_path(target_out_dir + "/js_uri.js"), + rebase_path("//base/compileruntime/js_api_module/uri/js_uri.js"), "--dst-file", rebase_path(target_out_dir + "/uri.abc"), "--node", @@ -48,10 +32,9 @@ action("gen_uri_abc") { "--node-modules", rebase_path("${node_modules}"), ] - deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build" , - ":build_ts_js" ] + deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build" ] - inputs = [ target_out_dir+"/js_uri.js" ] + inputs = [ "//base/compileruntime/js_api_module/uri/js_uri.js" ] outputs = [ target_out_dir + "/uri.abc" ] } @@ -63,6 +46,13 @@ gen_js_obj("uri_abc") { dep = ":gen_uri_abc" } +base_output_path = get_label_info(":js_uri", "target_out_dir") +js_uri_obj_path = base_output_path + "/uri.o" +gen_js_obj("js_uri") { + input = "//base/compileruntime/js_api_module/uri/js_uri.js" + output = js_uri_obj_path +} + ohos_shared_library("uri") { include_dirs = [ "//third_party/icu/icu4c/source/common", @@ -77,8 +67,8 @@ ohos_shared_library("uri") { ] deps = [ - ":uri_abc", ":js_uri", + ":uri_abc", "//base/compileruntime/js_api_module/uri/:js_uri", "//foundation/ace/napi/:ace_napi", "//foundation/ace/napi/:ace_napi_quickjs", @@ -100,4 +90,3 @@ ohos_shared_library("uri") { group("uri_packages") { deps = [ ":uri" ] } - diff --git a/uri/build_ts_js.py b/uri/build_ts_js.py deleted file mode 100644 index 27024303..00000000 --- a/uri/build_ts_js.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/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 - -if __name__ == '__main__': - - build_path = os.path.abspath(os.path.join(os.getcwd(), "../..")) - os.chdir("%s/base/compileruntime/js_api_module/uri" % build_path) - os.system('../../../../developtools/ace-ets2bundle/compiler/node_modules/typescript/bin/tsc') \ No newline at end of file diff --git a/uri/js_uri.cpp b/uri/js_uri.cpp index b6e7de17..34452121 100755 --- a/uri/js_uri.cpp +++ b/uri/js_uri.cpp @@ -15,15 +15,15 @@ #include "js_uri.h" #include "utils/log.h" namespace OHOS::Uri { - std::bitset g_ruleAlpha; - std::bitset g_ruleScheme; - std::bitset g_ruleUrlc; - std::bitset g_rulePath; - std::bitset g_ruleUserInfo; - std::bitset g_ruleScope; - std::bitset g_ruleDigit; - std::bitset g_rulePort; - void Uri::PreliminaryWork() + std::bitset g_ruleAlpha; + std::bitset g_ruleScheme; + std::bitset g_ruleUrlc; + std::bitset g_rulePath; + std::bitset g_ruleUserInfo; + std::bitset g_ruleScope; + std::bitset g_ruleDigit; + std::bitset g_rulePort; + void Uri::PreliminaryWork() const { std::string digitAggregate = "0123456789"; for (size_t i = 0; i < digitAggregate.size(); ++i) { @@ -129,11 +129,11 @@ namespace OHOS::Uri { } } - bool Uri::CheckCharacter(std::string data, std::bitset rule, bool flag) + bool Uri::CheckCharacter(std::string data, std::bitset rule, bool flag) const { size_t dataLen = data.size(); for (size_t i = 0; i < dataLen; ++i) { - if (data[i] >= 0 && data[i] < 128) { // 128:Number of ASCII characters + if (data[i] >= 0 && data[i] < 128) { // 128:Maximum value of char bool isLegal = rule.test(data[i]); if (!isLegal) { return false; @@ -255,7 +255,7 @@ namespace OHOS::Uri { } } else { // ipv4 - if (!isLawfulProt | !AnalysisIPV4()) { + if (!isLawfulProt || !AnalysisIPV4()) { uriData_.port = -1; uriData_.host = ""; uriData_.userInfo = ""; diff --git a/uri/js_uri.h b/uri/js_uri.h index 923be07f..16365f83 100755 --- a/uri/js_uri.h +++ b/uri/js_uri.h @@ -16,17 +16,17 @@ #ifndef COMPILERUNTIME_JS_API_URI_H #define COMPILERUNTIME_JS_API_URI_H -#include -#include -#include #include -#include #include +#include +#include +#include #include "napi/native_api.h" #include "napi/native_node_api.h" + namespace OHOS::Uri { constexpr int MAX_BIT_SIZE = 128; - struct uri_data { + struct UriData { int port = -1; std::string scheme = ""; std::string userInfo = ""; @@ -59,7 +59,7 @@ namespace OHOS::Uri { std::string GetQuery() const; std::string GetFragment() const; private: - void PreliminaryWork(); + void PreliminaryWork() const; void AnalysisUri(); void SpecialPath(); void AnalysisFragment(size_t pos); @@ -70,13 +70,13 @@ namespace OHOS::Uri { void AnalysisUserInfo(size_t pos); void AnalysisIPV6(); - bool CheckCharacter(std::string data, std::bitset rule, bool flag); + bool CheckCharacter(std::string data, std::bitset rule, bool flag) const; bool AnalysisPort(size_t pos); bool AnalysisIPV4(); std::string Split(std::string path) const; private: - uri_data uriData_; + UriData uriData_; std::string data_; std::string inputUri_; std::string errStr_; diff --git a/uri/src/js_uri.ts b/uri/js_uri.js similarity index 89% rename from uri/src/js_uri.ts rename to uri/js_uri.js index f94da8a9..a2e37574 100644 --- a/uri/src/js_uri.ts +++ b/uri/js_uri.js @@ -12,12 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -declare function requireInternal(s : string) : any; + +'use strict'; const uri = requireInternal("uri"); class URI { - uricalss : any - constructor(input:any) { + constructor(input) { if (typeof input !== 'string' || input.length === 0) { throw new Error("input type err"); } @@ -31,7 +31,7 @@ class URI { return toAscllString(this.uricalss.toString()); } - equals(other:any) { + equals(other) { return this.uricalss.equals(other.uricalss); } @@ -40,7 +40,8 @@ class URI { } normalize() { - return this.uricalss.normalize(); + let uriStr = this.uricalss.normalize(); + return createNewUri(uriStr); } get scheme() { @@ -81,7 +82,7 @@ class URI { } -function toAscllString(uriStr:any) { +function toAscllString(uriStr) { if (uriStr.indexOf('[') !== -1) { let arr = uriStr.split("["); let brr = arr[1].split("]"); @@ -95,6 +96,10 @@ function toAscllString(uriStr:any) { } } +function createNewUri(uriStr) { + return new URI(uriStr); +} + export default { URI: URI, } diff --git a/uri/native_module_uri.cpp b/uri/native_module_uri.cpp index 91581781..96107d99 100755 --- a/uri/native_module_uri.cpp +++ b/uri/native_module_uri.cpp @@ -13,17 +13,18 @@ * limitations under the License. */ -#include "napi/native_api.h" -#include "napi/native_node_api.h" + #include "js_uri.h" #include "utils/log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" extern const char _binary_js_uri_js_start[]; extern const char _binary_js_uri_js_end[]; extern const char _binary_uri_abc_start[]; extern const char _binary_uri_abc_end[]; + namespace OHOS::Uri { - napi_value g_uriClass = nullptr; static napi_value UriConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -36,20 +37,24 @@ namespace OHOS::Uri { NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); if (valuetype == napi_string) { char *type = nullptr; + std::string input = ""; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - object = new Uri(env, type); - delete[] type; + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + delete[] type; + } + object = new Uri(env, input); } else { napi_throw_error(env, nullptr, "parameter type is error"); } NAPI_CALL(env, napi_wrap(env, thisVar, object, [](napi_env env, void *data, void *hint) { - auto object = (Uri*)data; - if (object != nullptr) { - delete object; + auto obj = (Uri*)data; + if (obj != nullptr) { + delete obj; } }, nullptr, nullptr)); return thisVar; @@ -62,11 +67,9 @@ namespace OHOS::Uri { Uri *muri = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); std::string normalizeUri = muri->Normalize(); - size_t argc = 1; - napi_value args[1] = { 0 }; napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), normalizeUri.size(), args)); - NAPI_CALL(env, napi_new_instance(env, g_uriClass, argc, args, &result)); + size_t tempLen = normalizeUri.size(); + NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), tempLen, &result)); return result; } @@ -264,8 +267,7 @@ namespace OHOS::Uri { DECLARE_NAPI_GETTER("isFailed", IsFailed), }; NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor, - nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); - g_uriClass = uriClass; + nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); static napi_property_descriptor desc[] = { DECLARE_NAPI_PROPERTY("Uri", uriClass) }; diff --git a/uri/tsconfig.json b/uri/tsconfig.json deleted file mode 100644 index f8212040..00000000 --- a/uri/tsconfig.json +++ /dev/null @@ -1,14 +0,0 @@ -{ -"compilerOptions": { - "target": "es6", - "module": "es6", - "rootDir": "./src", - //"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/ohos-arm-release/obj/base/compileruntime/js_api_module/uri/", /* Specify an output folder for all emitted files. */ - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - "skipLibCheck": true, - "noImplicitThis": false, - } -} diff --git a/url/BUILD.gn b/url/BUILD.gn index 920d024a..11b32a4d 100755 --- a/url/BUILD.gn +++ b/url/BUILD.gn @@ -32,7 +32,7 @@ action("gen_url_abc") { "--node-modules", rebase_path("${node_modules}"), ] - deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build"] + deps = [ "//ark/ts2abc/ts2panda:ark_ts2abc_build" ] inputs = [ "//base/compileruntime/js_api_module/url/js_url.js" ] outputs = [ target_out_dir + "/url.abc" ] @@ -68,6 +68,7 @@ ohos_shared_library("url") { deps = [ ":js_url", + ":url_abc", "//base/compileruntime/js_api_module/url/:js_url", "//foundation/ace/napi/:ace_napi", "//foundation/ace/napi/:ace_napi_quickjs", diff --git a/url/native_module_url.cpp b/url/native_module_url.cpp index e80ab108..57fc077d 100755 --- a/url/native_module_url.cpp +++ b/url/native_module_url.cpp @@ -31,8 +31,8 @@ namespace OHOS::Url { void *data = nullptr; napi_get_cb_info(env, info, &argc, nullptr, &thisVar, &data); napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - napi_valuetype valuetype1; - napi_valuetype valuetype2; + napi_valuetype valuetype1 = napi_null; + napi_valuetype valuetype2 = napi_null; std::string input = ""; napi_typeof(env, argv[0], &valuetype1); if (valuetype1 == napi_string) { @@ -82,7 +82,7 @@ namespace OHOS::Url { if (argc == 1) { std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); - napi_valuetype valuetype; + napi_valuetype valuetype = napi_null; NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); if (valuetype == napi_string) { char *type = nullptr; @@ -104,9 +104,9 @@ namespace OHOS::Url { napi_wrap( env, thisVar, object, [](napi_env env, void *data, void *hint) { - auto object = (URL*)data; - if (object != nullptr) { - delete object; + auto obj = (URL*)data; + if (obj != nullptr) { + delete obj; } }, nullptr, nullptr); @@ -886,7 +886,7 @@ namespace OHOS::Url { DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6), }; NAPI_CALL(env, napi_define_class(env, urlClassName, strlen(urlClassName), UrlConstructor, - nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass)); + nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass)); static napi_property_descriptor desc[] = { DECLARE_NAPI_PROPERTY("Url", urlClass) }; @@ -926,7 +926,6 @@ namespace OHOS::Url { *buflen = _binary_url_abc_end - _binary_url_abc_start; } } - static napi_module UrlModule = { .nm_version = 1, .nm_flags = 0, -- Gitee