Get rid of HeapRootVisitor and make SlotVisitor less painful to use
[WebKit-https.git] / Source / WebCore / bindings / js / JSWorkerGlobalScopeBase.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2016 Apple Inc. All rights reserved.
3  * Copyright (C) 2009 Google Inc. All Rights Reserved.
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  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
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
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  *
26  */
27
28 #include "config.h"
29
30 #include "JSWorkerGlobalScopeBase.h"
31
32 #include "DOMWrapperWorld.h"
33 #include "JSDOMGlobalObjectTask.h"
34 #include "JSDedicatedWorkerGlobalScope.h"
35 #include "JSDynamicDowncast.h"
36 #include "JSWorkerGlobalScope.h"
37 #include "Language.h"
38 #include "WorkerGlobalScope.h"
39 #include "WorkerThread.h"
40 #include <runtime/JSCInlines.h>
41 #include <runtime/JSCJSValueInlines.h>
42 #include <runtime/Microtask.h>
43
44 using namespace JSC;
45
46 namespace WebCore {
47
48 const ClassInfo JSWorkerGlobalScopeBase::s_info = { "WorkerGlobalScope", &JSDOMGlobalObject::s_info, 0, CREATE_METHOD_TABLE(JSWorkerGlobalScopeBase) };
49
50 const GlobalObjectMethodTable JSWorkerGlobalScopeBase::s_globalObjectMethodTable = {
51     &supportsRichSourceInfo,
52     &shouldInterruptScript,
53     &javaScriptRuntimeFlags,
54     &queueTaskToEventLoop,
55     &shouldInterruptScriptBeforeTimeout,
56     nullptr,
57     nullptr,
58     nullptr,
59     nullptr,
60     &defaultLanguage
61 };
62
63 JSWorkerGlobalScopeBase::JSWorkerGlobalScopeBase(JSC::VM& vm, JSC::Structure* structure, RefPtr<WorkerGlobalScope>&& impl)
64     : JSDOMGlobalObject(vm, structure, normalWorld(vm), &s_globalObjectMethodTable)
65     , m_wrapped(WTFMove(impl))
66 {
67 }
68
69 void JSWorkerGlobalScopeBase::finishCreation(VM& vm, JSProxy* proxy)
70 {
71     m_proxy.set(vm, this, proxy);
72
73     Base::finishCreation(vm, m_proxy.get());
74     ASSERT(inherits(info()));
75 }
76
77 void JSWorkerGlobalScopeBase::visitChildren(JSCell* cell, SlotVisitor& visitor)
78 {
79     JSWorkerGlobalScopeBase* thisObject = jsCast<JSWorkerGlobalScopeBase*>(cell);
80     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
81     Base::visitChildren(thisObject, visitor);
82     visitor.append(thisObject->m_proxy);
83 }
84
85 void JSWorkerGlobalScopeBase::destroy(JSCell* cell)
86 {
87     static_cast<JSWorkerGlobalScopeBase*>(cell)->JSWorkerGlobalScopeBase::~JSWorkerGlobalScopeBase();
88 }
89
90 ScriptExecutionContext* JSWorkerGlobalScopeBase::scriptExecutionContext() const
91 {
92     return m_wrapped.get();
93 }
94
95 bool JSWorkerGlobalScopeBase::supportsRichSourceInfo(const JSGlobalObject* object)
96 {
97     return JSGlobalObject::supportsRichSourceInfo(object);
98 }
99
100 bool JSWorkerGlobalScopeBase::shouldInterruptScript(const JSGlobalObject* object)
101 {
102     return JSGlobalObject::shouldInterruptScript(object);
103 }
104
105 bool JSWorkerGlobalScopeBase::shouldInterruptScriptBeforeTimeout(const JSGlobalObject* object)
106 {
107     return JSGlobalObject::shouldInterruptScriptBeforeTimeout(object);
108 }
109
110 RuntimeFlags JSWorkerGlobalScopeBase::javaScriptRuntimeFlags(const JSGlobalObject* object)
111 {
112     const JSWorkerGlobalScopeBase *thisObject = jsCast<const JSWorkerGlobalScopeBase*>(object);
113     return thisObject->m_wrapped->thread().runtimeFlags();
114 }
115
116 void JSWorkerGlobalScopeBase::queueTaskToEventLoop(const JSGlobalObject* object, Ref<JSC::Microtask>&& task)
117 {
118     const JSWorkerGlobalScopeBase* thisObject = static_cast<const JSWorkerGlobalScopeBase*>(object);
119     thisObject->scriptExecutionContext()->postTask(JSGlobalObjectTask((JSDOMGlobalObject*)thisObject, WTFMove(task)));
120 }
121
122 JSValue toJS(ExecState* exec, JSDOMGlobalObject*, WorkerGlobalScope& workerGlobalScope)
123 {
124     return toJS(exec, workerGlobalScope);
125 }
126
127 JSValue toJS(ExecState*, WorkerGlobalScope& workerGlobalScope)
128 {
129     WorkerScriptController* script = workerGlobalScope.script();
130     if (!script)
131         return jsNull();
132     JSWorkerGlobalScope* contextWrapper = script->workerGlobalScopeWrapper();
133     ASSERT(contextWrapper);
134     return contextWrapper->proxy();
135 }
136
137 JSDedicatedWorkerGlobalScope* toJSDedicatedWorkerGlobalScope(JSValue value)
138 {
139     if (!value.isObject())
140         return 0;
141     const ClassInfo* classInfo = asObject(value)->classInfo();
142     if (classInfo == JSDedicatedWorkerGlobalScope::info())
143         return jsCast<JSDedicatedWorkerGlobalScope*>(asObject(value));
144     if (classInfo == JSProxy::info())
145         return jsDynamicDowncast<JSDedicatedWorkerGlobalScope*>(jsCast<JSProxy*>(asObject(value))->target());
146     return 0;
147 }
148
149
150 JSWorkerGlobalScope* toJSWorkerGlobalScope(JSValue value)
151 {
152     return toJSDedicatedWorkerGlobalScope(value);
153 }
154
155 } // namespace WebCore