Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dom/bindings/CustomElementRegistryBinding.cpp
Line
Count
Source (jump to first uncovered line)
1
/* THIS FILE IS AUTOGENERATED FROM CustomElementRegistry.webidl BY Codegen.py - DO NOT EDIT */
2
3
#include "AtomList.h"
4
#include "CustomElementRegistry.h"
5
#include "CustomElementRegistryBinding.h"
6
#include "FunctionBinding.h"
7
#include "WrapperFactory.h"
8
#include "mozilla/OwningNonNull.h"
9
#include "mozilla/dom/BindingUtils.h"
10
#include "mozilla/dom/CustomElementRegistry.h"
11
#include "mozilla/dom/DOMJSClass.h"
12
#include "mozilla/dom/DocGroup.h"
13
#include "mozilla/dom/NonRefcountedDOMObject.h"
14
#include "mozilla/dom/Promise.h"
15
#include "mozilla/dom/ScriptSettings.h"
16
#include "mozilla/dom/SimpleGlobalObject.h"
17
#include "mozilla/dom/ToJSValue.h"
18
#include "mozilla/dom/XrayExpandoClass.h"
19
#include "nsContentUtils.h"
20
#include "nsINode.h"
21
22
namespace mozilla {
23
namespace dom {
24
25
namespace binding_detail {}; // Just to make sure it's known as a namespace
26
using namespace mozilla::dom::binding_detail;
27
28
29
30
ElementDefinitionOptions::ElementDefinitionOptions()
31
0
{
32
0
  // Safe to pass a null context if we pass a null value
33
0
  Init(nullptr, JS::NullHandleValue);
34
0
}
35
36
37
38
bool
39
ElementDefinitionOptions::InitIds(JSContext* cx, ElementDefinitionOptionsAtoms* atomsCache)
40
0
{
41
0
  MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
42
0
43
0
  // Initialize these in reverse order so that any failure leaves the first one
44
0
  // uninitialized.
45
0
  if (!atomsCache->extends_id.init(cx, "extends")) {
46
0
    return false;
47
0
  }
48
0
  return true;
49
0
}
50
51
bool
52
ElementDefinitionOptions::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
53
0
{
54
0
  // Passing a null JSContext is OK only if we're initing from null,
55
0
  // Since in that case we will not have to do any property gets
56
0
  // Also evaluate isNullOrUndefined in order to avoid false-positive
57
0
  // checkers by static analysis tools
58
0
  MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
59
0
  ElementDefinitionOptionsAtoms* atomsCache = nullptr;
60
0
  if (cx) {
61
0
    atomsCache = GetAtomCache<ElementDefinitionOptionsAtoms>(cx);
62
0
    if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
63
0
      return false;
64
0
    }
65
0
  }
66
0
67
0
  if (!IsConvertibleToDictionary(val)) {
68
0
    return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
69
0
  }
70
0
71
0
  bool isNull = val.isNullOrUndefined();
72
0
  // We only need these if !isNull, in which case we have |cx|.
73
0
  Maybe<JS::Rooted<JSObject *> > object;
74
0
  Maybe<JS::Rooted<JS::Value> > temp;
75
0
  if (!isNull) {
76
0
    MOZ_ASSERT(cx);
77
0
    object.emplace(cx, &val.toObject());
78
0
    temp.emplace(cx);
79
0
  }
80
0
  if (!isNull) {
81
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->extends_id, temp.ptr())) {
82
0
      return false;
83
0
    }
84
0
  }
85
0
  if (!isNull && !temp->isUndefined()) {
86
0
    mExtends.Construct();
87
0
    if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, (mExtends.Value()))) {
88
0
      return false;
89
0
    }
90
0
    mIsAnyMemberPresent = true;
91
0
  }
92
0
  return true;
93
0
}
94
95
bool
96
ElementDefinitionOptions::Init(const nsAString& aJSON)
97
0
{
98
0
  AutoJSAPI jsapi;
99
0
  JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
100
0
  if (!cleanGlobal) {
101
0
    return false;
102
0
  }
103
0
  if (!jsapi.Init(cleanGlobal)) {
104
0
    return false;
105
0
  }
106
0
  JSContext* cx = jsapi.cx();
107
0
  JS::Rooted<JS::Value> json(cx);
108
0
  bool ok = ParseJSON(cx, aJSON, &json);
109
0
  NS_ENSURE_TRUE(ok, false);
110
0
  return Init(cx, json);
111
0
}
112
113
bool
114
ElementDefinitionOptions::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
115
0
{
116
0
  ElementDefinitionOptionsAtoms* atomsCache = GetAtomCache<ElementDefinitionOptionsAtoms>(cx);
117
0
  if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
118
0
    return false;
119
0
  }
