n-api: create napi_env as a real structure
1. We define struct napi_env__ to include the isolate, the last exception, and the info about the last error. 2. We instantiate one struct napi_env__ during module registration and we pass it into the FunctionCallbackInfo for all subsequent entries into N-API when we create functions/accessors/finalizers. Once module unloading will be supported we shall have to delete the napi_env we create during module init. There is a clear separation between public and private API wrt. env: 1. Public APIs assert that env is not nullptr as their first action. 2. Private APIs need not validate env. They assume it's not nullptr. PR-URL: https://github.com/nodejs/node/pull/12195 Fixes: https://github.com/nodejs/abi-stable-node/issues/198 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
parent
491d59da84
commit
0a5bf4aee3
833
src/node_api.cc
833
src/node_api.cc
File diff suppressed because it is too large
Load Diff
8
test/addons-napi/test_napi_status/binding.gyp
Normal file
8
test/addons-napi/test_napi_status/binding.gyp
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"target_name": "test_napi_status",
|
||||
"sources": [ "test_napi_status.cc" ]
|
||||
}
|
||||
]
|
||||
}
|
8
test/addons-napi/test_napi_status/test.js
Normal file
8
test/addons-napi/test_napi_status/test.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../../common');
|
||||
const addon = require(`./build/${common.buildType}/test_napi_status`);
|
||||
const assert = require('assert');
|
||||
|
||||
addon.createNapiError();
|
||||
assert(addon.testNapiErrorCleanup(), 'napi_status cleaned up for second call');
|
47
test/addons-napi/test_napi_status/test_napi_status.cc
Normal file
47
test/addons-napi/test_napi_status/test_napi_status.cc
Normal file
@ -0,0 +1,47 @@
|
||||
#include <node_api.h>
|
||||
|
||||
#define DECLARE_NAPI_METHOD(func) \
|
||||
{ #func, func, 0, 0, 0, napi_default, 0 }
|
||||
|
||||
void createNapiError(napi_env env, napi_callback_info info) {
|
||||
napi_status status;
|
||||
napi_value value;
|
||||
double double_value;
|
||||
|
||||
status = napi_create_string_utf8(env, "xyz", 3, &value);
|
||||
if (status != napi_ok) return;
|
||||
|
||||
status = napi_get_value_double(env, value, &double_value);
|
||||
if (status == napi_ok) {
|
||||
napi_throw_error(env, "Failed to produce error condition");
|
||||
}
|
||||
}
|
||||
|
||||
void testNapiErrorCleanup(napi_env env, napi_callback_info info) {
|
||||
napi_status status;
|
||||
const napi_extended_error_info *error_info = 0;
|
||||
napi_value result;
|
||||
|
||||
status = napi_get_last_error_info(env, &error_info);
|
||||
if (status != napi_ok) return;
|
||||
|
||||
status = napi_get_boolean(env, (error_info->error_code == napi_ok), &result);
|
||||
if (status != napi_ok) return;
|
||||
|
||||
napi_set_return_value(env, info, result);
|
||||
}
|
||||
|
||||
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
|
||||
napi_status status;
|
||||
|
||||
napi_property_descriptor descriptors[] = {
|
||||
DECLARE_NAPI_METHOD(createNapiError),
|
||||
DECLARE_NAPI_METHOD(testNapiErrorCleanup)
|
||||
};
|
||||
|
||||
status = napi_define_properties(
|
||||
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors);
|
||||
if (status != napi_ok) return;
|
||||
}
|
||||
|
||||
NAPI_MODULE(addon, Init)
|
Loading…
x
Reference in New Issue
Block a user