Cleanup: Make QueueTaskToEventLoopFunctionPtr take JSGlobalObject&
[WebKit.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 #include "JSWorkerGlobalScopeBase.h"
30
31 #include "DOMWrapperWorld.h"
32 #include "JSDOMGlobalObjectTask.h"
33 #include "JSDedicatedWorkerGlobalScope.h"
34 #include "JSDynamicDowncast.h"
35 #include "JSWorkerGlobalScope.h"
36 #include "Language.h"
37 #include "WorkerGlobalScope.h"
38 #include "WorkerThread.h"
39 #include <runtime/JSCInlines.h>
40 #include <runtime/JSCJSValueInlines.h>
41 #include <runtime/Microtask.h>
42
43 using namespace JSC;
44
45 namespace WebCore {
46
47 const ClassInfo JSWorkerGlobalScopeBase::s_info = { "WorkerGlobalScope", &JSDOMGlobalObject::s_info, 0, CREATE_METHOD_TABLE(JSWorkerGlobalScopeBase) };
48
49 const GlobalObjectMethodTable JSWorkerGlobalScopeBase::s_globalObjectMethodTable = {
50     &supportsRichSourceInfo,
51     &shouldInterruptScript,
52     &javaScriptRuntimeFlags,
53     &queueTaskToEventLoop,
54     &shouldInterruptScriptBeforeTimeout,
55     nullptr, // moduleLoaderImportModule
56     nullptr, // moduleLoaderResolve
57     nullptr, // moduleLoaderFetch
58     nullptr, // moduleLoaderInstantiate
59     nullptr, // moduleLoaderEvaluate
60     nullptr, // promiseRejectionTracker
61     &defaultLanguage
62 };
63
64 JSWorkerGlobalScopeBase::JSWorkerGlobalScopeBase(JSC::VM& vm, JSC::Structure* structure, RefPtr<WorkerGlobalScope>&& impl)
65     : JSDOMGlobalObject(vm, structure, normalWorld(vm), &s_globalObjectMethodTable)
66     , m_wrapped(WTFMove(impl))
67 {
68 }
69
70 void JSWorkerGlobalScopeBase::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
78 void JSWorkerGlobalScopeBase::visitChildren(JSCell* cell, SlotVisitor& visitor)
79 {
80     JSWorkerGlobalScopeBase* thisObject = jsCast<JSWorkerGlobalScopeBase*>(cell);
81     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
82     Base::visitChildren(thisObject, visitor);
83     visitor.append(thisObject->m_proxy);
84 }
85
86 void JSWorkerGlobalScopeBase::destroy(JSCell* cell)
87 {
88     static_cast<JSWorkerGlobalScopeBase*>(cell)->JSWorkerGlobalScopeBase::~JSWorkerGlobalScopeBase();
89 }
90
91 ScriptExecutionContext* JSWorkerGlobalScopeBase::scriptExecutionContext() const
92 {
93     return m_wrapped.get();
94 }
95
96 bool JSWorkerGlobalScopeBase::supportsRichSourceInfo(const JSGlobalObject* object)
97 {
98     return JSGlobalObject::supportsRichSourceInfo(object);
99 }
100
101 bool JSWorkerGlobalScopeBase::shouldInterruptScript(const JSGlobalObject* object)
102 {
103     return JSGlobalObject::shouldInterruptScript(object);
104 }
105
106 bool JSWorkerGlobalScopeBase::shouldInterruptScriptBeforeTimeout(const JSGlobalObject* object)
107 {
108     return JSGlobalObject::shouldInterruptScriptBeforeTimeout(object);
109 }
110
111 RuntimeFlags JSWorkerGlobalScopeBase::javaScriptRuntimeFlags(const JSGlobalObject* object)
112 {
113     const JSWorkerGlobalScopeBase *thisObject = jsCast<const JSWorkerGlobalScopeBase*>(object);
114     return thisObject->m_wrapped->thread().runtimeFlags();
115 }
116
117 void JSWorkerGlobalScopeBase::queueTaskToEventLoop(JSGlobalObject& object, Ref<JSC::Microtask>&& task)
118 {
119     JSWorkerGlobalScopeBase& thisObject = static_cast<JSWorkerGlobalScopeBase&>(object);
120     thisObject.scriptExecutionContext()->postTask(JSGlobalObjectTask(thisObject, WTFMove(task)));
121 }
122
123 JSValue toJS(ExecState* exec, JSDOMGlobalObject*, WorkerGlobalScope& workerGlobalScope)
124 {
125     return toJS(exec, workerGlobalScope);
126 }
127
128 JSValue toJS(ExecState*, WorkerGlobalScope& workerGlobalScope)
129 {
130     WorkerScriptController* script = workerGlobalScope.script();
131     if (!script)
132         return jsNull();
133     JSWorkerGlobalScope* contextWrapper = script->workerGlobalScopeWrapper();
134     ASSERT(contextWrapper);
135     return contextWrapper->proxy();
136 }
137
138 JSDedicatedWorkerGlobalScope* toJSDedicatedWorkerGlobalScope(VM& vm, JSValue value)
139 {
140     if (!value.isObject())
141         return 0;
142     const ClassInfo* classInfo = asObject(value)->classInfo(vm);
143     if (classInfo == JSDedicatedWorkerGlobalScope::info())
144         return jsCast<JSDedicatedWorkerGlobalScope*>(asObject(value));
145     if (classInfo == JSProxy::info())
146         return jsDynamicDowncast<JSDedicatedWorkerGlobalScope*>(vm, jsCast<JSProxy*>(asObject(value))->target());
147     return 0;
148 }
149
150
151 JSWorkerGlobalScope* toJSWorkerGlobalScope(VM& vm, JSValue value)
152 {
153     return toJSDedicatedWorkerGlobalScope(vm, value);
154 }
155
156 } // namespace WebCore