-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Completion.cpp
276 lines (229 loc) · 11.3 KB
/
Completion.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2003-2019 Apple Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "Completion.h"
#include "BytecodeCacheError.h"
#include "CatchScope.h"
#include "CodeCache.h"
#include "Exception.h"
#include "IdentifierInlines.h"
#include "Interpreter.h"
#include "JSGlobalObject.h"
#include "JSInternalPromise.h"
#include "JSLock.h"
#include "JSModuleLoader.h"
#include "JSWithScope.h"
#include "ModuleAnalyzer.h"
#include "Parser.h"
#include "ScriptProfilingScope.h"
namespace JSC {
static inline bool checkSyntaxInternal(VM& vm, const SourceCode& source, ParserError& error)
{
return !!parse<ProgramNode>(
vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin,
JSParserStrictMode::NotStrict, JSParserScriptMode::Classic, SourceParseMode::ProgramMode, SuperBinding::NotNeeded, error);
}
bool checkSyntax(JSGlobalObject* globalObject, const SourceCode& source, JSValue* returnedException)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
ParserError error;
if (checkSyntaxInternal(vm, source, error))
return true;
ASSERT(error.isValid());
if (returnedException)
*returnedException = error.toErrorObject(globalObject, source);
return false;
}
bool checkSyntax(VM& vm, const SourceCode& source, ParserError& error)
{
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
return checkSyntaxInternal(vm, source, error);
}
bool checkModuleSyntax(JSGlobalObject* globalObject, const SourceCode& source, ParserError& error)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
std::unique_ptr<ModuleProgramNode> moduleProgramNode = parse<ModuleProgramNode>(
vm, source, Identifier(), JSParserBuiltinMode::NotBuiltin,
JSParserStrictMode::Strict, JSParserScriptMode::Module, SourceParseMode::ModuleAnalyzeMode, SuperBinding::NotNeeded, error);
if (!moduleProgramNode)
return false;
PrivateName privateName(PrivateName::Description, "EntryPointModule");
ModuleAnalyzer moduleAnalyzer(globalObject, Identifier::fromUid(privateName), source, moduleProgramNode->varDeclarations(), moduleProgramNode->lexicalVariables());
moduleAnalyzer.analyze(*moduleProgramNode);
return true;
}
RefPtr<CachedBytecode> generateProgramBytecode(VM& vm, const SourceCode& source, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error)
{
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
JSParserStrictMode strictMode = JSParserStrictMode::NotStrict;
JSParserScriptMode scriptMode = JSParserScriptMode::Classic;
EvalContextType evalContextType = EvalContextType::None;
ParserError parserError;
UnlinkedCodeBlock* unlinkedCodeBlock = recursivelyGenerateUnlinkedCodeBlockForProgram(vm, source, strictMode, scriptMode, { }, parserError, evalContextType);
if (parserError.isValid())
error = parserError;
if (!unlinkedCodeBlock)
return nullptr;
return serializeBytecode(vm, unlinkedCodeBlock, source, SourceCodeType::ProgramType, strictMode, scriptMode, fd, error, { });
}
RefPtr<CachedBytecode> generateModuleBytecode(VM& vm, const SourceCode& source, FileSystem::PlatformFileHandle fd, BytecodeCacheError& error)
{
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
JSParserStrictMode strictMode = JSParserStrictMode::Strict;
JSParserScriptMode scriptMode = JSParserScriptMode::Module;
EvalContextType evalContextType = EvalContextType::None;
ParserError parserError;
UnlinkedCodeBlock* unlinkedCodeBlock = recursivelyGenerateUnlinkedCodeBlockForModuleProgram(vm, source, strictMode, scriptMode, { }, parserError, evalContextType);
if (parserError.isValid())
error = parserError;
if (!unlinkedCodeBlock)
return nullptr;
return serializeBytecode(vm, unlinkedCodeBlock, source, SourceCodeType::ModuleType, strictMode, scriptMode, fd, error, { });
}
JSValue evaluate(JSGlobalObject* globalObject, const SourceCode& source, JSValue thisValue, NakedPtr<Exception>& returnedException)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
auto scope = DECLARE_CATCH_SCOPE(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
if (!thisValue || thisValue.isUndefinedOrNull())
thisValue = globalObject;
JSObject* thisObj = jsCast<JSObject*>(thisValue.toThis(globalObject, ECMAMode::sloppy()));
JSValue result = vm.interpreter->executeProgram(source, globalObject, thisObj);
if (scope.exception()) {
returnedException = scope.exception();
scope.clearException();
return jsUndefined();
}
RELEASE_ASSERT(result);
return result;
}
JSValue profiledEvaluate(JSGlobalObject* globalObject, ProfilingReason reason, const SourceCode& source, JSValue thisValue, NakedPtr<Exception>& returnedException)
{
ScriptProfilingScope profilingScope(globalObject, reason);
return evaluate(globalObject, source, thisValue, returnedException);
}
JSValue evaluateWithScopeExtension(JSGlobalObject* globalObject, const SourceCode& source, JSObject* scopeExtensionObject, NakedPtr<Exception>& returnedException)
{
VM& vm = globalObject->vm();
if (scopeExtensionObject) {
JSScope* ignoredPreviousScope = globalObject->globalScope();
globalObject->setGlobalScopeExtension(JSWithScope::create(vm, globalObject, ignoredPreviousScope, scopeExtensionObject));
}
JSValue returnValue = JSC::evaluate(globalObject, source, globalObject, returnedException);
if (scopeExtensionObject)
globalObject->clearGlobalScopeExtension();
return returnValue;
}
static Symbol* createSymbolForEntryPointModule(VM& vm)
{
// Generate the unique key for the source-provided module.
PrivateName privateName(PrivateName::Description, "EntryPointModule");
return Symbol::create(vm, privateName.uid());
}
static JSInternalPromise* rejectPromise(JSGlobalObject* globalObject)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_CATCH_SCOPE(vm);
scope.assertNoException();
JSValue exception = scope.exception()->value();
scope.clearException();
JSInternalPromise* promise = JSInternalPromise::create(vm, globalObject->internalPromiseStructure());
promise->reject(globalObject, exception);
return promise;
}
JSInternalPromise* loadAndEvaluateModule(JSGlobalObject* globalObject, Symbol* moduleId, JSValue parameters, JSValue scriptFetcher)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
return globalObject->moduleLoader()->loadAndEvaluateModule(globalObject, moduleId, parameters, scriptFetcher);
}
JSInternalPromise* loadAndEvaluateModule(JSGlobalObject* globalObject, const String& moduleName, JSValue parameters, JSValue scriptFetcher)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
return globalObject->moduleLoader()->loadAndEvaluateModule(globalObject, identifierToJSValue(vm, Identifier::fromString(vm, moduleName)), parameters, scriptFetcher);
}
JSInternalPromise* loadAndEvaluateModule(JSGlobalObject* globalObject, const SourceCode& source, JSValue scriptFetcher)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
auto scope = DECLARE_THROW_SCOPE(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
Symbol* key = createSymbolForEntryPointModule(vm);
// Insert the given source code to the ModuleLoader registry as the fetched registry entry.
globalObject->moduleLoader()->provideFetch(globalObject, key, source);
RETURN_IF_EXCEPTION(scope, rejectPromise(globalObject));
return globalObject->moduleLoader()->loadAndEvaluateModule(globalObject, key, jsUndefined(), scriptFetcher);
}
JSInternalPromise* loadModule(JSGlobalObject* globalObject, const String& moduleName, JSValue parameters, JSValue scriptFetcher)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
return globalObject->moduleLoader()->loadModule(globalObject, identifierToJSValue(vm, Identifier::fromString(vm, moduleName)), parameters, scriptFetcher);
}
JSInternalPromise* loadModule(JSGlobalObject* globalObject, const SourceCode& source, JSValue scriptFetcher)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
auto scope = DECLARE_THROW_SCOPE(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
Symbol* key = createSymbolForEntryPointModule(vm);
// Insert the given source code to the ModuleLoader registry as the fetched registry entry.
// FIXME: Introduce JSSourceCode object to wrap around this source.
globalObject->moduleLoader()->provideFetch(globalObject, key, source);
RETURN_IF_EXCEPTION(scope, rejectPromise(globalObject));
return globalObject->moduleLoader()->loadModule(globalObject, key, jsUndefined(), scriptFetcher);
}
JSValue linkAndEvaluateModule(JSGlobalObject* globalObject, const Identifier& moduleKey, JSValue scriptFetcher)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
return globalObject->moduleLoader()->linkAndEvaluateModule(globalObject, identifierToJSValue(vm, moduleKey), scriptFetcher);
}
JSInternalPromise* importModule(JSGlobalObject* globalObject, const Identifier& moduleKey, JSValue parameters, JSValue scriptFetcher)
{
VM& vm = globalObject->vm();
JSLockHolder lock(vm);
RELEASE_ASSERT(vm.atomStringTable() == Thread::current().atomStringTable());
RELEASE_ASSERT(!vm.isCollectorBusyOnCurrentThread());
return globalObject->moduleLoader()->requestImportModule(globalObject, moduleKey, parameters, scriptFetcher);
}
} // namespace JSC