Line data Source code
1 : /* THIS FILE IS AUTOGENERATED FROM GamepadEvent.webidl BY Codegen.py - DO NOT EDIT */
2 :
3 : #include "AtomList.h"
4 : #include "EventBinding.h"
5 : #include "GamepadEventBinding.h"
6 : #include "WrapperFactory.h"
7 : #include "mozilla/OwningNonNull.h"
8 : #include "mozilla/Preferences.h"
9 : #include "mozilla/dom/BindingUtils.h"
10 : #include "mozilla/dom/DOMJSClass.h"
11 : #include "mozilla/dom/Gamepad.h"
12 : #include "mozilla/dom/GamepadEvent.h"
13 : #include "mozilla/dom/NonRefcountedDOMObject.h"
14 : #include "mozilla/dom/Nullable.h"
15 : #include "mozilla/dom/PrimitiveConversions.h"
16 : #include "mozilla/dom/ScriptSettings.h"
17 : #include "mozilla/dom/XrayExpandoClass.h"
18 :
19 : namespace mozilla {
20 : namespace dom {
21 :
22 :
23 0 : GamepadEventInit::GamepadEventInit()
24 0 : : EventInit(FastDictionaryInitializer())
25 : {
26 : // Safe to pass a null context if we pass a null value
27 0 : Init(nullptr, JS::NullHandleValue);
28 0 : }
29 :
30 :
31 :
32 : bool
33 0 : GamepadEventInit::InitIds(JSContext* cx, GamepadEventInitAtoms* atomsCache)
34 : {
35 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
36 :
37 : // Initialize these in reverse order so that any failure leaves the first one
38 : // uninitialized.
39 0 : if (!atomsCache->gamepad_id.init(cx, "gamepad")) {
40 0 : return false;
41 : }
42 0 : return true;
43 : }
44 :
45 : bool
46 0 : GamepadEventInit::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
47 : {
48 : // Passing a null JSContext is OK only if we're initing from null,
49 : // Since in that case we will not have to do any property gets
50 : // Also evaluate isNullOrUndefined in order to avoid false-positive
51 : // checkers by static analysis tools
52 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
53 0 : GamepadEventInitAtoms* atomsCache = nullptr;
54 0 : if (cx) {
55 0 : atomsCache = GetAtomCache<GamepadEventInitAtoms>(cx);
56 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
57 0 : return false;
58 : }
59 : }
60 :
61 : // Per spec, we init the parent's members first
62 0 : if (!EventInit::Init(cx, val)) {
63 0 : return false;
64 : }
65 :
66 0 : bool isNull = val.isNullOrUndefined();
67 : // We only need these if !isNull, in which case we have |cx|.
68 0 : Maybe<JS::Rooted<JSObject *> > object;
69 0 : Maybe<JS::Rooted<JS::Value> > temp;
70 0 : if (!isNull) {
71 0 : MOZ_ASSERT(cx);
72 0 : object.emplace(cx, &val.toObject());
73 0 : temp.emplace(cx);
74 : }
75 0 : if (!isNull) {
76 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->gamepad_id, temp.ptr())) {
77 0 : return false;
78 : }
79 : }
80 0 : if (!isNull && !temp->isUndefined()) {
81 0 : if (temp.ref().isObject()) {
82 : static_assert(IsRefcounted<mozilla::dom::Gamepad>::value, "We can only store refcounted classes.");{
83 0 : nsresult rv = UnwrapObject<prototypes::id::Gamepad, mozilla::dom::Gamepad>(temp.ptr(), mGamepad);
84 0 : if (NS_FAILED(rv)) {
85 0 : ThrowErrorMessage(cx, MSG_DOES_NOT_IMPLEMENT_INTERFACE, "'gamepad' member of GamepadEventInit", "Gamepad");
86 0 : return false;
87 : }
88 : }
89 0 : } else if (temp.ref().isNullOrUndefined()) {
90 0 : mGamepad = nullptr;
91 : } else {
92 0 : ThrowErrorMessage(cx, MSG_NOT_OBJECT, "'gamepad' member of GamepadEventInit");
93 0 : return false;
94 : }
95 : } else {
96 0 : mGamepad = nullptr;
97 : }
98 0 : mIsAnyMemberPresent = true;
99 0 : return true;
100 : }
101 :
102 : bool
103 0 : GamepadEventInit::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
104 : {
105 0 : GamepadEventInitAtoms* atomsCache = GetAtomCache<GamepadEventInitAtoms>(cx);
106 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
107 0 : return false;
108 : }
109 :
110 : // Per spec, we define the parent's members first
111 0 : if (!EventInit::ToObjectInternal(cx, rval)) {
112 0 : return false;
113 : }
114 0 : JS::Rooted<JSObject*> obj(cx, &rval.toObject());
115 :
116 : do {
117 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
118 0 : JS::Rooted<JS::Value> temp(cx);
119 0 : RefPtr<mozilla::dom::Gamepad> const & currentValue = mGamepad;
120 0 : if (!currentValue) {
121 0 : temp.setNull();
122 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->gamepad_id, temp, JSPROP_ENUMERATE)) {
123 0 : return false;
124 : }
125 0 : break;
126 : }
127 0 : if (!GetOrCreateDOMReflector(cx, currentValue, &temp)) {
128 0 : MOZ_ASSERT(true || JS_IsExceptionPending(cx));
129 0 : return false;
130 : }
131 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->gamepad_id, temp, JSPROP_ENUMERATE)) {
132 0 : return false;
133 : }
134 0 : break;
135 : } while(0);
136 :
137 0 : return true;
138 : }
139 :
140 : void
141 0 : GamepadEventInit::TraceDictionary(JSTracer* trc)
142 : {
143 0 : EventInit::TraceDictionary(trc);
144 0 : }
145 :
146 : GamepadEventInit&
147 0 : GamepadEventInit::operator=(const GamepadEventInit& aOther)
148 : {
149 0 : EventInit::operator=(aOther);
150 0 : mGamepad = aOther.mGamepad;
151 0 : return *this;
152 : }
153 :
154 : namespace binding_detail {
155 : } // namespace binding_detail
156 :
157 :
158 : namespace GamepadEventBinding {
159 :
160 : static_assert(IsRefcounted<NativeType>::value == IsRefcounted<EventBinding::NativeType>::value,
161 : "Can't inherit from an interface with a different ownership model.");
162 :
163 : static bool
164 0 : get_gamepad(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::GamepadEvent* self, JSJitGetterCallArgs args)
165 : {
166 0 : auto result(StrongOrRawPtr<mozilla::dom::Gamepad>(self->GetGamepad()));
167 0 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
168 0 : if (!result) {
169 0 : args.rval().setNull();
170 0 : return true;
171 : }
172 0 : if (!GetOrCreateDOMReflector(cx, result, args.rval())) {
173 0 : MOZ_ASSERT(true || JS_IsExceptionPending(cx));
174 0 : return false;
175 : }
176 0 : return true;
177 : }
178 :
179 : static const JSJitInfo gamepad_getterinfo = {
180 : { (JSJitGetterOp)get_gamepad },
181 : { prototypes::id::GamepadEvent },
182 : { PrototypeTraits<prototypes::id::GamepadEvent>::Depth },
183 : JSJitInfo::Getter,
184 : JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */
185 : JSVAL_TYPE_UNKNOWN, /* returnType. Not relevant for setters. */
186 : false, /* isInfallible. False in setters. */
187 : false, /* isMovable. Not relevant for setters. */
188 : false, /* isEliminatable. Not relevant for setters. */
189 : false, /* isAlwaysInSlot. Only relevant for getters. */
190 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
191 : false, /* isTypedMethod. Only relevant for methods. */
192 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
193 : };
194 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
195 : static_assert(0 < 1, "There is no slot for us");
196 :
197 : static bool
198 0 : get_isTrusted(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::GamepadEvent* self, JSJitGetterCallArgs args)
199 : {
200 0 : bool result(self->IsTrusted());
201 0 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
202 0 : args.rval().setBoolean(result);
203 0 : return true;
204 : }
205 :
206 : static const JSJitInfo isTrusted_getterinfo = {
207 : { (JSJitGetterOp)get_isTrusted },
208 : { prototypes::id::GamepadEvent },
209 : { PrototypeTraits<prototypes::id::GamepadEvent>::Depth },
210 : JSJitInfo::Getter,
211 : JSJitInfo::AliasDOMSets, /* aliasSet. Not relevant for setters. */
212 : JSVAL_TYPE_BOOLEAN, /* returnType. Not relevant for setters. */
213 : true, /* isInfallible. False in setters. */
214 : true, /* isMovable. Not relevant for setters. */
215 : true, /* isEliminatable. Not relevant for setters. */
216 : false, /* isAlwaysInSlot. Only relevant for getters. */
217 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
218 : false, /* isTypedMethod. Only relevant for methods. */
219 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
220 : };
221 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
222 : static_assert(0 < 1, "There is no slot for us");
223 :
224 : static bool
225 0 : _addProperty(JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::Handle<JS::Value> val)
226 : {
227 0 : mozilla::dom::GamepadEvent* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::GamepadEvent>(obj);
228 : // We don't want to preserve if we don't have a wrapper, and we
229 : // obviously can't preserve if we're not initialized.
230 0 : if (self && self->GetWrapperPreserveColor()) {
231 0 : PreserveWrapper(self);
232 : }
233 0 : return true;
234 : }
235 :
236 : static void
237 0 : _finalize(js::FreeOp* fop, JSObject* obj)
238 : {
239 0 : mozilla::dom::GamepadEvent* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::GamepadEvent>(obj);
240 0 : if (self) {
241 0 : ClearWrapper(self, self, obj);
242 0 : AddForDeferredFinalization<mozilla::dom::GamepadEvent>(self);
243 : }
244 0 : }
245 :
246 : static void
247 0 : _objectMoved(JSObject* obj, const JSObject* old)
248 : {
249 0 : mozilla::dom::GamepadEvent* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::GamepadEvent>(obj);
250 0 : if (self) {
251 0 : UpdateWrapper(self, self, obj, old);
252 : }
253 0 : }
254 :
255 : // We deliberately use brace-elision to make Visual Studio produce better initalization code.
256 : #if defined(__clang__)
257 : #pragma clang diagnostic push
258 : #pragma clang diagnostic ignored "-Wmissing-braces"
259 : #endif
260 : static const JSPropertySpec sAttributes_specs[] = {
261 : { "gamepad", JSPROP_SHARED | JSPROP_ENUMERATE, GenericBindingGetter, &gamepad_getterinfo, nullptr, nullptr },
262 : { nullptr, 0, nullptr, nullptr, nullptr, nullptr }
263 : };
264 : #if defined(__clang__)
265 : #pragma clang diagnostic pop
266 : #endif
267 :
268 :
269 : // Can't be const because the pref-enabled boolean needs to be writable
270 : static Prefable<const JSPropertySpec> sAttributes[] = {
271 : { nullptr, &sAttributes_specs[0] },
272 : { nullptr, nullptr }
273 : };
274 :
275 : static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
276 : "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
277 : static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
278 : "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
279 :
280 : // We deliberately use brace-elision to make Visual Studio produce better initalization code.
281 : #if defined(__clang__)
282 : #pragma clang diagnostic push
283 : #pragma clang diagnostic ignored "-Wmissing-braces"
284 : #endif
285 : static const JSPropertySpec sUnforgeableAttributes_specs[] = {
286 : { "isTrusted", JSPROP_SHARED | JSPROP_ENUMERATE | JSPROP_PERMANENT, GenericBindingGetter, &isTrusted_getterinfo, nullptr, nullptr },
287 : { nullptr, 0, nullptr, nullptr, nullptr, nullptr }
288 : };
289 : #if defined(__clang__)
290 : #pragma clang diagnostic pop
291 : #endif
292 :
293 :
294 : // Can't be const because the pref-enabled boolean needs to be writable
295 : static Prefable<const JSPropertySpec> sUnforgeableAttributes[] = {
296 : { nullptr, &sUnforgeableAttributes_specs[0] },
297 : { nullptr, nullptr }
298 : };
299 :
300 : static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
301 : "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
302 : static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
303 : "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
304 :
305 :
306 : static uint16_t sNativeProperties_sortedPropertyIndices[2];
307 : static PropertyInfo sNativeProperties_propertyInfos[2];
308 :
309 : static const NativePropertiesN<2> sNativeProperties = {
310 : false, 0,
311 : false, 0,
312 : false, 0,
313 : true, 0 /* sAttributes */,
314 : false, 0,
315 : true, 1 /* sUnforgeableAttributes */,
316 : false, 0,
317 : -1,
318 : 2,
319 : sNativeProperties_sortedPropertyIndices,
320 : {
321 : { sAttributes, &sNativeProperties_propertyInfos[0] },
322 : { sUnforgeableAttributes, &sNativeProperties_propertyInfos[1] }
323 : }
324 : };
325 : static_assert(2 < 1ull << CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount),
326 : "We have a property info count that is oversized");
327 :
328 : static bool
329 0 : _constructor(JSContext* cx, unsigned argc, JS::Value* vp)
330 : {
331 0 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
332 0 : JS::Rooted<JSObject*> obj(cx, &args.callee());
333 0 : if (!args.isConstructing()) {
334 : // XXXbz wish I could get the name from the callee instead of
335 : // Adding more relocations
336 0 : return ThrowConstructorWithoutNew(cx, "GamepadEvent");
337 : }
338 :
339 0 : GlobalObject global(cx, obj);
340 0 : if (global.Failed()) {
341 0 : return false;
342 : }
343 :
344 0 : JS::Rooted<JSObject*> desiredProto(cx);
345 0 : if (!GetDesiredProto(cx, args, &desiredProto)) {
346 0 : return false;
347 : }
348 :
349 0 : if (MOZ_UNLIKELY(args.length() < 1)) {
350 0 : return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "GamepadEvent");
351 : }
352 0 : bool objIsXray = xpc::WrapperFactory::IsXrayWrapper(obj);
353 0 : binding_detail::FakeString arg0;
354 0 : if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
355 0 : return false;
356 : }
357 0 : binding_detail::FastGamepadEventInit arg1;
358 0 : if (!arg1.Init(cx, (args.hasDefined(1)) ? args[1] : JS::NullHandleValue, "Argument 2 of GamepadEvent.constructor", false)) {
359 0 : return false;
360 : }
361 0 : Maybe<JSAutoCompartment> ac;
362 0 : if (objIsXray) {
363 0 : obj = js::CheckedUnwrap(obj);
364 0 : if (!obj) {
365 0 : return false;
366 : }
367 0 : ac.emplace(cx, obj);
368 0 : if (!JS_WrapObject(cx, &desiredProto)) {
369 0 : return false;
370 : }
371 : }
372 0 : binding_detail::FastErrorResult rv;
373 0 : auto result(StrongOrRawPtr<mozilla::dom::GamepadEvent>(mozilla::dom::GamepadEvent::Constructor(global, NonNullHelper(Constify(arg0)), Constify(arg1), rv)));
374 0 : if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
375 0 : return false;
376 : }
377 0 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
378 : static_assert(!IsPointer<decltype(result)>::value,
379 : "NewObject implies that we need to keep the object alive with a strong reference.");
380 0 : if (!GetOrCreateDOMReflector(cx, result, args.rval(), desiredProto)) {
381 0 : MOZ_ASSERT(true || JS_IsExceptionPending(cx));
382 0 : return false;
383 : }
384 0 : return true;
385 : }
386 :
387 : static const js::ClassOps sInterfaceObjectClassOps = {
388 : nullptr, /* addProperty */
389 : nullptr, /* delProperty */
390 : nullptr, /* getProperty */
391 : nullptr, /* setProperty */
392 : nullptr, /* enumerate */
393 : nullptr, /* newEnumerate */
394 : nullptr, /* resolve */
395 : nullptr, /* mayResolve */
396 : nullptr, /* finalize */
397 : _constructor, /* call */
398 : nullptr, /* hasInstance */
399 : _constructor, /* construct */
400 : nullptr, /* trace */
401 : };
402 :
403 : static const DOMIfaceAndProtoJSClass sInterfaceObjectClass = {
404 : {
405 : "Function",
406 : JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_SLOTS_BASE),
407 : &sInterfaceObjectClassOps,
408 : JS_NULL_CLASS_SPEC,
409 : JS_NULL_CLASS_EXT,
410 : &sInterfaceObjectClassObjectOps
411 : },
412 : eInterface,
413 : true,
414 : prototypes::id::GamepadEvent,
415 : PrototypeTraits<prototypes::id::GamepadEvent>::Depth,
416 : sNativePropertyHooks,
417 : "function GamepadEvent() {\n [native code]\n}",
418 : EventBinding::GetConstructorObject
419 : };
420 :
421 : static const DOMIfaceAndProtoJSClass sPrototypeClass = {
422 : {
423 : "GamepadEventPrototype",
424 : JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE + 1 /* slot for the JSObject holding the unforgeable properties */),
425 : JS_NULL_CLASS_OPS,
426 : JS_NULL_CLASS_SPEC,
427 : JS_NULL_CLASS_EXT,
428 : JS_NULL_OBJECT_OPS
429 : },
430 : eInterfacePrototype,
431 : false,
432 : prototypes::id::GamepadEvent,
433 : PrototypeTraits<prototypes::id::GamepadEvent>::Depth,
434 : sNativePropertyHooks,
435 : "[object GamepadEventPrototype]",
436 : EventBinding::GetProtoObject
437 : };
438 :
439 : bool
440 0 : ConstructorEnabled(JSContext* aCx, JS::Handle<JSObject*> aObj)
441 : {
442 : static bool sPrefValue;
443 : static bool sPrefCacheSetUp = false;
444 0 : if (!sPrefCacheSetUp) {
445 0 : sPrefCacheSetUp = true;
446 0 : Preferences::AddBoolVarCache(&sPrefValue, "dom.gamepad.enabled");
447 : }
448 :
449 0 : return sPrefValue;
450 : }
451 :
452 : JSObject*
453 0 : DefineDOMInterface(JSContext* aCx, JS::Handle<JSObject*> aGlobal, JS::Handle<jsid> id, bool aDefineOnGlobal)
454 : {
455 0 : return GetConstructorObjectHandle(aCx, aDefineOnGlobal);
456 : }
457 :
458 : static const js::ClassOps sClassOps = {
459 : _addProperty, /* addProperty */
460 : nullptr, /* delProperty */
461 : nullptr, /* getProperty */
462 : nullptr, /* setProperty */
463 : nullptr, /* enumerate */
464 : nullptr, /* newEnumerate */
465 : nullptr, /* resolve */
466 : nullptr, /* mayResolve */
467 : _finalize, /* finalize */
468 : nullptr, /* call */
469 : nullptr, /* hasInstance */
470 : nullptr, /* construct */
471 : nullptr, /* trace */
472 : };
473 :
474 : static const js::ClassExtension sClassExtension = {
475 : nullptr, /* weakmapKeyDelegateOp */
476 : _objectMoved /* objectMovedOp */
477 : };
478 :
479 : static const DOMJSClass sClass = {
480 : { "GamepadEvent",
481 : JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_SKIP_NURSERY_FINALIZE,
482 : &sClassOps,
483 : JS_NULL_CLASS_SPEC,
484 : &sClassExtension,
485 : JS_NULL_OBJECT_OPS
486 : },
487 : { prototypes::id::Event, prototypes::id::GamepadEvent, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count },
488 : IsBaseOf<nsISupports, mozilla::dom::GamepadEvent >::value,
489 : sNativePropertyHooks,
490 : FindAssociatedGlobalForNative<mozilla::dom::GamepadEvent>::Get,
491 : GetProtoObjectHandle,
492 : GetCCParticipant<mozilla::dom::GamepadEvent>::Get()
493 : };
494 : static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS,
495 : "Must have the right minimal number of reserved slots.");
496 : static_assert(1 >= 1,
497 : "Must have enough reserved slots.");
498 :
499 : const JSClass*
500 0 : GetJSClass()
501 : {
502 0 : return sClass.ToJSClass();
503 : }
504 :
505 : bool
506 0 : Wrap(JSContext* aCx, mozilla::dom::GamepadEvent* aObject, nsWrapperCache* aCache, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
507 : {
508 : MOZ_ASSERT(static_cast<mozilla::dom::GamepadEvent*>(aObject) ==
509 : reinterpret_cast<mozilla::dom::GamepadEvent*>(aObject),
510 : "Multiple inheritance for mozilla::dom::GamepadEvent is broken.");
511 : MOZ_ASSERT(static_cast<mozilla::dom::Event*>(aObject) ==
512 : reinterpret_cast<mozilla::dom::Event*>(aObject),
513 : "Multiple inheritance for mozilla::dom::Event is broken.");
514 0 : MOZ_ASSERT(ToSupportsIsCorrect(aObject));
515 0 : MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx));
516 0 : MOZ_ASSERT(!aCache->GetWrapper(),
517 : "You should probably not be using Wrap() directly; use "
518 : "GetOrCreateDOMReflector instead");
519 :
520 0 : MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache),
521 : "nsISupports must be on our primary inheritance chain");
522 :
523 0 : JS::Rooted<JSObject*> global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject()));
524 0 : if (!global) {
525 0 : return false;
526 : }
527 0 : MOZ_ASSERT(JS_IsGlobalObject(global));
528 0 : MOZ_ASSERT(JS::ObjectIsNotGray(global));
529 :
530 : // That might have ended up wrapping us already, due to the wonders
531 : // of XBL. Check for that, and bail out as needed.
532 0 : aReflector.set(aCache->GetWrapper());
533 0 : if (aReflector) {
534 : #ifdef DEBUG
535 0 : binding_detail::AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto);
536 : #endif // DEBUG
537 0 : return true;
538 : }
539 :
540 0 : JSAutoCompartment ac(aCx, global);
541 0 : JS::Handle<JSObject*> canonicalProto = GetProtoObjectHandle(aCx);
542 0 : if (!canonicalProto) {
543 0 : return false;
544 : }
545 0 : JS::Rooted<JSObject*> proto(aCx);
546 0 : if (aGivenProto) {
547 0 : proto = aGivenProto;
548 : // Unfortunately, while aGivenProto was in the compartment of aCx
549 : // coming in, we changed compartments to that of "parent" so may need
550 : // to wrap the proto here.
551 0 : if (js::GetContextCompartment(aCx) != js::GetObjectCompartment(proto)) {
552 0 : if (!JS_WrapObject(aCx, &proto)) {
553 0 : return false;
554 : }
555 : }
556 : } else {
557 0 : proto = canonicalProto;
558 : }
559 :
560 0 : BindingJSObjectCreator<mozilla::dom::GamepadEvent> creator(aCx);
561 0 : creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector);
562 0 : if (!aReflector) {
563 0 : return false;
564 : }
565 :
566 0 : aCache->SetWrapper(aReflector);
567 :
568 : // Important: do unforgeable property setup after we have handed
569 : // over ownership of the C++ object to obj as needed, so that if
570 : // we fail and it ends up GCed it won't have problems in the
571 : // finalizer trying to drop its ownership of the C++ object.
572 : JS::Rooted<JSObject*> unforgeableHolder(aCx,
573 0 : &js::GetReservedSlot(canonicalProto, DOM_INTERFACE_PROTO_SLOTS_BASE).toObject());
574 0 : if (!JS_InitializePropertiesFromCompatibleNativeObject(aCx, aReflector, unforgeableHolder)) {
575 0 : aCache->ReleaseWrapper(aObject);
576 0 : aCache->ClearWrapper();
577 0 : return false;
578 : }
579 0 : creator.InitializationSucceeded();
580 :
581 0 : MOZ_ASSERT(aCache->GetWrapperPreserveColor() &&
582 : aCache->GetWrapperPreserveColor() == aReflector);
583 : // If proto != canonicalProto, we have to preserve our wrapper;
584 : // otherwise we won't be able to properly recreate it later, since
585 : // we won't know what proto to use. Note that we don't check
586 : // aGivenProto here, since it's entirely possible (and even
587 : // somewhat common) to have a non-null aGivenProto which is the
588 : // same as canonicalProto.
589 0 : if (proto != canonicalProto) {
590 0 : PreserveWrapper(aObject);
591 : }
592 :
593 0 : return true;
594 : }
595 :
596 : const NativePropertyHooks sNativePropertyHooks[] = { {
597 : nullptr,
598 : nullptr,
599 : nullptr,
600 : { sNativeProperties.Upcast(), nullptr },
601 : prototypes::id::GamepadEvent,
602 : constructors::id::GamepadEvent,
603 : EventBinding::sNativePropertyHooks,
604 : &DefaultXrayExpandoObjectClass
605 : } };
606 :
607 : void
608 0 : CreateInterfaceObjects(JSContext* aCx, JS::Handle<JSObject*> aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, bool aDefineOnGlobal)
609 : {
610 0 : JS::Handle<JSObject*> parentProto(EventBinding::GetProtoObjectHandle(aCx));
611 0 : if (!parentProto) {
612 0 : return;
613 : }
614 :
615 0 : JS::Handle<JSObject*> constructorProto(EventBinding::GetConstructorObjectHandle(aCx));
616 0 : if (!constructorProto) {
617 0 : return;
618 : }
619 :
620 : static bool sIdsInited = false;
621 0 : if (!sIdsInited && NS_IsMainThread()) {
622 0 : if (!InitIds(aCx, sNativeProperties.Upcast())) {
623 0 : return;
624 : }
625 0 : sIdsInited = true;
626 : }
627 :
628 0 : JS::Heap<JSObject*>* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::GamepadEvent);
629 0 : JS::Heap<JSObject*>* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::GamepadEvent);
630 0 : dom::CreateInterfaceObjects(aCx, aGlobal, parentProto,
631 : &sPrototypeClass.mBase, protoCache,
632 : constructorProto, &sInterfaceObjectClass.mBase, 1, nullptr,
633 : interfaceCache,
634 : sNativeProperties.Upcast(),
635 : nullptr,
636 : "GamepadEvent", aDefineOnGlobal,
637 : nullptr,
638 0 : false);
639 :
640 0 : JS::Rooted<JSObject*> unforgeableHolder(aCx);
641 : {
642 0 : JS::Rooted<JSObject*> holderProto(aCx, *protoCache);
643 0 : unforgeableHolder = JS_NewObjectWithoutMetadata(aCx, sClass.ToJSClass(), holderProto);
644 0 : if (!unforgeableHolder) {
645 0 : *protoCache = nullptr;
646 0 : if (interfaceCache) {
647 0 : *interfaceCache = nullptr;
648 : }
649 0 : return;
650 : }
651 : }
652 :
653 0 : if (!DefineUnforgeableAttributes(aCx, unforgeableHolder, sUnforgeableAttributes)) {
654 0 : *protoCache = nullptr;
655 0 : if (interfaceCache) {
656 0 : *interfaceCache = nullptr;
657 : }
658 0 : return;
659 : }
660 :
661 0 : if (*protoCache) {
662 0 : js::SetReservedSlot(*protoCache, DOM_INTERFACE_PROTO_SLOTS_BASE,
663 0 : JS::ObjectValue(*unforgeableHolder));
664 : }
665 : }
666 :
667 : JS::Handle<JSObject*>
668 0 : GetProtoObjectHandle(JSContext* aCx)
669 : {
670 : /* Get the interface prototype object for this class. This will create the
671 : object as needed. */
672 0 : bool aDefineOnGlobal = true;
673 :
674 : /* Make sure our global is sane. Hopefully we can remove this sometime */
675 0 : JSObject* global = JS::CurrentGlobalOrNull(aCx);
676 0 : if (!(js::GetObjectClass(global)->flags & JSCLASS_DOM_GLOBAL)) {
677 0 : return nullptr;
678 : }
679 :
680 : /* Check to see whether the interface objects are already installed */
681 0 : ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
682 0 : if (!protoAndIfaceCache.HasEntryInSlot(prototypes::id::GamepadEvent)) {
683 0 : JS::Rooted<JSObject*> rootedGlobal(aCx, global);
684 0 : CreateInterfaceObjects(aCx, rootedGlobal, protoAndIfaceCache, aDefineOnGlobal);
685 : }
686 :
687 : /*
688 : * The object might _still_ be null, but that's OK.
689 : *
690 : * Calling fromMarkedLocation() is safe because protoAndIfaceCache is
691 : * traced by TraceProtoAndIfaceCache() and its contents are never
692 : * changed after they have been set.
693 : *
694 : * Calling address() avoids the read read barrier that does gray
695 : * unmarking, but it's not possible for the object to be gray here.
696 : */
697 :
698 0 : const JS::Heap<JSObject*>& entrySlot = protoAndIfaceCache.EntrySlotMustExist(prototypes::id::GamepadEvent);
699 0 : MOZ_ASSERT(JS::ObjectIsNotGray(entrySlot));
700 0 : return JS::Handle<JSObject*>::fromMarkedLocation(entrySlot.address());
701 : }
702 :
703 : JSObject*
704 0 : GetProtoObject(JSContext* aCx)
705 : {
706 0 : return GetProtoObjectHandle(aCx);
707 : }
708 :
709 : JS::Handle<JSObject*>
710 0 : GetConstructorObjectHandle(JSContext* aCx, bool aDefineOnGlobal)
711 : {
712 : /* Get the interface object for this class. This will create the object as
713 : needed. */
714 :
715 : /* Make sure our global is sane. Hopefully we can remove this sometime */
716 0 : JSObject* global = JS::CurrentGlobalOrNull(aCx);
717 0 : if (!(js::GetObjectClass(global)->flags & JSCLASS_DOM_GLOBAL)) {
718 0 : return nullptr;
719 : }
720 :
721 : /* Check to see whether the interface objects are already installed */
722 0 : ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
723 0 : if (!protoAndIfaceCache.HasEntryInSlot(constructors::id::GamepadEvent)) {
724 0 : JS::Rooted<JSObject*> rootedGlobal(aCx, global);
725 0 : CreateInterfaceObjects(aCx, rootedGlobal, protoAndIfaceCache, aDefineOnGlobal);
726 : }
727 :
728 : /*
729 : * The object might _still_ be null, but that's OK.
730 : *
731 : * Calling fromMarkedLocation() is safe because protoAndIfaceCache is
732 : * traced by TraceProtoAndIfaceCache() and its contents are never
733 : * changed after they have been set.
734 : *
735 : * Calling address() avoids the read read barrier that does gray
736 : * unmarking, but it's not possible for the object to be gray here.
737 : */
738 :
739 0 : const JS::Heap<JSObject*>& entrySlot = protoAndIfaceCache.EntrySlotMustExist(constructors::id::GamepadEvent);
740 0 : MOZ_ASSERT(JS::ObjectIsNotGray(entrySlot));
741 0 : return JS::Handle<JSObject*>::fromMarkedLocation(entrySlot.address());
742 : }
743 :
744 : JSObject*
745 0 : GetConstructorObject(JSContext* aCx)
746 : {
747 0 : return GetConstructorObjectHandle(aCx);
748 : }
749 :
750 : } // namespace GamepadEventBinding
751 :
752 :
753 :
754 : } // namespace dom
755 : } // namespace mozilla
|