From 6211c0c4c543bc17b696369892fe7c79866dac71 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Tue, 14 Sep 2021 19:01:08 +0800 Subject: [PATCH 1/3] add scop module Signed-off-by: lifansheng --- ohos.build | 3 +- scope/BUILD.gn | 61 ++++++++++++++ scope/native_module_scope.cpp | 66 +++++++++++++++ scope/scope_js.js | 147 ++++++++++++++++++++++++++++++++++ 4 files changed, 276 insertions(+), 1 deletion(-) create mode 100755 scope/BUILD.gn create mode 100755 scope/native_module_scope.cpp create mode 100755 scope/scope_js.js diff --git a/ohos.build b/ohos.build index e5737bd..a3bfa82 100755 --- a/ohos.build +++ b/ohos.build @@ -7,7 +7,8 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_util_module/util:util_packages" + "//base/compileruntime/js_util_module/util:util_packages", + "//base/compileruntime/js_util_module/scope:scope_packages" ], "inner_kits": [ ], diff --git a/scope/BUILD.gn b/scope/BUILD.gn new file mode 100755 index 0000000..b7a1589 --- /dev/null +++ b/scope/BUILD.gn @@ -0,0 +1,61 @@ +# 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(":scope_js", "target_out_dir") +scope_js_obj_path = base_output_path + "/scope.o" + +gen_js_obj("scope_js") { + input = "//base/compileruntime/js_util_module/scope/scope_js.js" + output = scope_js_obj_path +} + +ohos_shared_library("scope") { + include_dirs = [ + "//foundation/ace/napi", + "//foundation/ace/napi/native_engine", + "//third_party/icu/icu4c/source/common", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/compileruntime/js_util_module/scope", + ] + + sources = [ + "native_module_scope.cpp", + ] + + deps = [ + ":scope_js", + "//base/compileruntime/js_util_module/scope/:scope_js", + "//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_scope" + + relative_install_dir = "module" +} + +group("scope_packages") { + deps = [ ":scope" ] +} diff --git a/scope/native_module_scope.cpp b/scope/native_module_scope.cpp new file mode 100755 index 0000000..c8ffe06 --- /dev/null +++ b/scope/native_module_scope.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "utils/log.h" + +extern const char _binary_scope_js_js_start[]; +extern const char _binary_scope_js_js_end[]; + +static napi_value ScopeInit(napi_env env, napi_value exports) +{ + const char* ClassName = "scope"; + napi_value scopeClass = nullptr; + NAPI_CALL(env, napi_define_class(env, ClassName, strlen(ClassName), nullptr, + nullptr, 0, nullptr, + &scopeClass)); + static napi_property_descriptor desc[] = { + DECLARE_NAPI_PROPERTY("scope", scopeClass) + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} + +// Scope module define +static napi_module scopeModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ScopeInit, + .nm_modname = "scope", + .nm_priv = ((void*)0), + .reserved = {0}, +}; + +// Scope module register +extern "C" +__attribute__((constructor)) void RegisterModule() +{ + napi_module_register(&scopeModule); +} + +// Scope JS register +extern "C" + __attribute__((visibility("default"))) void NAPI_scope_GetJSCode(const char** buf, int* buflen) + { + if (buf != nullptr) { + *buf = _binary_scope_js_js_start; + } + if (buflen != nullptr) { + *buflen = _binary_scope_js_js_end - _binary_scope_js_js_start; + } + } + diff --git a/scope/scope_js.js b/scope/scope_js.js new file mode 100755 index 0000000..c93eec7 --- /dev/null +++ b/scope/scope_js.js @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Scope { + constructor(lowerObj, upperObj) { + this.lowerObj = lowerObj; + this.upperObj = upperObj; + this.checkNull(lowerObj, 'lower limit not be null'); + this.checkNull(upperObj, 'upper limit not be null'); + if (lowerObj.compareTo(upperObj)) { + throw new Error('lower limit must be less than or equal to upper limit'); + } + this._lowerLimit = lowerObj; + this._upperLimit = upperObj; + } + + getLower() { + return this._lowerLimit; + } + + getUpper() { + return this._upperLimit; + } + + contains(x) { + let resLower; + let resUpper; + this.checkNull(x, 'value must not be null'); + if (x instanceof Scope) { + resLower = x._lowerLimit.compareTo(this._lowerLimit); + resUpper = this._upperLimit.compareTo(x._upperLimit); + } else { + resLower = x.compareTo(this._lowerLimit); + resUpper = this._upperLimit.compareTo(x); + } + return resLower && resUpper; + } + + clamp(value) { + this.checkNull(value, 'value must not be null'); + if (!value.compareTo(this._lowerLimit)) { + return this._lowerLimit; + } else if (value.compareTo(this._upperLimit)) { + return this._upperLimit; + } else { + return value; + } + } + + intersect(x, y) { + let reLower; + let reUpper; + let mLower; + let mUpper; + if (y) { + this.checkNull(x, 'lower limit must not be null'); + this.checkNull(y, 'upper limit must not be null'); + reLower = this._lowerLimit.compareTo(x); + reUpper = y.compareTo(this._upperLimit); + if (reLower && reUpper) { + return this; + } else { + mLower = reLower ? this._lowerLimit : x; + mUpper = reUpper ? this._upperLimit : y; + return new Scope(mLower, mUpper); + } + } else { + this.checkNull(x, 'scope must not be null'); + reLower = this._lowerLimit.compareTo(x._lowerLimit); + reUpper = x._upperLimit.compareTo(this._upperLimit); + if (!reLower && !reUpper) { + return x; + } else if (reLower && reUpper) { + return this; + } else { + mLower = reLower ? this._lowerLimit : x._lowerLimit; + mUpper = reUpper ? this._upperLimit : x._upperLimit; + return new Scope(mLower, mUpper); + } + } + } + + expand(x, y) { + let reLower; + let reUpper; + let mLower; + let mUpper; + if (!y) { + this.checkNull(x, 'value must not be null'); + if (!(x instanceof Scope)) { + this.checkNull(x, 'value must not be null'); + return this.expand(x, x); + } + let reLower = x._lowerLimit.compareTo(this._lowerLimit); + let reUpper = this._upperLimit.compareTo(x._upperLimit); + if (reLower && reUpper) { + return this; + } else if (!reLower && !reUpper) { + return x; + } else { + let mLower = reLower ? this._lowerLimit : x._lowerLimit; + let mUpper = reUpper ? this._upperLimit : x._upperLimit; + return new Scope(mLower, mUpper); + } + } + else { + this.checkNull(x, 'lower limit must not be null'); + this.checkNull(y, 'upper limit must not be null'); + let reLower = x.compareTo(this._lowerLimit); + let reUpper = this._upperLimit.compareTo(y); + if (reLower && reUpper) { + return this; + } + let mLower = reLower ? this._lowerLimit : x; + let mUpper = reUpper ? this._upperLimit : y; + return new Scope(mLower, mUpper); + } + } + + toString() { + let strLower = this._lowerLimit.toString(); + let strUpper = this._upperLimit.toString(); + return `[${strLower}, ${strUpper}]`; + } + + checkNull(o, str) { + if (o == null) { + throw new Error(str); + } + } +} + +export default { + Scope: Scope, +}; \ No newline at end of file -- Gitee From cf567a62955d50a0dfe824e2e262025597315f1a Mon Sep 17 00:00:00 2001 From: lifansheng Date: Tue, 14 Sep 2021 19:23:25 +0800 Subject: [PATCH 2/3] add lrubuffer module Signed-off-by: lifansheng --- lrubuffer/BUILD.gn | 61 +++++++++ lrubuffer/js_lrubuffer.js | 189 ++++++++++++++++++++++++++ lrubuffer/native_module_lrubuffer.cpp | 65 +++++++++ ohos.build | 4 +- 4 files changed, 317 insertions(+), 2 deletions(-) create mode 100755 lrubuffer/BUILD.gn create mode 100755 lrubuffer/js_lrubuffer.js create mode 100755 lrubuffer/native_module_lrubuffer.cpp diff --git a/lrubuffer/BUILD.gn b/lrubuffer/BUILD.gn new file mode 100755 index 0000000..7c373a9 --- /dev/null +++ b/lrubuffer/BUILD.gn @@ -0,0 +1,61 @@ +# 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_lrubuffer", "target_out_dir") +js_lrubuffer_obj_path = base_output_path + "/lrubuffer.o" + +gen_js_obj("js_lrubuffer") { + input = "//base/compileruntime/js_util_module/lrubuffer/js_lrubuffer.js" + output = js_lrubuffer_obj_path +} + +ohos_shared_library("lrubuffer") { + include_dirs = [ + "//foundation/ace/napi", + "//foundation/ace/napi/native_engine", + "//third_party/icu/icu4c/source/common", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/compileruntime/js_util_module/lrubuffer", + ] + + sources = [ + "native_module_lrubuffer.cpp", + ] + + deps = [ + ":js_lrubuffer", + "//base/compileruntime/js_util_module/lrubuffer/:js_lrubuffer", + "//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_util" + + relative_install_dir = "module" +} + +group("lrubuffer_packages") { + deps = [ ":lrubuffer" ] +} diff --git a/lrubuffer/js_lrubuffer.js b/lrubuffer/js_lrubuffer.js new file mode 100755 index 0000000..a287e39 --- /dev/null +++ b/lrubuffer/js_lrubuffer.js @@ -0,0 +1,189 @@ +/* + * 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'; +class LruBuffer { + constructor(capacity) { + this.maxSize = 64; + this.putCount = 0; + this.createCount = 0; + this.evictionCount = 0; + this.hitCount = 0; + this.missCount = 0; + if (capacity !== undefined) { + if (capacity <= 0) { + throw new Error('data error'); + } + this.maxSize = capacity; + } + this.cache = new Map(); + } + updateCapacity(newCapacity) { + if (newCapacity <= 0) { + throw new Error('data error'); + } + else if (this.cache.size > newCapacity) { + this.changeCapacity(newCapacity); + } + this.maxSize = newCapacity; + } + get(key) { + if (key === null) { + throw new Error('key search failed'); + } + let value; + if (this.cache.has(key)) { + value = this.cache.get(key); + this.hitCount++; + this.cache.delete(key); + this.cache.set(key, value); + return value; + } + this.missCount++; + let createValue = this.createDefault(key); + if (createValue === undefined) { + return undefined; + } + else { + value = this.put(key, createValue); + this.createCount++; + if (value !== null) { + this.put(key, value); + this.afterRemoval(false, key, createValue, value); + return value; + } + return createValue; + } + } + put(key, value) { + if (key === null || value === null) { + throw new Error('key or value search failed'); + } + let former; + this.putCount++; + if (this.cache.has(key)) { + former = this.cache.get(key); + this.cache.delete(key); + this.afterRemoval(false, key, former, null); + } + else if (this.cache.size >= this.maxSize) { + this.cache.delete(this.cache.keys().next().value); + this.evictionCount++; + } + this.cache.set(key, value); + return former; + } + getCreatCount() { + return this.createCount; + } + getMissCount() { + return this.missCount; + } + getRemovalCount() { + return this.evictionCount; + } + getMatchCount() { + return this.hitCount; + } + getPutCount() { + return this.putCount; + } + capacity() { + return this.maxSize; + } + size() { + return this.cache.size; + } + clear() { + this.cache.clear(); + this.afterRemoval(false, this.cache.keys(), this.cache.values(), null); + } + isEmpty() { + let temp = false; + if (this.cache.size === 0) { + temp = true; + } + return temp; + } + contains(key) { + let flag = false; + if (this.cache.has(key)) { + flag = true; + let value; + value = this.cache.get(key); + this.cache.delete(key); + this.cache.set(key, value); + } + return flag; + } + remove(key) { + if (key === null) { + throw new Error('key search failed'); + } + else if (this.cache.has(key)) { + let former; + former = this.cache.get(key); + this.cache.delete(key); + if (key !== null) { + this.afterRemoval(false, key, former, null); + return former; + } + } + return undefined; + } + toString() { + let peek = 0; + let hitRate = 0; + peek = this.hitCount + this.missCount; + if (peek !== 0) { + hitRate = 100 * this.hitCount / peek; + } + else { + hitRate = 0; + } + let str = ''; + str = 'Lrubuffer[ maxSize = ' + this.maxSize + ', hits = ' + this.hitCount + ', misses = ' + this.missCount + + ', hitRate = ' + hitRate + '% ]'; + return str; + } + values() { + let arr = []; + for (let value of this.cache.values()) { + arr.push(value); + } + return arr; + } + keys() { + let arr = []; + for (let key of this.cache.keys()) { + arr.push(key); + } + return arr; + } + afterRemoval(isEvict, key, value, newValue) { + } + createDefault(key) { + return undefined; + } + changeCapacity(newCapacity) { + while (this.cache.size > newCapacity) { + this.cache.delete(this.cache.keys().next().value); + this.evictionCount++; + this.afterRemoval(true, this.cache.keys(), this.cache.values(), null); + } + } +} +export default { + LruBuffer: LruBuffer, +}; diff --git a/lrubuffer/native_module_lrubuffer.cpp b/lrubuffer/native_module_lrubuffer.cpp new file mode 100755 index 0000000..a7e32e4 --- /dev/null +++ b/lrubuffer/native_module_lrubuffer.cpp @@ -0,0 +1,65 @@ +/* + * 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 "utils/log.h" + +extern const char _binary_js_lrubuffer_js_start[]; +extern const char _binary_js_lrubuffer_js_end[]; + +static napi_value LruBufferInit(napi_env env, napi_value exports) + { + HILOG_INFO("LXY -------module----- LruBufferInit start"); + const char* lruBufferClassName = "lrubuffer"; + napi_value lruBufferClass = nullptr; + NAPI_CALL(env, napi_define_class(env, lruBufferClassName, strlen(lruBufferClassName), nullptr, + nullptr, 0, nullptr, + &lruBufferClass)); + static napi_property_descriptor desc[] = { + DECLARE_NAPI_PROPERTY("lrubuffer", lruBufferClass), + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + HILOG_INFO("LXY -------module----- LruBufferInit end "); + return exports; +} + +//lrubuffer module define +static napi_module lrubufferModule = { +.nm_version = 1, +.nm_flags = 0, +.nm_filename = nullptr, +.nm_register_func = LruBufferInit, +.nm_modname = "lrubuffer", +.nm_priv = ((void*)0), +.reserved = { 0 }, +}; + +extern "C" __attribute__ ((constructor)) void RegisterModule() +{ + napi_module_register(&lrubufferModule); +} + +// lrubuffer JS register +extern "C" +__attribute__((visibility("default"))) void NAPI_lrubuffer_GetJSCode(const char** buf, int* buflen) +{ + if (buf != nullptr) { + *buf = _binary_js_lrubuffer_js_start; + } + if (buflen != nullptr) { + *buflen = _binary_js_lrubuffer_js_end - _binary_js_lrubuffer_js_start; + } +} \ No newline at end of file diff --git a/ohos.build b/ohos.build index a3bfa82..8a06643 100755 --- a/ohos.build +++ b/ohos.build @@ -7,8 +7,8 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_util_module/util:util_packages", - "//base/compileruntime/js_util_module/scope:scope_packages" + "//base/compileruntime/js_util_module/lrubuffer:lrubuffer_packages", + "//base/compileruntime/js_util_module/util:util_packages" ], "inner_kits": [ ], -- Gitee From fd15db46819a3ddb947ecfc20b5d89b36acce3e3 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Tue, 14 Sep 2021 19:25:28 +0800 Subject: [PATCH 3/3] modify ohos.build Signed-off-by: lifansheng --- ohos.build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ohos.build b/ohos.build index 8a06643..a3bfa82 100755 --- a/ohos.build +++ b/ohos.build @@ -7,8 +7,8 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_util_module/lrubuffer:lrubuffer_packages", - "//base/compileruntime/js_util_module/util:util_packages" + "//base/compileruntime/js_util_module/util:util_packages", + "//base/compileruntime/js_util_module/scope:scope_packages" ], "inner_kits": [ ], -- Gitee