GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/module_wrap.cc Lines: 657 736 89.3 %
Date: 2019-09-07 22:28:56 Branches: 365 521 70.1 %

Line Branch Exec Source
1
#include "module_wrap.h"
2
3
#include "env.h"
4
#include "memory_tracker-inl.h"
5
#include "node_errors.h"
6
#include "node_url.h"
7
#include "util-inl.h"
8
#include "node_contextify.h"
9
#include "node_watchdog.h"
10
11
#include <sys/stat.h>  // S_IFDIR
12
13
#include <algorithm>
14
#include <climits>  // PATH_MAX
15
16
namespace node {
17
namespace loader {
18
19
using errors::TryCatchScope;
20
21
using node::contextify::ContextifyContext;
22
using node::url::URL;
23
using node::url::URL_FLAGS_FAILED;
24
using v8::Array;
25
using v8::Context;
26
using v8::Function;
27
using v8::FunctionCallbackInfo;
28
using v8::FunctionTemplate;
29
using v8::Global;
30
using v8::HandleScope;
31
using v8::Integer;
32
using v8::IntegrityLevel;
33
using v8::Isolate;
34
using v8::Just;
35
using v8::Local;
36
using v8::Maybe;
37
using v8::MaybeLocal;
38
using v8::Module;
39
using v8::Nothing;
40
using v8::Number;
41
using v8::Object;
42
using v8::PrimitiveArray;
43
using v8::Promise;
44
using v8::ScriptCompiler;
45
using v8::ScriptOrigin;
46
using v8::String;
47
using v8::Undefined;
48
using v8::Value;
49
50
static const char* const EXTENSIONS[] = {
51
  ".mjs",
52
  ".cjs",
53
  ".js",
54
  ".json",
55
  ".node"
56
};
57
58
429
ModuleWrap::ModuleWrap(Environment* env,
59
                       Local<Object> object,
60
                       Local<Module> module,
61
                       Local<String> url) :
62
  BaseObject(env, object),
63
1716
  id_(env->get_next_module_id()) {
64
429
  module_.Reset(env->isolate(), module);
65
429
  url_.Reset(env->isolate(), url);
66
429
  env->id_to_module_map.emplace(id_, this);
67
429
}
68
69
2436
ModuleWrap::~ModuleWrap() {
70
406
  HandleScope scope(env()->isolate());
71
812
  Local<Module> module = module_.Get(env()->isolate());
72
406
  env()->id_to_module_map.erase(id_);
73
406
  auto range = env()->hash_to_module_map.equal_range(module->GetIdentityHash());
74
406
  for (auto it = range.first; it != range.second; ++it) {
75
406
    if (it->second == this) {
76
406
      env()->hash_to_module_map.erase(it);
77
406
      break;
78
    }
79
406
  }
80
812
}
81
82
370
ModuleWrap* ModuleWrap::GetFromModule(Environment* env,
83
                                      Local<Module> module) {
84
370
  auto range = env->hash_to_module_map.equal_range(module->GetIdentityHash());
85
370
  for (auto it = range.first; it != range.second; ++it) {
86
740
    if (it->second->module_ == module) {
87
370
      return it->second;
88
    }
89
  }
90
  return nullptr;
91
}
92
93
38
ModuleWrap* ModuleWrap::GetFromID(Environment* env, uint32_t id) {
94
38
  auto module_wrap_it = env->id_to_module_map.find(id);
95
38
  if (module_wrap_it == env->id_to_module_map.end()) {
96
    return nullptr;
97
  }
98
38
  return module_wrap_it->second;
99
}
100
101
435
void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
102
435
  Environment* env = Environment::GetCurrent(args);
103
435
  Isolate* isolate = env->isolate();
104
105
435
  CHECK(args.IsConstructCall());
106
435
  Local<Object> that = args.This();
107
108
435
  const int argc = args.Length();
109
435
  CHECK_GE(argc, 2);
110
111
1305
  CHECK(args[0]->IsString());
112
870
  Local<String> source_text = args[0].As<String>();
113
114
1305
  CHECK(args[1]->IsString());
115
870
  Local<String> url = args[1].As<String>();
116
117
  Local<Context> context;
118
  Local<Integer> line_offset;
119
  Local<Integer> column_offset;
120
121
435
  if (argc == 5) {
122
    // new ModuleWrap(source, url, context?, lineOffset, columnOffset)
123
180
    if (args[2]->IsUndefined()) {
124
53
      context = that->CreationContext();
125
    } else {
126
14
      CHECK(args[2]->IsObject());
127
      ContextifyContext* sandbox =
128
          ContextifyContext::ContextFromContextifiedSandbox(
129
14
              env, args[2].As<Object>());
130
7
      CHECK_NOT_NULL(sandbox);
131
7
      context = sandbox->context();
132
    }
133
134
120
    CHECK(args[3]->IsNumber());
135
120
    line_offset = args[3].As<Integer>();
136
137
120
    CHECK(args[4]->IsNumber());
138
120
    column_offset = args[4].As<Integer>();
139
  } else {
140
    // new ModuleWrap(source, url)
141
375
    context = that->CreationContext();
142
375
    line_offset = Integer::New(isolate, 0);
143
375
    column_offset = Integer::New(isolate, 0);
144
  }
145
146
435
  ShouldNotAbortOnUncaughtScope no_abort_scope(env);
147
864
  TryCatchScope try_catch(env);
148
  Local<Module> module;
149
150
  Local<PrimitiveArray> host_defined_options =
151
435
      PrimitiveArray::New(isolate, HostDefinedOptions::kLength);
152
  host_defined_options->Set(isolate, HostDefinedOptions::kType,
153
870
                            Number::New(isolate, ScriptType::kModule));
154
155
  // compile
156
  {
157
    ScriptOrigin origin(url,
158
                        line_offset,                          // line offset
159
                        column_offset,                        // column offset
160
                        True(isolate),                        // is cross origin
161
                        Local<Integer>(),                     // script id
162
                        Local<Value>(),                       // source map URL
163
                        False(isolate),                       // is opaque (?)
164
                        False(isolate),                       // is WASM
165
                        True(isolate),                        // is ES Module
166
435
                        host_defined_options);
167
    Context::Scope context_scope(context);
168
429
    ScriptCompiler::Source source(source_text, origin);
169
870
    if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
170

6
      if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
171
12
        CHECK(!try_catch.Message().IsEmpty());
172
12
        CHECK(!try_catch.Exception().IsEmpty());
173
        AppendExceptionLine(env, try_catch.Exception(), try_catch.Message(),
174
6
                            ErrorHandlingMode::MODULE_ERROR);
175
6
        try_catch.ReThrow();
176
      }
177
6
      return;
178
429
    }
179
  }
180
181
1716
  if (!that->Set(context, env->url_string(), url).FromMaybe(false)) {
182
    return;
183
  }
184
185
429
  ModuleWrap* obj = new ModuleWrap(env, that, module, url);
186
429
  obj->context_.Reset(isolate, context);
187
188
429
  env->hash_to_module_map.emplace(module->GetIdentityHash(), obj);
189
190
  host_defined_options->Set(isolate, HostDefinedOptions::kID,
191
858
                            Number::New(isolate, obj->id()));
192
193
429
  that->SetIntegrityLevel(context, IntegrityLevel::kFrozen);
194
1287
  args.GetReturnValue().Set(that);
