-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move
napi_adjust_external_memory
to JS (#87)
- Loading branch information
1 parent
9614ccb
commit 6f0398f
Showing
7 changed files
with
128 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint-disable @typescript-eslint/indent */ | ||
|
||
function _napi_adjust_external_memory ( | ||
env: napi_env, | ||
change_in_bytes: bigint, | ||
adjusted_value: number | ||
): napi_status { | ||
$CHECK_ENV!(env) | ||
const envObject = emnapiCtx.envStore.get(env)! | ||
$CHECK_ARG!(envObject, adjusted_value) | ||
|
||
const change_in_bytes_number = Number(change_in_bytes) | ||
|
||
if (change_in_bytes_number < 0) { | ||
return envObject.setLastError(napi_status.napi_invalid_arg) | ||
} | ||
|
||
const old_size = wasmMemory.buffer.byteLength | ||
let new_size = old_size + change_in_bytes_number | ||
new_size = new_size + ((65536 - new_size % 65536) % 65536) | ||
if (wasmMemory.grow((new_size - old_size + 65535) >> 16) === -1) { | ||
return envObject.setLastError(napi_status.napi_generic_failure) | ||
} | ||
|
||
$from64('adjusted_value') | ||
if (emnapiCtx.feature.supportBigInt) { | ||
$makeSetValue('adjusted_value', 0, 'wasmMemory.buffer.byteLength', 'i64') | ||
} else { | ||
emnapiSetValueI64(adjusted_value, wasmMemory.buffer.byteLength) | ||
} | ||
|
||
return envObject.clearLastError() | ||
} | ||
|
||
emnapiImplement('napi_adjust_external_memory', 'ipjp', _napi_adjust_external_memory, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable @typescript-eslint/indent */ | ||
declare function _emscripten_resize_heap (requested: number): boolean | ||
|
||
function _napi_adjust_external_memory ( | ||
env: napi_env, | ||
low: number, | ||
high: number, | ||
adjusted_value: number | ||
): napi_status { | ||
$CHECK_ENV!(env) | ||
const envObject = emnapiCtx.envStore.get(env)! | ||
|
||
let change_in_bytes: number | ||
|
||
// #if WASM_BIGINT | ||
if (!high) return envObject.setLastError(napi_status.napi_invalid_arg) | ||
change_in_bytes = Number(low) | ||
// #else | ||
if (!adjusted_value) return envObject.setLastError(napi_status.napi_invalid_arg) | ||
change_in_bytes = (low >>> 0) + (high * Math.pow(2, 32)) | ||
// #endif | ||
|
||
if (change_in_bytes < 0) { | ||
return envObject.setLastError(napi_status.napi_invalid_arg) | ||
} | ||
|
||
if (change_in_bytes > 0) { | ||
const old_size = wasmMemory.buffer.byteLength | ||
const new_size = old_size + change_in_bytes | ||
if (!_emscripten_resize_heap(new_size)) { | ||
return envObject.setLastError(napi_status.napi_generic_failure) | ||
} | ||
} | ||
|
||
// #if WASM_BIGINT | ||
$from64('high') | ||
if (emnapiCtx.feature.supportBigInt) { | ||
$makeSetValue('high', 0, 'wasmMemory.buffer.byteLength', 'i64') | ||
} else { | ||
emnapiSetValueI64(high, wasmMemory.buffer.byteLength) | ||
} | ||
// #else | ||
$from64('adjusted_value') | ||
$makeSetValue('adjusted_value', 0, 'wasmMemory.buffer.byteLength', 'i64') | ||
// #endif | ||
|
||
return envObject.clearLastError() | ||
} | ||
|
||
emnapiImplement('napi_adjust_external_memory', 'ipjp', _napi_adjust_external_memory, | ||
[ | ||
// #if WASM_BIGINT | ||
'$emnapiSetValueI64', | ||
// #endif | ||
'emscripten_resize_heap' | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters