Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dom/bindings/MediaTrackConstraintSetBinding.cpp
Line
Count
Source (jump to first uncovered line)
1
/* THIS FILE IS AUTOGENERATED FROM MediaTrackConstraintSet.webidl BY Codegen.py - DO NOT EDIT */
2
3
#include "AtomList.h"
4
#include "MediaTrackConstraintSetBinding.h"
5
#include "mozilla/FloatingPoint.h"
6
#include "mozilla/OwningNonNull.h"
7
#include "mozilla/dom/BindingUtils.h"
8
#include "mozilla/dom/NonRefcountedDOMObject.h"
9
#include "mozilla/dom/PrimitiveConversions.h"
10
#include "mozilla/dom/ScriptSettings.h"
11
#include "mozilla/dom/SimpleGlobalObject.h"
12
#include "mozilla/dom/UnionConversions.h"
13
14
namespace mozilla {
15
namespace dom {
16
17
namespace binding_detail {}; // Just to make sure it's known as a namespace
18
using namespace mozilla::dom::binding_detail;
19
20
21
22
ConstrainBooleanParameters::ConstrainBooleanParameters()
23
0
{
24
0
  // Safe to pass a null context if we pass a null value
25
0
  Init(nullptr, JS::NullHandleValue);
26
0
}
27
28
29
30
bool
31
ConstrainBooleanParameters::InitIds(JSContext* cx, ConstrainBooleanParametersAtoms* atomsCache)
32
0
{
33
0
  MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
34
0
35
0
  // Initialize these in reverse order so that any failure leaves the first one
36
0
  // uninitialized.
37
0
  if (!atomsCache->ideal_id.init(cx, "ideal") ||
38
0
      !atomsCache->exact_id.init(cx, "exact")) {
39
0
    return false;
40
0
  }
41
0
  return true;
42
0
}
43
44
bool
45
ConstrainBooleanParameters::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
46
0
{
47
0
  // Passing a null JSContext is OK only if we're initing from null,
48
0
  // Since in that case we will not have to do any property gets
49
0
  // Also evaluate isNullOrUndefined in order to avoid false-positive
50
0
  // checkers by static analysis tools
51
0
  MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
52
0
  ConstrainBooleanParametersAtoms* atomsCache = nullptr;
53
0
  if (cx) {
54
0
    atomsCache = GetAtomCache<ConstrainBooleanParametersAtoms>(cx);
55
0
    if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
56
0
      return false;
57
0
    }
58
0
  }
59
0
60
0
  if (!IsConvertibleToDictionary(val)) {
61
0
    return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
62
0
  }
63
0
64
0
  bool isNull = val.isNullOrUndefined();
65
0
  // We only need these if !isNull, in which case we have |cx|.
66
0
  Maybe<JS::Rooted<JSObject *> > object;
67
0
  Maybe<JS::Rooted<JS::Value> > temp;
68
0
  if (!isNull) {
69
0
    MOZ_ASSERT(cx);
70
0
    object.emplace(cx, &val.toObject());
71
0
    temp.emplace(cx);
72
0
  }
73
0
  if (!isNull) {
74
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->exact_id, temp.ptr())) {
75
0
      return false;
76
0
    }
77
0
  }
78
0
  if (!isNull && !temp->isUndefined()) {
79
0
    mExact.Construct();
80
0
    if (!ValueToPrimitive<bool, eDefault>(cx, temp.ref(), &(mExact.Value()))) {
81
0
      return false;
82
0
    }
83
0
    mIsAnyMemberPresent = true;
84
0
  }
85
0
86
0
  if (!isNull) {
87
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->ideal_id, temp.ptr())) {
88
0
      return false;
89
0
    }
90
0
  }
91
0
  if (!isNull && !temp->isUndefined()) {
92
0
    mIdeal.Construct();
93
0
    if (!ValueToPrimitive<bool, eDefault>(cx, temp.ref(), &(mIdeal.Value()))) {
94
0
      return false;
95
0
    }
96
0
    mIsAnyMemberPresent = true;
97
0
  }
98
0
  return true;
