|
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) \
|
@@ -240,42 +237,6 @@ inline v8impl::Persistent<v8::Value>* NodePersistentFromJsDeferred(
|
240 | 237 | return reinterpret_cast<v8impl::Persistent<v8::Value>*>(local);
|
241 | 238 | }
|
242 | 239 |
|
243 |
| -class EmbeddedEnvironment : public node::EmbeddedEnvironment { |
244 |
| - public: |
245 |
| - explicit EmbeddedEnvironment( |
246 |
| - std::unique_ptr<node::CommonEnvironmentSetup>&& setup, |
247 |
| - const std::shared_ptr<node::StaticExternalTwoByteResource>& main_resource) |
248 |
| - : main_resource_(main_resource), |
249 |
| - setup_(std::move(setup)), |
250 |
| - locker_(setup_->isolate()), |
251 |
| - isolate_scope_(setup_->isolate()), |
252 |
| - handle_scope_(setup_->isolate()), |
253 |
| - context_scope_(setup_->context()), |
254 |
| - seal_scope_(nullptr) {} |
255 |
| - |
256 |
| - inline node::CommonEnvironmentSetup* setup() { return setup_.get(); } |
257 |
| - inline void seal() { |
258 |
| - seal_scope_ = |
259 |
| - std::make_unique<node::DebugSealHandleScope>(setup_->isolate()); |
260 |
| - } |
261 |
| - |
262 |
| - private: |
263 |
| - // The pointer to the UTF-16 main script convertible to V8 UnionBytes resource |
264 |
| - // This must be constructed first and destroyed last because the isolate |
265 |
| - // references it |
266 |
| - std::shared_ptr<node::StaticExternalTwoByteResource> main_resource_; |
267 |
| - |
268 |
| - std::unique_ptr<node::CommonEnvironmentSetup> setup_; |
269 |
| - v8::Locker locker_; |
270 |
| - v8::Isolate::Scope isolate_scope_; |
271 |
| - v8::HandleScope handle_scope_; |
272 |
| - v8::Context::Scope context_scope_; |
273 |
| - // As this handle scope will remain open for the lifetime |
274 |
| - // of the environment, we seal it to prevent it from |
275 |
| - // becoming everyone's favorite trash bin |
276 |
| - std::unique_ptr<node::DebugSealHandleScope> seal_scope_; |
277 |
| -}; |
278 |
| - |
279 | 240 | class HandleScopeWrapper {
|
280 | 241 | public:
|
281 | 242 | explicit HandleScopeWrapper(v8::Isolate* isolate) : scope(isolate) {}
|
@@ -876,206 +837,6 @@ napi_status NAPI_CDECL napi_get_last_error_info(
|
876 | 837 | return napi_ok;
|
877 | 838 | }
|
878 | 839 |
|
879 |
| -napi_status NAPI_CDECL |
880 |
| -napi_create_platform(int argc, |
881 |
| - char** argv, |
882 |
| - napi_error_message_handler err_handler, |
883 |
| - napi_platform* result) { |
884 |
| - argv = uv_setup_args(argc, argv); |
885 |
| - std::vector<std::string> args(argv, argv + argc); |
886 |
| - if (args.size() < 1) args.push_back("libnode"); |
887 |
| - |
888 |
| - std::unique_ptr<node::InitializationResult> node_platform = |
889 |
| - node::InitializeOncePerProcess( |
890 |
| - args, |
891 |
| - {node::ProcessInitializationFlags::kNoInitializeV8, |
892 |
| - node::ProcessInitializationFlags::kNoInitializeNodeV8Platform}); |
893 |
| - |
894 |
| - for (const std::string& error : node_platform->errors()) { |
895 |
| - if (err_handler != nullptr) { |
896 |
| - err_handler(error.c_str()); |
897 |
| - } else { |
898 |
| - fprintf(stderr, "%s\n", error.c_str()); |
899 |
| - } |
900 |
| - } |
901 |
| - if (node_platform->early_return() != 0) { |
902 |
| - return napi_generic_failure; |
903 |
| - } |
904 |
| - |
905 |
| - auto thread_pool_size = node::per_process::cli_options->v8_thread_pool_size; |
906 |
| - std::unique_ptr<node::MultiIsolatePlatform> v8_platform = |
907 |
| - node::MultiIsolatePlatform::Create(thread_pool_size); |
908 |
| - v8::V8::InitializePlatform(v8_platform.get()); |
909 |
| - v8::V8::Initialize(); |
910 |
| - reinterpret_cast<node::InitializationResultImpl*>(node_platform.get()) |
911 |
| - ->platform_ = v8_platform.release(); |
912 |
| - *result = reinterpret_cast<napi_platform>(node_platform.release()); |
913 |
| - return napi_ok; |
914 |
| -} |
915 |
| - |
916 |
| -napi_status NAPI_CDECL napi_destroy_platform(napi_platform platform) { |
917 |
| - auto wrapper = reinterpret_cast<node::InitializationResult*>(platform); |
918 |
| - v8::V8::Dispose(); |
919 |
| - v8::V8::DisposePlatform(); |
920 |
| - node::TearDownOncePerProcess(); |
921 |
| - delete wrapper->platform(); |
922 |
| - delete wrapper; |
923 |
| - return napi_ok; |
924 |
| -} |
925 |
| - |
926 |
| -napi_status NAPI_CDECL |
927 |
| -napi_create_environment(napi_platform platform, |
928 |
| - napi_error_message_handler err_handler, |
929 |
| - const char* main_script, |
930 |
| - int32_t api_version, |
931 |
| - napi_env* result) { |
932 |
| - auto wrapper = reinterpret_cast<node::InitializationResult*>(platform); |
933 |
| - std::vector<std::string> errors_vec; |
934 |
| - |
935 |
| - auto setup = node::CommonEnvironmentSetup::Create( |
936 |
| - wrapper->platform(), &errors_vec, wrapper->args(), wrapper->exec_args()); |
937 |
| - |
938 |
| - for (const std::string& error : errors_vec) { |
939 |
| - if (err_handler != nullptr) { |
940 |
| - err_handler(error.c_str()); |
941 |
| - } else { |
942 |
| - fprintf(stderr, "%s\n", error.c_str()); |
943 |
| - } |
944 |
| - } |
945 |
| - if (setup == nullptr) { |
946 |
| - return napi_generic_failure; |
947 |
| - } |
948 |
| - |
949 |
| - std::shared_ptr<node::StaticExternalTwoByteResource> main_resource = nullptr; |
950 |
| - if (main_script != nullptr) { |
951 |
| - // We convert the user-supplied main_script to a UTF-16 resource |
952 |
| - // and we store its shared_ptr in the environment |
953 |
| - size_t u8_length = strlen(main_script); |
954 |
| - size_t expected_u16_length = |
955 |
| - simdutf::utf16_length_from_utf8(main_script, u8_length); |
956 |
| - auto out = std::make_shared<std::vector<uint16_t>>(expected_u16_length); |
957 |
| - size_t u16_length = simdutf::convert_utf8_to_utf16( |
958 |
| - main_script, u8_length, reinterpret_cast<char16_t*>(out->data())); |
959 |
| - out->resize(u16_length); |
960 |
| - main_resource = std::make_shared<node::StaticExternalTwoByteResource>( |
961 |
| - out->data(), out->size(), out); |
962 |
| - } |
963 |
| - |
964 |
| - auto emb_env = |
965 |
| - new v8impl::EmbeddedEnvironment(std::move(setup), main_resource); |
966 |
| - |
967 |
| - std::string filename = |
968 |
| - wrapper->args().size() > 1 ? wrapper->args()[1] : "<internal>"; |
969 |
| - auto env__ = |
970 |
| - new node_napi_env__(emb_env->setup()->context(), filename, api_version); |
971 |
| - emb_env->setup()->env()->set_embedded(emb_env); |
972 |
| - env__->node_env()->AddCleanupHook( |
973 |
| - [](void* arg) { static_cast<napi_env>(arg)->Unref(); }, |
974 |
| - static_cast<void*>(env__)); |
975 |
| - *result = env__; |
976 |
| - |
977 |
| - auto env = emb_env->setup()->env(); |
978 |
| - |
979 |
| - auto ret = node::LoadEnvironment( |
980 |
| - env, |
981 |
| - [env, resource = main_resource.get()]( |
982 |
| - const node::StartExecutionCallbackInfo& info) |
983 |
| - -> v8::MaybeLocal<v8::Value> { |
984 |
| - node::Realm* realm = env->principal_realm(); |
985 |
| - auto ret = realm->ExecuteBootstrapper( |
986 |
| - "internal/bootstrap/switches/is_embedded_env"); |
987 |
| - if (ret.IsEmpty()) return ret; |
988 |
| - |
989 |
| - std::string name = |
990 |
| - "embedder_main_napi_" + std::to_string(env->thread_id()); |
991 |
| - if (resource != nullptr) { |
992 |
| - env->builtin_loader()->Add(name.c_str(), node::UnionBytes(resource)); |
993 |
| - return realm->ExecuteBootstrapper(name.c_str()); |
994 |
| - } else { |
995 |
| - return v8::Undefined(env->isolate()); |
996 |
| - } |
997 |
| - }); |
998 |
| - if (ret.IsEmpty()) return napi_pending_exception; |
999 |
| - |
1000 |
| - emb_env->seal(); |
1001 |
| - |
1002 |
| - return napi_ok; |
1003 |
| -} |
1004 |
| - |
1005 |
| -napi_status NAPI_CDECL napi_destroy_environment(napi_env env, int* exit_code) { |
1006 |
| - CHECK_ARG(env, env); |
1007 |
| - node_napi_env node_env = reinterpret_cast<node_napi_env>(env); |
1008 |
| - |
1009 |
| - int r = node::SpinEventLoop(node_env->node_env()).FromMaybe(1); |
1010 |
| - if (exit_code != nullptr) *exit_code = r; |
1011 |
| - node::Stop(node_env->node_env()); |
1012 |
| - |
1013 |
| - auto emb_env = reinterpret_cast<v8impl::EmbeddedEnvironment*>( |
1014 |
| - node_env->node_env()->get_embedded()); |
1015 |
| - node_env->node_env()->set_embedded(nullptr); |
1016 |
| - // This deletes the uniq_ptr to node::CommonEnvironmentSetup |
1017 |
| - // and the v8::locker |
1018 |
| - delete emb_env; |
1019 |
| - |
1020 |
| - cppgc::ShutdownProcess(); |
1021 |
| - |
1022 |
| - return napi_ok; |
1023 |
| -} |
1024 |
| - |
1025 |
| -napi_status NAPI_CDECL napi_run_environment(napi_env env) { |
1026 |
| - CHECK_ARG(env, env); |
1027 |
| - node_napi_env node_env = reinterpret_cast<node_napi_env>(env); |
1028 |
| - |
1029 |
| - if (node::SpinEventLoopWithoutCleanup(node_env->node_env()).IsNothing()) |
1030 |
| - return napi_closing; |
1031 |
| - |
1032 |
| - return napi_ok; |
1033 |
| -} |
1034 |
| - |
1035 |
| -static void napi_promise_error_handler( |
1036 |
| - const v8::FunctionCallbackInfo<v8::Value>& info) { |
1037 |
| - return; |
1038 |
| -} |
1039 |
| - |
1040 |
| -napi_status napi_await_promise(napi_env env, |
1041 |
| - napi_value promise, |
1042 |
| - napi_value* result) { |
1043 |
| - NAPI_PREAMBLE(env); |
1044 |
| - CHECK_ARG(env, result); |
1045 |
| - |
1046 |
| - v8::EscapableHandleScope scope(env->isolate); |
1047 |
| - node_napi_env node_env = reinterpret_cast<node_napi_env>(env); |
1048 |
| - |
1049 |
| - v8::Local<v8::Value> promise_value = v8impl::V8LocalValueFromJsValue(promise); |
1050 |
| - if (promise_value.IsEmpty() || !promise_value->IsPromise()) |
1051 |
| - return napi_invalid_arg; |
1052 |
| - v8::Local<v8::Promise> promise_object = promise_value.As<v8::Promise>(); |
1053 |
| - |
1054 |
| - v8::Local<v8::Value> rejected = v8::Boolean::New(env->isolate, false); |
1055 |
| - v8::Local<v8::Function> err_handler = |
1056 |
| - v8::Function::New(env->context(), napi_promise_error_handler, rejected) |
1057 |
| - .ToLocalChecked(); |
1058 |
| - |
1059 |
| - if (promise_object->Catch(env->context(), err_handler).IsEmpty()) |
1060 |
| - return napi_pending_exception; |
1061 |
| - |
1062 |
| - if (node::SpinEventLoopWithoutCleanup( |
1063 |
| - node_env->node_env(), |
1064 |
| - [&promise_object]() { |
1065 |
| - return promise_object->State() == |
1066 |
| - v8::Promise::PromiseState::kPending; |
1067 |
| - }) |
1068 |
| - .IsNothing()) |
1069 |
| - return napi_closing; |
1070 |
| - |
1071 |
| - *result = |
1072 |
| - v8impl::JsValueFromV8LocalValue(scope.Escape(promise_object->Result())); |
1073 |
| - if (promise_object->State() == v8::Promise::PromiseState::kRejected) |
1074 |
| - return napi_pending_exception; |
1075 |
| - |
1076 |
| - return napi_ok; |
1077 |
| -} |
1078 |
| - |
1079 | 840 | napi_status NAPI_CDECL napi_create_function(napi_env env,
|
1080 | 841 | const char* utf8name,
|
1081 | 842 | size_t length,
|
|
0 commit comments