GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_binding.cc Lines: 191 208 91.8 %
Date: 2020-02-27 22:14:15 Branches: 102 134 76.1 %

Line Branch Exec Source
1
#include "node_binding.h"
2
#include "node_errors.h"
3
#include <atomic>
4
#include "env-inl.h"
5
#include "node_native_module_env.h"
6
#include "util.h"
7
8
#if HAVE_OPENSSL
9
#define NODE_BUILTIN_OPENSSL_MODULES(V) V(crypto) V(tls_wrap)
10
#else
11
#define NODE_BUILTIN_OPENSSL_MODULES(V)
12
#endif
13
14
#if NODE_HAVE_I18N_SUPPORT
15
#define NODE_BUILTIN_ICU_MODULES(V) V(icu)
16
#else
17
#define NODE_BUILTIN_ICU_MODULES(V)
18
#endif
19
20
#if NODE_REPORT
21
#define NODE_BUILTIN_REPORT_MODULES(V) V(report)
22
#else
23
#define NODE_BUILTIN_REPORT_MODULES(V)
24
#endif
25
26
#if HAVE_INSPECTOR
27
#define NODE_BUILTIN_PROFILER_MODULES(V) V(profiler)
28
#else
29
#define NODE_BUILTIN_PROFILER_MODULES(V)
30
#endif
31
32
#if HAVE_DTRACE || HAVE_ETW
33
#define NODE_BUILTIN_DTRACE_MODULES(V) V(dtrace)
34
#else
35
#define NODE_BUILTIN_DTRACE_MODULES(V)
36
#endif
37
38
// A list of built-in modules. In order to do module registration
39
// in node::Init(), need to add built-in modules in the following list.
40
// Then in binding::RegisterBuiltinModules(), it calls modules' registration
41
// function. This helps the built-in modules are loaded properly when
42
// node is built as static library. No need to depend on the
43
// __attribute__((constructor)) like mechanism in GCC.
44
#define NODE_BUILTIN_STANDARD_MODULES(V)                                       \
45
  V(async_wrap)                                                                \
46
  V(buffer)                                                                    \
47
  V(cares_wrap)                                                                \
48
  V(config)                                                                    \
49
  V(contextify)                                                                \
50
  V(credentials)                                                               \
51
  V(domain)                                                                    \
52
  V(errors)                                                                    \
53
  V(fs)                                                                        \
54
  V(fs_dir)                                                                    \
55
  V(fs_event_wrap)                                                             \
56
  V(heap_utils)                                                                \
57
  V(http2)                                                                     \
58
  V(http_parser)                                                               \
59
  V(inspector)                                                                 \
60
  V(js_stream)                                                                 \
61
  V(messaging)                                                                 \
62
  V(module_wrap)                                                               \
63
  V(native_module)                                                             \
64
  V(options)                                                                   \
65
  V(os)                                                                        \
66
  V(performance)                                                               \
67
  V(pipe_wrap)                                                                 \
68
  V(process_wrap)                                                              \
69
  V(process_methods)                                                           \
70
  V(serdes)                                                                    \
71
  V(signal_wrap)                                                               \
72
  V(spawn_sync)                                                                \
73
  V(stream_pipe)                                                               \
74
  V(stream_wrap)                                                               \
75
  V(string_decoder)                                                            \
76
  V(symbols)                                                                   \
77
  V(task_queue)                                                                \
78
  V(tcp_wrap)                                                                  \
79
  V(timers)                                                                    \
80
  V(trace_events)                                                              \
81
  V(tty_wrap)                                                                  \
82
  V(types)                                                                     \
83
  V(udp_wrap)                                                                  \
84
  V(url)                                                                       \
85
  V(util)                                                                      \
86
  V(uv)                                                                        \
87
  V(v8)                                                                        \
88
  V(wasi)                                                                      \
89
  V(worker)                                                                    \
90
  V(watchdog)                                                                  \
