diff --git a/bundle.json b/bundle.json index 058a07072140c41d97d1c4042292271e7c8b8df9..252d483fc0ae0566c3d4cc4ea700496afd6227d9 100644 --- a/bundle.json +++ b/bundle.json @@ -77,6 +77,7 @@ "//commonlibrary/ets_utils/js_api_module/convertxml:convertxml_packages", "//commonlibrary/ets_utils/js_api_module/xml:xml_packages", "//commonlibrary/ets_utils/js_api_module/buffer:buffer_packages", + "//commonlibrary/ets_utils/js_api_module/fastbuffer:fastbuffer_packages", "//commonlibrary/ets_utils/js_concurrent_module/utils:utils_packages", "//commonlibrary/ets_utils/js_concurrent_module/taskpool:taskpool_packages", "//commonlibrary/ets_utils/js_concurrent_module/worker:worker_packages", diff --git a/js_api_module/fastbuffer/BUILD.gn b/js_api_module/fastbuffer/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..9eeea56ae1f315718da98c5458dd79d6528dd644 --- /dev/null +++ b/js_api_module/fastbuffer/BUILD.gn @@ -0,0 +1,151 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/components/ace_engine/ace_gen_obj.gni") +import("//build/config/components/ets_frontend/es2abc_config.gni") +import("//build/ohos.gni") +import("//commonlibrary/ets_utils/ets_utils_config.gni") + +# compile .ts to .js. +action("build_ts_js") { + script = "${ets_util_path}/js_api_module/build_ts_js.py" + + outFile_Path = target_out_dir + "/" + current_cpu + args = [ + "--dst-file", + rebase_path(target_out_dir + "/js_fastbuffer.js"), + "--module-path", + rebase_path("/commonlibrary/ets_utils/js_api_module/fastbuffer"), + "--out-file", + rebase_path(outFile_Path + "/js_fastbuffer.js"), + "--out-filePath", + rebase_path(outFile_Path), + "--relative-path", + rebase_path("//", root_build_dir), + ] + outputs = [ target_out_dir + "/js_fastbuffer.js" ] +} + +config("fastbuffer_config") { + cflags_cc = [ + "-std=c++17", + "-Wno-deprecated-declarations", + ] +} + +base_output_path = get_label_info(":js_fastbuffer", "target_out_dir") +gen_obj("js_fastbuffer") { + input = "$target_out_dir/js_fastbuffer.js" + if (use_mac || use_mingw_win || use_ios || use_linux) { + js_fastbuffer_obj_path = base_output_path + "/fastbuffer.c" + } else { + js_fastbuffer_obj_path = base_output_path + "/fastbuffer.o" + } + output = js_fastbuffer_obj_path + snapshot_dep = [ ":build_ts_js" ] +} + +# compile .js to .abc. +es2abc_gen_abc("gen_fastbuffer_abc") { + extra_visibility = [ ":*" ] + src_js = rebase_path(target_out_dir + "/js_fastbuffer.js") + dst_file = rebase_path(target_out_dir + "/fastbuffer.abc") + in_puts = [ target_out_dir + "/js_fastbuffer.js" ] + out_puts = [ target_out_dir + "/fastbuffer.abc" ] + extra_args = [ "--module" ] + extra_dependencies = [ ":build_ts_js" ] +} + +abc_output_path = get_label_info(":fastbuffer_abc", "target_out_dir") +gen_obj("fastbuffer_abc") { + input = "$target_out_dir/fastbuffer.abc" + if (use_mac || use_mingw_win || use_ios || use_linux) { + fastbuffer_abc_obj_path = abc_output_path + "/fastbuffer_abc.c" + } else { + fastbuffer_abc_obj_path = abc_output_path + "/fastbuffer_abc.o" + } + output = fastbuffer_abc_obj_path + snapshot_dep = [ ":gen_fastbuffer_abc" ] +} + +fastbuffer_sources = [ + "native_module_fastbuffer.cpp", +] + +ohos_shared_library("fastbuffer") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + deps = [ ":fastbuffer_static" ] + external_deps = [ "hilog:libhilog" ] + subsystem_name = "commonlibrary" + part_name = "ets_utils" + + relative_install_dir = "module" +} + +ohos_source_set("fastbuffer_static") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + include_dirs = [ + "include", + ets_util_path, + ] + + sources = fastbuffer_sources + + deps = [ + ":gen_obj_src_fastbuffer_abc", + ":gen_obj_src_js_fastbuffer", + ] + + configs = [ ":fastbuffer_config" ] + + if (is_arkui_x) { + include_dirs += [ + "$plugins_root/hilog/include", + "$plugins_root/interfaces", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + ] + if (target_os == "android") { + defines = [ "ANDROID_PLATFORM" ] + } else if (target_os == "ios") { + defines = [ "IOS_PLATFORM" ] + } + deps += [ + "$plugins_root/libs/icu:icu_${target_os}", + "$plugins_root/libs/napi:napi_${target_os}", + "$plugins_root/libs/securec:sec_${target_os}", + ] + } else { + external_deps = [ + "bounds_checking_function:libsec_shared", + "hilog:libhilog", + "icu:shared_icuuc", + "napi:ace_napi", + ] + } + subsystem_name = "commonlibrary" + part_name = "ets_utils" +} + +group("fastbuffer_packages") { + public_deps = [ ":fastbuffer" ] +} diff --git a/js_util_module/container/fastbuffer/native_module_fastbuffer.cpp b/js_api_module/fastbuffer/native_module_fastbuffer.cpp similarity index 97% rename from js_util_module/container/fastbuffer/native_module_fastbuffer.cpp rename to js_api_module/fastbuffer/native_module_fastbuffer.cpp index e34e37789a8a47c2d7c2cb260032864f65009a18..36395a72dd1412a0b6e84e0cace42556afe00fe8 100644 --- a/js_util_module/container/fastbuffer/native_module_fastbuffer.cpp +++ b/js_api_module/fastbuffer/native_module_fastbuffer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2025 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/js_util_module/container/fastbuffer/js_fastbuffer.ts b/js_api_module/fastbuffer/src/js_fastbuffer.ts similarity index 88% rename from js_util_module/container/fastbuffer/js_fastbuffer.ts rename to js_api_module/fastbuffer/src/js_fastbuffer.ts index a390a6dd5ca1edea181bf787af4fd7e5508960cc..b2e1c2ea4725ed5f8a373f7503f809aaed5d4ab8 100644 --- a/js_util_module/container/fastbuffer/js_fastbuffer.ts +++ b/js_api_module/fastbuffer/src/js_fastbuffer.ts @@ -67,8 +67,9 @@ enum RangeErrorCategories { LEFT }; -const UINT32MAX = 4294967296; -const MAX_LENGTH: number = Math.pow(2, 32); +const UINT32MAX = 4294967295; +const INT64_MAX = 2n ** 63n - 1n; +const UINT64_MAX = 2n ** 64n - 1n; class ErrorMessage { public errorNumber: number = 0; @@ -332,21 +333,6 @@ class FastBuffer extends FastBufferInner { lastIndexOf(value: string | number | FastBuffer | Uint8Array, byteOffset: number = this.length, encoding: string = 'utf8'): number { - if (typeof value === 'string') { - if (value.length === 0) { - return -1; - } - if (typeof byteOffset === null) { - byteOffset = 0; - } - if (encoding === null) { - encoding = 'utf8'; - } - encoding = encoding.toLowerCase(); - let str = this.toString(encoding); - byteOffset = byteOffset < 0 ? str.length + byteOffset : byteOffset; - return str.lastIndexOf(value, byteOffset); - } if (typeof byteOffset === 'string') { encoding = byteOffset; } @@ -365,25 +351,11 @@ class FastBuffer extends FastBufferInner { targetStart = isNaN(targetStart) ? 0 : Number(targetStart); sourceStart = isNaN(sourceStart) ? 0 : Number(sourceStart); sourceEnd = isNaN(sourceEnd) ? this.length : Number(sourceEnd); - rangeLeftErrorCheck(targetStart, 'targetStart', 0); - rangeLeftErrorCheck(sourceStart, 'sourceStart', 0); - rangeLeftErrorCheck(sourceEnd, 'sourceEnd', 0); - if (targetStart >= target.length) { - return 0; - } - if (sourceEnd <= sourceStart || sourceStart >= this.length) { - return 0; - } return super.copy(target, targetStart, sourceStart, sourceEnd); } fill(value: string | FastBuffer | Uint8Array | number, offset: number = 0, end: number = this.length, encoding: string = 'utf8'): FastBuffer { - rangeErrorCheck(offset, 'offset', 0, UINT32MAX); - rangeErrorCheck(end, 'end', 0, this.length); - if (offset > end - 1) { - return this; - } return super.fill(value, offset, end, encoding); } @@ -402,16 +374,6 @@ class FastBuffer extends FastBufferInner { sourceEnd = this.length; } typeErrorCheck(target, ['FastBuffer', 'Uint8Array'], 'target'); - typeErrorCheck(targetStart, ['number'], 'targetStart'); - typeErrorCheck(targetEnd, ['number'], 'targetEnd'); - typeErrorCheck(sourceStart, ['number'], 'sourceStart'); - typeErrorCheck(sourceEnd, ['number'], 'sourceEnd'); - if (sourceStart >= sourceEnd) { - return (targetStart >= targetEnd ? 0 : -1); - } - if (targetStart >= targetEnd) { - return 1; - } return super.compare(target, targetStart, targetEnd, sourceStart, sourceEnd); } @@ -438,6 +400,38 @@ class FastBuffer extends FastBufferInner { return new FastBuffer(this.buffer, start, end - start); } + writeBigInt64BE(value: bigint, offset: number = 0): number { + if (typeof value !== "bigint") { + throw typeError(value, 'value', ['bigint']); + } + rangeErrorCheck(value, 'value', -INT64_MAX, INT64_MAX); + return super.writeBigInt64BE(value, offset); + } + + writeBigInt64LE(value: bigint, offset: number = 0): number { + if (typeof value !== "bigint") { + throw typeError(value, 'value', ['bigint']); + } + rangeErrorCheck(value, 'value', -INT64_MAX, INT64_MAX); + return super.writeBigInt64LE(value, offset); + } + + writeBigUInt64BE(value: bigint, offset: number = 0): number { + if (typeof value !== "bigint") { + throw typeError(value, 'value', ['bigint']); + } + rangeErrorCheck(value, 'value', 0, UINT64_MAX); + return super.writeBigUInt64BE(value, offset); + } + + writeBigUInt64LE(value: bigint, offset: number = 0): number { + if (typeof value !== "bigint") { + throw typeError(value, 'value', ['bigint']); + } + rangeErrorCheck(value, 'value', 0, UINT64_MAX); + return super.writeBigUInt64LE(value, offset); + } + swap16(): FastBuffer { const len = this.length; const dealLen: number = twoBytes; @@ -531,28 +525,12 @@ function from(value: FastBuffer | Uint8Array | ArrayBuffer | SharedArrayBuffer | throw typeError(value, 'value', ['FastBuffer', 'ArrayBuffer', 'Array', 'Array-like', 'string', 'object']); } -function typeErrorForSize(param: unknown, paramName: string, excludedTypes: string[]): BusinessError { - let msg = new ErrorMessage(errorMap.typeError, paramName).setSizeTypeInfo(excludedTypes, param).getString(); - return new BusinessError(msg, errorMap.typeError); -} - -function sizeErrorCheck(param: unknown, paramName: string, types: string[], - rangeLeft: number, rangeRight: number): void { - let typeName = getTypeName(param); - if (!types.includes(typeName)) { - throw typeErrorForSize(param, paramName, types); - } - if (Number(param) < rangeLeft || Number(param) > rangeRight) { - let typeString = types.join(', '); - typeString = typeString.replace(',', ' or'); - let msg = 'Parameter error. The type of "' + paramName + '" must be ' + typeString + - ' and the value cannot be negative. Received value is: ' + Number(param).toString(); +function alloc(size: number, fill?: string | FastBuffer | number, encoding?: string): FastBuffer { + if (size < 0 || size > UINT32MAX) { + let msg = 'Parameter error. The type of "size" must be ' + 'number' + + ' and the value cannot be negative. Received value is: ' + Number(size).toString(); throw new BusinessError(msg, errorMap.typeError); } -} - -function alloc(size: number, fill?: string | FastBuffer | number, encoding?: string): FastBuffer { - sizeErrorCheck(size, 'size', ['number'], 0, MAX_LENGTH); const buf = new FastBuffer(size); if (arguments.length === twoBytes && fill !== undefined && fill !== 0) { buf.fill(fill); @@ -569,7 +547,11 @@ function alloc(size: number, fill?: string | FastBuffer | number, encoding?: str } function allocUninitialized(size: number): FastBuffer { - sizeErrorCheck(size, 'size', ['number'], 0, MAX_LENGTH); + if (size < 0 || size > UINT32MAX) { + let msg = 'Parameter error. The type of "size" must be ' + 'number' + + ' and the value cannot be negative. Received value is: ' + Number(size).toString(); + throw new BusinessError(msg, errorMap.typeError); + } const buf = new FastBuffer(size); return buf; } @@ -584,24 +566,13 @@ function typeError(param: unknown, paramName: string, excludedTypes: string[]): return new BusinessError(msg, errorMap.typeError); } -function rangeErrorCheck(param: number, paramName: string, - rangeLeft: number, rangeRight: number): void { +function rangeErrorCheck(param: number | bigint, paramName: string, + rangeLeft: number | bigint, rangeRight: number | bigint): void { if (param < rangeLeft || param > rangeRight) { throw rangeError(paramName, rangeLeft, rangeRight, param); } } -function rangeLeftErrorCheck(param: number, paramName: string, rangeLeft: number): void { - if (param < rangeLeft) { - throw rangeLeftError(paramName, rangeLeft, param); - } -} - -function rangeLeftError(paramName: string, rangeLeft: number, receivedValue: number): BusinessError { - let msg = new ErrorMessage(errorMap.rangeError, paramName).setRangeLeftInfo(rangeLeft, receivedValue).getString(); - return new BusinessError(msg, errorMap.rangeError); -} - function rangeError(paramName: string, rangeLeft: string | bigint | number, rangeRight: string | bigint | number, receivedValue: number | bigint): BusinessError { let msg = @@ -628,20 +599,11 @@ function alignPool(): void { } function allocUninitializedFromPool(size: number): FastBuffer { - sizeErrorCheck(size, 'size', ['number'], 0, MAX_LENGTH); - // Coming soon - // if (!pool) { - // createPool(); - // } - // if (size < (poolSize >>> 1)) { - // if (size > (poolSize - poolOffset)) { - // createPool(); - // } - // const b = new FastBuffer(pool, poolOffset, size); - // poolOffset += size; - // alignPool(); - // return b; - // } + if (size < 0 || size > UINT32MAX) { + let msg = 'Parameter error. The type of "size" must be ' + 'number' + + ' and the value cannot be negative. Received value is: ' + Number(size).toString(); + throw new BusinessError(msg, errorMap.typeError); + } return new FastBuffer(size); } diff --git a/js_api_module/fastbuffer/tsconfig.json b/js_api_module/fastbuffer/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..04c6658120629973741da9a7826a3c1959aea5a2 --- /dev/null +++ b/js_api_module/fastbuffer/tsconfig.json @@ -0,0 +1,14 @@ +{ +"compilerOptions": { + "target": "es2020", + "module": "es2020", + "rootDir": "./src", + "outDir": "./out", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, + "noImplicitThis": false, + "suppressImplicitAnyIndexErrors": true + } +} + diff --git a/js_util_module/container/BUILD.gn b/js_util_module/container/BUILD.gn index 199b294e1ff20e9dff1ae85e459dadae9833572c..7c9a5774f76629ccb9f244ec8f0e09230ae2d419 100644 --- a/js_util_module/container/BUILD.gn +++ b/js_util_module/container/BUILD.gn @@ -33,7 +33,6 @@ container_names = [ "lightweightmap", "lightweightset", "plainarray", - "fastbuffer", ] # compile .ts to .js.