From 9a264b96b48c052f0397fd86008b9f90317e823b Mon Sep 17 00:00:00 2001 From: wangyong1995626wywz Date: Tue, 8 Mar 2022 17:11:16 +0800 Subject: [PATCH 1/3] Correct relevant interfaces on TS side Description 1. Fix some interface errors 2. Correct some wrong parameters 3. complete some missing interfaces 4. Align with the ark side and throw exception description #I4WQ2O:Correct relevant interfaces on TS side Signed-off-by: wangyong1995626wywz --- container/arraylist/js_arraylist.ts | 83 ++--- container/deque/js_deque.ts | 54 ++-- container/deque/native_module_deque.cpp | 2 +- container/hashmap/js_hashmap.ts | 24 +- container/hashmap/native_module_hashmap.cpp | 2 +- container/hashset/js_hashset.ts | 14 +- container/hashset/native_module_hashset.cpp | 2 +- container/lightweightmap/js_lightweightmap.ts | 30 +- .../native_module_lightweightmap.cpp | 2 +- container/lightweightset/js_lightweightset.ts | 17 +- .../native_module_lightweightset.cpp | 2 +- container/linkedlist/js_linkedlist.ts | 301 +++++++++--------- .../linkedlist/native_module_linkedlist.cpp | 2 +- container/list/js_list.ts | 232 +++++++------- container/list/native_module_list.cpp | 2 +- container/plainarray/js_plainarray.ts | 45 ++- .../plainarray/native_module_plainarray.cpp | 2 +- container/queue/js_queue.ts | 72 +++-- container/queue/native_module_queue.cpp | 2 +- container/stack/js_stack.ts | 59 ++-- container/stack/native_module_stack.cpp | 2 +- container/struct/js_struct.ts | 77 +++-- container/struct/native_module_struct.cpp | 2 +- container/treemap/js_treemap.ts | 37 ++- container/treemap/native_module_treemap.cpp | 2 +- container/treeset/js_treeset.ts | 42 ++- container/treeset/native_module_treeset.cpp | 2 +- container/tsconfig.json | 2 +- container/vector/js_vector.ts | 77 +++-- container/vector/native_module_vector.cpp | 2 +- 30 files changed, 647 insertions(+), 547 deletions(-) diff --git a/container/arraylist/js_arraylist.ts b/container/arraylist/js_arraylist.ts index a0112fe..246a00d 100644 --- a/container/arraylist/js_arraylist.ts +++ b/container/arraylist/js_arraylist.ts @@ -22,16 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastArrayList == undefined) { class HandlerArrayList { - get(obj: ArrayList, prop: any) { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: ArrayList, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("ArrayList: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: ArrayList, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: ArrayList, prop: any, value: T) { @@ -39,10 +42,10 @@ if (flag || fastArrayList == undefined) { obj[prop] = value; return true; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new Error("ArrayList: set out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } else { obj[index] = value; return true; @@ -51,11 +54,9 @@ if (flag || fastArrayList == undefined) { return false; } deleteProperty(obj: ArrayList, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("ArrayList: deleteProperty out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { obj.removeByIndex(index); return true; } @@ -75,17 +76,15 @@ if (flag || fastArrayList == undefined) { return true; } getOwnPropertyDescriptor(obj: ArrayList, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("ArrayList: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): T { - throw new Error("Can setPrototype on ArrayList Object"); + throw new TypeError("Can setPrototype on ArrayList Object"); } } interface IterableIterator { @@ -111,6 +110,9 @@ if (flag || fastArrayList == undefined) { return true; } insert(element: T, index: number): void { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } if (this.isFull()) { this.resize(); } @@ -138,7 +140,7 @@ if (flag || fastArrayList == undefined) { } removeByIndex(index: number): T { if (index < 0 || index >= this.elementNum) { - throw new Error("removeByIndex is out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } let result = this[index]; for (let i = index; i < this.elementNum - 1; i++) { @@ -168,9 +170,12 @@ if (flag || fastArrayList == undefined) { } removeByRange(fromIndex: number, toIndex: number): void { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`); } - toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; + if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); + } + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let i = fromIndex; for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; @@ -178,13 +183,13 @@ if (flag || fastArrayList == undefined) { } this.elementNum -= toIndex - fromIndex; } - replaceAllElements(callbackfn: (value: T, index?: number, arraylist?: ArrayList) => T, + replaceAllElements(callbackfn: (value: T, index?: number, arrList?: ArrayList) => T, thisArg?: Object): void { for (let i = 0; i < this.elementNum; i++) { this[i] = callbackfn.call(thisArg, this[i], i, this); } } - forEach(callbackfn: (value: T, index?: number, arraylist?: ArrayList) => void, + forEach(callbackfn: (value: T, index?: number, arrList?: ArrayList) => void, thisArg?: Object): void { for (let i = 0; i < this.elementNum; i++) { callbackfn.call(thisArg, this[i], i, this); @@ -204,7 +209,7 @@ if (flag || fastArrayList == undefined) { } } } else { - for (var i = 0; i < this.length - 1; i++) { + for (let i = 0; i < this.length - 1; i++) { for (let j = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; @@ -221,26 +226,26 @@ if (flag || fastArrayList == undefined) { } private asciSort(curElement: any, nextElement: any): boolean { if ((Object.prototype.toString.call(curElement) === "[object String]" || - Object.prototype.toString.call(curElement) === "[object Number]") && - (Object.prototype.toString.call(nextElement) === "[object String]" || - Object.prototype.toString.call(nextElement) === "[object Number]")) { - curElement = curElement.toString(); - nextElement = nextElement.toString(); - if(curElement > nextElement) { - return true; - } - return false + Object.prototype.toString.call(curElement) === "[object Number]") && + (Object.prototype.toString.call(nextElement) === "[object String]" || + Object.prototype.toString.call(nextElement) === "[object Number]")) { + curElement = curElement.toString(); + nextElement = nextElement.toString(); + if (curElement > nextElement) { + return true; + } + return false } return false; } subArrayList(fromIndex: number, toIndex: number): ArrayList { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`); } if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { - throw new Error(`fromIndex or toIndex is out-of-bounds`); + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } - toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex; + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let arraylist = new ArrayList(); for (let i = fromIndex; i < toIndex; i++) { arraylist.add(this[i]); @@ -289,8 +294,8 @@ if (flag || fastArrayList == undefined) { let arraylist = this; return { next: function () { - var done = count >= arraylist.elementNum; - var value = !done ? arraylist[count++] : undefined; + let done = count >= arraylist.elementNum; + let value = !done ? arraylist[count++] : undefined; return { done: done, value: value, diff --git a/container/deque/js_deque.ts b/container/deque/js_deque.ts index 980507c..bfcc52b 100644 --- a/container/deque/js_deque.ts +++ b/container/deque/js_deque.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -22,16 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastDeque == undefined) { class HandlerDeque { - get(obj: Deque, prop: any): T { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: Deque, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0) { - throw new Error("Deque: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: Deque, prop: any): T { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Deque, prop: any, value: T): boolean { @@ -39,10 +42,10 @@ if (flag || fastDeque == undefined) { obj[prop] = value; return true; } - var index = Number(prop); + let index = Number(prop); if (Number.isInteger(index)) { if (index < 0) { - throw new Error("Deque: set out-of-bounds"); + throw new RangeError("index is out-of-bounds"); } else { obj[index] = value; return true; @@ -64,17 +67,15 @@ if (flag || fastDeque == undefined) { return true; } getOwnPropertyDescriptor(obj: Deque, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("Deque: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } - return; + return } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Deque Object"); + throw new RangeError("Can setPrototype on Deque Object"); } } interface IterableIterator { @@ -93,7 +94,7 @@ if (flag || fastDeque == undefined) { this.rear = 0; return new Proxy(this, new HandlerDeque()); } - get length(){ + get length() { let result = (this.rear - this.front + this.capacity) % this.capacity; return result; } @@ -112,9 +113,15 @@ if (flag || fastDeque == undefined) { this.rear = (this.rear + 1) % (this.capacity + 1); } getFirst(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.front]; } getLast(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.rear - 1]; } has(element: T): boolean { @@ -127,11 +134,17 @@ if (flag || fastDeque == undefined) { return result; } popFirst(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.front]; this.front = (this.front + 1) % (this.capacity + 1); return result; } popLast(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.rear - 1]; this.rear = (this.rear + this.capacity) % (this.capacity + 1); return result; @@ -170,13 +183,16 @@ if (flag || fastDeque == undefined) { private isFull(): boolean { return (this.rear + 1) % this.capacity === this.front; } + private isEmpty(): boolean { + return this.length == 0; + } [Symbol.iterator](): IterableIterator { let deque = this; let count = deque.front; return { next: function () { - var done = count == deque.rear; - var value = !done ? deque[count] : undefined; + let done = count == deque.rear; + let value = !done ? deque[count] : undefined; count = (count + 1) % deque.capacity; return { done: done, diff --git a/container/deque/native_module_deque.cpp b/container/deque/native_module_deque.cpp index 70eea6c..246d6bf 100644 --- a/container/deque/native_module_deque.cpp +++ b/container/deque/native_module_deque.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/hashmap/js_hashmap.ts b/container/hashmap/js_hashmap.ts index 542aad4..ffb3e86 100644 --- a/container/hashmap/js_hashmap.ts +++ b/container/hashmap/js_hashmap.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -70,6 +70,9 @@ if (flag || fastHashMap === undefined) { return this.getValueByKey(key); } setAll(map: HashMap): void { + if(!(map instanceof HashMap)) { + throw new TypeError("Incoming object is not JSAPIHashMap"); + } let memebers = map.keyValueArray; for (let i = 0; i < memebers.length; i++) { this.put(memebers[i].key, memebers[i].value); @@ -80,9 +83,6 @@ if (flag || fastHashMap === undefined) { } remove(key: K): V { let result = this.removeMember(key); - if (result === undefined) { - throw new Error("The removed element does not exist in this container"); - } return result; } clear(): void { @@ -93,8 +93,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, @@ -108,8 +108,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].value : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].value : undefined; count++; return { done: done, @@ -133,8 +133,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -148,8 +148,8 @@ if (flag || fastHashMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, diff --git a/container/hashmap/native_module_hashmap.cpp b/container/hashmap/native_module_hashmap.cpp index 4892c1c..d2d0058 100644 --- a/container/hashmap/native_module_hashmap.cpp +++ b/container/hashmap/native_module_hashmap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/hashset/js_hashset.ts b/container/hashset/js_hashset.ts index 394afff..880f33d 100644 --- a/container/hashset/js_hashset.ts +++ b/container/hashset/js_hashset.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -87,8 +87,8 @@ if (flag || fastHashSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, @@ -102,8 +102,8 @@ if (flag || fastHashSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -117,8 +117,8 @@ if (flag || fastHashSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, diff --git a/container/hashset/native_module_hashset.cpp b/container/hashset/native_module_hashset.cpp index c5d9cdf..6c1c485 100644 --- a/container/hashset/native_module_hashset.cpp +++ b/container/hashset/native_module_hashset.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/lightweightmap/js_lightweightmap.ts b/container/lightweightmap/js_lightweightmap.ts index 72ba6c1..f8be0e5 100644 --- a/container/lightweightmap/js_lightweightmap.ts +++ b/container/lightweightmap/js_lightweightmap.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -58,6 +58,9 @@ if (flag || fastLightWeightMap === undefined) { return this.memberNumber; } hasAll(map: LightWeightMap): boolean { + if(!(map instanceof LightWeightMap)) { + throw new TypeError("map is not JSAPILightWeightMap"); + } if (map.memberNumber > this.memberNumber) return false; if (LightWeightAbility.isIncludeToArray(this.keyValueStringArray(), map.keyValueStringArray()) ) { return true; @@ -71,6 +74,9 @@ if (flag || fastLightWeightMap === undefined) { return this.members.values.indexOf(value) > -1; } increaseCapacityTo(minimumCapacity: number): void { + if (typeof minimumCapacity !== "number") { + throw new TypeError("the size is not integer"); + } super.ensureCapacity(minimumCapacity); } entries(): IterableIterator<[K, V]> { @@ -78,8 +84,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; + let done = count >= data.memberNumber; + let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; count++; return { done: done, @@ -102,6 +108,9 @@ if (flag || fastLightWeightMap === undefined) { return this.memberNumber === 0; } getKeyAt(index: number): K { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } return this.members.keys[index]; } keys(): IterableIterator { @@ -109,8 +118,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.members.keys[count] : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.members.keys[count] : undefined; count++; return { done: done, @@ -120,6 +129,9 @@ if (flag || fastLightWeightMap === undefined) { }; } setAll(map: LightWeightMap): void { + if(!(map instanceof LightWeightMap)) { + throw new TypeError("Incoming object is not JSAPILightWeightMap"); + } if (this.memberNumber === 0) { this.members.hashs = map.members.hashs.slice(); this.members.keys = map.members.keys.slice(); @@ -172,8 +184,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; + let done = count >= data.memberNumber; + let value = !done ? [data.members.keys[count], data.members.values[count]] as [K, V] : undefined; count++; return { done: done, @@ -197,8 +209,8 @@ if (flag || fastLightWeightMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.members.values[count] : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.members.values[count] : undefined; count++; return { done: done, diff --git a/container/lightweightmap/native_module_lightweightmap.cpp b/container/lightweightmap/native_module_lightweightmap.cpp index d828099..e24270c 100644 --- a/container/lightweightmap/native_module_lightweightmap.cpp +++ b/container/lightweightmap/native_module_lightweightmap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/lightweightset/js_lightweightset.ts b/container/lightweightset/js_lightweightset.ts index 46c1e19..9e39294 100644 --- a/container/lightweightset/js_lightweightset.ts +++ b/container/lightweightset/js_lightweightset.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -63,6 +63,9 @@ if (flag || fastLightWeightSet === undefined) { return true; } addAll(set: LightWeightSet): boolean { + if(!(set instanceof LightWeightSet)) { + throw new TypeError("Incoming object is not JSAPILightWeightSet"); + } let change = false; if (set.memberNumber == 0) { change = false; @@ -85,6 +88,8 @@ if (flag || fastLightWeightSet === undefined) { } equal(obj: Object): boolean { if (this.memberNumber === 0) return false; + if(obj instanceof LightWeightSet) + return JSON.stringify(obj.members.keys) === JSON.stringify(this.members.keys); if (JSON.stringify(obj) === JSON.stringify(this.members.keys)) return true; return false; } @@ -129,8 +134,8 @@ if (flag || fastLightWeightSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.members.keys[count] : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.members.keys[count] : undefined; count++; return { done: done, @@ -156,9 +161,9 @@ if (flag || fastLightWeightSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var tempValue = data.members.keys[count]; - var value = !done ? ([tempValue, tempValue] as [T, T]) : undefined; + let done = count >= data.memberNumber; + let tempValue = data.members.keys[count]; + let value = !done ? ([tempValue, tempValue] as [T, T]) : undefined; count++; return { done: done, diff --git a/container/lightweightset/native_module_lightweightset.cpp b/container/lightweightset/native_module_lightweightset.cpp index 6d427d0..0d84dbb 100644 --- a/container/lightweightset/native_module_lightweightset.cpp +++ b/container/lightweightset/native_module_lightweightset.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/linkedlist/js_linkedlist.ts b/container/linkedlist/js_linkedlist.ts index 9f33e5b..3a51a94 100644 --- a/container/linkedlist/js_linkedlist.ts +++ b/container/linkedlist/js_linkedlist.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -26,11 +26,11 @@ if (flag || fastLinkedList == undefined) { if (typeof prop === "symbol") { return obj[prop]; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); let length = obj.length; if (Number.isInteger(index)) { if (index < 0 || index >= length) { - throw new Error("LinkedList: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return obj.get(index); } @@ -38,18 +38,18 @@ if (flag || fastLinkedList == undefined) { } set(obj: LinkedList, prop: any, value: any) { if (prop === "elementNum" || - prop === "capacity" || - prop === "head" || - prop == "next" || - prop == "tail" ) { + prop === "capacity" || + prop === "head" || + prop == "next" || + prop == "tail") { obj[prop] = value; return true; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { let length = obj.length; if (index < 0 || index >= length) { - throw new Error("LinkedList: set out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } else { obj.set(index, value); return true; @@ -58,11 +58,11 @@ if (flag || fastLinkedList == undefined) { return false; } deleteProperty(obj: LinkedList, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { let length = obj.length; if (index < 0 || index >= length) { - throw new Error("LinkedList: deleteProperty out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } obj.removeByIndex(index); return true; @@ -73,9 +73,9 @@ if (flag || fastLinkedList == undefined) { return obj.has(prop); } ownKeys(obj: LinkedList) { - var keys = []; + let keys = []; let length = obj.length; - for (var i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { keys.push(i.toString()); } return keys; @@ -84,18 +84,18 @@ if (flag || fastLinkedList == undefined) { return true; } getOwnPropertyDescriptor(obj: LinkedList, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { let length = obj.length; if (index < 0 || index >= length) { - throw new Error("LinkedList: getOwnPropertyDescriptor out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on LinkedList Object"); + throw new Error("Can setPrototype on LinkedList Object"); } } interface IterableIterator { @@ -105,52 +105,27 @@ if (flag || fastLinkedList == undefined) { }; } class NodeObj { - /* If 'any' is changed to 'T' here, an error will be reported: - Type 'unknown' cannot be assigned to type 'T'. */ - element: any; - next?: NodeObj | null; - prev?: NodeObj | null; - constructor( - element: any, - next?: NodeObj | null, - prev?: NodeObj | null - ) { + element: T; + next?: NodeObj; + prev?: NodeObj; + constructor(element: T, next?: NodeObj, prev?: NodeObj) { this.element = element; this.next = next; this.prev = prev; } } - class LinkIterator { - /* If 'any' is changed to 'T' here, an error will be reported: - Property 'next' does not exist on type 'T' */ - private linkNode: any; - constructor(linkNode: any) { - this.linkNode = linkNode; - } - hasNext(): boolean { - if (this.linkNode.next !== null) { - return true; - } else { - return false; - } - } - next(): NodeObj { - this.linkNode = this.linkNode.next; - return this.linkNode; - } - prev(): NodeObj { - this.linkNode = this.linkNode.prev; - return this.linkNode; - } - } class LinkedList { - private head?: any; - private tail?: any; - private elementNum : number; + private head?: NodeObj; + private tail?: NodeObj; + private elementNum: number; private capacity: number; - constructor(head?: NodeObj, tail?: NodeObj) { - this.head = head || null; - this.tail = tail || null; + private next?: NodeObj; + private prev?: NodeObj; + constructor() { + this.head = undefined; + this.tail = undefined; + this.next = undefined; + this.prev = undefined; this.elementNum = 0; this.capacity = 10; return new Proxy(this, new HandlerLinkedList()); @@ -158,37 +133,44 @@ if (flag || fastLinkedList == undefined) { get length() { return this.elementNum; } - private getNode(index: number): NodeObj { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + private getNode(index: number): NodeObj | undefined { + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index; i++) { + if (current !== undefined) { + current = current.next; + } + } + return current; } - return current; + return undefined; } get(index: number): T { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index && current != undefined; i++) { + current = current.next; + } + return current.element; } - return current.element; + return undefined; } add(element: T): boolean { - if (this.elementNum === 0) { - let head = this.head; - let tail = this.tail; - this.head = this.tail = new NodeObj(element, head, tail); + let node = new NodeObj(element); + if (this.head == undefined) { + this.head = this.tail = node; } else { - let prevNode = this.getNode(this.elementNum - 1); - prevNode.next = new NodeObj(element, prevNode["next"], this.tail); + let current = this.head; + while (current.next !== undefined) { + current = current.next; + } + this.tail = current.next = node; } this.elementNum++; return true; } addFirst(element: T): void { - if (!element) { - throw new Error("element cannot be null"); - } - let node = new NodeObj(element, this.head, this.tail); + let node = new NodeObj(element); if (this.elementNum === 0) { this.head = this.tail = node; } else { @@ -198,47 +180,45 @@ if (flag || fastLinkedList == undefined) { this.elementNum++; } removeFirst(): T { - let result = this.getNode(0).element; - this.removeByIndex(0); - return result; - } - popFirst(): T { - let result = this.getNode(0).element; - this.removeByIndex(0); - return result; - } - popLast(): T { - let result = this.getNode(this.elementNum - 1).element; - this.removeByIndex(this.elementNum - 1); - return result; + if (this.head !== undefined) { + let result = this.head.element; + this.removeByIndex(0); + return result; + } + return undefined; } removeLast(): T { - let result = this.getNode(this.elementNum - 1).element; - this.removeByIndex(this.elementNum - 1); - return result; + if (this.tail !== undefined) { + let result = this.tail.element; + this.removeByIndex(this.elementNum - 1); + return result; + } + return undefined; } clear(): void { - this.head = null; - this.tail = null; + this.head = undefined; + this.tail = undefined; this.elementNum = 0; } has(element: T): boolean { - if (this.head.element === element) { - return true; - } - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - if (newNode.element === element) { + if (this.head !== undefined) { + if (this.head.element === element) { return true; } + let current = this.head; + while (current.next != undefined) { + current = current.next; + if (current.element === element) { + return true; + } + } } return false; } getIndexOf(element: T): number { for (let i = 0; i < this.elementNum; i++) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } @@ -247,39 +227,45 @@ if (flag || fastLinkedList == undefined) { getLastIndexOf(element: T): number { for (let i = this.elementNum - 1; i >= 0; i--) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } return -1; } removeByIndex(index: number): T { - if (index < 0 || index >= this.elementNum) { - throw new Error("removeByIndex is out-of-bounds"); - } - let current = this.head; - if (index === 0) { - this.head = current && current.next; - if (this.elementNum == 1) { - this.head = this.tail = null; + if (index >= 0 && index < this.elementNum) { + let current = this.head; + if (index === 0 && current !== undefined) { + this.head = current.next; + this.head.prev = undefined; + if (this.elementNum == 1) { + this.head = this.tail = undefined; + } + } else if (index == this.elementNum - 1) { + current = this.getNode(index - 1); + if (current !== undefined) { + this.tail = current; + current.next = undefined; + } } else { - this.head.prev = null; + const prevNode = this.getNode(index - 1); + const nextNode = this.getNode(index + 1); + if (prevNode !== undefined && nextNode !== undefined) { + prevNode.next = nextNode; + nextNode.prev = prevNode; + } + } + if (current !== undefined) { + this.elementNum--; + return current.element; } - } else if (index == this.elementNum - 1) { - current = this.getNode(index - 1); - this.tail = current; - current.next = null; } else { - const prevNode = this.getNode(index - 1); - const nextNode = this.getNode(index + 1); - prevNode.next = nextNode; - nextNode.prev = prevNode; + throw new RangeError("the index is out-of-bounds"); } - this.elementNum--; - return current && current.element; } remove(element: T): boolean { - if(this.isEmpty()){ + if (this.isEmpty()) { return false; } if (this.has(element)) { @@ -294,42 +280,44 @@ if (flag || fastLinkedList == undefined) { let index = this.getIndexOf(element); this.removeByIndex(index); return true; - } else { - return false; } + return false; } removeLastFound(element: T): boolean { if (this.has(element)) { let index = this.getLastIndexOf(element); this.removeByIndex(index); return true; - } else { - return false; } + return false; } getFirst(): T { - let newNode = this.getNode(0); - let element = newNode.element; - return element; + if (this.head !== undefined) { + return this.head.element; + } + return undefined; } getLast(): T { - let newNode = this.getNode(this.elementNum - 1); - let element = newNode.element; - return element; + if (this.tail !== undefined) { + return this.tail.element; + } + return undefined; } insert(index: number, element: T): void { - if (index >= 0 && index < this.elementNum) { + if (index >= 0 && index <= this.elementNum) { let newNode = new NodeObj(element); let current = this.head; if (index === 0) { - if (this.head === null) { - this.head = newNode; - this.tail = newNode; + if (this.head === undefined) { + this.head = this.tail = newNode; } else { newNode.next = this.head; this.head.prev = newNode; this.head = newNode; } + } else if (index === this.elementNum && this.elementNum !== 0) { + let prevNode = this.getNode(this.elementNum - 1); + prevNode.next = this.tail = newNode; } else { const prevNode = this.getNode(index - 1); current = prevNode.next; @@ -338,18 +326,15 @@ if (flag || fastLinkedList == undefined) { current.prev = newNode; newNode.prev = prevNode; } - } else if (index === this.elementNum) { - let prevNode = this.getNode(this.elementNum - 1); - prevNode.next = new NodeObj(element, prevNode["next"], this.tail); } else { - throw new Error("index cannot Less than 0 and more than this length"); + throw new RangeError("the index is out-of-bounds"); } this.elementNum++; } set(index: number, element: T): T { const current = this.getNode(index); current.element = element; - return current && current.element; + return current.element; } convertToArray(): Array { let arr: Array = []; @@ -357,11 +342,13 @@ if (flag || fastLinkedList == undefined) { if (this.elementNum <= 0) { return arr; } - arr[index] = this.head.element; - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - arr[++index] = newNode.element; + if (this.head !== undefined) { + let current = this.head; + arr[index] = this.head.element; + while (current.next != undefined) { + current = current.next; + arr[++index] = current.element; + } } return arr; } @@ -369,7 +356,7 @@ if (flag || fastLinkedList == undefined) { let clone = new LinkedList(); let arr = this.convertToArray(); for (let i = 0; i < arr.length; i++) { - let item = arr[i] + let item = arr[i]; clone.add(item); } return clone; @@ -377,16 +364,18 @@ if (flag || fastLinkedList == undefined) { private isEmpty(): boolean { return this.elementNum == 0; } - forEach(callbackfn: (value: T,index?: number,linkedlist?: LinkedList) => void, + forEach(callbackfn: (value: T, index?: number, linkedList?: LinkedList) => void, thisArg?: Object): void { let index = 0; - const linkIterator = new LinkIterator(this.head); - if (this.elementNum > 0) { - callbackfn.call(thisArg, this.head.element, index, this); - } - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - callbackfn.call(thisArg, newNode.element, ++index, this); + if (this.head !== undefined) { + let current = this.head; + if (this.elementNum > 0) { + callbackfn.call(thisArg, this.head.element, index, this); + } + while (current.next != undefined) { + current = current.next; + callbackfn.call(thisArg, current.element, ++index, this); + } } } [Symbol.iterator](): IterableIterator { @@ -394,8 +383,8 @@ if (flag || fastLinkedList == undefined) { let linkedlist = this; return { next: function () { - var done = count >= linkedlist.elementNum; - var value = !done ? linkedlist.getNode(count++).element : undefined; + let done = count >= linkedlist.elementNum; + let value = !done ? linkedlist.getNode(count++).element : undefined; return { done: done, value: value, diff --git a/container/linkedlist/native_module_linkedlist.cpp b/container/linkedlist/native_module_linkedlist.cpp index b15a45a..98ad001 100644 --- a/container/linkedlist/native_module_linkedlist.cpp +++ b/container/linkedlist/native_module_linkedlist.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/list/js_list.ts b/container/list/js_list.ts index 1aa704b..45703e6 100644 --- a/container/list/js_list.ts +++ b/container/list/js_list.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -26,28 +26,27 @@ if (flag || fastList == undefined) { if (typeof prop === "symbol") { return obj[prop]; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return obj.get(index); } return obj[prop]; } set(obj: List, prop: any, value: T) { - if ( - prop === "elementNum" || + if (prop === "elementNum" || prop === "capacity" || prop === "head" || prop == "next") { obj[prop] = value; return true; } - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: set out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } else { obj.set(index, value); return true; @@ -56,10 +55,10 @@ if (flag || fastList == undefined) { return false; } deleteProperty(obj: List, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: deleteProperty out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } obj.removeByIndex(index); return true; @@ -70,8 +69,8 @@ if (flag || fastList == undefined) { return obj.has(prop); } ownKeys(obj: List) { - var keys = []; - for (var i = 0; i < obj.length; i++) { + let keys = []; + for (let i = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; @@ -80,17 +79,17 @@ if (flag || fastList == undefined) { return true; } getOwnPropertyDescriptor(obj: List, prop: any) { - var index = Number.parseInt(prop); + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("List: getOwnPropertyDescriptor out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on List Object"); + throw new Error("Can setPrototype on List Object"); } } interface IterableIterator { @@ -100,41 +99,21 @@ if (flag || fastList == undefined) { }; } class NodeObj { - /* If 'any' is changed to 'T' here, an error will be reported: - Type 'unknown' cannot be assigned to type 'T'. */ - element: any; - next?: NodeObj | null; - constructor(element: any, next?: NodeObj | null) { + element: T; + next?: NodeObj; + constructor(element: T, next?: NodeObj) { this.element = element; this.next = next; } } - class LinkIterator { - /* If 'any' is changed to 'T' here, an error will be reported: - Property 'next' does not exist on type 'T' */ - private linkNode: any; - constructor(linkNode: any) { - this.linkNode = linkNode; - } - hasNext(): boolean { - if (this.linkNode.next !== null) { - return true; - } - return false; - } - next(): NodeObj { - this.linkNode = this.linkNode.next; - return this.linkNode; - } - } class List { - /* If 'any' is changed to 'NodeObj | null ' here, an error will be reported: - Object may be 'null' */ - private head: any; + private head: NodeObj; private elementNum: number; private capacity: number; - constructor(head?: NodeObj) { - this.head = head || null; + private next?: NodeObj; + constructor() { + this.head = undefined; + this.next = undefined; this.elementNum = 0; this.capacity = 10; return new Proxy(this, new HandlerList()); @@ -142,45 +121,58 @@ if (flag || fastList == undefined) { get length() { return this.elementNum; } - private getNode(index: number): NodeObj { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + private getNode(index: number): NodeObj | undefined { + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index; i++) { + if (current !== undefined) { + current = current.next; + } + } + return current; } - return current; + return undefined; } get(index: number): T { - let current = this.head; - for (let i = 0; i < index; i++) { - current = current["next"]; + if (index >= 0 && index < this.elementNum) { + let current = this.head; + for (let i = 0; i < index && current != undefined; i++) { + current = current.next; + } + return current.element; } - return current.element; + return undefined; } add(element: T): boolean { - if (this.elementNum === 0) { - let head = this.head; - this.head = new NodeObj(element, head); + let node = new NodeObj(element); + if (this.head == undefined) { + this.head = node; } else { - let prevNode = this.getNode(this.elementNum - 1); - prevNode.next = new NodeObj(element, prevNode["next"]); + let current = this.head; + while (current.next !== undefined) { + current = current.next; + } + current.next = node; } this.elementNum++; return true; } clear(): void { - this.head = null; + this.head = undefined; this.elementNum = 0; } has(element: T): boolean { - if (this.head.element === element) { - return true; - } - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - if (newNode.element === element) { + if (this.head !== undefined) { + if (this.head.element === element) { return true; } + let current = this.head; + while (current.next != undefined) { + current = current.next; + if (current.element === element) { + return true; + } + } } return false; } @@ -191,23 +183,30 @@ if (flag || fastList == undefined) { if (!(obj instanceof List)) { return false; } else { - let e1 = new LinkIterator(this.head); - let e2 = new LinkIterator(obj.head); - while (e1.hasNext() && e2.hasNext()) { - const newNode1 = e1.next(); - const newNode2 = e2.next(); - if (newNode1.element !== newNode2.element) { - return false; + let e1 = this.head; + let e2 = obj.head; + if (e1 !== undefined && e2 !== undefined) { + while (e1.next !== undefined && e2.next !== undefined) { + e1 = e1.next; + e2 = e2.next; + if (e1.element !== e2.element) { + return false; + } } + return !(e1.next !== undefined || e2.next !== undefined); + } else if (e1 !== undefined && e2 === undefined) { + return false; + } else if (e1 === undefined && e2 !== undefined) { + return false; + } else { + return true; } - return !(e1.hasNext() || e2.hasNext()); } - return true; } getIndexOf(element: T): number { for (let i = 0; i < this.elementNum; i++) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } @@ -216,7 +215,7 @@ if (flag || fastList == undefined) { getLastIndexOf(element: T): number { for (let i = this.elementNum - 1; i >= 0; i--) { const curNode = this.getNode(i); - if (curNode.element === element) { + if (curNode !== undefined && curNode.element === element) { return i; } } @@ -224,7 +223,7 @@ if (flag || fastList == undefined) { } removeByIndex(index: number): T { if (index < 0 || index >= this.elementNum) { - throw new Error("removeByIndex is out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } let oldNode = this.head; if (index === 0) { @@ -249,27 +248,36 @@ if (flag || fastList == undefined) { replaceAllElements(callbackfn: (value: T, index?: number, list?: List) => T, thisArg?: Object): void { let index = 0; - const linkIterator = new LinkIterator(this.head); - if (this.elementNum > 0) { - const linkIterator = new LinkIterator(this.head); - this.getNode(index).element = callbackfn.call(thisArg, this.head.element, index, this); - } - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - this.getNode(++index).element = callbackfn.call(thisArg, newNode.element, index, this); + if (this.head !== undefined) { + let current = this.head; + if (this.elementNum > 0) { + this.getNode(index).element = callbackfn.call(thisArg, this.head.element, index, this); + } + while (current.next != undefined) { + current = current.next; + this.getNode(++index).element = callbackfn.call(thisArg, current.element, index, this); + } } } getFirst(): T { - let newNode = this.getNode(0); - let element = newNode.element; + if (this.isEmpty()) { + return undefined; + } + let element = this.head.element; return element; } getLast(): T { + if (this.isEmpty()) { + return undefined; + } let newNode = this.getNode(this.elementNum - 1); let element = newNode.element; return element; } insert(element: T, index: number): void { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } let newNode = new NodeObj(element); if (index >= 0 && index < this.elementNum) { if (index === 0) { @@ -285,15 +293,20 @@ if (flag || fastList == undefined) { } } set(index: number, element: T): T { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } const current = this.getNode(index); current.element = element; - return current && current.element; + return current.element; } sort(comparator: (firstValue: T, secondValue: T) => number): void { let isSort = true; for (let i = 0; i < this.elementNum; i++) { for (let j = 0; j < this.elementNum - 1 - i; j++) { - if (comparator(this.getNode(j).element, this.getNode(j + 1).element) > 0) { + if ( + comparator(this.getNode(j).element, this.getNode(j + 1).element) > 0 + ) { isSort = false; let temp = this.getNode(j).element; this.getNode(j).element = this.getNode(j + 1).element; @@ -307,17 +320,17 @@ if (flag || fastList == undefined) { } getSubList(fromIndex: number, toIndex: number): List { if (toIndex <= fromIndex) { - throw new Error("toIndex cannot be less than or equal to fromIndex"); + throw new RangeError(`the toIndex cannot be less than or equal to fromIndex`); } if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { - throw new Error(`fromIndex or toIndex is out-of-bounds`); + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } - toIndex = toIndex > this.elementNum ? this.elementNum - 1 : toIndex; + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let list = new List(); for (let i = fromIndex; i < toIndex; i++) { let element = this.getNode(i).element; list.add(element); - if (element === null) { + if (element === undefined) { break; } } @@ -329,11 +342,13 @@ if (flag || fastList == undefined) { if (this.elementNum <= 0) { return arr; } - arr[index] = this.head.element; - const linkIterator = new LinkIterator(this.head); - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - arr[++index] = newNode.element; + if (this.head !== undefined) { + let current = this.head; + arr[index] = this.head.element; + while (current.next != undefined) { + current = current.next; + arr[++index] = current.element; + } } return arr; } @@ -343,14 +358,15 @@ if (flag || fastList == undefined) { forEach(callbackfn: (value: T, index?: number, list?: List) => void, thisArg?: Object): void { let index = 0; - const linkIterator = new LinkIterator(this.head); - if (this.elementNum > 0) { - const linkIterator = new LinkIterator(this.head); - callbackfn.call(thisArg, this.head.element, index, this); - } - while (linkIterator.hasNext()) { - const newNode = linkIterator.next(); - callbackfn.call(thisArg, newNode.element, ++index, this); + if (this.head !== undefined) { + let current = this.head; + if (this.elementNum > 0) { + callbackfn.call(thisArg, this.head.element, index, this); + } + while (current.next != undefined) { + current = current.next; + callbackfn.call(thisArg, current.element, ++index, this); + } } } [Symbol.iterator](): IterableIterator { @@ -358,8 +374,8 @@ if (flag || fastList == undefined) { let list = this; return { next: function () { - var done = count >= list.elementNum; - var value = !done ? list.getNode(count++).element : undefined; + let done = count >= list.elementNum; + let value = !done ? list.getNode(count++).element : undefined; return { done: done, value: value, @@ -371,4 +387,4 @@ if (flag || fastList == undefined) { Object.freeze(List); fastList = List; } -export default fastList; \ No newline at end of file +export default fastList; diff --git a/container/list/native_module_list.cpp b/container/list/native_module_list.cpp index 379e8e4..0ffbde0 100644 --- a/container/list/native_module_list.cpp +++ b/container/list/native_module_list.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/plainarray/js_plainarray.ts b/container/plainarray/js_plainarray.ts index 6e0ae1a..5c38c4d 100644 --- a/container/plainarray/js_plainarray.ts +++ b/container/plainarray/js_plainarray.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -59,7 +59,7 @@ if (flag || fastPlainArray === undefined) { } add(key: number, value: T): void { if (typeof key !== "number") { - throw new Error("PlainArray's only number is allowed"); + throw new TypeError("the index is not integer"); } this.addmember(key, value); } @@ -78,13 +78,22 @@ if (flag || fastPlainArray === undefined) { return clone; } has(key: number): boolean { + if (typeof key !== "number") { + return false; + } return this.binarySearchAtPlain(key) > -1; } get(key: number): T { + if (typeof key !== "number") { + throw new TypeError("the index is not integer"); + } let index = this.binarySearchAtPlain(key); return this.members.values[index]; } getIndexOfKey(key: number): number { + if (typeof key !== "number") { + throw new TypeError("the index is not integer"); + } let result = this.binarySearchAtPlain(key); return result < 0 ? -1 : result; } @@ -95,28 +104,45 @@ if (flag || fastPlainArray === undefined) { return this.memberNumber === 0; } getKeyAt(index: number): number { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } return this.members.keys[index]; } remove(key: number): T { + let result: any = undefined; + if (typeof key !== "number") { + throw new TypeError("the index is not integer"); + } let index = this.binarySearchAtPlain(key); - if (index < 0) throw new Error("element not in this plainarray"); + if (index < 0) return result; return this.deletemember(index); } removeAt(index: number): T { - if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range"); + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } + let result: any = undefined; + if (index >= this.memberNumber || index < 0) return result; return this.deletemember(index); } removeRangeFrom(index: number, size: number): number { - if (index >= this.memberNumber || index < 0) throw new Error("index not in this plainarray range"); + if (typeof index !== "number" || typeof size !== "number") { + throw new TypeError("the index is not integer"); + } + if (index >= this.memberNumber || index < 0) throw new RangeError("the index is out-of-bounds"); let safeSize = (this.memberNumber - (index + size) < 0) ? this.memberNumber - index : size; this.deletemember(index, safeSize); return safeSize; } setValueAt(index: number, value: T): void { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } if (index >= 0 && index < this.memberNumber) { this.members.values[index] = value; } else { - throw new Error("index Out Of Bounds"); + throw new RangeError("the index is out-of-bounds"); } } toString(): string { @@ -127,6 +153,9 @@ if (flag || fastPlainArray === undefined) { return result.join(","); } getValueAt(index: number): T { + if (typeof index !== "number") { + throw new TypeError("the index is not integer"); + } return this.members.values[index]; } forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray) => void, @@ -140,8 +169,8 @@ if (flag || fastPlainArray === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? [data.members.keys[count], data.members.values[count]] as [number, T] : undefined; + let done = count >= data.memberNumber; + let value = !done ? [data.members.keys[count], data.members.values[count]] as [number, T] : undefined; count++; return { done: done, diff --git a/container/plainarray/native_module_plainarray.cpp b/container/plainarray/native_module_plainarray.cpp index 3065c31..d0ce8bc 100644 --- a/container/plainarray/native_module_plainarray.cpp +++ b/container/plainarray/native_module_plainarray.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/queue/js_queue.ts b/container/queue/js_queue.ts index 30a6a47..339dc07 100644 --- a/container/queue/js_queue.ts +++ b/container/queue/js_queue.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -22,16 +22,20 @@ if (arkPritvate !== undefined) { } if (flag || fastQueue == undefined) { class HandlerQueue { - get(obj: Queue, prop: any): T { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: Queue, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new Error("Queue: get out-of-bounds"); + console.log(index, obj.length) + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: Queue, prop: any): T { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Queue, prop: any, value: T): boolean { @@ -39,20 +43,17 @@ if (flag || fastQueue == undefined) { obj[prop] = value; return true; } - var index = Number(prop); - if (Number.isInteger(index)) { - if (index < 0 || index > obj.length) { - throw new Error("Queue: set out-of-bounds"); - } else { + this.isOutBounds(obj, prop); + let index = Number(prop); + if (index >= 0 && index <= obj.length && Number.isInteger(index)) { obj[index] = value; return true; - } } return false; } ownKeys(obj: Queue) { - var keys = []; - for (var i = 0; i < obj.length; i++) { + let keys = []; + for (let i = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; @@ -61,17 +62,15 @@ if (flag || fastQueue == undefined) { return true; } getOwnPropertyDescriptor(obj: Queue, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("Queue: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } - return; + return } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Queue Object"); + throw new RangeError("Can setPrototype on Queue Object"); } } interface IterableIterator { @@ -102,9 +101,15 @@ if (flag || fastQueue == undefined) { return true; } getFirst(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.front]; } pop(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.front]; this.front = (this.front + 1) % (this.capacity + 1); return result; @@ -113,25 +118,32 @@ if (flag || fastQueue == undefined) { thisArg?: Object): void { let k = 0; let i = this.front; - while (true) { - callbackfn.call(thisArg,this[i], k,this); - i = (i + 1) % this.capacity; - k++; - if (i === this.rear) { - break; + if (this.isEmpty()) { + return; + } else { + while (true) { + callbackfn.call(thisArg,this[i], k,this); + i = (i + 1) % this.capacity; + k++; + if (i === this.rear) { + break; + } } } } private isFull(): boolean { return this.length === this.capacity; } + private isEmpty(): boolean { + return this.length == 0; + } [Symbol.iterator](): IterableIterator { let count = this.front; let queue = this; return { next: function () { - var done = count == queue.rear; - var value = !done ? queue[count] : undefined; + let done = count == queue.rear; + let value = !done ? queue[count] : undefined; count = (count + 1) % queue.capacity; return { done: done, diff --git a/container/queue/native_module_queue.cpp b/container/queue/native_module_queue.cpp index 7f49eab..7a05352 100644 --- a/container/queue/native_module_queue.cpp +++ b/container/queue/native_module_queue.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/stack/js_stack.ts b/container/stack/js_stack.ts index 80efb4b..62ff223 100644 --- a/container/stack/js_stack.ts +++ b/container/stack/js_stack.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -22,17 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastStack == undefined) { class HandlerStack { + private isOutBounds(obj: Stack, prop: any) { + let index = Number.parseInt(prop); + if (Number.isInteger(index)) { + if (index < 0 || index >= obj.length) { + throw new RangeError("the index is out-of-bounds"); + } + } + } get(obj: Stack, prop: any) { if (typeof prop === "symbol") { return obj[prop]; } - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - let length = obj.length; - if (index < 0 || index >= length) { - throw new Error("Stack: get out-of-bounds"); - } - } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Stack, prop: any, value: T) { @@ -40,22 +42,18 @@ if (flag || fastStack == undefined) { obj[prop] = value; return true; } - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - let length = obj.length; - if (index < 0 || index >= length) { - throw new Error("Stack: set out-of-bounds"); - } else { - obj[index] = value; - return true; - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { + obj[index] = value; + return true; } return false; } ownKeys(obj: Stack) { - var keys = []; + let keys = []; let length = obj.length; - for (var i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { keys.push(i.toString()); } return keys; @@ -64,18 +62,15 @@ if (flag || fastStack == undefined) { return true; } getOwnPropertyDescriptor(obj: Stack, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - let length = obj.length; - if (index < 0 || index >= length) { - throw new Error("Stack: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } - return; + return } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Stack Object"); + throw new RangeError("Can setPrototype on Stack Object"); } } interface IterableIterator { @@ -101,11 +96,17 @@ if (flag || fastStack == undefined) { return item; } pop(): T { + if (this.isEmpty()) { + return undefined; + } let result = this[this.length - 1]; this.elementNum--; return result; } peek(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.length - 1]; } locate(element: T): number { @@ -136,8 +137,8 @@ if (flag || fastStack == undefined) { let stack = this; return { next: function () { - var done = count >= stack.elementNum; - var value = !done ? stack[count++] : undefined; + let done = count >= stack.elementNum; + let value = !done ? stack[count++] : undefined; return { done: done, value: value, diff --git a/container/stack/native_module_stack.cpp b/container/stack/native_module_stack.cpp index abd74f6..0acbe9e 100644 --- a/container/stack/native_module_stack.cpp +++ b/container/stack/native_module_stack.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/struct/js_struct.ts b/container/struct/js_struct.ts index 3f4e34d..751136a 100644 --- a/container/struct/js_struct.ts +++ b/container/struct/js_struct.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -12,6 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +type CompFun = (firstValue: T, secondValue: T) => boolean; + function hashCode(element: any): number { let str = String(element); let hash = 0; @@ -56,7 +58,7 @@ function compareToString(string1: String, string2: String) { throw new Error("this function run error"); } -function currencyCompare(a: any, b: any, compareFn?: Function): number { +function currencyCompare(a: any, b: any, compareFn?: CompFun): number { if (a === b) return ComparResult.EQUALS; if (a instanceof Pair && b instanceof Pair) { return currencyCompare(a.key, b.key, compareFn); @@ -198,14 +200,15 @@ class LightWeightClass { protected getIndexByKey(key: K): number { let hash = hashCode(key); let index = this.binarySearchAtLightWeight(hash); + // js ( A negative number indicates an inverted number in the array ) if (index < 0 || index >= this.memberNumber) return -1; return index; } protected deletemember(key: K): V { + let result: any = undefined; let index = this.getIndexByKey(key); - if (index < 0) - throw new Error("don't find the key in lightweight"); + if (index < 0) return result; this.memberNumber--; this.members.hashs.splice(index, 1); this.members.keys.splice(index, 1); @@ -279,8 +282,8 @@ class RBTreeClass { public memberNumber: number; private isChange: boolean; private treeNodeArray: Array>; - private compFun: Function | undefined; - constructor(comparator?: (firstValue: K, secondValue: K) => boolean, root?: RBTreeNode) { + private compFun: CompFun | undefined; + constructor(comparator?: CompFun, root?: RBTreeNode) { this.root = root; this.compFun = comparator; this.memberNumber = 0; @@ -751,39 +754,36 @@ class DictionaryClass { } protected put(key: K, value: V = key as unknown as V): boolean { - if (key != undefined && value != undefined) { - this.isChange = true; - if (!this.hasKey(key)) this.memberNumber++; - const position = this.getHashIndex(key); - let members = this.tableLink[position]; - if (members instanceof LinkedList && members.count >= 8) { - let newElement = new RBTreeClass(); - let current = members.getHead(); - while (current != null || current != undefined) { - if (!(current.element instanceof Pair)) - throw new Error("this hashtable member save error"); - newElement.addNode(current.element.key, current.element.value); - current = current.next; - } - newElement.addNode(key, value); - this.tableLink[position] = newElement; - return true; - } else if (members instanceof RBTreeClass) { - members.addNode(key, value); + + this.isChange = true; + if (!this.hasKey(key)) this.memberNumber++; + const position = this.getHashIndex(key); + let members = this.tableLink[position]; + if (members instanceof LinkedList && members.count >= 8) { + let newElement = new RBTreeClass(); + let current = members.getHead(); + while (current != null || current != undefined) { + if (!(current.element instanceof Pair)) return false; + newElement.addNode(current.element.key, current.element.value); + current = current.next; + } + newElement.addNode(key, value); + this.tableLink[position] = newElement; + return true; + } else if (members instanceof RBTreeClass) { + members.addNode(key, value); + this.tableLink[position] = members; + return true; + } else { + if (this.tableLink[position] == undefined) { + members = new LinkedList>(); + } + if (!this.replaceMember(key, value)) { + members.push(new Pair(key, value)); this.tableLink[position] = members; - return true; - } else { - if (this.tableLink[position] == undefined) { - members = new LinkedList>(); - } - if (!this.replaceMember(key, value)) { - members.push(new Pair(key, value)); - this.tableLink[position] = members; - } - return true; } + return true; } - return false; } protected replaceMember(key: K, value: V = key as unknown as V): boolean { @@ -978,7 +978,7 @@ class LinkedList { return false; } - indexOf(element: T, compareFn?: Function) { + indexOf(element: T, compareFn?: CompFun) { let current = this.head; for (let i = 0; i < this.count && current != undefined; i++) { if (currencyCompare(element, current.element, compareFn) === ComparResult.EQUALS) { @@ -989,7 +989,7 @@ class LinkedList { return -1; } - remove(element: T, compareFn?: Function) { + remove(element: T, compareFn?: CompFun) { this.removeAt(this.indexOf(element, compareFn)); } @@ -1020,7 +1020,6 @@ class LinkedList { } } - export default { isIncludeToArray, LightWeightClass, diff --git a/container/struct/native_module_struct.cpp b/container/struct/native_module_struct.cpp index ee65b63..084f170 100644 --- a/container/struct/native_module_struct.cpp +++ b/container/struct/native_module_struct.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/treemap/js_treemap.ts b/container/treemap/js_treemap.ts index 90ac9e3..1305aeb 100644 --- a/container/treemap/js_treemap.ts +++ b/container/treemap/js_treemap.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -69,20 +69,19 @@ if (flag || fastTreeMap === undefined) { } get(key: K): V { let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("The node of this key does not exist in the tree"); + if (tempNode === undefined) return tempNode; return tempNode.value; } getFirstKey(): K { let tempNode = this.constitute.firstNode(); if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + return tempNode; return tempNode.key; } getLastKey(): K { let tempNode = this.constitute.lastNode(); if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + return tempNode; return tempNode.key; } setAll(map: TreeMap) { @@ -98,36 +97,36 @@ if (flag || fastTreeMap === undefined) { this.constitute.clearTree(); } getLowerKey(key: K): K { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.left !== undefined) return tempNode.left.key; let node = tempNode; while (node.parent !== undefined) { if (node.parent.right === node) return node.parent.key; node = node.parent; } - throw new Error("don't find a key meet the conditions"); + return result; } getHigherKey(key: K): K { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.right !== undefined) return tempNode.right.key; let node = tempNode; while (node.parent !== undefined) { if (node.parent.left === node) return node.parent.key; node = node.parent; } - throw new Error("don't find a key meet the conditions"); + return result; } keys(): IterableIterator { let data = this.constitute; let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, @@ -141,8 +140,8 @@ if (flag || fastTreeMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].value : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].value : undefined; count++; return { done: done, @@ -170,8 +169,8 @@ if (flag || fastTreeMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -185,8 +184,8 @@ if (flag || fastTreeMap === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, diff --git a/container/treemap/native_module_treemap.cpp b/container/treemap/native_module_treemap.cpp index 430fca6..c330c0c 100644 --- a/container/treemap/native_module_treemap.cpp +++ b/container/treemap/native_module_treemap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/treeset/js_treeset.ts b/container/treeset/js_treeset.ts index 8ce0e00..6ed0e9e 100644 --- a/container/treeset/js_treeset.ts +++ b/container/treeset/js_treeset.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -77,63 +77,59 @@ if (flag || fastTreeSet === undefined) { } getFirstValue(): T { let tempNode = this.constitute.firstNode(); - if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + if (tempNode === undefined) return tempNode; return tempNode.key; } getLastValue(): T { let tempNode = this.constitute.lastNode(); - if (tempNode === undefined) - throw new Error("don't find this key,this tree is empty"); + if (tempNode === undefined) return tempNode; return tempNode.key; } getLowerValue(key: T): T { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.left !== undefined) return tempNode.left.key; let node = tempNode; while (node.parent !== undefined) { if (node.parent.right === node) return node.parent.key; node = node.parent; // node.parent.left === node is true; } - throw new Error("don't find a key meet the conditions"); + return result; } getHigherValue(key: T): T { + let result: any = undefined; let tempNode = this.constitute.getNode(key); - if (tempNode === undefined) - throw new Error("don't find this key,this node is undefine"); + if (tempNode === undefined) return tempNode; if (tempNode.right !== undefined) return tempNode.right.key; let node = tempNode; while (node.parent !== undefined) { if (node.parent.left === node) return node.parent.key; node = node.parent; // node.parent.right === node is true; } - throw new Error("don't find a key meet the conditions"); + return result; } popFirst(): T { let firstNode = this.constitute.firstNode(); - if (firstNode === undefined) - throw new Error("don't find first node,this tree is empty"); + if (firstNode === undefined) return firstNode; let value = firstNode.value; this.constitute.removeNodeProcess(firstNode); - return value as T; + return value; } popLast(): T { let lastNode = this.constitute.lastNode(); - if (lastNode === undefined) - throw new Error("don't find last node,this tree is empty"); + if (lastNode === undefined) return lastNode; let value = lastNode.value; this.constitute.removeNodeProcess(lastNode); - return value as T; + return value; } values(): IterableIterator { let data = this.constitute; let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].value as T : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].value as T : undefined; count++; return { done: done, @@ -155,8 +151,8 @@ if (flag || fastTreeSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].entry() : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].entry() : undefined; count++; return { done: done, @@ -170,8 +166,8 @@ if (flag || fastTreeSet === undefined) { let count = 0; return { next: function () { - var done = count >= data.memberNumber; - var value = !done ? data.keyValueArray[count].key : undefined; + let done = count >= data.memberNumber; + let value = !done ? data.keyValueArray[count].key : undefined; count++; return { done: done, diff --git a/container/treeset/native_module_treeset.cpp b/container/treeset/native_module_treeset.cpp index 769559e..d416e5f 100644 --- a/container/treeset/native_module_treeset.cpp +++ b/container/treeset/native_module_treeset.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/container/tsconfig.json b/container/tsconfig.json index cab5e9d..d1d5e1d 100644 --- a/container/tsconfig.json +++ b/container/tsconfig.json @@ -7,7 +7,7 @@ "outDir": "./jscode", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "strict": true, + // "strict": true, "skipLibCheck": true, "noImplicitThis": false, "suppressImplicitAnyIndexErrors": true diff --git a/container/vector/js_vector.ts b/container/vector/js_vector.ts index 28e946d..feec128 100644 --- a/container/vector/js_vector.ts +++ b/container/vector/js_vector.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -22,16 +22,19 @@ if (arkPritvate !== undefined) { } if (flag || fastVector == undefined) { class HandlerVector { - get(obj: Vector, prop: any) { - if (typeof prop === "symbol") { - return obj[prop]; - } - var index = Number.parseInt(prop); + private isOutBounds(obj: Vector, prop: any) { + let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index >= obj.length) { - throw new Error("Vector: get out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } } + } + get(obj: Vector, prop: any) { + if (typeof prop === "symbol") { + return obj[prop]; + } + this.isOutBounds(obj, prop); return obj[prop]; } set(obj: Vector, prop: any, value: T) { @@ -42,7 +45,7 @@ if (flag || fastVector == undefined) { let index = Number.parseInt(prop); if (Number.isInteger(index)) { if (index < 0 || index > obj.length) { - throw new Error("Vector: set out-of-bounds"); + throw new RangeError("the index is out-of-bounds"); } else { obj[index] = value; return true; @@ -51,11 +54,9 @@ if (flag || fastVector == undefined) { return false; } deleteProperty(obj: Vector, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("vector: deleteProperty out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { obj.removeByIndex(index); return true; } @@ -66,7 +67,7 @@ if (flag || fastVector == undefined) { } ownKeys(obj: Vector) { let keys = []; - for (var i = 0; i < obj.length; i++) { + for (let i = 0; i < obj.length; i++) { keys.push(i.toString()); } return keys; @@ -75,17 +76,15 @@ if (flag || fastVector == undefined) { return true; } getOwnPropertyDescriptor(obj: Vector, prop: any) { - var index = Number.parseInt(prop); - if (Number.isInteger(index)) { - if (index < 0 || index >= obj.length) { - throw new Error("Vector: getOwnPropertyDescriptor out-of-bounds"); - } + this.isOutBounds(obj, prop); + let index = Number.parseInt(prop); + if (index >= 0 && index < obj.length && Number.isInteger(index)) { return Object.getOwnPropertyDescriptor(obj, prop); } return; } setPrototypeOf(obj: any, prop: any): any { - throw new Error("Can setPrototype on Vector Object"); + throw new RangeError("Can setPrototype on Vector Object"); } } interface IterableIterator { @@ -111,6 +110,9 @@ if (flag || fastVector == undefined) { return true; } insert(element: T, index: number): void { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } if (this.isFull()) { this.resize(); } @@ -140,12 +142,22 @@ if (flag || fastVector == undefined) { return -1; } getFirstElement(): T { + if (this.isEmpty()) { + return undefined; + } return this[0]; } - set(index: number, element: T): void { + set(index: number, element: T): T { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } this[index] = element; + return this[index]; } removeByIndex(index: number): T { + if (index < 0 || index >= this.elementNum) { + throw new RangeError("the index is out-of-bounds"); + } let result = this[index]; for (let i = index; i < this.elementNum - 1; i++) { this[i] = this[i + 1]; @@ -165,6 +177,9 @@ if (flag || fastVector == undefined) { return false; } getLastElement(): T { + if (this.isEmpty()) { + return undefined; + } return this[this.elementNum - 1]; } getLastIndexOf(element: T): number { @@ -200,9 +215,12 @@ if (flag || fastVector == undefined) { } removeByRange(fromIndex: number, toIndex: number): void { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`); } - toIndex = toIndex >= this.length - 1 ? this.length - 1 : toIndex; + if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); + } + toIndex = toIndex >= this.elementNum ? this.elementNum : toIndex; let i = fromIndex; for (let j = toIndex; j < this.elementNum; j++) { this[i] = this[j]; @@ -211,6 +229,9 @@ if (flag || fastVector == undefined) { this.elementNum -= toIndex - fromIndex; } setLength(newSize: number): void { + if (newSize < 0) { + throw new RangeError(`An incorrect size was set`); + } this.elementNum = newSize; } replaceAllElements(callbackfn: (value: T, index?: number, vector?: Vector) => T, @@ -239,7 +260,7 @@ if (flag || fastVector == undefined) { } } } else { - for (var i = 0; i < this.length - 1; i++) { + for (let i = 0; i < this.elementNum - 1; i++) { for (let j = 0; j < this.elementNum - 1 - i; j++) { if (this.asciSort(this[j], this[j + 1])) { isSort = false; @@ -270,10 +291,10 @@ if (flag || fastVector == undefined) { } subVector(fromIndex: number, toIndex: number): Vector { if (fromIndex >= toIndex) { - throw new Error(`fromIndex cannot be less than or equal to toIndex`); + throw new RangeError(`the fromIndex cannot be less than or equal to toIndex`); } if (fromIndex >= this.elementNum || fromIndex < 0 || toIndex < 0) { - throw new Error(`fromIndex or toIndex is out-of-bounds`); + throw new RangeError(`the fromIndex or the toIndex is out-of-bounds`); } toIndex = toIndex >= this.elementNum - 1 ? this.elementNum - 1 : toIndex; let vector = new Vector(); @@ -334,8 +355,8 @@ if (flag || fastVector == undefined) { let vector = this; return { next: function () { - var done = count >= vector.elementNum; - var value = !done ? vector[count++] : undefined; + let done = count >= vector.elementNum; + let value = !done ? vector[count++] : undefined; return { done: done, value: value, diff --git a/container/vector/native_module_vector.cpp b/container/vector/native_module_vector.cpp index fb4088a..5974a8f 100644 --- a/container/vector/native_module_vector.cpp +++ b/container/vector/native_module_vector.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 -- Gitee From 9a0a0e2e1c4bb5443592a0efca20ea46328c0261 Mon Sep 17 00:00:00 2001 From: Bi Date: Fri, 11 Mar 2022 16:35:49 +0800 Subject: [PATCH 2/3] Modify the issue in the js_util_module warehouse Signed-off-by: xllify Author: xllify Date: Fri Mar 11 16:35:49 2022 +0800 --- README.md | 18 ++--- README_zh.md | 10 +-- build_ts_js.py | 4 +- mozilla_docs.txt | 4 +- util/js_base64.cpp | 32 ++++----- util/js_base64.h | 2 +- util/js_textdecoder.cpp | 10 +-- util/js_textdecoder.h | 2 +- util/js_textencoder.cpp | 8 +-- util/js_textencoder.h | 2 +- util/js_types.cpp | 2 +- util/js_types.h | 3 +- util/native_module_util.cpp | 2 +- util/src/util_js.ts | 129 ++++++++++++++++++------------------ 14 files changed, 113 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 5b78f40..5af5e0a 100755 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - [Related warehouse](#Related warehouse) ## Introduction -The interface of util is used for character Textencoder, TextDecoder and HelpFunction module.The TextEncoder represents a text encoder that accepts a string as input, encodes it in UTF-8 format, and outputs UTF-8 byte stream. The TextDecoder interface represents a text decoder. The decoder takes the byte stream as the input and outputs the Stirng string. HelpFunction is mainly used to callback and promise functions, write and output error codes, and format class strings. +The interface of util is used for character Textencoder, TextDecoder and HelpFunction module.The TextEncoder represents a text encoder that accepts a string as input, encodes it in UTF-8 format, and outputs UTF-8 byte stream. The TextDecoder interface represents a text decoder. The decoder takes the byte stream as the input and outputs the String string. HelpFunction is mainly used to callback and promise functions, write and output error codes, and format class strings. Encodes all bytes from the specified u8 array into a newly-allocated u8 array using the Base64 encoding scheme or Encodes the specified byte array into a String using the Base64 encoding scheme.Decodes a Base64 encoded String or input u8 array into a newly-allocated u8 array using the Base64 encoding scheme.The rational number is mainly to compare rational numbers and obtain the numerator and denominator.The LruBuffer algorithm replaces the least used data with new data when the buffer space is insufficient. The algorithm derives from the need to access resources: recently accessed data can be Will visit again in the near future. The least accessed data is the least valuable data that should be kicked out of the cache space. The Scope interface is used to describe the valid range of a field. The constructor for the Scope instance is used to create objects with specified lower and upper bounds and require that these objects be comparable. ## 目录 @@ -135,8 +135,8 @@ base/compileruntime/js_util_module/ | Interface name | Description | | -------- | -------- | | readonly encoding : string | In the TextEncoder module, get the encoding format, only UTF-8 is supported. | -| encode(input : string) : Uint8Array | Input stirng string, encode and output UTF-8 byte stream. | -| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | Enter the stirng string, dest represents the storage location after encoding, and returns an object, read represents the number of characters that have been encoded,and written represents the size of bytes occupied by the encoded characters. | +| encode(input : string) : Uint8Array | Input string string, encode and output UTF-8 byte stream. | +| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | Enter the string string, dest represents the storage location after encoding, and returns an object, read represents the number of characters that have been encoded,and written represents the size of bytes occupied by the encoded characters. | | constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | Constructor, the first parameter encoding indicates the format of decoding.The second parameter represents some attributes.Fatal in the attribute indicates whether an exception is thrown, and ignoreBOM indicates whether to ignore the bom flag. | | readonly encoding : string | In the TextDecoder module, get the set decoding format. | | readonly fatal : boolean | Get the setting that throws the exception. | @@ -277,30 +277,30 @@ var obj = textEncoder.encodeInto('abc', dest); 4.textDecoder() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); ``` 5.readonly encoding() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var getEncoding = textDecoder.encoding(); ``` 6.readonly fatal() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var fatalStr = textDecoder.fatal(); ``` 7.readonly ignoreBOM() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var ignoreBom = textDecoder.ignoreBOM(); ``` 8.decode() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var result = textDecoder.decode(input, {stream : true}); ``` 9.printf() @@ -1025,7 +1025,7 @@ var result = proc.isWeakSet(new WeakSet()); -[js_util_module subsystem](https://gitee.com/OHOS_STD/js_util_module) +[js_util_module subsystem](base/compileruntime/js_util_module-readme.md) [base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md) diff --git a/README_zh.md b/README_zh.md index 91fce74..7449c91 100644 --- a/README_zh.md +++ b/README_zh.md @@ -276,30 +276,30 @@ var obj = textEncoder.encodeInto('abc', dest); 4.textDecoder() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); ``` 5.readonly encoding() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var getEncoding = textDecoder.encoding(); ``` 6.readonly fatal() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var fatalStr = textDecoder.fatal(); ``` 7.readonly ignoreBOM() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var ignoreBom = textDecoder.ignoreBOM(); ``` 8.decode() ``` import util from '@ohos.util' -var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false}); +var textDecoder = new util.textDecoder("utf-16be", {fatal : true, ignoreBOM : false}); var result = textDecoder.decode(input, {stream : true}); ``` 9.printf() diff --git a/build_ts_js.py b/build_ts_js.py index 93af118..f1ddd35 100755 --- a/build_ts_js.py +++ b/build_ts_js.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -30,8 +30,6 @@ def run_command(in_cmd): raise Exception(stdout) if __name__ == '__main__': - - PARSER_INST = argparse.ArgumentParser() PARSER_INST.add_argument('--dst-file', help='the converted target file') diff --git a/mozilla_docs.txt b/mozilla_docs.txt index a458057..2fd6176 100644 --- a/mozilla_docs.txt +++ b/mozilla_docs.txt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -15,7 +15,7 @@ The definitions of some interfaces implemented in jsapi/api/js_url.cpp are released under Mozilla license. -The definitions and functions of these interfaces are consistent with the standard interfaces under mozila license, +The definitions and functions of these interfaces are consistent with the standard interfaces under mozilla license, but the implementation of specific functions is independent and self-developed. All interfaces are described in d.ts, the following is the interface written in d.ts under to Mozilla license diff --git a/util/js_base64.cpp b/util/js_base64.cpp index c4181be..fe78f66 100755 --- a/util/js_base64.cpp +++ b/util/js_base64.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -49,14 +49,14 @@ namespace OHOS::Util { NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset)); inputEncode_ = static_cast(resultData) + byteOffset; unsigned char *rets = EncodeAchieve(inputEncode_, length); - if (rets == nullptr) { + if (!rets) { napi_throw_error(env, "-1", "encode input is null"); } void *data = nullptr; napi_value arrayBuffer = nullptr; size_t bufferSize = outputLen; napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(rets), bufferSize) != 0) { + if (memcpy_s(data, bufferSize, reinterpret_cast(rets), bufferSize) != EOK) { FreeMemory(rets); HILOG_ERROR("copy ret to arraybuffer error"); return nullptr; @@ -78,7 +78,7 @@ 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) { + if (!ret) { napi_throw_error(env, "-1", "encodeToString input is null"); } const char *encString = reinterpret_cast(ret); @@ -101,7 +101,7 @@ namespace OHOS::Util { } if (outputLen > 0) { ret = new unsigned char[outputLen + 1]; - if (memset_s(ret, outputLen + 1, '\0', outputLen + 1) != 0) { + if (memset_s(ret, outputLen + 1, '\0', outputLen + 1) != EOK) { HILOG_ERROR("encode ret memset_s failed"); FreeMemory(ret); return nullptr; @@ -110,7 +110,7 @@ namespace OHOS::Util { HILOG_ERROR("outputLen is error"); return nullptr; } - if (ret == nullptr) { + if (!ret) { return ret; } while (inp < inputLen) { @@ -156,7 +156,7 @@ namespace OHOS::Util { napi_get_value_string_utf8(env, src, nullptr, 0, &prolen); if (prolen > 0) { inputString = new char[prolen + 1]; - if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) { + if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != EOK) { FreeMemory(inputString); napi_throw_error(env, "-1", "decode inputString memset_s failed"); } @@ -175,7 +175,7 @@ namespace OHOS::Util { napi_value arrayBuffer = nullptr; size_t bufferSize = decodeOutLen; napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(pret), bufferSize) != 0) { + if (memcpy_s(data, bufferSize, reinterpret_cast(pret), bufferSize) != EOK) { FreeMemory(inputString); FreeMemory(pret); HILOG_ERROR("copy retDecode to arraybuffer error"); @@ -209,14 +209,14 @@ namespace OHOS::Util { retLen = DecodeOut(equalCount, retLen); if (retLen > 0) { retDecode = new unsigned char[retLen + 1]; - if (memset_s(retDecode, retLen + 1, '\0', retLen + 1) != 0) { + if (memset_s(retDecode, retLen + 1, '\0', retLen + 1) != EOK) { FreeMemory(retDecode); napi_throw_error(env, "-1", "decode retDecode memset_s failed"); } } else { napi_throw_error(env, "-2", "retLen is error !"); } - if (retDecode == nullptr) { + if (!retDecode) { return retDecode; } while (inp < (inputLen - equalCount)) { @@ -364,14 +364,14 @@ namespace OHOS::Util { encodeInfo->soutputLen = outputLen; if (outputLen > 0) { ret = new unsigned char[outputLen + 1]; - if (memset_s(ret, outputLen + 1, '\0', outputLen + 1) != 0) { + if (memset_s(ret, outputLen + 1, '\0', outputLen + 1) != EOK) { FreeMemory(ret); napi_throw_error(encodeInfo->env, "-1", "ret path memset_s failed"); } } else { napi_throw_error(encodeInfo->env, "-2", "outputLen is error !"); } - if (ret == nullptr) { + if (!ret) { return ret; } while (inp < inputLen) { @@ -410,7 +410,7 @@ namespace OHOS::Util { napi_value arrayBuffer = nullptr; size_t bufferSize = stdEncodeInfo->soutputLen; napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(stdEncodeInfo->sinputEncoding), bufferSize) != 0) { + if (memcpy_s(data, bufferSize, reinterpret_cast(stdEncodeInfo->sinputEncoding), bufferSize) != EOK) { HILOG_ERROR("copy ret to arraybuffer error"); napi_delete_async_work(env, stdEncodeInfo->worker); return; @@ -458,7 +458,7 @@ namespace OHOS::Util { napi_get_value_string_utf8(env, src, nullptr, 0, &prolen); if (prolen > 0) { inputString = new char[prolen + 1]; - if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != 0) { + if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != EOK) { napi_throw_error(env, "-1", "decode inputString memset_s failed"); } } else { @@ -542,7 +542,7 @@ namespace OHOS::Util { retLen = DecodeOut(equalCount, retLen, decodeInfo); if (retLen > 0) { retDecode = new unsigned char[retLen + 1]; - if (memset_s(retDecode, retLen + 1, '\0', retLen + 1) != 0) { + if (memset_s(retDecode, retLen + 1, '\0', retLen + 1) != EOK) { FreeMemory(retDecode); napi_throw_error(decodeInfo->env, "-1", "decode retDecode memset_s failed"); } @@ -584,7 +584,7 @@ namespace OHOS::Util { napi_value arrayBuffer = nullptr; size_t bufferSize = stdDecodeInfo->decodeOutLen; napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(stdDecodeInfo->sinputDecoding), bufferSize) != 0) { + if (memcpy_s(data, bufferSize, reinterpret_cast(stdDecodeInfo->sinputDecoding), bufferSize) != EOK) { HILOG_ERROR("copy ret to arraybuffer error"); napi_delete_async_work(env, stdDecodeInfo->worker); return; diff --git a/util/js_base64.h b/util/js_base64.h index f5cac42..b35c7ce 100755 --- a/util/js_base64.h +++ b/util/js_base64.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/util/js_textdecoder.cpp b/util/js_textdecoder.cpp index a5dc293..3373451 100755 --- a/util/js_textdecoder.cpp +++ b/util/js_textdecoder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -78,7 +78,7 @@ namespace OHOS::Util { UChar *arr = nullptr; if (limit > 0) { arr = new UChar[limit + 1]; - if (memset_s(arr, len + sizeof(UChar), 0, len + sizeof(UChar)) != 0) { + if (memset_s(arr, len + sizeof(UChar), 0, len + sizeof(UChar)) != EOK) { HILOG_ERROR("decode arr memset_s failed"); FreedMemory(arr); return nullptr; @@ -148,7 +148,7 @@ namespace OHOS::Util { size_t TextDecoder::GetMinByteSize() const { - if (tranTool_ == nullptr) { + if (!tranTool_) { return 0; } size_t res = ucnv_getMinCharSize(tranTool_.get()); @@ -157,7 +157,7 @@ namespace OHOS::Util { void TextDecoder::Reset() const { - if (tranTool_ == nullptr) { + if (!tranTool_) { return; } ucnv_reset(tranTool_.get()); @@ -174,7 +174,7 @@ namespace OHOS::Util { void TextDecoder::SetBomFlag(const UChar *arr, const UErrorCode codeFlag, const DecodeArr decArr, size_t &rstLen, bool &bomFlag) { - if (arr == nullptr) { + if (!arr) { return; } if (U_SUCCESS(codeFlag)) { diff --git a/util/js_textdecoder.h b/util/js_textdecoder.h index bc74fda..1753e1d 100755 --- a/util/js_textdecoder.h +++ b/util/js_textdecoder.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/util/js_textencoder.cpp b/util/js_textencoder.cpp index 00d385a..375b28f 100755 --- a/util/js_textencoder.cpp +++ b/util/js_textencoder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -41,7 +41,7 @@ namespace OHOS::Util { NAPI_CALL(env_, napi_get_value_string_utf8(env_, src, buffer, 0, &bufferSize)); NAPI_ASSERT(env_, bufferSize > 0, "bufferSize == 0"); buffer = new char[bufferSize + 1]; - if (memset_s(buffer, bufferSize + 1, 0, bufferSize + 1) != 0) { + if (memset_s(buffer, bufferSize + 1, 0, bufferSize + 1) != EOK) { HILOG_ERROR("buffer memset error"); delete []buffer; return nullptr; @@ -51,14 +51,14 @@ namespace OHOS::Util { void *data = nullptr; napi_value arrayBuffer = nullptr; napi_create_arraybuffer(env_, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(buffer), bufferSize) != 0) { + if (memcpy_s(data, bufferSize, reinterpret_cast(buffer), bufferSize) != EOK) { HILOG_ERROR("copy buffer to arraybuffer error"); delete []buffer; return nullptr; } delete []buffer; - + buffer = nullptr; napi_value result = nullptr; NAPI_CALL(env_, napi_create_typedarray(env_, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); diff --git a/util/js_textencoder.h b/util/js_textencoder.h index ab9d419..46deaae 100755 --- a/util/js_textencoder.h +++ b/util/js_textencoder.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/util/js_types.cpp b/util/js_types.cpp index 57e9f7f..842863e 100644 --- a/util/js_types.cpp +++ b/util/js_types.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/util/js_types.h b/util/js_types.h index 26858e6..6c97253 100644 --- a/util/js_types.h +++ b/util/js_types.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include #include diff --git a/util/native_module_util.cpp b/util/native_module_util.cpp index 2582e97..90603f8 100755 --- a/util/native_module_util.cpp +++ b/util/native_module_util.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 diff --git a/util/src/util_js.ts b/util/src/util_js.ts index e3d7716..4fd8b1b 100644 --- a/util/src/util_js.ts +++ b/util/src/util_js.ts @@ -30,13 +30,13 @@ interface Fn{ } declare function requireInternal(s : string) : HelpUtil; const helpUtil = requireInternal('util'); -let TextEncoder = helpUtil.TextEncoder; -let TextDecoder = helpUtil.TextDecoder; -let Base64 = helpUtil.Base64; -let Types = helpUtil.Types; +var TextEncoder = helpUtil.TextEncoder; +var TextDecoder = helpUtil.TextDecoder; +var Base64 = helpUtil.Base64; +var Types = helpUtil.Types; function switchLittleObject(enter : string, obj : Object, count : number) : string | Object { - var str : string = ''; + let str : string = ''; if (obj === null) { str += obj; } else if (obj instanceof Array) { @@ -69,7 +69,7 @@ function switchLittleObject(enter : string, obj : Object, count : number) : stri function switchLittleValue(enter : string, protoName : string, obj : Object, count : number) : string { - var str : string = ''; + let str : string = ''; if (obj[protoName] === null) { str += protoName + ': null,' + enter; } else if (obj[protoName] instanceof Array) { @@ -84,7 +84,7 @@ function switchLittleValue(enter : string, protoName : string, obj : Object, cou str += switchLittleObject(enter + ' ', obj[protoName], count + 1) + ',' + enter; } } else if (typeof obj[protoName] === 'function') { - var space : string= enter; + let space : string= enter; if (obj[protoName].name !== '') { str += obj[protoName].name + ':' + space; } @@ -106,11 +106,11 @@ function switchLittleValue(enter : string, protoName : string, obj : Object, cou function arrayToString(enter : string, arr : Array, count : number) : string { - var str : string = ''; + let str : string = ''; if (!arr.length) { return ''; } - var arrayEnter : string = ', '; + let arrayEnter : string = ', '; for (let k in arr) { if (arr[k] !== null && (typeof arr[k] === 'function' || typeof arr[k] === 'object') && count <= 2) { arrayEnter += enter; @@ -124,9 +124,9 @@ function arrayToString(enter : string, arr : Array, count str += switchLittleObject(enter + ' ', i, count + 1); str += arrayEnter; } else if (typeof i === 'function') { - var space : string = enter; + let space : string = enter; space += ' '; - var end : string = ''; + let end : string = ''; if (i.name !== '') { str += '{ [Function: ' + i.name + ']' + space; end = i.name + ' { [constructor]: [Circular] } }' + arrayEnter; @@ -147,7 +147,7 @@ function arrayToString(enter : string, arr : Array, count function switchBigObject(enter : string, obj : Object, count : number) : string | Object { - var str : string = ''; + let str : string = ''; if (obj === null) { str += obj; } else if (obj instanceof Array) { @@ -177,7 +177,7 @@ function switchBigObject(enter : string, obj : Object, count : number) : string function switchBigValue(enter : string, protoName : string, obj : Object, count : number) : string { - var str : string = ''; + let str : string = ''; if (obj[protoName] === null) { str += protoName + ': null,' + enter; } else if (obj[protoName] instanceof Array) { @@ -206,12 +206,12 @@ function switchBigValue(enter : string, protoName : string, obj : Object, count } function arrayToBigString(enter : string, arr : Array, count : number) : string { - var str : string = ''; + let str : string = ''; if (!arr.length) { return ''; } - var arrayEnter = ', '; + let arrayEnter = ', '; for (let i of arr) { if (i !== null && (typeof i === 'object') && count <= 2) { arrayEnter += enter; @@ -239,7 +239,7 @@ function arrayToBigString(enter : string, arr : Array, cou } function switchIntValue(value : Object | symbol) : string { - var str : string = ''; + let str : string = ''; if (value === '') { str += 'NaN'; } else if (typeof value === 'bigint') { @@ -271,7 +271,7 @@ function switchIntValue(value : Object | symbol) : string } function switchFloatValue(value : Object | symbol) : string { - var str : string = ''; + let str : string = ''; if (value === '') { str += 'NaN'; } else if (typeof value === 'symbol') { @@ -304,7 +304,7 @@ function switchFloatValue(value : Object | symbol) : string function switchNumberValue(value : Object | symbol) : string { - var str : string = ''; + let str : string = ''; if (value === '') { str += '0'; } else if (typeof value === 'symbol') { @@ -329,7 +329,7 @@ function switchNumberValue(value : Object | symbol) : string function switchStringValue(value : Object | symbol) : string { - var str : string = ''; + let str : string = ''; if (typeof value === 'undefined') { str += 'undefined'; } else if (typeof value === 'object') { @@ -349,13 +349,13 @@ function switchStringValue(value : Object | symbol) : string // ...valueString : Array) : string; function printf(formatString : Array, ...valueString : Array) : string { - var formats : string = helpUtil.dealwithformatstring(formatString); - var arr : Array= []; + let formats : string = helpUtil.dealwithformatstring(formatString); + let arr : Array= []; arr = formats.split(' '); - var switchString : Array= []; - var valueLength : number = valueString.length; - var arrLength : number = arr.length; - var i : number= 0; + let switchString : Array= []; + let valueLength : number = valueString.length; + let arrLength : number = arr.length; + let i : number= 0; for (let sub of valueString) { if (i >= arrLength) { break; @@ -383,19 +383,19 @@ function printf(formatString : Array, ...valueString : Arr switchString.push(valueString[i].toString()); i++; } - var helpUtilString : string = helpUtil.printf(formatString, ...switchString); + let helpUtilString : string = helpUtil.printf(formatString, ...switchString); return helpUtilString; } function getErrorString(errnum : number) : string { - var errorString : string = helpUtil.getErrorString(errnum); + let errorString : string = helpUtil.getErrorString(errnum); return errorString; } function createExternalType() : Object { - var externalType : Object = helpUtil.createExternalType(); + let externalType : Object = helpUtil.createExternalType(); return externalType; } @@ -471,7 +471,7 @@ class LruBuffer public constructor(capacity?: number) { - if(capacity !== undefined) { + if (capacity !== undefined) { if (capacity <= 0 || capacity%1 !== 0 || capacity > this.maxNumber) { throw new Error('data error'); } @@ -482,7 +482,7 @@ class LruBuffer public updateCapacity(newCapacity : number) : void { - if (newCapacity <= 0 || newCapacity%1 !== 0 || newCapacity > this.maxNumber) { + if (newCapacity <= 0 || newCapacity % 1 !== 0 || newCapacity > this.maxNumber) { throw new Error('data error'); } else if (this.cache.size >newCapacity) { this.changeCapacity(newCapacity); @@ -695,7 +695,7 @@ class RationalNumber { num = den < 0 ? num * (-1) : num; den = den < 0 ? den * (-1) : den; - if (den == 0) { + if (den === 0) { if (num > 0) { this.mnum = 1; this.mden = 0; @@ -706,13 +706,13 @@ class RationalNumber this.mnum = 0; this.mden = 0; } - } else if (num == 0) { + } else if (num === 0) { this.mnum = 0; this.mden = 1; } else { let gnum : number = 0; gnum = this.getCommonDivisor(num, den); - if (gnum != 0) { + if (gnum !== 0) { this.mnum = num / gnum; this.mden = den / gnum; } @@ -721,10 +721,10 @@ class RationalNumber public createRationalFromString(str : string): RationalNumber { - if (str == null) { + if (str === null) { throw new Error('string invalid!'); } - if (str == 'NaN') { + if (str === 'NaN') { return new RationalNumber(0, 0); } let colon : number = str.indexOf(':'); @@ -742,15 +742,15 @@ class RationalNumber public compareTo(other : RationalNumber) : number { - if (this.mnum == other.mnum && this.mden == other.mden) { + if (this.mnum === other.mnum && this.mden === other.mden) { return 0; - } else if (this.mnum == 0 && this.mden == 0) { + } else if (this.mnum === 0 && this.mden === 0) { return 1; - } else if ((other.mnum == 0) && (other.mden == 0)) { + } else if ((other.mnum === 0) && (other.mden === 0)) { return -1; - } else if ((this.mden == 0 && this.mnum > 0) || (other.mden == 0 && other.mnum < 0)) { + } else if ((this.mden === 0 && this.mnum > 0) || (other.mden === 0 && other.mnum < 0)) { return 1; - } else if ((this.mden == 0 && this.mnum < 0) || (other.mden == 0 && other.mnum > 0)) { + } else if ((this.mden === 0 && this.mnum < 0) || (other.mden === 0 && other.mnum > 0)) { return -1; } let thisnum : number = this.mnum * other.mden; @@ -771,15 +771,15 @@ class RationalNumber } let thisnum : number = this.mnum * obj.mden; let objnum : number = obj.mnum * this.mden; - if (this.mnum == obj.mnum && this.mden == obj.mden) { + if (this.mnum === obj.mnum && this.mden === obj.mden) { return true; - } else if ((thisnum == objnum) && (this.mnum != 0 && this.mden != 0) && (obj.mnum != 0 && obj.mden != 0)) { + } else if ((thisnum === objnum) && (this.mnum !== 0 && this.mden !== 0) && (obj.mnum !== 0 && obj.mden !== 0)) { return true; - } else if ((this.mnum == 0 && this.mden != 0) && (obj.mnum == 0 && obj.mden != 0)) { + } else if ((this.mnum === 0 && this.mden !== 0) && (obj.mnum === 0 && obj.mden !== 0)) { return true; - } else if ((this.mnum > 0 && this.mden == 0) && (obj.mnum > 0 && obj.mden == 0)) { + } else if ((this.mnum > 0 && this.mden === 0) && (obj.mnum > 0 && obj.mden === 0)) { return true; - } else if ((this.mnum < 0 && this.mden == 0) && (obj.mnum < 0 && obj.mden == 0)) { + } else if ((this.mnum < 0 && this.mden === 0) && (obj.mnum < 0 && obj.mden === 0)) { return true; } else { return false; @@ -788,11 +788,11 @@ class RationalNumber public valueOf() : number { - if (this.mnum > 0 && this.mden == 0) { + if (this.mnum > 0 && this.mden === 0) { return Number.POSITIVE_INFINITY; - } else if (this.mnum < 0 && this.mden == 0) { + } else if (this.mnum < 0 && this.mden === 0) { return Number.NEGATIVE_INFINITY; - } else if ((this.mnum == 0) && (this.mden == 0)) { + } else if ((this.mnum === 0) && (this.mden === 0)) { return Number.NaN; } else { return this.mnum / this.mden; @@ -801,8 +801,8 @@ class RationalNumber public getCommonDivisor(number1 : number, number2 : number) : number { - if (number1 == 0 || number2 == 0) { - throw new Error("Parameter cannot be zero!"); + if (number1 === 0 || number2 === 0) { + throw new Error('Parameter cannot be zero!'); } let temp : number = 0; if (number1 < number2) { @@ -810,7 +810,7 @@ class RationalNumber number1 = number2; number2 = temp; } - while (number1 % number2 != 0) { + while (number1 % number2 !== 0) { temp = number1 % number2; number1 = number2; number2 = temp; @@ -830,28 +830,28 @@ class RationalNumber public isFinite() : boolean { - return this.mden != 0; + return this.mden !== 0; } public isNaN() : boolean { - return this.mnum == 0 && this.mden == 0; + return this.mnum === 0 && this.mden === 0; } public isZero() : boolean { - return this.mnum == 0 && this.mden != 0; + return this.mnum === 0 && this.mden !== 0; } public toString() : string { let buf : string; - if (this.mnum == 0 && this.mden == 0) { - buf = "NaN"; - } else if (this.mnum > 0 && this.mden == 0) { - buf = "Infinity"; - } else if (this.mnum < 0 && this.mden == 0) { - buf = "-Infinity"; + if (this.mnum === 0 && this.mden === 0) { + buf = 'NaN'; + } else if (this.mnum > 0 && this.mden === 0) { + buf = 'Infinity'; + } else if (this.mnum < 0 && this.mden === 0) { + buf = '-Infinity'; } else { buf = String(this.mnum) + '/' + String(this.mden); } @@ -898,7 +898,7 @@ class Scope { let resLower: boolean; let resUpper: boolean; this.checkNull(x, 'value must not be null'); - if(x instanceof Scope) { + if (x instanceof Scope) { resLower= x._lowerLimit.compareTo(this._lowerLimit); resUpper= this._upperLimit.compareTo(x._upperLimit); } else { @@ -927,8 +927,7 @@ class Scope { let reUpper: boolean; let mLower: ScopeType; let mUpper: ScopeType; - if(y) - { + if (y) { this.checkNull(x, 'lower limit must not be null'); this.checkNull(y, 'upper limit must not be null'); reLower = this._lowerLimit.compareTo(x); @@ -984,7 +983,7 @@ class Scope { } else { this.checkNull(x, 'lower limit must not be null'); - this.checkNull(y, "upper limit must not be null"); + this.checkNull(y, 'upper limit must not be null'); reLower = x.compareTo(this._lowerLimit); reUpper = this._upperLimit.compareTo(y); if (reLower && reUpper) { @@ -1003,7 +1002,7 @@ class Scope { } public checkNull(o: ScopeType, str: string): void { - if(o == null) { + if (o === null) { throw new Error(str); } } -- Gitee From 0a5cbc291633b4a03a0c720f5c330276f1c594ed Mon Sep 17 00:00:00 2001 From: xllify Date: Fri, 11 Mar 2022 17:02:39 +0800 Subject: [PATCH 3/3] Modify the issue in the js_util_module warehouse Signed-off-by: xllify Author: xllify Date: Fri Mar 11 17:02:39 2022 +0800 Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. Author: xllify Date: Fri Mar 11 17:02:39 2022 +0800 Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. Author: xllify Date: Fri Mar 11 17:02:39 2022 +0800 Please enter the commit message for your changes. Lines starting with '#' will be ignored, and an empty message aborts the commit. Author: xllify Date: Fri Mar 11 17:02:39 2022 +0800 --- README.md | 4 ---- README_zh.md | 6 +----- util/js_base64.cpp | 16 +++++++++------- util/js_textdecoder.cpp | 10 +++++----- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5af5e0a..1fc35cf 100755 --- a/README.md +++ b/README.md @@ -1023,12 +1023,8 @@ var result = proc.isWeakSet(new WeakSet()); ``` ## Related warehouse - - [js_util_module subsystem](base/compileruntime/js_util_module-readme.md) -[base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md) - ## License Util is available under [Mozilla license](https://www.mozilla.org/en-US/MPL/), and the documentation is detailed in [documentation](https://gitee.com/openharmony/js_util_module/blob/master/mozilla_docs.txt). See [LICENSE](https://gitee.com/openharmony/js_util_module/blob/master/LICENSE) for the full license text. \ No newline at end of file diff --git a/README_zh.md b/README_zh.md index 7449c91..4b1002c 100644 --- a/README_zh.md +++ b/README_zh.md @@ -1024,11 +1024,7 @@ 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) +[js_util_module子系统](base/compileruntime/js_util_module-readme_zh.md) ## 许可证 diff --git a/util/js_base64.cpp b/util/js_base64.cpp index fe78f66..03cbe86 100755 --- a/util/js_base64.cpp +++ b/util/js_base64.cpp @@ -49,7 +49,7 @@ namespace OHOS::Util { NAPI_CALL(env, napi_get_typedarray_info(env, src, &type, &length, &resultData, &resultBuffer, &byteOffset)); inputEncode_ = static_cast(resultData) + byteOffset; unsigned char *rets = EncodeAchieve(inputEncode_, length); - if (!rets) { + if (rets == nullptr) { napi_throw_error(env, "-1", "encode input is null"); } void *data = nullptr; @@ -78,7 +78,7 @@ 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) { + if (ret == nullptr) { napi_throw_error(env, "-1", "encodeToString input is null"); } const char *encString = reinterpret_cast(ret); @@ -110,7 +110,7 @@ namespace OHOS::Util { HILOG_ERROR("outputLen is error"); return nullptr; } - if (!ret) { + if (ret == nullptr) { return ret; } while (inp < inputLen) { @@ -216,7 +216,7 @@ namespace OHOS::Util { } else { napi_throw_error(env, "-2", "retLen is error !"); } - if (!retDecode) { + if (retDecode == nullptr) { return retDecode; } while (inp < (inputLen - equalCount)) { @@ -371,7 +371,7 @@ namespace OHOS::Util { } else { napi_throw_error(encodeInfo->env, "-2", "outputLen is error !"); } - if (!ret) { + if (ret == nullptr) { return ret; } while (inp < inputLen) { @@ -410,7 +410,8 @@ namespace OHOS::Util { napi_value arrayBuffer = nullptr; size_t bufferSize = stdEncodeInfo->soutputLen; napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(stdEncodeInfo->sinputEncoding), bufferSize) != EOK) { + if (memcpy_s(data, bufferSize, + reinterpret_cast(stdEncodeInfo->sinputEncoding), bufferSize) != EOK) { HILOG_ERROR("copy ret to arraybuffer error"); napi_delete_async_work(env, stdEncodeInfo->worker); return; @@ -584,7 +585,8 @@ namespace OHOS::Util { napi_value arrayBuffer = nullptr; size_t bufferSize = stdDecodeInfo->decodeOutLen; napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(stdDecodeInfo->sinputDecoding), bufferSize) != EOK) { + if (memcpy_s(data, bufferSize, + reinterpret_cast(stdDecodeInfo->sinputDecoding), bufferSize) != EOK) { HILOG_ERROR("copy ret to arraybuffer error"); napi_delete_async_work(env, stdDecodeInfo->worker); return; diff --git a/util/js_textdecoder.cpp b/util/js_textdecoder.cpp index 3373451..db58cc7 100755 --- a/util/js_textdecoder.cpp +++ b/util/js_textdecoder.cpp @@ -88,7 +88,7 @@ namespace OHOS::Util { return nullptr; } UChar *target = arr; - size_t tarStartPos = reinterpret_cast(arr); + size_t tarStartPos = reinterpret_cast(arr); ucnv_toUnicode(GetConverterPtr(), &target, target + len, &source, source + length, nullptr, flush, &codeFlag); size_t resultLength = 0; bool omitInitialBom = false; @@ -148,7 +148,7 @@ namespace OHOS::Util { size_t TextDecoder::GetMinByteSize() const { - if (!tranTool_) { + if (tranTool_ == nullptr) { return 0; } size_t res = ucnv_getMinCharSize(tranTool_.get()); @@ -157,7 +157,7 @@ namespace OHOS::Util { void TextDecoder::Reset() const { - if (!tranTool_) { + if (tranTool_ == nullptr) { return; } ucnv_reset(tranTool_.get()); @@ -174,12 +174,12 @@ namespace OHOS::Util { void TextDecoder::SetBomFlag(const UChar *arr, const UErrorCode codeFlag, const DecodeArr decArr, size_t &rstLen, bool &bomFlag) { - if (!arr) { + if (arr == nullptr) { return; } if (U_SUCCESS(codeFlag)) { if (decArr.limitLen > 0) { - rstLen = reinterpret_cast(decArr.target) - decArr.tarStartPos; + rstLen = reinterpret_cast(decArr.target) - decArr.tarStartPos; if (rstLen > 0 && IsUnicode() && !IsIgnoreBom() && !IsBomFlag()) { bomFlag = (arr[0] == 0xFEFF) ? true : false; label_ |= static_cast(ConverterFlags::BOM_SEEN_FLG); -- Gitee