91
  V(zlib)
92
93
#define NODE_BUILTIN_MODULES(V)                                                \
94
  NODE_BUILTIN_STANDARD_MODULES(V)                                             \
95
  NODE_BUILTIN_OPENSSL_MODULES(V)                                              \
96
  NODE_BUILTIN_ICU_MODULES(V)                                                  \
97
  NODE_BUILTIN_REPORT_MODULES(V)                                               \
98
  NODE_BUILTIN_PROFILER_MODULES(V)                                             \
99
  NODE_BUILTIN_DTRACE_MODULES(V)
100
101
// This is used to load built-in modules. Instead of using
102
// __attribute__((constructor)), we call the _register_<modname>
103
// function for each built-in modules explicitly in
104
// binding::RegisterBuiltinModules(). This is only forward declaration.
105
// The definitions are in each module's implementation when calling
106
// the NODE_MODULE_CONTEXT_AWARE_INTERNAL.
107
#define V(modname) void _register_##modname();
108
NODE_BUILTIN_MODULES(V)
109
#undef V
110
111
#ifdef _AIX
112
// On AIX, dlopen() behaves differently from other operating systems, in that
113
// it returns unique values from each call, rather than identical values, when
114
// loading the same handle.
115
// We try to work around that by providing wrappers for the dlopen() family of
116
// functions, and using st_dev and st_ino for the file that is to be loaded
117
// as keys for a cache.
118
119
namespace node {
120
namespace dlwrapper {
121
122
struct dl_wrap {
123
  uint64_t st_dev;
124
  uint64_t st_ino;
125
  uint64_t refcount;
126
  void* real_handle;
127
128
  struct hash {
129
    size_t operator()(const dl_wrap* wrap) const {
130
      return std::hash<uint64_t>()(wrap->st_dev) ^
131
             std::hash<uint64_t>()(wrap->st_ino);
132
    }
133
  };
134
135
  struct equal {
136
    bool operator()(const dl_wrap* a,
137
                    const dl_wrap* b) const {
138
      return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
139
    }
140
  };
141
};
142
143
static Mutex dlhandles_mutex;
144
static std::unordered_set<dl_wrap*, dl_wrap::hash, dl_wrap::equal>
145
    dlhandles;
146
static thread_local std::string dlerror_storage;
147
148
char* wrapped_dlerror() {
149
  return &dlerror_storage[0];
150
}
151
152
void* wrapped_dlopen(const char* filename, int flags) {
153
  CHECK_NOT_NULL(filename);  // This deviates from the 'real' dlopen().
154
  Mutex::ScopedLock lock(dlhandles_mutex);
155
156
  uv_fs_t req;
157
  auto cleanup = OnScopeLeave([&]() { uv_fs_req_cleanup(&req); });
158
  int rc = uv_fs_stat(nullptr, &req, filename, nullptr);
159
160
  if (rc != 0) {
161
    dlerror_storage = uv_strerror(rc);
162
    return nullptr;
163
  }
164
165
  dl_wrap search = {
166
    req.statbuf.st_dev,
167
    req.statbuf.st_ino,
168
    0, nullptr
169
  };
170
171
  auto it = dlhandles.find(&search);
172
  if (it != dlhandles.end()) {
173
    (*it)->refcount++;
174
    return *it;
175
  }
176
177
  void* real_handle = dlopen(filename, flags);
178
  if (real_handle == nullptr) {
179
    dlerror_storage = dlerror();
180
    return nullptr;
181
  }
182
  dl_wrap* wrap = new dl_wrap();
183
  wrap->st_dev = req.statbuf.st_dev;
184
  wrap->st_ino = req.statbuf.st_ino;
185
  wrap->refcount = 1;
186
  wrap->real_handle = real_handle;
187
  dlhandles.insert(wrap);
188
  return wrap;
189
}
190
191
int wrapped_dlclose(void* handle) {
192
  Mutex::ScopedLock lock(dlhandles_mutex);
193
  dl_wrap* wrap = static_cast<dl_wrap*>(handle);
194
  int ret = 0;
195
  CHECK_GE(wrap->refcount, 1);
196
  if (--wrap->refcount == 0) {
197
    ret = dlclose(wrap->real_handle);
198
    if (ret != 0) dlerror_storage = dlerror();
199
    dlhandles.erase(wrap);
200
    delete wrap;
201
  }
202
  return ret;
203
}
204
205
void* wrapped_dlsym(void* handle, const char* symbol) {
206
  if (handle == RTLD_DEFAULT || handle == RTLD_NEXT)
207
    return dlsym(handle, symbol);
208
  dl_wrap* wrap = static_cast<dl_wrap*>(handle);
209
  return dlsym(wrap->real_handle, symbol);
210
}
211
212
#define dlopen node::dlwrapper::wrapped_dlopen
213
#define dlerror node::dlwrapper::wrapped_dlerror
214
#define dlclose node::dlwrapper::wrapped_dlclose
215
#define dlsym node::dlwrapper::wrapped_dlsym
216
217
}  // namespace dlwrapper
218
}  // namespace node
219
220
#endif  // _AIX
221
222
#ifdef __linux__
223
16
static bool libc_may_be_musl() {
224

16
  static std::atomic_bool retval;  // Cache the return value.
225
  static std::atomic_bool has_cached_retval { false };
226
16
  if (has_cached_retval) return retval;
227
15
  retval = dlsym(RTLD_DEFAULT, "gnu_get_libc_version") == nullptr;
228
15
  has_cached_retval = true;
229
15
  return retval;
230
}
231
#else  // __linux__
232
static bool libc_may_be_musl() { return false; }
233
#endif  // __linux__
234
235
namespace node {
236
237
using v8::Context;
238
using v8::Exception;
239
using v8::FunctionCallbackInfo;
240
using v8::Local;
241
using v8::NewStringType;
242
using v8::Object;
243
using v8::String;
244
using v8::Value;
245
246
// Globals per process
247
static node_module* modlist_internal;
248
static node_module* modlist_linked;
249
static thread_local node_module* thread_local_modpending;
250
251
// This is set by node::Init() which is used by embedders
252
bool node_is_initialized = false;
253
254
218592
extern "C" void node_module_register(void* m) {
255
218592
  struct node_module* mp = reinterpret_cast<struct node_module*>(m);
256
257
218592
  if (mp->nm_flags & NM_F_INTERNAL) {
258
218452
    mp->nm_link = modlist_internal;
259
218452
    modlist_internal = mp;
260
140
  } else if (!node_is_initialized) {
261
    // "Linked" modules are included as part of the node project.
262
    // Like builtins they are registered *before* node::Init runs.
263
1
    mp->nm_flags = NM_F_LINKED;
264
1
    mp->nm_link = modlist_linked;
265
1
    modlist_linked = mp;
266
  } else {
267
139
    thread_local_modpending = mp;
268
  }
269
218592
}
270
271
namespace binding {
272
273
8404
static struct global_handle_map_t {
274
 public:
275
138
  void set(void* handle, node_module* mod) {
276
138
    CHECK_NE(handle, nullptr);
277
276
    Mutex::ScopedLock lock(mutex_);
278
279
138
    map_[handle].module = mod;
280
    // We need to store this flag internally to avoid a chicken-and-egg problem
281
    // during cleanup. By the time we actually use the flag's value,
282
    // the shared object has been unloaded, and its memory would be gone,
283
    // making it impossible to access fields of `mod` --
284
    // unless `mod` *is* dynamically allocated, but we cannot know that
285
    // without checking the flag.
286
138
    map_[handle].wants_delete_module = mod->nm_flags & NM_F_DELETEME;
287
138
    map_[handle].refcount++;
288
138
  }
289
290
6
  node_module* get_and_increase_refcount(void* handle) {
291
6
    CHECK_NE(handle, nullptr);
292
12
    Mutex::ScopedLock lock(mutex_);
293
294
6
    auto it = map_.find(handle);
295
6
    if (it == map_.end()) return nullptr;
296
5
    it->second.refcount++;
297
5
    return it->second.module;
298
  }
299
300
13
  void erase(void* handle) {
301
13
    CHECK_NE(handle, nullptr);
302
25
    Mutex::ScopedLock lock(mutex_);
303
304
13
    auto it = map_.find(handle);
305
13
    if (it == map_.end()) return;
306
12
    CHECK_GE(it->second.refcount, 1);
307
12
    if (--it->second.refcount == 0) {
308
7
      if (it->second.wants_delete_module)
309
2
        delete it->second.module;
310
7
      map_.erase(handle);
311
    }
312
  }
313
314
 private:
315
  Mutex mutex_;
316
  struct Entry {
317
    unsigned int refcount;
318
    bool wants_delete_module;
319
    node_module* module;
320
  };
321
  std::unordered_map<void*, Entry> map_;
322
4202
} global_handle_map;
323
324
152
DLib::DLib(const char* filename, int flags)
325
152
    : filename_(filename), flags_(flags), handle_(nullptr) {}
326
327
#ifdef __POSIX__
328
152
bool DLib::Open() {
329
152
  handle_ = dlopen(filename_.c_str(), flags_);
330
152
  if (handle_ != nullptr) return true;
331
3
  errmsg_ = dlerror();
332
3
  return false;
333
}
334
335
19
void DLib::Close() {
336
19
  if (handle_ == nullptr) return;
337
338
16
  if (libc_may_be_musl()) {
339
    // musl libc implements dlclose() as a no-op which returns 0.
340
    // As a consequence, trying to re-load a previously closed addon at a later
341
    // point will not call its static constructors, which Node.js uses.
342
    // Therefore, when we may be using musl libc, we assume that the shared
343
    // object exists indefinitely and keep it in our handle map.
344
    return;
345
  }
346
347
16
  int err = dlclose(handle_);
348
16
  if (err == 0) {
349
16
    if (has_entry_in_global_handle_map_)
350
13
      global_handle_map.erase(handle_);
351
  }
352
16
  handle_ = nullptr;
353
}
354
355
22
void* DLib::GetSymbolAddress(const char* name) {
356
22
  return dlsym(handle_, name);
357
}
358
#else   // !__POSIX__
359
bool DLib::Open() {
360
  int ret = uv_dlopen(filename_.c_str(), &lib_);
361
  if (ret == 0) {
362
    handle_ = static_cast<void*>(lib_.handle);
363
    return true;
364
  }
365
  errmsg_ = uv_dlerror(&lib_);
366
  uv_dlclose(&lib_);
367
  return false;
368
}
369
370
void DLib::Close() {
371
  if (handle_ == nullptr) return;
372
  if (has_entry_in_global_handle_map_)
373
    global_handle_map.erase(handle_);
374
  uv_dlclose(&lib_);
375
  handle_ = nullptr;
376
}
377
378
void* DLib::GetSymbolAddress(const char* name) {
379
  void* address;
380
  if (0 == uv_dlsym(&lib_, name, &address)) return address;
381
  return nullptr;
382
}
383
#endif  // !__POSIX__
384
385
138
void DLib::SaveInGlobalHandleMap(node_module* mp) {
386
138
  has_entry_in_global_handle_map_ = true;
387
138
  global_handle_map.set(handle_, mp);
388
138
}
389
390
6
node_module* DLib::GetSavedModuleFromGlobalHandleMap() {
391
6
  has_entry_in_global_handle_map_ = true;
392
6
  return global_handle_map.get_and_increase_refcount(handle_);
393
}
394
395
using InitializerCallback = void (*)(Local<Object> exports,
396
                                     Local<Value> module,
397
                                     Local<Context> context);
398
399
13
inline InitializerCallback GetInitializerCallback(DLib* dlib) {
400
13
  const char* name = "node_register_module_v" STRINGIFY(NODE_MODULE_VERSION);
401
13
  return reinterpret_cast<InitializerCallback>(dlib->GetSymbolAddress(name));
402
}
403
404
9
inline napi_addon_register_func GetNapiInitializerCallback(DLib* dlib) {
405
  const char* name =
406
9
      STRINGIFY(NAPI_MODULE_INITIALIZER_BASE) STRINGIFY(NAPI_MODULE_VERSION);
407
  return reinterpret_cast<napi_addon_register_func>(
408
9
      dlib->GetSymbolAddress(name));
409
}
410
411
// DLOpen is process.dlopen(module, filename, flags).
412
// Used to load 'module.node' dynamically shared objects.
413
//
414
// FIXME(bnoordhuis) Not multi-context ready. TBD how to resolve the conflict
415
// when two contexts try to load the same shared object. Maybe have a shadow
416
// cache that's a plain C list or hash table that's shared across contexts?
417
153
void DLOpen(const FunctionCallbackInfo<Value>& args) {
418
153
  Environment* env = Environment::GetCurrent(args);
419
153
  auto context = env->context();
420
421
153
  CHECK_NULL(thread_local_modpending);
422
423
153
  if (args.Length() < 2) {
424
    env->ThrowError("process.dlopen needs at least 2 arguments.");
425
    return;
426
  }
427
428
153
  int32_t flags = DLib::kDefaultFlags;
429

156
  if (args.Length() > 2 && !args[2]->Int32Value(context).To(&flags)) {
430
    return env->ThrowTypeError("flag argument must be an integer.");
431
  }
432
433
  Local<Object> module;
434
  Local<Object> exports;
435
  Local<Value> exports_v;
436

918
  if (!args[0]->ToObject(context).ToLocal(&module) ||
437

765
      !module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
438
459
      !exports_v->ToObject(context).ToLocal(&exports)) {
439
1
    return;  // Exception pending.
440
  }
441
442
304
  node::Utf8Value filename(env->isolate(), args[1]);  // Cast
443
608
  env->TryLoadAddon(*filename, flags, [&](DLib* dlib) {
444

152
    static Mutex dlib_load_mutex;
445
304
    Mutex::ScopedLock lock(dlib_load_mutex);
446
447
152
    const bool is_opened = dlib->Open();
448
449
    // Objects containing v14 or later modules will have registered themselves
450
    // on the pending list.  Activate all of them now.  At present, only one
451
    // module per object is supported.
452
152
    node_module* mp = thread_local_modpending;
453
152
    thread_local_modpending = nullptr;
454
455
152
    if (!is_opened) {
456
      Local<String> errmsg =
457
53
          OneByteString(env->isolate(), dlib->errmsg_.c_str());
458
3
      dlib->Close();
459
#ifdef _WIN32
460
      // Windows needs to add the filename into the error message
461
      errmsg = String::Concat(
462
          env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked());
463
#endif  // _WIN32
464
6
      env->isolate()->ThrowException(Exception::Error(errmsg));
465
3
      return false;
466
    }
467
468
149
    if (mp != nullptr) {
469
139
      if (mp->nm_context_register_func == nullptr) {
470
42
        if (env->options()->force_context_aware) {
471
1
          dlib->Close();
472
1
          THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
473
1
          return false;
474
        }
475
      }
476
138
      mp->nm_dso_handle = dlib->handle_;
477
138
      dlib->SaveInGlobalHandleMap(mp);
478
    } else {
479
10
      if (auto callback = GetInitializerCallback(dlib)) {
480
288
        callback(exports, module, context);
481
1
        return true;
482
9
      } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) {
483
3
        napi_module_register_by_symbol(exports, module, context, napi_callback);
484
3
        return true;
485
      } else {
486
6
        mp = dlib->GetSavedModuleFromGlobalHandleMap();
487

6
        if (mp == nullptr || mp->nm_context_register_func == nullptr) {
488
3
          dlib->Close();
489
          char errmsg[1024];
490
3
          snprintf(errmsg,
491
                   sizeof(errmsg),
492
                   "Module did not self-register: '%s'.",
493
7
                   *filename);
494
3
          env->ThrowError(errmsg);
495
3
          return false;
496
        }
497
      }
498
    }
499
500
    // -1 is used for N-API modules
501

141
    if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) {
502
      // Even if the module did self-register, it may have done so with the
503
      // wrong version. We must only give up after having checked to see if it
504
      // has an appropriate initializer callback.
505
3
      if (auto callback = GetInitializerCallback(dlib)) {
506
2
        callback(exports, module, context);
507
2
        return true;
508
      }
509
      char errmsg[1024];
510
2
      snprintf(errmsg,
511
               sizeof(errmsg),
512
               "The module '%s'"
513
               "\nwas compiled against a different Node.js version using"
514
               "\nNODE_MODULE_VERSION %d. This version of Node.js requires"
515
               "\nNODE_MODULE_VERSION %d. Please try re-compiling or "
516
               "re-installing\nthe module (for instance, using `npm rebuild` "
517
               "or `npm install`).",
518
               *filename,
519
               mp->nm_version,
520
1
               NODE_MODULE_VERSION);
521
522
      // NOTE: `mp` is allocated inside of the shared library's memory, calling
523
      // `dlclose` will deallocate it
524
1
      dlib->Close();
525
1
      env->ThrowError(errmsg);
526
1
      return false;
527
    }
528
138
    CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0);
529
530
    // Do not keep the lock while running userland addon loading code.
531
276
    Mutex::ScopedUnlock unlock(lock);
532
138
    if (mp->nm_context_register_func != nullptr) {
533
198
      mp->nm_context_register_func(exports, module, context, mp->nm_priv);
534
39
    } else if (mp->nm_register_func != nullptr) {
535
78
      mp->nm_register_func(exports, module, mp->nm_priv);
536
    } else {
537
      dlib->Close();
538
      env->ThrowError("Module has no declared entry point.");
539
      return false;
540
    }
541
542
138
    return true;
543
304
  });
