diff --git a/README.md b/README.md index 1b261e23260341381b5679727f1b049d33e324cd..5b78f40ade552f973ba179c601cb67bf1216179a 100755 --- a/README.md +++ b/README.md @@ -24,26 +24,26 @@ base/compileruntime/js_util_module/ │ ├── new TextDecoder() # create TextDecoder object │ ├── decode() # decode method │ ├── encoding # encoding property -│ ├── fatal # fatal property -│ └── ignoreBOM # ignoreBOM property +│ ├── fatal # fatal property +│ └── ignoreBOM # ignoreBOM property ├── printf() # printf method ├── getErrorString() # getErrorString method ├── callbackWrapper() # callbackWrapper method ├── promiseWrapper() # promiseWrapper method ├── Class:Base64 # Base64 class │ ├── new Base64() # create Base64 object +│ ├── encodeSync() # encodeSync method +│ ├── encodeToStringSync() # encodeToStringSync method +│ ├── decodeSync() # decodeSync method │ ├── encode() # encode method │ ├── encodeToString() # encodeToString method -│ ├── decode() # decode method -│ ├── encodeAsync() # encodeAsync method -│ ├── encodeToStringAsync() # encodeToStringAsync method -│ └── decodeAsync() # decodeAsync method +│ └── decode() # decode method ├── Class:RationalNumber # RationalNumber class │ ├── new RationalNumber() # create RationalNumber object │ ├── createRationalFromString() # creatRationalFromString method │ ├── compareTo() # compareTo method │ ├── equals() # equals method -│ ├── value() # value method +│ ├── valueOf() # valueOf method │ ├── getCommonDivisor() # getCommonDivisor method │ ├── getDenominator() # getDenominator method │ ├── getNumerator() # getNumerator method @@ -56,8 +56,8 @@ base/compileruntime/js_util_module/ │ ├── updateCapacity() # updateCapacity method │ ├── toString() # toString method │ ├── values() # values method -│ ├── size() # size method -│ ├── capacity() # capacity method +│ ├── length # attribute of length +│ ├── getCapacity() # getCapacity method │ ├── clear() # clear method │ ├── getCreateCount # getCreateCount method │ ├── getMissCount() # getMissCount method @@ -81,9 +81,9 @@ base/compileruntime/js_util_module/ | ├── intersect() # intersect method | ├── getUpper() # getUpper method | ├── getLower() # getLower method -| ├── expand() # expand method | ├── expand() # expand method -| ├── expand() # expand method +| ├── expand() # expand method +| ├── expand() # expand method | ├── contains() # contains method | ├── contains() # contains method | └── clamp() # clamp method @@ -97,7 +97,6 @@ base/compileruntime/js_util_module/ ├── isBigUint64Array() # isBigUint64Array method ├── isBooleanObject() # isBooleanObject method ├── isBoxedPrimitive() # isBoxedPrimitive method - ├── isCryptoKey() # isCryptoKey method ├── isDataView() # isDataView method ├── isDate() # isDate method ├── isExternal() # isExternal method @@ -108,7 +107,6 @@ base/compileruntime/js_util_module/ ├── isInt8Array() # isInt8Array method ├── isInt16Array() # isInt16Array method ├── isInt32Array() # isInt32Array method - ├── isKeyObject() # isKeyObject method ├── isMap() # isMap method ├── isMapIterator() # isMapIterator method ├── isModuleNamespaceObject() # isModuleNamespaceObject method @@ -144,16 +142,16 @@ base/compileruntime/js_util_module/ | 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 : 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. | -| encode(src: Uint8Array): Uint8Array; | Encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. | -| encodeToString(src: Uint8Array): string; | Encodes the specified byte array as a String using the Base64 encoding scheme. | -| decode(src: Uint8Array \| string): Uint8Array; | Decodes the Base64-encoded string or input u8 array into the newly allocated u8 array using the Base64 encoding scheme. | -| encodeAsync(src: Uint8Array): Promise\; | Asynchronously encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. | -| encodeToStringAsync(src: Uint8Array): Promise\; | Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. | -| decodeAsync(src: Uint8Array \| string): Promise\; | Use the Base64 encoding scheme to asynchronously decode a Base64-encoded string or input u8 array into a newly allocated u8 array. | +| encodeSync(src: Uint8Array): Uint8Array; | Encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. | +| encodeToStringSync(src: Uint8Array): string; | Encodes the specified byte array as a String using the Base64 encoding scheme. | +| decodeSync(src: Uint8Array \| string): Uint8Array; | Decodes the Base64-encoded string or input u8 array into the newly allocated u8 array using the Base64 encoding scheme. | +| encode(src: Uint8Array): Promise\; | Asynchronously encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. | +| encodeToString(src: Uint8Array): Promise\; | Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. | +| decode(src: Uint8Array \| string): Promise\; | Use the Base64 encoding scheme to asynchronously decode a Base64-encoded string or input u8 array into a newly allocated u8 array. | | static createRationalFromString(rationalString: string): RationalNumber | Create a RationalNumber object based on the given string. | | compareTo(another: RationalNumber): number | Compare the current RationalNumber object with the given object. | | equals(obj: object): number | Check if the given object is the same as the current RationalNumber object.| -| value(): number | Take the current RationalNumber object to an integer value or a floating point value. | +| valueOf(): number | Take the current RationalNumber object to an integer value or a floating point value. | | static getCommonDivisor(number1: number, number2: number,): number | Obtain the greatest common divisor of two specified numbers. | | getDenominator(): number | Get the denominator of the current RationalNumber object. | | getNumerator(): number | Get the numerator of the current RationalNumber object. | @@ -165,8 +163,8 @@ base/compileruntime/js_util_module/ | updateCapacity(newCapacity: number): void | Updates the buffer capacity to the specified capacity. This exception is thrown if newCapacity is less than or equal to 0. | | toString(): string | Returns the string representation of the object and outputs the string representation of the object. | | values(): V[ ] | Gets a list of all values in the current buffer, and the output returns a list of all values in the current buffer in ascending order, from most recently accessed to least recently accessed. | -| size(): number | Gets the total number of values in the current buffer. The output returns the total number of values in the current buffer. | -| capacity(): number | Gets the capacity of the current buffer. The output returns the capacity of the current buffer. | +| length: number | represents the total number of values in the current buffer. The output returns the total number of values in the current buffer. | +| getCapacity(): number | Gets the capacity of the current buffer. The output returns the capacity of the current buffer. | | clear(): void | The key value pairs are cleared from the current buffer, after the key value is cleared, the afterRemoval () method is invoked to perform subsequent operations in turn. | | getCreateCount(): number | Get the number of times the returned value of createdefault(), and output the number of times the returned value of createdefault(). | | getMissCount(): number | Get the number of times the query value does not match, and output the number of times the query value does not match. | @@ -208,7 +206,6 @@ base/compileruntime/js_util_module/ | isBigUint64Array(value: Object): boolean | Check whether the entered value is of biguint64array array array type. | | isBooleanObject(value: Object): boolean | Check whether the entered value is a Boolean object type. | | isBoxedPrimitive(value: Object): boolean | Check whether the entered value is a Boolean or number or string or symbol object type. | -| isCryptoKey(value: Object): boolean | Check whether the entered value is the cryptokey object type. | | isDataView(value: Object): boolean | Check whether the entered value is of DataView type. | | isDate(value: Object): boolean | Check whether the entered value is of type date. | | isExternal(value: Object): boolean | Check whether the entered value is a native external value type. | @@ -219,7 +216,6 @@ base/compileruntime/js_util_module/ | isInt8Array(value: Object): boolean | Check whether the entered value is of int8array array type. | | isInt16Array(value: Object): boolean | Check whether the entered value is the int16array type. | | isInt32Array(value: Object): boolean | Check whether the entered value is the int32array array type. | -| isKeyObject(value: Object): boolean | Check whether the entered value is the keyobject object type. | | isMap(value: Object): boolean | Check whether the entered value is of map type. | | isMapIterator(value: Object): boolean | Check whether the entered value is the iterator type of map. | | isModuleNamespaceObject(value: Object): boolean | Check whether the entered value is the module namespace object object type. | @@ -349,51 +345,51 @@ newPromiseObj.then(res => { expect(res).strictEqual('HelloWorld'); }) ``` -13.encode() +13.encodeSync() ``` import util from '@ohos.util' var that = new util.Base64(); var array = new Uint8Array([115,49,51]); -var result = that.encode(array); +var result = that.encodeSync(array); ``` -14.encodeToString() +14.encodeToStringSync() ``` import util from '@ohos.util' var that = new util.Base64(); var array = new Uint8Array([115,49,51]); -var result = that.encodeToString(array); +var result = that.encodeToStringSync(array); ``` -15.decode() +15.decodeSync() ``` import util from '@ohos.util' var that = new util.Base64() var buff = 'czEz'; -var result = that.decode(buff); +var result = that.decodeSync(buff); ``` -16.encodeAsync() +16.encode() ``` import util from '@ohos.util' var that = new util.Base64() var array = new Uint8Array([115,49,51]); -await that.encodeAsync(array).then(val=>{ +await that.encode(array).then(val=>{ }) done() ``` -17.encodeToStringAsync() +17.encodeToString() ``` import util from '@ohos.util' var that = new util.Base64() var array = new Uint8Array([115,49,51]); -await that.encodeToStringAsync(array).then(val=>{ +await that.encodeToString(array).then(val=>{ }) done() ``` -18.decodeAsync() +18.decode() ``` import util from '@ohos.util' var that = new util.Base64() var buff = 'czEz'; -await that.decodeAsync(buff).then(val=>{ +await that.decode(buff).then(val=>{ }) done() ``` @@ -418,7 +414,7 @@ var pro = new util.RationalNumber(2, 1); var proc = new util.RationalNumber(3, 4); var res = pro.equals(proc); ``` -22.value() +22.valueOf() ``` import util from '@ohos.util' var pro = new util.RationalNumber(2, 1); @@ -468,7 +464,7 @@ var pro = new util.RationalNumber(-2, 1); var res = pro.toString(); ``` -30.updateCapacity() +30.updateCapacity() ``` import util from '@ohos.util' var pro = new util.LruBuffer(); @@ -492,21 +488,21 @@ pro.put(2,"anhu"); pro.put("afaf","grfb"); var result = pro.values(); ``` -33.size() +33.length ``` import util from '@ohos.util' var pro = new util.LruBuffer(); pro.put(2,10); pro.put(1,8); -var result = pro.size(); +var result = pro.length; ``` -34.capacity() +34.getCapacity() ``` import util from '@ohos.util' var pro = new util.LruBuffer(); -var result = pro.capacity(); +var result = pro.getCapacity(); ``` -35.clear() +35.clear() ``` import util from '@ohos.util' var pro = new util.LruBuffer(); @@ -576,7 +572,7 @@ var result = pro.put(2,10); ``` import util from '@ohos.util' var pro = new util.LruBuffer(); -pro.put(2,10); + pro.put(2,10); var result = pro.keys(); ``` 45.remove() @@ -613,7 +609,7 @@ var pro = new util.LruBuffer(); pro.put(2,10); var result = pro[symbol.iterator](); ``` -50.afterRemoval() +50.afterRemoval() ``` import util from '@ohos.util' var arr = []; @@ -659,20 +655,26 @@ class Temperature { } } ``` + 51.constructor() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); ``` + 52.toString() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); var result = range.toString() // => [30,40] ``` + 53.intersect() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -682,7 +684,9 @@ var tempMidS = new Temperature(39); var rangeFir = new Scope(tempMiDF, tempMidS); var result = range.intersect(rangeFir) // => [35,39] ``` + 54.intersect() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -691,21 +695,27 @@ var tempMidS = new Temperature(39); var range = new Scope(tempLower, tempUpper); var result = range.intersect(tempMiDF, tempMidS) // => [35,39] ``` + 55.getUpper() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); var result = range.getUpper() // => 40 ``` + 56.getLower() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); var result = range.getLower() // => 30 ``` + 57.expand() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -714,7 +724,9 @@ var tempMidS = new Temperature(39); var range = new Scope(tempLower, tempUpper); var result = range.expand(tempMiDF, tempMidS) // => [30,40] ``` + 58.expand() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -724,7 +736,9 @@ var range = new Scope(tempLower, tempUpper); var rangeFir = new Scope(tempMiDF, tempMidS); var result = range.expand(rangeFir) // => [30,40] ``` + 59.expand() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -732,7 +746,9 @@ var tempMiDF = new Temperature(35); var range = new Scope(tempLower, tempUpper); var result = range.expand(tempMiDF) // => [30,40] ``` + 60.contains() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -740,7 +756,9 @@ var tempMiDF = new Temperature(35); var range = new Scope(tempLower, tempUpper); var result = range.contains(tempMiDF) // => true ``` + 61.contains() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -750,7 +768,9 @@ var tempMore = new Temperature(45); var rangeSec = new Scope(tempLess, tempMore); var result = range.contains(rangeSec) // => true ``` + 62.clamp() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -814,50 +834,44 @@ import util from '@ohos.util' var proc = new util.Types(); var result = proc.isBoxedPrimitive(new Boolean(false)); ``` -72.isCryptoKey() -``` -import util from '@ohos.util' -var proc = new util.Types(); -var result = proc.isCryptoKey(false); -``` -73.isDataView() +72.isDataView() ``` import util from '@ohos.util' var proc = new util.Types(); const ab = new ArrayBuffer(20); var result = proc.isDataView(new DataView(ab)); ``` -74.isDate() +73.isDate() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isDate(new Date()); ``` -75.isExternal() +74.isExternal() ``` import util from '@ohos.util' const data = util.createExternalType(); var reult13 = proc.isExternal(data); ``` -76.isFloat32Array() +75.isFloat32Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isFloat32Array(new Float32Array([])); ``` -77.isFloat64Array() +76.isFloat64Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isFloat64Array(new Float64Array([])); ``` -78.isGeneratorFunction() +77.isGeneratorFunction() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isGeneratorFunction(function* foo() {}); ``` -79.isGeneratorObject() +78.isGeneratorObject() ``` import util from '@ohos.util' var proc = new util.Types(); @@ -865,67 +879,61 @@ function* foo() {} const generator = foo(); var result = proc.isGeneratorObject(generator); ``` -80.isInt8Array() +79.isInt8Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isInt8Array(new Int8Array([])); ``` -81.isInt16Array() +80.isInt16Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isInt16Array(new Int16Array([])); ``` -82.isInt32Array() +81.isInt32Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isInt32Array(new Int32Array([])); ``` -83.isKeyObject() -``` -import util from '@ohos.util' -var proc = new util.Types(); -var result = proc.isKeyObject(0); -``` -84.isMap() +82.isMap() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isMap(new Map()); ``` -85.isMapIterator() +83.isMapIterator() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isMapIterator(map.keys()); ``` -86.isModuleNamespaceObject() +84.isModuleNamespaceObject() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isModuleNamespaceObject(util); ``` -87.isNativeError() +85.isNativeError() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isNativeError(new TypeError()); ``` -88.isNumberObject() +86.isNumberObject() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isNumberObject(new Number(0)); ``` -89.isPromise() +87.isPromise() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isPromise(Promise.resolve(42)); ``` -90.isProxy() +88.isProxy() ``` import util from '@ohos.util' var proc = new util.Types(); @@ -933,81 +941,81 @@ const target = {}; const proxy = new Proxy(target, {}); var result = proc.isProxy(proxy); ``` -91.isRegExp() +89.isRegExp() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isRegExp(new RegExp('abc')); ``` -92.isSet() +90.isSet() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isSet(new Set()); ``` -93.isSetIterator() +91.isSetIterator() ``` import util from '@ohos.util' var proc = new util.Types(); const set = new Set(); var result = proc.isSetIterator(set.keys()); ``` -94.isSharedArrayBuffer() +92.isSharedArrayBuffer() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isSharedArrayBuffer(new ArrayBuffer([])); ``` -95.isStringObject() +93.isStringObject() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isStringObject(new String('foo')); ``` -96.isSymbolObject() +94.isSymbolObject() ``` import util from '@ohos.util' var proc = new util.Types(); const symbols = Symbol('foo'); var result = proc.isSymbolObject(Object(symbols)); ``` -97.isTypedArray() +95.isTypedArray() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isTypedArray(new Float64Array([])); ``` -98.isUint8Array() +96.isUint8Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint8Array(new Uint8Array([])); ``` -99.isUint8ClampedArray() +97.isUint8ClampedArray() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint8ClampedArray(new Uint8ClampedArray([])); ``` -100.isUint16Array() +98.isUint16Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint16Array(new Uint16Array([])); ``` -101.isUint32Array() +99.isUint32Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint32Array(new Uint32Array([])); ``` -102.isWeakMap() +100.isWeakMap() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isWeakMap(new WeakMap()); ``` -103.isWeakSet() +101.isWeakSet() ``` import util from '@ohos.util' var proc = new util.Types(); @@ -1015,6 +1023,8 @@ var result = proc.isWeakSet(new WeakSet()); ``` ## Related warehouse + + [js_util_module subsystem](https://gitee.com/OHOS_STD/js_util_module) [base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md) diff --git a/README_zh.md b/README_zh.md index 9f3838a43c8998b989d9d46fdbcd0058d80c9bb5..91fce744db850ce0b1a85b4eedcd9aab6cc325bc 100644 --- a/README_zh.md +++ b/README_zh.md @@ -32,18 +32,18 @@ base/compileruntime/js_util_module/ ├── promiseWrapper() # promiseWrapper方法 ├── Class:Base64 # Base64类 │ ├── new Base64() # 创建Base64对象 +│ ├── encodeSync() # encodeSync方法 +│ ├── encodeToStringSync() # encodeToStringSync方法 +│ ├── decodeSync() # decodeSync方法 │ ├── encode() # encode方法 │ ├── encodeToString() # encodeToString方法 -│ ├── decode() # decode方法 -│ ├── encodeAsync() # encodeAsync方法 -│ ├── encodeToStringAsync() # encodeToStringAsync方法 -│ └── decodeAsync() # decodeAsync方法 +│ └── decode() # decode方法 ├── Class:RationalNumber # RationalNumber类 │ ├── new RationalNumber() # 创建RationalNumber对象 │ ├── createRationalFromString() # createRationalFromString方法 │ ├── compareTo() # compareTo方法 │ ├── equals() # equals方法 -│ ├── value() # value方法 +│ ├── valueOf() # valueOf方法 │ ├── getCommonDivisor() # getCommonDivisor方法 │ ├── getDenominator() # getDenominator方法 │ ├── getNumerator() # getNumerator方法 @@ -56,8 +56,8 @@ base/compileruntime/js_util_module/ │ ├── updateCapacity() # updateCapacity方法 │ ├── toString() # toString方法 │ ├── values() # values方法 -│ ├── size() # size方法 -│ ├── capacity() # capacity方法 +│ ├── length # length属性 +│ ├── getCapacity() # getCapacity方法 │ ├── clear() # clear方法 │ ├── getCreateCount() # getCreateCount方法 │ ├── getMissCount() # getMissCount方法 @@ -81,7 +81,7 @@ base/compileruntime/js_util_module/ | ├── intersect() # intersect方法 | ├── getUpper() # getUpper方法 | ├── getLower() # getLower方法 -| ├── expand() # expand方法 +| ├── expand() # expand方法 | ├── expand() # expand方法 | ├── expand() # expand法 | ├── contains() # contains方法 @@ -97,7 +97,6 @@ base/compileruntime/js_util_module/ ├── isBigUint64Array() # isBigUint64Array方法 ├── isBooleanObject() # isBooleanObject方法 ├── isBoxedPrimitive() # isBoxedPrimitive方法 - ├── isCryptoKey() # isCryptoKey方法 ├── isDataView() # isDataView方法 ├── isDate() # isDate方法 ├── isExternal() # isExternal方法 @@ -108,7 +107,6 @@ base/compileruntime/js_util_module/ ├── isInt8Array() # isInt8Array方法 ├── isInt16Array() # isInt16Array方法 ├── isInt32Array() # isInt32Array方法 - ├── isKeyObject() # isKeyObject方法 ├── isMap() # isMap方法 ├── isMapIterator() # isMapIterator方法 ├── isModuleNamespaceObject() # isModuleNamespaceObject方法 @@ -145,16 +143,16 @@ base/compileruntime/js_util_module/ | readonly fatal : boolean | 获取抛出异常的设置。 | | readonly ignoreBOM : boolean | 获取是否忽略bom标志的设置。 | | decode(input : Uint8Array, options?: { stream?: false }) : string | 输入要解码的数据,解出对应的string字符串。第一个参数input表示要解码的数据,第二个参数options表示一个bool标志,表示将跟随附加数据,默认为false。 | -| encode(src: Uint8Array): Uint8Array; | 使用Base64编码方案将指定u8数组中的所有字节编码到新分配的u8数组中。 | -| encodeToString(src: Uint8Array): string; | 使用Base64编码方案将指定的字节数组编码为String。 | -| decode(src: Uint8Array \| string): Uint8Array; | 使用Base64编码方案将Base64编码的字符串或输入u8数组解码为新分配的u8数组。 | -| encodeAsync(src: Uint8Array): Promise\; | 使用Base64编码方案将指定u8数组中的所有字节异步编码到新分配的u8数组中。 | -| encodeToStringAsync(src: Uint8Array): Promise\; | 使用Base64编码方案将指定的字节数组异步编码为String。 | -| decodeAsync(src: Uint8Array \| string): Promise\; | 使用Base64编码方案将Base64编码的字符串或输入u8数组异步解码为新分配的u8数组。 | +| encodeSync(src: Uint8Array): Uint8Array; | 使用Base64编码方案将指定u8数组中的所有字节编码到新分配的u8数组中。 | +| encodeToStringSync(src: Uint8Array): string; | 使用Base64编码方案将指定的字节数组编码为String。 | +| decodeSync(src: Uint8Array \| string): Uint8Array; | 使用Base64编码方案将Base64编码的字符串或输入u8数组解码为新分配的u8数组。 | +| encode(src: Uint8Array): Promise\; | 使用Base64编码方案将指定u8数组中的所有字节异步编码到新分配的u8数组中。 | +| encodeToString(src: Uint8Array): Promise\; | 使用Base64编码方案将指定的字节数组异步编码为String。 | +| decode(src: Uint8Array \| string): Promise\; | 使用Base64编码方案将Base64编码的字符串或输入u8数组异步解码为新分配的u8数组。 | | static createRationalFromString(rationalString: string): RationalNumber | 基于给定的字符串创建一个RationalNumber对象。 | | compareTo(another: RationalNumber): number | 将当前的RationalNumber对象与给定的对象进行比较。 | | equals(obj: object): number | 检查给定对象是否与当前 RationalNumber 对象相同。 | -| value(): number | 将当前的RationalNumber对象进行取整数值或者浮点数值。 | +| valueOf(): number | 将当前的RationalNumber对象进行取整数值或者浮点数值。 | | static getCommonDivisor(number1: number, number2: number,): number | 获得两个指定数的最大公约数。 | | getDenominator(): number | 获取当前的RationalNumber对象的分母。 | | getNumerator(): number | 获取当前的RationalNumber对象的分子。 | @@ -166,8 +164,8 @@ base/compileruntime/js_util_module/ | updateCapacity(newCapacity: number): void | 将缓冲区容量更新为指定容量,如果 newCapacity 小于或等于 0,则抛出此异常。 | | toString(): string | 返回对象的字符串表示形式,输出对象的字符串表示 | | values(): V[] | 获取当前缓冲区中所有值的列表,输出按升序返回当前缓冲区中所有值的列表,从最近访问到最近最少访问。 | -| size(): number | 获取当前缓冲区中值的总数,输出返回当前缓冲区中值的总数。 | -| capacity(): number | 获取当前缓冲区的容量,输出返回当前缓冲区的容量。 | +| length: number | 代表当前缓冲区中值的总数,输出返回当前缓冲区中值的总数。 | +| getCapacity(): number | 获取当前缓冲区的容量,输出返回当前缓冲区的容量。 | | clear(): void | 从当前缓冲区清除键值对,清除键值对后,调用afterRemoval()方法依次对其执行后续操作。 | | getCreateCount(): number | 获取createDefault()返回值的次数,输出返回createDefault()返回值的次数。 | | getMissCount(): number | 获取查询值不匹配的次数,输出返回查询值不匹配的次数。 | @@ -209,7 +207,6 @@ base/compileruntime/js_util_module/ | isBigUint64Array(value: Object): boolean | 检查输入的value是否是BigUint64Array数组类型。 | | isBooleanObject(value: Object): boolean | 检查输入的value是否是一个布尔对象类型。 | | isBoxedPrimitive(value: Object): boolean | 检查输入的value是否是Boolean或Number或String或Symbol对象类型。 | -| isCryptoKey(value: Object): boolean | 检查输入的value是否是CryptoKey对象类型。 | | isDataView(value: Object): boolean | 检查输入的value是否是DataView类型。 | | isDate(value: Object): boolean | 检查输入的value是否是Date类型。 | | isExternal(value: Object): boolean | 检查输入的value是否是一个native External值类型。 | @@ -220,7 +217,6 @@ base/compileruntime/js_util_module/ | isInt8Array(value: Object): boolean | 检查输入的value是否是Int8Array数组类型。 | | isInt16Array(value: Object): boolean | 检查输入的value是否是Int16Array数组类型。 | | isInt32Array(value: Object): boolean | 检查输入的value是否是Int32Array数组类型。 | -| isKeyObject(value: Object): boolean | 检查输入的value是否是KeyObject对象类型。 | | isMap(value: Object): boolean | 检查输入的value是否是Map类型。 | | isMapIterator(value: Object): boolean | 检查输入的value是否是Map的iterator类型。 | | isModuleNamespaceObject(value: Object): boolean | 检查输入的value是否是Module Namespace Object对象类型。 | @@ -348,52 +344,52 @@ newPromiseObj.then(res => { expect(res).strictEqual('HelloWorld'); }) ``` -13.encode() +13.encodeSync() ``` import util from '@ohos.util' var that = new util.Base64(); var array = new Uint8Array([115,49,51]); -var result = that.encode(array); +var result = that.encodeSync(array); ``` -14.encodeToString() +14.encodeToStringSync() ``` import util from '@ohos.util' var that = new util.Base64(); var array = new Uint8Array([115,49,51]); -var result = that.encodeToString(array); +var result = that.encodeToStringSync(array); ``` -15.decode() +15.decodeSync() ``` import util from '@ohos.util' var that = new util.Base64() var buff = 'czEz'; -var result = that.decode(buff); +var result = that.decodeSync(buff); ``` -16.encodeAsync() +16.encode() ``` import util from '@ohos.util' var that = new util.Base64() var array = new Uint8Array([115,49,51]); -await that.encodeAsync(array).then(val=>{ +await that.encode(array).then(val=>{ }) done() ``` -17.encodeToStringAsync() +17.encodeToString() ``` import util from '@ohos.util' var that = new util.Base64() var array = new Uint8Array([115,49,51]); -await that.encodeToStringAsync(array).then(val=>{ +await that.encodeToString(array).then(val=>{ }) done() ``` -18.decodeAsync() +18.decode() ``` import util from '@ohos.util' var that = new util.Base64() var buff = 'czEz'; -await that.decodeAsync(buff).then(val=>{ +await that.decode(buff).then(val=>{ }) done() ``` @@ -402,7 +398,7 @@ done() import util from '@ohos.util' var pro = new util.RationalNumber(0, 0); var res = pro.createRationalFromString("-1:2"); -var result1 = res.value(); +var result1 = res.valueOf(); ``` 20.compareTo() ``` @@ -418,11 +414,11 @@ var pro = new util.RationalNumber(2, 1); var proc = new util.RationalNumber(3, 4); var res = pro.equals(proc); ``` -22.value() +22.valueOf() ``` import util from '@ohos.util' var pro = new util.RationalNumber(2, 1); -var res = pro.value(); +var res = pro.valueOf(); ``` 23.getCommonDivisor() ``` @@ -459,16 +455,14 @@ var res = pro.isNaN(); import util from '@ohos.util' var pro = new util.RationalNumber(-2, 1); var res = pro.isZero(); - ``` 29.toString() ``` import util from '@ohos.util' var pro = new util.RationalNumber(-2, 1); var res = pro.toString(); - ``` -30.updateCapacity() +30.updateCapacity() ``` import util from '@ohos.util' var pro = new util.LruBuffer(); @@ -492,21 +486,21 @@ pro.put(2,"anhu"); pro.put("afaf","grfb"); var result = pro.values(); ``` -33.size() +33.length ``` import util from '@ohos.util' var pro = new util.LruBuffer(); pro.put(2,10); pro.put(1,8); -var result = pro.size(); +var result = pro.length; ``` -34.capacity() +34.getCapacity() ``` import util from '@ohos.util' var pro = new util.LruBuffer(); -var result = pro.capacity(); +var result = pro.getCapacity(); ``` -35.clear() +35.clear() ``` import util from '@ohos.util' var pro = new util.LruBuffer(); @@ -530,12 +524,14 @@ var result = pro.getMissCount(); ``` 38.getRemovalCount() ``` + import util from '@ohos.util' var pro = new util.LruBuffer(); pro.put(2,10); pro.updateCapacity(2); pro.put(50,22); var result = pro.getRemovalCount(); + ``` 39.getMatchCount() ``` @@ -560,6 +556,7 @@ pro.put(2,10); var result = pro.isEmpty(); ``` 42.get() + ``` import util from '@ohos.util' var pro = new util.LruBuffer(); @@ -607,14 +604,13 @@ pro.put(2,10); var result = pro.entries(); ``` 49.\[Symbol.iterator\]() - ``` import util from '@ohos.util' var pro = new util.LruBuffer(); -pro.put(2,10); +pro .put(2,10); var result = pro[symbol.iterator](); ``` -50.afterRemoval() +50.afterRemoval() ``` import util from '@ohos.util' var arr = [ ]; @@ -660,20 +656,26 @@ class Temperature { } } ``` + 51.constructor() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); ``` + 52.toString() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); var result = range.toString() // => [30,40] ``` + 53.intersect() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -683,7 +685,9 @@ var tempMidS = new Temperature(39); var rangeFir = new Scope(tempMiDF, tempMidS); var result = range.intersect(rangeFir) // => [35,39] ``` + 54.intersect() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -692,21 +696,27 @@ var tempMidS = new Temperature(39); var range = new Scope(tempLower, tempUpper); var result = range.intersect(tempMiDF, tempMidS) // => [35,39] ``` + 55.getUpper() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); var result = range.getUpper() // => 40 ``` + 56.getLower() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new Scope(tempLower, tempUpper); var result = range.getLower() // => 30 ``` + 57.expand() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -715,7 +725,9 @@ var tempMidS = new Temperature(39); var range = new Scope(tempLower, tempUpper); var result = range.expand(tempMiDF, tempMidS) // => [30,40] ``` + 58.expand() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -725,7 +737,9 @@ var range = new Scope(tempLower, tempUpper); var rangeFir = new Scope(tempMiDF, tempMidS); var result = range.expand(rangeFir) // => [30,40] ``` + 59.expand() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -733,7 +747,9 @@ var tempMiDF = new Temperature(35); var range = new Scope(tempLower, tempUpper); var result = range.expand(tempMiDF) // => [30,40] ``` + 60.contains() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -741,7 +757,9 @@ var tempMiDF = new Temperature(35); var range = new Scope(tempLower, tempUpper); var result = range.contains(tempMiDF) // => true ``` + 61.contains() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -751,7 +769,9 @@ var tempMore = new Temperature(45); var rangeSec = new Scope(tempLess, tempMore); var result = range.contains(rangeSec) // => true ``` + 62.clamp() + ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); @@ -815,50 +835,44 @@ import util from '@ohos.util' var proc = new util.Types(); var result = proc.isBoxedPrimitive(new Boolean(false)); ``` -72.isCryptoKey() -``` -import util from '@ohos.util' -var proc = new util.Types(); -var result = proc.isCryptoKey(false); -``` -73.isDataView() +72.isDataView() ``` import util from '@ohos.util' var proc = new util.Types(); const ab = new ArrayBuffer(20); var result = proc.isDataView(new DataView(ab)); ``` -74.isDate() +73.isDate() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isDate(new Date()); ``` -75.isExternal() +74.isExternal() ``` import util from '@ohos.util' const data = util.createExternalType(); var reult13 = proc.isExternal(data); ``` -76.isFloat32Array() +75.isFloat32Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isFloat32Array(new Float32Array([])); ``` -77.isFloat64Array() +76.isFloat64Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isFloat64Array(new Float64Array([])); ``` -78.isGeneratorFunction() +77.isGeneratorFunction() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isGeneratorFunction(function* foo() {}); ``` -79.isGeneratorObject() +78.isGeneratorObject() ``` import util from '@ohos.util' var proc = new util.Types(); @@ -866,67 +880,61 @@ function* foo() {} const generator = foo(); var result = proc.isGeneratorObject(generator); ``` -80.isInt8Array() +79.isInt8Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isInt8Array(new Int8Array([])); ``` -81.isInt16Array() +80.isInt16Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isInt16Array(new Int16Array([])); ``` -82.isInt32Array() +81.isInt32Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isInt32Array(new Int32Array([])); ``` -83.isKeyObject() -``` -import util from '@ohos.util' -var proc = new util.Types(); -var result = proc.isKeyObject(0); -``` -84.isMap() +82.isMap() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isMap(new Map()); ``` -85.isMapIterator() +83.isMapIterator() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isMapIterator(map.keys()); ``` -86.isModuleNamespaceObject() +84.isModuleNamespaceObject() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isModuleNamespaceObject(util); ``` -87.isNativeError() +85.isNativeError() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isNativeError(new TypeError()); ``` -88.isNumberObject() +86.isNumberObject() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isNumberObject(new Number(0)); ``` -89.isPromise() +87.isPromise() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isPromise(Promise.resolve(42)); ``` -90.isProxy() +88.isProxy() ``` import util from '@ohos.util' var proc = new util.Types(); @@ -934,81 +942,81 @@ const target = {}; const proxy = new Proxy(target, {}); var result = proc.isProxy(proxy); ``` -91.isRegExp() +89.isRegExp() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isRegExp(new RegExp('abc')); ``` -92.isSet() +90.isSet() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isSet(new Set()); ``` -93.isSetIterator() +91.isSetIterator() ``` import util from '@ohos.util' var proc = new util.Types(); const set = new Set(); var result = proc.isSetIterator(set.keys()); ``` -94.isSharedArrayBuffer() +92.isSharedArrayBuffer() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isSharedArrayBuffer(new ArrayBuffer([])); ``` -95.isStringObject() +93.isStringObject() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isStringObject(new String('foo')); ``` -96.isSymbolObject() +94.isSymbolObject() ``` import util from '@ohos.util' var proc = new util.Types(); const symbols = Symbol('foo'); var result = proc.isSymbolObject(Object(symbols)); ``` -97.isTypedArray() +95.isTypedArray() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isTypedArray(new Float64Array([])); ``` -98.isUint8Array() +96.isUint8Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint8Array(new Uint8Array([])); ``` -99.isUint8ClampedArray() +97.isUint8ClampedArray() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint8ClampedArray(new Uint8ClampedArray([])); ``` -100.isUint16Array() +98.isUint16Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint16Array(new Uint16Array([])); ``` -101.isUint32Array() +99.isUint32Array() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isUint32Array(new Uint32Array([])); ``` -102.isWeakMap() +100.isWeakMap() ``` import util from '@ohos.util' var proc = new util.Types(); var result = proc.isWeakMap(new WeakMap()); ``` -103.isWeakSet() +101.isWeakSet() ``` import util from '@ohos.util' var proc = new util.Types(); @@ -1016,6 +1024,8 @@ var result = proc.isWeakSet(new WeakSet()); ``` ## 相关仓 + + [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/util/build_ts_js.py b/build_ts_js.py similarity index 71% rename from util/build_ts_js.py rename to build_ts_js.py index cb4d8eba0f61dd5f29b8c6e6a250916b5f41b843..93af1187c962f41bd0d4cfae5baeefb3ab6d7fe9 100755 --- a/util/build_ts_js.py +++ b/build_ts_js.py @@ -13,10 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. import os +import shutil import platform import argparse import subprocess + def run_command(in_cmd): print(" ".join(in_cmd)) proc = subprocess.Popen(in_cmd, stdout=subprocess.PIPE, @@ -25,31 +27,34 @@ def run_command(in_cmd): shell=False) stdout, stderr = proc.communicate() if stdout != "": - print(stdout) - exit(1) - + raise Exception(stdout) if __name__ == '__main__': - BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), "../..")) - os.chdir("%s/base/compileruntime/js_util_module/util" % BUILD_PATH) PARSER_INST = argparse.ArgumentParser() PARSER_INST.add_argument('--dst-file', help='the converted target file') + PARSER_INST.add_argument('--module-path', + help='the module path') + PARSER_INST.add_argument('--out-file', + help='js output file') INPUT_ARGUMENTS = PARSER_INST.parse_args() - + BUILD_PATH = os.path.abspath(os.path.join(os.getcwd(), "../..")) + os.chdir(("%s" + INPUT_ARGUMENTS.module_path) % BUILD_PATH) NODE_PATH = '../../../../prebuilts/build-tools/common/nodejs/\ node-v12.18.4-linux-x64/bin/node' + if not os.path.exists(NODE_PATH): + raise Exception('NO souch file or directory') TSC_PATH = '../../../../ark/ts2abc/ts2panda/node_modules/\ typescript/bin/tsc' CMD_INST = [NODE_PATH, TSC_PATH] run_command(CMD_INST) + if not os.path.exists(INPUT_ARGUMENTS.out_file): + raise Exception('error:NO souch file or directory') + CMD_INST = shutil.copy(INPUT_ARGUMENTS.out_file, INPUT_ARGUMENTS.dst_file) - CMD_INST = ['cp', "-r", './out/util_js.js', INPUT_ARGUMENTS.dst_file] - run_command(CMD_INST) + CMD_INST = shutil.rmtree('./out') - CMD_INST = ['rm', "-rf", './out'] - run_command(CMD_INST) - exit(0) + exit(0) \ No newline at end of file diff --git a/ohos.build b/ohos.build index e5737bd24e47b197bb6bce8f9b9cc367ff4e96dd..b85022b46f4aed35299b6ac2e4a7130135d8b852 100755 --- a/ohos.build +++ b/ohos.build @@ -12,7 +12,6 @@ "inner_kits": [ ], "test_list": [ - "//base/compileruntime/js_util_module/test/unittest:unittest" ] } } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn deleted file mode 100755 index 6e24ec63c62873d59f6059ff65c0b598e3ba9a82..0000000000000000000000000000000000000000 --- a/test/unittest/BUILD.gn +++ /dev/null @@ -1,67 +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/test.gni") - -if (is_standard_system) { - module_output_path = "jsapi_util/util" -} - -ohos_unittest("test_util_unittest") { - module_out_path = module_output_path - - configs = [ "//ark/js_runtime:ark_jsruntime_public_config" ] - - include_dirs = [ - "//base/compileruntime/js_util_module/util", - "//ark/js_runtime", - "//foundation/ace/napi", - "//foundation/ace/napi/interfaces/kits", - "//foundation/ace/napi/native_engine", - "//foundation/ace/napi/native_engine/impl/ark", - "//third_party/icu/icu4c/source/common", - "//third_party/googletest/include", - "//third_party/node/src", - "//utils/native/base/include", - ] - - cflags = [ "-g3" ] - - sources = [ - "test_ark.cpp", - "test_util.cpp", - ] - - deps = [ - "//ark/js_runtime:libark_jsruntime", - "//base/compileruntime/js_util_module/util:util_packages", - "//foundation/ace/napi/:ace_napi", - "//foundation/ace/napi/:ace_napi_ark", - "//third_party/googletest:gtest", - "//third_party/googletest:gtest_main", - "//third_party/icu/icu4c:static_icuuc", - "//third_party/libuv:uv_static", - "//utils/native/base:utils", - "//utils/native/base:utilsecurec", - ] - - if (is_standard_system) { - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] - } else { - external_deps = [ "hilog:libhilog" ] - } -} - -group("unittest") { - testonly = true - deps = [ ":test_util_unittest" ] -} diff --git a/test/unittest/test.h b/test/unittest/test.h deleted file mode 100755 index 753199cf332de264e2097396495923991dfa140a..0000000000000000000000000000000000000000 --- a/test/unittest/test.h +++ /dev/null @@ -1,33 +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. - */ - -#ifndef FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H -#define FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H - -#include "native_engine.h" - -#include "gtest/gtest.h" - -class NativeEngineTest : public testing::Test { -public: - NativeEngineTest(); - virtual ~NativeEngineTest(); - void SetUp() override {} - void TearDown() override {} -protected: - NativeEngine *engine_; -}; - -#endif /* FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H */ \ No newline at end of file diff --git a/test/unittest/test_ark.cpp b/test/unittest/test_ark.cpp deleted file mode 100644 index c2fc82bf53d72321c097733462f57e4b200dfa28..0000000000000000000000000000000000000000 --- a/test/unittest/test_ark.cpp +++ /dev/null @@ -1,62 +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 "test.h" - -#include "utils/log.h" -#include "ark_native_engine.h" - -using panda::RuntimeOption; -static NativeEngine *g_nativeEngine = nullptr; - -NativeEngineTest::NativeEngineTest() -{ - engine_ = g_nativeEngine; -} - -NativeEngineTest::~NativeEngineTest() {} - -int main(int argc, char **argv) -{ - testing::GTEST_FLAG(output) = "xml:./"; - testing::InitGoogleTest(&argc, argv); - - // Setup - RuntimeOption option; - option.SetGcType(RuntimeOption::GC_TYPE::GEN_GC); - - const int64_t poolSize = 0x1000000; // 16M - option.SetGcPoolSize(poolSize); - - option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); - option.SetDebuggerLibraryPath(""); - EcmaVM *vm = panda::JSNApi::CreateJSVM(option); - if (vm == nullptr) { - return 0; - } - - g_nativeEngine = new ArkNativeEngine(vm, nullptr); - - int ret = testing::UnitTest::GetInstance()->Run(); - - g_nativeEngine->Loop(LOOP_NOWAIT); - - delete g_nativeEngine; - g_nativeEngine = nullptr; - panda::JSNApi::DestroyJSVM(vm); - vm = nullptr; - - return ret; -} diff --git a/test/unittest/test_util.cpp b/test/unittest/test_util.cpp deleted file mode 100755 index af2dbbf5dadbd77c2a369fd3bb30561093d6424e..0000000000000000000000000000000000000000 --- a/test/unittest/test_util.cpp +++ /dev/null @@ -1,679 +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 "test.h" - -#include -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -#include "securec.h" -#include "utils/log.h" -#include "js_textdecoder.h" -#include "js_textencoder.h" - -#define ASSERT_CHECK_CALL(call) \ - { \ - ASSERT_EQ(call, napi_ok); \ - } - -#define ASSERT_CHECK_VALUE_TYPE(env, value, type) \ - { \ - napi_valuetype valueType = napi_undefined; \ - ASSERT_TRUE(value != nullptr); \ - ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \ - ASSERT_EQ(valueType, type); \ - } - - -/* @tc.name: getEncodingTest001 - * @tc.desc: Test acquire encoding mode. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, getEncodingTest001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - - OHOS::Util::TextEncoder textEncoder(env); - napi_value result = textEncoder.GetEncoding(); - - char *buffer = nullptr; - size_t bufferSize = 0; - napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; - napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize); - } - - ASSERT_STREQ(buffer, "utf-8"); - if (buffer != nullptr) { - delete []buffer; - buffer = nullptr; - } -} - -/** - * @tc.name: textEncodeTest001 - * @tc.desc: Test encode src. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, textEncodeTest001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - OHOS::Util::TextEncoder textEncoder(env); - - std::string input = "abc123"; - napi_value src = nullptr; - napi_create_string_utf8(env, input.c_str(), input.size(), &src); - napi_value result = textEncoder.Encode(src); - - char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0}; - - napi_typedarray_type type; - size_t srcLength = 0; - void* srcData = nullptr; - napi_value srcBuffer = nullptr; - size_t byteOffset = 0; - - napi_get_typedarray_info( - env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); - - ASSERT_EQ(srcLength, 6); - char* res = reinterpret_cast(srcData); - - res[srcLength] = 0; - ASSERT_STREQ(res, excepted); -} - -/** - * @tc.name: textEncodeTest001 - * @tc.desc: Test encode src. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, textEncodeTest002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - OHOS::Util::TextEncoder textEncoder(env); - - std::string input = ""; - napi_value src = nullptr; - napi_create_string_utf8(env, input.c_str(), input.size(), &src); - napi_value result = textEncoder.Encode(src); - - napi_typedarray_type type; - size_t srcLength = 0; - void* srcData = nullptr; - napi_value srcBuffer = nullptr; - size_t byteOffset = 0; - - napi_get_typedarray_info( - env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); - - ASSERT_STREQ((char*)srcData, nullptr); -} - -/** - * @tc.name: textEncodeIntoTest001 - * @tc.desc: Test returns a dictionary object indicating the progress of the encoding - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, textEncodeIntoTest001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("textEncodeIntoTest001 start"); - napi_env env = (napi_env)engine_; - OHOS::Util::TextEncoder textEncoder(env); - - std::string input = "abc123"; - napi_value src = nullptr; - napi_create_string_utf8(env, input.c_str(), input.size(), &src); - - napi_value arrayBuffer = nullptr; - void* arrayBufferPtr = nullptr; - size_t arrayBufferSize = 20; - napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); - - napi_value dest = nullptr; - napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest); - - napi_value result = textEncoder.EncodeInto(src, dest); - - napi_value read = nullptr; - napi_get_named_property(env, result, "read", &read); - - uint32_t resRead = 0; - - napi_get_value_uint32(env, read, &resRead); - - napi_value written = nullptr; - napi_get_named_property(env, result, "written", &written); - - uint32_t resWritten = 0; - napi_get_value_uint32(env, read, &resWritten); - - ASSERT_EQ(resRead, (uint32_t)6); - ASSERT_EQ(resWritten, (uint32_t)6); -} - - -/** - * @tc.name: GetEncoding001 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetEncoding001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = -1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value testString = textDecoder.GetEncoding(); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - std::string tmpTestStr = "utf-8"; - size_t strLength = 0; - char* buffer = nullptr; - if (bufferSize > 0) { - buffer = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength); - } - const char *result = tmpTestStr.c_str(); - size_t resultLength = tmpTestStr.length(); - ASSERT_STREQ(result, buffer); - ASSERT_EQ(resultLength, strLength); - if (buffer != nullptr) { - delete []buffer; - buffer = nullptr; - } -} - -/** - * @tc.name: GetFatal001 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetFatal001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::GetFatal001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 1; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value naVal = textDecoder.GetFatal(); - bool result = false; - napi_get_value_bool(env, naVal, &result); - ASSERT_TRUE(result); -} - -/** - * @tc.name: GetFatal002 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetFatal002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::GetFatal002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value naVal = textDecoder.GetFatal(); - bool result = false; - napi_get_value_bool(env, naVal, &result); - ASSERT_FALSE(result); -} - -/** - * @tc.name: GetIgnoreBOM001 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetIgnoreBOM001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::GetIgnoreBOM001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value naVal = textDecoder.GetIgnoreBOM(); - bool result = false; - napi_get_value_bool(env, naVal, &result); - ASSERT_TRUE(result); -} - -/** - * @tc.name: decoderUtf8001 utf-8 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf8001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf8001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = -1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 3; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[3] = {0x61, 0x62, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - ASSERT_STREQ("abc", ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf8002 utf-8 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf8002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf8002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 3; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[3] = {0x61, 0x62, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - size_t length = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - ASSERT_STREQ("abc", ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le001 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 6; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - size_t length = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - ASSERT_STREQ("abc", ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le002 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 6; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - size_t length = 0; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - ASSERT_STREQ("abc", ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le003 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le003, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le003 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - size_t length = 0; - std::string tempStr01 = ""; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - tempStr01 = ch; - } - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le004 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le004, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le004 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = -1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - size_t length = 0; - std::string tempStr01 = ""; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - tempStr01 = ch; - } - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16be001 utf-16be - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16be001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16be001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16be"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 6; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[6] = {0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - ASSERT_STREQ("abc", ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16be002 utf-16be - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16be002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16be002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16be"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - std::string tempStr01 = ""; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - tempStr01 = ch; - } - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16be003 utf-16be - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16be003, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16be003 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16be"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff = nullptr; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2 = nullptr; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - std::string tempStr01 = ""; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - tempStr01 = ch; - } - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} diff --git a/util/BUILD.gn b/util/BUILD.gn index 1b57306c2496795e74df9abd86395365734f74ff..a4f09df76453566dcc6450b789bd546f338649f5 100755 --- a/util/BUILD.gn +++ b/util/BUILD.gn @@ -18,18 +18,20 @@ import("//foundation/ace/ace_engine/ace_config.gni") # compile .ts to .js. action("build_ts_js") { - script = "//base/compileruntime/js_util_module/util/build_ts_js.py" - + script = "//base/compileruntime/js_util_module/build_ts_js.py" args = [ "--dst-file", rebase_path(target_out_dir + "/util_js.js"), + "--module-path", + rebase_path("/base/compileruntime/js_util_module/util"), + "--out-file", + rebase_path("./out/util_js.js"), ] depfile = "$target_gen_dir/$target_name.d" outputs = [ target_out_dir + "/util_js.js" ] } base_output_path = get_label_info(":util_js", "target_out_dir") util_js_obj_path = base_output_path + "/util.o" - gen_js_obj("util_js") { input = "$target_out_dir/util_js.js" output = util_js_obj_path diff --git a/util/js_base64.cpp b/util/js_base64.cpp index 4581bb471cc22ac2f36cf319033ec0cb77451f4f..c613ff6e83efe03a1ed325e221e865cd47e32029 100755 --- a/util/js_base64.cpp +++ b/util/js_base64.cpp @@ -39,7 +39,7 @@ namespace OHOS::Util { Base64::Base64(napi_env env_) : env(env_) {} /* base64 encode */ - napi_value Base64::Encode(napi_value src) + napi_value Base64::EncodeSync(napi_value src) { napi_typedarray_type type; size_t byteOffset = 0; @@ -48,7 +48,10 @@ namespace OHOS::Util { napi_value resultBuffer = nullptr; NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset)); inputEncode_ = static_cast(resultData) + byteOffset; - const unsigned char *rets = EncodeAchieve(inputEncode_, length); + unsigned char *rets = EncodeAchieve(inputEncode_, length); + if (rets == nullptr) { + napi_throw_error(env, "-1", "encode input is null"); + } void *data = nullptr; napi_value arrayBuffer = nullptr; size_t bufferSize = outputLen; @@ -65,7 +68,7 @@ namespace OHOS::Util { } /* base64 encodeToString */ - napi_value Base64::EncodeToString(napi_value src) + napi_value Base64::EncodeToStringSync(napi_value src) { napi_typedarray_type type; size_t byteOffset = 0; @@ -75,6 +78,9 @@ namespace OHOS::Util { NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset)); inputEncode_ = static_cast(resultData) + byteOffset; unsigned char *ret = EncodeAchieve(inputEncode_, length); + if (ret == nullptr) { + napi_throw_error(env, "-1", "encodeToString input is null"); + } const char *encString = reinterpret_cast(ret); napi_value resultStr = nullptr; NAPI_CALL(env, napi_create_string_utf8(env, encString, strlen(encString), &resultStr)); @@ -96,11 +102,16 @@ namespace OHOS::Util { if (outputLen > 0) { ret = new unsigned char[outputLen + 1]; if (memset_s(ret, outputLen + 1, '\0', outputLen + 1) != 0) { + HILOG_ERROR("encode ret memset_s failed"); FreeMemory(ret); - napi_throw_error(env, "-1", "ret path memset_s failed"); + return nullptr; } } else { - napi_throw_error(env, "-2", "outputLen is error !"); + HILOG_ERROR("outputLen is error"); + return nullptr; + } + if (ret == nullptr) { + return ret; } while (inp < inputLen) { temp = 0; @@ -127,7 +138,7 @@ namespace OHOS::Util { } /* base64 decode */ - napi_value Base64::Decode(napi_value src) + napi_value Base64::DecodeSync(napi_value src) { napi_valuetype valuetype = napi_undefined; napi_typeof(env, src, &valuetype); @@ -152,8 +163,10 @@ namespace OHOS::Util { } else { napi_throw_error(env, "-2", "prolen is error !"); } - napi_get_value_string_utf8(env, src, inputString, prolen + 1, &prolen); - pret = DecodeAchieve(inputString, prolen); + if (inputString != nullptr) { + napi_get_value_string_utf8(env, src, inputString, prolen + 1, &prolen); + pret = DecodeAchieve(inputString, prolen); + } } else if (type == napi_typedarray_type::napi_uint8_array) { inputDecode_ = static_cast(resultData) + byteOffset; pret = DecodeAchieve(inputDecode_, length); @@ -203,6 +216,9 @@ namespace OHOS::Util { } else { napi_throw_error(env, "-2", "retLen is error !"); } + if (retDecode == nullptr) { + return retDecode; + } while (inp < (inputLen - equalCount)) { temp = 0; bitWise = 0; @@ -266,24 +282,23 @@ namespace OHOS::Util { } /* Memory cleanup function */ - void Base64::FreeMemory(const unsigned char *address) + void Base64::FreeMemory(unsigned char *address) { - const unsigned char *temp = address; + unsigned char *temp = address; if (temp != nullptr) { delete[] temp; temp = nullptr; } } - void Base64::FreeMemory(const char *address) + void Base64::FreeMemory(char *address) { - const char *temp = address; + char *temp = address; if (temp != nullptr) { delete[] temp; temp = nullptr; } } - - napi_value Base64::EncodeAsync(napi_value src) + napi_value Base64::Encode(napi_value src) { napi_typedarray_type type; size_t byteOffset = 0; @@ -296,8 +311,7 @@ namespace OHOS::Util { CreateEncodePromise(inputEncode, length); return stdEncodeInfo_->promise; } - - napi_value Base64::EncodeToStringAsync(napi_value src) + napi_value Base64::EncodeToString(napi_value src) { napi_typedarray_type type; size_t byteOffset = 0; @@ -310,7 +324,6 @@ namespace OHOS::Util { CreateEncodeToStringPromise(inputEncode, length); return stdEncodeInfo_->promise; } - void Base64::CreateEncodePromise(unsigned char *inputDecode, size_t length) { napi_value resourceName = nullptr; @@ -324,7 +337,6 @@ namespace OHOS::Util { reinterpret_cast(stdEncodeInfo_), &stdEncodeInfo_->worker); napi_queue_async_work(env, stdEncodeInfo_->worker); } - void Base64::CreateEncodeToStringPromise(unsigned char *inputDecode, size_t length) { napi_value resourceName = nullptr; @@ -337,7 +349,6 @@ namespace OHOS::Util { reinterpret_cast(stdEncodeInfo_), &stdEncodeInfo_->worker); napi_queue_async_work(env, stdEncodeInfo_->worker); } - unsigned char *EncodeAchieves(EncodeInfo *encodeInfo) { const unsigned char *input = encodeInfo->sinputEncode; @@ -361,6 +372,9 @@ namespace OHOS::Util { } else { napi_throw_error(encodeInfo->env, "-2", "outputLen is error !"); } + if (ret == nullptr) { + return ret; + } while (inp < inputLen) { temp = 0; bitWise = 0; @@ -384,14 +398,12 @@ namespace OHOS::Util { ret[index] = 0; return ret; } - void Base64::ReadStdEncode(napi_env env, void *data) { auto stdEncodeInfo = reinterpret_cast(data); unsigned char *rets = EncodeAchieves(stdEncodeInfo); stdEncodeInfo->sinputEncoding = rets; } - void Base64::EndStdEncode(napi_env env, napi_status status, void *buffer) { auto stdEncodeInfo = reinterpret_cast(buffer); @@ -411,14 +423,12 @@ namespace OHOS::Util { delete[] stdEncodeInfo->sinputEncoding; delete stdEncodeInfo; } - void Base64::ReadStdEncodeToString(napi_env env, void *data) { auto stdEncodeInfo = reinterpret_cast(data); unsigned char *rets = EncodeAchieves(stdEncodeInfo); stdEncodeInfo->sinputEncoding = rets; } - void Base64::EndStdEncodeToString(napi_env env, napi_status status, void *buffer) { auto stdEncodeInfo = reinterpret_cast(buffer); @@ -430,8 +440,7 @@ namespace OHOS::Util { delete[] stdEncodeInfo->sinputEncoding; delete stdEncodeInfo; } - - napi_value Base64::DecodeAsync(napi_value src) + napi_value Base64::Decode(napi_value src) { napi_valuetype valuetype = napi_undefined; napi_typeof(env, src, &valuetype); @@ -456,7 +465,7 @@ namespace OHOS::Util { } else { napi_throw_error(env, "-2", "prolen is error !"); } - napi_get_value_string_utf8(env, src, inputString, prolen+1, &prolen); + napi_get_value_string_utf8(env, src, inputString, prolen + 1, &prolen); CreateDecodePromise(inputString, prolen); } else if (type == napi_typedarray_type::napi_uint8_array) { inputDecode = static_cast(resultData) + byteOffset; @@ -465,7 +474,6 @@ namespace OHOS::Util { delete[] inputString; return stdDecodeInfo_->promise; } - void Base64::CreateDecodePromise(char *inputDecode, size_t length) { napi_value resourceName = nullptr; @@ -479,8 +487,6 @@ namespace OHOS::Util { reinterpret_cast(stdDecodeInfo_), &stdDecodeInfo_->worker); napi_queue_async_work(env, stdDecodeInfo_->worker); } - - /* Decoding lookup function */ size_t Finds(char ch) { size_t couts = 0; @@ -491,7 +497,6 @@ namespace OHOS::Util { } return couts; } - size_t DecodeOut(size_t equalCount, size_t retLen, DecodeInfo *decodeInfo) { if (equalCount == 1) { @@ -516,7 +521,6 @@ namespace OHOS::Util { } return retLen; } - unsigned char *DecodeAchieves(DecodeInfo *decodeInfo) { const char *input = decodeInfo->sinputDecode; @@ -567,14 +571,12 @@ namespace OHOS::Util { retDecode[index] = 0; return retDecode; } - void Base64::ReadStdDecode(napi_env env, void *data) { auto stdDecodeInfo = reinterpret_cast(data); unsigned char *rets = DecodeAchieves(stdDecodeInfo); stdDecodeInfo->sinputDecoding = rets; } - void Base64::EndStdDecode(napi_env env, napi_status status, void *buffer) { auto stdDecodeInfo = reinterpret_cast(buffer); @@ -594,4 +596,4 @@ namespace OHOS::Util { delete[] stdDecodeInfo->sinputDecoding; delete stdDecodeInfo; } -} +} \ No newline at end of file diff --git a/util/js_base64.h b/util/js_base64.h index e85e775b019c7416a5964e4649b9d453919be9a7..20d970542ded8c942db36b76df5cf26c7f699fb9 100755 --- a/util/js_base64.h +++ b/util/js_base64.h @@ -46,34 +46,31 @@ namespace OHOS::Util { napi_env env; }; - enum ConverterFlags { - BIT_FLG = 0x40, - SIXTEEN_FLG = 0x3F, - XFF_FLG = 0xFF, - }; - + enum ConverterFlags { + BIT_FLG = 0x40, + SIXTEEN_FLG = 0x3F, + XFF_FLG = 0xFF, + }; unsigned char *EncodeAchieves(EncodeInfo *encodeInfo); unsigned char *DecodeAchieves(DecodeInfo *decodeInfo); - class Base64 { public: explicit Base64(napi_env env); virtual ~Base64() {} + napi_value EncodeSync(napi_value src); + napi_value EncodeToStringSync(napi_value src); + napi_value DecodeSync(napi_value src); napi_value Encode(napi_value src); napi_value EncodeToString(napi_value src); napi_value Decode(napi_value src); - - napi_value EncodeAsync(napi_value src); - napi_value EncodeToStringAsync(napi_value src); - napi_value DecodeAsync(napi_value src); private: napi_env env; unsigned char *DecodeAchieve(const char *input, size_t inputLen); unsigned char *EncodeAchieve(const unsigned char *input, size_t inputLen); size_t Finds(char ch); size_t DecodeOut(size_t equalCount, size_t retLen); - void FreeMemory(const unsigned char *address); - void FreeMemory(const char *address); + void FreeMemory(unsigned char *address); + void FreeMemory(char *address); size_t retLen = 0; size_t decodeOutLen = 0; size_t outputLen = 0; @@ -81,7 +78,6 @@ namespace OHOS::Util { const unsigned char *inputEncode_ = nullptr; const char *inputDecode_ = nullptr; unsigned char *retDecode = nullptr; - void CreateEncodePromise(unsigned char *inputDecode, size_t length); void CreateEncodeToStringPromise(unsigned char *inputDecode, size_t length); void CreateDecodePromise(char *inputDecode, size_t length); diff --git a/util/js_types.cpp b/util/js_types.cpp index bd1db150d6180941a0eb0b5d8b325963c4537e2f..57e9f7f19dc3fb4787f27ac2af322c8be8ceb35f 100644 --- a/util/js_types.cpp +++ b/util/js_types.cpp @@ -16,15 +16,13 @@ #include "js_types.h" #include #include -#include "utils/log.h" #include "securec.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" +#include "utils/log.h" namespace OHOS::Util { - Types::Types(napi_env env_) : env_(env_) {} + Types::Types(napi_env env) : env_(env) {} - napi_value Types::IsAnyArrayBuffer(napi_value src) + napi_value Types::IsAnyArrayBuffer(napi_value src) const { bool flag = false; bool rstFlag = false; @@ -37,13 +35,13 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsArrayBufferView(napi_value src) + napi_value Types::IsArrayBufferView(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; napi_value rst = nullptr; bool flag = false; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); - if (valuetype != napi_valuetype::napi_object) { + napi_status rstStatus = napi_typeof(env_, src, &valuetype); + if ((valuetype != napi_valuetype::napi_object) || (rstStatus != napi_ok)) { napi_get_boolean(env_, flag, &rst); return rst; } @@ -53,37 +51,40 @@ namespace OHOS::Util { napi_get_boolean(env_, rstFlag, &rst); return rst; } - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, &resultData, &resultBuffer, &byteOffset)); - switch (type) { - case napi_typedarray_type::napi_int8_array: - case napi_typedarray_type::napi_uint8_array: - case napi_typedarray_type::napi_uint8_clamped_array: - case napi_typedarray_type::napi_int16_array: - case napi_typedarray_type::napi_uint16_array: - case napi_typedarray_type::napi_int32_array: - case napi_typedarray_type::napi_uint32_array: - case napi_typedarray_type::napi_float32_array: - case napi_typedarray_type::napi_float64_array: - flag = true; - break; - default : - flag = false; - break; + napi_status rstSta = napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); + if (rstSta == napi_ok) { + switch (type) { + case napi_typedarray_type::napi_int8_array: + case napi_typedarray_type::napi_uint8_array: + case napi_typedarray_type::napi_uint8_clamped_array: + case napi_typedarray_type::napi_int16_array: + case napi_typedarray_type::napi_uint16_array: + case napi_typedarray_type::napi_int32_array: + case napi_typedarray_type::napi_uint32_array: + case napi_typedarray_type::napi_float32_array: + case napi_typedarray_type::napi_float64_array: + flag = true; + break; + default: + flag = false; + break; + } } napi_get_boolean(env_, flag, &rst); return rst; } - napi_value Types::IsArgumentsObject(napi_value src) + napi_value Types::IsArgumentsObject(napi_value src) const { - napi_valuetype result; - NAPI_CALL(env_, napi_typeof(env_, src, &result)); + napi_valuetype result = napi_undefined; bool flag = false; + napi_typeof(env_, src, &result); if (result == napi_object) { NAPI_CALL(env_, napi_is_arguments_object(env_, src, &flag)); } @@ -92,7 +93,7 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsArrayBuffer(napi_value src) + napi_value Types::IsArrayBuffer(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_arraybuffer(env_, src, &flag)); @@ -101,11 +102,11 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsAsyncFunction(napi_value src) + napi_value Types::IsAsyncFunction(napi_value src) const { - napi_valuetype result; - NAPI_CALL(env_, napi_typeof(env_, src, &result)); + napi_valuetype result = napi_undefined; bool flag = false; + napi_typeof(env_, src, &result); if (result == napi_function) { NAPI_CALL(env_, napi_is_async_function(env_, src, &flag)); } @@ -114,15 +115,15 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsBigInt64Array(napi_value src) + napi_value Types::IsBigInt64Array(napi_value src) const { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, &resultData, &resultBuffer, &byteOffset)); bool flag = false; + napi_get_typedarray_info(env_, src, &type, &length, &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_bigint64_array) { flag = true; } @@ -132,15 +133,15 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsBigUint64Array(napi_value src) + napi_value Types::IsBigUint64Array(napi_value src) const { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, &resultData, &resultBuffer, &byteOffset)); bool flag = false; + napi_get_typedarray_info(env_, src, &type, &length, &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_biguint64_array) { flag = true; } @@ -150,10 +151,10 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsBooleanObject(napi_value src) + napi_value Types::IsBooleanObject(napi_value src) const { - napi_valuetype result; - NAPI_CALL(env_, napi_typeof(env_, src, &result)); + napi_valuetype result = napi_undefined; + napi_typeof(env_, src, &result); bool flag = false; if (result == napi_object) { NAPI_CALL(env_, napi_is_boolean_object(env_, src, &flag)); @@ -163,7 +164,7 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsBoxedPrimitive(napi_value src) + napi_value Types::IsBoxedPrimitive(napi_value src) const { bool flag = false; bool rstNum = false; @@ -182,16 +183,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsCryptoKey(napi_value src) - { - bool flag = false; - napi_value rst = nullptr; - napi_get_boolean(env_, flag, &rst); - HILOG_INFO("The type is not supported!"); - return rst; - } - - napi_value Types::IsDataView(napi_value src) + napi_value Types::IsDataView(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_dataview(env_, src, &flag)); @@ -200,7 +192,7 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsDate(napi_value src) + napi_value Types::IsDate(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_date(env_, src, &flag)); @@ -209,11 +201,11 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsExternal(napi_value src) + napi_value Types::IsExternal(napi_value src) const { - napi_valuetype type; + napi_valuetype type = napi_undefined; bool flag = false; - NAPI_CALL(env_, napi_typeof(env_, src, &type)); + napi_typeof(env_, src, &type); if (type == napi_valuetype::napi_external) { flag = true; } @@ -222,20 +214,20 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsFloat32Array(napi_value src) + napi_value Types::IsFloat32Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_float32_array) { flag = true; } @@ -244,20 +236,20 @@ namespace OHOS::Util { return result; } - napi_value Types::IsFloat64Array(napi_value src) + napi_value Types::IsFloat64Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_float64_array) { flag = true; } @@ -266,10 +258,10 @@ namespace OHOS::Util { return result; } - napi_value Types::IsGeneratorFunction(napi_value src) + napi_value Types::IsGeneratorFunction(napi_value src) const { - napi_valuetype result; - NAPI_CALL(env_, napi_typeof(env_, src, &result)); + napi_valuetype result = napi_undefined; + napi_typeof(env_, src, &result); bool flag = false; if (result == napi_function) { NAPI_CALL(env_, napi_is_generator_function(env_, src, &flag)); @@ -279,7 +271,7 @@ namespace OHOS::Util { return rst; } - napi_value Types::IsGeneratorObject(napi_value src) + napi_value Types::IsGeneratorObject(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_generator_object(env_, src, &flag)); @@ -288,21 +280,21 @@ namespace OHOS::Util { return result; } - napi_value Types::IsInt8Array(napi_value src) + napi_value Types::IsInt8Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); - if (type == napi_typedarray_type::napi_int8_array) { + napi_status rstSta = napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); + if ((rstSta == napi_ok) && (type == napi_typedarray_type::napi_int8_array)) { flag = true; } } @@ -310,20 +302,20 @@ namespace OHOS::Util { return result; } - napi_value Types::IsInt16Array(napi_value src) + napi_value Types::IsInt16Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_int16_array) { flag = true; } @@ -332,20 +324,20 @@ namespace OHOS::Util { return result; } - napi_value Types::IsInt32Array(napi_value src) + napi_value Types::IsInt32Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_int32_array) { flag = true; } @@ -354,16 +346,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsKeyObject(napi_value src) - { - bool flag = false; - napi_value result = nullptr; - napi_get_boolean(env_, flag, &result); - HILOG_INFO("The type is not supported!"); - return result; - } - - napi_value Types::IsMap(napi_value src) + napi_value Types::IsMap(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_map(env_, src, &flag)); @@ -372,7 +355,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsMapIterator(napi_value src) + napi_value Types::IsMapIterator(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_map_iterator(env_, src, &flag)); @@ -381,7 +364,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsModuleNamespaceObject(napi_value src) + napi_value Types::IsModuleNamespaceObject(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_module_namespace_object(env_, src, &flag)); @@ -391,7 +374,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsNativeError(napi_value src) + napi_value Types::IsNativeError(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_error(env_, src, &flag)); @@ -400,12 +383,12 @@ namespace OHOS::Util { return result; } - napi_value Types::IsNumberObject(napi_value src) + napi_value Types::IsNumberObject(napi_value src) const { bool flag = false; - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { NAPI_CALL(env_, napi_is_number_object(env_, src, &flag)); } @@ -413,7 +396,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsPromise(napi_value src) + napi_value Types::IsPromise(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_promise(env_, src, &flag)); @@ -422,7 +405,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsProxy(napi_value src) + napi_value Types::IsProxy(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_proxy(env_, src, &flag)); @@ -431,7 +414,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsRegExp(napi_value src) + napi_value Types::IsRegExp(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_reg_exp(env_, src, &flag)); @@ -440,7 +423,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsSet(napi_value src) + napi_value Types::IsSet(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_set(env_, src, &flag)); @@ -449,7 +432,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsSetIterator(napi_value src) + napi_value Types::IsSetIterator(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_set_iterator(env_, src, &flag)); @@ -458,7 +441,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsSharedArrayBuffer(napi_value src) + napi_value Types::IsSharedArrayBuffer(napi_value src) const { bool flag = false; napi_value result = nullptr; @@ -467,11 +450,11 @@ namespace OHOS::Util { return result; } - napi_value Types::IsStringObject(napi_value src) + napi_value Types::IsStringObject(napi_value src) const { bool flag = false; - napi_valuetype valuetype; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_valuetype valuetype = napi_undefined; + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { NAPI_CALL(env_, napi_is_string_object(env_, src, &flag)); } @@ -480,12 +463,12 @@ namespace OHOS::Util { return result; } - napi_value Types::IsSymbolObject(napi_value src) + napi_value Types::IsSymbolObject(napi_value src) const { bool flag = false; napi_value result = nullptr; - napi_valuetype valuetype; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_valuetype valuetype = napi_undefined; + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { NAPI_CALL(env_, napi_is_symbol_object(env_, src, &flag)); } @@ -493,7 +476,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsTypedArray(napi_value src) + napi_value Types::IsTypedArray(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_typedarray(env_, src, &flag)); @@ -502,20 +485,20 @@ namespace OHOS::Util { return result; } - napi_value Types::IsUint8Array(napi_value src) + napi_value Types::IsUint8Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_uint8_array) { flag = true; } @@ -524,20 +507,20 @@ namespace OHOS::Util { return result; } - napi_value Types::IsUint8ClampedArray(napi_value src) + napi_value Types::IsUint8ClampedArray(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_uint8_clamped_array) { flag = true; } @@ -546,20 +529,20 @@ namespace OHOS::Util { return result; } - napi_value Types::IsUint16Array(napi_value src) + napi_value Types::IsUint16Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_uint16_array) { flag = true; } @@ -568,20 +551,20 @@ namespace OHOS::Util { return result; } - napi_value Types::IsUint32Array(napi_value src) + napi_value Types::IsUint32Array(napi_value src) const { - napi_valuetype valuetype; + napi_valuetype valuetype = napi_undefined; bool flag = false; napi_value result = nullptr; - NAPI_CALL(env_, napi_typeof(env_, src, &valuetype)); + napi_typeof(env_, src, &valuetype); if (valuetype == napi_valuetype::napi_object) { - napi_typedarray_type type; + napi_typedarray_type type = napi_int8_array; size_t byteOffset = 0; size_t length = 0; void* resultData = nullptr; napi_value resultBuffer = nullptr; - NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, - &resultData, &resultBuffer, &byteOffset)); + napi_get_typedarray_info(env_, src, &type, &length, + &resultData, &resultBuffer, &byteOffset); if (type == napi_typedarray_type::napi_uint32_array) { flag = true; } @@ -590,7 +573,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsWeakMap(napi_value src) + napi_value Types::IsWeakMap(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_weak_map(env_, src, &flag)); @@ -599,7 +582,7 @@ namespace OHOS::Util { return result; } - napi_value Types::IsWeakSet(napi_value src) + napi_value Types::IsWeakSet(napi_value src) const { bool flag = false; NAPI_CALL(env_, napi_is_weak_set(env_, src, &flag)); diff --git a/util/js_types.h b/util/js_types.h index fadbcd361409da00ea12cf6a0f2dcf79f42b631a..26858e6a6cf5a36e4276ae5e87e1394b7fa4ebb0 100644 --- a/util/js_types.h +++ b/util/js_types.h @@ -16,10 +16,9 @@ #include #include -#include "napi/native_node_api.h" -#include "native_engine/native_engine.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "native_engine/native_engine.h" #ifndef BASE_COMPILERUNTIME_JS_UTIL_MODULE_TYPES_CLASS_H #define BASE_COMPILERUNTIME_JS_UTIL_MODULE_TYPES_CLASS_H @@ -29,47 +28,45 @@ namespace OHOS::Util { public: explicit Types(napi_env env); virtual ~Types() {} - napi_value IsAnyArrayBuffer(napi_value src); - napi_value IsArrayBufferView(napi_value src); - napi_value IsArgumentsObject(napi_value src); - napi_value IsArrayBuffer(napi_value src); - napi_value IsAsyncFunction(napi_value src); - napi_value IsBigInt64Array(napi_value src); - napi_value IsBigUint64Array(napi_value src); - napi_value IsBooleanObject(napi_value src); - napi_value IsBoxedPrimitive(napi_value src); - napi_value IsCryptoKey(napi_value src); - napi_value IsDataView(napi_value src); - napi_value IsDate(napi_value src); - napi_value IsExternal(napi_value src); - napi_value IsFloat32Array(napi_value src); - napi_value IsFloat64Array(napi_value src); - napi_value IsGeneratorFunction(napi_value src); - napi_value IsGeneratorObject(napi_value src); - napi_value IsInt8Array(napi_value src); - napi_value IsInt16Array(napi_value src); - napi_value IsInt32Array(napi_value src); - napi_value IsKeyObject(napi_value src); - napi_value IsMap(napi_value src); - napi_value IsMapIterator(napi_value src); - napi_value IsModuleNamespaceObject(napi_value src); - napi_value IsNativeError(napi_value src); - napi_value IsNumberObject(napi_value src); - napi_value IsPromise(napi_value src); - napi_value IsProxy(napi_value src); - napi_value IsRegExp(napi_value src); - napi_value IsSet(napi_value src); - napi_value IsSetIterator(napi_value src); - napi_value IsSharedArrayBuffer(napi_value src); - napi_value IsStringObject(napi_value src); - napi_value IsSymbolObject(napi_value src); - napi_value IsTypedArray(napi_value src); - napi_value IsUint8Array(napi_value src); - napi_value IsUint8ClampedArray(napi_value src); - napi_value IsUint16Array(napi_value src); - napi_value IsUint32Array(napi_value src); - napi_value IsWeakMap(napi_value src); - napi_value IsWeakSet(napi_value src); + napi_value IsAnyArrayBuffer(napi_value src) const; + napi_value IsArrayBufferView(napi_value src) const; + napi_value IsArgumentsObject(napi_value src) const; + napi_value IsArrayBuffer(napi_value src) const; + napi_value IsAsyncFunction(napi_value src) const; + napi_value IsBigInt64Array(napi_value src) const; + napi_value IsBigUint64Array(napi_value src) const; + napi_value IsBooleanObject(napi_value src) const; + napi_value IsBoxedPrimitive(napi_value src) const; + napi_value IsDataView(napi_value src) const; + napi_value IsDate(napi_value src) const; + napi_value IsExternal(napi_value src) const; + napi_value IsFloat32Array(napi_value src) const; + napi_value IsFloat64Array(napi_value src) const; + napi_value IsGeneratorFunction(napi_value src) const; + napi_value IsGeneratorObject(napi_value src) const; + napi_value IsInt8Array(napi_value src) const; + napi_value IsInt16Array(napi_value src) const; + napi_value IsInt32Array(napi_value src) const; + napi_value IsMap(napi_value src) const; + napi_value IsMapIterator(napi_value src) const; + napi_value IsModuleNamespaceObject(napi_value src) const; + napi_value IsNativeError(napi_value src) const; + napi_value IsNumberObject(napi_value src) const; + napi_value IsPromise(napi_value src) const; + napi_value IsProxy(napi_value src) const; + napi_value IsRegExp(napi_value src) const; + napi_value IsSet(napi_value src) const; + napi_value IsSetIterator(napi_value src) const; + napi_value IsSharedArrayBuffer(napi_value src) const; + napi_value IsStringObject(napi_value src) const; + napi_value IsSymbolObject(napi_value src) const; + napi_value IsTypedArray(napi_value src) const; + napi_value IsUint8Array(napi_value src) const; + napi_value IsUint8ClampedArray(napi_value src) const; + napi_value IsUint16Array(napi_value src) const; + napi_value IsUint32Array(napi_value src) const; + napi_value IsWeakMap(napi_value src) const; + napi_value IsWeakSet(napi_value src) const; private: napi_env env_; }; diff --git a/util/native_module_util.cpp b/util/native_module_util.cpp index 67faef9d8d7479ba1e546956de59d9cd0d18ea0c..d932b2831080f2becafdc2b372d1e26d8688a1f6 100755 --- a/util/native_module_util.cpp +++ b/util/native_module_util.cpp @@ -451,7 +451,6 @@ namespace OHOS::Util { return result; } - static napi_value TextcoderInit(napi_env env, napi_value exports) { const char *textEncoderClassName = "TextEncoder"; @@ -523,7 +522,7 @@ namespace OHOS::Util { NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected."); Base64 *object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->Encode(args[0]); + napi_value result = object->EncodeSync(args[0]); return result; } @@ -544,7 +543,7 @@ namespace OHOS::Util { NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected."); Base64 *object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->EncodeToString(args[0]); + napi_value result = object->EncodeToStringSync(args[0]); return result; } @@ -572,10 +571,9 @@ namespace OHOS::Util { } Base64 *object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->Decode(args[0]); + napi_value result = object->DecodeSync(args[0]); return result; } - static napi_value EncodeAsync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -593,10 +591,9 @@ namespace OHOS::Util { NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected."); Base64 *object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->EncodeAsync(args[0]); + napi_value result = object->Encode(args[0]); return result; } - static napi_value EncodeToStringAsync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -614,10 +611,9 @@ namespace OHOS::Util { NAPI_ASSERT(env, valuetype0 == napi_uint8_array, "Wrong argument type. napi_uint8_array expected."); Base64 *object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->EncodeToStringAsync(args[0]); + napi_value result = object->EncodeToString(args[0]); return result; } - static napi_value DecodeAsync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -642,7 +638,7 @@ namespace OHOS::Util { } Base64 *object = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->DecodeAsync(args[0]); + napi_value result = object->Decode(args[0]); return result; } @@ -787,18 +783,6 @@ namespace OHOS::Util { return rst; } - static napi_value IsCryptoKey(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = 1; - napi_value args = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr)); - Types* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value rst = object->IsCryptoKey(args); - return rst; - } - static napi_value IsDataView(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -927,20 +911,6 @@ namespace OHOS::Util { return result; } - static napi_value IsKeyObject(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t requireArgc = 1; - size_t argc = 1; - napi_value args = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr)); - NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments"); - Types* object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); - napi_value result = object->IsKeyObject(args); - return result; - } - static napi_value IsMap(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -1220,7 +1190,7 @@ namespace OHOS::Util { napi_value result = object->IsWeakSet(args); return result; } - + static napi_value TypeofInit(napi_env env, napi_value exports) { const char* typeofClassName = "Types"; @@ -1234,7 +1204,6 @@ namespace OHOS::Util { DECLARE_NAPI_FUNCTION("isArrayBufferView", IsArrayBufferView), DECLARE_NAPI_FUNCTION("isArgumentsObject", IsArgumentsObject), DECLARE_NAPI_FUNCTION("isArrayBuffer", IsArrayBuffer), - DECLARE_NAPI_FUNCTION("isCryptoKey", IsCryptoKey), DECLARE_NAPI_FUNCTION("isDataView", IsDataView), DECLARE_NAPI_FUNCTION("isDate", IsDate), DECLARE_NAPI_FUNCTION("isExternal", IsExternal), @@ -1245,7 +1214,6 @@ namespace OHOS::Util { DECLARE_NAPI_FUNCTION("isInt8Array", IsInt8Array), DECLARE_NAPI_FUNCTION("isInt16Array", IsInt16Array), DECLARE_NAPI_FUNCTION("isInt32Array", IsInt32Array), - DECLARE_NAPI_FUNCTION("isKeyObject", IsKeyObject), DECLARE_NAPI_FUNCTION("isMap", IsMap), DECLARE_NAPI_FUNCTION("isMapIterator", IsMapIterator), DECLARE_NAPI_FUNCTION("isModuleNamespaceObject", IsModuleNamespaceObject), @@ -1281,12 +1249,12 @@ namespace OHOS::Util { const char *base64ClassName = "Base64"; napi_value base64Class = nullptr; static napi_property_descriptor base64Desc[] = { - DECLARE_NAPI_FUNCTION("encode", EncodeBase64), - DECLARE_NAPI_FUNCTION("encodeToString", EncodeToString), - DECLARE_NAPI_FUNCTION("decode", DecodeBase64), - DECLARE_NAPI_FUNCTION("encodeAsync", EncodeAsync), - DECLARE_NAPI_FUNCTION("encodeToStringAsync", EncodeToStringAsync), - DECLARE_NAPI_FUNCTION("decodeAsync", DecodeAsync), + DECLARE_NAPI_FUNCTION("encodeSync", EncodeBase64), + DECLARE_NAPI_FUNCTION("encodeToStringSync", EncodeToString), + DECLARE_NAPI_FUNCTION("decodeSync", DecodeBase64), + DECLARE_NAPI_FUNCTION("encode", EncodeAsync), + DECLARE_NAPI_FUNCTION("encodeToString", EncodeToStringAsync), + DECLARE_NAPI_FUNCTION("decode", DecodeAsync), }; NAPI_CALL(env, napi_define_class(env, base64ClassName, strlen(base64ClassName), Base64Constructor, nullptr, sizeof(base64Desc) / sizeof(base64Desc[0]), base64Desc, diff --git a/util/src/util_js.ts b/util/src/util_js.ts index 818c06937e72483d6f570a3e0aeb20feb125368e..e7966ae33ab18e38b192315a0bfc31c58239e379 100644 --- a/util/src/util_js.ts +++ b/util/src/util_js.ts @@ -441,18 +441,20 @@ function promiseWrapper(func : any) { class LruBuffer { - private cache: Map; + private cache: Map; private maxSize : number = 64; + private maxNumber : number = 2147483647; private putCount : number = 0; private createCount : number = 0; private evictionCount : number = 0; private hitCount : number = 0; private missCount : number = 0; + public length : number = 0; public constructor(capacity?: number) { if(capacity !== undefined) { - if (capacity <= 0) { + if (capacity <= 0 || capacity%1 !== 0 || capacity > this.maxNumber) { throw new Error('data error'); } this.maxSize = capacity; @@ -461,14 +463,15 @@ class LruBuffer } public updateCapacity(newCapacity : number) { - if (newCapacity <= 0) { + if (newCapacity <= 0 || newCapacity%1 !== 0 || newCapacity > this.maxNumber) { throw new Error('data error'); } else if (this.cache.size >newCapacity) { this.changeCapacity(newCapacity); } + this.length = this.cache.size; this.maxSize = newCapacity; } - public get(key : any) + public get(key : any) { if (key === null) { throw new Error('key not be null'); @@ -496,7 +499,7 @@ class LruBuffer return createValue; } } - public put(key : any, value : any) + public put(key : any, value : any) { if (key === null || value === null) { throw new Error('key or value not be null'); @@ -510,42 +513,40 @@ class LruBuffer } else if (this.cache.size >= this.maxSize) { this.cache.delete(this.cache.keys().next().value); this.evictionCount++; - } + } this.cache.set(key, value); + this.length = this.cache.size; return former; } public getCreateCount() - { + { return this.createCount; } public getMissCount() { return this.missCount; } - public getRemovalCount() + public getRemovalCount() { return this.evictionCount; } - public getMatchCount() + public getMatchCount() { return this.hitCount; } - public getPutCount() + public getPutCount() { return this.putCount; } - public capacity() + public getCapacity() { return this.maxSize; } - public size() - { - return this.cache.size; - } public clear() { this.cache.clear(); this.afterRemoval(false, this.cache.keys(), this.cache.values(), null); + this.length = this.cache.size; } public isEmpty() { @@ -565,11 +566,12 @@ class LruBuffer value = this.cache.get(key); this.cache.delete(key); this.cache.set(key, value); + this.length = this.cache.size; return flag; } this.missCount++; return flag; - } + } public remove(key : any) { if (key === null) { @@ -580,9 +582,11 @@ class LruBuffer this.cache.delete(key); if (former !== null) { this.afterRemoval(false, key, former, null); + this.length = this.cache.size; return former; } - } + } + this.length = this.cache.size; return undefined; } public toString() @@ -618,13 +622,13 @@ class LruBuffer } protected afterRemoval(isEvict : boolean, key : any, value : any, newValue : any) { - + } protected createDefault(key : any) { return undefined; } - public entries() + public entries() { let arr = []; for (let entry of this.cache.entries()) { @@ -632,7 +636,7 @@ class LruBuffer } return arr; } - public [Symbol.iterator] () + public [Symbol.iterator]() { let arr = []; for (let [key, value] of this.cache) { @@ -749,7 +753,7 @@ class RationalNumber } } - public value() + public valueOf() { if (this.mnum > 0 && this.mden == 0) { return Number.POSITIVE_INFINITY; @@ -953,7 +957,7 @@ class Scope { let mUpper = reUpper ? this._upperLimit : y; return new Scope(mLower, mUpper); } - } + } public toString(): string { let strLower = this._lowerLimit.toString(); diff --git a/util/tsconfig.json b/util/tsconfig.json index 66ad148561b08a71630eb07c7484394c2acc7930..eff2af40253bd6fc9b4f44f21efcaac90b5487c1 100644 --- a/util/tsconfig.json +++ b/util/tsconfig.json @@ -1,8 +1,8 @@ { "compilerOptions": { "target": "es6", - "module": "es6", - "rootDir": "./src", + "module": "es6", + "rootDir": "./src", //"outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ "outDir": "./out", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, @@ -10,5 +10,5 @@ "strict": true, "skipLibCheck": true, "noImplicitThis": false, - } + } }