Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dom/bindings/HTMLCollectionBinding.cpp
Line
Count
Source (jump to first uncovered line)
1
/* THIS FILE IS AUTOGENERATED FROM HTMLCollection.webidl BY Codegen.py - DO NOT EDIT */
2
3
#include "HTMLCollectionBinding.h"
4
#include "WrapperFactory.h"
5
#include "mozilla/OwningNonNull.h"
6
#include "mozilla/dom/BindingUtils.h"
7
#include "mozilla/dom/DOMJSClass.h"
8
#include "mozilla/dom/DOMJSProxyHandler.h"
9
#include "mozilla/dom/Element.h"
10
#include "mozilla/dom/NonRefcountedDOMObject.h"
11
#include "mozilla/dom/Nullable.h"
12
#include "mozilla/dom/PrimitiveConversions.h"
13
#include "mozilla/dom/XrayExpandoClass.h"
14
#include "nsContentList.h"
15
16
namespace mozilla {
17
namespace dom {
18
19
namespace binding_detail {}; // Just to make sure it's known as a namespace
20
using namespace mozilla::dom::binding_detail;
21
22
23
namespace HTMLCollection_Binding {
24
25
MOZ_CAN_RUN_SCRIPT static bool
26
get_length(JSContext* cx, JS::Handle<JSObject*> obj, nsIHTMLCollection* self, JSJitGetterCallArgs args)
27
0
{
28
0
  AUTO_PROFILER_LABEL_FAST("get HTMLCollection.length", DOM, cx);
29
0
30
0
  uint32_t result(self->Length());
31
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
32
0
  args.rval().setNumber(result);
33
0
  return true;
34
0
}
35
36
static const JSJitInfo length_getterinfo = {
37
  { (JSJitGetterOp)get_length },
38
  { prototypes::id::HTMLCollection },
39
  { PrototypeTraits<prototypes::id::HTMLCollection>::Depth },
40
  JSJitInfo::Getter,
41
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
42
  JSVAL_TYPE_DOUBLE,  /* returnType.  Not relevant for setters. */
43
  true,  /* isInfallible. False in setters. */
44
  false,  /* isMovable.  Not relevant for setters. */
45
  false, /* isEliminatable.  Not relevant for setters. */
46
  false, /* isAlwaysInSlot.  Only relevant for getters. */
47
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
48
  false,  /* isTypedMethod.  Only relevant for methods. */
49
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
50
};
51
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
52
static_assert(0 < 1, "There is no slot for us");
53
54
MOZ_CAN_RUN_SCRIPT static bool
55
item(JSContext* cx, JS::Handle<JSObject*> obj, nsIHTMLCollection* self, const JSJitMethodCallArgs& args)
56
0
{
57
0
  AUTO_PROFILER_LABEL_FAST("HTMLCollection.item", DOM, cx);
58
0
59
0
  if (MOZ_UNLIKELY(args.length() < 1)) {
60
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "HTMLCollection.item");
61
0
  }
62
0
  uint32_t arg0;
63
0
  if (!ValueToPrimitive<uint32_t, eDefault>(cx, args[0], &arg0)) {
64
0
    return false;
65
0
  }
66
0
  auto result(StrongOrRawPtr<mozilla::dom::Element>(self->Item(arg0)));
67
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
68
0
  if (!result) {
69
0
    args.rval().setNull();
70
0
    return true;
71
0
  }
72
0
  if (!GetOrCreateDOMReflector(cx, result, args.rval())) {
73
0
    MOZ_ASSERT(JS_IsExceptionPending(cx));
74
0
    return false;
75
0
  }
76
0
  return true;
77
0
}
78
79
static const JSJitInfo item_methodinfo = {
80
  { (JSJitGetterOp)item },
81
  { prototypes::id::HTMLCollection },
82
  { PrototypeTraits<prototypes::id::HTMLCollection>::Depth },
83
  JSJitInfo::Method,
84
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
85
  JSVAL_TYPE_UNKNOWN,  /* returnType.  Not relevant for setters. */
86
  false,  /* isInfallible. False in setters. */
87
  false,  /* isMovable.  Not relevant for setters. */
88
  false, /* isEliminatable.  Not relevant for setters. */
89
  false, /* isAlwaysInSlot.  Only relevant for getters. */
90
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
91
  false,  /* isTypedMethod.  Only relevant for methods. */
92
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
93
};
94
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
95
static_assert(0 < 1, "There is no slot for us");
96
97
MOZ_CAN_RUN_SCRIPT static bool
98
namedItem(JSContext* cx, JS::Handle<JSObject*> obj, nsIHTMLCollection* self, const JSJitMethodCallArgs& args)
99
0
{
100
0
  AUTO_PROFILER_LABEL_FAST("HTMLCollection.namedItem", DOM, cx);
101
0
102
0
  if (MOZ_UNLIKELY(args.length() < 1)) {
103
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "HTMLCollection.namedItem");
104
0
  }
105
0
  binding_detail::FakeString arg0;
106
0
  if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
107
0
    return false;
108
0
  }
109
0
  auto result(StrongOrRawPtr<mozilla::dom::Element>(self->NamedItem(NonNullHelper(Constify(arg0)))));
110
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
111
0
  if (!result) {
112
0
    args.rval().setNull();
113
0
    return true;
114
0
  }
115
0
  if (!GetOrCreateDOMReflector(cx, result, args.rval())) {
116
0
    MOZ_ASSERT(JS_IsExceptionPending(cx));
117
0
    return false;
118
0
  }
119
0
  return true;
120
0
}
121
122
static const JSJitInfo namedItem_methodinfo = {
123
  { (JSJitGetterOp)namedItem },
124
  { prototypes::id::HTMLCollection },
125
  { PrototypeTraits<prototypes::id::HTMLCollection>::Depth },
126
  JSJitInfo::Method,
127
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
128
  JSVAL_TYPE_UNKNOWN,  /* returnType.  Not relevant for setters. */
129
  false,  /* isInfallible. False in setters. */
130
  false,  /* isMovable.  Not relevant for setters. */
131
  false, /* isEliminatable.  Not relevant for setters. */
132
  false, /* isAlwaysInSlot.  Only relevant for getters. */
133
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
134
  false,  /* isTypedMethod.  Only relevant for methods. */
135
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
136
};
137
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
138
static_assert(0 < 1, "There is no slot for us");
139
140
// We deliberately use brace-elision to make Visual Studio produce better initalization code.
141
#if defined(__clang__)
142
#pragma clang diagnostic push
143
#pragma clang diagnostic ignored "-Wmissing-braces"
144
#endif
145
static const JSFunctionSpec sMethods_specs[] = {
146
  JS_FNSPEC("item", (GenericMethod<NormalThisPolicy, ThrowExceptions>), reinterpret_cast<const JSJitInfo*>(&item_methodinfo), 1, JSPROP_ENUMERATE, nullptr),
147
  JS_FNSPEC("namedItem", (GenericMethod<NormalThisPolicy, ThrowExceptions>), reinterpret_cast<const JSJitInfo*>(&namedItem_methodinfo), 1, JSPROP_ENUMERATE, nullptr),
148
  JS_SYM_FNSPEC(iterator, nullptr, nullptr, 0, 0, "ArrayValues"),
149
  JS_FS_END
150
};
151
#if defined(__clang__)
152
#pragma clang diagnostic pop
153
#endif
154
155
156
static const Prefable<const JSFunctionSpec> sMethods[] = {
157
  { nullptr, &sMethods_specs[0] },
158
  { nullptr, nullptr }
159
};
160
161
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
162
    "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