99
0
}
100
101
bool
102
ConstrainBooleanParameters::Init(const nsAString& aJSON)
103
0
{
104
0
  AutoJSAPI jsapi;
105
0
  JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
106
0
  if (!cleanGlobal) {
107
0
    return false;
108
0
  }
109
0
  if (!jsapi.Init(cleanGlobal)) {
110
0
    return false;
111
0
  }
112
0
  JSContext* cx = jsapi.cx();
113
0
  JS::Rooted<JS::Value> json(cx);
114
0
  bool ok = ParseJSON(cx, aJSON, &json);
115
0
  NS_ENSURE_TRUE(ok, false);
116
0
  return Init(cx, json);
117
0
}
118
119
bool
120
ConstrainBooleanParameters::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
121
0
{
122
0
  ConstrainBooleanParametersAtoms* atomsCache = GetAtomCache<ConstrainBooleanParametersAtoms>(cx);
123
0
  if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
124
0
    return false;
125
0
  }
126
0
127
0
  JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
128
0
  if (!obj) {
129
0
    return false;
130
0
  }
131
0
  rval.set(JS::ObjectValue(*obj));
132
0
133
0
  if (mExact.WasPassed()) {
134
0
    do {
135
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
136
0
      JS::Rooted<JS::Value> temp(cx);
137
0
      bool const & currentValue = mExact.InternalValue();
138
0
      temp.setBoolean(currentValue);
139
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->exact_id, temp, JSPROP_ENUMERATE)) {
140
0
        return false;
141
0
      }
142
0
      break;
143
0
    } while(false);
144
0
  }
145
0
146
0
  if (mIdeal.WasPassed()) {
147
0
    do {
148
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
149
0
      JS::Rooted<JS::Value> temp(cx);
150
0
      bool const & currentValue = mIdeal.InternalValue();
151
0
      temp.setBoolean(currentValue);
152
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->ideal_id, temp, JSPROP_ENUMERATE)) {
153
0
        return false;
154
0
      }
155
0
      break;
156
0
    } while(false);
157
0
  }
158
0
159
0
  return true;
160
0
}
161
162
bool
163
ConstrainBooleanParameters::ToJSON(nsAString& aJSON) const
164
0
{
165
0
  AutoJSAPI jsapi;
166
0
  jsapi.Init();
167
0
  JSContext *cx = jsapi.cx();
168
0
  // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
169
0
  // because we'll only be creating objects, in ways that have no
170
0
  // side-effects, followed by a call to JS::ToJSONMaybeSafely,
171
0
  // which likewise guarantees no side-effects for the sorts of
172
0
  // things we will pass it.
173
0
  JSAutoRealm ar(cx, UnprivilegedJunkScopeOrWorkerGlobal());
174
0
  JS::Rooted<JS::Value> val(cx);
175
0
  if (!ToObjectInternal(cx, &val)) {
176
0
    return false;
177
0
  }
178
0
  JS::Rooted<JSObject*> obj(cx, &val.toObject());
179
0
  return StringifyToJSON(cx, obj, aJSON);
180
0
}
181
182
void
183
ConstrainBooleanParameters::TraceDictionary(JSTracer* trc)
184
0
{
185
0
}
186
187
ConstrainBooleanParameters&
188
ConstrainBooleanParameters::operator=(const ConstrainBooleanParameters& aOther)
189
0
{
190
0
  DictionaryBase::operator=(aOther);
191
0
  mExact.Reset();
192
0
  if (aOther.mExact.WasPassed()) {
193
0
    mExact.Construct(aOther.mExact.Value());
194
0
  }
195
0
  mIdeal.Reset();
196
0
  if (aOther.mIdeal.WasPassed()) {
197
0
    mIdeal.Construct(aOther.mIdeal.Value());
198
0
  }
199
0
  return *this;
200
0
}
201
202
namespace binding_detail {
203
} // namespace binding_detail
204
205
206
207
ConstrainDOMStringParameters::ConstrainDOMStringParameters()
208
0
{
209
0
  // Safe to pass a null context if we pass a null value
210
0
  Init(nullptr, JS::NullHandleValue);
211
0
}
212
213
214
215
bool
216
ConstrainDOMStringParameters::InitIds(JSContext* cx, ConstrainDOMStringParametersAtoms* atomsCache)
217
0
{
218
0
  MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
219
0
220
0
  // Initialize these in reverse order so that any failure leaves the first one
221
0
  // uninitialized.
222
0
  if (!atomsCache->ideal_id.init(cx, "ideal") ||
223
0
      !atomsCache->exact_id.init(cx, "exact")) {
224
0
    return false;
225
0
  }
226
0
  return true;
227
0
}
228
229
bool
230
ConstrainDOMStringParameters::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
231
0
{
232
0
  // Passing a null JSContext is OK only if we're initing from null,
233
0
  // Since in that case we will not have to do any property gets
234
0
  // Also evaluate isNullOrUndefined in order to avoid false-positive
235
0
  // checkers by static analysis tools
236
0
  MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
237
0
  ConstrainDOMStringParametersAtoms* atomsCache = nullptr;
238
0
  if (cx) {
239
0
    atomsCache = GetAtomCache<ConstrainDOMStringParametersAtoms>(cx);
240
0
    if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
241
0
      return false;
242
0
    }
243
0
  }