120
0
121
0
  JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
122
0
  if (!obj) {
123
0
    return false;
124
0
  }
125
0
  rval.set(JS::ObjectValue(*obj));
126
0
127
0
  if (mExtends.WasPassed()) {
128
0
    do {
129
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
130
0
      JS::Rooted<JS::Value> temp(cx);
131
0
      nsString const & currentValue = mExtends.InternalValue();
132
0
      if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
133
0
        return false;
134
0
      }
135
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->extends_id, temp, JSPROP_ENUMERATE)) {
136
0
        return false;
137
0
      }
138
0
      break;
139
0
    } while(false);
140
0
  }
141
0
142
0
  return true;
143
0
}
144
145
bool
146
ElementDefinitionOptions::ToJSON(nsAString& aJSON) const
147
0
{
148
0
  AutoJSAPI jsapi;
149
0
  jsapi.Init();
150
0
  JSContext *cx = jsapi.cx();
151
0
  // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
152
0
  // because we'll only be creating objects, in ways that have no
153
0
  // side-effects, followed by a call to JS::ToJSONMaybeSafely,
154
0
  // which likewise guarantees no side-effects for the sorts of
155
0
  // things we will pass it.
156
0
  JSAutoRealm ar(cx, UnprivilegedJunkScopeOrWorkerGlobal());
157
0
  JS::Rooted<JS::Value> val(cx);
158
0
  if (!ToObjectInternal(cx, &val)) {
159
0
    return false;
160
0
  }
161
0
  JS::Rooted<JSObject*> obj(cx, &val.toObject());
162
0
  return StringifyToJSON(cx, obj, aJSON);
163
0
}
164
165
void
166
ElementDefinitionOptions::TraceDictionary(JSTracer* trc)
167
0
{
168
0
}
169
170
ElementDefinitionOptions&
171
ElementDefinitionOptions::operator=(const ElementDefinitionOptions& aOther)
172
0
{
173
0
  DictionaryBase::operator=(aOther);
174
0
  mExtends.Reset();
175
0
  if (aOther.mExtends.WasPassed()) {
176
0
    mExtends.Construct(aOther.mExtends.Value());
177
0
  }
178
0
  return *this;
179
0
}
180
181
namespace binding_detail {
182
} // namespace binding_detail
183
184
185
void
186
CustomElementCreationCallback::Call(JSContext* cx, JS::Handle<JS::Value> aThisVal, const nsAString& name, ErrorResult& aRv)
187
0
{
188
0
  JS::Rooted<JS::Value> rval(cx, JS::UndefinedValue());
189
0
  JS::AutoValueVector argv(cx);
190
0
  if (!argv.resize(1)) {
191
0
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
192
0
    return;
193
0
  }
194
0
  unsigned argc = 1;
195
0
196
0
  do {
197
0
    nsString mutableStr(name);
198
0
    if (!xpc::NonVoidStringToJsval(cx, mutableStr, argv[0])) {
199
0
      aRv.Throw(NS_ERROR_UNEXPECTED);
200
0
      return;
201
0
    }
202
0
    break;
203
0
  } while (false);
204
0
205
0
  JS::Rooted<JS::Value> callable(cx, JS::ObjectValue(*mCallback));
206
0
  if (!JS::Call(cx, aThisVal, callable,
207
0
                JS::HandleValueArray::subarray(argv, 0, argc), &rval)) {
208
0
    aRv.NoteJSContextException(cx);
209
0
    return;
210
0
  }
211
0
}
212
213
214
215
namespace binding_detail {
216
} // namespace binding_detail
217
218
219
namespace CustomElementRegistry_Binding {
220
221
MOZ_CAN_RUN_SCRIPT static bool
222
define(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::CustomElementRegistry* self, const JSJitMethodCallArgs& args)
223
0
{
224
0
  AUTO_PROFILER_LABEL_FAST("CustomElementRegistry.define", DOM, cx);
225
0
226
0
  if (MOZ_UNLIKELY(args.length() < 2)) {
227
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "CustomElementRegistry.define");
228
0
  }
229
0
  binding_detail::FakeString arg0;
230
0
  if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
231
0
    return false;
232
0
  }
233
0
  RootedCallback<OwningNonNull<binding_detail::FastFunction>> arg1(cx);