163
static_assert(3 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
164
    "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
165
166
// We deliberately use brace-elision to make Visual Studio produce better initalization code.
167
#if defined(__clang__)
168
#pragma clang diagnostic push
169
#pragma clang diagnostic ignored "-Wmissing-braces"
170
#endif
171
static const JSPropertySpec sAttributes_specs[] = {
172
  { "length", JSPROP_ENUMERATE, GenericGetter<NormalThisPolicy, ThrowExceptions>, &length_getterinfo, nullptr, nullptr },
173
  { nullptr, 0, nullptr, nullptr, nullptr, nullptr }
174
};
175
#if defined(__clang__)
176
#pragma clang diagnostic pop
177
#endif
178
179
180
static const Prefable<const JSPropertySpec> sAttributes[] = {
181
  { nullptr, &sAttributes_specs[0] },
182
  { nullptr, nullptr }
183
};
184
185
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
186
    "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
187
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
188
    "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
189
190
191
static uint16_t sNativeProperties_sortedPropertyIndices[4];
192
static PropertyInfo sNativeProperties_propertyInfos[4];
193
194
static const NativePropertiesN<2> sNativeProperties = {
195
  false, 0,
196
  false, 0,
197
  true,  0 /* sMethods */,
198
  true,  1 /* sAttributes */,
199
  false, 0,
200
  false, 0,
201
  false, 0,
202
  -1,
203
  4,
204
  sNativeProperties_sortedPropertyIndices,
205
  {
206
    { sMethods, &sNativeProperties_propertyInfos[0] },
207
    { sAttributes, &sNativeProperties_propertyInfos[3] }
208
  }
209
};
210
static_assert(4 < 1ull << CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount),
211
    "We have a property info count that is oversized");
212
213
static const DOMIfaceAndProtoJSClass sInterfaceObjectClass = {
214
  {
215
    "Function",
216
    JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_SLOTS_BASE),
217
    &sBoringInterfaceObjectClassClassOps,
218
    JS_NULL_CLASS_SPEC,
219
    JS_NULL_CLASS_EXT,
220
    &sInterfaceObjectClassObjectOps
221
  },