244
0
245
0
  if (!IsConvertibleToDictionary(val)) {
246
0
    return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
247
0
  }
248
0
249
0
  bool isNull = val.isNullOrUndefined();
250
0
  // We only need these if !isNull, in which case we have |cx|.
251
0
  Maybe<JS::Rooted<JSObject *> > object;
252
0
  Maybe<JS::Rooted<JS::Value> > temp;
253
0
  if (!isNull) {
254
0
    MOZ_ASSERT(cx);
255
0
    object.emplace(cx, &val.toObject());
256
0
    temp.emplace(cx);
257
0
  }
258
0
  if (!isNull) {
259
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->exact_id, temp.ptr())) {
260
0
      return false;
261
0
    }
262
0
  }
263
0
  if (!isNull && !temp->isUndefined()) {
264
0
    mExact.Construct();
265
0
    {
266
0
      bool done = false, failed = false, tryNext;
267
0
      if (temp.ref().isObject()) {
268
0
        done = (failed = !(mExact.Value()).TrySetToStringSequence(cx, temp.ref(), tryNext, passedToJSImpl)) || !tryNext;
269
0
      }
270
0
      if (!done) {
271
0
        do {
272
0
          done = (failed = !(mExact.Value()).TrySetToString(cx, temp.ref(), tryNext)) || !tryNext;
273
0
          break;
274
0
        } while (false);
275
0
      }
276
0
      if (failed) {
277
0
        return false;
278
0
      }
279
0
      if (!done) {
280
0
        ThrowErrorMessage(cx, MSG_NOT_IN_UNION, "'exact' member of ConstrainDOMStringParameters", "StringSequence");
281
0
        return false;
282
0
      }
283
0
    }
284
0
    mIsAnyMemberPresent = true;
285
0
  }
286
0
287
0
  if (!isNull) {
288
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->ideal_id, temp.ptr())) {
289
0
      return false;
290
0
    }
291
0
  }
292
0
  if (!isNull && !temp->isUndefined()) {
293
0
    mIdeal.Construct();
294
0
    {
295
0
      bool done = false, failed = false, tryNext;
296
0
      if (temp.ref().isObject()) {
297
0
        done = (failed = !(mIdeal.Value()).TrySetToStringSequence(cx, temp.ref(), tryNext, passedToJSImpl)) || !tryNext;
298
0
      }
299
0
      if (!done) {
300
0
        do {
301
0
          done = (failed = !(mIdeal.Value()).TrySetToString(cx, temp.ref(), tryNext)) || !tryNext;
302
0
          break;
303
0
        } while (false);
304
0
      }
305
0
      if (failed) {
306
0
        return false;
307
0
      }
308
0
      if (!done) {
309
0
        ThrowErrorMessage(cx, MSG_NOT_IN_UNION, "'ideal' member of ConstrainDOMStringParameters", "StringSequence");
310
0
        return false;
311
0
      }
312
0
    }
313
0
    mIsAnyMemberPresent = true;
314
0
  }
315
0
  return true;
316
0
}
317
318
bool
319
ConstrainDOMStringParameters::Init(const nsAString& aJSON)
320
0
{
321
0
  AutoJSAPI jsapi;
322
0
  JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
323
0
  if (!cleanGlobal) {
324
0
    return false;
325
0
  }
326
0
  if (!jsapi.Init(cleanGlobal)) {
327
0
    return false;
328
0
  }
329
0
  JSContext* cx = jsapi.cx();
330
0
  JS::Rooted<JS::Value> json(cx);
331
0
  bool ok = ParseJSON(cx, aJSON, &json);
332
0
  NS_ENSURE_TRUE(ok, false);
333
0
  return Init(cx, json);
334
0
}
335
336
bool
337
ConstrainDOMStringParameters::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
338
0
{
339
0
  ConstrainDOMStringParametersAtoms* atomsCache = GetAtomCache<ConstrainDOMStringParametersAtoms>(cx);
340
0
  if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
341
0
    return false;
342
0
  }
343
0
344
0
  JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
345
0
  if (!obj) {
346
0
    return false;
347
0
  }
348
0
  rval.set(JS::ObjectValue(*obj));
