1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#include "config.h"
28#include "JSWorkletGlobalScopeBase.h"
29
30#if ENABLE(CSS_PAINTING_API)
31
32#include "DOMWrapperWorld.h"
33#include "JSDOMGlobalObjectTask.h"
34#include "JSWorkletGlobalScope.h"
35#include "WorkletGlobalScope.h"
36#include "WorkletScriptController.h"
37#include <JavaScriptCore/JSCInlines.h>
38#include <JavaScriptCore/JSCJSValueInlines.h>
39#include <wtf/Language.h>
40
41namespace WebCore {
42
43using namespace JSC;
44
45const ClassInfo JSWorkletGlobalScopeBase::s_info = { "WorkletGlobalScope", &JSDOMGlobalObject::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWorkletGlobalScopeBase) };
46
47const GlobalObjectMethodTable JSWorkletGlobalScopeBase::s_globalObjectMethodTable = {
48 &supportsRichSourceInfo,
49 &shouldInterruptScript,
50 &javaScriptRuntimeFlags,
51 nullptr, // queueTaskToEventLoop
52 &shouldInterruptScriptBeforeTimeout,
53 nullptr, // moduleLoaderImportModule
54 nullptr, // moduleLoaderResolve
55 nullptr, // moduleLoaderFetch
56 nullptr, // moduleLoaderCreateImportMetaProperties
57 nullptr, // moduleLoaderEvaluate
58 &promiseRejectionTracker,
59 &defaultLanguage,
60 nullptr, // compileStreaming
61 nullptr, // instantiateStreaming
62};
63
64JSWorkletGlobalScopeBase::JSWorkletGlobalScopeBase(JSC::VM& vm, JSC::Structure* structure, RefPtr<WorkletGlobalScope>&& impl)
65 : JSDOMGlobalObject(vm, structure, normalWorld(vm), &s_globalObjectMethodTable)
66 , m_wrapped(WTFMove(impl))
67{
68}
69
70void JSWorkletGlobalScopeBase::finishCreation(VM& vm, JSProxy* proxy)
71{
72 m_proxy.set(vm, this, proxy);
73
74 Base::finishCreation(vm, m_proxy.get());
75 ASSERT(inherits(vm, info()));
76}
77
78void JSWorkletGlobalScopeBase::clearDOMGuardedObjects()
79{
80 auto guardedObjects = m_guardedObjects;
81 for (auto& guarded : guardedObjects)
82 guarded->clear();
83}
84
85void JSWorkletGlobalScopeBase::visitChildren(JSCell* cell, SlotVisitor& visitor)
86{
87 auto* thisObject = jsCast<JSWorkletGlobalScopeBase*>(cell);
88 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
89 Base::visitChildren(thisObject, visitor);
90 visitor.append(thisObject->m_proxy);
91}
92
93void JSWorkletGlobalScopeBase::destroy(JSCell* cell)
94{
95 static_cast<JSWorkletGlobalScopeBase*>(cell)->JSWorkletGlobalScopeBase::~JSWorkletGlobalScopeBase();
96}
97
98ScriptExecutionContext* JSWorkletGlobalScopeBase::scriptExecutionContext() const
99{
100 return m_wrapped.get();
101}
102
103bool JSWorkletGlobalScopeBase::supportsRichSourceInfo(const JSGlobalObject* object)
104{
105 return JSGlobalObject::supportsRichSourceInfo(object);
106}
107
108bool JSWorkletGlobalScopeBase::shouldInterruptScript(const JSGlobalObject* object)
109{
110 return JSGlobalObject::shouldInterruptScript(object);
111}
112
113bool JSWorkletGlobalScopeBase::shouldInterruptScriptBeforeTimeout(const JSGlobalObject* object)
114{
115 return JSGlobalObject::shouldInterruptScriptBeforeTimeout(object);
116}
117
118RuntimeFlags JSWorkletGlobalScopeBase::javaScriptRuntimeFlags(const JSGlobalObject* object)
119{
120 auto* thisObject = jsCast<const JSWorkletGlobalScopeBase*>(object);
121 return thisObject->m_wrapped->jsRuntimeFlags();
122}
123
124JSValue toJS(ExecState* exec, JSDOMGlobalObject*, WorkletGlobalScope& workletGlobalScope)
125{
126 return toJS(exec, workletGlobalScope);
127}
128
129JSValue toJS(ExecState*, WorkletGlobalScope& workletGlobalScope)
130{
131 if (!workletGlobalScope.script())
132 return jsUndefined();
133 auto* contextWrapper = workletGlobalScope.script()->workletGlobalScopeWrapper();
134 if (!contextWrapper)
135 return jsUndefined();
136 return contextWrapper->proxy();
137}
138
139JSWorkletGlobalScope* toJSWorkletGlobalScope(VM& vm, JSValue value)
140{
141 if (!value.isObject())
142 return nullptr;
143 auto* classInfo = asObject(value)->classInfo(vm);
144
145 if (classInfo == JSProxy::info())
146 return jsDynamicCast<JSWorkletGlobalScope*>(vm, jsCast<JSProxy*>(asObject(value))->target());
147
148 return nullptr;
149}
150
151} // namespace WebCore
152#endif
153