195
}
196
197
406
void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
198
406
  Environment* env = Environment::GetCurrent(args);
199
406
  Isolate* isolate = args.GetIsolate();
200
201
406
  CHECK_EQ(args.Length(), 1);
202
812
  CHECK(args[0]->IsFunction());
203
204
406
  Local<Object> that = args.This();
205
206
  ModuleWrap* obj;
207
406
  ASSIGN_OR_RETURN_UNWRAP(&obj, that);
208
209
406
  if (obj->linked_)
210
    return;
211
406
  obj->linked_ = true;
212
213
812
  Local<Function> resolver_arg = args[0].As<Function>();
214
215
812
  Local<Context> mod_context = obj->context_.Get(isolate);
216
812
  Local<Module> module = obj->module_.Get(isolate);
217
218
  Local<Array> promises = Array::New(isolate,
219
406
                                     module->GetModuleRequestsLength());
220
221
  // call the dependency resolve callbacks
222
1220
  for (int i = 0; i < module->GetModuleRequestsLength(); i++) {
223
204
    Local<String> specifier = module->GetModuleRequest(i);
224
204
    Utf8Value specifier_utf8(env->isolate(), specifier);
225
408
    std::string specifier_std(*specifier_utf8, specifier_utf8.length());
226
227
    Local<Value> argv[] = {
228
      specifier
229
408
    };
230
231
    MaybeLocal<Value> maybe_resolve_return_value =
232
204
        resolver_arg->Call(mod_context, that, 1, argv);
233
204
    if (maybe_resolve_return_value.IsEmpty()) {
234
      return;
235
    }
236
    Local<Value> resolve_return_value =
237
204
        maybe_resolve_return_value.ToLocalChecked();
238
204
    if (!resolve_return_value->IsPromise()) {
239
      env->ThrowError("linking error, expected resolver to return a promise");
240
    }
241
204
    Local<Promise> resolve_promise = resolve_return_value.As<Promise>();
242
204
    obj->resolve_cache_[specifier_std].Reset(env->isolate(), resolve_promise);
243
244
612
    promises->Set(mod_context, i, resolve_promise).Check();
245
204
  }
246
247
812
  args.GetReturnValue().Set(promises);
248
}
249
250
131
void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
251
131
  Environment* env = Environment::GetCurrent(args);
252
131
  Isolate* isolate = args.GetIsolate();
253
  ModuleWrap* obj;
254
134
  ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
255
262
  Local<Context> context = obj->context_.Get(isolate);
256
262
  Local<Module> module = obj->module_.Get(isolate);
257
131
  TryCatchScope try_catch(env);
258
131
  USE(module->InstantiateModule(context, ResolveCallback));
259
260
  // clear resolve cache on instantiate
261
131
  obj->resolve_cache_.clear();
262
263

131
  if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
264
6
    CHECK(!try_catch.Message().IsEmpty());
265
6
    CHECK(!try_catch.Exception().IsEmpty());
266
    AppendExceptionLine(env, try_catch.Exception(), try_catch.Message(),
267
3
                        ErrorHandlingMode::MODULE_ERROR);
268
3
    try_catch.ReThrow();
269
3
    return;
270
128
  }
271
}
272
273
139
void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
274
139
  Environment* env = Environment::GetCurrent(args);
275
139
  Isolate* isolate = env->isolate();
276
  ModuleWrap* obj;
277
151
  ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
278
278
  Local<Context> context = obj->context_.Get(isolate);
279
278
  Local<Module> module = obj->module_.Get(isolate);
280
281
  // module.evaluate(timeout, breakOnSigint)
282
139
  CHECK_EQ(args.Length(), 2);
283
284
278
  CHECK(args[0]->IsNumber());
285
556
  int64_t timeout = args[0]->IntegerValue(env->context()).FromJust();
286
287
278
  CHECK(args[1]->IsBoolean());
288
278
  bool break_on_sigint = args[1]->IsTrue();
289
290
139
  ShouldNotAbortOnUncaughtScope no_abort_scope(env);
291
265
  TryCatchScope try_catch(env);
292
293
139
  bool timed_out = false;
294
139
  bool received_signal = false;
295
  MaybeLocal<Value> result;
296

139
  if (break_on_sigint && timeout != -1) {
297
    Watchdog wd(isolate, timeout, &timed_out);
298
    SigintWatchdog swd(isolate, &received_signal);
299
    result = module->Evaluate(context);
300
139
  } else if (break_on_sigint) {
301
    SigintWatchdog swd(isolate, &received_signal);
302
    result = module->Evaluate(context);
303
139
  } else if (timeout != -1) {
304
1
    Watchdog wd(isolate, timeout, &timed_out);
305
1
    result = module->Evaluate(context);
306
  } else {
307
138
    result = module->Evaluate(context);
308
  }
309
310
  // Convert the termination exception into a regular exception.
311

138
  if (timed_out || received_signal) {
312

1
    if (!env->is_main_thread() && env->is_stopping())
313
      return;
314
1
    env->isolate()->CancelTerminateExecution();
315
    // It is possible that execution was terminated by another timeout in
316
    // which this timeout is nested, so check whether one of the watchdogs
317
    // from this invocation is responsible for termination.
318
1
    if (timed_out) {
319
1
      THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, timeout);
320
    } else if (received_signal) {
321
      THROW_ERR_SCRIPT_EXECUTION_INTERRUPTED(env);
322
    }
323
  }
324
325
138
  if (try_catch.HasCaught()) {
326
12
    if (!try_catch.HasTerminated())
327
11
      try_catch.ReThrow();
328
12
    return;
329
  }
330
331
378
  args.GetReturnValue().Set(result.ToLocalChecked());
332
}
333
334
116
void ModuleWrap::Namespace(const FunctionCallbackInfo<Value>& args) {
335
116
  Environment* env = Environment::GetCurrent(args);
336
116
  Isolate* isolate = args.GetIsolate();
337
  ModuleWrap* obj;
338
116
  ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
339
340
232
  Local<Module> module = obj->module_.Get(isolate);
341
342
116
  switch (module->GetStatus()) {
343
    default:
344
      return env->ThrowError(
345
          "cannot get namespace, Module has not been instantiated");
346
    case v8::Module::Status::kInstantiated:
347
    case v8::Module::Status::kEvaluating:
348
    case v8::Module::Status::kEvaluated:
349
116
      break;
350
  }
351
352
116
  Local<Value> result = module->GetModuleNamespace();
353
232
  args.GetReturnValue().Set(result);
354
}
355
356
112
void ModuleWrap::GetStatus(const FunctionCallbackInfo<Value>& args) {
357
112
  Isolate* isolate = args.GetIsolate();
358
  ModuleWrap* obj;
359
224
  ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
360
361
224
  Local<Module> module = obj->module_.Get(isolate);
362
363
336
  args.GetReturnValue().Set(module->GetStatus());
364
}
365
366
2
void ModuleWrap::GetStaticDependencySpecifiers(
367
    const FunctionCallbackInfo<Value>& args) {
368
2
  Environment* env = Environment::GetCurrent(args);
369
  ModuleWrap* obj;
370
4
  ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
371
372
4
  Local<Module> module = obj->module_.Get(env->isolate());
373
374
2
  int count = module->GetModuleRequestsLength();
375
376
2
  Local<Array> specifiers = Array::New(env->isolate(), count);
377
378
3
  for (int i = 0; i < count; i++)
379
4
    specifiers->Set(env->context(), i, module->GetModuleRequest(i)).Check();
380
381
4
  args.GetReturnValue().Set(specifiers);
382
}
383
384
1
void ModuleWrap::GetError(const FunctionCallbackInfo<Value>& args) {
385
1
  Isolate* isolate = args.GetIsolate();
386
  ModuleWrap* obj;
387
2
  ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
388
389
2
  Local<Module> module = obj->module_.Get(isolate);
390
3
  args.GetReturnValue().Set(module->GetException());
391
}
392
393
194
MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
394
                                               Local<String> specifier,