349
0
350
0
  if (mExact.WasPassed()) {
351
0
    do {
352
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
353
0
      JS::Rooted<JS::Value> temp(cx);
354
0
      OwningStringOrStringSequence const & currentValue = mExact.InternalValue();
355
0
      if (!currentValue.ToJSVal(cx, obj, &temp)) {
356
0
        return false;
357
0
      }
358
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->exact_id, temp, JSPROP_ENUMERATE)) {
359
0
        return false;
360
0
      }
361
0
      break;
362
0
    } while(false);
363
0
  }
364
0
365
0
  if (mIdeal.WasPassed()) {
366
0
    do {
367
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
368
0
      JS::Rooted<JS::Value> temp(cx);
369
0
      OwningStringOrStringSequence const & currentValue = mIdeal.InternalValue();
370
0
      if (!currentValue.ToJSVal(cx, obj, &temp)) {
371
0
        return false;
372
0
      }
373
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->ideal_id, temp, JSPROP_ENUMERATE)) {
374
0
        return false;
375
0
      }
376
0
      break;
377
0
    } while(false);
378
0
  }
379
0
380
0
  return true;
381
0
}
382
383
bool
384
ConstrainDOMStringParameters::ToJSON(nsAString& aJSON) const
385
0
{
386
0
  AutoJSAPI jsapi;
387
0
  jsapi.Init();
388
0
  JSContext *cx = jsapi.cx();
389
0
  // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
390
0
  // because we'll only be creating objects, in ways that have no
391
0
  // side-effects, followed by a call to JS::ToJSONMaybeSafely,
392
0
  // which likewise guarantees no side-effects for the sorts of
393
0
  // things we will pass it.
394
0
  JSAutoRealm ar(cx, UnprivilegedJunkScopeOrWorkerGlobal());
395
0
  JS::Rooted<JS::Value> val(cx);
396
0
  if (!ToObjectInternal(cx, &val)) {
397
0
    return false;
398
0
  }
399
0
  JS::Rooted<JSObject*> obj(cx, &val.toObject());
400
0
  return StringifyToJSON(cx, obj, aJSON);
401
0
}
402
403
void
404
ConstrainDOMStringParameters::TraceDictionary(JSTracer* trc)
405
0
{
406
0
}
407
408
ConstrainDOMStringParameters&
409
ConstrainDOMStringParameters::operator=(const ConstrainDOMStringParameters& aOther)
410
0
{
411
0
  DictionaryBase::operator=(aOther);
412
0
  mExact.Reset();
413
0
  if (aOther.mExact.WasPassed()) {
414
0
    mExact.Construct(aOther.mExact.Value());
415
0
  }
416
0
  mIdeal.Reset();
417
0
  if (aOther.mIdeal.WasPassed()) {
418
0
    mIdeal.Construct(aOther.mIdeal.Value());
419
0
  }
420
0
  return *this;
421
0
}
422
423
namespace binding_detail {
424
} // namespace binding_detail
425
426
427
428
ConstrainDoubleRange::ConstrainDoubleRange()
429
0
{
430
0
  // Safe to pass a null context if we pass a null value
431
0
  Init(nullptr, JS::NullHandleValue);
432
0
}
433
434
435
436
bool
437
ConstrainDoubleRange::InitIds(JSContext* cx, ConstrainDoubleRangeAtoms* atomsCache)
438
0
{
439
0
  MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
440
0
441
0
  // Initialize these in reverse order so that any failure leaves the first one
442
0
  // uninitialized.
443
0
  if (!atomsCache->min_id.init(cx, "min") ||
444
0
      !atomsCache->max_id.init(cx, "max") ||
445
0
      !atomsCache->ideal_id.init(cx, "ideal") ||
446
0
      !atomsCache->exact_id.init(cx, "exact")) {
447
0
    return false;
448
0
  }
449
0
  return true;
450
0
}
451
452
bool
453
ConstrainDoubleRange::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
454
0
{
455
0
  // Passing a null JSContext is OK only if we're initing from null,
456
0
  // Since in that case we will not have to do any property gets
457
0
  // Also evaluate isNullOrUndefined in order to avoid false-positive
458
0
  // checkers by static analysis tools
459
0
  MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
460
0
  ConstrainDoubleRangeAtoms* atomsCache = nullptr;
461
0
  if (cx) {
462
0
    atomsCache = GetAtomCache<ConstrainDoubleRangeAtoms>(cx);
463
0
    if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
464
0
      return false;
465
0
    }
466
0
  }