222
  eInterface,
223
  true,
224
  prototypes::id::HTMLCollection,
225
  PrototypeTraits<prototypes::id::HTMLCollection>::Depth,
226
  sNativePropertyHooks,
227
  "function HTMLCollection() {\n    [native code]\n}",
228
  JS::GetRealmFunctionPrototype
229
};
230
231
static const DOMIfaceAndProtoJSClass sPrototypeClass = {
232
  {
233
    "HTMLCollectionPrototype",
234
    JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE),
235
    JS_NULL_CLASS_OPS,
236
    JS_NULL_CLASS_SPEC,
237
    JS_NULL_CLASS_EXT,
238
    JS_NULL_OBJECT_OPS
239
  },
240
  eInterfacePrototype,
241
  false,
242
  prototypes::id::HTMLCollection,
243
  PrototypeTraits<prototypes::id::HTMLCollection>::Depth,
244
  sNativePropertyHooks,
245
  "[object HTMLCollectionPrototype]",
246
  JS::GetRealmObjectPrototype
247
};
248
249
static_assert(IsBaseOf<nsISupports, nsIHTMLCollection >::value,
250
                  "We don't support non-nsISupports native classes for "
251
                  "proxy-based bindings yet");
252
253
254
class DOMProxyHandler : public mozilla::dom::DOMProxyHandler
255
{
256
public:
257
  explicit constexpr DOMProxyHandler()
258
0
  {
259
0
  }
260
261
  virtual bool
262
  getOwnPropDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool ignoreNamedProps, JS::MutableHandle<JS::PropertyDescriptor> desc) const override;
263
264
  virtual bool
265
  defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, JS::Handle<JS::PropertyDescriptor> desc, JS::ObjectOpResult& opresult, bool* defined) const override;
266
267
  using mozilla::dom::DOMProxyHandler::defineProperty;
268
269
  virtual bool
270
  ownPropNames(JSContext* cx, JS::Handle<JSObject*> proxy, unsigned flags, JS::AutoIdVector& props) const override;
271
272
  virtual bool
273
  hasOwn(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp) const override;
274
275
  virtual bool
276
  get(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<JS::Value> receiver, JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp) const override;
277
278
  virtual const char*
279
  className(JSContext* cx, JS::Handle<JSObject*> proxy) const override;
280
281
  virtual bool
282
  finalizeInBackground(const JS::Value& priv) const override;
283
284
  virtual void
285
  finalize(JSFreeOp* fop, JSObject* proxy) const override;
286
287
  static const DOMProxyHandler*
288
  getInstance();
289
290
  virtual bool
291
  delete_(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, JS::ObjectOpResult& opresult) const override;
292
293
  virtual bool
294
  getElements(JSContext* cx, JS::Handle<JSObject*> proxy, uint32_t begin, uint32_t end, js::ElementAdder* adder) const override;
295
296
  virtual size_t
297
  objectMoved(JSObject* obj, JSObject* old) const override;