234
0
  if (args[1].isObject()) {
235
0
    if (JS::IsCallable(&args[1].toObject())) {
236
0
    { // scope for tempRoot and tempGlobalRoot if needed
237
0
      arg1 = new binding_detail::FastFunction(&args[1].toObject(), JS::CurrentGlobalOrNull(cx));
238
0
    }
239
0
    } else {
240
0
      ThrowErrorMessage(cx, MSG_NOT_CALLABLE, "Argument 2 of CustomElementRegistry.define");
241
0
      return false;
242
0
    }
243
0
  } else {
244
0
    ThrowErrorMessage(cx, MSG_NOT_OBJECT, "Argument 2 of CustomElementRegistry.define");
245
0
    return false;
246
0
  }
247
0
  binding_detail::FastElementDefinitionOptions arg2;
248
0
  if (!arg2.Init(cx, (args.hasDefined(2)) ? args[2] : JS::NullHandleValue,  "Argument 3 of CustomElementRegistry.define", false)) {
249
0
    return false;
250
0
  }
251
0
  Maybe<AutoCEReaction> ceReaction;
252
0
  if (CustomElementRegistry::IsCustomElementEnabled(cx, obj)) {
253
0
    DocGroup* docGroup = self->GetDocGroup();
254
0
    if (docGroup) {
255
0
      ceReaction.emplace(docGroup->CustomElementReactionsStack(), cx);
256
0
    }
257
0
  }
258
0
  FastErrorResult rv;
259
0
  self->Define(cx, NonNullHelper(Constify(arg0)), NonNullHelper(arg1), Constify(arg2), rv);
260
0
  if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
261
0
    return false;
262
0
  }
263
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
264
0
  args.rval().setUndefined();
265
0
  return true;
266
0
}
267
268
static const JSJitInfo define_methodinfo = {
269
  { (JSJitGetterOp)define },
270
  { prototypes::id::CustomElementRegistry },
271
  { PrototypeTraits<prototypes::id::CustomElementRegistry>::Depth },
272
  JSJitInfo::Method,
273
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
274
  JSVAL_TYPE_UNDEFINED,  /* returnType.  Not relevant for setters. */
275
  false,  /* isInfallible. False in setters. */
276
  false,  /* isMovable.  Not relevant for setters. */
277
  false, /* isEliminatable.  Not relevant for setters. */
278
  false, /* isAlwaysInSlot.  Only relevant for getters. */
279
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
280
  false,  /* isTypedMethod.  Only relevant for methods. */
281
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
282
};
283
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
284
static_assert(0 < 1, "There is no slot for us");
285
286
MOZ_CAN_RUN_SCRIPT static bool
287
setElementCreationCallback(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::CustomElementRegistry* self, const JSJitMethodCallArgs& args)
288
0
{
289
0
  AUTO_PROFILER_LABEL_FAST("CustomElementRegistry.setElementCreationCallback", DOM, cx);
290
0
291
0
  if (MOZ_UNLIKELY(args.length() < 2)) {
292
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "CustomElementRegistry.setElementCreationCallback");
293
0
  }
294
0
  binding_detail::FakeString arg0;
295
0
  if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
296
0
    return false;
297
0
  }
298
0
  RootedCallback<OwningNonNull<binding_detail::FastCustomElementCreationCallback>> arg1(cx);
299
0
  if (args[1].isObject()) {
300
0
    if (JS::IsCallable(&args[1].toObject())) {
301
0
    { // scope for tempRoot and tempGlobalRoot if needed
302
0
      arg1 = new binding_detail::FastCustomElementCreationCallback(&args[1].toObject(), JS::CurrentGlobalOrNull(cx));
303
0
    }
304
0
    } else {
305
0
      ThrowErrorMessage(cx, MSG_NOT_CALLABLE, "Argument 2 of CustomElementRegistry.setElementCreationCallback");
306
0
      return false;
307
0
    }
308
0
  } else {
309
0
    ThrowErrorMessage(cx, MSG_NOT_OBJECT, "Argument 2 of CustomElementRegistry.setElementCreationCallback");
310
0
    return false;
311
0
  }
312
0
  FastErrorResult rv;
313
0
  self->SetElementCreationCallback(NonNullHelper(Constify(arg0)), NonNullHelper(arg1), rv);
314
0
  if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
315
0
    return false;
316
0
  }
317
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
318
0
  args.rval().setUndefined();
319
0
  return true;
