Skip to content

Commit 1e6d3bb

Browse files
Gabriel SchulhofMylesBorins
Gabriel Schulhof
authored andcommittedApr 16, 2018
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. Backport-PR-URL: #19447 PR-URL: #12195 Fixes: nodejs/abi-stable-node#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>
1 parent 9b36811 commit 1e6d3bb

File tree

4 files changed

+498
-399
lines changed

4 files changed

+498
-399
lines changed
 

‎src/node_api.cc

+435-399
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "test_napi_status",
5+
"sources": [ "test_napi_status.cc" ]
6+
}
7+
]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const addon = require(`./build/${common.buildType}/test_napi_status`);
5+
const assert = require('assert');
6+
7+
addon.createNapiError();
8+
assert(addon.testNapiErrorCleanup(), 'napi_status cleaned up for second call');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <node_api.h>
2+
3+
#define DECLARE_NAPI_METHOD(func) \
4+
{ #func, func, 0, 0, 0, napi_default, 0 }
5+
6+
void createNapiError(napi_env env, napi_callback_info info) {
7+
napi_status status;
8+
napi_value value;
9+
double double_value;
10+
11+
status = napi_create_string_utf8(env, "xyz", 3, &value);
12+
if (status != napi_ok) return;
13+
14+
status = napi_get_value_double(env, value, &double_value);
15+
if (status == napi_ok) {
16+
napi_throw_error(env, "Failed to produce error condition");
17+
}
18+
}
19+
20+
void testNapiErrorCleanup(napi_env env, napi_callback_info info) {
21+
napi_status status;
22+
const napi_extended_error_info *error_info = 0;
23+
napi_value result;
24+
25+
status = napi_get_last_error_info(env, &error_info);
26+
if (status != napi_ok) return;
27+
28+
status = napi_get_boolean(env, (error_info->error_code == napi_ok), &result);
29+
if (status != napi_ok) return;
30+
31+
napi_set_return_value(env, info, result);
32+
}
33+
34+
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
35+
napi_status status;
36+
37+
napi_property_descriptor descriptors[] = {
38+
DECLARE_NAPI_METHOD(createNapiError),
39+
DECLARE_NAPI_METHOD(testNapiErrorCleanup)
40+
};
41+
42+
status = napi_define_properties(
43+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors);
44+
if (status != napi_ok) return;
45+
}
46+
47+
NAPI_MODULE(addon, Init)

0 commit comments

Comments
 (0)
Please sign in to comment.