298
};
299
300
MOZ_ALWAYS_INLINE bool
301
IsProxy(JSObject* obj)
302
0
{
303
0
  return js::IsProxy(obj) && js::GetProxyHandler(obj) == DOMProxyHandler::getInstance();
304
0
}
305
306
MOZ_ALWAYS_INLINE nsIHTMLCollection*
307
UnwrapProxy(JSObject* obj)
308
0
{
309
0
  MOZ_ASSERT(js::IsProxy(obj));
310
0
  if (js::GetProxyHandler(obj) != DOMProxyHandler::getInstance()) {
311
0
    MOZ_ASSERT(xpc::WrapperFactory::IsXrayWrapper(obj));
312
0
    obj = js::UncheckedUnwrap(obj);
313
0
  }
314
0
  MOZ_ASSERT(IsProxy(obj));
315
0
  return static_cast<nsIHTMLCollection*>(js::GetProxyReservedSlot(obj, DOM_OBJECT_SLOT).toPrivate());
316
0
}
317
318
bool
319
DOMProxyHandler::getOwnPropDescriptor(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool ignoreNamedProps, JS::MutableHandle<JS::PropertyDescriptor> desc) const
320
0
{
321
0
  bool isXray = xpc::WrapperFactory::IsXrayWrapper(proxy);
322
0
  uint32_t index = GetArrayIndexFromId(cx, id);
323
0
  if (IsArrayIndex(index)) {
324
0
    nsIHTMLCollection* self = UnwrapProxy(proxy);
325
0
    bool found = false;
326
0
    auto result(StrongOrRawPtr<mozilla::dom::Element>(self->IndexedGetter(index, found)));
327
0
    MOZ_ASSERT(!JS_IsExceptionPending(cx));
328
0
329
0
    if (found) {
330
0
      if (!result) {
331
0
        desc.value().setNull();
332
0
        FillPropertyDescriptor(desc, proxy, true);
333
0
        return true;
334
0
      }
335
0
      if (!GetOrCreateDOMReflector(cx, result, desc.value())) {
336
0
        MOZ_ASSERT(JS_IsExceptionPending(cx));
337
0
        return false;
338
0
      }
339
0
      FillPropertyDescriptor(desc, proxy, true);
340
0
      return true;
341
0
    }
342
0
  }
343
0
344
0
  JS::Rooted<JSObject*> expando(cx);
345
0
  if (!isXray && (expando = GetExpandoObject(proxy))) {
346
0
    if (!JS_GetOwnPropertyDescriptorById(cx, expando, id, desc)) {
347
0
      return false;
348
0
    }
349
0
    if (desc.object()) {
350
0
      // Pretend the property lives on the wrapper.
351
0
      desc.object().set(proxy);
352
0
      return true;
353
0
    }
354
0
  }
355
0
356
0
  bool callNamedGetter = false;
357
0
  if (!IsArrayIndex(index) && !ignoreNamedProps) {
358
0
    bool hasOnProto;
359
0
    if (!HasPropertyOnPrototype(cx, proxy, id, &hasOnProto)) {
360
0
      return false;
361
0
    }
362
0
    callNamedGetter = !hasOnProto;
363
0
  }
364
0
  if (callNamedGetter) {
365
0
    FakeString name;
366
0
    bool isSymbol;
367
0
    if (!ConvertIdToString(cx, id, name, isSymbol)) {
368
0
      return false;
369
0
    }
370
0
    if (!isSymbol) {
371
0
      nsIHTMLCollection* self = UnwrapProxy(proxy);
372
0
      bool found = false;
373
0
      auto result(StrongOrRawPtr<mozilla::dom::Element>(self->NamedGetter(NonNullHelper(Constify(name)), found)));
374
0
      MOZ_ASSERT(!JS_IsExceptionPending(cx));
375
0
376
0
      if (found) {
377
0
        if (!result) {
378
0
          desc.value().setNull();
379
0
          FillPropertyDescriptor(desc, proxy, true, false);
380
0
          return true;
381
0
        }
382
0
        if (!GetOrCreateDOMReflector(cx, result, desc.value())) {
383
0
          MOZ_ASSERT(JS_IsExceptionPending(cx));
384
0
          return false;
385
0
        }
386
0
        FillPropertyDescriptor(desc, proxy, true, false);
387
0
        return true;
388
0
      }
389
0
    }
390
0
  }
391
0
392
0
  desc.object().set(nullptr);
393
0
  return true;
394
0
}
395
396
bool
397
DOMProxyHandler::defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, JS::Handle<JS::PropertyDescriptor> desc, JS::ObjectOpResult& opresult, bool* defined) const
398
0
{
399
0
  if (IsArrayIndex(GetArrayIndexFromId(cx, id))) {
400
0
    *defined = true;
401
0
    return opresult.failNoIndexedSetter();
402
0
  }
403
0
  bool found = false;
404
0
  FakeString name;
405
0
  bool isSymbol;
406
0
  if (!ConvertIdToString(cx, id, name, isSymbol)) {
407
0
    return false;
408
0
  }
409
0
  if (!isSymbol) {
410
0
    nsIHTMLCollection* self = UnwrapProxy(proxy);
411
0
    auto result(StrongOrRawPtr<mozilla::dom::Element>(self->NamedGetter(NonNullHelper(Constify(name)), found)));
412
0
    MOZ_ASSERT(!JS_IsExceptionPending(cx));
413
0
    (void)result;
414
0
  }
415
0
416
0
  if (found) {
417
0
    *defined = true;
418
0
    return opresult.failNoNamedSetter();
419
0
  }
420
0
  return mozilla::dom::DOMProxyHandler::defineProperty(cx, proxy, id, desc, opresult, defined);
421
0
}
422
423
424
bool
425
DOMProxyHandler::ownPropNames(JSContext* cx, JS::Handle<JSObject*> proxy, unsigned flags, JS::AutoIdVector& props) const
426
0
{
427
0
  bool isXray = xpc::WrapperFactory::IsXrayWrapper(proxy);
428
0
429
0
  uint32_t length = UnwrapProxy(proxy)->Length();
430
0
  MOZ_ASSERT(int32_t(length) >= 0);
431
0
  for (int32_t i = 0; i < int32_t(length); ++i) {
432
0
    if (!props.append(INT_TO_JSID(i))) {
433
0
      return false;
434
0
    }
435
0
  }
436
0
437
0
  if (flags & JSITER_HIDDEN) {
438
0
    nsTArray<nsString> names;
439
0
    UnwrapProxy(proxy)->GetSupportedNames(names);
440
0
    if (!AppendNamedPropertyIds(cx, proxy, names, false, props)) {
441
0
      return false;
442
0
    }
443
0
  }
444
0
445
0
  JS::Rooted<JSObject*> expando(cx);
446
0
  if (!isXray && (expando = DOMProxyHandler::GetExpandoObject(proxy)) &&
447
0
      !js::GetPropertyKeys(cx, expando, flags, &props)) {
448
0
    return false;
449
0
  }
450
0
451
0
  return true;
452
0
}
453
454
bool
455
DOMProxyHandler::hasOwn(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp) const
456
0
{
457
0
  MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(proxy),
458
0
            "Should not have a XrayWrapper here");
