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