Skip to content

Commit

Permalink
fixup! worker: add experimental SynchronousWorker implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Oct 15, 2022
1 parent 47e2483 commit 609b29a
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "node_worker.h"
#include "async_wrap-inl.h"
#include "base_object.h"
#include "debug_utils-inl.h"
#include "histogram-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_buffer.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_buffer.h"
#include "node_options-inl.h"
#include "node_perf.h"
#include "node_snapshot_builder.h"
#include "util-inl.h"
#include "async_wrap-inl.h"
#include "v8-local-handle.h"
#include "v8-microtask-queue.h"
#include "v8-snapshot.h"
Expand Down Expand Up @@ -884,11 +884,8 @@ class SynchronousWorker final : public MemoryRetainer {
static bool HasInstance(Environment* env, v8::Local<v8::Value> value);
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
static void Initialize(
Environment* env,
v8::Local<v8::Object> target);
static void RegisterExternalReferences(
ExternalReferenceRegistry* registry);
static void Initialize(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

SynchronousWorker(Environment* env, v8::Local<v8::Object> obj);

Expand Down Expand Up @@ -974,15 +971,13 @@ Local<FunctionTemplate> SynchronousWorker::GetConstructorTemplate(
return tmpl;
}

void SynchronousWorker::Initialize(
Environment* env,
v8::Local<v8::Object> target) {
SetConstructorFunction(
env->context(),
target,
"SynchronousWorker",
GetConstructorTemplate(env),
SetConstructorFunctionFlag::NONE);
void SynchronousWorker::Initialize(Environment* env,
v8::Local<v8::Object> target) {
SetConstructorFunction(env->context(),
target,
"SynchronousWorker",
GetConstructorTemplate(env),
SetConstructorFunctionFlag::NONE);

NODE_DEFINE_CONSTANT(target, UV_RUN_DEFAULT);
NODE_DEFINE_CONSTANT(target, UV_RUN_ONCE);
Expand Down Expand Up @@ -1048,9 +1043,8 @@ void SynchronousWorker::New(const FunctionCallbackInfo<Value>& args) {
void SynchronousWorker::Start(const FunctionCallbackInfo<Value>& args) {
SynchronousWorker* self = Unwrap(args);
if (self == nullptr) return;
self->Start(
args[0]->BooleanValue(args.GetIsolate()),
args[1]->BooleanValue(args.GetIsolate()));
self->Start(args[0]->BooleanValue(args.GetIsolate()),
args[1]->BooleanValue(args.GetIsolate()));
}

void SynchronousWorker::Stop(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -1118,7 +1112,7 @@ MaybeLocal<Value> SynchronousWorker::RunInCallbackScope(Local<Function> fn) {
}
SynchronousWorkerScope worker_scope(this);
v8::Isolate* isolate = isolate_;
CallbackScope callback_scope(isolate, wrap_.Get(isolate_), { 1, 0 });
CallbackScope callback_scope(isolate, wrap_.Get(isolate_), {1, 0});
MaybeLocal<Value> ret = fn->Call(context(), Null(isolate), 0, nullptr);
if (signaled_stop_) {
isolate->CancelTerminateExecution();
Expand All @@ -1145,10 +1139,10 @@ void SynchronousWorker::Start(bool own_loop, bool own_microtaskqueue) {
}

MicrotaskQueue* microtask_queue =
own_microtaskqueue ?
(microtask_queue_ = v8::MicrotaskQueue::New(
isolate_, v8::MicrotasksPolicy::kExplicit)).get() :
outer_context_.Get(isolate_)->GetMicrotaskQueue();
own_microtaskqueue ? (microtask_queue_ = v8::MicrotaskQueue::New(
isolate_, v8::MicrotasksPolicy::kExplicit))
.get()
: outer_context_.Get(isolate_)->GetMicrotaskQueue();
uv_loop_t* loop = own_loop ? &loop_ : GetCurrentEventLoop(isolate_);

Local<Context> context = Context::New(
Expand All @@ -1174,20 +1168,18 @@ void SynchronousWorker::Start(bool own_loop, bool own_microtaskqueue) {
ThreadId thread_id = AllocateEnvironmentThreadId();
auto inspector_parent_handle = GetInspectorParentHandle(
outer_env, thread_id, "file:///synchronous-worker.js");
env_ = CreateEnvironment(
isolate_data_,
context,
{},
{},
static_cast<EnvironmentFlags::Flags>(
EnvironmentFlags::kTrackUnmanagedFds |
EnvironmentFlags::kNoRegisterESMLoader),
thread_id,
std::move(inspector_parent_handle));
env_ = CreateEnvironment(isolate_data_,
context,
{},
{},
static_cast<EnvironmentFlags::Flags>(
EnvironmentFlags::kTrackUnmanagedFds |
EnvironmentFlags::kNoRegisterESMLoader),
thread_id,
std::move(inspector_parent_handle));
assert(env_ != nullptr);
SetProcessExitHandler(env_, [this](Environment* env, int code) {
OnExit(code);
});
SetProcessExitHandler(env_,
[this](Environment* env, int code) { OnExit(code); });
}

void SynchronousWorker::OnExit(int code) {
Expand All @@ -1198,10 +1190,11 @@ void SynchronousWorker::OnExit(int code) {
Isolate::SafeForTerminationScope termination_scope(isolate_);
Local<Value> onexit_v;
if (!self->Get(outer_context, String::NewFromUtf8Literal(isolate_, "onexit"))
.ToLocal(&onexit_v) || !onexit_v->IsFunction()) {
.ToLocal(&onexit_v) ||
!onexit_v->IsFunction()) {
return;
}
Local<Value> args[] = { Integer::New(isolate_, code) };
Local<Value> args[] = {Integer::New(isolate_, code)};
USE(onexit_v.As<Function>()->Call(outer_context, self, 1, args));
SignalStop();
}
Expand Down Expand Up @@ -1266,10 +1259,7 @@ MaybeLocal<Value> SynchronousWorker::Load(Local<Function> callback) {
return worker_scope.EscapeMaybe(
LoadEnvironment(env_, [&](const StartExecutionCallbackInfo& info) {
Local<Value> argv[] = {
info.process_object,
info.native_require,
context()->Global()
};
info.process_object, info.native_require, context()->Global()};
return callback->Call(context(), Null(isolate_), 3, argv);
}));
}
Expand Down

0 comments on commit 609b29a

Please sign in to comment.