459
0
460
0
  uint32_t index = GetArrayIndexFromId(cx, id);
461
0
  if (IsArrayIndex(index)) {
462
0
    bool found = false;
463
0
    nsIHTMLCollection* self = UnwrapProxy(proxy);
464
0
    auto result(StrongOrRawPtr<mozilla::dom::Element>(self->IndexedGetter(index, found)));
465
0
    MOZ_ASSERT(!JS_IsExceptionPending(cx));
466
0
    (void)result;
467
0
468
0
    *bp = found;
469
0
    return true;
470
0
  }
471
0
472
0
473
0
  JS::Rooted<JSObject*> expando(cx, GetExpandoObject(proxy));
474
0
  if (expando) {
475
0
    bool b = true;
476
0
    bool ok = JS_HasPropertyById(cx, expando, id, &b);
477
0
    *bp = !!b;
478
0
    if (!ok || *bp) {
479
0
      return ok;
480
0
    }
481
0
  }
482
0
483
0
  bool hasOnProto;
484
0
  if (!HasPropertyOnPrototype(cx, proxy, id, &hasOnProto)) {
485
0
    return false;
486
0
  }
487
0
  if (!hasOnProto) {
488
0
    bool found = false;
489
0
    FakeString name;
490
0
    bool isSymbol;
491
0
    if (!ConvertIdToString(cx, id, name, isSymbol)) {
492
0
      return false;
493
0
    }
494
0
    if (!isSymbol) {
495
0
      nsIHTMLCollection* self = UnwrapProxy(proxy);
496
0
      auto result(StrongOrRawPtr<mozilla::dom::Element>(self->NamedGetter(NonNullHelper(Constify(name)), found)));
497
0
      MOZ_ASSERT(!JS_IsExceptionPending(cx));
498
0
      (void)result;
499
0
    }
500
0
501
0
    *bp = found;
502
0
    return true;
503
0
  }
504
0
  *bp = false;
505
0
  return true;
506
0
}
507
508
bool
509
DOMProxyHandler::get(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<JS::Value> receiver, JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp) const
510
0
{
511
0
  MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(proxy),
512
0
              "Should not have a XrayWrapper here");
513
0
514
0
  uint32_t index = GetArrayIndexFromId(cx, id);
515
0
  if (IsArrayIndex(index)) {
516
0
    nsIHTMLCollection* self = UnwrapProxy(proxy);
517
0
    bool found = false;
518
0
    auto result(StrongOrRawPtr<mozilla::dom::Element>(self->IndexedGetter(index, found)));
519
0
    MOZ_ASSERT(!JS_IsExceptionPending(cx));
520
0
521
0
    if (found) {
522
0
      if (!result) {
523
0
        vp.setNull();
524
0
        return true;
525
0
      }
526
0
      if (!GetOrCreateDOMReflector(cx, result, vp)) {
527
0
        MOZ_ASSERT(JS_IsExceptionPending(cx));
528
0
        return false;
529
0
      }
530
0
      return true;
531
0
    }
532
0
    // Even if we don't have this index, we don't forward the
533
0
    // get on to our expando object.
534
0
  } else {
535
0
    { // Scope for expando
536
0
      JS::Rooted<JSObject*> expando(cx, DOMProxyHandler::GetExpandoObject(proxy));
537
0
      if (expando) {
538
0
        bool hasProp;
539
0
        if (!JS_HasPropertyById(cx, expando, id, &hasProp)) {
540
0
          return false;
541
0
        }
542
0
543
0
        if (hasProp) {
544
0
          // Forward the get to the expando object, but our receiver is whatever our
545
0
          // receiver is.
546
0
          return JS_ForwardGetPropertyTo(cx, expando, id, receiver, vp);
547
0
        }
548
0
      }
549
0
    }
550
0
  }
551
0
552
0
  bool foundOnPrototype;
553
0
  if (!GetPropertyOnPrototype(cx, proxy, receiver, id, &foundOnPrototype, vp)) {
554
0
    return false;
555
0
  }
556
0
557
0
  if (foundOnPrototype) {
558
0
    return true;
559
0
  }
