Skip to content

Commit

Permalink
adapt to the new embedding API
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev committed Sep 5, 2023
1 parent 107e1f9 commit e11b837
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 65 deletions.
4 changes: 2 additions & 2 deletions doc/api/embedding.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ An example can be found [in the Node.js source tree][napi_embedding.c].
napi_env env;
const char *main_script = "console.log('hello world')";

if (napi_create_platform(0, NULL, 0, NULL, NULL, &platform) != napi_ok) {
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}, &env) != napi_ok) {
(napi_stdio){NULL, NULL, NULL}, NAPI_VERSION, &env) != napi_ok) {
fprintf(stderr, "Failed running JS\n");
return -1;
}
Expand Down
7 changes: 3 additions & 4 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6415,16 +6415,12 @@ added: REPLACEME
```c
napi_status napi_create_platform(int argc,
char** argv,
int exec_argc,
char** exec_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] exec_argc`: Node.js CLI options count.
* `[in] exec_argv`: Node.js CLI options.
* `[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.
Expand Down Expand Up @@ -6463,6 +6459,7 @@ added: REPLACEME
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);
```

Expand All @@ -6479,6 +6476,8 @@ napi_status napi_create_environment(napi_platform platform,
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
Expand Down
73 changes: 30 additions & 43 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,6 @@ inline v8impl::Persistent<v8::Value>* NodePersistentFromJsDeferred(
return reinterpret_cast<v8impl::Persistent<v8::Value>*>(local);
}

struct PlatformWrapper {
explicit PlatformWrapper(int argc,
char** argv,
int exec_argc,
char** exec_argv,
int32_t _api_version)
: args(argv, argv + argc),
exec_args(exec_argv, exec_argv + exec_argc),
api_version(_api_version) {}
std::unique_ptr<node::MultiIsolatePlatform> platform;
std::vector<std::string> args;
std::vector<std::string> exec_args;
int32_t api_version;
};

class EmbeddedEnvironment : public node::EmbeddedEnvironment {
public:
explicit EmbeddedEnvironment(
Expand Down Expand Up @@ -892,49 +877,48 @@ napi_status NAPI_CDECL napi_get_last_error_info(
}

napi_status NAPI_CDECL
napi_create_platform_version(int argc,
char** argv,
int exec_argc,
char** exec_argv,
napi_error_message_handler err_handler,
napi_platform* result,
int32_t api_version) {
napi_create_platform(int argc,
char** argv,
napi_error_message_handler err_handler,
napi_platform* result) {
argv = uv_setup_args(argc, argv);
std::vector<std::string> errors_vec;

v8impl::PlatformWrapper* platform = new v8impl::PlatformWrapper(
argc, argv, exec_argc, exec_argv, api_version);
if (platform->args.size() < 1) platform->args.push_back("libnode");
std::vector<std::string> args(argv, argv + argc);
if (args.size() < 1) args.push_back("libnode");

int exit_code = node::InitializeNodeWithArgs(
&platform->args, &platform->exec_args, &errors_vec);
std::unique_ptr<node::InitializationResult> node_platform =
node::InitializeOncePerProcess(
args,
{node::ProcessInitializationFlags::kNoInitializeV8,
node::ProcessInitializationFlags::kNoInitializeNodeV8Platform});

for (const std::string& error : errors_vec) {
for (const std::string& error : node_platform->errors()) {
if (err_handler != nullptr) {
err_handler(error.c_str());
} else {
fprintf(stderr, "%s\n", error.c_str());
}
}

if (exit_code != 0) {
if (node_platform->early_return() != 0) {
return napi_generic_failure;
}

auto thread_pool_size = node::per_process::cli_options->v8_thread_pool_size;
platform->platform = node::MultiIsolatePlatform::Create(thread_pool_size);
v8::V8::InitializePlatform(platform->platform.get());
std::unique_ptr<node::MultiIsolatePlatform> v8_platform =
node::MultiIsolatePlatform::Create(thread_pool_size);
v8::V8::InitializePlatform(v8_platform.get());
v8::V8::Initialize();
*result = reinterpret_cast<napi_platform>(platform);
reinterpret_cast<node::InitializationResultImpl*>(node_platform.get())
->platform_ = v8_platform.release();
*result = reinterpret_cast<napi_platform>(node_platform.release());
return napi_ok;
}

napi_status NAPI_CDECL napi_destroy_platform(napi_platform platform) {
auto wrapper = reinterpret_cast<v8impl::PlatformWrapper*>(platform);
auto wrapper = reinterpret_cast<node::InitializationResult*>(platform);
v8::V8::Dispose();
v8::V8::DisposePlatform();

// The node::CommonEnvironmentSetup::Create uniq_ptr is destroyed here
node::TearDownOncePerProcess();
delete wrapper->platform();
delete wrapper;
return napi_ok;
}
Expand All @@ -943,12 +927,13 @@ napi_status NAPI_CDECL
napi_create_environment(napi_platform platform,
napi_error_message_handler err_handler,
const char* main_script,
int32_t api_version,
napi_env* result) {
auto wrapper = reinterpret_cast<v8impl::PlatformWrapper*>(platform);
auto wrapper = reinterpret_cast<node::InitializationResult*>(platform);
std::vector<std::string> errors_vec;

auto setup = node::CommonEnvironmentSetup::Create(
wrapper->platform.get(), &errors_vec, wrapper->args, wrapper->exec_args);
wrapper->platform(), &errors_vec, wrapper->args(), wrapper->exec_args());

for (const std::string& error : errors_vec) {
if (err_handler != nullptr) {
Expand Down Expand Up @@ -980,9 +965,9 @@ napi_create_environment(napi_platform platform,
new v8impl::EmbeddedEnvironment(std::move(setup), main_resource);

std::string filename =
wrapper->args.size() > 1 ? wrapper->args[1] : "<internal>";
auto env__ = new node_napi_env__(
emb_env->setup()->context(), filename, wrapper->api_version);
wrapper->args().size() > 1 ? wrapper->args()[1] : "<internal>";
auto env__ =
new node_napi_env__(emb_env->setup()->context(), filename, api_version);
emb_env->setup()->env()->set_embedded(emb_env);
env__->node_env()->AddCleanupHook(
[](void* arg) { static_cast<napi_env>(arg)->Unref(); },
Expand Down Expand Up @@ -1032,6 +1017,8 @@ napi_status NAPI_CDECL napi_destroy_environment(napi_env env, int* exit_code) {
// and the v8::locker
delete emb_env;

cppgc::ShutdownProcess();

return napi_ok;
}

Expand Down
16 changes: 5 additions & 11 deletions src/node_api_embedding.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ EXTERN_C_START
typedef void (*napi_error_message_handler)(const char* msg);

NAPI_EXTERN napi_status NAPI_CDECL
napi_create_platform_version(int argc,
char** argv,
int exec_argc,
char** exec_argv,
napi_error_message_handler err_handler,
napi_platform* result,
int32_t api_version);
#define napi_create_platform( \
argc, argv, exec_args, exec_argv, err_handler, result) \
napi_create_platform_version( \
argc, argv, exec_args, exec_argv, err_handler, result, NAPI_VERSION)
napi_create_platform(int argc,
char** argv,
napi_error_message_handler err_handler,
napi_platform* result);

NAPI_EXTERN napi_status NAPI_CDECL
napi_destroy_platform(napi_platform platform);
Expand All @@ -36,6 +29,7 @@ NAPI_EXTERN napi_status NAPI_CDECL
napi_create_environment(napi_platform platform,
napi_error_message_handler err_handler,
const char* main_script,
int32_t api_version,
napi_env* result);

NAPI_EXTERN napi_status NAPI_CDECL napi_run_environment(napi_env env);
Expand Down
7 changes: 4 additions & 3 deletions test/embedding/napi_embedding.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const char* main_script =
int main(int argc, char** argv) {
napi_platform platform;

CHECK(napi_create_platform(argc, argv, 0, NULL, NULL, &platform),
CHECK(napi_create_platform(argc, argv, NULL, &platform),
"Failed creating the platform");

int exit_code = RunNodeInstance(platform);
Expand Down Expand Up @@ -240,8 +240,9 @@ int RunNodeInstance(napi_platform platform) {
napi_env env;
int exit_code;

CHECK(napi_create_environment(platform, NULL, main_script, &env),
"Failed running JS");
CHECK(
napi_create_environment(platform, NULL, main_script, NAPI_VERSION, &env),
"Failed running JS");

if (callMe(env) != 0) exit_code = -1;
if (waitMe(env) != 0) exit_code = -1;
Expand Down
4 changes: 2 additions & 2 deletions test/embedding/napi_modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ int main(int argc, char* argv[]) {
return -2;
}

CHECK(napi_create_platform(0, NULL, 0, NULL, NULL, &platform),
CHECK(napi_create_platform(0, NULL, NULL, &platform),
"Failed creating the platform");

napi_env env;
CHECK(napi_create_environment(platform, NULL, NULL, &env),
CHECK(napi_create_environment(platform, NULL, NULL, NAPI_VERSION, &env),
"Failed running JS");

napi_handle_scope scope;
Expand Down

0 comments on commit e11b837

Please sign in to comment.