diff --git a/README.md b/README.md index 6f8cdb55a10cad991050127d5298fb63ec3b0362..99ca7f8737690d84e6599ce754f5db913dfb7289 100755 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ base/compileruntime/js_util_module/ | readonly encoding : string | Get the set decoding format. | | readonly fatal : boolean | Get the setting that throws the exception. | | readonly ignoreBOM : boolean | Get whether to ignore the setting of the bom flag. | -| decode(input : ArrayBuffer | Input the data to be decoded, and solve the corresponding string character string.The first parameter input represents the data to be decoded, and the second parameter options represents a bool flag, which means that additional data will be followed. The default is false. | +| decode(input : Uint8Array, options?: { stream?: false }): string | Input the data to be decoded, and solve the corresponding string character string.The first parameter input represents the data to be decoded, and the second parameter options represents a bool flag, which means that additional data will be followed. The default is false. | | function printf(format: string, ...args: Object[]): string | The util.format() method returns a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. | | function getErrorString(errno: number): string | The geterrorstring () method uses a system error number as a parameter to return system error information. | | function callbackWrapper(original: Function): (err: Object, value: Object) => void | Takes an async function (or a function that returns a Promise) and returns a function following the error-first callback style, i.e. taking an (err, value) => ... callback as the last argument. In the callback, the first argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the resolved value. | diff --git a/README_zh.md b/README_zh.md old mode 100755 new mode 100644 index 9210368799c511a43d04d2d67a8fafa28d736ae7..049a05b2f52abd36559035d4e1f1ab2fa58392d2 --- a/README_zh.md +++ b/README_zh.md @@ -1,165 +1,165 @@ -# js_util_module子系统/组件 - -- [简介](#简介) -- [目录](#目录) -- [说明](#说明) - - [接口说明](#接口说明) - - [使用说明](#使用说明) - -- [相关仓](#相关仓) - -## 简介 - -UTIL接口用于字符编码TextEncoder、解码TextDecoder和帮助函数HelpFunction。TextEncoder表示一个文本编码器,接受字符串作为输入,以UTF-8格式进行编码,输出UTF-8字节流。TextDecoder接口表示一个文本解码器,解码器将字节流作为输入,输出stirng字符串。HelpFunction主要是对函数做callback化、promise化以及对错误码进行编写输出,及类字符串的格式化输出。 -## 目录 - -``` -base/compileruntime/js_util_module/ -├── Class:TextEncoder # TextEncoder类 -│ ├── new TextEncoder() # 创建TextEncoder对象 -│ ├── encode() # encode方法 -│ ├── encoding # encoding属性 -│ └── encodeInto() # encodeInto方法 -├── Class:TextDecoder # TextDecoder类 -│ ├── new TextDecoder() # 创建TextDecoder对象 -│ ├── decode() # decode方法 -│ ├── encoding # encoding属性 -│ ├── fatal # fatal属性 -│ └── ignoreBOM # ignoreBOM属性 -├── printf() # printf方法 -├── getErrorString() # getErrorString方法 -├── callbackWrapper() # callbackWrapper方法 -└── promiseWrapper() # promiseWrapper方法 -``` - -## 说明 - -### 接口说明 - - -| 接口名 | 说明 | -| -------- | -------- | -| readonly encoding : string | 获取编码的格式,只支持UTF-8。 | -| encode(input : string) : Uint8Array | 输入stirng字符串,编码并输出UTF-8字节流。 | -| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | 输入stirng字符串,dest表示编码后存放位置,返回一个对象,read表示已经编码的字符的个数,written表示已编码字符所占字节的大小。 | -| constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | 构造函数,第一个参数encoding表示解码的格式。第二个参数表示一些属性。属性中fatal表示是否抛出异常,ignoreBOM表示是否忽略bom标志。 | -| readonly encoding : string | 获取设置的解码格式。 | -| readonly fatal : boolean | 获取抛出异常的设置 | -| readonly ignoreBOM : boolean | 获取是否忽略bom标志的设置 | -| decode(input : ArrayBuffer | 输入要解码的数据,解出对应的string字符串。第一个参数input表示要解码的数据,第二个参数options表示一个bool标志,表示将跟随附加数据,默认为false。 | -| function printf(format: string, ...args: Object[]): string | printf()方法使用第一个参数作为格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。 | -| function getErrorString(errno: number): string | getErrorString()方法使用一个系统的错误数字作为参数,用来返回系统的错误信息。 | -| function callbackWrapper(original: Function): (err: Object, value: Object) => void | 参数为一个采用 async 函数(或返回 Promise 的函数)并返回遵循错误优先回调风格的函数,即将 (err, value) => ... 回调作为最后一个参数。 在回调中,第一个参数将是拒绝原因(如果 Promise 已解决,则为 null),第二个参数将是已解决的值。 | -| function promiseWrapper(original: (err: Object, value: Object) => void): Object | 参数为采用遵循常见的错误优先的回调风格的函数(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的版本。 | - -printf中每个说明符都替换为来自相应参数的转换后的值。 支持的说明符有: -| 式样化字符 | 式样要求 | -| -------- | -------- | -| %s: | String 将用于转换除 BigInt、Object 和 -0 之外的所有值。| -| %d: |Number 将用于转换除 BigInt 和 Symbol 之外的所有值。| -| %i: |parseInt(value, 10) 用于除 BigInt 和 Symbol 之外的所有值。| -| %f: |parseFloat(value) 用于除 Symbol 之外的所有值。| -| %j: |JSON。 如果参数包含循环引用,则替换为字符串 '[Circular]'。| -| %o: |Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于具有选项 { showHidden: true, showProxy: true } 的 util.inspect()。这将显示完整的对象,包括不可枚举的属性和代理。| -| %O: |Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于没有选项的 util.inspect()。 这将显示完整的对象,但不包括不可枚举的属性和代理。| -| %c: | 此说明符被忽略,将跳过任何传入的 CSS。| -| %%: |单个百分号 ('%')。 这不消耗待式样化参数。| - -### 使用说明 - -各接口使用方法如下: - -1.readonly encoding() -``` -import util from '@ohos.util' -var textEncoder = new util.TextEncoder(); -var getEncoding = textEncoder.encoding(); -``` -2.encode() -``` -import util from '@ohos.util' -var textEncoder = new util.TextEncoder(); -var result = textEncoder.encode('abc'); -``` -3.encodeInto() -``` -import util from '@ohos.util' -var textEncoder = new util.TextEncoder(); -var obj = textEncoder.encodeInto('abc', dest); -``` -4.textDecoder() -``` -import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); -``` -5.readonly encoding() -``` -import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); -var getEncoding = textDecoder.encoding(); -``` -6.readonly fatal() -``` -import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); -var fatalStr = textDecoder.fatal(); -``` -7.readonly ignoreBOM() -``` -import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); -var ignoreBom = textDecoder.ignoreBOM(); -``` -8.decode() -``` -import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); -var result = textDecoder.decode(input, {stream : true}); -``` -9.printf() -``` -import util from '@ohos.util' -var format = "%%%o%%%i%s"; -var value = function aa(){}; -var value1 = 1.5; -var value2 = "qwer"; -var result = util.printf(format,value,value1,value2); -``` -10.getErrorString() -``` -import util from '@ohos.util' -var errnum = 13; -var result = util.getErrorString(errnum); -``` -11.callbackWrapper() -``` -import util from '@ohos.util' -async function promiseFn() { - return Promise.resolve('value'); -}; -var cb = util.callbackWrapper(promiseFn); -cb((err, ret) => { - expect(err).strictEqual(null); - expect(ret).strictEqual('value'); -}) -``` -12.promiseWrapper() -``` -import util from '@ohos.util' -function aysnFun(str1, str2, callback) { - if (typeof str1 === 'string' && typeof str1 === 'string') { - callback(null, str1 + str2); - } else { - callback('type err'); - } -} -let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World'); -newPromiseObj.then(res => { - expect(res).strictEqual('HelloWorld'); -}) -``` -## 相关仓 - -[js_util_module子系统](https://gitee.com/OHOS_STD/js_util_module) - -[base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md) +# js_util_module子系统/组件 + +- [简介](#简介) +- [目录](#目录) +- [说明](#说明) + - [接口说明](#接口说明) + - [使用说明](#使用说明) + +- [相关仓](#相关仓) + +## 简介 + +UTIL接口用于字符编码TextEncoder、解码TextDecoder和帮助函数HelpFunction。TextEncoder表示一个文本编码器,接受字符串作为输入,以UTF-8格式进行编码,输出UTF-8字节流。TextDecoder接口表示一个文本解码器,解码器将字节流作为输入,输出stirng字符串。HelpFunction主要是对函数做callback化、promise化以及对错误码进行编写输出,及类字符串的格式化输出。 +## 目录 + +``` +base/compileruntime/js_util_module/ +├── Class:TextEncoder # TextEncoder类 +│ ├── new TextEncoder() # 创建TextEncoder对象 +│ ├── encode() # encode方法 +│ ├── encoding # encoding属性 +│ └── encodeInto() # encodeInto方法 +├── Class:TextDecoder # TextDecoder类 +│ ├── new TextDecoder() # 创建TextDecoder对象 +│ ├── decode() # decode方法 +│ ├── encoding # encoding属性 +│ ├── fatal # fatal属性 +│ └── ignoreBOM # ignoreBOM属性 +├── printf() # printf方法 +├── getErrorString() # getErrorString方法 +├── callbackWrapper() # callbackWrapper方法 +└── promiseWrapper() # promiseWrapper方法 +``` + +## 说明 + +### 接口说明 + + +| 接口名 | 说明 | +| -------- | -------- | +| readonly encoding : string | 获取编码的格式,只支持UTF-8。 | +| encode(input : string) : Uint8Array | 输入stirng字符串,编码并输出UTF-8字节流。 | +| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | 输入stirng字符串,dest表示编码后存放位置,返回一个对象,read表示已经编码的字符的个数,written表示已编码字符所占字节的大小。 | +| constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | 构造函数,第一个参数encoding表示解码的格式。第二个参数表示一些属性。属性中fatal表示是否抛出异常,ignoreBOM表示是否忽略bom标志。 | +| readonly encoding : string | 获取设置的解码格式。 | +| readonly fatal : boolean | 获取抛出异常的设置 | +| readonly ignoreBOM : boolean | 获取是否忽略bom标志的设置 | +| decode(input : Uint8Array, options?: { stream?: false }): string| 输入要解码的数据,解出对应的string字符串。第一个参数input表示要解码的数据,第二个参数options表示一个bool标志,表示将跟随附加数据,默认为false。 | +| function printf(format: string, ...args: Object[]): string | printf()方法使用第一个参数作为格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。 | +| function getErrorString(errno: number): string | getErrorString()方法使用一个系统的错误数字作为参数,用来返回系统的错误信息。 | +| function callbackWrapper(original: Function): (err: Object, value: Object) => void | 参数为一个采用 async 函数(或返回 Promise 的函数)并返回遵循错误优先回调风格的函数,即将 (err, value) => ... 回调作为最后一个参数。 在回调中,第一个参数将是拒绝原因(如果 Promise 已解决,则为 null),第二个参数将是已解决的值。 | +| function promiseWrapper(original: (err: Object, value: Object) => void): Object | 参数为采用遵循常见的错误优先的回调风格的函数(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的版本。 | + +printf中每个说明符都替换为来自相应参数的转换后的值。 支持的说明符有: +| 式样化字符 | 式样要求 | +| -------- | -------- | +| %s: | String 将用于转换除 BigInt、Object 和 -0 之外的所有值。| +| %d: |Number 将用于转换除 BigInt 和 Symbol 之外的所有值。| +| %i: |parseInt(value, 10) 用于除 BigInt 和 Symbol 之外的所有值。| +| %f: |parseFloat(value) 用于除 Symbol 之外的所有值。| +| %j: |JSON。 如果参数包含循环引用,则替换为字符串 '[Circular]'。| +| %o: |Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于具有选项 { showHidden: true, showProxy: true } 的 util.inspect()。这将显示完整的对象,包括不可枚举的属性和代理。| +| %O: |Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于没有选项的 util.inspect()。 这将显示完整的对象,但不包括不可枚举的属性和代理。| +| %c: | 此说明符被忽略,将跳过任何传入的 CSS。| +| %%: |单个百分号 ('%')。 这不消耗待式样化参数。| + +### 使用说明 + +各接口使用方法如下: + +1.readonly encoding() +``` +import util from '@ohos.util' +var textEncoder = new util.TextEncoder(); +var getEncoding = textEncoder.encoding(); +``` +2.encode() +``` +import util from '@ohos.util' +var textEncoder = new util.TextEncoder(); +var result = textEncoder.encode('abc'); +``` +3.encodeInto() +``` +import util from '@ohos.util' +var textEncoder = new util.TextEncoder(); +var obj = textEncoder.encodeInto('abc', dest); +``` +4.textDecoder() +``` +import util from '@ohos.util' +var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +``` +5.readonly encoding() +``` +import util from '@ohos.util' +var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var getEncoding = textDecoder.encoding(); +``` +6.readonly fatal() +``` +import util from '@ohos.util' +var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var fatalStr = textDecoder.fatal(); +``` +7.readonly ignoreBOM() +``` +import util from '@ohos.util' +var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var ignoreBom = textDecoder.ignoreBOM(); +``` +8.decode() +``` +import util from '@ohos.util' +var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var result = textDecoder.decode(input, {stream : true}); +``` +9.printf() +``` +import util from '@ohos.util' +var format = "%%%o%%%i%s"; +var value = function aa(){}; +var value1 = 1.5; +var value2 = "qwer"; +var result = util.printf(format,value,value1,value2); +``` +10.getErrorString() +``` +import util from '@ohos.util' +var errnum = 13; +var result = util.getErrorString(errnum); +``` +11.callbackWrapper() +``` +import util from '@ohos.util' +async function promiseFn() { + return Promise.resolve('value'); +}; +var cb = util.callbackWrapper(promiseFn); +cb((err, ret) => { + expect(err).strictEqual(null); + expect(ret).strictEqual('value'); +}) +``` +12.promiseWrapper() +``` +import util from '@ohos.util' +function aysnFun(str1, str2, callback) { + if (typeof str1 === 'string' && typeof str1 === 'string') { + callback(null, str1 + str2); + } else { + callback('type err'); + } +} +let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World'); +newPromiseObj.then(res => { + expect(res).strictEqual('HelloWorld'); +}) +``` +## 相关仓 + +[js_util_module子系统](https://gitee.com/OHOS_STD/js_util_module) + +[base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md) diff --git a/lrubuffer/BUILD.gn b/lrubuffer/BUILD.gn deleted file mode 100755 index 97eae7a6ca836ab85cb684b3af98dc42010ee90f..0000000000000000000000000000000000000000 --- a/lrubuffer/BUILD.gn +++ /dev/null @@ -1,59 +0,0 @@ -# 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 deleted file mode 100755 index 71a4752da1ba1649253907ff8cbaa8951cce30bf..0000000000000000000000000000000000000000 --- a/lrubuffer/js_lrubuffer.js +++ /dev/null @@ -1,192 +0,0 @@ -/* - * 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; - this.hitCount++; - value = this.cache.get(key); - this.cache.delete(key); - this.cache.set(key, value); - return flag; - } - this.missCount++; - 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 (former !== 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 deleted file mode 100755 index cbd5a66d1938fc2c22e49218d64f54f0cf3d90dc..0000000000000000000000000000000000000000 --- a/lrubuffer/native_module_lrubuffer.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 - -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -static napi_value LruBufferInit(napi_env env, napi_value exports) -{ - 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); - 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) -{ - extern const char _binary_js_lrubuffer_js_start[]; - extern const char _binary_js_lrubuffer_js_end[]; - 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 b49feec8679cc7adc16bafbe5249ce644cd6b65f..e5737bd24e47b197bb6bce8f9b9cc367ff4e96dd 100755 --- a/ohos.build +++ b/ohos.build @@ -7,9 +7,7 @@ "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": [ ], diff --git a/scope/BUILD.gn b/scope/BUILD.gn deleted file mode 100755 index 81b858000701adc0902d743d43cfba01b0cb88a9..0000000000000000000000000000000000000000 --- a/scope/BUILD.gn +++ /dev/null @@ -1,59 +0,0 @@ -# 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_util" - - relative_install_dir = "module" -} - -group("scope_packages") { - deps = [ ":scope" ] -} diff --git a/scope/native_module_scope.cpp b/scope/native_module_scope.cpp deleted file mode 100755 index 3dd9411a1ce81ca5d7b2e28cda20943076047c92..0000000000000000000000000000000000000000 --- a/scope/native_module_scope.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 - -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -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) -{ - extern const char _binary_scope_js_js_start[]; - extern const char _binary_scope_js_js_end[]; - if (buf != nullptr) { - *buf = _binary_scope_js_js_start; - } - if (buflen != nullptr) { - *buflen = _binary_scope_js_js_end - _binary_scope_js_js_start; - } -} \ No newline at end of file diff --git a/scope/scope_js.js b/scope/scope_js.js deleted file mode 100755 index c93eec79e4932d70788547ecae6d60e85d03254c..0000000000000000000000000000000000000000 --- a/scope/scope_js.js +++ /dev/null @@ -1,147 +0,0 @@ -/* - * 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 diff --git a/test/unittest/test_util.cpp b/test/unittest/test_util.cpp index 00ee99790ff76a669375db0a0c9a73ae806596c6..3139e2a45d3ef7c504f5867695bfb658b47a9c39 100755 --- a/test/unittest/test_util.cpp +++ b/test/unittest/test_util.cpp @@ -93,7 +93,7 @@ HWTEST_F(NativeEngineTest, textEncodeTest001, testing::ext::TestSize.Level0) env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); ASSERT_EQ(srcLength, 6); - char* res = (char*)srcData; + char* res = reinterpret_cast(srcData); res[srcLength] = 0; ASSERT_STREQ(res, excepted); diff --git a/util/BUILD.gn b/util/BUILD.gn index ea5e47e21e07ea1bba26b2c1a3c2082e3ac36a00..cc2856c35fb222f7a60e6c145674479d84e59b6e 100755 --- a/util/BUILD.gn +++ b/util/BUILD.gn @@ -11,8 +11,33 @@ # 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_util_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path("//base/compileruntime/js_util_module/util/util_js.js"), + "--dst-file", + rebase_path(target_out_dir + "/util.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_util_module/util/util_js.js" ] + outputs = [ target_out_dir + "/util.abc" ] +} base_output_path = get_label_info(":util_js", "target_out_dir") util_js_obj_path = base_output_path + "/util.o" @@ -22,6 +47,14 @@ gen_js_obj("util_js") { output = util_js_obj_path } +abc_output_path = get_label_info(":util_abc", "target_out_dir") +util_abc_obj_path = abc_output_path + "/util_abc.o" +gen_js_obj("util_abc") { + input = "$target_out_dir/util.abc" + output = util_abc_obj_path + dep = ":gen_util_abc" +} + ohos_shared_library("util") { include_dirs = [ "//foundation/ace/napi", @@ -41,6 +74,7 @@ ohos_shared_library("util") { ] deps = [ + ":util_abc", ":util_js", "//base/compileruntime/js_util_module/util/:util_js", "//foundation/ace/napi/:ace_napi", diff --git a/util/js_rational.cpp b/util/js_rational.cpp index 544974093abcc367e4cef881437451d8956f49da..4133feba2b4c66ccc62cf936b0f689b9851ed617 100755 --- a/util/js_rational.cpp +++ b/util/js_rational.cpp @@ -90,23 +90,28 @@ namespace OHOS::Util { size_t index = (colon != std::string::npos) ? colon : semicolon; std::string s1 = buf.substr(0, index); std::string s2 = buf.substr(index + 1, buf.size()); - for (int i = 1; i < s1.size(); i++) { - if (((s1[0] == '+') || (s1[0] == '-') || (isdigit(s1[0]))) && (isdigit(s1[i]))) { - flag = 1; - } else { - napi_throw_error(env_, "invalidRational", "string invalid!"); - } - } + size_t len1 = s1.size(); + size_t len2 = s2.size(); + SetFlag(s1, len1, flag); int num1 = stoi(s1) * flag; - for (int i = 1; i < s2.size(); i++) { - if (((s2[0] == '+') || (s2[0] == '-') || (isdigit(s2[0]))) && (isdigit(s2[i]))) { + flag = 0; + SetFlag(s2, len2, flag); + int num2 = stoi(s2) * flag; + return CreateObj(num1, num2, RationalNumberClass); + } + + void RationalNumber::SetFlag(const std::string str, const size_t len, int& flag) const + { + if (str == "") { + return; + } + for (size_t i = 1; i < len; i++) { + if (((str[0] == '+') || (str[0] == '-') || (isdigit(str[0]))) && (isdigit(str[i]))) { flag = 1; } else { napi_throw_error(env_, "invalidRational", "string invalid!"); } } - int num2 = stoi(s2) * flag; - return CreateObj(num1, num2, RationalNumberClass); } napi_value RationalNumber::CreateObj(int num1, int num2, napi_value RationalNumberClass) const diff --git a/util/js_rational.h b/util/js_rational.h index 8e6f022940d668b2e0d6d43f633342e688a66f62..49a9ccd8d5ef40a922480ce98203426f0e7f88b0 100755 --- a/util/js_rational.h +++ b/util/js_rational.h @@ -41,6 +41,7 @@ namespace OHOS::Util { int mden = 0; napi_env env_; napi_value CreateObj(int num1, int num2, napi_value RationalNumberClass) const; + void SetFlag(const std::string str, const size_t len, int& flag) const; }; } #endif \ No newline at end of file diff --git a/util/js_textdecoder.cpp b/util/js_textdecoder.cpp index 8f38672f9318202954338916cc9c0d714d36fd5f..05e574f3c095cd75c38b02eff1330dc7e5729f39 100755 --- a/util/js_textdecoder.cpp +++ b/util/js_textdecoder.cpp @@ -88,7 +88,7 @@ namespace OHOS::Util { return nullptr; } UChar *target = arr; - size_t tarStartPos = (intptr_t)arr; + size_t tarStartPos = reinterpret_cast(arr); ucnv_toUnicode(GetConverterPtr(), &target, target + len, &source, source + length, nullptr, flush, &codeFlag); size_t resultLength = 0; bool omitInitialBom = false; @@ -179,7 +179,7 @@ namespace OHOS::Util { } if (U_SUCCESS(codeFlag)) { if (decArr.limitLen > 0) { - rstLen = (intptr_t)decArr.target - decArr.tarStartPos; + rstLen = reinterpret_cast(decArr.target) - decArr.tarStartPos; if (rstLen > 0 && IsUnicode() && !IsIgnoreBom() && !IsBomFlag()) { bomFlag = (arr[0] == 0xFEFF) ? true : false; label_ |= static_cast(ConverterFlags::BOM_SEEN_FLG); diff --git a/util/util_js.js b/util/util_js.js index f3ce3a0a334c7dfb5f341f3af4e50c8592bfd9c6..5de76432c5c8cd1cdf54f44e4ce725c9438cae8e 100755 --- a/util/util_js.js +++ b/util/util_js.js @@ -378,16 +378,16 @@ function getErrorString(errnum) } function callbackified(original, ...args) - { - const maybeCb = args.pop(); - if (typeof maybeCb !== 'function') { - throw new Error('maybe is not function'); - } - const cb = (...args) => { - Reflect.apply(maybeCb, this, args); - }; - Reflect.apply(original, this, args).then((ret) => cb(null, ret), (rej) => cb(rej)); +{ + const maybeCb = args.pop(); + if (typeof maybeCb !== 'function') { + throw new Error('maybe is not function'); } + const cb = (...args) => { + Reflect.apply(maybeCb, this, args); + }; + Reflect.apply(original, this, args).then((ret) => cb(null, ret), (rej) => cb(rej)); +} function callbackWrapper(original) { @@ -410,7 +410,8 @@ function callbackWrapper(original) return cb; } -function promiseWrapper(func) { +function promiseWrapper(func) +{ return function (...args) { return new Promise((resolve, reject) => { let callback = function (err, ...values) { @@ -425,6 +426,377 @@ function promiseWrapper(func) { }; } +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 not be null'); + } + 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 key or value not be null'); + } + 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; + this.hitCount++; + value = this.cache.get(key); + this.cache.delete(key); + this.cache.set(key, value); + return flag; + } + this.missCount++; + return flag; + } + + remove(key) + { + if (key === null) { + throw new Error('key not be null'); + } + else if (this.cache.has(key)) { + let former; + former = this.cache.get(key); + this.cache.delete(key); + if (former !== 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; + } + + entries() + { + let arr = []; + for (let entry of this.cache.entries()) { + arr.push(entry); + } + return arr; + } + + [Symbol.iterator]() + { + let arr = []; + for (let [key, value] of this.cache) { + arr.push([key, value]); + } + return arr; + } + + 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); + } + } +} +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 { printf: printf, getErrorString: getErrorString, @@ -434,4 +806,6 @@ export default { TextDecoder: TextDecoder, RationalNumber: RationalNumber, Base64: Base64, + LruBuffer: LruBuffer, + Scope: Scope, }; \ No newline at end of file