467
0
468
0
  if (!IsConvertibleToDictionary(val)) {
469
0
    return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
470
0
  }
471
0
472
0
  bool isNull = val.isNullOrUndefined();
473
0
  // We only need these if !isNull, in which case we have |cx|.
474
0
  Maybe<JS::Rooted<JSObject *> > object;
475
0
  Maybe<JS::Rooted<JS::Value> > temp;
476
0
  if (!isNull) {
477
0
    MOZ_ASSERT(cx);
478
0
    object.emplace(cx, &val.toObject());
479
0
    temp.emplace(cx);
480
0
  }
481
0
  if (!isNull) {
482
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->exact_id, temp.ptr())) {
483
0
      return false;
484
0
    }
485
0
  }
486
0
  if (!isNull && !temp->isUndefined()) {
487
0
    mExact.Construct();
488
0
    if (!ValueToPrimitive<double, eDefault>(cx, temp.ref(), &(mExact.Value()))) {
489
0
      return false;
490
0
    } else if (!mozilla::IsFinite((mExact.Value()))) {
491
0
      ThrowErrorMessage(cx, MSG_NOT_FINITE, "'exact' member of ConstrainDoubleRange");
492
0
      return false;
493
0
    }
494
0
    mIsAnyMemberPresent = true;
495
0
  }
496
0
497
0
  if (!isNull) {
498
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->ideal_id, temp.ptr())) {
499
0
      return false;
500
0
    }
501
0
  }
502
0
  if (!isNull && !temp->isUndefined()) {
503
0
    mIdeal.Construct();
504
0
    if (!ValueToPrimitive<double, eDefault>(cx, temp.ref(), &(mIdeal.Value()))) {
505
0
      return false;
506
0
    } else if (!mozilla::IsFinite((mIdeal.Value()))) {
507
0
      ThrowErrorMessage(cx, MSG_NOT_FINITE, "'ideal' member of ConstrainDoubleRange");
508
0
      return false;
509
0
    }
510
0
    mIsAnyMemberPresent = true;
511
0
  }
512
0
513
0
  if (!isNull) {
514
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->max_id, temp.ptr())) {
515
0
      return false;
516
0
    }
517
0
  }
518
0
  if (!isNull && !temp->isUndefined()) {
519
0
    mMax.Construct();
520
0
    if (!ValueToPrimitive<double, eDefault>(cx, temp.ref(), &(mMax.Value()))) {
521
0
      return false;
522
0
    } else if (!mozilla::IsFinite((mMax.Value()))) {
523
0
      ThrowErrorMessage(cx, MSG_NOT_FINITE, "'max' member of ConstrainDoubleRange");
524
0
      return false;
525
0
    }
526
0
    mIsAnyMemberPresent = true;
527
0
  }
528
0
529
0
  if (!isNull) {
530
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->min_id, temp.ptr())) {
531
0
      return false;
532
0
    }
533
0
  }
534
0
  if (!isNull && !temp->isUndefined()) {
535
0
    mMin.Construct();
536
0
    if (!ValueToPrimitive<double, eDefault>(cx, temp.ref(), &(mMin.Value()))) {
537
0
      return false;
538
0
    } else if (!mozilla::IsFinite((mMin.Value()))) {
539
0
      ThrowErrorMessage(cx, MSG_NOT_FINITE, "'min' member of ConstrainDoubleRange");
540
0
      return false;
541
0
    }
542
0
    mIsAnyMemberPresent = true;
543
0
  }
544
0
  return true;
545
0
}
546
547
bool
548
ConstrainDoubleRange::Init(const nsAString& aJSON)
549
0
{
550
0
  AutoJSAPI jsapi;
551
0
  JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
552
0
  if (!cleanGlobal) {
553
0
    return false;
554
0
  }
555
0
  if (!jsapi.Init(cleanGlobal)) {
556
0
    return false;
557
0
  }
558
0
  JSContext* cx = jsapi.cx();
559
0
  JS::Rooted<JS::Value> json(cx);
560
0
  bool ok = ParseJSON(cx, aJSON, &json);
561
0
  NS_ENSURE_TRUE(ok, false);
562
0
  return Init(cx, json);
563
0
}
564
565
bool
566
ConstrainDoubleRange::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
567
0
{
568
0
  ConstrainDoubleRangeAtoms* atomsCache = GetAtomCache<ConstrainDoubleRangeAtoms>(cx);
569
0
  if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
570
0
    return false;
571
0
  }
572
0
573
0
  JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
574
0
  if (!obj) {
575
0
    return false;
576
0
  }
