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