forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
178 additions
and
21 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
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
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,43 @@ | ||
#include "embedtest_node_api.h" | ||
|
||
#include <mutex> | ||
#include <thread> | ||
|
||
static const char* main_script = | ||
"globalThis.require = require('module').createRequire(process.execPath);\n" | ||
"require('vm').runInThisContext(process.argv[1]);"; | ||
|
||
// Test the preload callback being called. | ||
extern "C" int32_t test_main_preload_node_api(int32_t argc, char* argv[]) { | ||
CHECK(node_api_initialize_platform(argc, | ||
argv, | ||
node_api_platform_no_flags, | ||
nullptr, | ||
nullptr, | ||
nullptr, | ||
nullptr)); | ||
|
||
node_api_env_options options; | ||
CHECK(node_api_create_env_options(&options)); | ||
CHECK(node_api_env_options_set_preload_callback( | ||
options, | ||
[](napi_env env, | ||
napi_value /*process*/, | ||
napi_value /*require*/, | ||
void* /*cb_data*/) { | ||
napi_value global, value; | ||
napi_get_global(env, &global); | ||
napi_create_int32(env, 42, &value); | ||
napi_set_named_property(env, global, "preloadValue", value); | ||
}, | ||
nullptr)); | ||
napi_env env; | ||
CHECK(node_api_create_env( | ||
options, nullptr, nullptr, main_script, NAPI_VERSION, &env)); | ||
|
||
CHECK(node_api_delete_env(env, nullptr)); | ||
|
||
CHECK(node_api_dispose_platform()); | ||
|
||
return 0; | ||
} |
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,18 @@ | ||
// Print the globalThis.preloadValue set by the preload script. | ||
const mainPreloadValue = globalThis.preloadValue; | ||
|
||
// Test that the preload script is executed in the worker thread. | ||
const { Worker } = require('worker_threads'); | ||
const worker = new Worker( | ||
'require("worker_threads").parentPort.postMessage({value: globalThis.preloadValue})', | ||
{ eval: true } | ||
); | ||
|
||
const messages = []; | ||
worker.on('message', (message) => messages.push(message)); | ||
|
||
process.on('beforeExit', () => { | ||
console.log( | ||
`preloadValue=${mainPreloadValue}; worker preloadValue=${messages[0].value}` | ||
); | ||
}); |
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