Skip to content

Commit 7a566b7

Browse files
committed
move code to node_api_embedding.cc
1 parent ccd228b commit 7a566b7

File tree

5 files changed

+257
-240
lines changed

5 files changed

+257
-240
lines changed

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
'src/module_wrap.cc',
9595
'src/node.cc',
9696
'src/node_api.cc',
97+
'src/node_api_embedding.cc',
9798
'src/node_binding.cc',
9899
'src/node_blob.cc',
99100
'src/node_buffer.cc',

src/js_native_api_types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ typedef struct napi_handle_scope__* napi_handle_scope;
5757
typedef struct napi_escapable_handle_scope__* napi_escapable_handle_scope;
5858
typedef struct napi_callback_info__* napi_callback_info;
5959
typedef struct napi_deferred__* napi_deferred;
60-
typedef struct napi_platform__* napi_platform;
6160

6261
typedef enum {
6362
napi_default = 0,

src/js_native_api_v8.cc

Lines changed: 0 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include "env-inl.h"
66
#include "js_native_api.h"
77
#include "js_native_api_v8.h"
8-
#include "node_api_embedding.h"
9-
#include "node_api_internals.h"
10-
#include "simdutf.h"
118
#include "util-inl.h"
129

1310
#define CHECK_MAYBE_NOTHING(env, maybe, status) \
@@ -263,42 +260,6 @@ inline v8impl::Persistent<v8::Value>* NodePersistentFromJsDeferred(
263260
return reinterpret_cast<v8impl::Persistent<v8::Value>*>(local);
264261
}
265262

266-
class EmbeddedEnvironment : public node::EmbeddedEnvironment {
267-
public:
268-
explicit EmbeddedEnvironment(
269-
std::unique_ptr<node::CommonEnvironmentSetup>&& setup,
270-
const std::shared_ptr<node::StaticExternalTwoByteResource>& main_resource)
271-
: main_resource_(main_resource),
272-
setup_(std::move(setup)),
273-
locker_(setup_->isolate()),
274-
isolate_scope_(setup_->isolate()),
275-
handle_scope_(setup_->isolate()),
276-
context_scope_(setup_->context()),
277-
seal_scope_(nullptr) {}
278-
279-
inline node::CommonEnvironmentSetup* setup() { return setup_.get(); }
280-
inline void seal() {
281-
seal_scope_ =
282-
std::make_unique<node::DebugSealHandleScope>(setup_->isolate());
283-
}
284-
285-
private:
286-
// The pointer to the UTF-16 main script convertible to V8 UnionBytes resource
287-
// This must be constructed first and destroyed last because the isolate
288-
// references it
289-
std::shared_ptr<node::StaticExternalTwoByteResource> main_resource_;
290-
291-
std::unique_ptr<node::CommonEnvironmentSetup> setup_;
292-
v8::Locker locker_;
293-
v8::Isolate::Scope isolate_scope_;
294-
v8::HandleScope handle_scope_;
295-
v8::Context::Scope context_scope_;
296-
// As this handle scope will remain open for the lifetime
297-
// of the environment, we seal it to prevent it from
298-
// becoming everyone's favorite trash bin
299-
std::unique_ptr<node::DebugSealHandleScope> seal_scope_;
300-
};
301-
302263
class HandleScopeWrapper {
303264
public:
304265
explicit HandleScopeWrapper(v8::Isolate* isolate) : scope(isolate) {}
@@ -972,206 +933,6 @@ napi_status NAPI_CDECL napi_get_last_error_info(
972933
return napi_ok;
973934
}
974935

975-
napi_status NAPI_CDECL
976-
napi_create_platform(int argc,
977-
char** argv,
978-
napi_error_message_handler err_handler,
979-
napi_platform* result) {
980-
argv = uv_setup_args(argc, argv);
981-
std::vector<std::string> args(argv, argv + argc);
982-
if (args.size() < 1) args.push_back("libnode");
983-
984-
std::unique_ptr<node::InitializationResult> node_platform =
985-
node::InitializeOncePerProcess(
986-
args,
987-
{node::ProcessInitializationFlags::kNoInitializeV8,
988-
node::ProcessInitializationFlags::kNoInitializeNodeV8Platform});
989-
990-
for (const std::string& error : node_platform->errors()) {
991-
if (err_handler != nullptr) {
992-
err_handler(error.c_str());
993-
} else {
994-
fprintf(stderr, "%s\n", error.c_str());
995-
}
996-
}
997-
if (node_platform->early_return() != 0) {
998-
return napi_generic_failure;
999-
}
1000-
1001-
auto thread_pool_size = node::per_process::cli_options->v8_thread_pool_size;
1002-
std::unique_ptr<node::MultiIsolatePlatform> v8_platform =
1003-
node::MultiIsolatePlatform::Create(thread_pool_size);
1004-
v8::V8::InitializePlatform(v8_platform.get());
1005-
v8::V8::Initialize();
1006-
reinterpret_cast<node::InitializationResultImpl*>(node_platform.get())
1007-
->platform_ = v8_platform.release();
1008-
*result = reinterpret_cast<napi_platform>(node_platform.release());
1009-
return napi_ok;
1010-
}
1011-
1012-
napi_status NAPI_CDECL napi_destroy_platform(napi_platform platform) {
1013-
auto wrapper = reinterpret_cast<node::InitializationResult*>(platform);
1014-
v8::V8::Dispose();
1015-
v8::V8::DisposePlatform();
1016-
node::TearDownOncePerProcess();
1017-
delete wrapper->platform();
1018-
delete wrapper;
1019-
return napi_ok;
1020-
}
1021-
1022-
napi_status NAPI_CDECL
1023-
napi_create_environment(napi_platform platform,
1024-
napi_error_message_handler err_handler,
1025-
const char* main_script,
1026-
int32_t api_version,
1027-
napi_env* result) {
1028-
auto wrapper = reinterpret_cast<node::InitializationResult*>(platform);
1029-
std::vector<std::string> errors_vec;
1030-
1031-
auto setup = node::CommonEnvironmentSetup::Create(
1032-
wrapper->platform(), &errors_vec, wrapper->args(), wrapper->exec_args());
1033-
1034-
for (const std::string& error : errors_vec) {
1035-
if (err_handler != nullptr) {
1036-
err_handler(error.c_str());
1037-
} else {
1038-
fprintf(stderr, "%s\n", error.c_str());
1039-
}
1040-
}
1041-
if (setup == nullptr) {
1042-
return napi_generic_failure;
1043-
}
1044-
1045-
std::shared_ptr<node::StaticExternalTwoByteResource> main_resource = nullptr;
1046-
if (main_script != nullptr) {
1047-
// We convert the user-supplied main_script to a UTF-16 resource
1048-
// and we store its shared_ptr in the environment
1049-
size_t u8_length = strlen(main_script);
1050-
size_t expected_u16_length =
1051-
simdutf::utf16_length_from_utf8(main_script, u8_length);
1052-
auto out = std::make_shared<std::vector<uint16_t>>(expected_u16_length);
1053-
size_t u16_length = simdutf::convert_utf8_to_utf16(
1054-
main_script, u8_length, reinterpret_cast<char16_t*>(out->data()));
1055-
out->resize(u16_length);
1056-
main_resource = std::make_shared<node::StaticExternalTwoByteResource>(
1057-
out->data(), out->size(), out);
1058-
}
1059-
1060-
auto emb_env =
1061-
new v8impl::EmbeddedEnvironment(std::move(setup), main_resource);
1062-
1063-
std::string filename =
1064-
wrapper->args().size() > 1 ? wrapper->args()[1] : "<internal>";
1065-
auto env__ =
1066-
new node_napi_env__(emb_env->setup()->context(), filename, api_version);
1067-
emb_env->setup()->env()->set_embedded(emb_env);
1068-
env__->node_env()->AddCleanupHook(
1069-
[](void* arg) { static_cast<napi_env>(arg)->Unref(); },
1070-
static_cast<void*>(env__));
1071-
*result = env__;
1072-
1073-
auto env = emb_env->setup()->env();
1074-
1075-
auto ret = node::LoadEnvironment(
1076-
env,
1077-
[env, resource = main_resource.get()](
1078-
const node::StartExecutionCallbackInfo& info)
1079-
-> v8::MaybeLocal<v8::Value> {
1080-
node::Realm* realm = env->principal_realm();
1081-
auto ret = realm->ExecuteBootstrapper(
1082-
"internal/bootstrap/switches/is_embedded_env");
1083-
if (ret.IsEmpty()) return ret;
1084-
1085-
std::string name =
1086-
"embedder_main_napi_" + std::to_string(env->thread_id());
1087-
if (resource != nullptr) {
1088-
env->builtin_loader()->Add(name.c_str(), node::UnionBytes(resource));
1089-
return realm->ExecuteBootstrapper(name.c_str());
1090-
} else {
1091-
return v8::Undefined(env->isolate());
1092-
}
1093-
});
1094-
if (ret.IsEmpty()) return napi_pending_exception;
1095-
1096-
emb_env->seal();
1097-
1098-
return napi_ok;
1099-
}
1100-
1101-
napi_status NAPI_CDECL napi_destroy_environment(napi_env env, int* exit_code) {
1102-
CHECK_ARG(env, env);
1103-
node_napi_env node_env = reinterpret_cast<node_napi_env>(env);
1104-
1105-
int r = node::SpinEventLoop(node_env->node_env()).FromMaybe(1);
1106-
if (exit_code != nullptr) *exit_code = r;
1107-
node::Stop(node_env->node_env());
1108-
1109-
auto emb_env = reinterpret_cast<v8impl::EmbeddedEnvironment*>(
1110-
node_env->node_env()->get_embedded());
1111-
node_env->node_env()->set_embedded(nullptr);
1112-
// This deletes the uniq_ptr to node::CommonEnvironmentSetup
1113-
// and the v8::locker
1114-
delete emb_env;
1115-
1116-
cppgc::ShutdownProcess();
1117-
1118-
return napi_ok;
1119-
}
1120-
1121-
napi_status NAPI_CDECL napi_run_environment(napi_env env) {
1122-
CHECK_ARG(env, env);
1123-
node_napi_env node_env = reinterpret_cast<node_napi_env>(env);
1124-
1125-
if (node::SpinEventLoopWithoutCleanup(node_env->node_env()).IsNothing())
1126-
return napi_closing;
1127-
1128-
return napi_ok;
1129-
}
1130-
1131-
static void napi_promise_error_handler(
1132-
const v8::FunctionCallbackInfo<v8::Value>& info) {
1133-
return;
1134-
}
1135-
1136-
napi_status napi_await_promise(napi_env env,
1137-
napi_value promise,
1138-
napi_value* result) {
1139-
NAPI_PREAMBLE(env);
1140-
CHECK_ARG(env, result);
1141-
1142-
v8::EscapableHandleScope scope(env->isolate);
1143-
node_napi_env node_env = reinterpret_cast<node_napi_env>(env);
1144-
1145-
v8::Local<v8::Value> promise_value = v8impl::V8LocalValueFromJsValue(promise);
1146-
if (promise_value.IsEmpty() || !promise_value->IsPromise())
1147-
return napi_invalid_arg;
1148-
v8::Local<v8::Promise> promise_object = promise_value.As<v8::Promise>();
1149-
1150-
v8::Local<v8::Value> rejected = v8::Boolean::New(env->isolate, false);
1151-
v8::Local<v8::Function> err_handler =
1152-
v8::Function::New(env->context(), napi_promise_error_handler, rejected)
1153-
.ToLocalChecked();
1154-
1155-
if (promise_object->Catch(env->context(), err_handler).IsEmpty())
1156-
return napi_pending_exception;
1157-
1158-
if (node::SpinEventLoopWithoutCleanup(
1159-
node_env->node_env(),
1160-
[&promise_object]() {
1161-
return promise_object->State() ==
1162-
v8::Promise::PromiseState::kPending;
1163-
})
1164-
.IsNothing())
1165-
return napi_closing;
1166-
1167-
*result =
1168-
v8impl::JsValueFromV8LocalValue(scope.Escape(promise_object->Result()));
1169-
if (promise_object->State() == v8::Promise::PromiseState::kRejected)
1170-
return napi_pending_exception;
1171-
1172-
return napi_ok;
1173-
}
1174-
1175936
napi_status NAPI_CDECL napi_create_function(napi_env env,
1176937
const char* utf8name,
1177938
size_t length,

0 commit comments

Comments
 (0)