Skip to content

Commit

Permalink
test error
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jul 3, 2022
1 parent 79b41df commit 841e82e
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 22 deletions.
88 changes: 72 additions & 16 deletions packages/emnapi/src/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ function napi_get_all_property_names (
return emnapi.preamble(env, (envObject) => {
return emnapi.checkArgs(envObject, [result, object], () => {
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
if (key_mode !== napi_key_collection_mode.napi_key_include_prototypes && key_mode !== napi_key_collection_mode.napi_key_own_only) {
Expand All @@ -19,7 +26,7 @@ function napi_get_all_property_names (
if (key_conversion !== napi_key_conversion.napi_key_keep_numbers && key_conversion !== napi_key_conversion.napi_key_numbers_to_strings) {
return envObject.setLastError(napi_status.napi_invalid_arg)
}
const names = emnapiGetPropertyNames(h.value, key_mode, key_filter, key_conversion)
const names = emnapiGetPropertyNames(v, key_mode, key_filter, key_conversion)
HEAP32[result >> 2] = emnapi.addToCurrentScope(envObject, names).id
return envObject.getReturnStatus()
})
Expand Down Expand Up @@ -54,10 +61,17 @@ function napi_has_property (env: napi_env, object: napi_value, key: napi_value,
return emnapi.preamble(env, (envObject) => {
return emnapi.checkArgs(envObject, [key, result, object], () => {
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
HEAPU8[result] = (emnapi.handleStore.get(key)!.value in h.value) ? 1 : 0
HEAPU8[result] = (emnapi.handleStore.get(key)!.value in v) ? 1 : 0
return envObject.getReturnStatus()
})
})
Expand All @@ -67,10 +81,17 @@ function napi_get_property (env: napi_env, object: napi_value, key: napi_value,
return emnapi.preamble(env, (envObject) => {
return emnapi.checkArgs(envObject, [key, result, object], () => {
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
HEAP32[result >> 2] = envObject.ensureHandleId(h.value[emnapi.handleStore.get(key)!.value])
HEAP32[result >> 2] = envObject.ensureHandleId(v[emnapi.handleStore.get(key)!.value])
return envObject.getReturnStatus()
})
})
Expand All @@ -96,14 +117,21 @@ function napi_has_own_property (env: napi_env, object: napi_value, key: napi_val
return emnapi.preamble(env, (envObject) => {
return emnapi.checkArgs(envObject, [key, result, object], () => {
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
const prop = emnapi.handleStore.get(key)!.value
if (typeof prop !== 'string' && typeof prop !== 'symbol') {
return envObject.setLastError(napi_status.napi_name_expected)
}
const r = Object.prototype.hasOwnProperty.call(h.value, emnapi.handleStore.get(key)!.value)
const r = Object.prototype.hasOwnProperty.call(v, emnapi.handleStore.get(key)!.value)
HEAPU8[result] = r ? 1 : 0
return envObject.getReturnStatus()
})
Expand Down Expand Up @@ -133,10 +161,17 @@ function napi_has_named_property (env: napi_env, object: napi_value, utf8name: c
return envObject.setLastError(napi_status.napi_invalid_arg)
}
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
const r = UTF8ToString(utf8name) in h.value
const r = UTF8ToString(utf8name) in v
HEAPU8[result] = r ? 1 : 0
return envObject.getReturnStatus()
})
Expand All @@ -150,10 +185,17 @@ function napi_get_named_property (env: napi_env, object: napi_value, utf8name: c
return envObject.setLastError(napi_status.napi_invalid_arg)
}
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
HEAP32[result >> 2] = envObject.ensureHandleId(h.value[UTF8ToString(utf8name)])
HEAP32[result >> 2] = envObject.ensureHandleId(v[UTF8ToString(utf8name)])
return envObject.getReturnStatus()
})
})
Expand All @@ -176,10 +218,17 @@ function napi_has_element (env: napi_env, object: napi_value, index: uint32_t, r
return emnapi.preamble(env, (envObject) => {
return emnapi.checkArgs(envObject, [result, object], () => {
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
HEAPU8[result] = ((index >>> 0) in h.value) ? 1 : 0
HEAPU8[result] = ((index >>> 0) in v) ? 1 : 0
return envObject.getReturnStatus()
})
})
Expand All @@ -189,10 +238,17 @@ function napi_get_element (env: napi_env, object: napi_value, index: uint32_t, r
return emnapi.preamble(env, (envObject) => {
return emnapi.checkArgs(envObject, [result, object], () => {
const h = emnapi.handleStore.get(object)!
if (!(h.isObject() || h.isFunction())) {
if (h.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 = h.isObject() || h.isFunction() ? h.value : Object(h.value)
} catch (_) {
return envObject.setLastError(napi_status.napi_object_expected)
}
HEAP32[result >> 2] = envObject.ensureHandleId(h.value[index >>> 0])
HEAP32[result >> 2] = envObject.ensureHandleId(v[index >>> 0])
return envObject.getReturnStatus()
})
})
Expand Down
11 changes: 9 additions & 2 deletions packages/emnapi/src/value/convert2c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ function napi_get_prototype (env: napi_env, value: napi_value, result: Pointer<n
return emnapi.checkEnv(env, (envObject) => {
return emnapi.checkArgs(envObject, [value, result], () => {
const handle = emnapi.handleStore.get(value)!
if (!(handle.isObject() || handle.isFunction())) {
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)
}
HEAP32[result >> 2] = envObject.ensureHandleId(Object.getPrototypeOf(handle.value))
HEAP32[result >> 2] = envObject.ensureHandleId(Object.getPrototypeOf(v))
return envObject.clearLastError()
})
})
Expand Down
1 change: 1 addition & 0 deletions packages/test/cgen.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = function (_options, { isDebug, isEmscripten }) {

const buildSources = [
'addon.cc',
'error.cc',
'binding.cc'
].map(p => `./node-addon-api/${p}`)

Expand Down
4 changes: 2 additions & 2 deletions packages/test/node-addon-api/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Object InitAddon(Env env);
// Object InitDataViewReadWrite(Env env);
// Object InitEnvCleanup(Env env);
// Object InitErrorHandlingPrim(Env env);
// Object InitError(Env env);
Object InitError(Env env);
// Object InitExternal(Env env);
// Object InitFunction(Env env);
// Object InitFunctionReference(Env env);
Expand Down Expand Up @@ -114,7 +114,7 @@ Object Init(Env env, Object exports) {
// #if (NAPI_VERSION > 2)
// exports.Set("env_cleanup", InitEnvCleanup(env));
// #endif
// exports.Set("error", InitError(env));
exports.Set("error", InitError(env));
// exports.Set("errorHandlingPrim", InitErrorHandlingPrim(env));
// exports.Set("external", InitExternal(env));
// exports.Set("function", InitFunction(env));
Expand Down
Loading

0 comments on commit 841e82e

Please sign in to comment.