395
                                               Local<Module> referrer) {
396
194
  Environment* env = Environment::GetCurrent(context);
397
194
  CHECK_NOT_NULL(env);  // TODO(addaleax): Handle nullptr here.
398
194
  Isolate* isolate = env->isolate();
399
400
194
  ModuleWrap* dependent = GetFromModule(env, referrer);
401
194
  if (dependent == nullptr) {
402
    env->ThrowError("linking error, null dep");
403
    return MaybeLocal<Module>();
404
  }
405
406
194
  Utf8Value specifier_utf8(isolate, specifier);
407
388
  std::string specifier_std(*specifier_utf8, specifier_utf8.length());
408
409
194
  if (dependent->resolve_cache_.count(specifier_std) != 1) {
410
    env->ThrowError("linking error, not in local cache");
411
    return MaybeLocal<Module>();
412
  }
413
414
  Local<Promise> resolve_promise =
415
388
      dependent->resolve_cache_[specifier_std].Get(isolate);
416
417
194
  if (resolve_promise->State() != Promise::kFulfilled) {
418
    env->ThrowError("linking error, dependency promises must be resolved on "
419
                    "instantiate");
420
    return MaybeLocal<Module>();
421
  }
422
423
388
  Local<Object> module_object = resolve_promise->Result().As<Object>();
424

388
  if (module_object.IsEmpty() || !module_object->IsObject()) {
425
    env->ThrowError("linking error, expected a valid module object from "
426
                    "resolver");
427
    return MaybeLocal<Module>();
428
  }
429
430
  ModuleWrap* module;
431
194
  ASSIGN_OR_RETURN_UNWRAP(&module, module_object, MaybeLocal<Module>());
432
582
  return module->module_.Get(isolate);
433
}
434
435
namespace {
436
437
// Tests whether a path starts with /, ./ or ../
438
// In WhatWG terminology, the alternative case is called a "bare" specifier
439
// (e.g. in `import "jquery"`).
440
231
inline bool ShouldBeTreatedAsRelativeOrAbsolutePath(
441
    const std::string& specifier) {
442
231
  size_t len = specifier.length();
443
231
  if (len == 0)
444
    return false;
445
231
  if (specifier[0] == '/') {
446
87
    return true;
447
144
  } else if (specifier[0] == '.') {
448

106
    if (len == 1 || specifier[1] == '/')
449
32
      return true;
450
74
    if (specifier[1] == '.') {
451

74
      if (len == 2 || specifier[2] == '/')
452
74
        return true;
453
    }
454
  }
455
38
  return false;
456
}
457
458
18
std::string ReadFile(uv_file file) {
459
18
  std::string contents;
460
  uv_fs_t req;
461
  char buffer_memory[4096];
462
18
  uv_buf_t buf = uv_buf_init(buffer_memory, sizeof(buffer_memory));
463
464
  do {
465
    const int r = uv_fs_read(nullptr,
466
                             &req,
467
                             file,
468
                             &buf,
469
                             1,
470
36
                             contents.length(),  // offset
471
36
                             nullptr);
472
36
    uv_fs_req_cleanup(&req);
473
474
36
    if (r <= 0)
475
18
      break;
476
18
    contents.append(buf.base, r);
477
  } while (true);
478
18
  return contents;
479
}
480
481
enum DescriptorType {
482
  FILE,
483
  DIRECTORY,
484
  NONE
485
};
486
487
// When DescriptorType cache is added, this can also return
488
// Nothing for the "null" cache entries.
489
1152
inline Maybe<uv_file> OpenDescriptor(const std::string& path) {
490
  uv_fs_t fs_req;
491
1152
  uv_file fd = uv_fs_open(nullptr, &fs_req, path.c_str(), O_RDONLY, 0, nullptr);
492
1152
  uv_fs_req_cleanup(&fs_req);
493
1152
  if (fd < 0) return Nothing<uv_file>();
494
244
  return Just(fd);
495
}
496
497
244
inline void CloseDescriptor(uv_file fd) {
498
  uv_fs_t fs_req;
499
244
  CHECK_EQ(0, uv_fs_close(nullptr, &fs_req, fd, nullptr));
500
244
  uv_fs_req_cleanup(&fs_req);
501
244
}
502
503
244
inline DescriptorType CheckDescriptorAtFile(uv_file fd) {
504
  uv_fs_t fs_req;
505
244
  int rc = uv_fs_fstat(nullptr, &fs_req, fd, nullptr);
506
244
  if (rc == 0) {
507
244
    uint64_t is_directory = fs_req.statbuf.st_mode & S_IFDIR;
508
244
    uv_fs_req_cleanup(&fs_req);
509
244
    return is_directory ? DIRECTORY : FILE;
510
  }
511
  uv_fs_req_cleanup(&fs_req);
512
  return NONE;
513
}
514
515
// TODO(@guybedford): Add a DescriptorType cache layer here.
516
// Should be directory based -> if path/to/dir doesn't exist
517
// then the cache should early-fail any path/to/dir/file check.
518
312
DescriptorType CheckDescriptorAtPath(const std::string& path) {
519
312
  Maybe<uv_file> fd = OpenDescriptor(path);
520
312
  if (fd.IsNothing()) return NONE;
521
226
  DescriptorType type = CheckDescriptorAtFile(fd.FromJust());
522
226
  CloseDescriptor(fd.FromJust());
523
226
  return type;
524
}
525
526
840
Maybe<std::string> ReadIfFile(const std::string& path) {
527
840
  Maybe<uv_file> fd = OpenDescriptor(path);
528
840
  if (fd.IsNothing()) return Nothing<std::string>();
529
18
  DescriptorType type = CheckDescriptorAtFile(fd.FromJust());
530
18
  if (type != FILE) return Nothing<std::string>();
531
18
  std::string source = ReadFile(fd.FromJust());
532
18
  CloseDescriptor(fd.FromJust());
533
18
  return Just(source);
534
}
535
536
using Exists = PackageConfig::Exists;
537
using IsValid = PackageConfig::IsValid;
538
using HasMain = PackageConfig::HasMain;
539
using PackageType = PackageConfig::PackageType;
540
541
1682
Maybe<const PackageConfig*> GetPackageConfig(Environment* env,
542
                                             const std::string& path,
543
                                             const URL& base) {
544
1682
  auto existing = env->package_json_cache.find(path);
545
1682
  if (existing != env->package_json_cache.end()) {
546
842
    const PackageConfig* pcfg = &existing->second;
547
842
    if (pcfg->is_valid == IsValid::No) {
548
      std::string msg = "Invalid JSON in " + path +
549
        " imported from " + base.ToFilePath();
550
      node::THROW_ERR_INVALID_PACKAGE_CONFIG(env, msg.c_str());
551
      return Nothing<const PackageConfig*>();
552
    }
553
842
    return Just(pcfg);
554
  }
555
556
840
  Maybe<std::string> source = ReadIfFile(path);
557
558
840
  if (source.IsNothing()) {
559
    auto entry = env->package_json_cache.emplace(path,
560
        PackageConfig { Exists::No, IsValid::Yes, HasMain::No, "",
561
1644
                        PackageType::None, Global<Value>() });
562
822
    return Just(&entry.first->second);
563
  }
564
565
18
  std::string pkg_src = source.FromJust();
566
567
18
  Isolate* isolate = env->isolate();
568
36
  v8::HandleScope handle_scope(isolate);
569
570
  Local<Object> pkg_json;
571
  {
572
    Local<Value> src;
573
    Local<Value> pkg_json_v;
574
18
    Local<Context> context = env->context();
575
576

108
    if (!ToV8Value(context, pkg_src).ToLocal(&src) ||
577

144
        !v8::JSON::Parse(context, src.As<String>()).ToLocal(&pkg_json_v) ||
578
54
        !pkg_json_v->ToObject(context).ToLocal(&pkg_json)) {
579
      env->package_json_cache.emplace(path,
580
          PackageConfig { Exists::Yes, IsValid::No, HasMain::No, "",
581
                          PackageType::None, Global<Value>() });
582
      std::string msg = "Invalid JSON in " + path +
583
          " imported from " + base.ToFilePath();
584
      node::THROW_ERR_INVALID_PACKAGE_CONFIG(env, msg.c_str());
585
      return Nothing<const PackageConfig*>();
586
    }
587
  }
588
589
  Local<Value> pkg_main;
590
18
  HasMain has_main = HasMain::No;
591
36
  std::string main_std;
592
72
  if (pkg_json->Get(env->context(), env->main_string()).ToLocal(&pkg_main)) {
593
36
    if (pkg_main->IsString()) {
594
13
      has_main = HasMain::Yes;
595
    }
596
18
    Utf8Value main_utf8(isolate, pkg_main);
597
18
    main_std.assign(std::string(*main_utf8, main_utf8.length()));
598
  }
599
600
18
  PackageType pkg_type = PackageType::None;
601
  Local<Value> type_v;
602
72
  if (pkg_json->Get(env->context(), env->type_string()).ToLocal(&type_v)) {
603
36
    if (type_v->StrictEquals(env->module_string())) {
604
6
      pkg_type = PackageType::Module;
605
24
    } else if (type_v->StrictEquals(env->commonjs_string())) {
606
4
      pkg_type = PackageType::CommonJS;
607
    }
608
    // ignore unknown types for forwards compatibility
609
  }
610
611
  Local<Value> exports_v;
612

108
  if (env->options()->experimental_exports &&
613
      pkg_json->Get(env->context(),
614


144
      env->exports_string()).ToLocal(&exports_v) &&
615
36
      !exports_v->IsNullOrUndefined()) {
616
    Global<Value> exports;
617
2
    exports.Reset(env->isolate(), exports_v);
618
619
    auto entry = env->package_json_cache.emplace(path,
620
        PackageConfig { Exists::Yes, IsValid::Yes, has_main, main_std,
621
4
                        pkg_type, std::move(exports) });
622
2
    return Just(&entry.first->second);
623
  }
624
625
  auto entry = env->package_json_cache.emplace(path,
626
      PackageConfig { Exists::Yes, IsValid::Yes, has_main, main_std,
627
32
                      pkg_type, Global<Value>() });
628
856
  return Just(&entry.first->second);
629
}
630
631
193
Maybe<const PackageConfig*> GetPackageScopeConfig(Environment* env,
632
                                                  const URL& resolved,
633
                                                  const URL& base) {
634
193
  URL pjson_url("./package.json", &resolved);
635
  while (true) {
636
1656
    std::string pjson_url_path = pjson_url.path();
637

6138
    if (pjson_url_path.length() > 25 &&
638
5166
        pjson_url_path.substr(pjson_url_path.length() - 25, 25) ==
639
        "node_modules/package.json") {
640
2
      break;
641
    }
642
    Maybe<const PackageConfig*> pkg_cfg =
643
1654
        GetPackageConfig(env, pjson_url.ToFilePath(), base);
644
1654
    if (pkg_cfg.IsNothing()) return pkg_cfg;
645
1654
    if (pkg_cfg.FromJust()->exists == Exists::Yes) return pkg_cfg;
646
647
3088
    URL last_pjson_url = pjson_url;
648
1625
    pjson_url = URL("../package.json", pjson_url);
649
650
    // Terminates at root where ../package.json equals ../../package.json
651
    // (can't just check "/package.json" for Windows support).
652

1625
    if (pjson_url.path() == last_pjson_url.path()) break;
653
1463
  }
654
  auto entry = env->package_json_cache.emplace(pjson_url.ToFilePath(),
655
  PackageConfig { Exists::No, IsValid::Yes, HasMain::No, "",
656
328
                  PackageType::None, Global<Value>() });
657
164
  const PackageConfig* pcfg = &entry.first->second;
658
164
  return Just(pcfg);
659
}
660
661
/*
662
 * Legacy CommonJS main resolution:
663
 * 1. let M = pkg_url + (json main field)
664
 * 2. TRY(M, M.js, M.json, M.node)
665
 * 3. TRY(M/index.js, M/index.json, M/index.node)
666
 * 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node)
667
 * 5. NOT_FOUND
668
 */
669
59
inline bool FileExists(const URL& url) {
670
59
  return CheckDescriptorAtPath(url.ToFilePath()) == FILE;
671
}
672
1
Maybe<URL> LegacyMainResolve(const URL& pjson_url,
673
                             const PackageConfig& pcfg) {
674
1
  URL guess;
675
1
  if (pcfg.has_main == HasMain::Yes) {
676
    // Note: fs check redundances will be handled by Descriptor cache here.
677
    if (FileExists(guess = URL("./" + pcfg.main, pjson_url))) {
678
      return Just(guess);
679
    }
680
    if (FileExists(guess = URL("./" + pcfg.main + ".js", pjson_url))) {
681
      return Just(guess);
682
    }
683
    if (FileExists(guess = URL("./" + pcfg.main + ".json", pjson_url))) {
684
      return Just(guess);
685
    }
686
    if (FileExists(guess = URL("./" + pcfg.main + ".node", pjson_url))) {
687
      return Just(guess);
688
    }
689
    if (FileExists(guess = URL("./" + pcfg.main + "/index.js", pjson_url))) {
690
      return Just(guess);
691
    }
692
    // Such stat.
693
    if (FileExists(guess = URL("./" + pcfg.main + "/index.json", pjson_url))) {
694
      return Just(guess);
695
    }
696
    if (FileExists(guess = URL("./" + pcfg.main + "/index.node", pjson_url))) {
697
      return Just(guess);
698
    }
699
    // Fallthrough.
700
  }
701
1
  if (FileExists(guess = URL("./index.js", pjson_url))) {
702
    return Just(guess);
703
  }
704
  // So fs.
705
1
  if (FileExists(guess = URL("./index.json", pjson_url))) {
706
    return Just(guess);
707
  }
708
1
  if (FileExists(guess = URL("./index.node", pjson_url))) {
709
    return Just(guess);
710
  }
711
  // Not found.
712
1
  return Nothing<URL>();
713
}
714
715
enum ResolveExtensionsOptions {
716
  TRY_EXACT_NAME,
717
  ONLY_VIA_EXTENSIONS
718
};
719
720
template <ResolveExtensionsOptions options>
721
22
Maybe<URL> ResolveExtensions(const URL& search) {
722
  if (options == TRY_EXACT_NAME) {
723
17
    if (FileExists(search)) {
724
9
      return Just(search);
725
    }
726
  }
727
728

45
  for (const char* extension : EXTENSIONS) {
729
39
    URL guess(search.path() + extension, &search);
730

39
    if (FileExists(guess)) {
731

7
      return Just(guess);
732
    }
733
  }
734
735
6
  return Nothing<URL>();
736
}
737
738
5
inline Maybe<URL> ResolveIndex(const URL& search) {
739
5
  return ResolveExtensions<ONLY_VIA_EXTENSIONS>(URL("index", search));
740
}
741
742
208
Maybe<URL> FinalizeResolution(Environment* env,
743
                              const URL& resolved,
744
                              const URL& base) {
745
208
  if (env->options()->es_module_specifier_resolution == "node") {
746
17
    Maybe<URL> file = ResolveExtensions<TRY_EXACT_NAME>(resolved);
747
17
    if (!file.IsNothing()) {
748
12
      return file;
749
    }
750
5
    if (resolved.path().back() != '/') {
751
4
      file = ResolveIndex(URL(resolved.path() + "/", &base));
752
    } else {
753
1
      file = ResolveIndex(resolved);
754
    }
755
5
    if (!file.IsNothing()) {
756
4
      return file;
757
    }
758
2
    std::string msg = "Cannot find module " + resolved.path() +
759
4
        " imported from " + base.ToFilePath();
760
1
    node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
761
18
    return Nothing<URL>();
762
  }
763
764
191
  const std::string& path = resolved.ToFilePath();
765
191
  if (CheckDescriptorAtPath(path) != FILE) {
766
30
    std::string msg = "Cannot find module " +
767
45
        (path.length() != 0 ? path : resolved.path()) +
768
45
        " imported from " + base.ToFilePath();
769
15
    node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
770
15
    return Nothing<URL>();
771
  }
772
773
176
  return Just(resolved);
774
}
775
776
5
Maybe<URL> PackageMainResolve(Environment* env,
777
                              const URL& pjson_url,
778
                              const PackageConfig& pcfg,
779
                              const URL& base) {
780
5
  if (pcfg.exists == Exists::Yes) {
781
5
    if (pcfg.has_main == HasMain::Yes) {
782
4
      URL resolved(pcfg.main, pjson_url);
783
7
      const std::string& path = resolved.ToFilePath();
784
4
      if (CheckDescriptorAtPath(path) == FILE) {
785
1
        return Just(resolved);
786
3
      }
787
    }
788
4
    if (env->options()->es_module_specifier_resolution == "node") {
789
3
      if (pcfg.has_main == HasMain::Yes) {
790
3
        return FinalizeResolution(env, URL(pcfg.main, pjson_url), base);
791
      } else {
792
        return FinalizeResolution(env, URL("index", pjson_url), base);
793
      }
794
    }
795
1
    if (pcfg.type != PackageType::Module) {
796
1
      Maybe<URL> resolved = LegacyMainResolve(pjson_url, pcfg);
797
1
      if (!resolved.IsNothing()) {
798
        return resolved;
799
1
      }
800
    }
801
  }
802
2
  std::string msg = "Cannot find main entry point for " +
803
2
      URL(".", pjson_url).ToFilePath() + " imported from " +
804
3
      base.ToFilePath();
805
1
  node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
806
1
  return Nothing<URL>();
807
}
808
809
2
void ThrowExportsNotFound(Environment* env,
810
                          const std::string& subpath,
811
                          const URL& pjson_url,
812
                          const URL& base) {
813
4
  const std::string msg = "Package exports for " +
814
4
      pjson_url.ToFilePath() + " do not define a '" + subpath +
815
6
      "' subpath, imported from " + base.ToFilePath();
816
2
  node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
817
2
}
818
819
18
void ThrowExportsInvalid(Environment* env,
820
                         const std::string& subpath,
821
                         const std::string& target,
822
                         const URL& pjson_url,
823
                         const URL& base) {
824
36
  const std::string msg = "Cannot resolve package exports target '" + target +
825
72
      "' matched for '" + subpath + "' in " + pjson_url.ToFilePath() +
826
54
      ", imported from " + base.ToFilePath();
827
18
  node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
828
18
}
829
830
8
void ThrowExportsInvalid(Environment* env,
831
                         const std::string& subpath,
832
                         Local<Value> target,
833
                         const URL& pjson_url,
834
                         const URL& base) {
835
  Local<String> target_string;
836
24
  if (target->ToString(env->context()).ToLocal(&target_string)) {
837
8
    Utf8Value target_utf8(env->isolate(), target_string);
838
16
    std::string target_str(*target_utf8, target_utf8.length());
839
8
    if (target->IsArray()) {
840
1
      target_str = '[' + target_str + ']';
841
    }
842
16
    ThrowExportsInvalid(env, subpath, target_str, pjson_url, base);
843
  }
844
8
}
845
846
18
Maybe<URL> ResolveExportsTarget(Environment* env,
847
                                const std::string& target,
848
                                const std::string& subpath,
849
                                const std::string& match,
850
                                const URL& pjson_url,
851
                                const URL& base,
852
                                bool throw_invalid = true) {
853
18
  if (target.substr(0, 2) != "./") {
854
8
    if (throw_invalid) {
855
5
      ThrowExportsInvalid(env, match, target, pjson_url, base);
856
    }
857
8
    return Nothing<URL>();
858
  }
859

10
  if (subpath.length() > 0 && target.back() != '/') {
860
1
    if (throw_invalid) {
861
      ThrowExportsInvalid(env, match, target, pjson_url, base);
862
    }
863
1
    return Nothing<URL>();
864
  }
865
9
  URL resolved(target, pjson_url);
866
18
  std::string resolved_path = resolved.path();
867
18
  std::string pkg_path = URL(".", pjson_url).path();
868

18
  if (resolved_path.find(pkg_path) != 0 ||
869
9
      resolved_path.find("/node_modules/", pkg_path.length() - 1) !=
870
      std::string::npos) {
871
1
    if (throw_invalid) {
872
1
      ThrowExportsInvalid(env, match, target, pjson_url, base);
873
    }
874
1
    return Nothing<URL>();
875
  }
876
8
  if (subpath.length() == 0) return Just(resolved);
877
10
  URL subpath_resolved(subpath, resolved);
878
10
  std::string subpath_resolved_path = subpath_resolved.path();
879

9
  if (subpath_resolved_path.find(resolved_path) != 0 ||
880
4
      subpath_resolved_path.find("/node_modules/", pkg_path.length() - 1)
881
      != std::string::npos) {
882
1
    if (throw_invalid) {
883
1
      ThrowExportsInvalid(env, match, target + subpath, pjson_url, base);
884
    }
885
1
    return Nothing<URL>();
886
  }
887
13
  return Just(subpath_resolved);
888
}
889
890
21
Maybe<URL> PackageExportsResolve(Environment* env,
891
                                 const URL& pjson_url,
892
                                 const std::string& pkg_subpath,
893
                                 const PackageConfig& pcfg,
894
                                 const URL& base) {
895
21
  CHECK(env->options()->experimental_exports);
896
21
  Isolate* isolate = env->isolate();
897
21
  Local<Context> context = env->context();
898
42
  Local<Value> exports = pcfg.exports.Get(isolate);
899
21
  if (!exports->IsObject()) {
900
1
    ThrowExportsNotFound(env, pkg_subpath, pjson_url, base);
901
1
    return Nothing<URL>();
902
  }
903
20
  Local<Object> exports_obj = exports.As<Object>();
904
  Local<String> subpath = String::NewFromUtf8(isolate,
905
40
      pkg_subpath.c_str(), v8::NewStringType::kNormal).ToLocalChecked();
906
907
40
  if (exports_obj->HasOwnProperty(context, subpath).FromJust()) {
908
24
    Local<Value> target = exports_obj->Get(context, subpath).ToLocalChecked();
909
24
    if (target->IsString()) {
910
5
      Utf8Value target_utf8(isolate, target.As<v8::String>());
911
10
      std::string target_str(*target_utf8, target_utf8.length());
912
      Maybe<URL> resolved = ResolveExportsTarget(env, target_str, "",
913
10
          pkg_subpath, pjson_url, base);
914
5
      if (resolved.IsNothing()) {
915
3
        ThrowExportsInvalid(env, pkg_subpath, target, pjson_url, base);
916
3
        return Nothing<URL>();
917
      }
918
7
      return FinalizeResolution(env, resolved.FromJust(), base);
919
7
    } else if (target->IsArray()) {
920
3
      Local<Array> target_arr = target.As<Array>();
921
3
      const uint32_t length = target_arr->Length();
922
3
      if (length == 0) {
923
1
        ThrowExportsInvalid(env, pkg_subpath, target, pjson_url, base);
924
1
        return Nothing<URL>();
925
      }
926
9
      for (uint32_t i = 0; i < length; i++) {
927
16
        auto target_item = target_arr->Get(context, i).ToLocalChecked();
928
16
        if (target_item->IsString()) {
929
3
          Utf8Value target_utf8(isolate, target_item.As<v8::String>());
930
3
          std::string target(*target_utf8, target_utf8.length());
931
          Maybe<URL> resolved = ResolveExportsTarget(env, target, "",
932
3
              pkg_subpath, pjson_url, base, false);
933
3
          if (resolved.IsNothing()) continue;
934

1
          return FinalizeResolution(env, resolved.FromJust(), base);
935
        }
936
      }
937
3
      auto invalid = target_arr->Get(context, length - 1).ToLocalChecked();
938
2
      if (!invalid->IsString()) {
939
        ThrowExportsInvalid(env, pkg_subpath, invalid, pjson_url, base);
940
        return Nothing<URL>();
941
      }
942
1
      Utf8Value invalid_utf8(isolate, invalid.As<v8::String>());
943
2
      std::string invalid_str(*invalid_utf8, invalid_utf8.length());
944
      Maybe<URL> resolved = ResolveExportsTarget(env, invalid_str, "",
945
2
                                                 pkg_subpath, pjson_url, base);
946
1
      CHECK(resolved.IsNothing());
947
2
      return Nothing<URL>();
948
    } else {
949
4
      ThrowExportsInvalid(env, pkg_subpath, target, pjson_url, base);
950
4
      return Nothing<URL>();
951
    }
952
  }
953
954
  Local<String> best_match;
955
8
  std::string best_match_str = "";
956
  Local<Array> keys =
957
16
      exports_obj->GetOwnPropertyNames(context).ToLocalChecked();
958
288
  for (uint32_t i = 0; i < keys->Length(); ++i) {
959
408
    Local<String> key = keys->Get(context, i).ToLocalChecked().As<String>();
960
136
    Utf8Value key_utf8(isolate, key);
961
168
    std::string key_str(*key_utf8, key_utf8.length());
962
136
    if (key_str.back() != '/') continue;
963


39
    if (pkg_subpath.substr(0, key_str.length()) == key_str &&
964
7
        key_str.length() > best_match_str.length()) {
965
7
      best_match = key;
966
7
      best_match_str = key_str;
967
    }
968
32
  }
969
970
8
  if (best_match_str.length() > 0) {
971
14
    auto target = exports_obj->Get(context, best_match).ToLocalChecked();
972
7
    std::string subpath = pkg_subpath.substr(best_match_str.length());
973
14
    if (target->IsString()) {
974
6
      Utf8Value target_utf8(isolate, target.As<v8::String>());
975
12
      std::string target(*target_utf8, target_utf8.length());
976
      Maybe<URL> resolved = ResolveExportsTarget(env, target, subpath,
977
12
          pkg_subpath, pjson_url, base);
978
6
      if (resolved.IsNothing()) {
979
3
        ThrowExportsInvalid(env, pkg_subpath, target, pjson_url, base);
980
3
        return Nothing<URL>();
981
      }
982
9
      return FinalizeResolution(env, URL(subpath, resolved.FromJust()), base);
983
1
    } else if (target->IsArray()) {
984
1
      Local<Array> target_arr = target.As<Array>();
985
1
      const uint32_t length = target_arr->Length();
986
1
      if (length == 0) {
987
        ThrowExportsInvalid(env, pkg_subpath, target, pjson_url, base);
988
        return Nothing<URL>();
989
      }
990
6
      for (uint32_t i = 0; i < length; i++) {
991
12
        auto target_item = target_arr->Get(context, i).ToLocalChecked();
992
12
        if (target_item->IsString()) {
993
3
          Utf8Value target_utf8(isolate, target_item.As<v8::String>());
994
3
          std::string target_str(*target_utf8, target_utf8.length());
995
          Maybe<URL> resolved = ResolveExportsTarget(env, target_str, subpath,
996
3
              pkg_subpath, pjson_url, base, false);
997
3
          if (resolved.IsNothing()) continue;
998

1
          return FinalizeResolution(env, resolved.FromJust(), base);
999
        }
1000
      }
1001
      auto invalid = target_arr->Get(context, length - 1).ToLocalChecked();
1002
      if (!invalid->IsString()) {
1003
        ThrowExportsInvalid(env, pkg_subpath, invalid, pjson_url, base);
1004
        return Nothing<URL>();
1005
      }
1006
      Utf8Value invalid_utf8(isolate, invalid.As<v8::String>());
1007
      std::string invalid_str(*invalid_utf8, invalid_utf8.length());
1008
      Maybe<URL> resolved = ResolveExportsTarget(env, invalid_str, subpath,
1009
                                                 pkg_subpath, pjson_url, base);
1010
      CHECK(resolved.IsNothing());
1011
      return Nothing<URL>();
1012
    } else {
1013
      ThrowExportsInvalid(env, pkg_subpath, target, pjson_url, base);
1014
      return Nothing<URL>();
1015
7
    }
1016
  }
1017
1018
1
  ThrowExportsNotFound(env, pkg_subpath, pjson_url, base);
1019
1
  return Nothing<URL>();
1020
}
1021
1022
35
Maybe<URL> PackageResolve(Environment* env,
1023
                          const std::string& specifier,
1024
                          const URL& base) {
1025
35
  size_t sep_index = specifier.find('/');
1026
35
  bool valid_package_name = true;
1027
35
  bool scope = false;
1028
35
  if (specifier[0] == '@') {
1029
1
    scope = true;
1030

1
    if (sep_index == std::string::npos || specifier.length() == 0) {
1031
1
      valid_package_name = false;
1032
    } else {
1033
      sep_index = specifier.find('/', sep_index + 1);
1034
    }
1035
34
  } else if (specifier[0] == '.') {
1036
    valid_package_name = false;
1037
  }
1038
  std::string pkg_name = specifier.substr(0,
1039
35
      sep_index == std::string::npos ? std::string::npos : sep_index);
1040
  // Package name cannot have leading . and cannot have percent-encoding or
1041
  // separators.
1042
375
  for (size_t i = 0; i < pkg_name.length(); i++) {
1043
343
    char c = pkg_name[i];
1044

343
    if (c == '%' || c == '\\') {
1045
3
      valid_package_name = false;
1046
3
      break;
1047
    }
1048
  }
1049
35
  if (!valid_package_name) {
1050
8
    std::string msg = "Invalid package name '" + specifier +
1051
12
      "' imported from " + base.ToFilePath();
1052
4
    node::THROW_ERR_INVALID_MODULE_SPECIFIER(env, msg.c_str());
1053
4
    return Nothing<URL>();
1054
  }
1055
62
  std::string pkg_subpath;
1056

54
  if ((sep_index == std::string::npos ||
1057
23
      sep_index == specifier.length() - 1)) {
1058
8
    pkg_subpath = "";
1059
  } else {
1060
23
    pkg_subpath = "." + specifier.substr(sep_index);
1061
  }
1062
62
  URL pjson_url("./node_modules/" + pkg_name + "/package.json", &base);
1063
62
  std::string pjson_path = pjson_url.ToFilePath();
1064
62
  std::string last_path;
1065
30
  do {
1066
    DescriptorType check =
1067
58
        CheckDescriptorAtPath(pjson_path.substr(0, pjson_path.length() - 13));
1068
58
    if (check != DIRECTORY) {
1069
30
      last_path = pjson_path;
1070
60
      pjson_url = URL((scope ?
1071
60
          "../../../../node_modules/" : "../../../node_modules/") +
1072
90
          pkg_name + "/package.json", &pjson_url);
1073
30
      pjson_path = pjson_url.ToFilePath();
1074
30
      continue;
1075
    }
1076
1077
    // Package match.
1078
28
    Maybe<const PackageConfig*> pcfg = GetPackageConfig(env, pjson_path, base);
1079
    // Invalid package configuration error.
1080
56
    if (pcfg.IsNothing()) return Nothing<URL>();
1081
28
    if (!pkg_subpath.length()) {
1082
5
      return PackageMainResolve(env, pjson_url, *pcfg.FromJust(), base);
1083
    } else {
1084
46
      if (!pcfg.FromJust()->exports.IsEmpty()) {
1085
        return PackageExportsResolve(env, pjson_url, pkg_subpath,
1086
21
                                     *pcfg.FromJust(), base);
1087
      } else {
1088
2
        return FinalizeResolution(env, URL(pkg_subpath, pjson_url), base);
1089
      }
1090
    }
1091
    CHECK(false);
1092
    // Cross-platform root check.
1093
30
  } while (pjson_path.length() != last_path.length());
1094
1095
6
  std::string msg = "Cannot find package '" + pkg_name +
1096
12
      "' imported from " + base.ToFilePath();
1097
3
  node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
1098
38
  return Nothing<URL>();
1099
}
1100
1101
}  // anonymous namespace
1102
1103
231
Maybe<URL> Resolve(Environment* env,
1104
                   const std::string& specifier,
1105
                   const URL& base) {
1106
  // Order swapped from spec for minor perf gain.
1107
  // Ok since relative URLs cannot parse as URLs.
1108
231
  URL resolved;
1109
231
  if (ShouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
1110
193
    resolved = URL(specifier, base);
1111
  } else {
1112
38
    URL pure_url(specifier);
1113
38
    if (!(pure_url.flags() & URL_FLAGS_FAILED)) {
1114
3
      resolved = pure_url;
1115
    } else {
1116
35
      return PackageResolve(env, specifier, base);
1117
3
    }
1118
  }
1119
196
  return FinalizeResolution(env, resolved, base);
1120
}
1121
1122
231
void ModuleWrap::Resolve(const FunctionCallbackInfo<Value>& args) {
1123
231
  Environment* env = Environment::GetCurrent(args);
1124
1125
  // module.resolve(specifier, url)
1126
231
  CHECK_EQ(args.Length(), 2);
1127
1128
693
  CHECK(args[0]->IsString());
1129
231
  Utf8Value specifier_utf8(env->isolate(), args[0]);
1130
424
  std::string specifier_std(*specifier_utf8, specifier_utf8.length());
1131
1132
693
  CHECK(args[1]->IsString());
1133
424
  Utf8Value url_utf8(env->isolate(), args[1]);
1134
424
  URL url(*url_utf8, url_utf8.length());
1135
1136
231
  if (url.flags() & URL_FLAGS_FAILED) {
1137
    return node::THROW_ERR_INVALID_ARG_TYPE(
1138
        env, "second argument is not a URL string");
1139
  }
1140
1141
  Maybe<URL> result =
1142
      node::loader::Resolve(env,
1143
                            specifier_std,
1144
424
                            url);
1145
231
  if (result.IsNothing()) {
1146
38
    return;
1147
  }
1148
1149
193
  URL resolution = result.FromJust();
1150
193
  CHECK(!(resolution.flags() & URL_FLAGS_FAILED));
1151
1152
  Local<Value> resolution_obj;
1153
386
  if (resolution.ToObject(env).ToLocal(&resolution_obj))
1154
579
    args.GetReturnValue().Set(resolution_obj);
1155
}
1156
1157
193
void ModuleWrap::GetPackageType(const FunctionCallbackInfo<Value>& args) {
1158
193
  Environment* env = Environment::GetCurrent(args);
1159
1160
  // module.getPackageType(url)
1161
193
  CHECK_EQ(args.Length(), 1);
1162
1163
579
  CHECK(args[0]->IsString());
1164
193
  Utf8Value url_utf8(env->isolate(), args[0]);
1165
386
  URL url(*url_utf8, url_utf8.length());
1166
1167
193
  PackageType pkg_type = PackageType::None;
1168
  Maybe<const PackageConfig*> pcfg =
1169
193
      GetPackageScopeConfig(env, url, url);
1170
193
  if (!pcfg.IsNothing()) {
1171
193
    pkg_type = pcfg.FromJust()->type;
1172
  }
1173
1174
772
  args.GetReturnValue().Set(Integer::New(env->isolate(), pkg_type));
1175
193
}
1176
1177
61
static MaybeLocal<Promise> ImportModuleDynamically(
1178
    Local<Context> context,
1179
    Local<v8::ScriptOrModule> referrer,
1180
    Local<String> specifier) {
1181
61
  Isolate* iso = context->GetIsolate();
1182
61
  Environment* env = Environment::GetCurrent(context);
1183
61
  CHECK_NOT_NULL(env);  // TODO(addaleax): Handle nullptr here.
1184
61
  v8::EscapableHandleScope handle_scope(iso);
1185
1186
  Local<Function> import_callback =
1187
61
    env->host_import_module_dynamically_callback();
1188
1189
61
  Local<PrimitiveArray> options = referrer->GetHostDefinedOptions();
1190
61
  if (options->Length() != HostDefinedOptions::kLength) {
1191
    Local<Promise::Resolver> resolver =
1192
        Promise::Resolver::New(context).ToLocalChecked();
1193
    resolver
1194
        ->Reject(context,
1195
                 v8::Exception::TypeError(FIXED_ONE_BYTE_STRING(
1196
                     context->GetIsolate(), "Invalid host defined options")))
1197
        .ToChecked();
1198
    return handle_scope.Escape(resolver->GetPromise());
1199
  }
1200
1201
  Local<Value> object;
1202
1203
61
  int type = options->Get(iso, HostDefinedOptions::kType)
1204
122
                 .As<Number>()
1205
183
                 ->Int32Value(context)
1206
122
                 .ToChecked();
1207
61
  uint32_t id = options->Get(iso, HostDefinedOptions::kID)
1208
122
                    .As<Number>()
1209
183
                    ->Uint32Value(context)
1210
122
                    .ToChecked();
1211
61
  if (type == ScriptType::kScript) {
1212
3
    contextify::ContextifyScript* wrap = env->id_to_script_map.find(id)->second;
1213
6
    object = wrap->object();
1214
58
  } else if (type == ScriptType::kModule) {
1215
38
    ModuleWrap* wrap = ModuleWrap::GetFromID(env, id);
1216
76
    object = wrap->object();
1217
20
  } else if (type == ScriptType::kFunction) {
1218
20
    auto it = env->id_to_function_map.find(id);
1219
20
    CHECK_NE(it, env->id_to_function_map.end());
1220
40
    object = it->second->object();
1221
  } else {
1222
    UNREACHABLE();
1223
  }
1224
1225
  Local<Value> import_args[] = {
1226
    object,
1227
    Local<Value>(specifier),
1228
122
  };
1229
1230
  Local<Value> result;
1231
122
  if (import_callback->Call(
1232
        context,
1233
        v8::Undefined(iso),
1234
61
        arraysize(import_args),
1235
183
        import_args).ToLocal(&result)) {
1236
61
    CHECK(result->IsPromise());
1237
61
    return handle_scope.Escape(result.As<Promise>());
1238
  }
1239
1240
  return MaybeLocal<Promise>();
1241
}
1242
1243
109
void ModuleWrap::SetImportModuleDynamicallyCallback(
1244
    const FunctionCallbackInfo<Value>& args) {
1245
109
  Isolate* iso = args.GetIsolate();
1246
109
  Environment* env = Environment::GetCurrent(args);
1247
109
  HandleScope handle_scope(iso);
1248
1249
109
  CHECK_EQ(args.Length(), 1);
1250
218
  CHECK(args[0]->IsFunction());
1251
218
  Local<Function> import_callback = args[0].As<Function>();
1252
109
  env->set_host_import_module_dynamically_callback(import_callback);
1253
1254
109
  iso->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
1255
109
}
1256
1257
176
void ModuleWrap::HostInitializeImportMetaObjectCallback(
1258
    Local<Context> context, Local<Module> module, Local<Object> meta) {
1259
176
  Environment* env = Environment::GetCurrent(context);
1260
176
  CHECK_NOT_NULL(env);  // TODO(addaleax): Handle nullptr here.
1261
176
  ModuleWrap* module_wrap = GetFromModule(env, module);
1262
1263
176
  if (module_wrap == nullptr) {
1264
176
    return;
1265
  }
1266
1267
176
  Local<Object> wrap = module_wrap->object();
1268
  Local<Function> callback =
1269
176
      env->host_initialize_import_meta_object_callback();
1270
528
  Local<Value> args[] = { wrap, meta };
1271
528
  callback->Call(context, Undefined(env->isolate()), arraysize(args), args)
1272
352
      .ToLocalChecked();
1273
}
1274
1275
109
void ModuleWrap::SetInitializeImportMetaObjectCallback(
1276
    const FunctionCallbackInfo<Value>& args) {
1277
109
  Environment* env = Environment::GetCurrent(args);
1278
109
  Isolate* isolate = env->isolate();
1279
1280
109
  CHECK_EQ(args.Length(), 1);
1281
218
  CHECK(args[0]->IsFunction());
1282
218
  Local<Function> import_meta_callback = args[0].As<Function>();
1283
109
  env->set_host_initialize_import_meta_object_callback(import_meta_callback);
1284
1285
  isolate->SetHostInitializeImportMetaObjectCallback(
1286
109
      HostInitializeImportMetaObjectCallback);
1287
109
}
1288
1289
5078
void ModuleWrap::Initialize(Local<Object> target,
1290
                            Local<Value> unused,
1291
                            Local<Context> context,
1292
                            void* priv) {
1293
5078
  Environment* env = Environment::GetCurrent(context);
1294
5078
  Isolate* isolate = env->isolate();
1295
1296
5078
  Local<FunctionTemplate> tpl = env->NewFunctionTemplate(New);
1297
10156
  tpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"));
1298
10156
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
1299
1300
5078
  env->SetProtoMethod(tpl, "link", Link);
1301
5078
  env->SetProtoMethod(tpl, "instantiate", Instantiate);
1302
5078
  env->SetProtoMethod(tpl, "evaluate", Evaluate);
1303
5078
  env->SetProtoMethodNoSideEffect(tpl, "namespace", Namespace);
1304
5078
  env->SetProtoMethodNoSideEffect(tpl, "getStatus", GetStatus);
1305
5078
  env->SetProtoMethodNoSideEffect(tpl, "getError", GetError);
1306
  env->SetProtoMethodNoSideEffect(tpl, "getStaticDependencySpecifiers",
1307
5078
                                  GetStaticDependencySpecifiers);
1308
1309
  target->Set(env->context(), FIXED_ONE_BYTE_STRING(isolate, "ModuleWrap"),
1310
25390
              tpl->GetFunction(context).ToLocalChecked()).Check();
1311
5078
  env->SetMethod(target, "resolve", Resolve);
1312
5078
  env->SetMethod(target, "getPackageType", GetPackageType);
1313
  env->SetMethod(target,
1314
                 "setImportModuleDynamicallyCallback",
1315
5078
                 SetImportModuleDynamicallyCallback);
1316
  env->SetMethod(target,
1317
                 "setInitializeImportMetaObjectCallback",
1318
5078
                 SetInitializeImportMetaObjectCallback);
1319
1320
#define V(name)                                                                \
1321
    target->Set(context,                                                       \
1322
      FIXED_ONE_BYTE_STRING(env->isolate(), #name),                            \
1323
      Integer::New(env->isolate(), Module::Status::name))                      \
1324
        .FromJust()
1325
20312
    V(kUninstantiated);
1326
20312
    V(kInstantiating);
1327
20312
    V(kInstantiated);
1328
20312
    V(kEvaluating);
1329
20312
    V(kEvaluated);
1330
20312
    V(kErrored);
1331
#undef V
1332
5078
}
1333
1334
}  // namespace loader
1335
}  // namespace node
1336
1337
4947
NODE_MODULE_CONTEXT_AWARE_INTERNAL(module_wrap,
1338
                                   node::loader::ModuleWrap::Initialize)