ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | 03916fe | 2017-02-28 01:20:54 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008-2017 Apple Inc. All Rights Reserved. |
michaeln@google.com | 6e104c0 | 2012-03-08 03:07:44 +0000 | [diff] [blame] | 3 | * Copyright (C) 2011, 2012 Google Inc. All Rights Reserved. |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
joepeck@webkit.org | 8851929 | 2016-10-27 22:18:55 +0000 | [diff] [blame] | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include "config.h" |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 28 | #include "WorkerScriptController.h" |
| 29 | |
benjamin@webkit.org | 5ea5978 | 2012-09-14 01:50:17 +0000 | [diff] [blame] | 30 | #include "JSDOMBinding.h" |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 31 | #include "JSDedicatedWorkerGlobalScope.h" |
cdumez@apple.com | d620980 | 2016-02-15 02:17:33 +0000 | [diff] [blame] | 32 | #include "JSEventTarget.h" |
utatane.tea@gmail.com | 11098d6 | 2018-08-06 04:37:10 +0000 | [diff] [blame] | 33 | #include "JSExecState.h" |
beidson@apple.com | 7ce560f | 2017-10-13 06:44:47 +0000 | [diff] [blame] | 34 | #include "JSServiceWorkerGlobalScope.h" |
darin@chromium.org | 6b41247 | 2008-11-24 23:07:38 +0000 | [diff] [blame] | 35 | #include "ScriptSourceCode.h" |
weinig@apple.com | 4ee5585 | 2010-03-07 00:38:12 +0000 | [diff] [blame] | 36 | #include "WebCoreJSClientData.h" |
commit-queue@webkit.org | 31e0b1a | 2016-02-15 21:23:23 +0000 | [diff] [blame] | 37 | #include "WorkerConsoleClient.h" |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 38 | #include "WorkerGlobalScope.h" |
don.olmstead@sony.com | 71f34a0 | 2018-02-07 05:20:34 +0000 | [diff] [blame] | 39 | #include <JavaScriptCore/Completion.h> |
keith_miller@apple.com | 8af61cf | 2020-07-20 21:03:16 +0000 | [diff] [blame] | 40 | #include <JavaScriptCore/DeferredWorkTimer.h> |
don.olmstead@sony.com | 71f34a0 | 2018-02-07 05:20:34 +0000 | [diff] [blame] | 41 | #include <JavaScriptCore/Exception.h> |
| 42 | #include <JavaScriptCore/ExceptionHelpers.h> |
| 43 | #include <JavaScriptCore/GCActivityCallback.h> |
justin_michaud@apple.com | 16d7ba8 | 2018-11-08 05:29:59 +0000 | [diff] [blame] | 44 | #include <JavaScriptCore/JSCInlines.h> |
don.olmstead@sony.com | 71f34a0 | 2018-02-07 05:20:34 +0000 | [diff] [blame] | 45 | #include <JavaScriptCore/JSLock.h> |
don.olmstead@sony.com | 71f34a0 | 2018-02-07 05:20:34 +0000 | [diff] [blame] | 46 | #include <JavaScriptCore/StrongInlines.h> |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 47 | |
| 48 | namespace WebCore { |
keith_miller@apple.com | ce64b73 | 2017-10-17 07:10:58 +0000 | [diff] [blame] | 49 | using namespace JSC; |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 50 | |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 51 | WorkerScriptController::WorkerScriptController(WorkerGlobalScope* workerGlobalScope) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 52 | : m_vm(VM::create()) |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 53 | , m_workerGlobalScope(workerGlobalScope) |
| 54 | , m_workerGlobalScopeWrapper(*m_vm) |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 55 | { |
fpizlo@apple.com | 8b6ca58 | 2016-11-02 22:01:04 +0000 | [diff] [blame] | 56 | m_vm->heap.acquireAccess(); // It's not clear that we have good discipline for heap access, so turn it on permanently. |
fpizlo@apple.com | 5fa9d96 | 2017-01-18 04:22:45 +0000 | [diff] [blame] | 57 | JSVMClientData::initNormalWorld(m_vm.get()); |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | WorkerScriptController::~WorkerScriptController() |
| 61 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 62 | JSLockHolder lock(vm()); |
commit-queue@webkit.org | 31e0b1a | 2016-02-15 21:23:23 +0000 | [diff] [blame] | 63 | if (m_workerGlobalScopeWrapper) { |
commit-queue@webkit.org | 471132e | 2017-11-29 21:39:56 +0000 | [diff] [blame] | 64 | m_workerGlobalScopeWrapper->clearDOMGuardedObjects(); |
commit-queue@webkit.org | 31e0b1a | 2016-02-15 21:23:23 +0000 | [diff] [blame] | 65 | m_workerGlobalScopeWrapper->setConsoleClient(nullptr); |
| 66 | m_consoleClient = nullptr; |
| 67 | } |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 68 | m_workerGlobalScopeWrapper.clear(); |
cdumez@apple.com | d839ea1 | 2015-07-04 19:42:18 +0000 | [diff] [blame] | 69 | m_vm = nullptr; |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void WorkerScriptController::initScript() |
| 73 | { |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 74 | ASSERT(!m_workerGlobalScopeWrapper); |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 75 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 76 | JSLockHolder lock(m_vm.get()); |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 77 | |
ggaren@apple.com | 9d883d3 | 2009-03-26 02:12:27 +0000 | [diff] [blame] | 78 | // Explicitly protect the global object's prototype so it isn't collected |
| 79 | // when we allocate the global object. (Once the global object is fully |
| 80 | // constructed, it can mark its own prototype.) |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 81 | if (m_workerGlobalScope->isDedicatedWorkerGlobalScope()) { |
joepeck@webkit.org | 8851929 | 2016-10-27 22:18:55 +0000 | [diff] [blame] | 82 | Structure* dedicatedContextPrototypeStructure = JSDedicatedWorkerGlobalScopePrototype::createStructure(*m_vm, nullptr, jsNull()); |
ysuzuki@apple.com | 3daa3be | 2020-03-27 21:25:38 +0000 | [diff] [blame] | 83 | JSDedicatedWorkerGlobalScopePrototype* dedicatedContextPrototype = JSDedicatedWorkerGlobalScopePrototype::create(*m_vm, nullptr, dedicatedContextPrototypeStructure); |
| 84 | JSC::EnsureStillAliveScope protectedDedicatedContextPrototype(dedicatedContextPrototype); |
| 85 | Structure* structure = JSDedicatedWorkerGlobalScope::createStructure(*m_vm, nullptr, dedicatedContextPrototype); |
cdumez@apple.com | 12d8c41 | 2016-06-08 17:31:12 +0000 | [diff] [blame] | 86 | auto* proxyStructure = JSProxy::createStructure(*m_vm, nullptr, jsNull(), PureForwardingProxyType); |
| 87 | auto* proxy = JSProxy::create(*m_vm, proxyStructure); |
dimich@chromium.org | 0d83cae | 2009-07-24 19:23:38 +0000 | [diff] [blame] | 88 | |
cdumez@apple.com | 12d8c41 | 2016-06-08 17:31:12 +0000 | [diff] [blame] | 89 | m_workerGlobalScopeWrapper.set(*m_vm, JSDedicatedWorkerGlobalScope::create(*m_vm, structure, static_cast<DedicatedWorkerGlobalScope&>(*m_workerGlobalScope), proxy)); |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 90 | dedicatedContextPrototypeStructure->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get()); |
| 91 | ASSERT(structure->globalObject() == m_workerGlobalScopeWrapper); |
utatane.tea@gmail.com | b860d69 | 2018-05-31 06:19:33 +0000 | [diff] [blame] | 92 | ASSERT(m_workerGlobalScopeWrapper->structure(*m_vm)->globalObject() == m_workerGlobalScopeWrapper); |
| 93 | dedicatedContextPrototype->structure(*m_vm)->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get()); |
keith_miller@apple.com | 0e528c1 | 2019-06-25 21:19:21 +0000 | [diff] [blame] | 94 | auto* workerGlobalScopePrototype = JSWorkerGlobalScope::prototype(*m_vm, *m_workerGlobalScopeWrapper.get()); |
| 95 | workerGlobalScopePrototype->didBecomePrototype(); |
| 96 | dedicatedContextPrototype->structure(*m_vm)->setPrototypeWithoutTransition(*m_vm, workerGlobalScopePrototype); |
cdumez@apple.com | 12d8c41 | 2016-06-08 17:31:12 +0000 | [diff] [blame] | 97 | |
| 98 | proxy->setTarget(*m_vm, m_workerGlobalScopeWrapper.get()); |
utatane.tea@gmail.com | b860d69 | 2018-05-31 06:19:33 +0000 | [diff] [blame] | 99 | proxy->structure(*m_vm)->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get()); |
beidson@apple.com | 7ce560f | 2017-10-13 06:44:47 +0000 | [diff] [blame] | 100 | #if ENABLE(SERVICE_WORKER) |
| 101 | } else if (m_workerGlobalScope->isServiceWorkerGlobalScope()) { |
| 102 | Structure* contextPrototypeStructure = JSServiceWorkerGlobalScopePrototype::createStructure(*m_vm, nullptr, jsNull()); |
ysuzuki@apple.com | 3daa3be | 2020-03-27 21:25:38 +0000 | [diff] [blame] | 103 | JSServiceWorkerGlobalScopePrototype* contextPrototype = JSServiceWorkerGlobalScopePrototype::create(*m_vm, nullptr, contextPrototypeStructure); |
| 104 | JSC::EnsureStillAliveScope protectedContextPrototype(contextPrototype); |
| 105 | Structure* structure = JSServiceWorkerGlobalScope::createStructure(*m_vm, nullptr, contextPrototype); |
beidson@apple.com | 7ce560f | 2017-10-13 06:44:47 +0000 | [diff] [blame] | 106 | auto* proxyStructure = JSProxy::createStructure(*m_vm, nullptr, jsNull(), PureForwardingProxyType); |
| 107 | auto* proxy = JSProxy::create(*m_vm, proxyStructure); |
| 108 | |
| 109 | m_workerGlobalScopeWrapper.set(*m_vm, JSServiceWorkerGlobalScope::create(*m_vm, structure, static_cast<ServiceWorkerGlobalScope&>(*m_workerGlobalScope), proxy)); |
| 110 | contextPrototypeStructure->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get()); |
| 111 | ASSERT(structure->globalObject() == m_workerGlobalScopeWrapper); |
| 112 | ASSERT(m_workerGlobalScopeWrapper->structure()->globalObject() == m_workerGlobalScopeWrapper); |
utatane.tea@gmail.com | b860d69 | 2018-05-31 06:19:33 +0000 | [diff] [blame] | 113 | contextPrototype->structure(*m_vm)->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get()); |
keith_miller@apple.com | 0e528c1 | 2019-06-25 21:19:21 +0000 | [diff] [blame] | 114 | auto* workerGlobalScopePrototype = JSWorkerGlobalScope::prototype(*m_vm, *m_workerGlobalScopeWrapper.get()); |
| 115 | workerGlobalScopePrototype->didBecomePrototype(); |
| 116 | contextPrototype->structure(*m_vm)->setPrototypeWithoutTransition(*m_vm, workerGlobalScopePrototype); |
beidson@apple.com | 7ce560f | 2017-10-13 06:44:47 +0000 | [diff] [blame] | 117 | |
| 118 | proxy->setTarget(*m_vm, m_workerGlobalScopeWrapper.get()); |
utatane.tea@gmail.com | b860d69 | 2018-05-31 06:19:33 +0000 | [diff] [blame] | 119 | proxy->structure(*m_vm)->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get()); |
beidson@apple.com | 7ce560f | 2017-10-13 06:44:47 +0000 | [diff] [blame] | 120 | #endif |
atwilson@chromium.org | 5790d73 | 2009-08-06 15:30:37 +0000 | [diff] [blame] | 121 | } |
beidson@apple.com | 7ce560f | 2017-10-13 06:44:47 +0000 | [diff] [blame] | 122 | |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 123 | ASSERT(m_workerGlobalScopeWrapper->globalObject() == m_workerGlobalScopeWrapper); |
sbarati@apple.com | f36537e | 2017-10-20 07:50:08 +0000 | [diff] [blame] | 124 | ASSERT(asObject(m_workerGlobalScopeWrapper->getPrototypeDirect(*m_vm))->globalObject() == m_workerGlobalScopeWrapper); |
commit-queue@webkit.org | 31e0b1a | 2016-02-15 21:23:23 +0000 | [diff] [blame] | 125 | |
ysuzuki@apple.com | 1d8e24d | 2019-08-19 06:59:40 +0000 | [diff] [blame] | 126 | m_consoleClient = makeUnique<WorkerConsoleClient>(*m_workerGlobalScope); |
commit-queue@webkit.org | 31e0b1a | 2016-02-15 21:23:23 +0000 | [diff] [blame] | 127 | m_workerGlobalScopeWrapper->setConsoleClient(m_consoleClient.get()); |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 128 | } |
| 129 | |
beidson@apple.com | 3dfb9af | 2017-11-01 21:23:52 +0000 | [diff] [blame] | 130 | void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, String* returnedExceptionMessage) |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 131 | { |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 132 | if (isExecutionForbidden()) |
benjamin@webkit.org | 621743c | 2012-02-02 04:11:09 +0000 | [diff] [blame] | 133 | return; |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 134 | |
darin@apple.com | 8570494 | 2016-10-08 23:01:27 +0000 | [diff] [blame] | 135 | NakedPtr<JSC::Exception> exception; |
beidson@apple.com | 3dfb9af | 2017-11-01 21:23:52 +0000 | [diff] [blame] | 136 | evaluate(sourceCode, exception, returnedExceptionMessage); |
mark.lam@apple.com | 6ed0827 | 2015-06-05 18:52:12 +0000 | [diff] [blame] | 137 | if (exception) { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 138 | JSLockHolder lock(vm()); |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 139 | reportException(m_workerGlobalScopeWrapper.get(), exception); |
oliver@apple.com | 991c37e | 2009-03-10 09:44:30 +0000 | [diff] [blame] | 140 | } |
oliver@apple.com | 991c37e | 2009-03-10 09:44:30 +0000 | [diff] [blame] | 141 | } |
| 142 | |
beidson@apple.com | 3dfb9af | 2017-11-01 21:23:52 +0000 | [diff] [blame] | 143 | void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, NakedPtr<JSC::Exception>& returnedException, String* returnedExceptionMessage) |
oliver@apple.com | 991c37e | 2009-03-10 09:44:30 +0000 | [diff] [blame] | 144 | { |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 145 | if (isExecutionForbidden()) |
benjamin@webkit.org | 621743c | 2012-02-02 04:11:09 +0000 | [diff] [blame] | 146 | return; |
ap@webkit.org | 6b9d412 | 2008-11-22 07:42:44 +0000 | [diff] [blame] | 147 | |
ap@webkit.org | 3568f13 | 2008-11-13 07:27:12 +0000 | [diff] [blame] | 148 | initScriptIfNeeded(); |
ap@webkit.org | 3568f13 | 2008-11-13 07:27:12 +0000 | [diff] [blame] | 149 | |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 150 | auto& globalObject = *m_workerGlobalScopeWrapper.get(); |
| 151 | VM& vm = globalObject.vm(); |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 152 | JSLockHolder lock { vm }; |
weinig@apple.com | f41a7da | 2011-09-08 22:38:44 +0000 | [diff] [blame] | 153 | |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 154 | JSExecState::profiledEvaluate(&globalObject, JSC::ProfilingReason::Other, sourceCode.jsSourceCode(), m_workerGlobalScopeWrapper->globalThis(), returnedException); |
weinig@apple.com | 76c1146 | 2011-09-11 05:16:09 +0000 | [diff] [blame] | 155 | |
keith_miller@apple.com | 45da760 | 2017-01-27 01:47:52 +0000 | [diff] [blame] | 156 | if ((returnedException && isTerminatedExecutionException(vm, returnedException)) || isTerminatingExecution()) { |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 157 | forbidExecution(); |
benjamin@webkit.org | 621743c | 2012-02-02 04:11:09 +0000 | [diff] [blame] | 158 | return; |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 159 | } |
| 160 | |
mark.lam@apple.com | 6c5e076 | 2015-06-16 20:51:04 +0000 | [diff] [blame] | 161 | if (returnedException) { |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 162 | if (m_workerGlobalScope->canIncludeErrorDetails(sourceCode.cachedScript(), sourceCode.url().string())) { |
| 163 | // FIXME: It's not great that this can run arbitrary code to string-ify the value of the exception. |
| 164 | // Do we need to do anything to handle that properly, if it, say, raises another exception? |
| 165 | if (returnedExceptionMessage) |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 166 | *returnedExceptionMessage = returnedException->value().toWTFString(&globalObject); |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 167 | } else { |
| 168 | // Overwrite the detailed error with a generic error. |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 169 | String genericErrorMessage { "Script error."_s }; |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 170 | if (returnedExceptionMessage) |
| 171 | *returnedExceptionMessage = genericErrorMessage; |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 172 | returnedException = JSC::Exception::create(vm, createError(&globalObject, genericErrorMessage)); |
darin@apple.com | 0b38bee | 2018-02-08 06:06:29 +0000 | [diff] [blame] | 173 | } |
levin@chromium.org | 8544e5d | 2011-02-04 00:48:28 +0000 | [diff] [blame] | 174 | } |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 175 | } |
| 176 | |
mark.lam@apple.com | 6ed0827 | 2015-06-05 18:52:12 +0000 | [diff] [blame] | 177 | void WorkerScriptController::setException(JSC::Exception* exception) |
oliver@apple.com | 991c37e | 2009-03-10 09:44:30 +0000 | [diff] [blame] | 178 | { |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 179 | JSC::JSGlobalObject* lexicalGlobalObject = m_workerGlobalScopeWrapper.get(); |
| 180 | VM& vm = lexicalGlobalObject->vm(); |
mark.lam@apple.com | 284f456 | 2016-08-30 20:54:54 +0000 | [diff] [blame] | 181 | auto scope = DECLARE_THROW_SCOPE(vm); |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 182 | throwException(lexicalGlobalObject, scope, exception); |
oliver@apple.com | 991c37e | 2009-03-10 09:44:30 +0000 | [diff] [blame] | 183 | } |
| 184 | |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 185 | void WorkerScriptController::scheduleExecutionTermination() |
ap@webkit.org | 6b9d412 | 2008-11-22 07:42:44 +0000 | [diff] [blame] | 186 | { |
mark.lam@apple.com | 2519988 | 2017-05-15 20:34:13 +0000 | [diff] [blame] | 187 | if (m_isTerminatingExecution) |
| 188 | return; |
| 189 | |
mark.lam@apple.com | f842cd8 | 2017-03-09 19:08:46 +0000 | [diff] [blame] | 190 | { |
| 191 | // The mutex provides a memory barrier to ensure that once |
| 192 | // termination is scheduled, isTerminatingExecution() will |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 193 | // accurately reflect that lexicalGlobalObject when called from another thread. |
mark.lam@apple.com | f842cd8 | 2017-03-09 19:08:46 +0000 | [diff] [blame] | 194 | LockHolder locker(m_scheduledTerminationMutex); |
| 195 | m_isTerminatingExecution = true; |
| 196 | } |
mark.lam@apple.com | 03916fe | 2017-02-28 01:20:54 +0000 | [diff] [blame] | 197 | m_vm->notifyNeedTermination(); |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 198 | } |
| 199 | |
mark.lam@apple.com | 127015c | 2015-08-27 02:49:52 +0000 | [diff] [blame] | 200 | bool WorkerScriptController::isTerminatingExecution() const |
michaeln@google.com | 6e104c0 | 2012-03-08 03:07:44 +0000 | [diff] [blame] | 201 | { |
| 202 | // See comments in scheduleExecutionTermination regarding mutex usage. |
fpizlo@apple.com | 7398598 | 2015-08-18 19:31:28 +0000 | [diff] [blame] | 203 | LockHolder locker(m_scheduledTerminationMutex); |
mark.lam@apple.com | 127015c | 2015-08-27 02:49:52 +0000 | [diff] [blame] | 204 | return m_isTerminatingExecution; |
michaeln@google.com | 6e104c0 | 2012-03-08 03:07:44 +0000 | [diff] [blame] | 205 | } |
| 206 | |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 207 | void WorkerScriptController::forbidExecution() |
| 208 | { |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 209 | ASSERT(m_workerGlobalScope->isContextThread()); |
ap@webkit.org | f809c4d | 2008-12-03 15:25:08 +0000 | [diff] [blame] | 210 | m_executionForbidden = true; |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bool WorkerScriptController::isExecutionForbidden() const |
| 214 | { |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 215 | ASSERT(m_workerGlobalScope->isContextThread()); |
dimich@chromium.org | ae6b777 | 2011-04-14 22:14:01 +0000 | [diff] [blame] | 216 | return m_executionForbidden; |
ap@webkit.org | 6b9d412 | 2008-11-22 07:42:44 +0000 | [diff] [blame] | 217 | } |
| 218 | |
commit-queue@webkit.org | 7415e10 | 2012-09-14 23:33:20 +0000 | [diff] [blame] | 219 | void WorkerScriptController::disableEval(const String& errorMessage) |
weinig@apple.com | 76f3d93 | 2011-10-05 00:52:50 +0000 | [diff] [blame] | 220 | { |
| 221 | initScriptIfNeeded(); |
jfbastien@apple.com | 7b29cef | 2017-06-29 18:49:18 +0000 | [diff] [blame] | 222 | JSLockHolder lock{vm()}; |
weinig@apple.com | 76f3d93 | 2011-10-05 00:52:50 +0000 | [diff] [blame] | 223 | |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 224 | m_workerGlobalScopeWrapper->setEvalEnabled(false, errorMessage); |
weinig@apple.com | 76f3d93 | 2011-10-05 00:52:50 +0000 | [diff] [blame] | 225 | } |
| 226 | |
jfbastien@apple.com | 7b29cef | 2017-06-29 18:49:18 +0000 | [diff] [blame] | 227 | void WorkerScriptController::disableWebAssembly(const String& errorMessage) |
| 228 | { |
| 229 | initScriptIfNeeded(); |
| 230 | JSLockHolder lock{vm()}; |
| 231 | |
| 232 | m_workerGlobalScopeWrapper->setWebAssemblyEnabled(false, errorMessage); |
| 233 | } |
| 234 | |
fpizlo@apple.com | 8b6ca58 | 2016-11-02 22:01:04 +0000 | [diff] [blame] | 235 | void WorkerScriptController::releaseHeapAccess() |
| 236 | { |
| 237 | m_vm->heap.releaseAccess(); |
| 238 | } |
| 239 | |
| 240 | void WorkerScriptController::acquireHeapAccess() |
| 241 | { |
| 242 | m_vm->heap.acquireAccess(); |
| 243 | } |
| 244 | |
sbarati@apple.com | 492e443 | 2017-04-14 02:10:17 +0000 | [diff] [blame] | 245 | void WorkerScriptController::addTimerSetNotification(JSC::JSRunLoopTimer::TimerNotificationCallback callback) |
| 246 | { |
| 247 | auto processTimer = [&] (JSRunLoopTimer* timer) { |
| 248 | if (!timer) |
| 249 | return; |
| 250 | timer->addTimerSetNotification(callback); |
| 251 | }; |
| 252 | |
| 253 | processTimer(m_vm->heap.fullActivityCallback()); |
| 254 | processTimer(m_vm->heap.edenActivityCallback()); |
keith_miller@apple.com | 8af61cf | 2020-07-20 21:03:16 +0000 | [diff] [blame] | 255 | processTimer(m_vm->deferredWorkTimer.ptr()); |
sbarati@apple.com | 492e443 | 2017-04-14 02:10:17 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void WorkerScriptController::removeTimerSetNotification(JSC::JSRunLoopTimer::TimerNotificationCallback callback) |
| 259 | { |
| 260 | auto processTimer = [&] (JSRunLoopTimer* timer) { |
| 261 | if (!timer) |
| 262 | return; |
| 263 | timer->removeTimerSetNotification(callback); |
| 264 | }; |
| 265 | |
| 266 | processTimer(m_vm->heap.fullActivityCallback()); |
| 267 | processTimer(m_vm->heap.edenActivityCallback()); |
keith_miller@apple.com | 8af61cf | 2020-07-20 21:03:16 +0000 | [diff] [blame] | 268 | processTimer(m_vm->deferredWorkTimer.ptr()); |
sbarati@apple.com | 492e443 | 2017-04-14 02:10:17 +0000 | [diff] [blame] | 269 | } |
| 270 | |
commit-queue@webkit.org | b721bb37 | 2012-11-02 10:24:40 +0000 | [diff] [blame] | 271 | void WorkerScriptController::attachDebugger(JSC::Debugger* debugger) |
| 272 | { |
| 273 | initScriptIfNeeded(); |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 274 | debugger->attach(m_workerGlobalScopeWrapper.get()); |
commit-queue@webkit.org | b721bb37 | 2012-11-02 10:24:40 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void WorkerScriptController::detachDebugger(JSC::Debugger* debugger) |
| 278 | { |
ysuzuki@apple.com | 52e98bb | 2019-10-22 09:24:48 +0000 | [diff] [blame] | 279 | debugger->detach(m_workerGlobalScopeWrapper.get(), JSC::Debugger::TerminatingDebuggingSession); |
commit-queue@webkit.org | b721bb37 | 2012-11-02 10:24:40 +0000 | [diff] [blame] | 280 | } |
| 281 | |
ap@webkit.org | 90b52e8 | 2008-11-06 07:04:47 +0000 | [diff] [blame] | 282 | } // namespace WebCore |