544
545
  // Tell coverity that 'handle' should not be freed when we return.
546
  // coverity[leaked_storage]
547
}
548
549
149334
inline struct node_module* FindModule(struct node_module* list,
550
                                      const char* name,
551
                                      int flag) {
552
  struct node_module* mp;
553
554
4235200
  for (mp = list; mp != nullptr; mp = mp->nm_link) {
555
4230800
    if (strcmp(mp->nm_modname, name) == 0) break;
556
  }
557
558

149334
  CHECK(mp == nullptr || (mp->nm_flags & flag) != 0);
559
149334
  return mp;
560
}
561
562
144932
static Local<Object> InitModule(Environment* env,
563
                                node_module* mod,
564
                                Local<String> module) {
565
144932
  Local<Object> exports = Object::New(env->isolate());
566
  // Internal bindings don't have a "module" object, only exports.
567
144932
  CHECK_NULL(mod->nm_register_func);
568
144932
  CHECK_NOT_NULL(mod->nm_context_register_func);
569
144932
  Local<Value> unused = Undefined(env->isolate());
570
144932
  mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv);
571
144932
  return exports;
572
}
573
574
static void ThrowIfNoSuchModule(Environment* env, const char* module_v) {
575
  char errmsg[1024];
576
  snprintf(errmsg, sizeof(errmsg), "No such module: %s", module_v);
577
  env->ThrowError(errmsg);
578
}
579
580
149331
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
581
149331
  Environment* env = Environment::GetCurrent(args);
