1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
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 namespace v8 {
13 namespace internal {
14 
15 namespace {
GetEvalOrigin(Isolate * isolate,Script origin_script)16 Handle<Script> GetEvalOrigin(Isolate* isolate, Script origin_script) {
17   DisallowGarbageCollection no_gc;
18   while (origin_script.has_eval_from_shared()) {
19     HeapObject maybe_script = origin_script.eval_from_shared().script();
20     CHECK(maybe_script.IsScript());
21     origin_script = Script::cast(maybe_script);
22   }
23   return handle(origin_script, isolate);
24 }
25 }  // namespace
26 
RUNTIME_FUNCTION(Runtime_DynamicImportCall)27 RUNTIME_FUNCTION(Runtime_DynamicImportCall) {
28   HandleScope scope(isolate);
29   DCHECK_LE(2, args.length());
30   DCHECK_GE(3, args.length());
31   CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
32   CONVERT_ARG_HANDLE_CHECKED(Object, specifier, 1);
33 
34   MaybeHandle<Object> import_assertions;
35   if (args.length() == 3) {
36     CHECK(args[2].IsObject());
37     import_assertions = args.at<Object>(2);
38   }
39 
40   Handle<Script> referrer_script =
41       GetEvalOrigin(isolate, Script::cast(function->shared().script()));
42   RETURN_RESULT_OR_FAILURE(isolate,
43                            isolate->RunHostImportModuleDynamicallyCallback(
44                                referrer_script, specifier, import_assertions));
45 }
46 
RUNTIME_FUNCTION(Runtime_GetModuleNamespace)47 RUNTIME_FUNCTION(Runtime_GetModuleNamespace) {
48   HandleScope scope(isolate);
49   DCHECK_EQ(1, args.length());
50   CONVERT_SMI_ARG_CHECKED(module_request, 0);
51   Handle<SourceTextModule> module(isolate->context().module(), isolate);
52   return *SourceTextModule::GetModuleNamespace(isolate, module, module_request);
53 }
54 
RUNTIME_FUNCTION(Runtime_GetImportMetaObject)55 RUNTIME_FUNCTION(Runtime_GetImportMetaObject) {
56   HandleScope scope(isolate);
57   DCHECK_EQ(0, args.length());
58   Handle<SourceTextModule> module(isolate->context().module(), isolate);
59   RETURN_RESULT_OR_FAILURE(isolate,
60                            SourceTextModule::GetImportMeta(isolate, module));
61 }
62 
63 }  // namespace internal
64 }  // namespace v8
65