|
5 | 5 | #include "env-inl.h"
|
6 | 6 | #include "js_native_api.h"
|
7 | 7 | #include "js_native_api_v8.h"
|
8 |
| -#include "node_api_embedding.h" |
9 |
| -#include "node_api_internals.h" |
10 |
| -#include "simdutf.h" |
11 | 8 | #include "util-inl.h"
|
12 | 9 |
|
13 | 10 | #define CHECK_MAYBE_NOTHING(env, maybe, status) \
|
@@ -263,42 +260,6 @@ inline v8impl::Persistent<v8::Value>* NodePersistentFromJsDeferred(
|
263 | 260 | return reinterpret_cast<v8impl::Persistent<v8::Value>*>(local);
|
264 | 261 | }
|
265 | 262 |
|
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 |
| - |
302 | 263 | class HandleScopeWrapper {
|
303 | 264 | public:
|
304 | 265 | explicit HandleScopeWrapper(v8::Isolate* isolate) : scope(isolate) {}
|
@@ -972,206 +933,6 @@ napi_status NAPI_CDECL napi_get_last_error_info(
|
972 | 933 | return napi_ok;
|
973 | 934 | }
|
974 | 935 |
|
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 |
| - |
1175 | 936 | napi_status NAPI_CDECL napi_create_function(napi_env env,
|
1176 | 937 | const char* utf8name,
|
1177 | 938 | size_t length,
|
|
0 commit comments