320
0
}
321
322
static const JSJitInfo setElementCreationCallback_methodinfo = {
323
  { (JSJitGetterOp)setElementCreationCallback },
324
  { prototypes::id::CustomElementRegistry },
325
  { PrototypeTraits<prototypes::id::CustomElementRegistry>::Depth },
326
  JSJitInfo::Method,
327
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
328
  JSVAL_TYPE_UNDEFINED,  /* returnType.  Not relevant for setters. */
329
  false,  /* isInfallible. False in setters. */
330
  false,  /* isMovable.  Not relevant for setters. */
331
  false, /* isEliminatable.  Not relevant for setters. */
332
  false, /* isAlwaysInSlot.  Only relevant for getters. */
333
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
334
  false,  /* isTypedMethod.  Only relevant for methods. */
335
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
336
};
337
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
338
static_assert(0 < 1, "There is no slot for us");
339
340
MOZ_CAN_RUN_SCRIPT static bool
341
get(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::CustomElementRegistry* self, const JSJitMethodCallArgs& args)
342
0
{
343
0
  AUTO_PROFILER_LABEL_FAST("CustomElementRegistry.get", DOM, cx);
344
0
345
0
  if (MOZ_UNLIKELY(args.length() < 1)) {
346
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "CustomElementRegistry.get");
347
0
  }
348
0
  binding_detail::FakeString arg0;
349
0
  if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
350
0
    return false;
351
0
  }
352
0
  JS::Rooted<JS::Value> result(cx);
353
0
  self->Get(cx, NonNullHelper(Constify(arg0)), &result);
354
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
355
0
  JS::ExposeValueToActiveJS(result);
356
0
  args.rval().set(result);
357
0
  if (!MaybeWrapValue(cx, args.rval())) {
358
0
    return false;
359
0
  }
360
0
  return true;
361
0
}
362
363
static const JSJitInfo get_methodinfo = {
364
  { (JSJitGetterOp)get },
365
  { prototypes::id::CustomElementRegistry },
366
  { PrototypeTraits<prototypes::id::CustomElementRegistry>::Depth },
367
  JSJitInfo::Method,
368
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
369
  JSVAL_TYPE_UNKNOWN,  /* returnType.  Not relevant for setters. */
370
  false,  /* isInfallible. False in setters. */
371
  false,  /* isMovable.  Not relevant for setters. */
372
  false, /* isEliminatable.  Not relevant for setters. */
373
  false, /* isAlwaysInSlot.  Only relevant for getters. */
374
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
375
  false,  /* isTypedMethod.  Only relevant for methods. */
376
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
377
};
378
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
379
static_assert(0 < 1, "There is no slot for us");
380
381
MOZ_CAN_RUN_SCRIPT static bool
382
whenDefined(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::CustomElementRegistry* self, const JSJitMethodCallArgs& args)
383
0
{
384
0
  AUTO_PROFILER_LABEL_FAST("CustomElementRegistry.whenDefined", DOM, cx);
385
0
386
0
  if (MOZ_UNLIKELY(args.length() < 1)) {
387
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "CustomElementRegistry.whenDefined");
388
0
  }
389
0
  binding_detail::FakeString arg0;
390
0
  if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
391
0
    return false;
392
0
  }
393
0
  FastErrorResult rv;
394
0
  auto result(StrongOrRawPtr<Promise>(self->WhenDefined(NonNullHelper(Constify(arg0)), rv)));
395
0
  if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
396
0
    return false;
397
0
  }
398
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
399
0
  if (!ToJSValue(cx, result, args.rval())) {
400
0
    return false;
401
0
  }
402
0
  return true;
403
0
}
404
405
MOZ_CAN_RUN_SCRIPT static bool
406
whenDefined_promiseWrapper(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::CustomElementRegistry* self, const JSJitMethodCallArgs& args)
407
0
{
408
0
  bool ok = whenDefined(cx, obj, self, args);
409
0
  if (ok) {
410
0
    return true;
411
0
  }
412
0
  return ConvertExceptionToPromise(cx, args.rval());
413
0
}
414
415
static const JSJitInfo whenDefined_methodinfo = {
416
  { (JSJitGetterOp)whenDefined_promiseWrapper },
417
  { prototypes::id::CustomElementRegistry },
418
  { PrototypeTraits<prototypes::id::CustomElementRegistry>::Depth },
419
  JSJitInfo::Method,
420
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
421
  JSVAL_TYPE_OBJECT,  /* returnType.  Not relevant for setters. */
422
  false,  /* isInfallible. False in setters. */
423
  false,  /* isMovable.  Not relevant for setters. */
424
  false, /* isEliminatable.  Not relevant for setters. */
425
  false, /* isAlwaysInSlot.  Only relevant for getters. */
426
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
427
  false,  /* isTypedMethod.  Only relevant for methods. */
428
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
429
};
430
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
431
static_assert(0 < 1, "There is no slot for us");
432
433
MOZ_CAN_RUN_SCRIPT static bool
434
upgrade(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::CustomElementRegistry* self, const JSJitMethodCallArgs& args)
435
0
{
436
0
  AUTO_PROFILER_LABEL_FAST("CustomElementRegistry.upgrade", DOM, cx);
437
0
438
0
  if (MOZ_UNLIKELY(args.length() < 1)) {
439
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "CustomElementRegistry.upgrade");
440
0
  }