582
583
447990
  CHECK(args[0]->IsString());
584
585
298660
  Local<String> module = args[0].As<String>();
586
298661
  node::Utf8Value module_v(env->isolate(), module);
587
  Local<Object> exports;
588
589
149331
  node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL);
590
149331
  if (mod != nullptr) {
591
144932
    exports = InitModule(env, mod, module);
592
4399
  } else if (!strcmp(*module_v, "constants")) {
593
4392
    exports = Object::New(env->isolate());
594
17568
    CHECK(
595
        exports->SetPrototype(env->context(), Null(env->isolate())).FromJust());
596
4392
    DefineConstants(env->isolate(), exports);
597
7
  } else if (!strcmp(*module_v, "natives")) {
598
7
    exports = native_module::NativeModuleEnv::GetSourceObject(env->context());
599
    // Legacy feature: process.binding('natives').config contains stringified
600
    // config.gypi
601
35
    CHECK(exports
602
              ->Set(env->context(),
603
                    env->config_string(),
604
                    native_module::NativeModuleEnv::GetConfigString(
605
                        env->isolate()))
606
              .FromJust());
607
  } else {
608
    return ThrowIfNoSuchModule(env, *module_v);
609
  }
610
611
298662
  args.GetReturnValue().Set(exports);
612
}
613
614
2
void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
615
2
  Environment* env = Environment::GetCurrent(args);
