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: 162 199 81.4 %
Date: 2019-03-02 22:23:06 Branches: 97 142 68.3 %

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

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


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


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


1233
      !module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
433
411
      !exports_v->ToObject(context).ToLocal(&exports)) {
434
1
    return;  // Exception pending.
435
  }
436
437
136
  node::Utf8Value filename(env->isolate(), args[1]);  // Cast
438
272
  env->TryLoadAddon(*filename, flags, [&](DLib* dlib) {
439

136
    static Mutex dlib_load_mutex;
440
136
    Mutex::ScopedLock lock(dlib_load_mutex);
441
442
136
    const bool is_opened = dlib->Open();
443
444
    // Objects containing v14 or later modules will have registered themselves
445
    // on the pending list.  Activate all of them now.  At present, only one
446
    // module per object is supported.
447
    node_module* mp =
448
136
        static_cast<node_module*>(uv_key_get(&thread_local_modpending));
449
136
    uv_key_set(&thread_local_modpending, nullptr);
450
451
136
    if (!is_opened) {
452
      Local<String> errmsg =
453
3
          OneByteString(env->isolate(), dlib->errmsg_.c_str());
454
3
      dlib->Close();
455
#ifdef _WIN32
456
      // Windows needs to add the filename into the error message
457
      errmsg = String::Concat(
458
          env->isolate(), errmsg, args[1]->ToString(context).ToLocalChecked());
459
#endif  // _WIN32
460
3
      env->isolate()->ThrowException(Exception::Error(errmsg));
461
3
      return false;
462
    }
463
464
133
    if (mp != nullptr) {
465
125
      mp->nm_dso_handle = dlib->handle_;
466
125
      dlib->SaveInGlobalHandleMap(mp);
467
    } else {
468
8
      if (auto callback = GetInitializerCallback(dlib)) {
469
2
        callback(exports, module, context);
470
1
        return true;
471
7
      } else if (auto napi_callback = GetNapiInitializerCallback(dlib)) {
472
4
        napi_module_register_by_symbol(exports, module, context, napi_callback);
473
2
        return true;
474
      } else {
475
5
        mp = dlib->GetSavedModuleFromGlobalHandleMap();
476

5
        if (mp == nullptr || mp->nm_context_register_func == nullptr) {
477
3
          dlib->Close();
478
3
          env->ThrowError("Module did not self-register.");
479
3
          return false;
480
        }
481
      }
482
    }
483
484
    // -1 is used for N-API modules
485

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

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

12876
}  // namespace node