forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathJSModuleRecord.cpp
More file actions
198 lines (172 loc) · 8.78 KB
/
Copy pathJSModuleRecord.cpp
File metadata and controls
198 lines (172 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (C) 2015-2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JSModuleRecord.h"
#include "BuiltinNames.h"
#include "Interpreter.h"
#include "JSAsyncFunction.h"
#include "JSAsyncGeneratorFunction.h"
#include "JSCInlines.h"
#include "JSGeneratorFunction.h"
#include "JSModuleEnvironment.h"
#include "JSModuleLoader.h"
#include "JSModuleNamespaceObject.h"
#include "JSPromise.h"
#include "ModuleProgramExecutable.h"
#include "SourceProfiler.h"
#include "UnlinkedModuleProgramCodeBlock.h"
#include "VariableEnvironmentInlines.h"
#include <wtf/text/MakeString.h>
namespace JSC {
const ClassInfo JSModuleRecord::s_info = { "ModuleRecord"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSModuleRecord) };
JSModuleRecord* JSModuleRecord::create(JSGlobalObject* globalObject, VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables, CodeFeatures features)
{
JSModuleRecord* instance = new (NotNull, allocateCell<JSModuleRecord>(vm)) JSModuleRecord(vm, structure, moduleKey, sourceCode, declaredVariables, lexicalVariables, features);
instance->finishCreation(globalObject, vm);
return instance;
}
JSModuleRecord::JSModuleRecord(VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables, CodeFeatures features)
: Base(vm, structure, moduleKey)
, m_sourceCode(sourceCode)
, m_declaredVariables(declaredVariables)
, m_lexicalVariables(lexicalVariables)
, m_features(features)
{
}
void JSModuleRecord::destroy(JSCell* cell)
{
JSModuleRecord* thisObject = static_cast<JSModuleRecord*>(cell);
thisObject->JSModuleRecord::~JSModuleRecord();
}
void JSModuleRecord::finishCreation(JSGlobalObject* globalObject, VM& vm)
{
Base::finishCreation(globalObject, vm);
ASSERT(inherits(info()));
}
#if USE(BUN_JSC_ADDITIONS)
size_t JSModuleRecord::estimatedSize(JSCell* cell, VM& vm)
{
const auto& thisObject = uncheckedDowncast<JSModuleRecord>(cell);
size_t size = Base::estimatedSize(cell, vm);
const SourceCode& sourceCode = thisObject->sourceCode();
StringView view = sourceCode.provider() ? sourceCode.provider()->source() : StringView();
size += view.length() * (view.is8Bit() ? sizeof(Latin1Character) : sizeof(UChar));
size += sourceCode.memoryCost();
return size;
}
#endif
template<typename Visitor>
void JSModuleRecord::visitChildrenImpl(JSCell* cell, Visitor& visitor)
{
JSModuleRecord* thisObject = uncheckedDowncast<JSModuleRecord>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
visitor.append(thisObject->m_moduleProgramExecutable);
#if USE(BUN_JSC_ADDITIONS)
visitor.reportExtraMemoryVisited(thisObject->sourceCode().memoryCost());
#endif
}
DEFINE_VISIT_CHILDREN(JSModuleRecord);
JSValue JSModuleRecord::evaluate(JSGlobalObject* globalObject, JSValue sentValue, JSValue resumeMode)
{
if (!m_moduleProgramExecutable) {
ASSERT_NOT_REACHED_WITH_MESSAGE("Can't evaluate a JSModuleRecord that has no executable");
return jsUndefined();
}
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
// A TLA body suspended at an await (State != Init) must resume even if a
// sibling in the same cycle set our graph-level evaluationError meanwhile;
// asyncExecution{Fulfilled,Rejected} already no-op when status == Evaluated.
JSValue state = internalField(Field::State).get();
if (state.isNumber() && state.asInt32AsAnyInt() == std::to_underlying(State::Init)) {
if (JSValue error = evaluationError()) {
scope.throwException(globalObject, error);
return { };
}
}
ModuleProgramExecutable* executable = m_moduleProgramExecutable.get();
JSValue resultOrAwaitedValue = vm.interpreter.executeModuleProgram(this, executable, globalObject, moduleEnvironment(), sentValue, resumeMode);
RETURN_IF_EXCEPTION(scope, { });
if (JSValue state = internalField(Field::State).get(); !state.isNumber() || state.asInt32AsAnyInt() == std::to_underlying(State::Executing))
m_moduleProgramExecutable.clear();
RELEASE_AND_RETURN(scope, resultOrAwaitedValue);
}
void JSModuleRecord::execute(JSGlobalObject* globalObject, JSPromise* capability)
{
// ExecuteModule([capability])
// https://tc39.es/ecma262/#sec-source-text-module-record-execute-module
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
// 1. Let moduleContext be a new ECMAScript code execution context.
// 2. Set the Function of moduleContext to null.
// 3. Set the Realm of moduleContext to module.[[Realm]].
// 4. Set the ScriptOrModule of moduleContext to module.
// 5. Assert: module has been linked and declarations in its module environment have been instantiated.
ASSERT(static_cast<int>(status()) >= static_cast<int>(Status::Linked));
// 6. Set the VariableEnvironment of moduleContext to module.[[Environment]].
// 7. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
// 8. Suspend the running execution context.
// 9. If module.[[HasTLA]] is false, then
if (!hasTLA()) {
// 9.a. Assert: capability is not present.
ASSERT(capability == nullptr);
// 9.b. Push moduleContext onto the execution context stack; moduleContext is now the running execution context.
// 9.c. Let result be Completion(Evaluation of module.[[ECMAScriptCode]]).
globalObject->moduleLoader()->evaluate(globalObject, identifierToJSValue(vm, moduleKey()), this, nullptr, jsUndefined(), jsNumber(static_cast<int32_t>(ResumeMode::NormalMode)));
// 9.d. Suspend moduleContext and remove it from the execution context stack.
// 9.e. Resume the context that is now on the top of the execution context stack as the running execution context.
// 9.f. If result is an abrupt completion, then
// 9.f.i. Return ? result.
RETURN_IF_EXCEPTION(scope, void());
// 10. Else,
} else {
// 10.a. Assert: capability is a PromiseCapability Record.
ASSERT(capability != nullptr);
// 10.b. Perform AsyncBlockStart(capability, module.[[ECMAScriptCode]], moduleContext).
asyncCapability(vm, capability);
JSValue result = globalObject->moduleLoader()->evaluate(globalObject, identifierToJSValue(vm, moduleKey()), this, nullptr, jsUndefined(), jsNumber(static_cast<int32_t>(ResumeMode::NormalMode)));
if (scope.exception())
capability->rejectWithCaughtException(vm, scope);
else if (JSValue state = internalField(Field::State).get(); !state.isNumber() || state.asInt32AsAnyInt() == std::to_underlying(State::Executing))
capability->resolve(globalObject, vm, result);
else
JSPromise::resolveWithInternalMicrotaskForAsyncAwait(globalObject, vm, result, InternalMicrotask::AsyncModuleExecutionResume, this);
}
// 11. Return unused.
}
ModuleProgramExecutable* JSModuleRecord::getOrMakeExecutable(JSGlobalObject* globalObject)
{
ModuleProgramExecutable* executable = m_moduleProgramExecutable.get();
if (executable)
return executable;
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
executable = ModuleProgramExecutable::tryCreate(globalObject, sourceCode());
RETURN_IF_EXCEPTION(scope, nullptr);
m_moduleProgramExecutable.set(vm, this, executable);
RELEASE_AND_RETURN(scope, executable);
}
} // namespace JSC