560
0
561
0
  if (!IsArrayIndex(index)) {
562
0
    FakeString name;
563
0
    bool isSymbol;
564
0
    if (!ConvertIdToString(cx, id, name, isSymbol)) {
565
0
      return false;
566
0
    }
567
0
    if (!isSymbol) {
568
0
      nsIHTMLCollection* self = UnwrapProxy(proxy);
569
0
      bool found = false;
570
0
      auto result(StrongOrRawPtr<mozilla::dom::Element>(self->NamedGetter(NonNullHelper(Constify(name)), found)));
571
0
      MOZ_ASSERT(!JS_IsExceptionPending(cx));
572
0
573
0
      if (found) {
574
0
        if (!result) {
575
0
          vp.setNull();
576
0
          return true;
577
0
        }
578
0
        if (!GetOrCreateDOMReflector(cx, result, vp)) {
579
0
          MOZ_ASSERT(JS_IsExceptionPending(cx));
580
0
          return false;
581
0
        }
582
0
        return true;
583
0
      }
584
0
    }
585
0
  }
586
0
587
0
  vp.setUndefined();
588
0
  return true;
589
0
}
590
591
const char*
592
DOMProxyHandler::className(JSContext* cx, JS::Handle<JSObject*> proxy) const
593
0
{
594
0
  return "HTMLCollection";
595
0
}
596
597
bool
598
DOMProxyHandler::finalizeInBackground(const JS::Value& priv) const
599
0
{
600
0
  return false;
601
0
}
602
603
void
604
DOMProxyHandler::finalize(JSFreeOp* fop, JSObject* proxy) const
605
0
{
606
0
  nsIHTMLCollection* self = UnwrapPossiblyNotInitializedDOMObject<nsIHTMLCollection>(proxy);
607
0
  if (self) {
608
0
    ClearWrapper(self, self, proxy);
609
0
    AddForDeferredFinalization<nsIHTMLCollection>(self);
610
0
  }
611
0
}
612
613
const DOMProxyHandler*
614
DOMProxyHandler::getInstance()
615
0
{
616
0
  static const DOMProxyHandler instance;
617
0
  return &instance;
618
0
}
619
620
bool
621
DOMProxyHandler::delete_(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, JS::ObjectOpResult& opresult) const
622
0
{
623
0
  MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(proxy),
624
0
            "Should not have a XrayWrapper here");
625
0
626
0
  uint32_t index = GetArrayIndexFromId(cx, id);
627
0
  if (IsArrayIndex(index)) {
628
0
    bool deleteSucceeded;
629
0
    bool found = false;
630
0
    nsIHTMLCollection* self = UnwrapProxy(proxy);
631
0
    auto result(StrongOrRawPtr<mozilla::dom::Element>(self->IndexedGetter(index, found)));
632
0
    MOZ_ASSERT(!JS_IsExceptionPending(cx));
633
0
    (void)result;
634
0
    deleteSucceeded = !found;
635
0
    return deleteSucceeded ? opresult.succeed() : opresult.failCantDelete();
636
0
  }
637
0
  // Try named delete only if the named property visibility
638
0
  // algorithm says the property is visible.
639
0
  bool tryNamedDelete = true;
640
0
  { // Scope for expando
641
0
    JS::Rooted<JSObject*> expando(cx, DOMProxyHandler::GetExpandoObject(proxy));
642
0
    if (expando) {
643
0
      bool hasProp;
644
0
      if (!JS_HasPropertyById(cx, expando, id, &hasProp)) {
645
0
        return false;
646
0
      }
647
0
      tryNamedDelete = !hasProp;
648
0
    }
649
0
  }
650
0
  if (tryNamedDelete) {
651
0
    bool hasOnProto;
652
0
    if (!HasPropertyOnPrototype(cx, proxy, id, &hasOnProto)) {
653
0
      return false;
654
0
    }
655
0
    tryNamedDelete = !hasOnProto;
656
0
  }
657
0
  if (tryNamedDelete) {
658
0
    bool found = false;
659
0
    bool deleteSucceeded;
660
0
    FakeString name;
661
0
    bool isSymbol;
662
0
    if (!ConvertIdToString(cx, id, name, isSymbol)) {
663
0
      return false;
664
0
    }
665
0
    if (!isSymbol) {
666
0
      nsIHTMLCollection* self = UnwrapProxy(proxy);
667
0
      auto result(StrongOrRawPtr<mozilla::dom::Element>(self->NamedGetter(NonNullHelper(Constify(name)), found)));
668
0
      MOZ_ASSERT(!JS_IsExceptionPending(cx));
669
0
      (void)result;
670
0
    }
671
0
    deleteSucceeded = !found;
672
0
    if (found) {
673
0
      return deleteSucceeded ? opresult.succeed() : opresult.failCantDelete();
674
0
    }
675
0
  }
676
0
677
0
  return dom::DOMProxyHandler::delete_(cx, proxy, id, opresult);
