-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend Node-API to libnode
#43542
base: main
Are you sure you want to change the base?
Extend Node-API to libnode
#43542
Changes from all commits
c55bf50
414c501
1fb6c8e
bd223ae
890bfcb
23ddf96
213aea4
fc34203
a4b2b90
5220a89
a0b3b25
ccd228b
7a566b7
b7adf10
ebe1c55
fc3f0ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,8 +166,47 @@ int RunNodeInstance(MultiIsolatePlatform* platform, | |
} | ||
``` | ||
|
||
## Node-API Embedding | ||
|
||
<!--introduced_in=REPLACEME--> | ||
|
||
As an alternative, an embedded Node.js can also be fully controlled through | ||
Node-API. This API supports both C and C++ through [node-addon-api][]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we do this, we should probably start adding converters between some Node-API values and V8/Node.js C++ API values ( The reason nobody has implemented a Node-API embedder API yet is that as an embedder, you often need fine-grained control over a lot of V8 options, and it’s not very practical to mirror the entire V8/Node.js embedder API to C (and that the Node.js C++ embedder API itself still has some rough edges that should probably be ironed out first). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need to use raw V8 primitives then you can't use Node-API - this applies to both Node.js -> C++ (binary node addons) and to C++ -> Node.js (libnode). Still this API covers lots of use cases - mostly C++ software that needs to accept JS plugins. It does not cover Electron which has very particular needs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Right, that’s why I’m bringing this up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should you be able to mix V8 primitives and Node-API is another topic. If you take the ABI compatibility out of Node-API, there remains little use for it. However There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe Cloudflare (service) Workers will need converters. Is this a Private API thing? |
||
|
||
An example can be found [in the Node.js source tree][napi_embedding.c]. | ||
|
||
```c | ||
napi_platform platform; | ||
napi_env env; | ||
const char *main_script = "console.log('hello world')"; | ||
|
||
if (napi_create_platform(0, NULL, NULL, &platform) != napi_ok) { | ||
fprintf(stderr, "Failed creating the platform\n"); | ||
return -1; | ||
} | ||
|
||
if (napi_create_environment(platform, NULL, main_script, | ||
(napi_stdio){NULL, NULL, NULL}, NAPI_VERSION, &env) != napi_ok) { | ||
fprintf(stderr, "Failed running JS\n"); | ||
return -1; | ||
} | ||
|
||
// Here you can interact with the environment through Node-API env | ||
|
||
if (napi_destroy_environment(env, NULL) != napi_ok) { | ||
return -1; | ||
} | ||
|
||
if (napi_destroy_platform(platform) != napi_ok) { | ||
fprintf(stderr, "Failed destroying the platform\n"); | ||
return -1; | ||
} | ||
``` | ||
|
||
[CLI options]: cli.md | ||
[`process.memoryUsage()`]: process.md#processmemoryusage | ||
[deprecation policy]: deprecations.md | ||
[embedtest.cc]: https://github.com/nodejs/node/blob/HEAD/test/embedding/embedtest.cc | ||
[napi_embedding.c]: https://github.com/nodejs/node/blob/HEAD/test/embedding/napi_embedding.c | ||
[node-addon-api]: https://github.com/nodejs/node-addon-api | ||
[src/node.h]: https://github.com/nodejs/node/blob/HEAD/src/node.h |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6581,6 +6581,145 @@ idempotent. | |
|
||
This API may only be called from the main thread. | ||
|
||
## Using embedded Node.js | ||
|
||
### `napi_create_platform` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
```c | ||
napi_status napi_create_platform(int argc, | ||
char** argv, | ||
napi_error_message_handler err_handler, | ||
napi_platform* result); | ||
``` | ||
|
||
* `[in] argc`: CLI argument count, pass 0 for autofilling. | ||
* `[in] argv`: CLI arguments, pass NULL for autofilling. | ||
* `[in] err_handler`: If different than NULL, will be called back with each | ||
error message. There can be multiple error messages but the API guarantees | ||
that no calls will be made after the `napi_create_platform` has returned. | ||
In practice, the implementation of graceful recovery is still in progress, | ||
and many errors will be fatal, resulting in an `abort()`. | ||
* `[out] result`: A `napi_platform` result. | ||
|
||
This function must be called once to initialize V8 and Node.js when using as a | ||
shared library. | ||
|
||
### `napi_destroy_platform` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
```c | ||
napi_status napi_destroy_platform(napi_platform platform); | ||
``` | ||
|
||
* `[in] platform`: platform handle. | ||
|
||
Destroy the Node.js / V8 processes. | ||
|
||
### `napi_create_environment` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
```c | ||
napi_status napi_create_environment(napi_platform platform, | ||
napi_error_message_handler err_handler, | ||
const char* main_script, | ||
int32_t api_version, | ||
napi_env* result); | ||
``` | ||
|
||
* `[in] platform`: platform handle. | ||
* `[in] err_handler`: If different than NULL, will be called back with each | ||
error message. There can be multiple error messages but the API guarantees | ||
that no calls will be made after the `napi_create_platform` has returned. | ||
In practice, the implementation of graceful recovery is still in progress, | ||
and many errors will be fatal, resulting in an `abort()`. | ||
* `[in] main_script`: If different than NULL, custom JavaScript to run in | ||
addition to the default bootstrap that creates an empty | ||
ready-to-use CJS/ES6 environment with `global.require()` and | ||
`global.import()` functions that resolve modules from the directory of | ||
the compiled binary. | ||
It can be used to redirect `process.stdin`/ `process.stdout` streams | ||
since Node.js might switch these file descriptors to non-blocking mode. | ||
* `[in] api_version`: Node-API version to conform to, pass `NAPI_VERSION` | ||
for the latest available. | ||
* `[out] result`: A `napi_env` result. | ||
|
||
Initialize a new environment. A single platform can hold multiple Node.js | ||
environments that will run in a separate V8 isolate each. If the returned | ||
value is `napi_ok` or `napi_pending_exception`, the environment must be | ||
destroyed with `napi_destroy_environment` to free all allocated memory. | ||
|
||
### `napi_run_environment` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
```c | ||
napi_status napi_run_environment(napi_env env); | ||
``` | ||
|
||
* `[in] env`: environment handle. | ||
|
||
Iterate the event loop of the environment. Executes all pending | ||
JavaScript callbacks. Cannot be called with JavaScript on the | ||
stack (ie in a JavaScript callback). | ||
|
||
### `napi_await_promise` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
```c | ||
napi_status napi_await_promise(napi_env env, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vmoroz Your proposed |
||
napi_value promise, | ||
napi_value *result); | ||
``` | ||
|
||
* `[in] env`: environment handle. | ||
* `[in] promise`: JS Promise. | ||
* `[out] result`: Will receive the value that the Promise resolved with. | ||
|
||
Iterate the event loop of the environment until the `promise` has been | ||
resolved. Returns `napi_pending_exception` on rejection. Cannot be called | ||
with JavaScript on the stack (ie in a JavaScript callback). | ||
|
||
### `napi_destroy_environment` | ||
|
||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
> Stability: 1 - Experimental | ||
|
||
```c | ||
napi_status napi_destroy_environment(napi_env env); | ||
``` | ||
|
||
* `[in] env`: environment handle. | ||
|
||
Destroy the Node.js environment / V8 isolate. | ||
|
||
## Miscellaneous utilities | ||
|
||
### `node_api_get_module_file_name` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
// This is the bootstrapping code used when creating a new environment | ||
// through Node-API | ||
|
||
// Set up globalThis.require and globalThis.import so that they can | ||
// be easily accessed from C/C++ | ||
|
||
/* global primordials */ | ||
|
||
const { globalThis } = primordials; | ||
const cjsLoader = require('internal/modules/cjs/loader'); | ||
const esmLoader = require('internal/process/esm_loader').esmLoader; | ||
|
||
globalThis.module = new cjsLoader.Module(); | ||
globalThis.require = require('module').createRequire(process.execPath); | ||
|
||
const parent_path = require('url').pathToFileURL(process.execPath); | ||
globalThis.import = (mod) => esmLoader.import(mod, parent_path, { __proto__: null }); | ||
globalThis.import.meta = { url: parent_path }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Could we please change
napi_*
tonode_api_*
for all newly introduced elements across the entire PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabrielschulhof this is the name of the unit test, most unit tests / benchmarks that test Node-API use the
napi
abbreviation