Merge pull request !12 from clearme777/master
This commit is contained in:
openharmony_ci
2021-09-10 08:30:22 +00:00
committed by Gitee
5 changed files with 1402 additions and 1235 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{
"subsystem": "ccruntime",
"parts": {
"jsapi_api": {
"jsapi_url": {
"variants": [
"wearable",
"phone"
+938 -799
View File
File diff suppressed because it is too large Load Diff
+44 -29
View File
@@ -12,34 +12,47 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <vector>
#include <iostream>
#include <bitset>
#include <regex>
#include <ctype.h>
#ifndef COMPILERUNTIME_JS_API_URL_H
#define COMPILERUNTIME_JS_API_URL_H
#include <algorithm>
#include <map>
#include <cstdlib>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <map>
#include <string>
#include <vector>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
struct url_data {
int port = -1;
std::string scheme;
std::string username;
std::string password;
std::string host;
std::string query;
std::string fragment;
std::vector<std::string> path;
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
BIT2 = 2, // 2:Bit 2 Set to true,The URL has username
BIT3 = 3, // 3:Bit 3 Set to true,The URL has password
BIT4 = 4, // 4:Bit 4 Set to true,The URL has hostname
BIT5 = 5, // 5:Bit 5 Set to true,The URL Port is the specially
BIT6 = 6, // 6:Bit 6 Set to true,The URL has pathname
BIT7 = 7, // 7:Bit 7 Set to true,The URL has query
BIT8 = 8, // 8:Bit 8 Set to true,The URL has fragment
BIT9 = 9, // 9:Bit 9 Set to true,The URL Can not be base
BIT10 = 10, // 10:Bit 10 Set to true,The host is IPV6
BIT_STATUS_11 = 11 // 11:Each bit of a BIT represents a different parsing state.
};
class URL
{
struct UrlData {
int port = -1;
std::vector<std::string> path;
std::string password = "";
std::string scheme = "";
std::string query = "";
std::string username = "";
std::string fragment = "";
std::string host = "";
};
class URL {
public:
URL(napi_env env, const std::string& input);
URL(napi_env env, const std::string& input, const std::string& base);
@@ -68,37 +81,39 @@ public:
napi_value GetIsIpv6() const;
napi_value GetHost() const;
static void InitOnlyInput(std::string& input, url_data& urlData, std::bitset<11>& flags);
static void InitOnlyInput(std::string& input, UrlData& urlData,
std::bitset<static_cast<size_t>(BitsetStatusFlag::BIT_STATUS_11)>& flags);
virtual ~URL() {}
private:
url_data urlData_;
std::bitset<11> flags_;
UrlData urlData_;
std::bitset<static_cast<size_t>(BitsetStatusFlag::BIT_STATUS_11)> flags_;
// bitset<11>:Similar to bool array, each bit status represents the real-time status of current URL parsing
napi_env env_ = nullptr;
};
class URLSearchParams {
public:
URLSearchParams(napi_env env);
explicit URLSearchParams(napi_env env);
virtual ~URLSearchParams() {}
napi_value IsHas(napi_value name);
napi_value IsHas(napi_value name) const;
napi_value Get(napi_value buffer);
napi_value GetAll(napi_value buffer);
void Append(napi_value buffer, napi_value temp);
void Delete(napi_value buffer);
void ForEach(napi_value function, napi_value thisVar);
napi_value Entries();
napi_value Entries() const;
void Set(napi_value name, napi_value value);
void Sort();
napi_value ToString();
napi_value IterByKeys();
napi_value IterByValues();
void SetArray(std::vector<std::string> input);
napi_value GetArray();
std::vector<std::string> StringParmas (std::string Stringpar);
napi_value GetArray() const;
std::vector<std::string> StringParmas(std::string Stringpar);
private:
std::string ToUSVString(std::string inputStr);
void HandleIllegalChar(std::wstring& inputStr, std::wstring::const_iterator it);
std::vector<std::string> searchParams;
napi_env env;
};
};
#endif /* COMPILERUNTIME_JS_API_URL_H */
+173 -237
View File
@@ -14,7 +14,7 @@
*/
'use strict';
const urlUtil = requireInternal("url");
const urlUtil = requireInternal('url');
let seachParamsArr = [];
class URLSearchParams {
@@ -40,7 +40,6 @@ class URLSearchParams {
has(hasname) {
return this.urlcalss.has(hasname);
}
toString() {
return this.urlcalss.toString();
}
@@ -50,6 +49,7 @@ class URLSearchParams {
}
values() {
console.log('constructor failed');
return this.urlcalss.values();
}
@@ -84,7 +84,6 @@ class URLSearchParams {
this.urlcalss.array = out;
}
}
function toHleString(arg) {
return arg.toString();
}
@@ -99,38 +98,36 @@ function parameterProcessing(input) {
return initToStringSeachParams(input);
}
}
function initObjectSeachParams(input) {
if (typeof input[Symbol.iterator] === 'function') {
return iteratorMethod(input);
}
return recordMethod(input);
}
function recordMethod(input) {
const keys = Reflect.ownKeys(input);
const objectkeys = Reflect.ownKeys(input);
seachParamsArr = [];
for (let i = 0; i <= keys.length; i++) {
const key = keys[i];
const desc = Reflect.getOwnPropertyDescriptor(input, key);
if (desc !== undefined && desc.enumerable) {
const typedKey = toHleString(key);
const typedValue = toHleString(input[key]);
seachParamsArr.push(typedKey, typedValue);
let objectlength = objectkeys.length;
for (let i = 0; i <= objectlength; i++) {
const objectkey = objectkeys[i];
const descry = Reflect.getOwnPropertyDescriptor(input, objectkey);
if (descry !== undefined && descry.enumerable) {
const inputkey = toHleString(objectkey);
const inputValue = toHleString(input[objectkey]);
seachParamsArr.push(inputkey, inputValue);
}
}
return seachParamsArr;
return seachParamsArr;
}
function iteratorMethod(input) {
let pairs = [];
seachParamsArr = [];
for (const pair of input) {
const convertedPair = [];
const conversionsPair = [];
for (let element of pair) {
convertedPair.push(element);
conversionsPair.push(element);
}
pairs.push(convertedPair);
pairs.push(conversionsPair);
}
for (const pair of pairs) {
@@ -150,7 +147,6 @@ function initToStringSeachParams(input) {
seachParamsArr = urlUtil.stringParmas(strVal);
return seachParamsArr;
}
class URL {
href_;
search_;
@@ -192,7 +188,7 @@ class URL {
}
}
if (typeof inputBase === 'object') {
let nativeBase = inputBase.get_info();
let nativeBase = inputBase.getInfo();
nativeUrl = new urlUtil.Url(inputUrl, nativeBase);
}
}
@@ -220,9 +216,165 @@ class URL {
console.log('constructor failed');
}
}
get_info() {
getInfo() {
return this.c_info;
}
toString() {
return this.href_;
}
get protocol() {
return this.protocol_;
}
set protocol(scheme) {
if (scheme.length === 0) {
return;
}
if (this.protocol_ === 'file:'
&& (this.host_ === '' || this.host_ == null)) {
return;
}
this.c_info.protocol = scheme;
this.protocol_ = this.c_info.protocol;
this.set_href()
}
get origin() {
let kOpaqueOrigin = 'null';
switch (this.protocol_) {
case 'ftp:':
case 'gopher:':
case 'http:':
case 'https:':
case 'ws:':
case 'wss:':
return this.origin_;
}
return kOpaqueOrigin;
}
get username() {
return this.username_;
}
set username(input) {
if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') {
return;
}
const usname_ = escape(input);
this.c_info.username = usname_;
this.username_ = this.c_info.username;
this.set_href();
}
get password() {
return this.password_;
}
set password(input) {
if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') {
return;
}
const passwd_ = escape(input);
this.c_info.password = passwd_;
this.password_ = this.c_info.password;
this.set_href();
}
get hash() {
return this.hash_;
}
set hash(fragment) {
const fragment_ = encodeURI(fragment);
this.c_info.hash = fragment_;
this.hash_ = this.c_info.hash;
this.set_href();
}
get search() {
return this.search_;
}
set search(query) {
const query_ = encodeURI(query);
this.c_info.search = query_;
this.search_ = this.c_info.search;
this.searchParamsClass_.updateParams(this.search_);
this.set_href();
}
get hostname() {
return this.hostname_;
}
set hostname(hostname) {
this.c_info.hostname = hostname;
if (this.c_info.GetIsIpv6) {
this.hostname_ = this.c_info.hostname;
} else {
this.hostname_ = encodeURI(this.c_info.hostname);
}
this.set_href();
}
get host() {
return this.host_;
}
set host(host_) {
this.c_info.host = host_;
if (this.c_info.GetIsIpv6) {
this.host_ = this.c_info.host;
this.hostname_ = this.c_info.hostname;
this.port_ = this.c_info.port;
} else {
this.host_ = encodeURI(this.c_info.host);
this.hostname_ = encodeURI(this.c_info.hostname);
this.port_ = this.c_info.port;
}
this.set_href();
}
get port() {
return this.port_;
}
set port(port) {
if (this.host_ === '' || this.protocol_ === 'file:' || port === '') {
return;
}
this.c_info.port = port;
this.port_ = this.c_info.port;
this.set_href();
}
get href() {
return this.href_;
}
set href(href_) {
this.c_info.href(href_);
if (this.c_info.onOrOff) {
this.search_ = encodeURI(this.c_info.search);
this.username_ = encodeURI(this.c_info.username);
this.password_ = encodeURI(this.c_info.password);
if (this.c_info.GetIsIpv6) {
this.hostname_ = this.c_info.hostname;
this.host_ = this.c_info.host;
} else {
this.hostname_ = encodeURI(this.c_info.hostname);
this.host_ = encodeURI(this.c_info.host);
}
this.hash_ = encodeURI(this.c_info.hash);
this.protocol_ = encodeURI(this.c_info.protocol);
this.pathname_ = encodeURI(this.c_info.pathname);
this.port_ = this.c_info.port;
this.origin_ = this.protocol_ + '//' + this.host_;
this.searchParamsClass_.updateParams(this.search_);
this.set_href();
}
}
get pathname() {
return this.pathname_;
}
set pathname(path) {
const path_ = encodeURI(path);
this.c_info.pathname = path_;
this.pathname_ = this.c_info.pathname;
this.set_href();
}
get searchParams() {
return this.searchParamsClass_;
}
toJSON() {
return this.href_;
}
set_href() {
let temp = this.protocol_;
if (this.hostname_ !== '') {
@@ -256,222 +408,6 @@ class URL {
}
}
Object.defineProperties(URL.prototype, {
to_string: {
writable: true,
enumerable: true,
configurable: true,
value: function toString() {
return this.href_;
}
},
origin: {
enumerable: true,
configurable: true,
get() {
let kOpaqueOrigin = 'null';
switch (this.protocol_) {
case 'ftp:':
case 'gopher:':
case 'http:':
case 'https:':
case 'ws:':
case 'wss:':
return this.origin_;
}
return kOpaqueOrigin;
}
},
protocol: {
enumerable: true,
configurable: true,
get() {
return this.protocol_;
},
set(scheme) {
if (scheme.length === 0) {
return;
}
if (this.protocol_ === 'file:'
&& (this.host_ === '' || this.host_ == null)) {
return;
}
this.c_info.protocol = scheme;
this.protocol_ = this.c_info.protocol;
this.set_href();
}
},
username: {
enumerable: true,
configurable: true,
get() {
return this.username_;
},
set(input) {
if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') {
return;
}
const usname_ = escape(input);
this.c_info.username = usname_;
this.username_ = this.c_info.username;
this.set_href();
}
},
password: {
enumerable: true,
configurable: true,
get() {
return this.password_;
},
set(input) {
if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') {
return;
}
const passwd_ = escape(input);
this.c_info.password = passwd_;
this.password_ = this.c_info.password;
this.set_href();
}
},
hash: {
enumerable: true,
configurable: true,
get() {
return this.hash_;
},
set(fragment) {
const fragment_ = encodeURI(fragment);
this.c_info.hash = fragment_;
this.hash_ = this.c_info.hash;
this.set_href();
}
},
search: {
enumerable: true,
configurable: true,
get() {
return this.search_;
},
set(query) {
const query_ = encodeURI(query);
this.c_info.search = query_;
this.search_ = this.c_info.search;
this.searchParamsClass_.updateParams(this.search_);
this.set_href();
}
},
hostname: {
enumerable: true,
configurable: true,
get() {
return this.hostname_;
},
set(hostname) {
this.c_info.hostname = hostname;
if (this.c_info.GetIsIpv6) {
this.hostname_ = this.c_info.hostname;
} else {
this.hostname_ = encodeURI(this.c_info.hostname);
}
this.set_href();
}
},
host: {
enumerable: true,
configurable: true,
get() {
return this.host_;
},
set(host_) {
this.c_info.host = host_;
if (this.c_info.GetIsIpv6) {
this.host_ = this.c_info.host;
this.hostname_ = this.c_info.hostname;
this.port_ = this.c_info.port;
} else {
this.host_ = encodeURI(this.c_info.host);
this.hostname_ = encodeURI(this.c_info.hostname);
this.port_ = this.c_info.port;
}
this.set_href();
}
},
port: {
enumerable: true,
configurable: true,
get() {
return this.port_;
},
set(port) {
if (this.host_ === '' || this.protocol_ === 'file:' || port === '') {
return;
}
this.c_info.port = port;
this.port_ = this.c_info.port;
this.set_href();
}
},
href: {
enumerable: true,
configurable: true,
get() {
return this.href_;
},
set(href_) {
this.c_info.href(href_);
if (this.c_info.onOrOff) {
this.search_ = encodeURI(this.c_info.search);
this.username_ = encodeURI(this.c_info.username);
this.password_ = encodeURI(this.c_info.password);
if (this.c_info.GetIsIpv6) {
this.hostname_ = this.c_info.hostname;
this.host_ = this.c_info.host;
} else {
this.hostname_ = encodeURI(this.c_info.hostname);
this.host_ = encodeURI(this.c_info.host);
}
this.hash_ = encodeURI(this.c_info.hash);
this.protocol_ = encodeURI(this.c_info.protocol);
this.pathname_ = encodeURI(this.c_info.pathname);
this.port_ = this.c_info.port;
this.origin_ = this.protocol_ + '//' + this.host_;
this.searchParamsClass_.updateParams(this.search_);
this.set_href();
}
return this.href_;
}
},
pathname: {
enumerable: true,
configurable: true,
get() {
return this.pathname_;
},
set(path) {
const path_ = encodeURI(path);
this.c_info.pathname = path_;
this.pathname_ = this.c_info.pathname;
this.set_href();
}
},
toJSON: {
writable: true,
enumerable: true,
configurable: true,
value: function toJSON() {
return this.href_;
}
},
searchParams: {
enumerable: true,
configurable: true,
get() {
return this.searchParamsClass_;
}
}
});
export default {
URLSearchParams: URLSearchParams,
URL: URL,
+246 -169
View File
@@ -1,4 +1,4 @@
/*
/*
* 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.
@@ -12,14 +12,62 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utils/log.h"
#include "js_url.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "js_url.h"
#include "utils/log.h"
extern const char _binary_js_url_js_start[];
extern const char _binary_js_url_js_end[];
static void UrlStructor(napi_env &env, napi_callback_info &info, URL* &object)
{
napi_value thisVar = nullptr;
size_t argc = 2;
napi_value argv[2] = { 0 };
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;
std::string input = "";
napi_typeof(env, argv[0], &valuetype1);
if (valuetype1 == napi_string) {
char* tem = nullptr;
size_t temlen = 0;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &temlen);
if (temlen > 0) {
tem = new char[temlen + 1];
napi_get_value_string_utf8(env, argv[0], tem, temlen + 1, &temlen);
input = tem;
delete[] tem;
}
napi_typeof(env, argv[1], &valuetype2);
if (valuetype2 == napi_string) {
std::string base = "";
char* type1 = nullptr;
size_t typelen1 = 0;
napi_get_value_string_utf8(env, argv[1], nullptr, 0, &typelen1);
if (typelen1 > 0) {
type1 = new char[typelen1 + 1];
napi_get_value_string_utf8(env, argv[1], type1, typelen1 + 1, &typelen1);
base = type1;
delete[] type1;
}
object = new URL(env, input, base);
} else if (valuetype2 == napi_object) {
URL* temp = nullptr;
napi_unwrap(env, argv[1], (void**)&temp);
object = new URL(env, input, *temp);
} else {
HILOG_INFO("secondParameter error");
}
} else {
HILOG_INFO("firstParameter error");
}
return;
}
static napi_value UrlConstructor(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
@@ -29,62 +77,37 @@ static napi_value UrlConstructor(napi_env env, napi_callback_info info)
URL* object = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, &data));
if (argc == 1) {
std::string input = "";
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));
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 URL(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 URL(env, input);
} else {
HILOG_INFO("Parameter error");
}
} else if (argc == 2) {
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
napi_valuetype valuetype1;
napi_valuetype valuetype2;
NAPI_CALL(env,napi_typeof(env, argv[0], &valuetype1));
if (valuetype1 == napi_string) {
char* tem = nullptr;
size_t temlen = 0;
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &temlen));
tem = new char[temlen+1];
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], tem, temlen + 1, &temlen));
std::string input_(tem);
delete[] tem;
NAPI_CALL(env,napi_typeof(env, argv[1], &valuetype2));
if (valuetype2 == napi_string){
char* type1 = nullptr;
size_t typelen1 = 0;
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], nullptr, 0, &typelen1));
type1 = new char[typelen1 + 1];
NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], type1, typelen1 + 1, &typelen1));
std::string base_(type1);
delete[] type1;
object = new URL(env, input_, base_);
} else if (valuetype2 == napi_object) {
URL * temp = nullptr;
NAPI_CALL(env, napi_unwrap(env, argv[1], (void**)&temp));
object = new URL(env,input_,*temp);
} else {
HILOG_INFO("secondParameter error");
}
} else {
HILOG_INFO("firstParameter error");
}
} else if (argc == 2) { // 2:When the input parameter is set to 2
UrlStructor(env, info, object);
}
NAPI_CALL(env, napi_wrap(env, thisVar, object,
[](napi_env env, void* data, void* hint) {
auto object = (URL*)data;
if (object != nullptr) {
delete object;
}
}, nullptr, nullptr));
return thisVar;
napi_wrap(
env, thisVar, object,
[](napi_env env, void* data, void* hint) {
auto object = (URL*)data;
if (object != nullptr) {
delete object;
}
},
nullptr, nullptr);
return thisVar;
}
static napi_value GetHostname(napi_env env, napi_callback_info info)
@@ -202,13 +225,16 @@ static napi_value SetHref(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -226,13 +252,16 @@ static napi_value SetHostname(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -250,13 +279,16 @@ static napi_value SetPort(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -274,13 +306,16 @@ static napi_value SetHost(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -298,13 +333,16 @@ static napi_value SetSearch(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -322,13 +360,16 @@ static napi_value SetScheme(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -346,13 +387,16 @@ static napi_value SetFragment(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -370,13 +414,16 @@ static napi_value SetUsername(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -394,13 +441,16 @@ static napi_value SetPath(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -418,13 +468,16 @@ static napi_value SetPassword(napi_env env, napi_callback_info info)
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
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));
std::string input(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;
}
if (type != nullptr) {
delete[] type;
type = nullptr;
@@ -443,7 +496,7 @@ static napi_value SeachParamsConstructor(napi_env env, napi_callback_info info)
void* data = nullptr;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data));
auto object = new URLSearchParams(env);
NAPI_CALL(env, napi_wrap(
napi_wrap(
env, thisVar, object,
[](napi_env env, void* data, void* hint) {
auto object = (URLSearchParams*)data;
@@ -451,30 +504,34 @@ static napi_value SeachParamsConstructor(napi_env env, napi_callback_info info)
delete object;
}
},
nullptr, nullptr));
nullptr, nullptr);
return thisVar;
}
static napi_value SetArray(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value argv[1];
napi_value argv[1] = {0};
size_t argc = 1;
uint32_t length = 0;
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
napi_get_array_length(env, argv[0], &length);
std::vector<std::string> vec;
char* cstr = nullptr;
size_t arraySize = 0;
napi_value napiStr = nullptr;
for (size_t i = 0; i < length; i++) {
char* cstr = nullptr;
napi_get_element(env, argv[0], i, &napiStr);
napi_get_value_string_utf8(env, napiStr, nullptr, 0, &arraySize);
cstr = new char[arraySize + 1];
napi_get_value_string_utf8(env, napiStr, cstr, arraySize + 1, &arraySize);
vec.push_back(cstr);
delete []cstr;
cstr = nullptr;
if (arraySize > 0) {
cstr = new char[arraySize + 1];
napi_get_value_string_utf8(env, napiStr, cstr, arraySize + 1, &arraySize);
vec.push_back(cstr);
delete []cstr;
cstr = nullptr;
} else {
vec.push_back("");
}
}
URLSearchParams* murl = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl));
@@ -500,8 +557,7 @@ static napi_value Get(napi_env env, napi_callback_info info)
size_t argc = 1;
napi_value args = nullptr;
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
if(argc != 1) {
if (argc != 1) {
HILOG_INFO("One arg needs to be specified");
return nullptr;
}
@@ -517,8 +573,7 @@ static napi_value GetAll(napi_env env, napi_callback_info info)
size_t argc = 1;
napi_value args = nullptr;
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
if(argc != 1) {
if (argc != 1) {
HILOG_INFO("One arg needs to be specified");
return nullptr;
}
@@ -535,8 +590,7 @@ static napi_value Append(napi_env env, napi_callback_info info)
napi_value args[2] = { 0 };
void* data = nullptr;
napi_get_cb_info(env, info, &argc, args, &thisVar, &data);
if(argc != 2) {
if (argc != 2) { // 2:If the input parameter is not set to 2,
HILOG_INFO("Two args needs to be specified");
return nullptr;
}
@@ -552,8 +606,7 @@ static napi_value Delete(napi_env env, napi_callback_info info)
size_t argc = 1;
napi_value args = nullptr;
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
if(argc != 1) {
if (argc != 1) {
HILOG_INFO("One arg needs to be specified");
return nullptr;
}
@@ -569,7 +622,6 @@ static napi_value ForEach(napi_env env, napi_callback_info info)
size_t argc = 1;
napi_value args = nullptr;
napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
URLSearchParams* object = nullptr;
napi_unwrap(env, thisVar, (void**)&object);
object->ForEach(args, thisVar);
@@ -660,91 +712,119 @@ static napi_value IterByValues(napi_env env, napi_callback_info info)
return result;
}
std::vector<std::string> stringParsing (std::string Stringpar)
static void IsPlusSign(size_t &strLastPos, size_t &iteaor, std::string &buf, std::string &stringParm)
{
if (strLastPos < iteaor) {
buf += stringParm.substr(strLastPos, iteaor - strLastPos);
}
buf += "";
strLastPos = iteaor + 1;
return;
}
static void IsEqualSign(size_t &strLastPos, size_t &iteaor,
std::string &buf, std::string &stringParm, std::vector<std::string> &seachParasVec)
{
if (strLastPos < iteaor) {
buf += stringParm.substr(strLastPos, iteaor - strLastPos);
}
seachParasVec.push_back(buf);
buf = "";
strLastPos = iteaor + 1;
return;
}
static void IsAddressSign(size_t &strLastPos, size_t &iteaor, std::string &buf,
std::string &stringParm, std::vector<std::string> &seachParasVec)
{
if (strLastPos < iteaor) {
buf += stringParm.substr(strLastPos, iteaor - strLastPos);
}
seachParasVec.push_back(buf);
return;
}
static void DealParmsString(size_t &strLastPos, size_t &iteaor, std::string &buf,
std::string &stringParm, std::vector<std::string> &seachParasVec)
{
if (strLastPos < iteaor) {
buf += stringParm.substr(strLastPos, iteaor - strLastPos);
}
seachParasVec.push_back(buf);
}
static void IsEqualCode(size_t &strStartPos, size_t &iteaor, size_t &strLastPos)
{
if (strStartPos == iteaor) {
strLastPos = iteaor + 1;
strStartPos = iteaor + 1;
}
return;
}
static std::vector<std::string> StringParsing(std::string stringParm)
{
std::vector<std::string> seachParasVec;
size_t strStartPos = 0;
size_t strLastPos = 0;
bool isHasSpace = false;
std::string buf = "";
size_t i;
for(i = 0;i < Stringpar.length(); i++) {
char code = Stringpar[i];
switch(code) {
case '&' :
size_t iteaor = 0;
for (iteaor = 0; iteaor < stringParm.length(); iteaor++) {
char code = stringParm[iteaor];
switch (code) {
case '&':
{
if (strStartPos == i){
strLastPos = i + 1;
strStartPos = i + 1;
break;
}
if (strLastPos < i) {
buf += Stringpar.substr(strLastPos, i - strLastPos);
}
seachParasVec.push_back(buf);
IsEqualCode(strStartPos, iteaor, strLastPos);
IsAddressSign(strLastPos, iteaor, buf, stringParm, seachParasVec);
if (!isHasSpace) {
seachParasVec.push_back("");
}
isHasSpace = false;
buf = "";
strLastPos = i + 1;
strStartPos = i + 1;
strLastPos = iteaor + 1;
strStartPos = iteaor + 1;
break;
}
case '=' :
case '=':
{
if (isHasSpace) {
break;
}
if (strLastPos < i) {
buf += Stringpar.substr(strLastPos, i- strLastPos);
}
seachParasVec.push_back(buf);
IsEqualSign(strLastPos, iteaor, buf, stringParm, seachParasVec);
isHasSpace = true;
buf = "";
strLastPos = i + 1;
break;
}
case '+' :
{
if (strLastPos < i) {
buf += Stringpar.substr(strLastPos, i - strLastPos);
}
buf += "";
strLastPos = i + 1;
break;
}
case '+':
IsPlusSign(strLastPos, iteaor, buf, stringParm);
break;
default:break;
}
}
if (strStartPos == i) {
if (strStartPos == iteaor) {
return seachParasVec;
}
if (strLastPos < i) {
buf += Stringpar.substr(strLastPos, i - strLastPos);
}
seachParasVec.push_back(buf);
DealParmsString(strLastPos, iteaor, buf, stringParm, seachParasVec);
if (!isHasSpace) {
seachParasVec.push_back("");
}
return seachParasVec;
}
static napi_value StringParmas(napi_env env, napi_callback_info info)
{
static napi_value StringParmas(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
napi_value argv[1] = {0};
size_t argc = 1;
std::string input = "";
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
char* type = nullptr;
size_t typelen = 0;
napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen);
type = new char[typelen+1];
napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen);
std::string input(type);
delete[] type;
if (typelen > 0) {
type = new char[typelen + 1];
napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen);
input = type;
delete[] type;
}
std::vector<std::string> seachParasmsString;
seachParasmsString = stringParsing(input);
seachParasmsString = StringParsing(input);
napi_value arr = nullptr;
napi_create_array(env, &arr);
for (size_t i = 0; i < seachParasmsString.size(); i++) {
@@ -757,10 +837,9 @@ static napi_value StringParmas(napi_env env, napi_callback_info info)
static napi_value SeachParamsInit(napi_env env, napi_value exports)
{
const char* SeachParamsClassName = "URLSearchParams";
napi_value SeachParamsInitClass = nullptr;
static napi_property_descriptor UrlDesc[] =
{
const char *seachParamsClassName = "URLSearchParams";
napi_value seachParamsInitClass = nullptr;
static napi_property_descriptor UrlDesc[] = {
DECLARE_NAPI_FUNCTION("has", IsHas),
DECLARE_NAPI_FUNCTION("set", Set),
DECLARE_NAPI_FUNCTION("sort", Sort),
@@ -775,39 +854,38 @@ static napi_value SeachParamsInit(napi_env env, napi_value exports)
DECLARE_NAPI_FUNCTION("entries", Entries),
DECLARE_NAPI_GETTER_SETTER("array", GetArray, SetArray),
};
NAPI_CALL(env, napi_define_class(env, SeachParamsClassName, strlen(SeachParamsClassName), SeachParamsConstructor,
nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &SeachParamsInitClass));
NAPI_CALL(env, napi_define_class(env, seachParamsClassName, strlen(seachParamsClassName), SeachParamsConstructor,
nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &seachParamsInitClass));
static napi_property_descriptor desc[] = {
DECLARE_NAPI_PROPERTY("URLSearchParams1", SeachParamsInitClass)
DECLARE_NAPI_PROPERTY("URLSearchParams1", seachParamsInitClass)
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
};
static napi_value UrlInit(napi_env env, napi_value exports)
{
const char* UrlClassName = "Url";
napi_value UrlClass = nullptr;
static napi_property_descriptor UrlDesc[] =
{
{
const char *urlClassName = "Url";
napi_value urlClass = nullptr;
static napi_property_descriptor UrlDesc[] = {
DECLARE_NAPI_GETTER_SETTER("hostname", GetHostname, SetHostname),
DECLARE_NAPI_FUNCTION("href", SetHref),
DECLARE_NAPI_GETTER_SETTER("search", GetSearch, SetSearch),
DECLARE_NAPI_GETTER_SETTER("username", GetUsername, SetUsername),
DECLARE_NAPI_GETTER_SETTER("password", GetPassword, SetPassword),
DECLARE_NAPI_GETTER_SETTER("search", GetSearch, SetSearch),
DECLARE_NAPI_GETTER_SETTER("username", GetUsername, SetUsername),
DECLARE_NAPI_GETTER_SETTER("password", GetPassword, SetPassword),
DECLARE_NAPI_GETTER_SETTER("host", GetHost, SetHost),
DECLARE_NAPI_GETTER_SETTER("hash", GetFragment, SetFragment),
DECLARE_NAPI_GETTER_SETTER("protocol", GetScheme, SetScheme),
DECLARE_NAPI_GETTER_SETTER("pathname", GetPath, SetPath),
DECLARE_NAPI_GETTER_SETTER("hash", GetFragment, SetFragment),
DECLARE_NAPI_GETTER_SETTER("protocol", GetScheme, SetScheme),
DECLARE_NAPI_GETTER_SETTER("pathname", GetPath, SetPath),
DECLARE_NAPI_GETTER_SETTER("port", GetPort, SetPort),
DECLARE_NAPI_GETTER("onOrOff", GetOnOrOff),
DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6),
};
NAPI_CALL(env, napi_define_class(env, UrlClassName, strlen(UrlClassName), UrlConstructor,
nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc,
&UrlClass));
NAPI_CALL(env, napi_define_class(env, urlClassName, strlen(urlClassName), UrlConstructor,
nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc,
&urlClass));
static napi_property_descriptor desc[] = {
DECLARE_NAPI_PROPERTY("Url", UrlClass)
DECLARE_NAPI_PROPERTY("Url", urlClass)
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
@@ -816,15 +894,15 @@ static napi_value UrlInit(napi_env env, napi_value exports)
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
DECLARE_NAPI_FUNCTION("stringParmas",StringParmas),
DECLARE_NAPI_FUNCTION("stringParmas", StringParmas),
};
NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
SeachParamsInit(env, exports);
SeachParamsInit(env, exports);
UrlInit(env, exports);
return exports;
}
extern "C"
extern "C"
__attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char** buf, int* bufLen)
{
if (buf != nullptr) {
@@ -836,8 +914,7 @@ __attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char** buf,
}
}
static napi_module UrlModule =
{
static napi_module UrlModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,