-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
n-api: re-use napi_env between modules
Store the `napi_env` on the global object at a private key. This gives us one `napi_env` per context. Refs: #14367 PR-URL: #14217 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
- Loading branch information
Showing
5 changed files
with
98 additions
and
3 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
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,12 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "store_env", | ||
"sources": [ "store_env.c" ] | ||
}, | ||
{ | ||
"target_name": "compare_env", | ||
"sources": [ "compare_env.c" ] | ||
} | ||
] | ||
} |
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,22 @@ | ||
#include <node_api.h> | ||
#include "../common.h" | ||
|
||
napi_value compare(napi_env env, napi_callback_info info) { | ||
napi_value external; | ||
size_t argc = 1; | ||
void* data; | ||
napi_value return_value; | ||
|
||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &external, NULL, NULL)); | ||
NAPI_CALL(env, napi_get_value_external(env, external, &data)); | ||
NAPI_CALL(env, napi_get_boolean(env, ((napi_env)data) == env, &return_value)); | ||
|
||
return return_value; | ||
} | ||
|
||
void Init(napi_env env, napi_value exports, napi_value module, void* context) { | ||
napi_property_descriptor prop = DECLARE_NAPI_PROPERTY("exports", compare); | ||
NAPI_CALL_RETURN_VOID(env, napi_define_properties(env, module, 1, &prop)); | ||
} | ||
|
||
NAPI_MODULE(compare_env, Init) |
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,12 @@ | ||
#include <node_api.h> | ||
#include "../common.h" | ||
|
||
void Init(napi_env env, napi_value exports, napi_value module, void* context) { | ||
napi_value external; | ||
NAPI_CALL_RETURN_VOID(env, | ||
napi_create_external(env, env, NULL, NULL, &external)); | ||
NAPI_CALL_RETURN_VOID(env, | ||
napi_set_named_property(env, module, "exports", external)); | ||
} | ||
|
||
NAPI_MODULE(store_env, Init) |
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,10 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const storeEnv = require(`./build/${common.buildType}/store_env`); | ||
const compareEnv = require(`./build/${common.buildType}/compare_env`); | ||
const assert = require('assert'); | ||
|
||
assert.strictEqual(compareEnv(storeEnv), true, | ||
'N-API environment pointers in two different modules have ' + | ||
'the same value'); |