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: 202 215 94.0 %
Date: 2021-04-20 04:11:54 Branches: 102 134 76.1 %

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

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

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

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

815
      !module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
436
489
      !exports_v->ToObject(context).ToLocal(&exports)) {
437
1
    return;  // Exception pending.
438
  }
439
440
324
  node::Utf8Value filename(env->isolate(), args[1]);  // Cast
441
648
  env->TryLoadAddon(*filename, flags, [&](DLib* dlib) {
442

162
    static Mutex dlib_load_mutex;
443
324
    Mutex::ScopedLock lock(dlib_load_mutex);
444
445
162
    const bool is_opened = dlib->Open();
446
447
    // Objects containing v14 or later modules will have registered themselves
448
    // on the pending list.  Activate all of them now.  At present, only one
449
    // module per object is supported.
450
162
    node_module* mp = thread_local_modpending;
451
162
    thread_local_modpending = nullptr;
452
453
162
    if (!is_opened) {
454
6
      std::string errmsg = dlib->errmsg_.c_str();
455
3
      dlib->Close();
456
#ifdef _WIN32
457
      // Windows needs to add the filename into the error message
458
      errmsg += *filename;
459
#endif  // _WIN32
460
50
      THROW_ERR_DLOPEN_FAILED(env, errmsg.c_str());
461
3
      return false;
462
    }
463
464
159
    if (mp != nullptr) {
465
149
      if (mp->nm_context_register_func == nullptr) {
466
42
        if (env->force_context_aware()) {
467
1
          dlib->Close();
468
1
          THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
469
1
          return false;
470
        }
471
      }
472
148
      mp->nm_dso_handle = dlib->handle_;
473
148
      dlib->SaveInGlobalHandleMap(mp);
474
    } else {
475
10
      if (auto callback = GetInitializerCallback(dlib)) {
476
308
        callback(exports, module, context);
477
1
        return true;
478
9
      } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) {
479
3
        napi_module_register_by_symbol(exports, module, context, napi_callback);
480
3
        return true;
481
      } else {
482
6
        mp = dlib->GetSavedModuleFromGlobalHandleMap();
483

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

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

77583
  CHECK(mp == nullptr || (mp->nm_flags & flag) != 0);
555
77583
  return mp;
556
}
557
558
77109
static Local<Object> InitModule(Environment* env,
559
                                node_module* mod,
560
                                Local<String> module) {
561
  // Internal bindings don't have a "module" object, only exports.
562
154219
  Local<Function> ctor = env->binding_data_ctor_template()
563
231328
                             ->GetFunction(env->context())
564
77110
                             .ToLocalChecked();
565
231326
  Local<Object> exports = ctor->NewInstance(env->context()).ToLocalChecked();
566
77106
  CHECK_NULL(mod->nm_register_func);
567
77106
  CHECK_NOT_NULL(mod->nm_context_register_func);
568
77106
  Local<Value> unused = Undefined(env->isolate());
569
77109
  mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv);
570
77111
  return exports;
571
}
572
573
77579
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
574
77579
  Environment* env = Environment::GetCurrent(args);
575
576
232734
  CHECK(args[0]->IsString());
577
578
155156
  Local<String> module = args[0].As<String>();
579
155157
  node::Utf8Value module_v(env->isolate(), module);
580
  Local<Object> exports;
581
582
77578
  node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL);
583
77577
  if (mod != nullptr) {
584
77109
    exports = InitModule(env, mod, module);
585
77111
    env->internal_bindings.insert(mod);
586
468
  } else if (!strcmp(*module_v, "constants")) {
587
462
    exports = Object::New(env->isolate());
588
1848
    CHECK(
589
        exports->SetPrototype(env->context(), Null(env->isolate())).FromJust());
590
462
    DefineConstants(env->isolate(), exports);
591
6
  } else if (!strcmp(*module_v, "natives")) {
592
6
    exports = native_module::NativeModuleEnv::GetSourceObject(env->context());
593
    // Legacy feature: process.binding('natives').config contains stringified
594
    // config.gypi
595
30
    CHECK(exports
596
              ->Set(env->context(),
597
                    env->config_string(),
598
                    native_module::NativeModuleEnv::GetConfigString(
599
                        env->isolate()))
600
              .FromJust());
601
  } else {
602
    char errmsg[1024];
603
    snprintf(errmsg, sizeof(errmsg), "No such module: %s", *module_v);
604
    return THROW_ERR_INVALID_MODULE(env, errmsg);
605
  }
606
607
155158
  args.GetReturnValue().Set(exports);
608
}
609
610
4
void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
611
4
  Environment* env = Environment::GetCurrent(args);
612
613
12
  CHECK(args[0]->IsString());
614
615
8
  Local<String> module_name = args[0].As<String>();
616
617
8
  node::Utf8Value module_name_v(env->isolate(), module_name);
618
4
  const char* name = *module_name_v;
619
4
  node_module* mod = nullptr;
620
621
  // Iterate from here to the nearest non-Worker Environment to see if there's
622
  // a linked binding defined locally rather than through the global list.
623
4
  Environment* cur_env = env;
624

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

19013
NODE_MODULE_EXTERNAL_REFERENCE(binding,
682
                               node::binding::RegisterExternalReferences)