From 15e3d09c6e6d9f13155cfd2250c6314fd81b53f7 Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Sat, 1 Jul 2023 00:12:05 +0800 Subject: [PATCH] fix: wrap `napi_get_array_length` and `napi_get_prototype` with PREAMBLE --- packages/emnapi/src/value/convert2c.ts | 65 +++++++++++++------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/packages/emnapi/src/value/convert2c.ts b/packages/emnapi/src/value/convert2c.ts index 88440fbd..732c8a3a 100644 --- a/packages/emnapi/src/value/convert2c.ts +++ b/packages/emnapi/src/value/convert2c.ts @@ -1,17 +1,17 @@ function napi_get_array_length (env: napi_env, value: napi_value, result: Pointer): napi_status { - $CHECK_ENV!(env) - const envObject = emnapiCtx.envStore.get(env)! - $CHECK_ARG!(envObject, value) - $CHECK_ARG!(envObject, result) - const handle = emnapiCtx.handleStore.get(value)! - if (!handle.isArray()) { - return envObject.setLastError(napi_status.napi_array_expected) - } - $from64('result') - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const v = handle.value.length >>> 0 - $makeSetValue('result', 0, 'v', 'u32') - return envObject.clearLastError() + return $PREAMBLE!(env, (envObject) => { + $CHECK_ARG!(envObject, value) + $CHECK_ARG!(envObject, result) + const handle = emnapiCtx.handleStore.get(value)! + if (!handle.isArray()) { + return envObject.setLastError(napi_status.napi_array_expected) + } + $from64('result') + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const v = handle.value.length >>> 0 + $makeSetValue('result', 0, 'v', 'u32') + return envObject.getReturnStatus() + }) } function napi_get_arraybuffer_info (env: napi_env, arraybuffer: napi_value, data: void_pp, byte_length: Pointer): napi_status { @@ -38,27 +38,26 @@ function napi_get_arraybuffer_info (env: napi_env, arraybuffer: napi_value, data } function napi_get_prototype (env: napi_env, value: napi_value, result: Pointer): napi_status { - $CHECK_ENV!(env) - const envObject = emnapiCtx.envStore.get(env)! - $CHECK_ARG!(envObject, value) - $CHECK_ARG!(envObject, result) - const handle = emnapiCtx.handleStore.get(value)! - if (handle.value == null) { - envObject.tryCatch.setError(new TypeError('Cannot convert undefined or null to object')) - return envObject.setLastError(napi_status.napi_pending_exception) - } - let v: any - try { - v = handle.isObject() || handle.isFunction() ? handle.value : Object(handle.value) - } catch (_) { - return envObject.setLastError(napi_status.napi_object_expected) - } - $from64('result') + return $PREAMBLE!(env, (envObject) => { + $CHECK_ARG!(envObject, value) + $CHECK_ARG!(envObject, result) + const handle = emnapiCtx.handleStore.get(value)! + if (handle.value == null) { + throw new TypeError('Cannot convert undefined or null to object') + } + let v: any + try { + v = handle.isObject() || handle.isFunction() ? handle.value : Object(handle.value) + } catch (_) { + return envObject.setLastError(napi_status.napi_object_expected) + } + $from64('result') - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const p = envObject.ensureHandleId(Object.getPrototypeOf(v)) - $makeSetValue('result', 0, 'p', '*') - return envObject.clearLastError() + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const p = envObject.ensureHandleId(Object.getPrototypeOf(v)) + $makeSetValue('result', 0, 'p', '*') + return envObject.getReturnStatus() + }) } function _napi_get_typedarray_info (