Skip to content

Commit

Permalink
move
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jun 19, 2022
1 parent 2095282 commit ac04397
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
60 changes: 0 additions & 60 deletions packages/emnapi/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,66 +284,6 @@ function $emnapiSetErrorCode (envObject: emnapi.Env, error: Error & { code?: str
return napi_status.napi_ok
}

declare const arrayBufferMemoryMap: WeakMap<ArrayBuffer, number>
declare const typedArrayMemoryMap: WeakMap<TypedArray | DataView, number>
declare const memoryPointerDeleter: FinalizationRegistry<number>

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)
Expand Down
60 changes: 60 additions & 0 deletions packages/emnapi/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,63 @@ mergeInto(LibraryManager.library, {
},
napi_clear_last_error__deps: ['$emnapi']
})

declare const arrayBufferMemoryMap: WeakMap<ArrayBuffer, number>
declare const typedArrayMemoryMap: WeakMap<TypedArray | DataView, number>
declare const memoryPointerDeleter: FinalizationRegistry<number>

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

0 comments on commit ac04397

Please sign in to comment.