From ac04397043359b7b57da22d8158caa7cb5f1db6d Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Sun, 19 Jun 2022 14:52:53 +0800 Subject: [PATCH] move --- packages/emnapi/src/internal.ts | 60 --------------------------------- packages/emnapi/src/util.ts | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/packages/emnapi/src/internal.ts b/packages/emnapi/src/internal.ts index d59a669e..6e3dd709 100644 --- a/packages/emnapi/src/internal.ts +++ b/packages/emnapi/src/internal.ts @@ -284,66 +284,6 @@ function $emnapiSetErrorCode (envObject: emnapi.Env, error: Error & { code?: str return napi_status.napi_ok } -declare const arrayBufferMemoryMap: WeakMap -declare const typedArrayMemoryMap: WeakMap -declare const memoryPointerDeleter: FinalizationRegistry - -mergeInto(LibraryManager.library, { - $memoryPointerDeleter__deps: ['free'], - $memoryPointerDeleter: 'typeof FinalizationRegistry === "function" ? new FinalizationRegistry(function (pointer) { _free(pointer); }) : undefined', - $arrayBufferMemoryMap: 'new WeakMap()', - $typedArrayMemoryMap: 'new WeakMap()', - - $getArrayBufferPointer__deps: ['$arrayBufferMemoryMap', '$memoryPointerDeleter'], - $getArrayBufferPointer: function (arrayBuffer: ArrayBuffer): void_p { - if ((!memoryPointerDeleter) || (arrayBuffer === HEAPU8.buffer)) { - return NULL - } - - let pointer: void_p - if (arrayBufferMemoryMap.has(arrayBuffer)) { - pointer = arrayBufferMemoryMap.get(arrayBuffer)! - HEAPU8.set(new Uint8Array(arrayBuffer), pointer) - return pointer - } - - pointer = emnapiGetDynamicCalls.call_malloc('$getArrayBufferPointer', arrayBuffer.byteLength) - HEAPU8.set(new Uint8Array(arrayBuffer), pointer) - arrayBufferMemoryMap.set(arrayBuffer, pointer) - memoryPointerDeleter.register(arrayBuffer, pointer) - return pointer - }, - - $getViewPointer__deps: ['$typedArrayMemoryMap', '$memoryPointerDeleter'], - $getViewPointer: function (view: TypedArray | DataView): void_p { - if (!memoryPointerDeleter) { - return NULL - } - if (view.buffer === HEAPU8.buffer) { - return view.byteOffset - } - - let pointer: void_p - if (typedArrayMemoryMap.has(view)) { - pointer = typedArrayMemoryMap.get(view)! - HEAPU8.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength), pointer) - return pointer - } - - pointer = emnapiGetDynamicCalls.call_malloc('$getViewPointer', view.byteLength) - HEAPU8.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength), pointer) - typedArrayMemoryMap.set(view, pointer) - memoryPointerDeleter.register(view, pointer) - return pointer - } -}) - -// eslint-disable-next-line @typescript-eslint/no-unused-vars -declare function getArrayBufferPointer (arrayBuffer: ArrayBuffer): void_p -// eslint-disable-next-line @typescript-eslint/no-unused-vars -declare function getViewPointer (arrayBuffer: TypedArray | DataView): void_p -declare type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array - emnapiImplement('$emnapiCreateFunction', $emnapiCreateFunction, ['$emnapiGetDynamicCalls']) emnapiImplement('$emnapiDefineProperty', $emnapiDefineProperty, ['$emnapiCreateFunction']) emnapiImplement('$emnapiCreateTypedArray', $emnapiCreateTypedArray) diff --git a/packages/emnapi/src/util.ts b/packages/emnapi/src/util.ts index 4be73dd2..3f1be5e4 100644 --- a/packages/emnapi/src/util.ts +++ b/packages/emnapi/src/util.ts @@ -24,3 +24,63 @@ mergeInto(LibraryManager.library, { }, napi_clear_last_error__deps: ['$emnapi'] }) + +declare const arrayBufferMemoryMap: WeakMap +declare const typedArrayMemoryMap: WeakMap +declare const memoryPointerDeleter: FinalizationRegistry + +mergeInto(LibraryManager.library, { + $memoryPointerDeleter__deps: ['free'], + $memoryPointerDeleter: 'typeof FinalizationRegistry === "function" ? new FinalizationRegistry(function (pointer) { _free(pointer); }) : undefined', + $arrayBufferMemoryMap: 'new WeakMap()', + $typedArrayMemoryMap: 'new WeakMap()', + + $getArrayBufferPointer__deps: ['$arrayBufferMemoryMap', '$memoryPointerDeleter'], + $getArrayBufferPointer: function (arrayBuffer: ArrayBuffer): void_p { + if ((!memoryPointerDeleter) || (arrayBuffer === HEAPU8.buffer)) { + return NULL + } + + let pointer: void_p + if (arrayBufferMemoryMap.has(arrayBuffer)) { + pointer = arrayBufferMemoryMap.get(arrayBuffer)! + HEAPU8.set(new Uint8Array(arrayBuffer), pointer) + return pointer + } + + pointer = emnapiGetDynamicCalls.call_malloc('$getArrayBufferPointer', arrayBuffer.byteLength) + HEAPU8.set(new Uint8Array(arrayBuffer), pointer) + arrayBufferMemoryMap.set(arrayBuffer, pointer) + memoryPointerDeleter.register(arrayBuffer, pointer) + return pointer + }, + + $getViewPointer__deps: ['$typedArrayMemoryMap', '$memoryPointerDeleter'], + $getViewPointer: function (view: TypedArray | DataView): void_p { + if (!memoryPointerDeleter) { + return NULL + } + if (view.buffer === HEAPU8.buffer) { + return view.byteOffset + } + + let pointer: void_p + if (typedArrayMemoryMap.has(view)) { + pointer = typedArrayMemoryMap.get(view)! + HEAPU8.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength), pointer) + return pointer + } + + pointer = emnapiGetDynamicCalls.call_malloc('$getViewPointer', view.byteLength) + HEAPU8.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength), pointer) + typedArrayMemoryMap.set(view, pointer) + memoryPointerDeleter.register(view, pointer) + return pointer + } +}) + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +declare function getArrayBufferPointer (arrayBuffer: ArrayBuffer): void_p +// eslint-disable-next-line @typescript-eslint/no-unused-vars +declare function getViewPointer (arrayBuffer: TypedArray | DataView): void_p +declare type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array