From ccc822c7c8f4a205323231b000b7920d8a04cb26 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 5 Oct 2020 16:16:59 -0700 Subject: [PATCH] src: expose v8::Isolate setup callbacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/35512 Reviewed-By: Gus Caplan Reviewed-By: Juan José Arboleda Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- src/api/environment.cc | 14 +++++++------- src/node.h | 10 ++++++++++ src/node_internals.h | 4 ---- src/node_task_queue.cc | 41 ++++++++++++++++++++--------------------- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index 65ee6020933a27..fa994bc41ec9bc 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -31,14 +31,14 @@ using v8::SealHandleScope; using v8::String; using v8::Value; -static bool AllowWasmCodeGenerationCallback(Local context, - Local) { +bool AllowWasmCodeGenerationCallback(Local context, + Local) { Local wasm_code_gen = context->GetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration); return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue(); } -static bool ShouldAbortOnUncaughtException(Isolate* isolate) { +bool ShouldAbortOnUncaughtException(Isolate* isolate) { DebugSealHandleScope scope(isolate); Environment* env = Environment::GetCurrent(isolate); return env != nullptr && @@ -48,9 +48,9 @@ static bool ShouldAbortOnUncaughtException(Isolate* isolate) { !env->inside_should_not_abort_on_uncaught_scope(); } -static MaybeLocal PrepareStackTraceCallback(Local context, - Local exception, - Local trace) { +MaybeLocal PrepareStackTraceCallback(Local context, + Local exception, + Local trace) { Environment* env = Environment::GetCurrent(context); if (env == nullptr) { return exception->ToString(context).FromMaybe(Local()); @@ -245,7 +245,7 @@ void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) { if ((s.flags & SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK) == 0) { auto* promise_reject_cb = s.promise_reject_callback ? - s.promise_reject_callback : task_queue::PromiseRejectCallback; + s.promise_reject_callback : PromiseRejectCallback; isolate->SetPromiseRejectCallback(promise_reject_cb); } diff --git a/src/node.h b/src/node.h index 6cd383cb790641..8b018bac69bdab 100644 --- a/src/node.h +++ b/src/node.h @@ -495,6 +495,16 @@ NODE_EXTERN void DefaultProcessExitHandler(Environment* env, int exit_code); // This may return nullptr if context is not associated with a Node instance. NODE_EXTERN Environment* GetCurrentEnvironment(v8::Local context); +NODE_EXTERN void OnFatalError(const char* location, const char* message); +NODE_EXTERN void PromiseRejectCallback(v8::PromiseRejectMessage message); +NODE_EXTERN bool AllowWasmCodeGenerationCallback(v8::Local context, + v8::Local); +NODE_EXTERN bool ShouldAbortOnUncaughtException(v8::Isolate* isolate); +NODE_EXTERN v8::MaybeLocal PrepareStackTraceCallback( + v8::Local context, + v8::Local exception, + v8::Local trace); + // This returns the MultiIsolatePlatform used in the main thread of Node.js. // If NODE_USE_V8_PLATFORM has not been defined when Node.js was built, // it returns nullptr. diff --git a/src/node_internals.h b/src/node_internals.h index c8952e59a2b071..aa7180e18544ca 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -100,10 +100,6 @@ std::string GetHumanReadableProcessName(); void InitializeContextRuntime(v8::Local); bool InitializePrimordials(v8::Local context); -namespace task_queue { -void PromiseRejectCallback(v8::PromiseRejectMessage message); -} // namespace task_queue - class NodeArrayBufferAllocator : public ArrayBufferAllocator { public: inline uint32_t* zero_fill_field() { return &zero_fill_field_; } diff --git a/src/node_task_queue.cc b/src/node_task_queue.cc index 0f29fe0a59f0fc..926b27fcf2a05a 100644 --- a/src/node_task_queue.cc +++ b/src/node_task_queue.cc @@ -29,27 +29,6 @@ using v8::PromiseRejectEvent; using v8::PromiseRejectMessage; using v8::Value; -namespace task_queue { - -static void EnqueueMicrotask(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - Isolate* isolate = env->isolate(); - - CHECK(args[0]->IsFunction()); - - isolate->EnqueueMicrotask(args[0].As()); -} - -static void RunMicrotasks(const FunctionCallbackInfo& args) { - MicrotasksScope::PerformCheckpoint(args.GetIsolate()); -} - -static void SetTickCallback(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - CHECK(args[0]->IsFunction()); - env->set_tick_callback_function(args[0].As()); -} - void PromiseRejectCallback(PromiseRejectMessage message) { static std::atomic unhandledRejections{0}; static std::atomic rejectionsHandledAfter{0}; @@ -109,6 +88,26 @@ void PromiseRejectCallback(PromiseRejectMessage message) { PrintCaughtException(isolate, env->context(), try_catch); } } +namespace task_queue { + +static void EnqueueMicrotask(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + Isolate* isolate = env->isolate(); + + CHECK(args[0]->IsFunction()); + + isolate->EnqueueMicrotask(args[0].As()); +} + +static void RunMicrotasks(const FunctionCallbackInfo& args) { + MicrotasksScope::PerformCheckpoint(args.GetIsolate()); +} + +static void SetTickCallback(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + CHECK(args[0]->IsFunction()); + env->set_tick_callback_function(args[0].As()); +} static void SetPromiseRejectCallback( const FunctionCallbackInfo& args) {