577
0
  rval.set(JS::ObjectValue(*obj));
578
0
579
0
  if (mExact.WasPassed()) {
580
0
    do {
581
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
582
0
      JS::Rooted<JS::Value> temp(cx);
583
0
      double const & currentValue = mExact.InternalValue();
584
0
      temp.set(JS_NumberValue(double(currentValue)));
585
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->exact_id, temp, JSPROP_ENUMERATE)) {
586
0
        return false;
587
0
      }
588
0
      break;
589
0
    } while(false);
590
0
  }
591
0
592
0
  if (mIdeal.WasPassed()) {
593
0
    do {
594
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
595
0
      JS::Rooted<JS::Value> temp(cx);
596
0
      double const & currentValue = mIdeal.InternalValue();
597
0
      temp.set(JS_NumberValue(double(currentValue)));
598
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->ideal_id, temp, JSPROP_ENUMERATE)) {
599
0
        return false;
600
0
      }
601
0
      break;
602
0
    } while(false);
603
0
  }
604
0
605
0
  if (mMax.WasPassed()) {
606
0
    do {
607
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
608
0
      JS::Rooted<JS::Value> temp(cx);
609
0
      double const & currentValue = mMax.InternalValue();
610
0
      temp.set(JS_NumberValue(double(currentValue)));
611
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->max_id, temp, JSPROP_ENUMERATE)) {
612
0
        return false;
613
0
      }
614
0
      break;
615
0
    } while(false);
616
0
  }
617
0
618
0
  if (mMin.WasPassed()) {
619
0
    do {
620
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
621
0
      JS::Rooted<JS::Value> temp(cx);
622
0
      double const & currentValue = mMin.InternalValue();
623
0
      temp.set(JS_NumberValue(double(currentValue)));
624
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->min_id, temp, JSPROP_ENUMERATE)) {
625
0
        return false;
626
0
      }
627
0
      break;
628
0
    } while(false);
629
0
  }
630
0
631
0
  return true;
632
0
}
633
634
bool
635
ConstrainDoubleRange::ToJSON(nsAString& aJSON) const
636
0
{
637
0
  AutoJSAPI jsapi;
638
0
  jsapi.Init();
639
0
  JSContext *cx = jsapi.cx();
640
0
  // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
641
0
  // because we'll only be creating objects, in ways that have no
642
0
  // side-effects, followed by a call to JS::ToJSONMaybeSafely,
643
0
  // which likewise guarantees no side-effects for the sorts of
644
0
  // things we will pass it.
645
0
  JSAutoRealm ar(cx, UnprivilegedJunkScopeOrWorkerGlobal());
646
0
  JS::Rooted<JS::Value> val(cx);
647
0
  if (!ToObjectInternal(cx, &val)) {
648
0
    return false;
649
0
  }
650
0
  JS::Rooted<JSObject*> obj(cx, &val.toObject());
651
0
  return StringifyToJSON(cx, obj, aJSON);
652
0
}
653
654
void
655
ConstrainDoubleRange::TraceDictionary(JSTracer* trc)
656
0
{
657
0
}
658
659
ConstrainDoubleRange&
660
ConstrainDoubleRange::operator=(const ConstrainDoubleRange& aOther)
661
0
{
662
0
  DictionaryBase::operator=(aOther);
663
0
  mExact.Reset();
664
0
  if (aOther.mExact.WasPassed()) {
665
0
    mExact.Construct(aOther.mExact.Value());
666
0
  }
667
0
  mIdeal.Reset();
668
0
  if (aOther.mIdeal.WasPassed()) {
669
0
    mIdeal.Construct(aOther.mIdeal.Value());
670
0
  }
671
0
  mMax.Reset();
672
0
  if (aOther.mMax.WasPassed()) {
673
0
    mMax.Construct(aOther.mMax.Value());
674
0
  }
675
0
  mMin.Reset();
676
0
  if (aOther.mMin.WasPassed()) {
677
0
    mMin.Construct(aOther.mMin.Value());
678
0
  }
679
0
  return *this;
680
0
}
681
682
namespace binding_detail {
683
} // namespace binding_detail
684
685
686
687
ConstrainLongRange::ConstrainLongRange()
688
0
{
689
0
  // Safe to pass a null context if we pass a null value
690
0
  Init(nullptr, JS::NullHandleValue);
691
0
}
692
693
694
695
bool
696
ConstrainLongRange::InitIds(JSContext* cx, ConstrainLongRangeAtoms* atomsCache)
697
0
{
698
0
  MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
699
0
700
0
  // Initialize these in reverse order so that any failure leaves the first one
701
0
  // uninitialized.
702
0
  if (!atomsCache->min_id.init(cx, "min") ||
703
0
      !atomsCache->max_id.init(cx, "max") ||
704
0
      !atomsCache->ideal_id.init(cx, "ideal") ||
705
0
      !atomsCache->exact_id.init(cx, "exact")) {
706
0
    return false;
707
0
  }
708
0
  return true;
709
0
}
710
711
bool
712
ConstrainLongRange::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
713
0
{
714
0
  // Passing a null JSContext is OK only if we're initing from null,
715
0
  // Since in that case we will not have to do any property gets
716
0
  // Also evaluate isNullOrUndefined in order to avoid false-positive
717
0
  // checkers by static analysis tools
718
0
  MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
719
0
  ConstrainLongRangeAtoms* atomsCache = nullptr;
720
0
  if (cx) {
721
0
    atomsCache = GetAtomCache<ConstrainLongRangeAtoms>(cx);
722
0
    if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
723
0
      return false;
724
0
    }
725
0
  }