441
0
  NonNull<nsINode> arg0;
442
0
  if (args[0].isObject()) {
443
0
    {
444
0
      nsresult rv = UnwrapObject<prototypes::id::Node, nsINode>(args[0], arg0);
445
0
      if (NS_FAILED(rv)) {
446
0
        ThrowErrorMessage(cx, MSG_DOES_NOT_IMPLEMENT_INTERFACE, "Argument 1 of CustomElementRegistry.upgrade", "Node");
447
0
        return false;
448
0
      }
449
0
    }
450
0
  } else {
451
0
    ThrowErrorMessage(cx, MSG_NOT_OBJECT, "Argument 1 of CustomElementRegistry.upgrade");
452
0
    return false;
453
0
  }
454
0
  Maybe<AutoCEReaction> ceReaction;
455
0
  if (CustomElementRegistry::IsCustomElementEnabled(cx, obj)) {
456
0
    DocGroup* docGroup = self->GetDocGroup();
457
0
    if (docGroup) {
458
0
      ceReaction.emplace(docGroup->CustomElementReactionsStack(), cx);
459
0
    }
460
0
  }
461
0
  self->Upgrade(MOZ_KnownLive(NonNullHelper(arg0)));
462
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
463
0
  args.rval().setUndefined();
464
0
  return true;
465
0
}
466
467
static const JSJitInfo upgrade_methodinfo = {
468
  { (JSJitGetterOp)upgrade },
469
  { prototypes::id::CustomElementRegistry },
470
  { PrototypeTraits<prototypes::id::CustomElementRegistry>::Depth },
471
  JSJitInfo::Method,
472
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
473
  JSVAL_TYPE_UNDEFINED,  /* returnType.  Not relevant for setters. */
474
  false,  /* isInfallible. False in setters. */
475
  false,  /* isMovable.  Not relevant for setters. */
476
  false, /* isEliminatable.  Not relevant for setters. */
477
  false, /* isAlwaysInSlot.  Only relevant for getters. */
478
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
479
  false,  /* isTypedMethod.  Only relevant for methods. */
480
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
481
};
482
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
483
static_assert(0 < 1, "There is no slot for us");
484
485
static bool
486
_addProperty(JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::Handle<JS::Value> val)
487
0
{
488
0
  mozilla::dom::CustomElementRegistry* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::CustomElementRegistry>(obj);
489
0
  // We don't want to preserve if we don't have a wrapper, and we
490
0
  // obviously can't preserve if we're not initialized.
491
0
  if (self && self->GetWrapperPreserveColor()) {
492
0
    PreserveWrapper(self);
493
0
  }
494
0
  return true;
495
0
}
496
497
static void
498
_finalize(js::FreeOp* fop, JSObject* obj)
499
0
{
500
0
  mozilla::dom::CustomElementRegistry* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::CustomElementRegistry>(obj);
501
0
  if (self) {
502
0
    ClearWrapper(self, self, obj);
503
0
    AddForDeferredFinalization<mozilla::dom::CustomElementRegistry>(self);
504
0
  }
505
0
}
506
507
static size_t
508
_objectMoved(JSObject* obj, JSObject* old)
509
0
{
510
0
  mozilla::dom::CustomElementRegistry* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::CustomElementRegistry>(obj);
511
0
  if (self) {
512
0
    UpdateWrapper(self, self, obj, old);
513
0
  }
514
0
515
0
  return 0;
516
0
}
517
518
// We deliberately use brace-elision to make Visual Studio produce better initalization code.
519
#if defined(__clang__)
520
#pragma clang diagnostic push
521
#pragma clang diagnostic ignored "-Wmissing-braces"
522
#endif
523
static const JSFunctionSpec sMethods_specs[] = {
524
  JS_FNSPEC("define", (GenericMethod<NormalThisPolicy, ThrowExceptions>), reinterpret_cast<const JSJitInfo*>(&define_methodinfo), 2, JSPROP_ENUMERATE, nullptr),
525
  JS_FNSPEC("get", (GenericMethod<NormalThisPolicy, ThrowExceptions>), reinterpret_cast<const JSJitInfo*>(&get_methodinfo), 1, JSPROP_ENUMERATE, nullptr),
526
  JS_FNSPEC("whenDefined", (GenericMethod<NormalThisPolicy, ConvertExceptionsToPromises>), reinterpret_cast<const JSJitInfo*>(&whenDefined_methodinfo), 1, JSPROP_ENUMERATE, nullptr),
527
  JS_FNSPEC("upgrade", (GenericMethod<NormalThisPolicy, ThrowExceptions>), reinterpret_cast<const JSJitInfo*>(&upgrade_methodinfo), 1, JSPROP_ENUMERATE, nullptr),
528
  JS_FS_END
529
};
530
#if defined(__clang__)
531
#pragma clang diagnostic pop
532
#endif
533
534
535
static const Prefable<const JSFunctionSpec> sMethods[] = {
536
  { nullptr, &sMethods_specs[0] },
537
  { nullptr, nullptr }
538
};
539
540
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
541
    "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
