Lines 32-37
a/Source/WebCore/bindings/js/WorkerScriptController.cpp_sec1
|
32 |
#include "JSEventTarget.h" |
32 |
#include "JSEventTarget.h" |
33 |
#include "JSExecState.h" |
33 |
#include "JSExecState.h" |
34 |
#include "JSServiceWorkerGlobalScope.h" |
34 |
#include "JSServiceWorkerGlobalScope.h" |
|
|
35 |
#include "LoadableModuleScript.h" |
36 |
#include "ModuleFetchParameters.h" |
35 |
#include "ScriptSourceCode.h" |
37 |
#include "ScriptSourceCode.h" |
36 |
#include "WebCoreJSClientData.h" |
38 |
#include "WebCoreJSClientData.h" |
37 |
#include "WorkerConsoleClient.h" |
39 |
#include "WorkerConsoleClient.h" |
Lines 174-179
void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, NakedP
a/Source/WebCore/bindings/js/WorkerScriptController.cpp_sec2
|
174 |
} |
176 |
} |
175 |
} |
177 |
} |
176 |
|
178 |
|
|
|
179 |
MessageQueueWaitResult WorkerScriptController::loadModuleSynchronously(LoadableModuleScript& moduleScript, const ScriptSourceCode& sourceCode) |
180 |
{ |
181 |
if (isExecutionForbidden()) |
182 |
return MessageQueueTerminated; |
183 |
|
184 |
initScriptIfNeeded(); |
185 |
|
186 |
auto& lexicalGlobalObject = *m_workerGlobalScopeWrapper.get(); |
187 |
VM& vm = lexicalGlobalObject.vm(); |
188 |
JSLockHolder lock { vm }; |
189 |
|
190 |
auto& promise = JSExecState::loadModule(lexicalGlobalObject, sourceCode.jsSourceCode(), JSC::JSScriptFetcher::create(vm, { &moduleScript })); |
191 |
|
192 |
RefPtr<LoadableModuleScript> moduleScriptRef(&moduleScript); |
193 |
|
194 |
auto& fulfillHandler = *JSNativeStdFunction::create(vm, &lexicalGlobalObject, 1, String(), [moduleScriptRef](JSGlobalObject* globalObject, CallFrame* callFrame) -> JSC::EncodedJSValue { |
195 |
VM& vm = globalObject->vm(); |
196 |
auto scope = DECLARE_THROW_SCOPE(vm); |
197 |
Identifier moduleKey = jsValueToModuleKey(globalObject, callFrame->argument(0)); |
198 |
RETURN_IF_EXCEPTION(scope, { }); |
199 |
moduleScriptRef->notifyLoadCompleted(*moduleKey.impl()); |
200 |
return JSValue::encode(jsUndefined()); |
201 |
}); |
202 |
|
203 |
auto& rejectHandler = *JSNativeStdFunction::create(vm, &lexicalGlobalObject, 1, String(), [moduleScriptRef](JSGlobalObject* globalObject, CallFrame* callFrame) { |
204 |
VM& vm = globalObject->vm(); |
205 |
JSValue errorValue = callFrame->argument(0); |
206 |
if (errorValue.isObject()) { |
207 |
auto* object = JSC::asObject(errorValue); |
208 |
if (JSValue failureKindValue = object->getDirect(vm, static_cast<JSVMClientData&>(*vm.clientData).builtinNames().failureKindPrivateName())) { |
209 |
// This is host propagated error in the module loader pipeline. |
210 |
switch (static_cast<ModuleFetchFailureKind>(failureKindValue.asInt32())) { |
211 |
case ModuleFetchFailureKind::WasErrored: |
212 |
moduleScriptRef->notifyLoadFailed(LoadableScript::Error { |
213 |
LoadableScript::ErrorType::CachedScript, |
214 |
WTF::nullopt |
215 |
}); |
216 |
break; |
217 |
case ModuleFetchFailureKind::WasCanceled: |
218 |
moduleScriptRef->notifyLoadWasCanceled(); |
219 |
break; |
220 |
} |
221 |
return JSValue::encode(jsUndefined()); |
222 |
} |
223 |
} |
224 |
|
225 |
auto scope = DECLARE_CATCH_SCOPE(vm); |
226 |
moduleScriptRef->notifyLoadFailed(LoadableScript::Error { |
227 |
LoadableScript::ErrorType::CachedScript, |
228 |
LoadableScript::ConsoleMessage { |
229 |
MessageSource::JS, |
230 |
MessageLevel::Error, |
231 |
retrieveErrorMessage(*globalObject, vm, errorValue, scope), |
232 |
} |
233 |
}); |
234 |
return JSValue::encode(jsUndefined()); |
235 |
}); |
236 |
|
237 |
promise.then(&lexicalGlobalObject, &fulfillHandler, &rejectHandler); |
238 |
|
239 |
// Drive RunLoop until we get either of "Worker is terminated", "Loading is done", or "Loading is failed". |
240 |
WorkerRunLoop& runLoop = m_workerGlobalScope->thread().runLoop(); |
241 |
String mode = makeString("loadModuleSynchronously", runLoop.createUniqueId()); |
242 |
|
243 |
MessageQueueWaitResult result = MessageQueueMessageReceived; |
244 |
while ((!moduleScriptRef->isLoaded() || !moduleScriptRef->wasCanceled()) && result != MessageQueueTerminated) |
245 |
result = runLoop.runInMode(m_workerGlobalScope, mode); |
246 |
|
247 |
// FIXME: Currently we are not offering cancelling. |
248 |
// if (!loader->done() && result == MessageQueueTerminated) |
249 |
// loader->cancel(); |
250 |
|
251 |
return result; |
252 |
} |
253 |
|
254 |
void WorkerScriptController::linkAndEvaluateModule(LoadableModuleScript& moduleScript, const ScriptSourceCode& sourceCode, String* returnedExceptionMessage) |
255 |
{ |
256 |
if (isExecutionForbidden()) |
257 |
return; |
258 |
|
259 |
initScriptIfNeeded(); |
260 |
|
261 |
auto& lexicalGlobalObject = *m_workerGlobalScopeWrapper.get(); |
262 |
VM& vm = lexicalGlobalObject.vm(); |
263 |
JSLockHolder lock { vm }; |
264 |
|
265 |
NakedPtr<JSC::Exception> returnedException; |
266 |
JSExecState::linkAndEvaluateModule(lexicalGlobalObject, Identifier::fromUid(vm, moduleScript.moduleKey()), jsUndefined(), returnedException); |
267 |
if ((returnedException && isTerminatedExecutionException(vm, returnedException)) || isTerminatingExecution()) { |
268 |
forbidExecution(); |
269 |
return; |
270 |
} |
271 |
|
272 |
if (returnedException) { |
273 |
if (m_workerGlobalScope->canIncludeErrorDetails(sourceCode.cachedScript(), sourceCode.url().string())) { |
274 |
// FIXME: It's not great that this can run arbitrary code to string-ify the value of the exception. |
275 |
// Do we need to do anything to handle that properly, if it, say, raises another exception? |
276 |
if (returnedExceptionMessage) |
277 |
*returnedExceptionMessage = returnedException->value().toWTFString(&lexicalGlobalObject); |
278 |
} else { |
279 |
// Overwrite the detailed error with a generic error. |
280 |
String genericErrorMessage { "Script error."_s }; |
281 |
if (returnedExceptionMessage) |
282 |
*returnedExceptionMessage = genericErrorMessage; |
283 |
returnedException = JSC::Exception::create(vm, createError(&lexicalGlobalObject, genericErrorMessage)); |
284 |
} |
285 |
} |
286 |
} |
287 |
|
177 |
void WorkerScriptController::setException(JSC::Exception* exception) |
288 |
void WorkerScriptController::setException(JSC::Exception* exception) |
178 |
{ |
289 |
{ |
179 |
JSC::JSGlobalObject* lexicalGlobalObject = m_workerGlobalScopeWrapper.get(); |
290 |
JSC::JSGlobalObject* lexicalGlobalObject = m_workerGlobalScopeWrapper.get(); |