v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
synthetic-module.cc
Go to the documentation of this file.
1// Copyright 2019 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
6
7#include "src/api/api-inl.h"
14#include "src/utils/ostreams.h"
15
16namespace v8 {
17namespace internal {
18
19// Implements SetSyntheticModuleBinding:
20// https://heycam.github.io/webidl/#setsyntheticmoduleexport
23 DirectHandle<String> export_name,
24 DirectHandle<Object> export_value) {
25 DirectHandle<ObjectHashTable> exports(module->exports(), isolate);
26 DirectHandle<Object> export_object(exports->Lookup(export_name), isolate);
27
28 if (!IsCell(*export_object)) {
29 isolate->Throw(*isolate->factory()->NewReferenceError(
30 MessageTemplate::kModuleExportUndefined, export_name));
31 return Nothing<bool>();
32 }
33
34 // Spec step 2: Set the mutable binding of export_name to export_value
35 Cast<Cell>(*export_object)->set_value(*export_value);
36
37 return Just(true);
38}
39
42 DirectHandle<String> export_name,
43 DirectHandle<Object> export_value) {
44 DirectHandle<ObjectHashTable> exports(module->exports(), isolate);
45 DirectHandle<Object> export_object(exports->Lookup(export_name), isolate);
46 CHECK(IsCell(*export_object));
47 Maybe<bool> set_export_result =
48 SetExport(isolate, module, export_name, export_value);
49 CHECK(set_export_result.FromJust());
50}
51
52// Implements Synthetic Module Record's ResolveExport concrete method:
53// https://heycam.github.io/webidl/#smr-resolveexport
56 DirectHandle<String> module_specifier, DirectHandle<String> export_name,
57 MessageLocation loc, bool must_resolve) {
58 Handle<Object> object(module->exports()->Lookup(export_name), isolate);
59 if (IsCell(*object)) return Cast<Cell>(object);
60
61 if (!must_resolve) return kNullMaybeHandle;
62
63 isolate->ThrowAt(
64 isolate->factory()->NewSyntaxError(MessageTemplate::kUnresolvableExport,
65 module_specifier, export_name),
66 &loc);
67 return kNullMaybeHandle;
68}
69
70// Implements Synthetic Module Record's Instantiate concrete method :
71// https://heycam.github.io/webidl/#smr-instantiate
74 v8::Local<v8::Context> context) {
75 Handle<ObjectHashTable> exports(module->exports(), isolate);
76 DirectHandle<FixedArray> export_names(module->export_names(), isolate);
77 // Spec step 7: For each export_name in module->export_names...
78 for (int i = 0, n = export_names->length(); i < n; ++i) {
79 // Spec step 7.1: Create a new mutable binding for export_name.
80 // Spec step 7.2: Initialize the new mutable binding to undefined.
81 DirectHandle<Cell> cell = isolate->factory()->NewCell();
82 DirectHandle<String> name(Cast<String>(export_names->get(i)), isolate);
83 CHECK(IsTheHole(exports->Lookup(name), isolate));
84 exports = ObjectHashTable::Put(exports, name, cell);
85 }
86 module->set_exports(*exports);
87 return true;
88}
89
90// Second step of module instantiation. No real work to do for SyntheticModule
91// as there are no imports or indirect exports to resolve;
92// just update status.
95 module->SetStatus(kLinked);
96 return true;
97}
98
99// Implements Synthetic Module Record's Evaluate concrete method:
100// https://heycam.github.io/webidl/#smr-evaluate
102 Isolate* isolate, DirectHandle<SyntheticModule> module) {
103 module->SetStatus(kEvaluating);
104
107 module->evaluation_steps()->foreign_address<kSyntheticModuleTag>());
109 if (!evaluation_steps(Utils::ToLocal(isolate->native_context()),
110 Utils::ToLocal(Cast<Module>(module)))
111 .ToLocal(&result)) {
112 module->RecordError(isolate, isolate->exception());
114 }
115
116 module->SetStatus(kEvaluated);
117
118 DirectHandle<Object> result_from_callback = Utils::OpenDirectHandle(*result);
119
120 DirectHandle<JSPromise> capability;
121 if (IsJSPromise(*result_from_callback)) {
122 capability = Cast<JSPromise>(result_from_callback);
123 } else {
124 // The host's evaluation steps should have returned a resolved Promise,
125 // but as an allowance to hosts that have not yet finished the migration
126 // to top-level await, create a Promise if the callback result didn't give
127 // us one.
128 capability = isolate->factory()->NewJSPromise();
129 JSPromise::Resolve(capability, isolate->factory()->undefined_value())
130 .ToHandleChecked();
131 }
132
133 module->set_top_level_capability(*capability);
134
135 return result_from_callback;
136}
137
138} // namespace internal
139} // namespace v8
V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local< S > *out) const
V8_INLINE T FromJust() const &
Definition v8-maybe.h:64
static v8::internal::DirectHandle< To > OpenDirectHandle(v8::Local< From > handle)
Definition api.h:279
static V8_WARN_UNUSED_RESULT MaybeHandle< Object > Resolve(DirectHandle< JSPromise > promise, DirectHandle< Object > resolution)
Definition objects.cc:5109
static void SetExportStrict(Isolate *isolate, DirectHandle< SyntheticModule > module, DirectHandle< String > export_name, DirectHandle< Object > export_value)
static V8_WARN_UNUSED_RESULT bool PrepareInstantiate(Isolate *isolate, DirectHandle< SyntheticModule > module, v8::Local< v8::Context > context)
static V8_WARN_UNUSED_RESULT MaybeHandle< Cell > ResolveExport(Isolate *isolate, DirectHandle< SyntheticModule > module, DirectHandle< String > module_specifier, DirectHandle< String > export_name, MessageLocation loc, bool must_resolve)
static NEVER_READ_ONLY_SPACE Maybe< bool > SetExport(Isolate *isolate, DirectHandle< SyntheticModule > module, DirectHandle< String > export_name, DirectHandle< Object > export_value)
static V8_WARN_UNUSED_RESULT bool FinishInstantiate(Isolate *isolate, DirectHandle< SyntheticModule > module)
static V8_WARN_UNUSED_RESULT MaybeDirectHandle< Object > Evaluate(Isolate *isolate, DirectHandle< SyntheticModule > module)
ZoneVector< RpoNumber > & result
constexpr NullMaybeHandleType kNullMaybeHandle
F FUNCTION_CAST(uint8_t *addr)
Definition globals.h:717
too high values may cause the compiler to set high thresholds for inlining to as much as possible avoid inlined allocation of objects that cannot escape trace load stores from virtual maglev objects use TurboFan fast string builder analyze liveness of environment slots and zap dead values trace TurboFan load elimination emit data about basic block usage in builtins to this enable builtin reordering when run mksnapshot flag for emit warnings when applying builtin profile data verify register allocation in TurboFan randomly schedule instructions to stress dependency tracking enable store store elimination in TurboFan rewrite far to near simulate GC compiler thread race related to allow float parameters to be passed in simulator mode JS Wasm Run additional turbo_optimize_inlined_js_wasm_wrappers enable experimental feedback collection in generic lowering enable Turboshaft s WasmLoadElimination enable Turboshaft s low level load elimination for JS enable Turboshaft s escape analysis for string concatenation use enable Turbolev features that we want to ship in the not too far future trace individual Turboshaft reduction steps trace intermediate Turboshaft reduction steps invocation count threshold for early optimization Enables optimizations which favor memory size over execution speed Enables sampling allocation profiler with X as a sample interval min size of a semi the new space consists of two semi spaces max size of the Collect garbage after Collect garbage after keeps maps alive for< n > old space garbage collections print one detailed trace line in name
Definition flags.cc:2086
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
Maybe< T > Nothing()
Definition v8-maybe.h:112
Maybe< T > Just(const T &t)
Definition v8-maybe.h:117
#define CHECK(condition)
Definition logging.h:124