Skip to content

Commit 3256e39

Browse files
committed
node-api: minimal C node_embedding_api function
Co-authored-by: vmoroz <vmorozov@microsoft.com> Updated embedding.md to include docs for C embedder API linting
1 parent 7fd3688 commit 3256e39

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

doc/api/embedding.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,51 @@ int RunNodeInstance(MultiIsolatePlatform* platform,
167167
}
168168
```
169169

170+
# C embedder API
171+
172+
<!--introduced_in=REPLACEME-->
173+
174+
While Node.js provides an extensive C++ embedding API that can be used from C++
175+
applications, the C-based API is useful when Node.js is embedded as a shared
176+
libnode library into C++ or non-C++ applications.
177+
178+
## API design overview
179+
180+
One of the goals for the C based embedder API is to be ABI stable. It means that
181+
applications must be able to use newer libnode versions without recompilation.
182+
The following design principles are targeting to achieve that goal.
183+
184+
- Follow the best practices for the [node-api][] design and build on top of
185+
the [node-api][].
186+
187+
## API reference
188+
189+
#### Functions
190+
191+
##### `node_embedding_main`
192+
193+
<!-- YAML
194+
added: REPLACEME
195+
-->
196+
197+
> Stability: 1 - Experimental
198+
199+
Runs Node.js runtime instance.
200+
201+
```c
202+
int32_t NAPI_CDECL node_embedding_main(
203+
int32_t argc,
204+
char* argv[]);
205+
```
206+
207+
- `[in] argc`: Number of items in the `argv` array.
208+
- `[in] argv`: CLI arguments as an array of zero terminated strings.
209+
210+
Returns `int32_t` with instance exit code.
211+
170212
[CLI options]: cli.md
171213
[`process.memoryUsage()`]: process.md#processmemoryusage
172214
[deprecation policy]: deprecations.md
173215
[embedtest.cc]: https://github.com/nodejs/node/blob/HEAD/test/embedding/embedtest.cc
216+
[node-api]: n-api.md
174217
[src/node.h]: https://github.com/nodejs/node/blob/HEAD/src/node.h

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@
113113
'src/node_dir.cc',
114114
'src/node_dotenv.cc',
115115
'src/node_env_var.cc',
116+
'src/node_embedding_api.cc',
117+
'src/node_embedding_api.h',
116118
'src/node_errors.cc',
117119
'src/node_external_reference.cc',
118120
'src/node_file.cc',

src/node_embedding_api.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Description: C-based API for embedding Node.js.
3+
//
4+
// !!! WARNING !!! WARNING !!! WARNING !!!
5+
// This is a new API and is subject to change.
6+
// While it is C-based, it is not ABI safe yet.
7+
// Consider all functions and data structures as experimental.
8+
// !!! WARNING !!! WARNING !!! WARNING !!!
9+
//
10+
// This file contains the C-based API for embedding Node.js in a host
11+
// application. The API is designed to be used by applications that want to
12+
// embed Node.js as a shared library (.so or .dll) and can interop with
13+
// C-based API.
14+
//
15+
16+
#include "node_embedding_api.h"
17+
#include "node.h"
18+
19+
EXTERN_C_START
20+
21+
int32_t NAPI_CDECL node_embedding_main(int32_t argc, char* argv[]) {
22+
return node::Start(argc, argv);
23+
}
24+
25+
EXTERN_C_END

src/node_embedding_api.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Description: C-based API for embedding Node.js.
3+
//
4+
// !!! WARNING !!! WARNING !!! WARNING !!!
5+
// This is a new API and is subject to change.
6+
// While it is C-based, it is not ABI safe yet.
7+
// Consider all functions and data structures as experimental.
8+
// !!! WARNING !!! WARNING !!! WARNING !!!
9+
//
10+
// This file contains the C-based API for embedding Node.js in a host
11+
// application. The API is designed to be used by applications that want to
12+
// embed Node.js as a shared library (.so or .dll) and can interop with
13+
// C-based API.
14+
//
15+
16+
#ifndef SRC_NODE_EMBEDDING_API_H_
17+
#define SRC_NODE_EMBEDDING_API_H_
18+
19+
#include "node_api.h"
20+
21+
#define NODE_EMBEDDING_VERSION 1
22+
23+
EXTERN_C_START
24+
25+
// Runs Node.js main function. It is the same as running Node.js from CLI.
26+
NAPI_EXTERN int32_t NAPI_CDECL node_embedding_main(int32_t argc, char* argv[]);
27+
28+
EXTERN_C_END
29+
30+
#endif // SRC_NODE_EMBEDDING_API_H_

0 commit comments

Comments
 (0)