From 732c46c635c3106ad7f2d854914095983dd3fc4b Mon Sep 17 00:00:00 2001 From: David Alsh Date: Mon, 12 May 2025 12:52:49 +1000 Subject: [PATCH] node-api: minimal C node_embedding_api function Co-authored-by: vmoroz --- node.gyp | 2 ++ src/node_embedding_api.cc | 26 ++++++++++++++++++++++++++ src/node_embedding_api.h | 30 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/node_embedding_api.cc create mode 100644 src/node_embedding_api.h diff --git a/node.gyp b/node.gyp index 0496f75fea5637..d4b1c52930d78b 100644 --- a/node.gyp +++ b/node.gyp @@ -113,6 +113,8 @@ 'src/node_dir.cc', 'src/node_dotenv.cc', 'src/node_env_var.cc', + 'src/node_embedding_api.cc', + 'src/node_embedding_api.h', 'src/node_errors.cc', 'src/node_external_reference.cc', 'src/node_file.cc', diff --git a/src/node_embedding_api.cc b/src/node_embedding_api.cc new file mode 100644 index 00000000000000..4c50eeb9e21893 --- /dev/null +++ b/src/node_embedding_api.cc @@ -0,0 +1,26 @@ +// +// Description: C-based API for embedding Node.js. +// +// !!! WARNING !!! WARNING !!! WARNING !!! +// This is a new API and is subject to change. +// While it is C-based, it is not ABI safe yet. +// Consider all functions and data structures as experimental. +// !!! WARNING !!! WARNING !!! WARNING !!! +// +// This file contains the C-based API for embedding Node.js in a host +// application. The API is designed to be used by applications that want to +// embed Node.js as a shared library (.so or .dll) and can interop with +// C-based API. +// + + +#include "node_embedding_api.h" +#include "node.h" + +EXTERN_C_START + +int32_t NAPI_CDECL node_embedding_main(int32_t argc, char* argv[]) { + return node::Start(argc, argv); +} + +EXTERN_C_END diff --git a/src/node_embedding_api.h b/src/node_embedding_api.h new file mode 100644 index 00000000000000..d012a525b48735 --- /dev/null +++ b/src/node_embedding_api.h @@ -0,0 +1,30 @@ +// +// Description: C-based API for embedding Node.js. +// +// !!! WARNING !!! WARNING !!! WARNING !!! +// This is a new API and is subject to change. +// While it is C-based, it is not ABI safe yet. +// Consider all functions and data structures as experimental. +// !!! WARNING !!! WARNING !!! WARNING !!! +// +// This file contains the C-based API for embedding Node.js in a host +// application. The API is designed to be used by applications that want to +// embed Node.js as a shared library (.so or .dll) and can interop with +// C-based API. +// + +#ifndef SRC_NODE_EMBEDDING_API_H_ +#define SRC_NODE_EMBEDDING_API_H_ + +#include "node_api.h" + +#define NODE_EMBEDDING_VERSION 1 + +EXTERN_C_START + +// Runs Node.js main function. It is the same as running Node.js from CLI. +NAPI_EXTERN int32_t NAPI_CDECL node_embedding_main(int32_t argc, char* argv[]); + +EXTERN_C_END + +#endif // SRC_NODE_EMBEDDING_API_H_