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-05-05 22:32:45 Branches: 97 142 68.3 %

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

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


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


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


1233
      !module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
440
411
      !exports_v->ToObject(context).ToLocal(&exports)) {
441
1
    return;  // Exception pending.
442
  }
443
444
136
  node::Utf8Value filename(env->isolate(), args[1]);  // Cast
445
272
  env->TryLoadAddon(*filename, flags, [&](DLib* dlib) {
446

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

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

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

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

13572
}  // namespace node