GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_worker.cc Lines: 310 315 98.4 %
Date: 2019-05-05 22:32:45 Branches: 123 174 70.7 %

Line Branch Exec Source
1
#include "node_worker.h"
2
#include "debug_utils.h"
3
#include "node_errors.h"
4
#include "node_buffer.h"
5
#include "node_options-inl.h"
6
#include "node_perf.h"
7
#include "util.h"
8
#include "async_wrap-inl.h"
9
10
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
11
#include "inspector/worker_inspector.h"  // ParentInspectorHandle
12
#endif
13
14
#include <memory>
15
#include <string>
16
#include <vector>
17
18
using node::options_parser::kDisallowedInEnvironment;
19
using v8::ArrayBuffer;
20
using v8::Boolean;
21
using v8::Context;
22
using v8::Function;
23
using v8::FunctionCallbackInfo;
24
using v8::FunctionTemplate;
25
using v8::HandleScope;
26
using v8::Integer;
27
using v8::Isolate;
28
using v8::Local;
29
using v8::Locker;
30
using v8::Number;
31
using v8::Object;
32
using v8::SealHandleScope;
33
using v8::String;
34
using v8::Value;
35
36
namespace node {
37
namespace worker {
38
39
namespace {
40
41
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
42
182
void StartWorkerInspector(
43
    Environment* child,
44
    std::unique_ptr<inspector::ParentInspectorHandle> parent_handle,
45
    const std::string& url) {
46
182
  child->inspector_agent()->SetParentHandle(std::move(parent_handle));
47
  child->inspector_agent()->Start(url,
48
364
                                  child->options()->debug_options(),
49
                                  child->inspector_host_port(),
50
546
                                  false);
51
182
}
52
53
182
void WaitForWorkerInspectorToStop(Environment* child) {
54
182
  child->inspector_agent()->WaitForDisconnect();
55
182
  child->inspector_agent()->Stop();
56
182
}
57
#endif
58
59
}  // anonymous namespace
60
61
186
Worker::Worker(Environment* env,
62
               Local<Object> wrap,
63
               const std::string& url,
64
               std::shared_ptr<PerIsolateOptions> per_isolate_opts,
65
               std::vector<std::string>&& exec_argv)
66
    : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER),
67
      url_(url),
68
      per_isolate_opts_(per_isolate_opts),
69
      exec_argv_(exec_argv),
70
186
      platform_(env->isolate_data()->platform()),
71
186
      profiler_idle_notifier_started_(env->profiler_idle_notifier_started()),
72
186
      thread_id_(Environment::AllocateThreadId()),
73
744
      env_vars_(env->env_vars()) {
74
186
  Debug(this, "Creating new worker instance with thread id %llu", thread_id_);
75
76
  // Set up everything that needs to be set up in the parent environment.
77
186
  parent_port_ = MessagePort::New(env, env->context());
78
186
  if (parent_port_ == nullptr) {
79
    // This can happen e.g. because execution is terminating.
80
186
    return;
81
  }
82
83
186
  child_port_data_ = std::make_unique<MessagePortData>(nullptr);
84
186
  MessagePort::Entangle(parent_port_, child_port_data_.get());
85
86
186
  object()->Set(env->context(),
87
                env->message_port_string(),
88
1116
                parent_port_->object()).Check();
89
90
186
  object()->Set(env->context(),
91
                env->thread_id_string(),
92
1116
                Number::New(env->isolate(), static_cast<double>(thread_id_)))
93
372
      .Check();
94
95
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
96
372
  inspector_parent_handle_ =
97
186
      env->inspector_agent()->GetParentHandle(thread_id_, url);
98
#endif
99
100
186
  argv_ = std::vector<std::string>{env->argv()[0]};
101
  // Mark this Worker object as weak until we actually start the thread.
102
186
  MakeWeak();
103
104
186
  Debug(this, "Preparation for worker %llu finished", thread_id_);
105
}
106
107
1568
bool Worker::is_stopped() const {
108
1568
  Mutex::ScopedLock lock(mutex_);
109
1568
  if (env_ != nullptr)
110
1060
    return env_->is_stopping();
111
508
  return stopped_;
112
}
113
114
// This class contains data that is only relevant to the child thread itself,
115
// and only while it is running.
116
// (Eventually, the Environment instance should probably also be moved here.)
117
class WorkerThreadData {
118
 public:
119
185
  explicit WorkerThreadData(Worker* w)
120
    : w_(w),
121
185
      array_buffer_allocator_(ArrayBufferAllocator::Create()) {
122
185
    CHECK_EQ(uv_loop_init(&loop_), 0);
123
124
185
    Isolate* isolate = NewIsolate(array_buffer_allocator_.get(), &loop_);
125
185
    CHECK_NOT_NULL(isolate);
126
127
    {
128
185
      Locker locker(isolate);
129
370
      Isolate::Scope isolate_scope(isolate);
130
185
      isolate->SetStackLimit(w_->stack_base_);
131
132
370
      HandleScope handle_scope(isolate);
133
      isolate_data_.reset(CreateIsolateData(isolate,
134
                                            &loop_,
135
                                            w_->platform_,
136
185
                                            array_buffer_allocator_.get()));
137
185
      CHECK(isolate_data_);
138
185
      if (w_->per_isolate_opts_)
139
193
        isolate_data_->set_options(std::move(w_->per_isolate_opts_));
140
    }
141
142
185
    Mutex::ScopedLock lock(w_->mutex_);
143
185
    w_->isolate_ = isolate;
144
185
  }
145
146
370
  ~WorkerThreadData() {
147
185
    Debug(w_, "Worker %llu dispose isolate", w_->thread_id_);
148
    Isolate* isolate;
149
    {
150
185
      Mutex::ScopedLock lock(w_->mutex_);
151
185
      isolate = w_->isolate_;
152
185
      w_->isolate_ = nullptr;
153
    }
154
155
185
    w_->platform_->CancelPendingDelayedTasks(isolate);
156
157
185
    bool platform_finished = false;
158
159
185
    isolate_data_.reset();
160
161
555
    w_->platform_->AddIsolateFinishedCallback(isolate, [](void* data) {
162
185
      *static_cast<bool*>(data) = true;
163
740
    }, &platform_finished);
164
185
    w_->platform_->UnregisterIsolate(isolate);
165
166
185
    isolate->Dispose();
167
168
    // Wait until the platform has cleaned up all relevant resources.
169
555
    while (!platform_finished)
170
185
      uv_run(&loop_, UV_RUN_ONCE);
171
172
185
    CheckedUvLoopClose(&loop_);
173
185
  }
174
175
 private:
176
  Worker* const w_;
177
  uv_loop_t loop_;
178
  std::unique_ptr<ArrayBufferAllocator> array_buffer_allocator_;
179
  DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data_;
180
181
  friend class Worker;
182
};
183
184
185
void Worker::Run() {
185
185
  std::string name = "WorkerThread ";
186
185
  name += std::to_string(thread_id_);
187

372
  TRACE_EVENT_METADATA1(
188
      "__metadata", "thread_name", "name",
189
      TRACE_STR_COPY(name.c_str()));
190
185
  CHECK_NOT_NULL(platform_);
191
192
185
  Debug(this, "Creating isolate for worker with id %llu", thread_id_);
193
194
359
  WorkerThreadData data(this);
195
196
185
  Debug(this, "Starting worker with id %llu", thread_id_);
197
  {
198
185
    Locker locker(isolate_);
199
359
    Isolate::Scope isolate_scope(isolate_);
200
359
    SealHandleScope outer_seal(isolate_);
201
185
    bool inspector_started = false;
202
203
359
    DeleteFnPtr<Environment, FreeEnvironment> env_;
204
185
    OnScopeLeave cleanup_env([&]() {
205
370
      if (!env_) return;
206
182
      env_->set_can_call_into_js(false);
207
      Isolate::DisallowJavascriptExecutionScope disallow_js(isolate_,
208
182
          Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
209
210
      // Grab the parent-to-child channel and render is unusable.
211
      MessagePort* child_port;
212
      {
213
182
        Mutex::ScopedLock lock(mutex_);
214
182
        child_port = child_port_;
215
182
        child_port_ = nullptr;
216
      }
217
218
      {
219
182
        Context::Scope context_scope(env_->context());
220
182
        if (child_port != nullptr)
221
348
          child_port->Close();
222
        {
223
182
          Mutex::ScopedLock lock(mutex_);
224
182
          stopped_ = true;
225
182
          this->env_ = nullptr;
226
        }
227
182
        env_->thread_stopper()->set_stopped(true);
228
182
        env_->stop_sub_worker_contexts();
229
182
        env_->RunCleanup();
230
182
        RunAtExit(env_.get());
231
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
232
182
        if (inspector_started)
233
182
          WaitForWorkerInspectorToStop(env_.get());
234
#endif
235
236
        // This call needs to be made while the `Environment` is still alive
237
        // because we assume that it is available for async tracking in the
238
        // NodePlatform implementation.
239
182
        platform_->DrainTasks(isolate_);
240
      }
241
541
    });
242
243
185
    if (is_stopped()) return;
244
    {
245
185
      HandleScope handle_scope(isolate_);
246
185
      Local<Context> context = NewContext(isolate_);
247
248
185
      if (is_stopped()) return;
249
182
      CHECK(!context.IsEmpty());
250
174
      Context::Scope context_scope(context);
251
      {
252
        // TODO(addaleax): Use CreateEnvironment(), or generally another
253
        // public API.
254
        env_.reset(new Environment(data.isolate_data_.get(),
255
                                   context,
256
                                   Environment::kNoFlags,
257
182
                                   thread_id_));
258
182
        CHECK_NOT_NULL(env_);
259
182
        env_->set_env_vars(std::move(env_vars_));
260
182
        env_->set_abort_on_uncaught_exception(false);
261
182
        env_->set_worker_context(this);
262
263
182
        env_->InitializeLibuv(profiler_idle_notifier_started_);
264
182
        env_->ProcessCliArgs(std::move(argv_), std::move(exec_argv_));
265
      }
266
      {
267
182
        Mutex::ScopedLock lock(mutex_);
268
182
        if (stopped_) return;
269
182
        this->env_ = env_.get();
270
      }
271
182
      Debug(this, "Created Environment for worker with id %llu", thread_id_);
272
182
      if (is_stopped()) return;
273
      {
274
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
275
        StartWorkerInspector(env_.get(),
276
182
                             std::move(inspector_parent_handle_),
277
364
                             url_);
278
#endif
279
182
        inspector_started = true;
280
281
182
        HandleScope handle_scope(isolate_);
282
364
        AsyncCallbackScope callback_scope(env_.get());
283
182
        env_->async_hooks()->push_async_ids(1, 0);
284
364
        if (!RunBootstrapping(env_.get()).IsEmpty()) {
285
174
          CreateEnvMessagePort(env_.get());
286
174
          if (is_stopped()) return;
287
174
          Debug(this, "Created message port for worker %llu", thread_id_);
288
174
          USE(StartExecution(env_.get(), "internal/main/worker_thread"));
289
        }
290
291
182
        env_->async_hooks()->pop_async_id(1);
292
293
364
        Debug(this, "Loaded environment for worker %llu", thread_id_);
294
      }
295
296
182
      if (is_stopped()) return;
297
      {
298
174
        SealHandleScope seal(isolate_);
299
        bool more;
300
        env_->performance_state()->Mark(
301
174
            node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
302
120
        do {
303
174
          if (is_stopped()) break;
304
174
          uv_run(&data.loop_, UV_RUN_DEFAULT);
305
174
          if (is_stopped()) break;
306
307
120
          platform_->DrainTasks(isolate_);
308
309
120
          more = uv_loop_alive(&data.loop_);
310

120
          if (more && !is_stopped()) continue;
311
312
120
          EmitBeforeExit(env_.get());
313
314
          // Emit `beforeExit` if the loop became alive either after emitting
315
          // event, or after running some callbacks.
316
120
          more = uv_loop_alive(&data.loop_);
317

120
        } while (more == true && !is_stopped());
318
        env_->performance_state()->Mark(
319
174
            node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
320
174
      }
321
    }
322
323
    {
324
      int exit_code;
325
174
      bool stopped = is_stopped();
326
174
      if (!stopped)
327
120
        exit_code = EmitExit(env_.get());
328
174
      Mutex::ScopedLock lock(mutex_);
329

174
      if (exit_code_ == 0 && !stopped)
330
120
        exit_code_ = exit_code;
331
332
#if HAVE_INSPECTOR
333
174
      profiler::EndStartedProfilers(env_.get());
334
#endif
335
      Debug(this, "Exiting thread for worker %llu with exit code %d",
336
348
            thread_id_, exit_code_);
337
174
    }
338
  }
339
340
348
  Debug(this, "Worker %llu thread stops", thread_id_);
341
}
342
343
174
void Worker::CreateEnvMessagePort(Environment* env) {
344
174
  HandleScope handle_scope(isolate_);
345
348
  Mutex::ScopedLock lock(mutex_);
346
  // Set up the message channel for receiving messages in the child.
347
  child_port_ = MessagePort::New(env,
348
                                 env->context(),
349
174
                                 std::move(child_port_data_));
350
  // MessagePort::New() may return nullptr if execution is terminated
351
  // within it.
352
174
  if (child_port_ != nullptr)
353
348
    env->set_message_port(child_port_->object(isolate_));
354
174
}
355
356
185
void Worker::JoinThread() {
357
185
  if (thread_joined_)
358
181
    return;
359
185
  CHECK_EQ(uv_thread_join(&tid_), 0);
360
185
  thread_joined_ = true;
361
362
185
  env()->remove_sub_worker_context(this);
363
185
  OnThreadStopped();
364
181
  on_thread_finished_.Uninstall();
365
}
366
367
185
void Worker::OnThreadStopped() {
368
  {
369
185
    HandleScope handle_scope(env()->isolate());
370
185
    Context::Scope context_scope(env()->context());
371
372
    // Reset the parent port as we're closing it now anyway.
373
185
    object()->Set(env()->context(),
374
                  env()->message_port_string(),
375
1110
                  Undefined(env()->isolate())).Check();
376
377
185
    Local<Value> code = Integer::New(env()->isolate(), exit_code_);
378
366
    MakeCallback(env()->onexit_string(), 1, &code);
379
  }
380
381
  // JoinThread() cleared all libuv handles bound to this Worker,
382
  // the C++ object is no longer needed for anything now.
383
181
  MakeWeak();
384
181
}
385
386
486
Worker::~Worker() {
387
162
  Mutex::ScopedLock lock(mutex_);
388
389
162
  CHECK(stopped_);
390
162
  CHECK_NULL(env_);
391
162
  CHECK(thread_joined_);
392
393
324
  Debug(this, "Worker %llu destroyed", thread_id_);
394
324
}
395
396
189
void Worker::New(const FunctionCallbackInfo<Value>& args) {
397
189
  Environment* env = Environment::GetCurrent(args);
398
399
189
  CHECK(args.IsConstructCall());
400
401
189
  if (env->isolate_data()->platform() == nullptr) {
402
    THROW_ERR_MISSING_PLATFORM_FOR_WORKER(env);
403
    return;
404
  }
405
406
189
  std::string url;
407
375
  std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr;
408
409
375
  std::vector<std::string> exec_argv_out;
410
189
  bool has_explicit_exec_argv = false;
411
412
189
  CHECK_EQ(args.Length(), 2);
413
  // Argument might be a string or URL
414
567
  if (!args[0]->IsNullOrUndefined()) {
415
    Utf8Value value(
416
        args.GetIsolate(),
417
264
        args[0]->ToString(env->context()).FromMaybe(v8::Local<v8::String>()));
418
66
    url.append(value.out(), value.length());
419
  }
420
421
378
  if (args[1]->IsArray()) {
422
22
    v8::Local<v8::Array> array = args[1].As<v8::Array>();
423
    // The first argument is reserved for program name, but we don't need it
424
    // in workers.
425
11
    has_explicit_exec_argv = true;
426
11
    std::vector<std::string> exec_argv = {""};
427
11
    uint32_t length = array->Length();
428
50
    for (uint32_t i = 0; i < length; i++) {
429
      v8::Local<v8::Value> arg;
430
42
      if (!array->Get(env->context(), i).ToLocal(&arg)) {
431
        return;
432
      }
433
      v8::MaybeLocal<v8::String> arg_v8_string =
434
28
          arg->ToString(env->context());
435
14
      if (arg_v8_string.IsEmpty()) {
436
        return;
437
      }
438
      Utf8Value arg_utf8_value(
439
          args.GetIsolate(),
440
14
          arg_v8_string.FromMaybe(v8::Local<v8::String>()));
441
28
      std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
442
14
      exec_argv.push_back(arg_string);
443
14
    }
444
445
19
    std::vector<std::string> invalid_args{};
446
19
    std::vector<std::string> errors{};
447
11
    per_isolate_opts.reset(new PerIsolateOptions());
448
449
    // Using invalid_args as the v8_args argument as it stores unknown
450
    // options for the per isolate parser.
451
    options_parser::Parse(
452
        &exec_argv,
453
        &exec_argv_out,
454
        &invalid_args,
455
        per_isolate_opts.get(),
456
        kDisallowedInEnvironment,
457
11
        &errors);
458
459
    // The first argument is program name.
460
11
    invalid_args.erase(invalid_args.begin());
461

11
    if (errors.size() > 0 || invalid_args.size() > 0) {
462
      v8::Local<v8::Value> error;
463
6
      if (!ToV8Value(env->context(),
464
3
                     errors.size() > 0 ? errors : invalid_args)
465
9
                         .ToLocal(&error)) {
466
        return;
467
      }
468
      Local<String> key =
469
3
          FIXED_ONE_BYTE_STRING(env->isolate(), "invalidExecArgv");
470
      // Ignore the return value of Set() because exceptions bubble up to JS
471
      // when we return anyway.
472
9
      USE(args.This()->Set(env->context(), key, error));
473
3
      return;
474
8
    }
475
  }
476
186
  if (!has_explicit_exec_argv)
477
178
    exec_argv_out = env->exec_argv();
478
372
  new Worker(env, args.This(), url, per_isolate_opts, std::move(exec_argv_out));
479
}
480
481
184
void Worker::CloneParentEnvVars(const FunctionCallbackInfo<Value>& args) {
482
  Worker* w;
483
368
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
484
184
  CHECK(w->thread_joined_);  // The Worker has not started yet.
485
486
368
  w->env_vars_ = w->env()->env_vars()->Clone(args.GetIsolate());
487
}
488
489
1
void Worker::SetEnvVars(const FunctionCallbackInfo<Value>& args) {
490
  Worker* w;
491
2
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
492
1
  CHECK(w->thread_joined_);  // The Worker has not started yet.
493
494
2
  CHECK(args[0]->IsObject());
495
1
  w->env_vars_ = KVStore::CreateMapKVStore();
496
1
  w->env_vars_->AssignFromObject(args.GetIsolate()->GetCurrentContext(),
497
3
                                args[0].As<Object>());
498
}
499
500
185
void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
501
  Worker* w;
502
370
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
503
185
  Mutex::ScopedLock lock(w->mutex_);
504
505
  // The object now owns the created thread and should not be garbage collected
506
  // until that finishes.
507
185
  w->ClearWeak();
508
509
185
  w->env()->add_sub_worker_context(w);
510
185
  w->stopped_ = false;
511
185
  w->thread_joined_ = false;
512
513
461
  w->on_thread_finished_.Install(w->env(), w, [](uv_async_t* handle) {
514
138
    Worker* w_ = static_cast<Worker*>(handle->data);
515
138
    CHECK(w_->is_stopped());
516
138
    w_->parent_port_ = nullptr;
517
138
    w_->JoinThread();
518
135
    delete w_;
519
643
  });
520
521
  uv_thread_options_t thread_options;
522
185
  thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
523
185
  thread_options.stack_size = kStackSize;
524
740
  CHECK_EQ(uv_thread_create_ex(&w->tid_, &thread_options, [](void* arg) {
525
    Worker* w = static_cast<Worker*>(arg);
526
    const uintptr_t stack_top = reinterpret_cast<uintptr_t>(&arg);
527
528
    // Leave a few kilobytes just to make sure we're within limits and have
529
    // some space to do work in C++ land.
530
    w->stack_base_ = stack_top - (kStackSize - kStackBufferSize);
531
532
    w->Run();
533
534
    Mutex::ScopedLock lock(w->mutex_);
535
    w->on_thread_finished_.Stop();
536
185
  }, static_cast<void*>(w)), 0);
537
}
538
539
24
void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
540
  Worker* w;
541
48
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
542
543
24
  Debug(w, "Worker %llu is getting stopped by parent", w->thread_id_);
544
24
  w->Exit(1);
545
24
  w->JoinThread();
546
24
  delete w;
547
}
548
549
1
void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
550
  Worker* w;
551
2
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
552
1
  uv_ref(reinterpret_cast<uv_handle_t*>(w->on_thread_finished_.GetHandle()));
553
}
554
555
3
void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
556
  Worker* w;
557
6
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
558
3
  uv_unref(reinterpret_cast<uv_handle_t*>(w->on_thread_finished_.GetHandle()));
559
}
560
561
70
void Worker::Exit(int code) {
562
70
  Mutex::ScopedLock lock(mutex_);
563
70
  Debug(this, "Worker %llu called Exit(%d)", thread_id_, code);
564
70
  if (env_ != nullptr) {
565
63
    exit_code_ = code;
566
63
    Stop(env_);
567
  } else {
568
7
    stopped_ = true;
569
70
  }
570
70
}
571
572
namespace {
573
574
// Return the MessagePort that is global for this Environment and communicates
575
// with the internal [kPort] port of the JS Worker class in the parent thread.
576
347
void GetEnvMessagePort(const FunctionCallbackInfo<Value>& args) {
577
347
  Environment* env = Environment::GetCurrent(args);
578
347
  Local<Object> port = env->message_port();
579
347
  if (!port.IsEmpty()) {
580
1041
    CHECK_EQ(port->CreationContext()->GetIsolate(), args.GetIsolate());
581
694
    args.GetReturnValue().Set(port);
582
  }
583
347
}
584
585
3390
void InitWorker(Local<Object> target,
586
                Local<Value> unused,
587
                Local<Context> context,
588
                void* priv) {
589
3390
  Environment* env = Environment::GetCurrent(context);
590
591
  {
592
3390
    Local<FunctionTemplate> w = env->NewFunctionTemplate(Worker::New);
593
594
6780
    w->InstanceTemplate()->SetInternalFieldCount(1);
595
6780
    w->Inherit(AsyncWrap::GetConstructorTemplate(env));
596
597
3390
    env->SetProtoMethod(w, "setEnvVars", Worker::SetEnvVars);
598
3390
    env->SetProtoMethod(w, "cloneParentEnvVars", Worker::CloneParentEnvVars);
599
3390
    env->SetProtoMethod(w, "startThread", Worker::StartThread);
600
3390
    env->SetProtoMethod(w, "stopThread", Worker::StopThread);
601
3390
    env->SetProtoMethod(w, "ref", Worker::Ref);
602
3390
    env->SetProtoMethod(w, "unref", Worker::Unref);
603
604
    Local<String> workerString =
605
3390
        FIXED_ONE_BYTE_STRING(env->isolate(), "Worker");
606
3390
    w->SetClassName(workerString);
607
    target->Set(env->context(),
608
                workerString,
609
16950
                w->GetFunction(env->context()).ToLocalChecked()).Check();
610
  }
611
612
3390
  env->SetMethod(target, "getEnvMessagePort", GetEnvMessagePort);
613
614
  target
615
      ->Set(env->context(),
616
            env->thread_id_string(),
617
16950
            Number::New(env->isolate(), static_cast<double>(env->thread_id())))
618
6780
      .Check();
619
620
  target
621
      ->Set(env->context(),
622
            FIXED_ONE_BYTE_STRING(env->isolate(), "isMainThread"),
623
16950
            Boolean::New(env->isolate(), env->is_main_thread()))
624
6780
      .Check();
625
626
  target
627
      ->Set(env->context(),
628
            FIXED_ONE_BYTE_STRING(env->isolate(), "ownsProcessState"),
629
16950
            Boolean::New(env->isolate(), env->owns_process_state()))
630
6780
      .Check();
631
3390
}
632
633
}  // anonymous namespace
634
635
}  // namespace worker
636
}  // namespace node
637
638
4524
NODE_MODULE_CONTEXT_AWARE_INTERNAL(worker, node::worker::InitWorker)