1/*
2 This file is part of the WebKit open source project.
3 This file has been generated by generate-bindings.pl. DO NOT MODIFY!
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "config.h"
22#include "JSEventTarget.h"
23
24#include "JSDOMBinding.h"
25#include "JSDOMConstructorNotConstructable.h"
26#include "JSDOMConvertBoolean.h"
27#include "JSDOMConvertEventListener.h"
28#include "JSDOMConvertInterface.h"
29#include "JSDOMConvertNullable.h"
30#include "JSDOMConvertStrings.h"
31#include "JSDOMConvertUnion.h"
32#include "JSDOMExceptionHandling.h"
33#include "JSDOMOperation.h"
34#include "JSDOMWrapperCache.h"
35#include "JSEvent.h"
36#include "JSEventListener.h"
37#include "ScriptExecutionContext.h"
38#include <JavaScriptCore/FunctionPrototype.h>
39#include <JavaScriptCore/HeapSnapshotBuilder.h>
40#include <JavaScriptCore/JSCInlines.h>
41#include <wtf/GetPtr.h>
42#include <wtf/PointerPreparations.h>
43#include <wtf/URL.h>
44#include <wtf/Variant.h>
45
46
47namespace WebCore {
48using namespace JSC;
49
50template<> EventTarget::ListenerOptions convertDictionary<EventTarget::ListenerOptions>(ExecState& state, JSValue value)
51{
52 VM& vm = state.vm();
53 auto throwScope = DECLARE_THROW_SCOPE(vm);
54 bool isNullOrUndefined = value.isUndefinedOrNull();
55 auto* object = isNullOrUndefined ? nullptr : value.getObject();
56 if (UNLIKELY(!isNullOrUndefined && !object)) {
57 throwTypeError(&state, throwScope);
58 return { };
59 }
60 EventTarget::ListenerOptions result;
61 JSValue captureValue;
62 if (isNullOrUndefined)
63 captureValue = jsUndefined();
64 else {
65 captureValue = object->get(&state, Identifier::fromString(&state, "capture"));
66 RETURN_IF_EXCEPTION(throwScope, { });
67 }
68 if (!captureValue.isUndefined()) {
69 result.capture = convert<IDLBoolean>(state, captureValue);
70 RETURN_IF_EXCEPTION(throwScope, { });
71 } else
72 result.capture = false;
73 return result;
74}
75
76template<> EventTarget::AddEventListenerOptions convertDictionary<EventTarget::AddEventListenerOptions>(ExecState& state, JSValue value)
77{
78 VM& vm = state.vm();
79 auto throwScope = DECLARE_THROW_SCOPE(vm);
80 bool isNullOrUndefined = value.isUndefinedOrNull();
81 auto* object = isNullOrUndefined ? nullptr : value.getObject();
82 if (UNLIKELY(!isNullOrUndefined && !object)) {
83 throwTypeError(&state, throwScope);
84 return { };
85 }
86 EventTarget::AddEventListenerOptions result;
87 JSValue captureValue;
88 if (isNullOrUndefined)
89 captureValue = jsUndefined();
90 else {
91 captureValue = object->get(&state, Identifier::fromString(&state, "capture"));
92 RETURN_IF_EXCEPTION(throwScope, { });
93 }
94 if (!captureValue.isUndefined()) {
95 result.capture = convert<IDLBoolean>(state, captureValue);
96 RETURN_IF_EXCEPTION(throwScope, { });
97 } else
98 result.capture = false;
99 JSValue onceValue;
100 if (isNullOrUndefined)
101 onceValue = jsUndefined();
102 else {
103 onceValue = object->get(&state, Identifier::fromString(&state, "once"));
104 RETURN_IF_EXCEPTION(throwScope, { });
105 }
106 if (!onceValue.isUndefined()) {
107 result.once = convert<IDLBoolean>(state, onceValue);
108 RETURN_IF_EXCEPTION(throwScope, { });
109 } else
110 result.once = false;
111 JSValue passiveValue;
112 if (isNullOrUndefined)
113 passiveValue = jsUndefined();
114 else {
115 passiveValue = object->get(&state, Identifier::fromString(&state, "passive"));
116 RETURN_IF_EXCEPTION(throwScope, { });
117 }
118 if (!passiveValue.isUndefined()) {
119 result.passive = convert<IDLBoolean>(state, passiveValue);
120 RETURN_IF_EXCEPTION(throwScope, { });
121 }
122 return result;
123}
124
125// Functions
126
127JSC::EncodedJSValue JSC_HOST_CALL jsEventTargetPrototypeFunctionAddEventListener(JSC::ExecState*);
128JSC::EncodedJSValue JSC_HOST_CALL jsEventTargetPrototypeFunctionRemoveEventListener(JSC::ExecState*);
129JSC::EncodedJSValue JSC_HOST_CALL jsEventTargetPrototypeFunctionDispatchEvent(JSC::ExecState*);
130
131// Attributes
132
133JSC::EncodedJSValue jsEventTargetConstructor(JSC::ExecState*, JSC::EncodedJSValue, JSC::PropertyName);
134bool setJSEventTargetConstructor(JSC::ExecState*, JSC::EncodedJSValue, JSC::EncodedJSValue);
135
136class JSEventTargetPrototype : public JSC::JSNonFinalObject {
137public:
138 using Base = JSC::JSNonFinalObject;
139 static JSEventTargetPrototype* create(JSC::VM& vm, JSDOMGlobalObject* globalObject, JSC::Structure* structure)
140 {
141 JSEventTargetPrototype* ptr = new (NotNull, JSC::allocateCell<JSEventTargetPrototype>(vm.heap)) JSEventTargetPrototype(vm, globalObject, structure);
142 ptr->finishCreation(vm);
143 return ptr;
144 }
145
146 DECLARE_INFO;
147 static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
148 {
149 return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
150 }
151
152private:
153 JSEventTargetPrototype(JSC::VM& vm, JSC::JSGlobalObject*, JSC::Structure* structure)
154 : JSC::JSNonFinalObject(vm, structure)
155 {
156 }
157
158 void finishCreation(JSC::VM&);
159public:
160 static const unsigned StructureFlags = Base::StructureFlags | JSC::IsImmutablePrototypeExoticObject;
161};
162
163using JSEventTargetConstructor = JSDOMConstructorNotConstructable<JSEventTarget>;
164
165template<> JSValue JSEventTargetConstructor::prototypeForStructure(JSC::VM& vm, const JSDOMGlobalObject& globalObject)
166{
167 UNUSED_PARAM(vm);
168 return globalObject.functionPrototype();
169}
170
171template<> void JSEventTargetConstructor::initializeProperties(VM& vm, JSDOMGlobalObject& globalObject)
172{
173 putDirect(vm, vm.propertyNames->prototype, JSEventTarget::prototype(vm, globalObject), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum);
174 putDirect(vm, vm.propertyNames->name, jsNontrivialString(&vm, String("EventTarget"_s)), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum);
175 putDirect(vm, vm.propertyNames->length, jsNumber(0), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum);
176}
177
178template<> const ClassInfo JSEventTargetConstructor::s_info = { "EventTarget", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSEventTargetConstructor) };
179
180/* Hash table for prototype */
181
182static const HashTableValue JSEventTargetPrototypeTableValues[] =
183{
184 { "constructor", static_cast<unsigned>(JSC::PropertyAttribute::DontEnum), NoIntrinsic, { (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsEventTargetConstructor), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(setJSEventTargetConstructor) } },
185 { "addEventListener", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t)static_cast<RawNativeFunction>(jsEventTargetPrototypeFunctionAddEventListener), (intptr_t) (2) } },
186 { "removeEventListener", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t)static_cast<RawNativeFunction>(jsEventTargetPrototypeFunctionRemoveEventListener), (intptr_t) (2) } },
187 { "dispatchEvent", static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t)static_cast<RawNativeFunction>(jsEventTargetPrototypeFunctionDispatchEvent), (intptr_t) (1) } },
188};
189
190const ClassInfo JSEventTargetPrototype::s_info = { "EventTargetPrototype", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSEventTargetPrototype) };
191
192void JSEventTargetPrototype::finishCreation(VM& vm)
193{
194 Base::finishCreation(vm);
195 reifyStaticProperties(vm, JSEventTarget::info(), JSEventTargetPrototypeTableValues, *this);
196}
197
198const ClassInfo JSEventTarget::s_info = { "EventTarget", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSEventTarget) };
199
200JSEventTarget::JSEventTarget(Structure* structure, JSDOMGlobalObject& globalObject, Ref<EventTarget>&& impl)
201 : JSDOMWrapper<EventTarget>(structure, globalObject, WTFMove(impl))
202{
203}
204
205void JSEventTarget::finishCreation(VM& vm)
206{
207 Base::finishCreation(vm);
208 ASSERT(inherits(vm, info()));
209
210}
211
212JSObject* JSEventTarget::createPrototype(VM& vm, JSDOMGlobalObject& globalObject)
213{
214 return JSEventTargetPrototype::create(vm, &globalObject, JSEventTargetPrototype::createStructure(vm, &globalObject, globalObject.objectPrototype()));
215}
216
217JSObject* JSEventTarget::prototype(VM& vm, JSDOMGlobalObject& globalObject)
218{
219 return getDOMPrototype<JSEventTarget>(vm, globalObject);
220}
221
222JSValue JSEventTarget::getConstructor(VM& vm, const JSGlobalObject* globalObject)
223{
224 return getDOMConstructor<JSEventTargetConstructor>(vm, *jsCast<const JSDOMGlobalObject*>(globalObject));
225}
226
227void JSEventTarget::destroy(JSC::JSCell* cell)
228{
229 JSEventTarget* thisObject = static_cast<JSEventTarget*>(cell);
230 thisObject->JSEventTarget::~JSEventTarget();
231}
232
233EncodedJSValue jsEventTargetConstructor(ExecState* state, EncodedJSValue thisValue, PropertyName)
234{
235 VM& vm = state->vm();
236 auto throwScope = DECLARE_THROW_SCOPE(vm);
237 auto* prototype = jsDynamicCast<JSEventTargetPrototype*>(vm, JSValue::decode(thisValue));
238 if (UNLIKELY(!prototype))
239 return throwVMTypeError(state, throwScope);
240 return JSValue::encode(JSEventTarget::getConstructor(state->vm(), prototype->globalObject()));
241}
242
243bool setJSEventTargetConstructor(ExecState* state, EncodedJSValue thisValue, EncodedJSValue encodedValue)
244{
245 VM& vm = state->vm();
246 auto throwScope = DECLARE_THROW_SCOPE(vm);
247 auto* prototype = jsDynamicCast<JSEventTargetPrototype*>(vm, JSValue::decode(thisValue));
248 if (UNLIKELY(!prototype)) {
249 throwVMTypeError(state, throwScope);
250 return false;
251 }
252 // Shadowing a built-in constructor
253 return prototype->putDirect(vm, vm.propertyNames->constructor, JSValue::decode(encodedValue));
254}
255
256static inline JSC::EncodedJSValue jsEventTargetPrototypeFunctionAddEventListenerBody(JSC::ExecState* state, typename IDLOperation<JSEventTarget>::ClassParameter castedThis, JSC::ThrowScope& throwScope)
257{
258 UNUSED_PARAM(state);
259 UNUSED_PARAM(throwScope);
260 auto& impl = castedThis->wrapped();
261 if (UNLIKELY(state->argumentCount() < 2))
262 return throwVMError(state, throwScope, createNotEnoughArgumentsError(state));
263 auto type = convert<IDLAtomicStringAdaptor<IDLDOMString>>(*state, state->uncheckedArgument(0));
264 RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
265 auto callback = convert<IDLNullable<IDLEventListener<JSEventListener>>>(*state, state->uncheckedArgument(1), *castedThis);
266 RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
267 auto options = state->argument(2).isUndefined() ? false : convert<IDLUnion<IDLDictionary<EventTarget::AddEventListenerOptions>, IDLBoolean>>(*state, state->uncheckedArgument(2));
268 RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
269 impl.addEventListenerForBindings(WTFMove(type), WTFMove(callback), WTFMove(options));
270 return JSValue::encode(jsUndefined());
271}
272
273EncodedJSValue JSC_HOST_CALL jsEventTargetPrototypeFunctionAddEventListener(ExecState* state)
274{
275 return IDLOperation<JSEventTarget>::call<jsEventTargetPrototypeFunctionAddEventListenerBody>(*state, "addEventListener");
276}
277
278static inline JSC::EncodedJSValue jsEventTargetPrototypeFunctionRemoveEventListenerBody(JSC::ExecState* state, typename IDLOperation<JSEventTarget>::ClassParameter castedThis, JSC::ThrowScope& throwScope)
279{
280 UNUSED_PARAM(state);
281 UNUSED_PARAM(throwScope);
282 auto& impl = castedThis->wrapped();
283 if (UNLIKELY(state->argumentCount() < 2))
284 return throwVMError(state, throwScope, createNotEnoughArgumentsError(state));
285 auto type = convert<IDLAtomicStringAdaptor<IDLDOMString>>(*state, state->uncheckedArgument(0));
286 RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
287 auto callback = convert<IDLNullable<IDLEventListener<JSEventListener>>>(*state, state->uncheckedArgument(1), *castedThis);
288 RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
289 auto options = state->argument(2).isUndefined() ? false : convert<IDLUnion<IDLDictionary<EventTarget::ListenerOptions>, IDLBoolean>>(*state, state->uncheckedArgument(2));
290 RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
291 impl.removeEventListenerForBindings(WTFMove(type), WTFMove(callback), WTFMove(options));
292 return JSValue::encode(jsUndefined());
293}
294
295EncodedJSValue JSC_HOST_CALL jsEventTargetPrototypeFunctionRemoveEventListener(ExecState* state)
296{
297 return IDLOperation<JSEventTarget>::call<jsEventTargetPrototypeFunctionRemoveEventListenerBody>(*state, "removeEventListener");
298}
299
300static inline JSC::EncodedJSValue jsEventTargetPrototypeFunctionDispatchEventBody(JSC::ExecState* state, typename IDLOperation<JSEventTarget>::ClassParameter castedThis, JSC::ThrowScope& throwScope)
301{
302 UNUSED_PARAM(state);
303 UNUSED_PARAM(throwScope);
304 auto& impl = castedThis->wrapped();
305 if (UNLIKELY(state->argumentCount() < 1))
306 return throwVMError(state, throwScope, createNotEnoughArgumentsError(state));
307 auto event = convert<IDLInterface<Event>>(*state, state->uncheckedArgument(0), [](JSC::ExecState& state, JSC::ThrowScope& scope) { throwArgumentTypeError(state, scope, 0, "event", "EventTarget", "dispatchEvent", "Event"); });
308 RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
309 return JSValue::encode(toJS<IDLBoolean>(*state, throwScope, impl.dispatchEventForBindings(*event)));
310}
311
312EncodedJSValue JSC_HOST_CALL jsEventTargetPrototypeFunctionDispatchEvent(ExecState* state)
313{
314 return IDLOperation<JSEventTarget>::call<jsEventTargetPrototypeFunctionDispatchEventBody>(*state, "dispatchEvent");
315}
316
317void JSEventTarget::visitChildren(JSCell* cell, SlotVisitor& visitor)
318{
319 auto* thisObject = jsCast<JSEventTarget*>(cell);
320 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
321 Base::visitChildren(thisObject, visitor);
322 thisObject->visitAdditionalChildren(visitor);
323}
324
325void JSEventTarget::visitOutputConstraints(JSCell* cell, SlotVisitor& visitor)
326{
327 auto* thisObject = jsCast<JSEventTarget*>(cell);
328 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
329 Base::visitOutputConstraints(thisObject, visitor);
330 thisObject->visitAdditionalChildren(visitor);
331}
332
333void JSEventTarget::heapSnapshot(JSCell* cell, HeapSnapshotBuilder& builder)
334{
335 auto* thisObject = jsCast<JSEventTarget*>(cell);
336 builder.setWrappedObjectForCell(cell, &thisObject->wrapped());
337 if (thisObject->scriptExecutionContext())
338 builder.setLabelForCell(cell, "url " + thisObject->scriptExecutionContext()->url().string());
339 Base::heapSnapshot(cell, builder);
340}
341
342bool JSEventTargetOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor, const char** reason)
343{
344 auto* jsEventTarget = jsCast<JSEventTarget*>(handle.slot()->asCell());
345 if (jsEventTarget->wrapped().isFiringEventListeners()) {
346 if (UNLIKELY(reason))
347 *reason = "EventTarget firing event listeners";
348 return true;
349 }
350 UNUSED_PARAM(visitor);
351 UNUSED_PARAM(reason);
352 return false;
353}
354
355void JSEventTargetOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
356{
357 auto* jsEventTarget = static_cast<JSEventTarget*>(handle.slot()->asCell());
358 auto& world = *static_cast<DOMWrapperWorld*>(context);
359 uncacheWrapper(world, &jsEventTarget->wrapped(), jsEventTarget);
360}
361
362
363}
364