runtime-module.cc 2.08 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/execution/arguments-inl.h"
6
#include "src/logging/counters.h"
7
#include "src/objects/js-promise.h"
8
#include "src/objects/objects-inl.h"
9
#include "src/objects/source-text-module.h"
10
#include "src/runtime/runtime-utils.h"
11 12 13 14

namespace v8 {
namespace internal {

15
namespace {
16
Handle<Script> GetEvalOrigin(Isolate* isolate, Script origin_script) {
17 18 19
  DisallowGarbageCollection no_gc;
  while (origin_script.has_eval_from_shared()) {
    HeapObject maybe_script = origin_script.eval_from_shared().script();
20
    CHECK(maybe_script.IsScript());
21 22 23 24 25 26
    origin_script = Script::cast(maybe_script);
  }
  return handle(origin_script, isolate);
}
}  // namespace

27 28
RUNTIME_FUNCTION(Runtime_DynamicImportCall) {
  HandleScope scope(isolate);
29 30
  DCHECK_LE(2, args.length());
  DCHECK_GE(3, args.length());
31 32
  Handle<JSFunction> function = args.at<JSFunction>(0);
  Handle<Object> specifier = args.at(1);
33

34 35 36 37 38
  MaybeHandle<Object> import_assertions;
  if (args.length() == 3) {
    import_assertions = args.at<Object>(2);
  }

39
  Handle<Script> referrer_script =
40
      GetEvalOrigin(isolate, Script::cast(function->shared().script()));
41 42
  RETURN_RESULT_OR_FAILURE(isolate,
                           isolate->RunHostImportModuleDynamicallyCallback(
43
                               referrer_script, specifier, import_assertions));
44 45
}

46 47
RUNTIME_FUNCTION(Runtime_GetModuleNamespace) {
  HandleScope scope(isolate);
48
  DCHECK_EQ(1, args.length());
49
  int module_request = args.smi_value_at(0);
50 51
  Handle<SourceTextModule> module(isolate->context().module(), isolate);
  return *SourceTextModule::GetModuleNamespace(isolate, module, module_request);
52 53
}

54 55 56
RUNTIME_FUNCTION(Runtime_GetImportMetaObject) {
  HandleScope scope(isolate);
  DCHECK_EQ(0, args.length());
57
  Handle<SourceTextModule> module(isolate->context().module(), isolate);
58 59
  RETURN_RESULT_OR_FAILURE(isolate,
                           SourceTextModule::GetImportMeta(isolate, module));
60 61
}

62 63
}  // namespace internal
}  // namespace v8