modify js_api_module

Signed-off-by: lifansheng <lifansheng1@huawei.com>
This commit is contained in:
lifansheng
2021-09-17 10:27:58 +08:00
parent 5ca6e92f3c
commit bee85b0646
12 changed files with 498 additions and 369 deletions
+1
View File
@@ -202,6 +202,7 @@ void ConvertXml::SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1)
napi_set_element(env_, recvElement, index1++, m_prevObj[i]);
}
}
}
void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag)
+2 -1
View File
@@ -7,7 +7,8 @@
"phone"
],
"module_list": [
"//base/compileruntime/js_api_module/api:api_packages",
"//base/compileruntime/js_api_module/uri:uri_packages",
"//base/compileruntime/js_api_module/url:url_packages",
"//base/compileruntime/js_api_module/convertxml:convertxml_packages"
],
"inner_kits": [
+13 -13
View File
@@ -14,30 +14,29 @@
import("//build/ohos.gni")
import("//build/ohos/ace/ace.gni")
base_output_path = get_label_info(":js_api", "target_out_dir")
js_api_obj_path = base_output_path + "/api.o"
gen_js_obj("js_api") {
input = "//base/compileruntime/js_api_module/api/js_api.js"
output = js_api_obj_path
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("api") {
ohos_shared_library("uri") {
include_dirs = [
"//third_party/icu/icu4c/source/common",
"//third_party/node/src",
"//foundation/ace/napi/interfaces/kits",
"//base/compileruntime/js_api_module/api",
"//base/compileruntime/js_api_module/uri",
]
sources = [
"js_uri.cpp",
"js_url.cpp",
"native_module_api.cpp",
"native_module_uri.cpp",
]
deps = [
":js_api",
"//base/compileruntime/js_api_module/api/:js_api",
":js_uri",
"//base/compileruntime/js_api_module/uri/:js_uri",
"//foundation/ace/napi/:ace_napi",
"//foundation/ace/napi/:ace_napi_quickjs",
"//third_party/icu/icu4c:static_icuuc",
@@ -55,6 +54,7 @@ ohos_shared_library("api") {
relative_install_dir = "module"
}
group("api_packages") {
deps = [ ":api" ]
group("uri_packages") {
deps = [ ":uri" ]
}
+6 -5
View File
@@ -14,7 +14,7 @@
*/
#include "js_uri.h"
#include "utils/log.h"
namespace OHOS::Api {
namespace OHOS::Uri {
std::bitset <MAX_BIT_SIZE> g_ruleAlpha;
std::bitset <MAX_BIT_SIZE> g_ruleScheme;
std::bitset <MAX_BIT_SIZE> g_ruleUrlc;
@@ -86,9 +86,10 @@ namespace OHOS::Api {
if ((pos != std::string::npos) && (pos != 0)) {
AnalysisFragment(pos);
if (!errStr_.empty()) {
return;
return;
}
}
if ((pos != std::string::npos) && (pos == 0))
if ((pos != std::string::npos) && (pos == 0)) {
errStr_ = "#It can't be the first";
return;
}
@@ -289,10 +290,10 @@ namespace OHOS::Api {
bool Uri::AnalysisPort(size_t pos)
{
std::string port = data_.substr(pos + 1);
if (!CheckCharacter(port, g_rulePort, true)) { // 存在非规则内字符
if (!CheckCharacter(port, g_rulePort, true)) {
errStr_ = "port does not conform to the rule";
return false;
} else if (CheckCharacter(port, g_ruleDigit, false)) { // 纯数字
} else if (CheckCharacter(port, g_ruleDigit, false)) {
uriData_.port = std::stoi(port);
data_ = data_.substr(0, pos);
return true;
+1 -1
View File
@@ -24,7 +24,7 @@
#include <cstdlib>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
namespace OHOS::Api {
namespace OHOS::Uri {
constexpr int MAX_BIT_SIZE = 128;
struct uri_data {
int port = -1;
Executable
+100
View File
@@ -0,0 +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() {
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) {
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,
}
+299
View File
@@ -0,0 +1,299 @@
/*
* 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 "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[];
namespace OHOS::Uri {
napi_value g_uriClass = nullptr;
static napi_value UriConstructor(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
void *data = nullptr;
size_t argc = 1;
napi_value argv[1] = { 0 };
Uri *object = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
napi_valuetype valuetype;
NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
if (valuetype == napi_string) {
char *type = nullptr;
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;
} 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;
}
}, nullptr, nullptr));
return thisVar;
}
static napi_value Normalize(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
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));
return result;
}
static napi_value Equals(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
size_t argc = 1;
napi_value argv[1] = { 0 };
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
Uri *other = nullptr;
NAPI_CALL(env, napi_unwrap(env, argv[0], (void**)&other));
bool flag = muri->Equals(*other);
NAPI_CALL(env, napi_get_boolean(env, flag, &result));
return result;
}
static napi_value IsAbsolute(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
bool flag = muri->IsAbsolute();
NAPI_CALL(env, napi_get_boolean(env, flag, &result));
return result;
}
static napi_value IsFailed(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->IsFailed();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value UriToString(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->ToString();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetScheme(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetScheme();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetAuthority(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetAuthority();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetSsp(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetSsp();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetUserinfo(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetUserinfo();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetHost(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetHost();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetPort(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetPort();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetPath(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetPath();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetQuery(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetQuery();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetFragment(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetFragment();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value UriInit(napi_env env, napi_value exports)
{
const char *uriClassName = "uri";
napi_value uriClass = nullptr;
static napi_property_descriptor uriDesc[] = {
DECLARE_NAPI_FUNCTION("normalize", Normalize),
DECLARE_NAPI_FUNCTION("equals", Equals),
DECLARE_NAPI_FUNCTION("isAbsolute", IsAbsolute),
DECLARE_NAPI_FUNCTION("toString", UriToString),
DECLARE_NAPI_GETTER("scheme", GetScheme),
DECLARE_NAPI_GETTER("authority", GetAuthority),
DECLARE_NAPI_GETTER("ssp", GetSsp),
DECLARE_NAPI_GETTER("userinfo", GetUserinfo),
DECLARE_NAPI_GETTER("host", GetHost),
DECLARE_NAPI_GETTER("port", GetPort),
DECLARE_NAPI_GETTER("path", GetPath),
DECLARE_NAPI_GETTER("query", GetQuery),
DECLARE_NAPI_GETTER("fragment", GetFragment),
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;
static napi_property_descriptor desc[] = {
DECLARE_NAPI_PROPERTY("Uri", uriClass)
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
extern "C"
__attribute__((visibility("default"))) void NAPI_uri_GetJSCode(const char **buf, int *bufLen)
{
if (buf != nullptr) {
*buf = _binary_js_uri_js_start;
}
if (bufLen != nullptr) {
*bufLen = _binary_js_uri_js_end - _binary_js_uri_js_start;
}
}
static napi_module UriModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = UriInit,
.nm_modname = "uri",
.nm_priv = ((void*)0),
.reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterModule()
{
napi_module_register(&UriModule);
}
} // namespace
Executable
+59
View File
@@ -0,0 +1,59 @@
# 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("//build/ohos.gni")
import("//build/ohos/ace/ace.gni")
base_output_path = get_label_info(":js_url", "target_out_dir")
js_url_obj_path = base_output_path + "/url.o"
gen_js_obj("js_url") {
input = "//base/compileruntime/js_api_module/url/js_url.js"
output = js_url_obj_path
}
ohos_shared_library("url") {
include_dirs = [
"//third_party/icu/icu4c/source/common",
"//third_party/node/src",
"//foundation/ace/napi/interfaces/kits",
"//base/compileruntime/js_api_module/url",
]
sources = [
"js_url.cpp",
"native_module_url.cpp",
]
deps = [
":js_url",
"//base/compileruntime/js_api_module/url/:js_url",
"//foundation/ace/napi/:ace_napi",
"//foundation/ace/napi/:ace_napi_quickjs",
"//third_party/icu/icu4c:static_icuuc",
"//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_api"
relative_install_dir = "module"
}
group("url_packages") {
deps = [ ":url" ]
}
+1 -1
View File
@@ -18,7 +18,7 @@
#include <sstream>
#include "securec.h"
#include "utils/log.h"
namespace OHOS::Api {
namespace OHOS::Url {
static std::map<std::string, int> g_head = {
{"ftp:", 21}, {"file:", -1}, {"gopher:", 70}, {"http:", 80},
{"https:", 443}, {"ws:", 80}, {"wss:", 443}
+1 -1
View File
@@ -25,7 +25,7 @@
#include <vector>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
namespace OHOS::Api {
namespace OHOS::Url {
enum class BitsetStatusFlag {
BIT0 = 0, // 0:Bit 0 Set to true,The URL analysis failed
BIT1 = 1, // 1:Bit 1 Set to true,The protocol is the default protocol
+6 -86
View File
@@ -14,86 +14,7 @@
*/
'use strict';
const api = requireInternal("api");
class URI {
constructor(input) {
if (typeof input !== 'string' || input.length === 0) {
throw new Error("input type err");
}
this.uricalss = new api.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() {
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) {
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);
}
}
const Url = requireInternal("url");
let seachParamsArr = [];
class URLSearchParams {
@@ -101,7 +22,7 @@ class URLSearchParams {
constructor(input) {
let out = [];
out = parameterProcessing(input);
this.urlcalss = new api.URLSearchParams1();
this.urlcalss = new Url.URLSearchParams1();
this.urlcalss.array = out;
}
append(params1, params2) {
@@ -224,7 +145,7 @@ function initToStringSeachParams(input) {
input = input.slice(1);
}
let strVal = decodeURI(input);
seachParamsArr = api.stringParmas(strVal);
seachParamsArr = Url.stringParmas(strVal);
return seachParamsArr;
}
class URL {
@@ -249,7 +170,7 @@ class URL {
if (arguments.length === 1) {
inputUrl = arguments[0];
if (typeof inputUrl === 'string' && inputUrl.length > 0) {
nativeUrl = new api.Url(inputUrl);
nativeUrl = new Url.Url(inputUrl);
} else {
console.log('Input parameter error');
}
@@ -261,7 +182,7 @@ class URL {
if (typeof inputUrl === 'string') {
if (typeof inputBase === 'string') {
if (inputBase.length > 0) {
nativeUrl = new api.Url(inputUrl, inputBase);
nativeUrl = new Url.Url(inputUrl, inputBase);
} else {
console.log('Input parameter error');
return;
@@ -269,7 +190,7 @@ class URL {
}
if (typeof inputBase === 'object') {
let nativeBase = inputBase.getInfo();
nativeUrl = new api.Url(inputUrl, nativeBase);
nativeUrl = new Url.Url(inputUrl, nativeBase);
}
}
}
@@ -490,7 +411,6 @@ class URL {
}
export default {
URI: URI,
URLSearchParams: URLSearchParams,
URL: URL,
}
@@ -15,233 +15,12 @@
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "js_uri.h"
#include "js_url.h"
#include "utils/log.h"
extern const char _binary_js_api_js_start[];
extern const char _binary_js_api_js_end[];
namespace OHOS::Api {
napi_value g_uriClass = nullptr;
static napi_value UriConstructor(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
void *data = nullptr;
size_t argc = 1;
napi_value argv[1] = { 0 };
Uri *object = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
napi_valuetype valuetype;
NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
if (valuetype == napi_string) {
char *type = nullptr;
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;
} 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;
}
}, nullptr, nullptr));
return thisVar;
}
static napi_value Normalize(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
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));
return result;
}
static napi_value Equals(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
size_t argc = 1;
napi_value argv[1] = { 0 };
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
Uri *other = nullptr;
NAPI_CALL(env, napi_unwrap(env, argv[0], (void**)&other));
bool flag = muri->Equals(*other);
NAPI_CALL(env, napi_get_boolean(env, flag, &result));
return result;
}
static napi_value IsAbsolute(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
bool flag = muri->IsAbsolute();
NAPI_CALL(env, napi_get_boolean(env, flag, &result));
return result;
}
static napi_value IsFailed(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->IsFailed();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value UriToString(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->ToString();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetScheme(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetScheme();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetAuthority(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetAuthority();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetSsp(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetSsp();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetUserinfo(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetUserinfo();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetHost(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetHost();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetPort(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetPort();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetPath(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetPath();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetQuery(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetQuery();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
static napi_value GetFragment(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value result = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
Uri *muri = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri));
std::string temp = muri->GetFragment();
size_t templen = temp.size();
NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result));
return result;
}
extern const char _binary_js_url_js_start[];
extern const char _binary_js_url_js_end[];
namespace OHOS::Url {
static void UrlStructor(napi_env &env, napi_callback_info &info, URL *&object)
{
napi_value thisVar = nullptr;
@@ -1113,36 +892,6 @@ namespace OHOS::Api {
return exports;
}
static napi_value UriInit(napi_env env, napi_value exports)
{
const char *uriClassName = "uri";
napi_value uriClass = nullptr;
static napi_property_descriptor uriDesc[] = {
DECLARE_NAPI_FUNCTION("normalize", Normalize),
DECLARE_NAPI_FUNCTION("equals", Equals),
DECLARE_NAPI_FUNCTION("isAbsolute", IsAbsolute),
DECLARE_NAPI_FUNCTION("toString", UriToString),
DECLARE_NAPI_GETTER("scheme", GetScheme),
DECLARE_NAPI_GETTER("authority", GetAuthority),
DECLARE_NAPI_GETTER("ssp", GetSsp),
DECLARE_NAPI_GETTER("userinfo", GetUserinfo),
DECLARE_NAPI_GETTER("host", GetHost),
DECLARE_NAPI_GETTER("port", GetPort),
DECLARE_NAPI_GETTER("path", GetPath),
DECLARE_NAPI_GETTER("query", GetQuery),
DECLARE_NAPI_GETTER("fragment", GetFragment),
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;
static napi_property_descriptor desc[] = {
DECLARE_NAPI_PROPERTY("Uri", uriClass)
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
@@ -1151,33 +900,32 @@ namespace OHOS::Api {
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
SeachParamsInit(env, exports);
UrlInit(env, exports);
UriInit(env, exports);
return exports;
}
extern "C"
__attribute__((visibility("default"))) void NAPI_api_GetJSCode(const char **buf, int *bufLen)
__attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char **buf, int *bufLen)
{
if (buf != nullptr) {
*buf = _binary_js_api_js_start;
*buf = _binary_js_url_js_start;
}
if (bufLen != nullptr) {
*bufLen = _binary_js_api_js_end - _binary_js_api_js_start;
*bufLen = _binary_js_url_js_end - _binary_js_url_js_start;
}
}
static napi_module ApiModule = {
static napi_module UrlModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "api",
.nm_modname = "url",
.nm_priv = ((void*)0),
.reserved = {0},
};
extern "C" __attribute__((constructor)) void RegisterModule()
{
napi_module_register(&ApiModule);
napi_module_register(&UrlModule);
}
} // namespace