678
0
}
679
680
bool
681
DOMProxyHandler::getElements(JSContext* cx, JS::Handle<JSObject*> proxy, uint32_t begin, uint32_t end, js::ElementAdder* adder) const
682
0
{
683
0
  JS::Rooted<JS::Value> temp(cx);
684
0
  MOZ_ASSERT(!xpc::WrapperFactory::IsXrayWrapper(proxy),
685
0
             "Should not have a XrayWrapper here");
686
0
687
0
  nsIHTMLCollection* self = UnwrapProxy(proxy);
688
0
  uint32_t length = self->Length();
689
0
  // Compute the end of the indices we'll get ourselves
690
0
  uint32_t ourEnd = std::max(begin, std::min(end, length));
691
0
692
0
  for (uint32_t index = begin; index < ourEnd; ++index) {
693
0
    bool found = false;
694
0
    auto result(StrongOrRawPtr<mozilla::dom::Element>(self->IndexedGetter(index, found)));
695
0
    MOZ_ASSERT(!JS_IsExceptionPending(cx));
696
0
697
0
    MOZ_ASSERT(found);
698
0
    if (!result) {
699
0
      temp.setNull();
700
0
      if (!adder->append(cx, temp)) return false;
701
0
      continue;
702
0
    }
703
0
    if (!GetOrCreateDOMReflector(cx, result, &temp)) {
704
0
      MOZ_ASSERT(JS_IsExceptionPending(cx));
705
0
      return false;
706
0
    }
707
0
    if (!adder->append(cx, temp)) return false;
708
0
    continue;
709
0
  }
710
0
711
0
  if (end > ourEnd) {
712
0
    JS::Rooted<JSObject*> proto(cx);
713
0
    if (!js::GetObjectProto(cx, proxy, &proto)) {
714
0
      return false;
715
0
    }
716
0
    return js::GetElementsWithAdder(cx, proto, proxy, ourEnd, end, adder);
717
0
  }
718
0
719
0
  return true;
720
0
}
721
722
size_t
723
DOMProxyHandler::objectMoved(JSObject* obj, JSObject* old) const
724
0
{
725
0
  nsIHTMLCollection* self = UnwrapPossiblyNotInitializedDOMObject<nsIHTMLCollection>(obj);
726
0
  if (self) {
727
0
    UpdateWrapper(self, self, obj, old);
728
0
  }
729
0
730
0
  return 0;
731
0
}
732
733
static const DOMJSClass sClass = {
734
  PROXY_CLASS_DEF("HTMLCollection",
735
                  JSCLASS_IS_DOMJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(1)),
736
  { prototypes::id::HTMLCollection, 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 },
737
  IsBaseOf<nsISupports, nsIHTMLCollection >::value,
738
  sNativePropertyHooks,
739
  FindAssociatedGlobalForNative<nsIHTMLCollection>::Get,
740
  GetProtoObjectHandle,
741
  GetCCParticipant<nsIHTMLCollection>::Get()
742
};
743
744
bool
745
Wrap(JSContext* aCx, nsIHTMLCollection* aObject, nsWrapperCache* aCache, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
746
0
{
747
0
  static_assert(!IsBaseOf<NonRefcountedDOMObject, nsIHTMLCollection>::value,
748
0
                "Shouldn't have wrappercached things that are not refcounted.");
749
0
  MOZ_ASSERT(static_cast<nsIHTMLCollection*>(aObject) ==
750
0
             reinterpret_cast<nsIHTMLCollection*>(aObject),
751
0
             "Multiple inheritance for nsIHTMLCollection is broken.");
752
0
  MOZ_ASSERT(ToSupportsIsCorrect(aObject));
753
0
  MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx));
754
0
  MOZ_ASSERT(!aCache->GetWrapper(),
755
0
             "You should probably not be using Wrap() directly; use "
756
0
             "GetOrCreateDOMReflector instead");
757
0
758
0
  MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache),
759
0
             "nsISupports must be on our primary inheritance chain");
760
0
761
0
  JS::Rooted<JSObject*> global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject()));
762
0
  if (!global) {
763
0
    return false;
764
0
  }
765
0
  MOZ_ASSERT(JS_IsGlobalObject(global));
766
0
  MOZ_ASSERT(JS::ObjectIsNotGray(global));
767
0
768
0
  // That might have ended up wrapping us already, due to the wonders
769
0
  // of XBL.  Check for that, and bail out as needed.
770
0
  aReflector.set(aCache->GetWrapper());
771
0
  if (aReflector) {
772
#ifdef DEBUG
773
    AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto);
774
#endif // DEBUG
775
    return true;
776
0
  }
777
0
778
0
  JSAutoRealm ar(aCx, global);
779
0
  JS::Handle<JSObject*> canonicalProto = GetProtoObjectHandle(aCx);
780
0
  if (!canonicalProto) {
781
0
    return false;
782
0
  }
783
0
  JS::Rooted<JSObject*> proto(aCx);
