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: 164 200 82.0 %
Date: 2019-10-08 22:34:21 Branches: 101 144 70.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(worker)                                                                    \
89
  V(zlib)
90
91
#define NODE_BUILTIN_MODULES(V)                                                \
92
  NODE_BUILTIN_STANDARD_MODULES(V)                                             \
93
  NODE_BUILTIN_OPENSSL_MODULES(V)                                              \
94
  NODE_BUILTIN_ICU_MODULES(V)                                                  \
95
  NODE_BUILTIN_REPORT_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
  OnScopeLeave cleanup([&]() { 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
14
static bool libc_may_be_musl() {
222

14
  static std::atomic_bool retval;  // Cache the return value.
223
  static std::atomic_bool has_cached_retval { false };
224
14
  if (has_cached_retval) return retval;
225
13
  retval = dlsym(RTLD_DEFAULT, "gnu_get_libc_version") == nullptr;
226
13
  has_cached_retval = true;
227
13
  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::FunctionCallbackInfo;
238
using v8::Local;
239
using v8::NewStringType;
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
249191
extern "C" void node_module_register(void* m) {
253
249191
  struct node_module* mp = reinterpret_cast<struct node_module*>(m);
254
255
249191
  if (mp->nm_flags & NM_F_INTERNAL) {
256
249050
    mp->nm_link = modlist_internal;
257
249050
    modlist_internal = mp;
258
141
  } 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
    mp->nm_flags = NM_F_LINKED;
262
    mp->nm_link = modlist_linked;
263
    modlist_linked = mp;
264
  } else {
265
141
    thread_local_modpending = mp;
266
  }
267
249191
}
268
269
namespace binding {
270
271
9964
static struct global_handle_map_t {
272
 public:
273
140
  void set(void* handle, node_module* mod) {
274
140
    CHECK_NE(handle, nullptr);
275
140
    Mutex::ScopedLock lock(mutex_);
276
277
140
    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
140
    map_[handle].wants_delete_module = mod->nm_flags & NM_F_DELETEME;
285
140
    map_[handle].refcount++;
286
140
  }
287
288
5
  node_module* get_and_increase_refcount(void* handle) {
289
5
    CHECK_NE(handle, nullptr);
290
5
    Mutex::ScopedLock lock(mutex_);
291
292
5
    auto it = map_.find(handle);
293
5
    if (it == map_.end()) return nullptr;
294
4
    it->second.refcount++;
295
4
    return it->second.module;
296
  }
297
298
12
  void erase(void* handle) {
299
12
    CHECK_NE(handle, nullptr);
300
12
    Mutex::ScopedLock lock(mutex_);
301
302
12
    auto it = map_.find(handle);
303
24
    if (it == map_.end()) return;
304
11
    CHECK_GE(it->second.refcount, 1);
305
11
    if (--it->second.refcount == 0) {
306
7
      if (it->second.wants_delete_module)
307
2
        delete it->second.module;
308
7
      map_.erase(handle);
309
11
    }
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
4982
} global_handle_map;
321
322
152
DLib::DLib(const char* filename, int flags)
323
152
    : filename_(filename), flags_(flags), handle_(nullptr) {}
324
325
#ifdef __POSIX__
326
152
bool DLib::Open() {
327
152
  handle_ = dlopen(filename_.c_str(), flags_);
328
152
  if (handle_ != nullptr) return true;
329
3
  errmsg_ = dlerror();
330
3
  return false;
331
}
332
333
17
void DLib::Close() {
334
17
  if (handle_ == nullptr) return;
335
336
14
  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
14
  int err = dlclose(handle_);
346
14
  if (err == 0) {
347
14
    if (has_entry_in_global_handle_map_)
348
12
      global_handle_map.erase(handle_);
349
  }
350
14
  handle_ = nullptr;
351
}
352
353
18
void* DLib::GetSymbolAddress(const char* name) {
354
18
  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
140
void DLib::SaveInGlobalHandleMap(node_module* mp) {
384
140
  has_entry_in_global_handle_map_ = true;
385
140
  global_handle_map.set(handle_, mp);
386
140
}
387
388
5
node_module* DLib::GetSavedModuleFromGlobalHandleMap() {
389
5
  has_entry_in_global_handle_map_ = true;
390
5
  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
11
inline InitializerCallback GetInitializerCallback(DLib* dlib) {
398
11
  const char* name = "node_register_module_v" STRINGIFY(NODE_MODULE_VERSION);
399
11
  return reinterpret_cast<InitializerCallback>(dlib->GetSymbolAddress(name));
400
}
401
402
7
inline napi_addon_register_func GetNapiInitializerCallback(DLib* dlib) {
403
  const char* name =
404
7
      STRINGIFY(NAPI_MODULE_INITIALIZER_BASE) STRINGIFY(NAPI_MODULE_VERSION);
405
  return reinterpret_cast<napi_addon_register_func>(
406
7
      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
153
void DLOpen(const FunctionCallbackInfo<Value>& args) {
416
153
  Environment* env = Environment::GetCurrent(args);
417
153
  auto context = env->context();
418
419
153
  CHECK_NULL(thread_local_modpending);
420
421
153
  if (args.Length() < 2) {
422
    env->ThrowError("process.dlopen needs at least 2 arguments.");
423
    return;
424
  }
425
426
153
  int32_t flags = DLib::kDefaultFlags;
427


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


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


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

152
    static Mutex dlib_load_mutex;
443
152
    Mutex::ScopedLock lock(dlib_load_mutex);
444
445
152
    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
152
    node_module* mp = thread_local_modpending;
451
152
    thread_local_modpending = nullptr;
452
453
152
    if (!is_opened) {
454
      Local<String> errmsg =
455
3
          OneByteString(env->isolate(), dlib->errmsg_.c_str());
456
3
      dlib->Close();
457
#ifdef _WIN32
458
      // Windows needs to add the filename into the error message
459
      errmsg = String::Concat(
460
          env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked());
461
#endif  // _WIN32
462
3
      env->isolate()->ThrowException(Exception::Error(errmsg));
463
3
      return false;
464
    }
465
466
149
    if (mp != nullptr) {
467
141
      if (mp->nm_context_register_func == nullptr) {
468
48
        if (env->options()->force_context_aware) {
469
1
          dlib->Close();
470
1
          THROW_ERR_NON_CONTEXT_AWARE_DISABLED(env);
471
1
          return false;
472
        }
473
      }
474
140
      mp->nm_dso_handle = dlib->handle_;
475
140
      dlib->SaveInGlobalHandleMap(mp);
476
    } else {
477
8
      if (auto callback = GetInitializerCallback(dlib)) {
478
2
        callback(exports, module, context);
479
1
        return true;
480
7
      } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) {
481
4
        napi_module_register_by_symbol(exports, module, context, napi_callback);
482
2
        return true;
483
      } else {
484
5
        mp = dlib->GetSavedModuleFromGlobalHandleMap();
485

5
        if (mp == nullptr || mp->nm_context_register_func == nullptr) {
486
3
          dlib->Close();
487
3
          env->ThrowError("Module did not self-register.");
488
3
          return false;
489
        }
490
      }
491
    }
492
493
    // -1 is used for N-API modules
494

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

174943
  CHECK(mp == nullptr || (mp->nm_flags & flag) != 0);
552
174943
  return mp;
553
}
554
555
174943
node_module* get_internal_module(const char* name) {
556
174943
  return FindModule(modlist_internal, name, NM_F_INTERNAL);
557
}
558
node_module* get_linked_module(const char* name) {
559
  return FindModule(modlist_linked, name, NM_F_LINKED);
560
}
561
562
169532
static Local<Object> InitModule(Environment* env,
563
                                node_module* mod,
564
                                Local<String> module) {
565
169532
  Local<Object> exports = Object::New(env->isolate());
566
  // Internal bindings don't have a "module" object, only exports.
567
169532
  CHECK_NULL(mod->nm_register_func);
568
169532
  CHECK_NOT_NULL(mod->nm_context_register_func);
569
169532
  Local<Value> unused = Undefined(env->isolate());
570
169532
  mod->nm_context_register_func(exports, unused, env->context(), mod->nm_priv);
571
169532
  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
174943
void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {
581
174943
  Environment* env = Environment::GetCurrent(args);
582
583
524829
  CHECK(args[0]->IsString());
584
585
349886
  Local<String> module = args[0].As<String>();
586
174943
  node::Utf8Value module_v(env->isolate(), module);
587
  Local<Object> exports;
588
589
174943
  node_module* mod = get_internal_module(*module_v);
590
174943
  if (mod != nullptr) {
591
169532
    exports = InitModule(env, mod, module);
592
5411
  } else if (!strcmp(*module_v, "constants")) {
593
5132
    exports = Object::New(env->isolate());
594
20528
    CHECK(
595
        exports->SetPrototype(env->context(), Null(env->isolate())).FromJust());
596
5132
    DefineConstants(env->isolate(), exports);
597
279
  } else if (!strcmp(*module_v, "natives")) {
598
279
    exports = native_module::NativeModuleEnv::GetSourceObject(env->context());
599
    // Legacy feature: process.binding('natives').config contains stringified
600
    // config.gypi
601
1395
    CHECK(exports
602
              ->Set(env->context(),
603
                    env->config_string(),
604
                    native_module::NativeModuleEnv::GetConfigString(
605
                        env->isolate()))
606
              .FromJust());
607
  } else {
608
174943
    return ThrowIfNoSuchModule(env, *module_v);
609
  }
610
611
349886
  args.GetReturnValue().Set(exports);
612
}
613
614
void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
615
  Environment* env = Environment::GetCurrent(args);
616
617
  CHECK(args[0]->IsString());
618
619
  Local<String> module_name = args[0].As<String>();
620
621
  node::Utf8Value module_name_v(env->isolate(), module_name);
622
  node_module* mod = get_linked_module(*module_name_v);
623
624
  if (mod == nullptr) {
625
    char errmsg[1024];
626
    snprintf(errmsg,
627
             sizeof(errmsg),
628
             "No such module was linked: %s",
629
             *module_name_v);
630
    return env->ThrowError(errmsg);
631
  }
632
633
  Local<Object> module = Object::New(env->isolate());
634
  Local<Object> exports = Object::New(env->isolate());
635
  Local<String> exports_prop =
636
      String::NewFromUtf8(env->isolate(), "exports", NewStringType::kNormal)
637
          .ToLocalChecked();
638
  module->Set(env->context(), exports_prop, exports).Check();
639
640
  if (mod->nm_context_register_func != nullptr) {
641
    mod->nm_context_register_func(
642
        exports, module, env->context(), mod->nm_priv);
643
  } else if (mod->nm_register_func != nullptr) {
644
    mod->nm_register_func(exports, module, mod->nm_priv);
645
  } else {
646
    return env->ThrowError("Linked module has no declared entry point.");
647
  }
648
649
  auto effective_exports =
650
      module->Get(env->context(), exports_prop).ToLocalChecked();
651
652
  args.GetReturnValue().Set(effective_exports);
653
}
654
655
// Call built-in modules' _register_<module name> function to
656
// do module registration explicitly.
657
4981
void RegisterBuiltinModules() {
658
#define V(modname) _register_##modname();
659
4981
  NODE_BUILTIN_MODULES(V)
660
#undef V
661
4981
}
662
663
}  // namespace binding
664

14946
}  // namespace node