616
617
6
  CHECK(args[0]->IsString());
618
619
4
  Local<String> module_name = args[0].As<String>();
620
621
4
  node::Utf8Value module_name_v(env->isolate(), module_name);
622
2
  const char* name = *module_name_v;
623
2
  node_module* mod = nullptr;
624
625
  // Iterate from here to the nearest non-Worker Environment to see if there's
626
  // a linked binding defined locally rather than through the global list.
627
2
  Environment* cur_env = env;
628

6
  while (mod == nullptr && cur_env != nullptr) {
629
4
    Mutex::ScopedLock lock(cur_env->extra_linked_bindings_mutex());
630
2
    mod = FindModule(cur_env->extra_linked_bindings_head(), name, NM_F_LINKED);
631
2
    cur_env = cur_env->worker_parent_env();
632
  }
633
634
2
  if (mod == nullptr)
635
1
    mod = FindModule(modlist_linked, name, NM_F_LINKED);
636
637
2
  if (mod == nullptr) {
638
    char errmsg[1024];
639
    snprintf(errmsg,
640
             sizeof(errmsg),
641
             "No such module was linked: %s",
642
             *module_name_v);
643
    return env->ThrowError(errmsg);
644
  }
645
646
2
  Local<Object> module = Object::New(env->isolate());
647
2
  Local<Object> exports = Object::New(env->isolate());
648
  Local<String> exports_prop =
649
4
      String::NewFromUtf8(env->isolate(), "exports", NewStringType::kNormal)
650
2
          .ToLocalChecked();
651
6
  module->Set(env->context(), exports_prop, exports).Check();
652
653
2
  if (mod->nm_context_register_func != nullptr) {
654
4
    mod->nm_context_register_func(
655
2
        exports, module, env->context(), mod->nm_priv);
656
  } else if (mod->nm_register_func != nullptr) {
657
    mod->nm_register_func(exports, module, mod->nm_priv);
658
  } else {
659
    return env->ThrowError("Linked module has no declared entry point.");
660
  }
661
662
  auto effective_exports =
663
6
      module->Get(env->context(), exports_prop).ToLocalChecked();
664
665
4
  args.GetReturnValue().Set(effective_exports);
666
}
667
668
// Call built-in modules' _register_<module name> function to
669
// do module registration explicitly.
670
4201
void RegisterBuiltinModules() {
671
#define V(modname) _register_##modname();
672
4201
  NODE_BUILTIN_MODULES(V)
673
#undef V
674
4201
}
675
676
}  // namespace binding
677

12606
}  // namespace node