784
0
  if (aGivenProto) {
785
0
    proto = aGivenProto;
786
0
    // Unfortunately, while aGivenProto was in the compartment of aCx
787
0
    // coming in, we changed compartments to that of "parent" so may need
788
0
    // to wrap the proto here.
789
0
    if (js::GetContextCompartment(aCx) != js::GetObjectCompartment(proto)) {
790
0
      if (!JS_WrapObject(aCx, &proto)) {
791
0
        return false;
792
0
      }
793
0
    }
794
0
  } else {
795
0
    proto = canonicalProto;
796
0
  }
797
0
798
0
  BindingJSObjectCreator<nsIHTMLCollection> creator(aCx);
799
0
  creator.CreateProxyObject(aCx, &sClass.mBase, DOMProxyHandler::getInstance(),
800
0
                            proto, aObject, JS::UndefinedHandleValue, aReflector);
801
0
  if (!aReflector) {
802
0
    return false;
803
0
  }
804
0
805
0
  aCache->SetWrapper(aReflector);
806
0
  creator.InitializationSucceeded();
807
0
808
0
  MOZ_ASSERT(aCache->GetWrapperPreserveColor() &&
809
0
             aCache->GetWrapperPreserveColor() == aReflector);
810
0
  // If proto != canonicalProto, we have to preserve our wrapper;
811
0
  // otherwise we won't be able to properly recreate it later, since
812
0
  // we won't know what proto to use.  Note that we don't check
813
0
  // aGivenProto here, since it's entirely possible (and even
814
0
  // somewhat common) to have a non-null aGivenProto which is the
815
0
  // same as canonicalProto.
816
0
  if (proto != canonicalProto) {
817
0
    PreserveWrapper(aObject);
818
0
  }
819
0
820
0
  return true;
821
0
}
822
823
static bool
824
ResolveOwnProperty(JSContext* cx, JS::Handle<JSObject*> wrapper, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::MutableHandle<JS::PropertyDescriptor> desc)
825
0
{
826
0
  return js::GetProxyHandler(obj)->getOwnPropertyDescriptor(cx, wrapper, id, desc);
827
0
}
828
829
static bool
830
EnumerateOwnProperties(JSContext* cx, JS::Handle<JSObject*> wrapper, JS::Handle<JSObject*> obj, JS::AutoIdVector& props)
831
0
{
832
0
  return js::GetProxyHandler(obj)->ownPropertyKeys(cx, wrapper, props);
833
0
}
834
835
const NativePropertyHooks sNativePropertyHooks[] = { {
836
  ResolveOwnProperty,
837
  EnumerateOwnProperties,
838
  nullptr,
839
  { sNativeProperties.Upcast(), nullptr },
840
  prototypes::id::HTMLCollection,
841
  constructors::id::HTMLCollection,
842
  nullptr,
843
  &DefaultXrayExpandoObjectClass
844
} };
845
846
void
847
CreateInterfaceObjects(JSContext* aCx, JS::Handle<JSObject*> aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, bool aDefineOnGlobal)
848
0
{
849
0
  JS::Rooted<JSObject*> parentProto(aCx, JS::GetRealmObjectPrototype(aCx));
850
0
  if (!parentProto) {
851
0
    return;
852
0
  }
853
0
854
0
  JS::Rooted<JSObject*> constructorProto(aCx, JS::GetRealmFunctionPrototype(aCx));
855
0
  if (!constructorProto) {
856
0
    return;
857
0
  }
858
0
859
0
  static bool sIdsInited = false;
860
0
  if (!sIdsInited && NS_IsMainThread()) {
861
0
    if (!InitIds(aCx, sNativeProperties.Upcast())) {
862
0
      return;
863
0
    }
864
0
    sIdsInited = true;
865
0
  }
866
0
867
0
  JS::Heap<JSObject*>* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::HTMLCollection);
868
0
  JS::Heap<JSObject*>* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::HTMLCollection);
869
0
  dom::CreateInterfaceObjects(aCx, aGlobal, parentProto,
870
0
                              &sPrototypeClass.mBase, protoCache,
871
0
                              nullptr,
872
0
                              constructorProto, &sInterfaceObjectClass.mBase, 0, nullptr,
873
0
                              interfaceCache,
874
0
                              sNativeProperties.Upcast(),
875
0
                              nullptr,
876
0
                              "HTMLCollection", aDefineOnGlobal,
877
0
                              nullptr,
878
0
                              false);
879
0
}
880
881
JSObject*
882
GetProtoObject(JSContext* aCx)
883
0
{
884
0
  return GetProtoObjectHandle(aCx);
885
0
}
886
887
JSObject*
888
GetConstructorObject(JSContext* aCx)
889
0
{
890
0
  return GetConstructorObjectHandle(aCx);
891
0
}
892
893
} // namespace HTMLCollection_Binding
894
895
896
897
} // namespace dom
898
} // namespace mozilla