Lines 35-40
a/Source/WebCore/workers/WorkerOrWorkletScriptController.cpp_sec1
|
35 |
#include "JSExecState.h" |
35 |
#include "JSExecState.h" |
36 |
#include "JSPaintWorkletGlobalScope.h" |
36 |
#include "JSPaintWorkletGlobalScope.h" |
37 |
#include "JSServiceWorkerGlobalScope.h" |
37 |
#include "JSServiceWorkerGlobalScope.h" |
|
|
38 |
#include "LoadableModuleScript.h" |
39 |
#include "ModuleFetchParameters.h" |
38 |
#include "ScriptSourceCode.h" |
40 |
#include "ScriptSourceCode.h" |
39 |
#include "WebCoreJSClientData.h" |
41 |
#include "WebCoreJSClientData.h" |
40 |
#include "WorkerConsoleClient.h" |
42 |
#include "WorkerConsoleClient.h" |
Lines 228-234
void WorkerOrWorkletScriptController::evaluate(const ScriptSourceCode& sourceCod
a/Source/WebCore/workers/WorkerOrWorkletScriptController.cpp_sec2
|
228 |
} |
230 |
} |
229 |
} |
231 |
} |
230 |
|
232 |
|
231 |
template<typename JSGlobalScopePrototype, typename JSGlobalScope, typename GlobalScope> |
233 |
MessageQueueWaitResult WorkerOrWorkletScriptController::loadModuleSynchronously(LoadableModuleScript& moduleScript, const ScriptSourceCode& sourceCode) |
|
|
234 |
{ |
235 |
if (isExecutionForbidden()) |
236 |
return MessageQueueTerminated; |
237 |
|
238 |
initScriptIfNeeded(); |
239 |
|
240 |
auto& lexicalGlobalObject = *m_workerGlobalScopeWrapper.get(); |
241 |
VM& vm = lexicalGlobalObject.vm(); |
242 |
JSLockHolder lock { vm }; |
243 |
|
244 |
auto& promise = JSExecState::loadModule(lexicalGlobalObject, sourceCode.jsSourceCode(), JSC::JSScriptFetcher::create(vm, { &moduleScript })); |
245 |
|
246 |
RefPtr<LoadableModuleScript> moduleScriptRef(&moduleScript); |
247 |
|
248 |
auto& fulfillHandler = *JSNativeStdFunction::create(vm, &lexicalGlobalObject, 1, String(), [moduleScriptRef](JSGlobalObject* globalObject, CallFrame* callFrame) -> JSC::EncodedJSValue { |
249 |
VM& vm = globalObject->vm(); |
250 |
auto scope = DECLARE_THROW_SCOPE(vm); |
251 |
Identifier moduleKey = jsValueToModuleKey(globalObject, callFrame->argument(0)); |
252 |
RETURN_IF_EXCEPTION(scope, { }); |
253 |
moduleScriptRef->notifyLoadCompleted(*moduleKey.impl()); |
254 |
return JSValue::encode(jsUndefined()); |
255 |
}); |
256 |
|
257 |
auto& rejectHandler = *JSNativeStdFunction::create(vm, &lexicalGlobalObject, 1, String(), [moduleScriptRef](JSGlobalObject* globalObject, CallFrame* callFrame) { |
258 |
VM& vm = globalObject->vm(); |
259 |
JSValue errorValue = callFrame->argument(0); |
260 |
if (errorValue.isObject()) { |
261 |
auto* object = JSC::asObject(errorValue); |
262 |
if (JSValue failureKindValue = object->getDirect(vm, static_cast<JSVMClientData&>(*vm.clientData).builtinNames().failureKindPrivateName())) { |
263 |
// This is host propagated error in the module loader pipeline. |
264 |
switch (static_cast<ModuleFetchFailureKind>(failureKindValue.asInt32())) { |
265 |
case ModuleFetchFailureKind::WasErrored: |
266 |
moduleScriptRef->notifyLoadFailed(LoadableScript::Error { |
267 |
LoadableScript::ErrorType::CachedScript, |
268 |
WTF::nullopt |
269 |
}); |
270 |
break; |
271 |
case ModuleFetchFailureKind::WasCanceled: |
272 |
moduleScriptRef->notifyLoadWasCanceled(); |
273 |
break; |
274 |
} |
275 |
return JSValue::encode(jsUndefined()); |
276 |
} |
277 |
} |
278 |
|
279 |
auto scope = DECLARE_CATCH_SCOPE(vm); |
280 |
moduleScriptRef->notifyLoadFailed(LoadableScript::Error { |
281 |
LoadableScript::ErrorType::CachedScript, |
282 |
LoadableScript::ConsoleMessage { |
283 |
MessageSource::JS, |
284 |
MessageLevel::Error, |
285 |
retrieveErrorMessage(*globalObject, vm, errorValue, scope), |
286 |
} |
287 |
}); |
288 |
return JSValue::encode(jsUndefined()); |
289 |
}); |
290 |
|
291 |
promise.then(&lexicalGlobalObject, &fulfillHandler, &rejectHandler); |
292 |
|
293 |
// Drive RunLoop until we get either of "Worker is terminated", "Loading is done", or "Loading is failed". |
294 |
WorkerRunLoop& runLoop = m_workerGlobalScope->thread().runLoop(); |
295 |
String mode = makeString("loadModuleSynchronously", runLoop.createUniqueId()); |
296 |
|
297 |
MessageQueueWaitResult result = MessageQueueMessageReceived; |
298 |
while ((!moduleScriptRef->isLoaded() || !moduleScriptRef->wasCanceled()) && result != MessageQueueTerminated) |
299 |
result = runLoop.runInMode(m_workerGlobalScope, mode); |
300 |
|
301 |
// FIXME: Currently we are not offering cancelling. |
302 |
// if (!loader->done() && result == MessageQueueTerminated) |
303 |
// loader->cancel(); |
304 |
|
305 |
return result; |
306 |
} |
307 |
|
308 |
void WorkerOrWorkletScriptController::linkAndEvaluateModule(LoadableModuleScript& moduleScript, const ScriptSourceCode& sourceCode, String* returnedExceptionMessage) |
309 |
{ |
310 |
if (isExecutionForbidden()) |
311 |
return; |
312 |
|
313 |
initScriptIfNeeded(); |
314 |
|
315 |
auto& lexicalGlobalObject = *m_workerGlobalScopeWrapper.get(); |
316 |
VM& vm = lexicalGlobalObject.vm(); |
317 |
JSLockHolder lock { vm }; |
318 |
|
319 |
NakedPtr<JSC::Exception> returnedException; |
320 |
JSExecState::linkAndEvaluateModule(lexicalGlobalObject, Identifier::fromUid(vm, moduleScript.moduleKey()), jsUndefined(), returnedException); |
321 |
if ((returnedException && isTerminatedExecutionException(vm, returnedException)) || isTerminatingExecution()) { |
322 |
forbidExecution(); |
323 |
return; |
324 |
} |
325 |
|
326 |
if (returnedException) { |
327 |
if (m_workerGlobalScope->canIncludeErrorDetails(sourceCode.cachedScript(), sourceCode.url().string())) { |
328 |
// FIXME: It's not great that this can run arbitrary code to string-ify the value of the exception. |
329 |
// Do we need to do anything to handle that properly, if it, say, raises another exception? |
330 |
if (returnedExceptionMessage) |
331 |
*returnedExceptionMessage = returnedException->value().toWTFString(&lexicalGlobalObject); |
332 |
} else { |
333 |
// Overwrite the detailed error with a generic error. |
334 |
String genericErrorMessage { "Script error."_s }; |
335 |
if (returnedExceptionMessage) |
336 |
*returnedExceptionMessage = genericErrorMessage; |
337 |
returnedException = JSC::Exception::create(vm, createError(&lexicalGlobalObject, genericErrorMessage)); |
338 |
} |
339 |
} |
340 |
} |
341 |
|
342 |
template<typename JSGlobalScopePrototype, typename JSGlobalScope, typename GlobalScope> |
232 |
void WorkerOrWorkletScriptController::initScriptWithSubclass() |
343 |
void WorkerOrWorkletScriptController::initScriptWithSubclass() |
233 |
{ |
344 |
{ |
234 |
ASSERT(!m_globalScopeWrapper); |
345 |
ASSERT(!m_globalScopeWrapper); |