542
static_assert(4 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
543
    "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
544
545
// We deliberately use brace-elision to make Visual Studio produce better initalization code.
546
#if defined(__clang__)
547
#pragma clang diagnostic push
548
#pragma clang diagnostic ignored "-Wmissing-braces"
549
#endif
550
static const JSFunctionSpec sChromeMethods_specs[] = {
551
  JS_FNSPEC("setElementCreationCallback", (GenericMethod<NormalThisPolicy, ThrowExceptions>), reinterpret_cast<const JSJitInfo*>(&setElementCreationCallback_methodinfo), 2, JSPROP_ENUMERATE, nullptr),
552
  JS_FS_END
553
};
554
#if defined(__clang__)
555
#pragma clang diagnostic pop
556
#endif
557
558
559
static const Prefable<const JSFunctionSpec> sChromeMethods[] = {
560
  { nullptr, &sChromeMethods_specs[0] },
561
  { nullptr, nullptr }
562
};
563
564
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
565
    "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
566
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
567
    "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
568
569
570
static uint16_t sNativeProperties_sortedPropertyIndices[4];
571
static PropertyInfo sNativeProperties_propertyInfos[4];
572
573
static const NativePropertiesN<1> sNativeProperties = {
574
  false, 0,
575
  false, 0,
576
  true,  0 /* sMethods */,
577
  false, 0,
578
  false, 0,
579
  false, 0,
580
  false, 0,
581
  -1,
582
  4,
583
  sNativeProperties_sortedPropertyIndices,
584
  {
585
    { sMethods, &sNativeProperties_propertyInfos[0] }
586
  }
587
};
588
static_assert(4 < 1ull << CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount),
589
    "We have a property info count that is oversized");
590
591
static uint16_t sChromeOnlyNativeProperties_sortedPropertyIndices[1];
592
static PropertyInfo sChromeOnlyNativeProperties_propertyInfos[1];
593
594
static const NativePropertiesN<1> sChromeOnlyNativeProperties = {
595
  false, 0,
596
  false, 0,
597
  true,  0 /* sChromeMethods */,
598
  false, 0,
599
  false, 0,
600
  false, 0,
601
  false, 0,
602
  -1,
603
  1,
604
  sChromeOnlyNativeProperties_sortedPropertyIndices,
605
  {
606
    { sChromeMethods, &sChromeOnlyNativeProperties_propertyInfos[0] }
607
  }
608
};
609
static_assert(1 < 1ull << CHAR_BIT * sizeof(sChromeOnlyNativeProperties.propertyInfoCount),
610
    "We have a property info count that is oversized");
611
612
static const DOMIfaceAndProtoJSClass sInterfaceObjectClass = {
613
  {
614
    "Function",
615
    JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_SLOTS_BASE),
616
    &sBoringInterfaceObjectClassClassOps,
617
    JS_NULL_CLASS_SPEC,
618
    JS_NULL_CLASS_EXT,
619
    &sInterfaceObjectClassObjectOps
620
  },
621
  eInterface,
622
  true,
623
  prototypes::id::CustomElementRegistry,
624
  PrototypeTraits<prototypes::id::CustomElementRegistry>::Depth,
625
  sNativePropertyHooks,