726
0
727
0
  if (!IsConvertibleToDictionary(val)) {
728
0
    return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
729
0
  }
730
0
731
0
  bool isNull = val.isNullOrUndefined();
732
0
  // We only need these if !isNull, in which case we have |cx|.
733
0
  Maybe<JS::Rooted<JSObject *> > object;
734
0
  Maybe<JS::Rooted<JS::Value> > temp;
735
0
  if (!isNull) {
736
0
    MOZ_ASSERT(cx);
737
0
    object.emplace(cx, &val.toObject());
738
0
    temp.emplace(cx);
739
0
  }
740
0
  if (!isNull) {
741
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->exact_id, temp.ptr())) {
742
0
      return false;
743
0
    }
744
0
  }
745
0
  if (!isNull && !temp->isUndefined()) {
746
0
    mExact.Construct();
747
0
    if (!ValueToPrimitive<int32_t, eDefault>(cx, temp.ref(), &(mExact.Value()))) {
748
0
      return false;
749
0
    }
750
0
    mIsAnyMemberPresent = true;
751
0
  }
752
0
753
0
  if (!isNull) {
754
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->ideal_id, temp.ptr())) {
755
0
      return false;
756
0
    }
757
0
  }
758
0
  if (!isNull && !temp->isUndefined()) {
759
0
    mIdeal.Construct();
760
0
    if (!ValueToPrimitive<int32_t, eDefault>(cx, temp.ref(), &(mIdeal.Value()))) {
761
0
      return false;
762
0
    }
763
0
    mIsAnyMemberPresent = true;
764
0
  }
765
0
766
0
  if (!isNull) {
767
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->max_id, temp.ptr())) {
768
0
      return false;
769
0
    }
770
0
  }
771
0
  if (!isNull && !temp->isUndefined()) {
772
0
    mMax.Construct();
773
0
    if (!ValueToPrimitive<int32_t, eDefault>(cx, temp.ref(), &(mMax.Value()))) {
774
0
      return false;
775
0
    }
776
0
    mIsAnyMemberPresent = true;
777
0
  }
778
0
779
0
  if (!isNull) {
780
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->min_id, temp.ptr())) {
781
0
      return false;
782
0
    }
783
0
  }
784
0
  if (!isNull && !temp->isUndefined()) {
785
0
    mMin.Construct();
786
0
    if (!ValueToPrimitive<int32_t, eDefault>(cx, temp.ref(), &(mMin.Value()))) {
787
0
      return false;
788
0
    }
789
0
    mIsAnyMemberPresent = true;
790
0
  }
791
0
  return true;
792
0
}
793
794
bool
795
ConstrainLongRange::Init(const nsAString& aJSON)
796
0
{
797
0
  AutoJSAPI jsapi;
798
0
  JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
799
0
  if (!cleanGlobal) {
800
0
    return false;
801
0
  }
802
0
  if (!jsapi.Init(cleanGlobal)) {
803
0
    return false;
804
0
  }
805
0
  JSContext* cx = jsapi.cx();
806
0
  JS::Rooted<JS::Value> json(cx);
807
0
  bool ok = ParseJSON(cx, aJSON, &json);
808
0
  NS_ENSURE_TRUE(ok, false);
809
0
  return Init(cx, json);
810
0
}
811
812
bool
813
ConstrainLongRange::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
814
0
{
815
0
  ConstrainLongRangeAtoms* atomsCache = GetAtomCache<ConstrainLongRangeAtoms>(cx);
816
0
  if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
817
0
    return false;
818
0
  }
