GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_binding.cc Lines: 197 211 93.4 %
Date: 2022-11-20 04:28:36 Branches: 98 126 77.8 %

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

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

189
  if (args.Length() > 2 && !args[2]->Int32Value(context).To(&flags)) {
430
    return THROW_ERR_INVALID_ARG_TYPE(env, "flag argument must be an integer.");
431
  }
432
433
  Local<Object> module;
434
  Local<Object> exports;
435
  Local<Value> exports_v;
436
187
  if (!args[0]->ToObject(context).ToLocal(&module) ||
437

935
      !module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
438

561
      !exports_v->ToObject(context).ToLocal(&exports)) {
439
1
    return;  // Exception pending.
440
  }
441
442
186
  node::Utf8Value filename(env->isolate(), args[1]);  // Cast
443
186
  env->TryLoadAddon(*filename, flags, [&](DLib* dlib) {
444

186
    static Mutex dlib_load_mutex;
445
372
    Mutex::ScopedLock lock(dlib_load_mutex);
446
447
186
    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
186
    node_module* mp = thread_local_modpending;
453
186
    thread_local_modpending = nullptr;
454
455
186
    if (!is_opened) {
456
4
      std::string errmsg = dlib->errmsg_.c_str();
457
4
      dlib->Close();
458
#ifdef _WIN32
459
      // Windows needs to add the filename into the error message
460
      errmsg += *filename;
461
#endif  // _WIN32
462
4
      THROW_ERR_DLOPEN_FAILED(env, "%s", errmsg.c_str());
463
4
      return false;
464
    }
465
466
182
    if (mp != nullptr) {
467
171
      if (mp->nm_context_register_func == nullptr) {
468
44
        if (env->force_context_aware()) {
469
1
          dlib->Close();
470
1
          THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
471
1
          return false;
472
        }
473
      }
474
170
      mp->nm_dso_handle = dlib->handle_;
475
170
      dlib->SaveInGlobalHandleMap(mp);
476
    } else {
477
11
      if (auto callback = GetInitializerCallback(dlib)) {
478
4
        callback(exports, module, context);
479
1
        return true;
480
10
      } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) {
481
3
        napi_module_register_by_symbol(exports, module, context, napi_callback);
482
3
        return true;
483
      } else {
484
7
        mp = dlib->GetSavedModuleFromGlobalHandleMap();
485

7
        if (mp == nullptr || mp->nm_context_register_func == nullptr) {
486
4
          dlib->Close();
487
4
          THROW_ERR_DLOPEN_FAILED(
488
4
              env, "Module did not self-register: '%s'.", *filename);
489
4
          return false;
490
        }
491
      }
492
    }
493
494
    // -1 is used for N-API modules
495

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

90820
  CHECK(mp == nullptr || (mp->nm_flags & flag) != 0);
552
90820
  return mp;
553
}
554
555
89975
static Local<Object> InitModule(Environment* env,
556
                                node_module* mod,
557
                                Local<String> module) {
558
  // Internal bindings don't have a "module" object, only exports.
559
89975
  Local<Function> ctor = env->binding_data_ctor_template()
560
89975
                             ->GetFunction(env->context())
561
89975
                             .ToLocalChecked();
562
89975
  Local<Object> exports = ctor->NewInstance(env->context()).ToLocalChecked();
563
89975
  CHECK_NULL(mod->nm_register_func);
564
89975
  CHECK_NOT_NULL(mod->nm_context_register_func);
565
89975
  Local<Value> unused = Undefined(env->isolate());
566
89975
  mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv);
567
89975
  return exports;
568
}
569
570
90809
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
571
90809
  Environment* env = Environment::GetCurrent(args);
572
573
181618
  CHECK(args[0]->IsString());
574
575
181618
  Local<String> module = args[0].As<String>();
576
90809
  node::Utf8Value module_v(env->isolate(), module);
577
  Local<Object> exports;
578
579
90809
  node_module* mod = FindModule(modlist_internal, *module_v, NM_F_INTERNAL);
580
90809
  if (mod != nullptr) {
581
89975
    exports = InitModule(env, mod, module);
582
89975
    env->internal_bindings.insert(mod);
583
834
  } else if (!strcmp(*module_v, "constants")) {
584
792
    exports = Object::New(env->isolate());
585
2376
    CHECK(
586
        exports->SetPrototype(env->context(), Null(env->isolate())).FromJust());
587
792
    DefineConstants(env->isolate(), exports);
588
42
  } else if (!strcmp(*module_v, "natives")) {
589
42
    exports = builtins::BuiltinLoader::GetSourceObject(env->context());
590
    // Legacy feature: process.binding('natives').config contains stringified
591
    // config.gypi
592
168
    CHECK(exports
593
              ->Set(env->context(),
594
                    env->config_string(),
595
                    builtins::BuiltinLoader::GetConfigString(env->isolate()))
596
              .FromJust());
597
  } else {
598
    return THROW_ERR_INVALID_MODULE(env, "No such module: %s", *module_v);
599
  }
600
601
181618
  args.GetReturnValue().Set(exports);
602
}
603
604
10
void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
605
10
  Environment* env = Environment::GetCurrent(args);
606
607
20
  CHECK(args[0]->IsString());
608
609
20
  Local<String> module_name = args[0].As<String>();
610
611
10
  node::Utf8Value module_name_v(env->isolate(), module_name);
612
10
  const char* name = *module_name_v;
613
10
  node_module* mod = nullptr;
614
615
  // Iterate from here to the nearest non-Worker Environment to see if there's
616
  // a linked binding defined locally rather than through the global list.
617
10
  Environment* cur_env = env;
618

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