626
  "function CustomElementRegistry() {\n    [native code]\n}",
627
  JS::GetRealmFunctionPrototype
628
};
629
630
static const DOMIfaceAndProtoJSClass sPrototypeClass = {
631
  {
632
    "CustomElementRegistryPrototype",
633
    JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE),
634
    JS_NULL_CLASS_OPS,
635
    JS_NULL_CLASS_SPEC,
636
    JS_NULL_CLASS_EXT,
637
    JS_NULL_OBJECT_OPS
638
  },
639
  eInterfacePrototype,
640
  false,
641
  prototypes::id::CustomElementRegistry,
642
  PrototypeTraits<prototypes::id::CustomElementRegistry>::Depth,
643
  sNativePropertyHooks,
644
  "[object CustomElementRegistryPrototype]",
645
  JS::GetRealmObjectPrototype
646
};
647
648
bool
649
ConstructorEnabled(JSContext* aCx, JS::Handle<JSObject*> aObj)
650
0
{
651
0
  return CustomElementRegistry::IsCustomElementEnabled(aCx, aObj);
652
0
}
653
654
static const js::ClassOps sClassOps = {
655
  _addProperty, /* addProperty */
656
  nullptr,               /* delProperty */
657
  nullptr,               /* enumerate */
658
  nullptr, /* newEnumerate */
659
  nullptr, /* resolve */
660
  nullptr, /* mayResolve */
661
  _finalize, /* finalize */
662
  nullptr, /* call */
663
  nullptr,               /* hasInstance */
664
  nullptr,               /* construct */
665
  nullptr, /* trace */
666
};
667
668
static const js::ClassExtension sClassExtension = {
669
  nullptr, /* weakmapKeyDelegateOp */
670
  _objectMoved /* objectMovedOp */
671
};
672
673
static const DOMJSClass sClass = {
674
  { "CustomElementRegistry",
675
    JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(1),
676
    &sClassOps,
677
    JS_NULL_CLASS_SPEC,
678
    &sClassExtension,
679
    JS_NULL_OBJECT_OPS
680
  },
681
  { prototypes::id::CustomElementRegistry, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count },
682
  IsBaseOf<nsISupports, mozilla::dom::CustomElementRegistry >::value,
683
  sNativePropertyHooks,
684
  FindAssociatedGlobalForNative<mozilla::dom::CustomElementRegistry>::Get,
685
  GetProtoObjectHandle,
686
  GetCCParticipant<mozilla::dom::CustomElementRegistry>::Get()
687
};
688
static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS,
689
              "Must have the right minimal number of reserved slots.");
690
static_assert(1 >= 1,
691
              "Must have enough reserved slots.");
692
693
const JSClass*
694
GetJSClass()
695
0
{
696
0
  return sClass.ToJSClass();
697
0
}
698
699
bool
700
Wrap(JSContext* aCx, mozilla::dom::CustomElementRegistry* aObject, nsWrapperCache* aCache, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
701
0
{
702
0
  static_assert(!IsBaseOf<NonRefcountedDOMObject, mozilla::dom::CustomElementRegistry>::value,
703
0
                "Shouldn't have wrappercached things that are not refcounted.");
704
0
  MOZ_ASSERT(static_cast<mozilla::dom::CustomElementRegistry*>(aObject) ==
705
0
             reinterpret_cast<mozilla::dom::CustomElementRegistry*>(aObject),
706
0
             "Multiple inheritance for mozilla::dom::CustomElementRegistry is broken.");
707
0
  MOZ_ASSERT(ToSupportsIsCorrect(aObject));
708
0
  MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx));
709
0
  MOZ_ASSERT(!aCache->GetWrapper(),
710
0
             "You should probably not be using Wrap() directly; use "
711
0
             "GetOrCreateDOMReflector instead");
712
0
713
0
  MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache),
714
0
             "nsISupports must be on our primary inheritance chain");
715
0
716
0
  JS::Rooted<JSObject*> global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject()));
717
0
  if (!global) {
718
0
    return false;
719
0
  }
720
0
  MOZ_ASSERT(JS_IsGlobalObject(global));
721
0
  MOZ_ASSERT(JS::ObjectIsNotGray(global));
722
0
723
0
  // That might have ended up wrapping us already, due to the wonders
724
0
  // of XBL.  Check for that, and bail out as needed.
725
0
  aReflector.set(aCache->GetWrapper());
726
0
  if (aReflector) {
727
#ifdef DEBUG
728
    AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto);