819
0
820
0
  JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
821
0
  if (!obj) {
822
0
    return false;
823
0
  }
824
0
  rval.set(JS::ObjectValue(*obj));
825
0
826
0
  if (mExact.WasPassed()) {
827
0
    do {
828
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
829
0
      JS::Rooted<JS::Value> temp(cx);
830
0
      int32_t const & currentValue = mExact.InternalValue();
831
0
      temp.setInt32(int32_t(currentValue));
832
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->exact_id, temp, JSPROP_ENUMERATE)) {
833
0
        return false;
834
0
      }
835
0
      break;
836
0
    } while(false);
837
0
  }
838
0
839
0
  if (mIdeal.WasPassed()) {
840
0
    do {
841
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
842
0
      JS::Rooted<JS::Value> temp(cx);
843
0
      int32_t const & currentValue = mIdeal.InternalValue();
844
0
      temp.setInt32(int32_t(currentValue));
845
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->ideal_id, temp, JSPROP_ENUMERATE)) {
846
0
        return false;
847
0
      }
848
0
      break;
849
0
    } while(false);
850
0
  }
851
0
852
0
  if (mMax.WasPassed()) {
853
0
    do {
854
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
855
0
      JS::Rooted<JS::Value> temp(cx);
856
0
      int32_t const & currentValue = mMax.InternalValue();
857
0
      temp.setInt32(int32_t(currentValue));
858
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->max_id, temp, JSPROP_ENUMERATE)) {
859
0
        return false;
860
0
      }
861
0
      break;
862
0
    } while(false);
863
0
  }
864
0
865
0
  if (mMin.WasPassed()) {
866
0
    do {
867
0
      // block for our 'break' successCode and scope for 'temp' and 'currentValue'
868
0
      JS::Rooted<JS::Value> temp(cx);
869
0
      int32_t const & currentValue = mMin.InternalValue();
870
0
      temp.setInt32(int32_t(currentValue));
871
0
      if (!JS_DefinePropertyById(cx, obj, atomsCache->min_id, temp, JSPROP_ENUMERATE)) {
872
0
        return false;
873
0
      }
874
0
      break;
875
0
    } while(false);
876
0
  }
877
0
878
0
  return true;
879
0
}
880
881
bool
882
ConstrainLongRange::ToJSON(nsAString& aJSON) const
883
0
{
884
0
  AutoJSAPI jsapi;
885
0
  jsapi.Init();
886
0
  JSContext *cx = jsapi.cx();
887
0
  // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
888
0
  // because we'll only be creating objects, in ways that have no
889
0
  // side-effects, followed by a call to JS::ToJSONMaybeSafely,
890
0
  // which likewise guarantees no side-effects for the sorts of
891
0
  // things we will pass it.
892
0
  JSAutoRealm ar(cx, UnprivilegedJunkScopeOrWorkerGlobal());
893
0
  JS::Rooted<JS::Value> val(cx);
894
0
  if (!ToObjectInternal(cx, &val)) {
895
0
    return false;
896
0
  }
897
0
  JS::Rooted<JSObject*> obj(cx, &val.toObject());
898
0
  return StringifyToJSON(cx, obj, aJSON);
899
0
}
900
901
void
902
ConstrainLongRange::TraceDictionary(JSTracer* trc)
903
0
{
904
0
}
905
906
ConstrainLongRange&
907
ConstrainLongRange::operator=(const ConstrainLongRange& aOther)
908
0
{
909
0
  DictionaryBase::operator=(aOther);
910
0
  mExact.Reset();
911
0
  if (aOther.mExact.WasPassed()) {
912
0
    mExact.Construct(aOther.mExact.Value());
913
0
  }
914
0
  mIdeal.Reset();
915
0
  if (aOther.mIdeal.WasPassed()) {
916
0
    mIdeal.Construct(aOther.mIdeal.Value());
917
0
  }
918
0
  mMax.Reset();
919
0
  if (aOther.mMax.WasPassed()) {
920
0
    mMax.Construct(aOther.mMax.Value());
921
0
  }
922
0
  mMin.Reset();
923
0
  if (aOther.mMin.WasPassed()) {
924
0
    mMin.Construct(aOther.mMin.Value());
925
0
  }
926
0
  return *this;
927
0
}
928
929
namespace binding_detail {
930
} // namespace binding_detail
931
932
933
} // namespace dom
934
} // namespace mozilla