729
#endif // DEBUG
730
    return true;
731
0
  }
732
0
733
0
  JSAutoRealm ar(aCx, global);
734
0
  JS::Handle<JSObject*> canonicalProto = GetProtoObjectHandle(aCx);
735
0
  if (!canonicalProto) {
736
0
    return false;
737
0
  }
738
0
  JS::Rooted<JSObject*> proto(aCx);
739
0
  if (aGivenProto) {
740
0
    proto = aGivenProto;
741
0
    // Unfortunately, while aGivenProto was in the compartment of aCx
742
0
    // coming in, we changed compartments to that of "parent" so may need
743
0
    // to wrap the proto here.
744
0
    if (js::GetContextCompartment(aCx) != js::GetObjectCompartment(proto)) {
745
0
      if (!JS_WrapObject(aCx, &proto)) {
746
0
        return false;
747
0
      }
748
0
    }
749
0
  } else {
750
0
    proto = canonicalProto;
751
0
  }
752
0
753
0
  BindingJSObjectCreator<mozilla::dom::CustomElementRegistry> creator(aCx);
754
0
  creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector);
755
0
  if (!aReflector) {
756
0
    return false;
757
0
  }
758
0
759
0
  aCache->SetWrapper(aReflector);
760
0
  creator.InitializationSucceeded();
761
0
762
0
  MOZ_ASSERT(aCache->GetWrapperPreserveColor() &&
763
0
             aCache->GetWrapperPreserveColor() == aReflector);
764
0
  // If proto != canonicalProto, we have to preserve our wrapper;
765
0
  // otherwise we won't be able to properly recreate it later, since
766
0
  // we won't know what proto to use.  Note that we don't check
767
0
  // aGivenProto here, since it's entirely possible (and even
768
0
  // somewhat common) to have a non-null aGivenProto which is the
769
0
  // same as canonicalProto.
770
0
  if (proto != canonicalProto) {
771
0
    PreserveWrapper(aObject);
772
0
  }
773
0
774
0
  return true;
775
0
}
776
777
const NativePropertyHooks sNativePropertyHooks[] = { {
778
  nullptr,
779
  nullptr,
780
  nullptr,
781
  { sNativeProperties.Upcast(), sChromeOnlyNativeProperties.Upcast() },
782
  prototypes::id::CustomElementRegistry,
783
  constructors::id::CustomElementRegistry,
784
  nullptr,
785
  &DefaultXrayExpandoObjectClass
786
} };
787
788
void
789
CreateInterfaceObjects(JSContext* aCx, JS::Handle<JSObject*> aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, bool aDefineOnGlobal)
790
0
{
791
0
  JS::Rooted<JSObject*> parentProto(aCx, JS::GetRealmObjectPrototype(aCx));
792
0
  if (!parentProto) {
793
0
    return;
794
0
  }
795
0
796
0
  JS::Rooted<JSObject*> constructorProto(aCx, JS::GetRealmFunctionPrototype(aCx));
797
0
  if (!constructorProto) {
798
0
    return;
799
0
  }
800
0
801
0
  static bool sIdsInited = false;
802
0
  if (!sIdsInited && NS_IsMainThread()) {
803
0
    if (!InitIds(aCx, sNativeProperties.Upcast())) {
804
0
      return;
805
0
    }
806
0
    if (!InitIds(aCx, sChromeOnlyNativeProperties.Upcast())) {
807
0
      return;
808
0
    }
809
0
    sIdsInited = true;
810
0
  }
811
0
812
0
  JS::Heap<JSObject*>* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::CustomElementRegistry);
813
0
  JS::Heap<JSObject*>* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::CustomElementRegistry);
814
0
  dom::CreateInterfaceObjects(aCx, aGlobal, parentProto,
815
0
                              &sPrototypeClass.mBase, protoCache,
816
0
                              nullptr,
817
0
                              constructorProto, &sInterfaceObjectClass.mBase, 0, nullptr,
818
0
                              interfaceCache,
819
0
                              sNativeProperties.Upcast(),
820
0
                              sChromeOnlyNativeProperties.Upcast(),
821
0
                              "CustomElementRegistry", aDefineOnGlobal,
822
0
                              nullptr,
823
0
                              false);
824
0
}
825
826
JSObject*
827
GetConstructorObject(JSContext* aCx)
828
0
{
829
0
  return GetConstructorObjectHandle(aCx);
830
0
}
831
832
} // namespace CustomElementRegistry_Binding
833
834
835
836
} // namespace dom
837
} // namespace mozilla