Bug 1866894 - Update failing subtest for content-visibility-auto-resize.html. r=fredw
[gecko.git] / xpcom / ds / nsVariant.cpp
blobf8a40431c7ead277e95f113897dca3f874c95b0e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsVariant.h"
8 #include "prprf.h"
9 #include "prdtoa.h"
10 #include <math.h>
11 #include "nsCycleCollectionParticipant.h"
12 #include "xptinfo.h"
13 #include "nsReadableUtils.h"
14 #include "nsString.h"
15 #include "nsCRTGlue.h"
16 #include "mozilla/FloatingPoint.h"
17 #include "mozilla/IntegerPrintfMacros.h"
18 #include "mozilla/Printf.h"
20 /***************************************************************************/
21 // Helpers for static convert functions...
23 static nsresult String2Double(const char* aString, double* aResult) {
24 char* next;
25 double value = PR_strtod(aString, &next);
26 if (next == aString) {
27 return NS_ERROR_CANNOT_CONVERT_DATA;
29 *aResult = value;
30 return NS_OK;
33 static nsresult AString2Double(const nsAString& aString, double* aResult) {
34 char* pChars = ToNewCString(aString, mozilla::fallible);
35 if (!pChars) {
36 return NS_ERROR_OUT_OF_MEMORY;
38 nsresult rv = String2Double(pChars, aResult);
39 free(pChars);
40 return rv;
43 static nsresult AUTF8String2Double(const nsAUTF8String& aString,
44 double* aResult) {
45 return String2Double(PromiseFlatUTF8String(aString).get(), aResult);
48 static nsresult ACString2Double(const nsACString& aString, double* aResult) {
49 return String2Double(PromiseFlatCString(aString).get(), aResult);
52 // Fills aOutData with double, uint32_t, or int32_t.
53 // Returns NS_OK, an error code, or a non-NS_OK success code
54 nsresult nsDiscriminatedUnion::ToManageableNumber(
55 nsDiscriminatedUnion* aOutData) const {
56 nsresult rv;
58 switch (mType) {
59 // This group results in a int32_t...
61 #define CASE__NUMBER_INT32(type_, member_) \
62 case nsIDataType::type_: \
63 aOutData->u.mInt32Value = u.member_; \
64 aOutData->mType = nsIDataType::VTYPE_INT32; \
65 return NS_OK;
67 CASE__NUMBER_INT32(VTYPE_INT8, mInt8Value)
68 CASE__NUMBER_INT32(VTYPE_INT16, mInt16Value)
69 CASE__NUMBER_INT32(VTYPE_INT32, mInt32Value)
70 CASE__NUMBER_INT32(VTYPE_UINT8, mUint8Value)
71 CASE__NUMBER_INT32(VTYPE_UINT16, mUint16Value)
72 CASE__NUMBER_INT32(VTYPE_BOOL, mBoolValue)
73 CASE__NUMBER_INT32(VTYPE_CHAR, mCharValue)
74 CASE__NUMBER_INT32(VTYPE_WCHAR, mWCharValue)
76 #undef CASE__NUMBER_INT32
78 // This group results in a uint32_t...
80 case nsIDataType::VTYPE_UINT32:
81 aOutData->u.mUint32Value = u.mUint32Value;
82 aOutData->mType = nsIDataType::VTYPE_UINT32;
83 return NS_OK;
85 // This group results in a double...
87 case nsIDataType::VTYPE_INT64:
88 case nsIDataType::VTYPE_UINT64:
89 // XXX Need boundary checking here.
90 // We may need to return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA
91 aOutData->u.mDoubleValue = double(u.mInt64Value);
92 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
93 return NS_OK;
94 case nsIDataType::VTYPE_FLOAT:
95 aOutData->u.mDoubleValue = u.mFloatValue;
96 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
97 return NS_OK;
98 case nsIDataType::VTYPE_DOUBLE:
99 aOutData->u.mDoubleValue = u.mDoubleValue;
100 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
101 return NS_OK;
102 case nsIDataType::VTYPE_CHAR_STR:
103 case nsIDataType::VTYPE_STRING_SIZE_IS:
104 rv = String2Double(u.str.mStringValue, &aOutData->u.mDoubleValue);
105 if (NS_FAILED(rv)) {
106 return rv;
108 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
109 return NS_OK;
110 case nsIDataType::VTYPE_ASTRING:
111 rv = AString2Double(*u.mAStringValue, &aOutData->u.mDoubleValue);
112 if (NS_FAILED(rv)) {
113 return rv;
115 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
116 return NS_OK;
117 case nsIDataType::VTYPE_UTF8STRING:
118 rv = AUTF8String2Double(*u.mUTF8StringValue, &aOutData->u.mDoubleValue);
119 if (NS_FAILED(rv)) {
120 return rv;
122 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
123 return NS_OK;
124 case nsIDataType::VTYPE_CSTRING:
125 rv = ACString2Double(*u.mCStringValue, &aOutData->u.mDoubleValue);
126 if (NS_FAILED(rv)) {
127 return rv;
129 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
130 return NS_OK;
131 case nsIDataType::VTYPE_WCHAR_STR:
132 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
133 rv = AString2Double(nsDependentString(u.wstr.mWStringValue),
134 &aOutData->u.mDoubleValue);
135 if (NS_FAILED(rv)) {
136 return rv;
138 aOutData->mType = nsIDataType::VTYPE_DOUBLE;
139 return NS_OK;
141 // This group fails...
143 case nsIDataType::VTYPE_VOID:
144 case nsIDataType::VTYPE_ID:
145 case nsIDataType::VTYPE_INTERFACE:
146 case nsIDataType::VTYPE_INTERFACE_IS:
147 case nsIDataType::VTYPE_ARRAY:
148 case nsIDataType::VTYPE_EMPTY_ARRAY:
149 case nsIDataType::VTYPE_EMPTY:
150 default:
151 return NS_ERROR_CANNOT_CONVERT_DATA;
155 /***************************************************************************/
156 // Array helpers...
158 void nsDiscriminatedUnion::FreeArray() {
159 NS_ASSERTION(mType == nsIDataType::VTYPE_ARRAY, "bad FreeArray call");
160 NS_ASSERTION(u.array.mArrayValue, "bad array");
161 NS_ASSERTION(u.array.mArrayCount, "bad array count");
163 #define CASE__FREE_ARRAY_PTR(type_, ctype_) \
164 case nsIDataType::type_: { \
165 ctype_** p = (ctype_**)u.array.mArrayValue; \
166 for (uint32_t i = u.array.mArrayCount; i > 0; p++, i--) \
167 if (*p) free((char*)*p); \
168 break; \
171 #define CASE__FREE_ARRAY_IFACE(type_, ctype_) \
172 case nsIDataType::type_: { \
173 ctype_** p = (ctype_**)u.array.mArrayValue; \
174 for (uint32_t i = u.array.mArrayCount; i > 0; p++, i--) \
175 if (*p) (*p)->Release(); \
176 break; \
179 switch (u.array.mArrayType) {
180 case nsIDataType::VTYPE_INT8:
181 case nsIDataType::VTYPE_INT16:
182 case nsIDataType::VTYPE_INT32:
183 case nsIDataType::VTYPE_INT64:
184 case nsIDataType::VTYPE_UINT8:
185 case nsIDataType::VTYPE_UINT16:
186 case nsIDataType::VTYPE_UINT32:
187 case nsIDataType::VTYPE_UINT64:
188 case nsIDataType::VTYPE_FLOAT:
189 case nsIDataType::VTYPE_DOUBLE:
190 case nsIDataType::VTYPE_BOOL:
191 case nsIDataType::VTYPE_CHAR:
192 case nsIDataType::VTYPE_WCHAR:
193 break;
195 // XXX We ASSUME that "array of nsID" means "array of pointers to nsID".
196 CASE__FREE_ARRAY_PTR(VTYPE_ID, nsID)
197 CASE__FREE_ARRAY_PTR(VTYPE_CHAR_STR, char)
198 CASE__FREE_ARRAY_PTR(VTYPE_WCHAR_STR, char16_t)
199 CASE__FREE_ARRAY_IFACE(VTYPE_INTERFACE, nsISupports)
200 CASE__FREE_ARRAY_IFACE(VTYPE_INTERFACE_IS, nsISupports)
202 // The rest are illegal.
203 case nsIDataType::VTYPE_VOID:
204 case nsIDataType::VTYPE_ASTRING:
205 case nsIDataType::VTYPE_UTF8STRING:
206 case nsIDataType::VTYPE_CSTRING:
207 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
208 case nsIDataType::VTYPE_STRING_SIZE_IS:
209 case nsIDataType::VTYPE_ARRAY:
210 case nsIDataType::VTYPE_EMPTY_ARRAY:
211 case nsIDataType::VTYPE_EMPTY:
212 default:
213 NS_ERROR("bad type in array!");
214 break;
217 // Free the array memory.
218 free((char*)u.array.mArrayValue);
220 #undef CASE__FREE_ARRAY_PTR
221 #undef CASE__FREE_ARRAY_IFACE
224 static nsresult CloneArray(uint16_t aInType, const nsIID* aInIID,
225 uint32_t aInCount, void* aInValue,
226 uint16_t* aOutType, nsIID* aOutIID,
227 uint32_t* aOutCount, void** aOutValue) {
228 NS_ASSERTION(aInCount, "bad param");
229 NS_ASSERTION(aInValue, "bad param");
230 NS_ASSERTION(aOutType, "bad param");
231 NS_ASSERTION(aOutCount, "bad param");
232 NS_ASSERTION(aOutValue, "bad param");
234 uint32_t i;
236 // First we figure out the size of the elements for the new u.array.
238 size_t elementSize;
239 size_t allocSize;
241 switch (aInType) {
242 case nsIDataType::VTYPE_INT8:
243 elementSize = sizeof(int8_t);
244 break;
245 case nsIDataType::VTYPE_INT16:
246 elementSize = sizeof(int16_t);
247 break;
248 case nsIDataType::VTYPE_INT32:
249 elementSize = sizeof(int32_t);
250 break;
251 case nsIDataType::VTYPE_INT64:
252 elementSize = sizeof(int64_t);
253 break;
254 case nsIDataType::VTYPE_UINT8:
255 elementSize = sizeof(uint8_t);
256 break;
257 case nsIDataType::VTYPE_UINT16:
258 elementSize = sizeof(uint16_t);
259 break;
260 case nsIDataType::VTYPE_UINT32:
261 elementSize = sizeof(uint32_t);
262 break;
263 case nsIDataType::VTYPE_UINT64:
264 elementSize = sizeof(uint64_t);
265 break;
266 case nsIDataType::VTYPE_FLOAT:
267 elementSize = sizeof(float);
268 break;
269 case nsIDataType::VTYPE_DOUBLE:
270 elementSize = sizeof(double);
271 break;
272 case nsIDataType::VTYPE_BOOL:
273 elementSize = sizeof(bool);
274 break;
275 case nsIDataType::VTYPE_CHAR:
276 elementSize = sizeof(char);
277 break;
278 case nsIDataType::VTYPE_WCHAR:
279 elementSize = sizeof(char16_t);
280 break;
282 // XXX We ASSUME that "array of nsID" means "array of pointers to nsID".
283 case nsIDataType::VTYPE_ID:
284 case nsIDataType::VTYPE_CHAR_STR:
285 case nsIDataType::VTYPE_WCHAR_STR:
286 case nsIDataType::VTYPE_INTERFACE:
287 case nsIDataType::VTYPE_INTERFACE_IS:
288 elementSize = sizeof(void*);
289 break;
291 // The rest are illegal.
292 case nsIDataType::VTYPE_ASTRING:
293 case nsIDataType::VTYPE_UTF8STRING:
294 case nsIDataType::VTYPE_CSTRING:
295 case nsIDataType::VTYPE_STRING_SIZE_IS:
296 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
297 case nsIDataType::VTYPE_VOID:
298 case nsIDataType::VTYPE_ARRAY:
299 case nsIDataType::VTYPE_EMPTY_ARRAY:
300 case nsIDataType::VTYPE_EMPTY:
301 default:
302 NS_ERROR("bad type in array!");
303 return NS_ERROR_CANNOT_CONVERT_DATA;
306 // Alloc the u.array.
308 allocSize = aInCount * elementSize;
309 *aOutValue = moz_xmalloc(allocSize);
311 // Clone the elements.
313 switch (aInType) {
314 case nsIDataType::VTYPE_INT8:
315 case nsIDataType::VTYPE_INT16:
316 case nsIDataType::VTYPE_INT32:
317 case nsIDataType::VTYPE_INT64:
318 case nsIDataType::VTYPE_UINT8:
319 case nsIDataType::VTYPE_UINT16:
320 case nsIDataType::VTYPE_UINT32:
321 case nsIDataType::VTYPE_UINT64:
322 case nsIDataType::VTYPE_FLOAT:
323 case nsIDataType::VTYPE_DOUBLE:
324 case nsIDataType::VTYPE_BOOL:
325 case nsIDataType::VTYPE_CHAR:
326 case nsIDataType::VTYPE_WCHAR:
327 memcpy(*aOutValue, aInValue, allocSize);
328 break;
330 case nsIDataType::VTYPE_INTERFACE_IS:
331 if (aOutIID) {
332 *aOutIID = *aInIID;
334 [[fallthrough]];
336 case nsIDataType::VTYPE_INTERFACE: {
337 memcpy(*aOutValue, aInValue, allocSize);
339 nsISupports** p = (nsISupports**)*aOutValue;
340 for (i = aInCount; i > 0; ++p, --i)
341 if (*p) {
342 (*p)->AddRef();
344 break;
347 // XXX We ASSUME that "array of nsID" means "array of pointers to nsID".
348 case nsIDataType::VTYPE_ID: {
349 nsID** inp = (nsID**)aInValue;
350 nsID** outp = (nsID**)*aOutValue;
351 for (i = aInCount; i > 0; --i) {
352 nsID* idp = *(inp++);
353 if (idp) {
354 *(outp++) = idp->Clone();
355 } else {
356 *(outp++) = nullptr;
359 break;
362 case nsIDataType::VTYPE_CHAR_STR: {
363 char** inp = (char**)aInValue;
364 char** outp = (char**)*aOutValue;
365 for (i = aInCount; i > 0; i--) {
366 char* str = *(inp++);
367 if (str) {
368 *(outp++) = moz_xstrdup(str);
369 } else {
370 *(outp++) = nullptr;
373 break;
376 case nsIDataType::VTYPE_WCHAR_STR: {
377 char16_t** inp = (char16_t**)aInValue;
378 char16_t** outp = (char16_t**)*aOutValue;
379 for (i = aInCount; i > 0; i--) {
380 char16_t* str = *(inp++);
381 if (str) {
382 *(outp++) = NS_xstrdup(str);
383 } else {
384 *(outp++) = nullptr;
387 break;
390 // The rest are illegal.
391 case nsIDataType::VTYPE_VOID:
392 case nsIDataType::VTYPE_ARRAY:
393 case nsIDataType::VTYPE_EMPTY_ARRAY:
394 case nsIDataType::VTYPE_EMPTY:
395 case nsIDataType::VTYPE_ASTRING:
396 case nsIDataType::VTYPE_UTF8STRING:
397 case nsIDataType::VTYPE_CSTRING:
398 case nsIDataType::VTYPE_STRING_SIZE_IS:
399 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
400 default:
401 NS_ERROR("bad type in array!");
402 return NS_ERROR_CANNOT_CONVERT_DATA;
405 *aOutType = aInType;
406 *aOutCount = aInCount;
407 return NS_OK;
410 /***************************************************************************/
412 #define TRIVIAL_DATA_CONVERTER(type_, member_, retval_) \
413 if (mType == nsIDataType::type_) { \
414 *retval_ = u.member_; \
415 return NS_OK; \
418 #define NUMERIC_CONVERSION_METHOD_BEGIN(type_, Ctype_, name_) \
419 nsresult nsDiscriminatedUnion::ConvertTo##name_(Ctype_* aResult) const { \
420 TRIVIAL_DATA_CONVERTER(type_, m##name_##Value, aResult) \
421 nsDiscriminatedUnion tempData; \
422 nsresult rv = ToManageableNumber(&tempData); \
423 /* */ \
424 /* NOTE: rv may indicate a success code that we want to preserve */ \
425 /* For the final return. So all the return cases below should return */ \
426 /* this rv when indicating success. */ \
427 /* */ \
428 if (NS_FAILED(rv)) return rv; \
429 switch (tempData.mType) {
430 #define CASE__NUMERIC_CONVERSION_INT32_JUST_CAST(Ctype_) \
431 case nsIDataType::VTYPE_INT32: \
432 *aResult = (Ctype_)tempData.u.mInt32Value; \
433 return rv;
435 #define CASE__NUMERIC_CONVERSION_INT32_MIN_MAX(Ctype_, min_, max_) \
436 case nsIDataType::VTYPE_INT32: { \
437 int32_t value = tempData.u.mInt32Value; \
438 if (value < (min_) || value > (max_)) { \
439 return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA; \
441 *aResult = (Ctype_)value; \
442 return rv; \
445 #define CASE__NUMERIC_CONVERSION_UINT32_JUST_CAST(Ctype_) \
446 case nsIDataType::VTYPE_UINT32: \
447 *aResult = (Ctype_)tempData.u.mUint32Value; \
448 return rv;
450 #define CASE__NUMERIC_CONVERSION_UINT32_MAX(Ctype_, max_) \
451 case nsIDataType::VTYPE_UINT32: { \
452 uint32_t value = tempData.u.mUint32Value; \
453 if (value > (max_)) return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA; \
454 *aResult = (Ctype_)value; \
455 return rv; \
458 #define CASE__NUMERIC_CONVERSION_DOUBLE_JUST_CAST(Ctype_) \
459 case nsIDataType::VTYPE_DOUBLE: \
460 *aResult = (Ctype_)tempData.u.mDoubleValue; \
461 return rv;
463 #define CASE__NUMERIC_CONVERSION_DOUBLE_MIN_MAX_INT(Ctype_, min_, max_) \
464 case nsIDataType::VTYPE_DOUBLE: { \
465 double value = tempData.u.mDoubleValue; \
466 if (std::isnan(value) || value < (min_) || value > (max_)) { \
467 return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA; \
469 *aResult = (Ctype_)value; \
470 return (0.0 == fmod(value, 1.0)) ? rv \
471 : NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA; \
474 #define CASES__NUMERIC_CONVERSION_NORMAL(Ctype_, min_, max_) \
475 CASE__NUMERIC_CONVERSION_INT32_MIN_MAX(Ctype_, min_, max_) \
476 CASE__NUMERIC_CONVERSION_UINT32_MAX(Ctype_, max_) \
477 CASE__NUMERIC_CONVERSION_DOUBLE_MIN_MAX_INT(Ctype_, min_, max_)
479 #define NUMERIC_CONVERSION_METHOD_END \
480 default: \
481 NS_ERROR("bad type returned from ToManageableNumber"); \
482 return NS_ERROR_CANNOT_CONVERT_DATA; \
486 #define NUMERIC_CONVERSION_METHOD_NORMAL(type_, Ctype_, name_, min_, max_) \
487 NUMERIC_CONVERSION_METHOD_BEGIN(type_, Ctype_, name_) \
488 CASES__NUMERIC_CONVERSION_NORMAL(Ctype_, min_, max_) \
489 NUMERIC_CONVERSION_METHOD_END
491 /***************************************************************************/
492 // These expand into full public methods...
494 NUMERIC_CONVERSION_METHOD_NORMAL(VTYPE_INT8, uint8_t, Int8, (-127 - 1), 127)
495 NUMERIC_CONVERSION_METHOD_NORMAL(VTYPE_INT16, int16_t, Int16, (-32767 - 1),
496 32767)
498 NUMERIC_CONVERSION_METHOD_BEGIN(VTYPE_INT32, int32_t, Int32)
499 CASE__NUMERIC_CONVERSION_INT32_JUST_CAST(int32_t)
500 CASE__NUMERIC_CONVERSION_UINT32_MAX(int32_t, 2147483647)
501 CASE__NUMERIC_CONVERSION_DOUBLE_MIN_MAX_INT(int32_t, (-2147483647 - 1),
502 2147483647)
503 NUMERIC_CONVERSION_METHOD_END
505 NUMERIC_CONVERSION_METHOD_NORMAL(VTYPE_UINT8, uint8_t, Uint8, 0, 255)
506 NUMERIC_CONVERSION_METHOD_NORMAL(VTYPE_UINT16, uint16_t, Uint16, 0, 65535)
508 NUMERIC_CONVERSION_METHOD_BEGIN(VTYPE_UINT32, uint32_t, Uint32)
509 CASE__NUMERIC_CONVERSION_INT32_MIN_MAX(uint32_t, 0, 2147483647)
510 CASE__NUMERIC_CONVERSION_UINT32_JUST_CAST(uint32_t)
511 CASE__NUMERIC_CONVERSION_DOUBLE_MIN_MAX_INT(uint32_t, 0, 4294967295U)
512 NUMERIC_CONVERSION_METHOD_END
514 // XXX toFloat conversions need to be fixed!
515 NUMERIC_CONVERSION_METHOD_BEGIN(VTYPE_FLOAT, float, Float)
516 CASE__NUMERIC_CONVERSION_INT32_JUST_CAST(float)
517 CASE__NUMERIC_CONVERSION_UINT32_JUST_CAST(float)
518 CASE__NUMERIC_CONVERSION_DOUBLE_JUST_CAST(float)
519 NUMERIC_CONVERSION_METHOD_END
521 NUMERIC_CONVERSION_METHOD_BEGIN(VTYPE_DOUBLE, double, Double)
522 CASE__NUMERIC_CONVERSION_INT32_JUST_CAST(double)
523 CASE__NUMERIC_CONVERSION_UINT32_JUST_CAST(double)
524 CASE__NUMERIC_CONVERSION_DOUBLE_JUST_CAST(double)
525 NUMERIC_CONVERSION_METHOD_END
527 // XXX toChar conversions need to be fixed!
528 NUMERIC_CONVERSION_METHOD_NORMAL(VTYPE_CHAR, char, Char, CHAR_MIN, CHAR_MAX)
530 // XXX toWChar conversions need to be fixed!
531 NUMERIC_CONVERSION_METHOD_NORMAL(VTYPE_WCHAR, char16_t, WChar, 0,
532 std::numeric_limits<char16_t>::max())
534 #undef NUMERIC_CONVERSION_METHOD_BEGIN
535 #undef CASE__NUMERIC_CONVERSION_INT32_JUST_CAST
536 #undef CASE__NUMERIC_CONVERSION_INT32_MIN_MAX
537 #undef CASE__NUMERIC_CONVERSION_UINT32_JUST_CAST
538 #undef CASE__NUMERIC_CONVERSION_UINT32_MIN_MAX
539 #undef CASE__NUMERIC_CONVERSION_DOUBLE_JUST_CAST
540 #undef CASE__NUMERIC_CONVERSION_DOUBLE_MIN_MAX
541 #undef CASE__NUMERIC_CONVERSION_DOUBLE_MIN_MAX_INT
542 #undef CASES__NUMERIC_CONVERSION_NORMAL
543 #undef NUMERIC_CONVERSION_METHOD_END
544 #undef NUMERIC_CONVERSION_METHOD_NORMAL
546 /***************************************************************************/
548 // Just leverage a numeric converter for bool (but restrict the values).
549 // XXX Is this really what we want to do?
551 nsresult nsDiscriminatedUnion::ConvertToBool(bool* aResult) const {
552 TRIVIAL_DATA_CONVERTER(VTYPE_BOOL, mBoolValue, aResult)
554 double val;
555 nsresult rv = ConvertToDouble(&val);
556 if (NS_FAILED(rv)) {
557 return rv;
559 // NaN is falsy in JS, so we might as well make it false here.
560 if (std::isnan(val)) {
561 *aResult = false;
562 } else {
563 *aResult = 0.0 != val;
565 return rv;
568 /***************************************************************************/
570 nsresult nsDiscriminatedUnion::ConvertToInt64(int64_t* aResult) const {
571 TRIVIAL_DATA_CONVERTER(VTYPE_INT64, mInt64Value, aResult)
572 TRIVIAL_DATA_CONVERTER(VTYPE_UINT64, mUint64Value, aResult)
574 nsDiscriminatedUnion tempData;
575 nsresult rv = ToManageableNumber(&tempData);
576 if (NS_FAILED(rv)) {
577 return rv;
579 switch (tempData.mType) {
580 case nsIDataType::VTYPE_INT32:
581 *aResult = tempData.u.mInt32Value;
582 return rv;
583 case nsIDataType::VTYPE_UINT32:
584 *aResult = tempData.u.mUint32Value;
585 return rv;
586 case nsIDataType::VTYPE_DOUBLE: {
587 double value = tempData.u.mDoubleValue;
588 if (std::isnan(value)) {
589 return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
591 // XXX should check for data loss here!
592 *aResult = value;
593 return rv;
595 default:
596 NS_ERROR("bad type returned from ToManageableNumber");
597 return NS_ERROR_CANNOT_CONVERT_DATA;
601 nsresult nsDiscriminatedUnion::ConvertToUint64(uint64_t* aResult) const {
602 return ConvertToInt64((int64_t*)aResult);
605 /***************************************************************************/
607 bool nsDiscriminatedUnion::String2ID(nsID* aPid) const {
608 nsAutoString tempString;
609 nsAString* pString;
611 switch (mType) {
612 case nsIDataType::VTYPE_CHAR_STR:
613 case nsIDataType::VTYPE_STRING_SIZE_IS:
614 return aPid->Parse(u.str.mStringValue);
615 case nsIDataType::VTYPE_CSTRING:
616 return aPid->Parse(PromiseFlatCString(*u.mCStringValue).get());
617 case nsIDataType::VTYPE_UTF8STRING:
618 return aPid->Parse(PromiseFlatUTF8String(*u.mUTF8StringValue).get());
619 case nsIDataType::VTYPE_ASTRING:
620 pString = u.mAStringValue;
621 break;
622 case nsIDataType::VTYPE_WCHAR_STR:
623 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
624 tempString.Assign(u.wstr.mWStringValue);
625 pString = &tempString;
626 break;
627 default:
628 NS_ERROR("bad type in call to String2ID");
629 return false;
632 char* pChars = ToNewCString(*pString, mozilla::fallible);
633 if (!pChars) {
634 return false;
636 bool result = aPid->Parse(pChars);
637 free(pChars);
638 return result;
641 nsresult nsDiscriminatedUnion::ConvertToID(nsID* aResult) const {
642 nsID id;
644 switch (mType) {
645 case nsIDataType::VTYPE_ID:
646 *aResult = u.mIDValue;
647 return NS_OK;
648 case nsIDataType::VTYPE_INTERFACE:
649 *aResult = NS_GET_IID(nsISupports);
650 return NS_OK;
651 case nsIDataType::VTYPE_INTERFACE_IS:
652 *aResult = u.iface.mInterfaceID;
653 return NS_OK;
654 case nsIDataType::VTYPE_ASTRING:
655 case nsIDataType::VTYPE_UTF8STRING:
656 case nsIDataType::VTYPE_CSTRING:
657 case nsIDataType::VTYPE_CHAR_STR:
658 case nsIDataType::VTYPE_WCHAR_STR:
659 case nsIDataType::VTYPE_STRING_SIZE_IS:
660 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
661 if (!String2ID(&id)) {
662 return NS_ERROR_CANNOT_CONVERT_DATA;
664 *aResult = id;
665 return NS_OK;
666 default:
667 return NS_ERROR_CANNOT_CONVERT_DATA;
671 /***************************************************************************/
673 nsresult nsDiscriminatedUnion::ToString(nsACString& aOutString) const {
674 mozilla::SmprintfPointer pptr;
676 switch (mType) {
677 // all the stuff we don't handle...
678 case nsIDataType::VTYPE_ASTRING:
679 case nsIDataType::VTYPE_UTF8STRING:
680 case nsIDataType::VTYPE_CSTRING:
681 case nsIDataType::VTYPE_CHAR_STR:
682 case nsIDataType::VTYPE_WCHAR_STR:
683 case nsIDataType::VTYPE_STRING_SIZE_IS:
684 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
685 case nsIDataType::VTYPE_WCHAR:
686 NS_ERROR("ToString being called for a string type - screwy logic!");
687 [[fallthrough]];
689 // XXX We might want stringified versions of these... ???
691 case nsIDataType::VTYPE_VOID:
692 case nsIDataType::VTYPE_EMPTY:
693 aOutString.SetIsVoid(true);
694 return NS_OK;
696 case nsIDataType::VTYPE_EMPTY_ARRAY:
697 case nsIDataType::VTYPE_ARRAY:
698 case nsIDataType::VTYPE_INTERFACE:
699 case nsIDataType::VTYPE_INTERFACE_IS:
700 default:
701 return NS_ERROR_CANNOT_CONVERT_DATA;
703 // nsID has its own text formatter.
705 case nsIDataType::VTYPE_ID: {
706 aOutString.Assign(u.mIDValue.ToString().get());
707 return NS_OK;
710 // Can't use Smprintf for floats, since it's locale-dependent
711 #define CASE__APPENDFLOAT_NUMBER(type_, member_) \
712 case nsIDataType::type_: { \
713 nsAutoCString str; \
714 str.AppendFloat(u.member_); \
715 aOutString.Assign(str); \
716 return NS_OK; \
719 CASE__APPENDFLOAT_NUMBER(VTYPE_FLOAT, mFloatValue)
720 CASE__APPENDFLOAT_NUMBER(VTYPE_DOUBLE, mDoubleValue)
722 #undef CASE__APPENDFLOAT_NUMBER
724 // the rest can be Smprintf'd and use common code.
726 #define CASE__SMPRINTF_NUMBER(type_, format_, cast_, member_) \
727 case nsIDataType::type_: \
728 static_assert(sizeof(cast_) >= sizeof(u.member_), \
729 "size of type should be at least as big as member"); \
730 pptr = mozilla::Smprintf(format_, (cast_)u.member_); \
731 break;
733 CASE__SMPRINTF_NUMBER(VTYPE_INT8, "%d", int, mInt8Value)
734 CASE__SMPRINTF_NUMBER(VTYPE_INT16, "%d", int, mInt16Value)
735 CASE__SMPRINTF_NUMBER(VTYPE_INT32, "%d", int, mInt32Value)
736 CASE__SMPRINTF_NUMBER(VTYPE_INT64, "%" PRId64, int64_t, mInt64Value)
738 CASE__SMPRINTF_NUMBER(VTYPE_UINT8, "%u", unsigned, mUint8Value)
739 CASE__SMPRINTF_NUMBER(VTYPE_UINT16, "%u", unsigned, mUint16Value)
740 CASE__SMPRINTF_NUMBER(VTYPE_UINT32, "%u", unsigned, mUint32Value)
741 CASE__SMPRINTF_NUMBER(VTYPE_UINT64, "%" PRIu64, int64_t, mUint64Value)
743 // XXX Would we rather print "true" / "false" ?
744 CASE__SMPRINTF_NUMBER(VTYPE_BOOL, "%d", int, mBoolValue)
746 CASE__SMPRINTF_NUMBER(VTYPE_CHAR, "%c", char, mCharValue)
748 #undef CASE__SMPRINTF_NUMBER
751 if (!pptr) {
752 return NS_ERROR_OUT_OF_MEMORY;
754 aOutString.Assign(pptr.get());
755 return NS_OK;
758 nsresult nsDiscriminatedUnion::ConvertToAString(nsAString& aResult) const {
759 switch (mType) {
760 case nsIDataType::VTYPE_ASTRING:
761 aResult.Assign(*u.mAStringValue);
762 return NS_OK;
763 case nsIDataType::VTYPE_CSTRING:
764 CopyASCIItoUTF16(*u.mCStringValue, aResult);
765 return NS_OK;
766 case nsIDataType::VTYPE_UTF8STRING:
767 CopyUTF8toUTF16(*u.mUTF8StringValue, aResult);
768 return NS_OK;
769 case nsIDataType::VTYPE_CHAR_STR:
770 CopyASCIItoUTF16(mozilla::MakeStringSpan(u.str.mStringValue), aResult);
771 return NS_OK;
772 case nsIDataType::VTYPE_WCHAR_STR:
773 aResult.Assign(u.wstr.mWStringValue);
774 return NS_OK;
775 case nsIDataType::VTYPE_STRING_SIZE_IS:
776 CopyASCIItoUTF16(
777 nsDependentCString(u.str.mStringValue, u.str.mStringLength), aResult);
778 return NS_OK;
779 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
780 aResult.Assign(u.wstr.mWStringValue, u.wstr.mWStringLength);
781 return NS_OK;
782 case nsIDataType::VTYPE_WCHAR:
783 aResult.Assign(u.mWCharValue);
784 return NS_OK;
785 default: {
786 nsAutoCString tempCString;
787 nsresult rv = ToString(tempCString);
788 if (NS_FAILED(rv)) {
789 return rv;
791 CopyASCIItoUTF16(tempCString, aResult);
792 return NS_OK;
797 nsresult nsDiscriminatedUnion::ConvertToACString(nsACString& aResult) const {
798 switch (mType) {
799 case nsIDataType::VTYPE_ASTRING:
800 LossyCopyUTF16toASCII(*u.mAStringValue, aResult);
801 return NS_OK;
802 case nsIDataType::VTYPE_CSTRING:
803 aResult.Assign(*u.mCStringValue);
804 return NS_OK;
805 case nsIDataType::VTYPE_UTF8STRING:
806 // XXX This is an extra copy that should be avoided
807 // once Jag lands support for UTF8String and associated
808 // conversion methods.
809 LossyCopyUTF16toASCII(NS_ConvertUTF8toUTF16(*u.mUTF8StringValue),
810 aResult);
811 return NS_OK;
812 case nsIDataType::VTYPE_CHAR_STR:
813 aResult.Assign(*u.str.mStringValue);
814 return NS_OK;
815 case nsIDataType::VTYPE_WCHAR_STR:
816 LossyCopyUTF16toASCII(nsDependentString(u.wstr.mWStringValue), aResult);
817 return NS_OK;
818 case nsIDataType::VTYPE_STRING_SIZE_IS:
819 aResult.Assign(u.str.mStringValue, u.str.mStringLength);
820 return NS_OK;
821 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
822 LossyCopyUTF16toASCII(
823 nsDependentString(u.wstr.mWStringValue, u.wstr.mWStringLength),
824 aResult);
825 return NS_OK;
826 case nsIDataType::VTYPE_WCHAR: {
827 const char16_t* str = &u.mWCharValue;
828 LossyCopyUTF16toASCII(Substring(str, 1), aResult);
829 return NS_OK;
831 default:
832 return ToString(aResult);
836 nsresult nsDiscriminatedUnion::ConvertToAUTF8String(
837 nsAUTF8String& aResult) const {
838 switch (mType) {
839 case nsIDataType::VTYPE_ASTRING:
840 CopyUTF16toUTF8(*u.mAStringValue, aResult);
841 return NS_OK;
842 case nsIDataType::VTYPE_CSTRING:
843 // XXX Extra copy, can be removed if we're sure CSTRING can
844 // only contain ASCII.
845 CopyUTF16toUTF8(NS_ConvertASCIItoUTF16(*u.mCStringValue), aResult);
846 return NS_OK;
847 case nsIDataType::VTYPE_UTF8STRING:
848 aResult.Assign(*u.mUTF8StringValue);
849 return NS_OK;
850 case nsIDataType::VTYPE_CHAR_STR:
851 // XXX Extra copy, can be removed if we're sure CHAR_STR can
852 // only contain ASCII.
853 CopyUTF16toUTF8(NS_ConvertASCIItoUTF16(u.str.mStringValue), aResult);
854 return NS_OK;
855 case nsIDataType::VTYPE_WCHAR_STR:
856 CopyUTF16toUTF8(mozilla::MakeStringSpan(u.wstr.mWStringValue), aResult);
857 return NS_OK;
858 case nsIDataType::VTYPE_STRING_SIZE_IS:
859 // XXX Extra copy, can be removed if we're sure CHAR_STR can
860 // only contain ASCII.
861 CopyUTF16toUTF8(NS_ConvertASCIItoUTF16(nsDependentCString(
862 u.str.mStringValue, u.str.mStringLength)),
863 aResult);
864 return NS_OK;
865 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
866 CopyUTF16toUTF8(
867 nsDependentString(u.wstr.mWStringValue, u.wstr.mWStringLength),
868 aResult);
869 return NS_OK;
870 case nsIDataType::VTYPE_WCHAR: {
871 const char16_t* str = &u.mWCharValue;
872 CopyUTF16toUTF8(Substring(str, 1), aResult);
873 return NS_OK;
875 default: {
876 nsAutoCString tempCString;
877 nsresult rv = ToString(tempCString);
878 if (NS_FAILED(rv)) {
879 return rv;
881 // XXX Extra copy, can be removed if we're sure tempCString can
882 // only contain ASCII.
883 CopyUTF16toUTF8(NS_ConvertASCIItoUTF16(tempCString), aResult);
884 return NS_OK;
889 nsresult nsDiscriminatedUnion::ConvertToString(char** aResult) const {
890 uint32_t ignored;
891 return ConvertToStringWithSize(&ignored, aResult);
894 nsresult nsDiscriminatedUnion::ConvertToWString(char16_t** aResult) const {
895 uint32_t ignored;
896 return ConvertToWStringWithSize(&ignored, aResult);
899 nsresult nsDiscriminatedUnion::ConvertToStringWithSize(uint32_t* aSize,
900 char** aStr) const {
901 nsAutoString tempString;
902 nsAutoCString tempCString;
903 nsresult rv;
905 switch (mType) {
906 case nsIDataType::VTYPE_ASTRING:
907 *aSize = u.mAStringValue->Length();
908 *aStr = ToNewCString(*u.mAStringValue);
909 break;
910 case nsIDataType::VTYPE_CSTRING:
911 *aSize = u.mCStringValue->Length();
912 *aStr = ToNewCString(*u.mCStringValue);
913 break;
914 case nsIDataType::VTYPE_UTF8STRING: {
915 // XXX This is doing 1 extra copy. Need to fix this
916 // when Jag lands UTF8String
917 // we want:
918 // *aSize = *mUTF8StringValue->Length();
919 // *aStr = ToNewCString(*mUTF8StringValue);
920 // But this will have to do for now.
921 const NS_ConvertUTF8toUTF16 tempString16(*u.mUTF8StringValue);
922 *aSize = tempString16.Length();
923 *aStr = ToNewCString(tempString16);
924 break;
926 case nsIDataType::VTYPE_CHAR_STR: {
927 nsDependentCString cString(u.str.mStringValue);
928 *aSize = cString.Length();
929 *aStr = ToNewCString(cString);
930 break;
932 case nsIDataType::VTYPE_WCHAR_STR: {
933 nsDependentString string(u.wstr.mWStringValue);
934 *aSize = string.Length();
935 *aStr = ToNewCString(string);
936 break;
938 case nsIDataType::VTYPE_STRING_SIZE_IS: {
939 nsDependentCString cString(u.str.mStringValue, u.str.mStringLength);
940 *aSize = cString.Length();
941 *aStr = ToNewCString(cString);
942 break;
944 case nsIDataType::VTYPE_WSTRING_SIZE_IS: {
945 nsDependentString string(u.wstr.mWStringValue, u.wstr.mWStringLength);
946 *aSize = string.Length();
947 *aStr = ToNewCString(string);
948 break;
950 case nsIDataType::VTYPE_WCHAR:
951 tempString.Assign(u.mWCharValue);
952 *aSize = tempString.Length();
953 *aStr = ToNewCString(tempString);
954 break;
955 default:
956 rv = ToString(tempCString);
957 if (NS_FAILED(rv)) {
958 return rv;
960 *aSize = tempCString.Length();
961 *aStr = ToNewCString(tempCString);
962 break;
965 return *aStr ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
967 nsresult nsDiscriminatedUnion::ConvertToWStringWithSize(uint32_t* aSize,
968 char16_t** aStr) const {
969 nsAutoString tempString;
970 nsAutoCString tempCString;
971 nsresult rv;
973 switch (mType) {
974 case nsIDataType::VTYPE_ASTRING:
975 *aSize = u.mAStringValue->Length();
976 *aStr = ToNewUnicode(*u.mAStringValue);
977 break;
978 case nsIDataType::VTYPE_CSTRING:
979 *aSize = u.mCStringValue->Length();
980 *aStr = ToNewUnicode(*u.mCStringValue);
981 break;
982 case nsIDataType::VTYPE_UTF8STRING: {
983 *aStr = UTF8ToNewUnicode(*u.mUTF8StringValue, aSize);
984 break;
986 case nsIDataType::VTYPE_CHAR_STR: {
987 nsDependentCString cString(u.str.mStringValue);
988 *aSize = cString.Length();
989 *aStr = ToNewUnicode(cString);
990 break;
992 case nsIDataType::VTYPE_WCHAR_STR: {
993 nsDependentString string(u.wstr.mWStringValue);
994 *aSize = string.Length();
995 *aStr = ToNewUnicode(string);
996 break;
998 case nsIDataType::VTYPE_STRING_SIZE_IS: {
999 nsDependentCString cString(u.str.mStringValue, u.str.mStringLength);
1000 *aSize = cString.Length();
1001 *aStr = ToNewUnicode(cString);
1002 break;
1004 case nsIDataType::VTYPE_WSTRING_SIZE_IS: {
1005 nsDependentString string(u.wstr.mWStringValue, u.wstr.mWStringLength);
1006 *aSize = string.Length();
1007 *aStr = ToNewUnicode(string);
1008 break;
1010 case nsIDataType::VTYPE_WCHAR:
1011 tempString.Assign(u.mWCharValue);
1012 *aSize = tempString.Length();
1013 *aStr = ToNewUnicode(tempString);
1014 break;
1015 default:
1016 rv = ToString(tempCString);
1017 if (NS_FAILED(rv)) {
1018 return rv;
1020 *aSize = tempCString.Length();
1021 *aStr = ToNewUnicode(tempCString);
1022 break;
1025 return *aStr ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
1028 nsresult nsDiscriminatedUnion::ConvertToISupports(nsISupports** aResult) const {
1029 switch (mType) {
1030 case nsIDataType::VTYPE_INTERFACE:
1031 case nsIDataType::VTYPE_INTERFACE_IS:
1032 if (u.iface.mInterfaceValue) {
1033 return u.iface.mInterfaceValue->QueryInterface(NS_GET_IID(nsISupports),
1034 (void**)aResult);
1035 } else {
1036 *aResult = nullptr;
1037 return NS_OK;
1039 default:
1040 return NS_ERROR_CANNOT_CONVERT_DATA;
1044 nsresult nsDiscriminatedUnion::ConvertToInterface(nsIID** aIID,
1045 void** aInterface) const {
1046 const nsIID* piid;
1048 switch (mType) {
1049 case nsIDataType::VTYPE_INTERFACE:
1050 piid = &NS_GET_IID(nsISupports);
1051 break;
1052 case nsIDataType::VTYPE_INTERFACE_IS:
1053 piid = &u.iface.mInterfaceID;
1054 break;
1055 default:
1056 return NS_ERROR_CANNOT_CONVERT_DATA;
1059 *aIID = piid->Clone();
1061 if (u.iface.mInterfaceValue) {
1062 return u.iface.mInterfaceValue->QueryInterface(*piid, aInterface);
1065 *aInterface = nullptr;
1066 return NS_OK;
1069 nsresult nsDiscriminatedUnion::ConvertToArray(uint16_t* aType, nsIID* aIID,
1070 uint32_t* aCount,
1071 void** aPtr) const {
1072 // XXX perhaps we'd like to add support for converting each of the various
1073 // types into an array containing one element of that type. We can leverage
1074 // CloneArray to do this if we want to support this.
1076 if (mType == nsIDataType::VTYPE_ARRAY) {
1077 return CloneArray(u.array.mArrayType, &u.array.mArrayInterfaceID,
1078 u.array.mArrayCount, u.array.mArrayValue, aType, aIID,
1079 aCount, aPtr);
1081 return NS_ERROR_CANNOT_CONVERT_DATA;
1084 /***************************************************************************/
1085 // static setter functions...
1087 #define DATA_SETTER_PROLOGUE Cleanup()
1089 #define DATA_SETTER_EPILOGUE(type_) mType = nsIDataType::type_;
1091 #define DATA_SETTER(type_, member_, value_) \
1092 DATA_SETTER_PROLOGUE; \
1093 u.member_ = value_; \
1094 DATA_SETTER_EPILOGUE(type_)
1096 #define DATA_SETTER_WITH_CAST(type_, member_, cast_, value_) \
1097 DATA_SETTER_PROLOGUE; \
1098 u.member_ = cast_ value_; \
1099 DATA_SETTER_EPILOGUE(type_)
1101 /********************************************/
1103 #define CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(type_) {
1104 #define CASE__SET_FROM_VARIANT_VTYPE__GETTER(member_, name_) \
1105 rv = aValue->GetAs##name_(&(u.member_));
1107 #define CASE__SET_FROM_VARIANT_VTYPE__GETTER_CAST(cast_, member_, name_) \
1108 rv = aValue->GetAs##name_(cast_ & (u.member_));
1110 #define CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(type_) \
1111 if (NS_SUCCEEDED(rv)) { \
1112 mType = nsIDataType::type_; \
1114 break; \
1117 #define CASE__SET_FROM_VARIANT_TYPE(type_, member_, name_) \
1118 case nsIDataType::type_: \
1119 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(type_) \
1120 CASE__SET_FROM_VARIANT_VTYPE__GETTER(member_, name_) \
1121 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(type_)
1123 #define CASE__SET_FROM_VARIANT_VTYPE_CAST(type_, cast_, member_, name_) \
1124 case nsIDataType::type_: \
1125 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(type_) \
1126 CASE__SET_FROM_VARIANT_VTYPE__GETTER_CAST(cast_, member_, name_) \
1127 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(type_)
1129 nsresult nsDiscriminatedUnion::SetFromVariant(nsIVariant* aValue) {
1130 nsresult rv = NS_OK;
1132 Cleanup();
1134 uint16_t type = aValue->GetDataType();
1136 switch (type) {
1137 CASE__SET_FROM_VARIANT_VTYPE_CAST(VTYPE_INT8, (uint8_t*), mInt8Value, Int8)
1138 CASE__SET_FROM_VARIANT_TYPE(VTYPE_INT16, mInt16Value, Int16)
1139 CASE__SET_FROM_VARIANT_TYPE(VTYPE_INT32, mInt32Value, Int32)
1140 CASE__SET_FROM_VARIANT_TYPE(VTYPE_UINT8, mUint8Value, Uint8)
1141 CASE__SET_FROM_VARIANT_TYPE(VTYPE_UINT16, mUint16Value, Uint16)
1142 CASE__SET_FROM_VARIANT_TYPE(VTYPE_UINT32, mUint32Value, Uint32)
1143 CASE__SET_FROM_VARIANT_TYPE(VTYPE_FLOAT, mFloatValue, Float)
1144 CASE__SET_FROM_VARIANT_TYPE(VTYPE_DOUBLE, mDoubleValue, Double)
1145 CASE__SET_FROM_VARIANT_TYPE(VTYPE_BOOL, mBoolValue, Bool)
1146 CASE__SET_FROM_VARIANT_TYPE(VTYPE_CHAR, mCharValue, Char)
1147 CASE__SET_FROM_VARIANT_TYPE(VTYPE_WCHAR, mWCharValue, WChar)
1148 CASE__SET_FROM_VARIANT_TYPE(VTYPE_ID, mIDValue, ID)
1150 case nsIDataType::VTYPE_ASTRING:
1151 case nsIDataType::VTYPE_WCHAR_STR:
1152 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
1153 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(VTYPE_ASTRING);
1154 u.mAStringValue = new nsString();
1156 rv = aValue->GetAsAString(*u.mAStringValue);
1157 if (NS_FAILED(rv)) {
1158 delete u.mAStringValue;
1160 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(VTYPE_ASTRING)
1162 case nsIDataType::VTYPE_CSTRING:
1163 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(VTYPE_CSTRING);
1164 u.mCStringValue = new nsCString();
1166 rv = aValue->GetAsACString(*u.mCStringValue);
1167 if (NS_FAILED(rv)) {
1168 delete u.mCStringValue;
1170 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(VTYPE_CSTRING)
1172 case nsIDataType::VTYPE_UTF8STRING:
1173 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(VTYPE_UTF8STRING);
1174 u.mUTF8StringValue = new nsUTF8String();
1176 rv = aValue->GetAsAUTF8String(*u.mUTF8StringValue);
1177 if (NS_FAILED(rv)) {
1178 delete u.mUTF8StringValue;
1180 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(VTYPE_UTF8STRING)
1182 case nsIDataType::VTYPE_CHAR_STR:
1183 case nsIDataType::VTYPE_STRING_SIZE_IS:
1184 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(VTYPE_STRING_SIZE_IS);
1185 rv = aValue->GetAsStringWithSize(&u.str.mStringLength,
1186 &u.str.mStringValue);
1187 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(VTYPE_STRING_SIZE_IS)
1189 case nsIDataType::VTYPE_INTERFACE:
1190 case nsIDataType::VTYPE_INTERFACE_IS:
1191 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(VTYPE_INTERFACE_IS);
1192 // XXX This iid handling is ugly!
1193 nsIID* iid;
1194 rv = aValue->GetAsInterface(&iid, (void**)&u.iface.mInterfaceValue);
1195 if (NS_SUCCEEDED(rv)) {
1196 u.iface.mInterfaceID = *iid;
1197 free((char*)iid);
1199 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(VTYPE_INTERFACE_IS)
1201 case nsIDataType::VTYPE_ARRAY:
1202 CASE__SET_FROM_VARIANT_VTYPE_PROLOGUE(VTYPE_ARRAY);
1203 rv = aValue->GetAsArray(&u.array.mArrayType, &u.array.mArrayInterfaceID,
1204 &u.array.mArrayCount, &u.array.mArrayValue);
1205 CASE__SET_FROM_VARIANT_VTYPE_EPILOGUE(VTYPE_ARRAY)
1207 case nsIDataType::VTYPE_VOID:
1208 SetToVoid();
1209 rv = NS_OK;
1210 break;
1211 case nsIDataType::VTYPE_EMPTY_ARRAY:
1212 SetToEmptyArray();
1213 rv = NS_OK;
1214 break;
1215 case nsIDataType::VTYPE_EMPTY:
1216 SetToEmpty();
1217 rv = NS_OK;
1218 break;
1219 default:
1220 NS_ERROR("bad type in variant!");
1221 rv = NS_ERROR_FAILURE;
1222 break;
1224 return rv;
1227 void nsDiscriminatedUnion::SetFromInt8(uint8_t aValue) {
1228 DATA_SETTER_WITH_CAST(VTYPE_INT8, mInt8Value, (uint8_t), aValue);
1230 void nsDiscriminatedUnion::SetFromInt16(int16_t aValue) {
1231 DATA_SETTER(VTYPE_INT16, mInt16Value, aValue);
1233 void nsDiscriminatedUnion::SetFromInt32(int32_t aValue) {
1234 DATA_SETTER(VTYPE_INT32, mInt32Value, aValue);
1236 void nsDiscriminatedUnion::SetFromInt64(int64_t aValue) {
1237 DATA_SETTER(VTYPE_INT64, mInt64Value, aValue);
1239 void nsDiscriminatedUnion::SetFromUint8(uint8_t aValue) {
1240 DATA_SETTER(VTYPE_UINT8, mUint8Value, aValue);
1242 void nsDiscriminatedUnion::SetFromUint16(uint16_t aValue) {
1243 DATA_SETTER(VTYPE_UINT16, mUint16Value, aValue);
1245 void nsDiscriminatedUnion::SetFromUint32(uint32_t aValue) {
1246 DATA_SETTER(VTYPE_UINT32, mUint32Value, aValue);
1248 void nsDiscriminatedUnion::SetFromUint64(uint64_t aValue) {
1249 DATA_SETTER(VTYPE_UINT64, mUint64Value, aValue);
1251 void nsDiscriminatedUnion::SetFromFloat(float aValue) {
1252 DATA_SETTER(VTYPE_FLOAT, mFloatValue, aValue);
1254 void nsDiscriminatedUnion::SetFromDouble(double aValue) {
1255 DATA_SETTER(VTYPE_DOUBLE, mDoubleValue, aValue);
1257 void nsDiscriminatedUnion::SetFromBool(bool aValue) {
1258 DATA_SETTER(VTYPE_BOOL, mBoolValue, aValue);
1260 void nsDiscriminatedUnion::SetFromChar(char aValue) {
1261 DATA_SETTER(VTYPE_CHAR, mCharValue, aValue);
1263 void nsDiscriminatedUnion::SetFromWChar(char16_t aValue) {
1264 DATA_SETTER(VTYPE_WCHAR, mWCharValue, aValue);
1266 void nsDiscriminatedUnion::SetFromID(const nsID& aValue) {
1267 DATA_SETTER(VTYPE_ID, mIDValue, aValue);
1269 void nsDiscriminatedUnion::SetFromAString(const nsAString& aValue) {
1270 DATA_SETTER_PROLOGUE;
1271 u.mAStringValue = new nsString(aValue);
1272 DATA_SETTER_EPILOGUE(VTYPE_ASTRING);
1275 void nsDiscriminatedUnion::SetFromACString(const nsACString& aValue) {
1276 DATA_SETTER_PROLOGUE;
1277 u.mCStringValue = new nsCString(aValue);
1278 DATA_SETTER_EPILOGUE(VTYPE_CSTRING);
1281 void nsDiscriminatedUnion::SetFromAUTF8String(const nsAUTF8String& aValue) {
1282 DATA_SETTER_PROLOGUE;
1283 u.mUTF8StringValue = new nsUTF8String(aValue);
1284 DATA_SETTER_EPILOGUE(VTYPE_UTF8STRING);
1287 nsresult nsDiscriminatedUnion::SetFromString(const char* aValue) {
1288 DATA_SETTER_PROLOGUE;
1289 if (!aValue) {
1290 return NS_ERROR_NULL_POINTER;
1292 return SetFromStringWithSize(strlen(aValue), aValue);
1294 nsresult nsDiscriminatedUnion::SetFromWString(const char16_t* aValue) {
1295 DATA_SETTER_PROLOGUE;
1296 if (!aValue) {
1297 return NS_ERROR_NULL_POINTER;
1299 return SetFromWStringWithSize(NS_strlen(aValue), aValue);
1301 void nsDiscriminatedUnion::SetFromISupports(nsISupports* aValue) {
1302 return SetFromInterface(NS_GET_IID(nsISupports), aValue);
1304 void nsDiscriminatedUnion::SetFromInterface(const nsIID& aIID,
1305 nsISupports* aValue) {
1306 DATA_SETTER_PROLOGUE;
1307 NS_IF_ADDREF(aValue);
1308 u.iface.mInterfaceValue = aValue;
1309 u.iface.mInterfaceID = aIID;
1310 DATA_SETTER_EPILOGUE(VTYPE_INTERFACE_IS);
1312 nsresult nsDiscriminatedUnion::SetFromArray(uint16_t aType, const nsIID* aIID,
1313 uint32_t aCount, void* aValue) {
1314 DATA_SETTER_PROLOGUE;
1315 if (!aValue || !aCount) {
1316 return NS_ERROR_NULL_POINTER;
1319 nsresult rv = CloneArray(aType, aIID, aCount, aValue, &u.array.mArrayType,
1320 &u.array.mArrayInterfaceID, &u.array.mArrayCount,
1321 &u.array.mArrayValue);
1322 if (NS_FAILED(rv)) {
1323 return rv;
1325 DATA_SETTER_EPILOGUE(VTYPE_ARRAY);
1326 return NS_OK;
1328 nsresult nsDiscriminatedUnion::SetFromStringWithSize(uint32_t aSize,
1329 const char* aValue) {
1330 DATA_SETTER_PROLOGUE;
1331 if (!aValue) {
1332 return NS_ERROR_NULL_POINTER;
1334 u.str.mStringValue = (char*)moz_xmemdup(aValue, (aSize + 1) * sizeof(char));
1335 u.str.mStringLength = aSize;
1336 DATA_SETTER_EPILOGUE(VTYPE_STRING_SIZE_IS);
1337 return NS_OK;
1339 nsresult nsDiscriminatedUnion::SetFromWStringWithSize(uint32_t aSize,
1340 const char16_t* aValue) {
1341 DATA_SETTER_PROLOGUE;
1342 if (!aValue) {
1343 return NS_ERROR_NULL_POINTER;
1345 u.wstr.mWStringValue =
1346 (char16_t*)moz_xmemdup(aValue, (aSize + 1) * sizeof(char16_t));
1347 u.wstr.mWStringLength = aSize;
1348 DATA_SETTER_EPILOGUE(VTYPE_WSTRING_SIZE_IS);
1349 return NS_OK;
1351 void nsDiscriminatedUnion::AllocateWStringWithSize(uint32_t aSize) {
1352 DATA_SETTER_PROLOGUE;
1353 u.wstr.mWStringValue = (char16_t*)moz_xmalloc((aSize + 1) * sizeof(char16_t));
1354 u.wstr.mWStringValue[aSize] = '\0';
1355 u.wstr.mWStringLength = aSize;
1356 DATA_SETTER_EPILOGUE(VTYPE_WSTRING_SIZE_IS);
1358 void nsDiscriminatedUnion::SetToVoid() {
1359 DATA_SETTER_PROLOGUE;
1360 DATA_SETTER_EPILOGUE(VTYPE_VOID);
1362 void nsDiscriminatedUnion::SetToEmpty() {
1363 DATA_SETTER_PROLOGUE;
1364 DATA_SETTER_EPILOGUE(VTYPE_EMPTY);
1366 void nsDiscriminatedUnion::SetToEmptyArray() {
1367 DATA_SETTER_PROLOGUE;
1368 DATA_SETTER_EPILOGUE(VTYPE_EMPTY_ARRAY);
1371 /***************************************************************************/
1373 void nsDiscriminatedUnion::Cleanup() {
1374 switch (mType) {
1375 case nsIDataType::VTYPE_INT8:
1376 case nsIDataType::VTYPE_INT16:
1377 case nsIDataType::VTYPE_INT32:
1378 case nsIDataType::VTYPE_INT64:
1379 case nsIDataType::VTYPE_UINT8:
1380 case nsIDataType::VTYPE_UINT16:
1381 case nsIDataType::VTYPE_UINT32:
1382 case nsIDataType::VTYPE_UINT64:
1383 case nsIDataType::VTYPE_FLOAT:
1384 case nsIDataType::VTYPE_DOUBLE:
1385 case nsIDataType::VTYPE_BOOL:
1386 case nsIDataType::VTYPE_CHAR:
1387 case nsIDataType::VTYPE_WCHAR:
1388 case nsIDataType::VTYPE_VOID:
1389 case nsIDataType::VTYPE_ID:
1390 break;
1391 case nsIDataType::VTYPE_ASTRING:
1392 delete u.mAStringValue;
1393 break;
1394 case nsIDataType::VTYPE_CSTRING:
1395 delete u.mCStringValue;
1396 break;
1397 case nsIDataType::VTYPE_UTF8STRING:
1398 delete u.mUTF8StringValue;
1399 break;
1400 case nsIDataType::VTYPE_CHAR_STR:
1401 case nsIDataType::VTYPE_STRING_SIZE_IS:
1402 free((char*)u.str.mStringValue);
1403 break;
1404 case nsIDataType::VTYPE_WCHAR_STR:
1405 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
1406 free((char*)u.wstr.mWStringValue);
1407 break;
1408 case nsIDataType::VTYPE_INTERFACE:
1409 case nsIDataType::VTYPE_INTERFACE_IS:
1410 NS_IF_RELEASE(u.iface.mInterfaceValue);
1411 break;
1412 case nsIDataType::VTYPE_ARRAY:
1413 FreeArray();
1414 break;
1415 case nsIDataType::VTYPE_EMPTY_ARRAY:
1416 case nsIDataType::VTYPE_EMPTY:
1417 break;
1418 default:
1419 NS_ERROR("bad type in variant!");
1420 break;
1423 mType = nsIDataType::VTYPE_EMPTY;
1426 void nsDiscriminatedUnion::Traverse(
1427 nsCycleCollectionTraversalCallback& aCb) const {
1428 switch (mType) {
1429 case nsIDataType::VTYPE_INTERFACE:
1430 case nsIDataType::VTYPE_INTERFACE_IS:
1431 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "mData");
1432 aCb.NoteXPCOMChild(u.iface.mInterfaceValue);
1433 break;
1434 case nsIDataType::VTYPE_ARRAY:
1435 switch (u.array.mArrayType) {
1436 case nsIDataType::VTYPE_INTERFACE:
1437 case nsIDataType::VTYPE_INTERFACE_IS: {
1438 nsISupports** p = (nsISupports**)u.array.mArrayValue;
1439 for (uint32_t i = u.array.mArrayCount; i > 0; ++p, --i) {
1440 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCb, "mData[i]");
1441 aCb.NoteXPCOMChild(*p);
1443 break;
1445 default:
1446 break;
1448 break;
1449 default:
1450 break;
1454 /***************************************************************************/
1455 /***************************************************************************/
1456 // members...
1458 nsVariantBase::nsVariantBase() : mWritable(true) {}
1460 // For all the data getters we just forward to the static (and sharable)
1461 // 'ConvertTo' functions.
1463 uint16_t nsVariantBase::GetDataType() { return mData.GetType(); }
1465 NS_IMETHODIMP
1466 nsVariantBase::GetAsInt8(uint8_t* aResult) {
1467 return mData.ConvertToInt8(aResult);
1470 NS_IMETHODIMP
1471 nsVariantBase::GetAsInt16(int16_t* aResult) {
1472 return mData.ConvertToInt16(aResult);
1475 NS_IMETHODIMP
1476 nsVariantBase::GetAsInt32(int32_t* aResult) {
1477 return mData.ConvertToInt32(aResult);
1480 NS_IMETHODIMP
1481 nsVariantBase::GetAsInt64(int64_t* aResult) {
1482 return mData.ConvertToInt64(aResult);
1485 NS_IMETHODIMP
1486 nsVariantBase::GetAsUint8(uint8_t* aResult) {
1487 return mData.ConvertToUint8(aResult);
1490 NS_IMETHODIMP
1491 nsVariantBase::GetAsUint16(uint16_t* aResult) {
1492 return mData.ConvertToUint16(aResult);
1495 NS_IMETHODIMP
1496 nsVariantBase::GetAsUint32(uint32_t* aResult) {
1497 return mData.ConvertToUint32(aResult);
1500 NS_IMETHODIMP
1501 nsVariantBase::GetAsUint64(uint64_t* aResult) {
1502 return mData.ConvertToUint64(aResult);
1505 NS_IMETHODIMP
1506 nsVariantBase::GetAsFloat(float* aResult) {
1507 return mData.ConvertToFloat(aResult);
1510 NS_IMETHODIMP
1511 nsVariantBase::GetAsDouble(double* aResult) {
1512 return mData.ConvertToDouble(aResult);
1515 NS_IMETHODIMP
1516 nsVariantBase::GetAsBool(bool* aResult) { return mData.ConvertToBool(aResult); }
1518 NS_IMETHODIMP
1519 nsVariantBase::GetAsChar(char* aResult) { return mData.ConvertToChar(aResult); }
1521 NS_IMETHODIMP
1522 nsVariantBase::GetAsWChar(char16_t* aResult) {
1523 return mData.ConvertToWChar(aResult);
1526 NS_IMETHODIMP_(nsresult)
1527 nsVariantBase::GetAsID(nsID* aResult) { return mData.ConvertToID(aResult); }
1529 NS_IMETHODIMP
1530 nsVariantBase::GetAsAString(nsAString& aResult) {
1531 return mData.ConvertToAString(aResult);
1534 NS_IMETHODIMP
1535 nsVariantBase::GetAsACString(nsACString& aResult) {
1536 return mData.ConvertToACString(aResult);
1539 NS_IMETHODIMP
1540 nsVariantBase::GetAsAUTF8String(nsAUTF8String& aResult) {
1541 return mData.ConvertToAUTF8String(aResult);
1544 NS_IMETHODIMP
1545 nsVariantBase::GetAsString(char** aResult) {
1546 return mData.ConvertToString(aResult);
1549 NS_IMETHODIMP
1550 nsVariantBase::GetAsWString(char16_t** aResult) {
1551 return mData.ConvertToWString(aResult);
1554 NS_IMETHODIMP
1555 nsVariantBase::GetAsISupports(nsISupports** aResult) {
1556 return mData.ConvertToISupports(aResult);
1559 NS_IMETHODIMP
1560 nsVariantBase::GetAsJSVal(JS::MutableHandleValue) {
1561 // Can only get the jsval from an XPCVariant.
1562 return NS_ERROR_CANNOT_CONVERT_DATA;
1565 NS_IMETHODIMP
1566 nsVariantBase::GetAsInterface(nsIID** aIID, void** aInterface) {
1567 return mData.ConvertToInterface(aIID, aInterface);
1570 NS_IMETHODIMP_(nsresult)
1571 nsVariantBase::GetAsArray(uint16_t* aType, nsIID* aIID, uint32_t* aCount,
1572 void** aPtr) {
1573 return mData.ConvertToArray(aType, aIID, aCount, aPtr);
1576 NS_IMETHODIMP
1577 nsVariantBase::GetAsStringWithSize(uint32_t* aSize, char** aStr) {
1578 return mData.ConvertToStringWithSize(aSize, aStr);
1581 NS_IMETHODIMP
1582 nsVariantBase::GetAsWStringWithSize(uint32_t* aSize, char16_t** aStr) {
1583 return mData.ConvertToWStringWithSize(aSize, aStr);
1586 /***************************************************************************/
1588 NS_IMETHODIMP
1589 nsVariantBase::GetWritable(bool* aWritable) {
1590 *aWritable = mWritable;
1591 return NS_OK;
1593 NS_IMETHODIMP
1594 nsVariantBase::SetWritable(bool aWritable) {
1595 if (!mWritable && aWritable) {
1596 return NS_ERROR_FAILURE;
1598 mWritable = aWritable;
1599 return NS_OK;
1602 /***************************************************************************/
1604 // For all the data setters we just forward to the static (and sharable)
1605 // 'SetFrom' functions.
1607 NS_IMETHODIMP
1608 nsVariantBase::SetAsInt8(uint8_t aValue) {
1609 if (!mWritable) {
1610 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1612 mData.SetFromInt8(aValue);
1613 return NS_OK;
1616 NS_IMETHODIMP
1617 nsVariantBase::SetAsInt16(int16_t aValue) {
1618 if (!mWritable) {
1619 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1621 mData.SetFromInt16(aValue);
1622 return NS_OK;
1625 NS_IMETHODIMP
1626 nsVariantBase::SetAsInt32(int32_t aValue) {
1627 if (!mWritable) {
1628 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1630 mData.SetFromInt32(aValue);
1631 return NS_OK;
1634 NS_IMETHODIMP
1635 nsVariantBase::SetAsInt64(int64_t aValue) {
1636 if (!mWritable) {
1637 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1639 mData.SetFromInt64(aValue);
1640 return NS_OK;
1643 NS_IMETHODIMP
1644 nsVariantBase::SetAsUint8(uint8_t aValue) {
1645 if (!mWritable) {
1646 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1648 mData.SetFromUint8(aValue);
1649 return NS_OK;
1652 NS_IMETHODIMP
1653 nsVariantBase::SetAsUint16(uint16_t aValue) {
1654 if (!mWritable) {
1655 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1657 mData.SetFromUint16(aValue);
1658 return NS_OK;
1661 NS_IMETHODIMP
1662 nsVariantBase::SetAsUint32(uint32_t aValue) {
1663 if (!mWritable) {
1664 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1666 mData.SetFromUint32(aValue);
1667 return NS_OK;
1670 NS_IMETHODIMP
1671 nsVariantBase::SetAsUint64(uint64_t aValue) {
1672 if (!mWritable) {
1673 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1675 mData.SetFromUint64(aValue);
1676 return NS_OK;
1679 NS_IMETHODIMP
1680 nsVariantBase::SetAsFloat(float aValue) {
1681 if (!mWritable) {
1682 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1684 mData.SetFromFloat(aValue);
1685 return NS_OK;
1688 NS_IMETHODIMP
1689 nsVariantBase::SetAsDouble(double aValue) {
1690 if (!mWritable) {
1691 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1693 mData.SetFromDouble(aValue);
1694 return NS_OK;
1697 NS_IMETHODIMP
1698 nsVariantBase::SetAsBool(bool aValue) {
1699 if (!mWritable) {
1700 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1702 mData.SetFromBool(aValue);
1703 return NS_OK;
1706 NS_IMETHODIMP
1707 nsVariantBase::SetAsChar(char aValue) {
1708 if (!mWritable) {
1709 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1711 mData.SetFromChar(aValue);
1712 return NS_OK;
1715 NS_IMETHODIMP
1716 nsVariantBase::SetAsWChar(char16_t aValue) {
1717 if (!mWritable) {
1718 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1720 mData.SetFromWChar(aValue);
1721 return NS_OK;
1724 NS_IMETHODIMP
1725 nsVariantBase::SetAsID(const nsID& aValue) {
1726 if (!mWritable) {
1727 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1729 mData.SetFromID(aValue);
1730 return NS_OK;
1733 NS_IMETHODIMP
1734 nsVariantBase::SetAsAString(const nsAString& aValue) {
1735 if (!mWritable) {
1736 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1738 mData.SetFromAString(aValue);
1739 return NS_OK;
1742 NS_IMETHODIMP
1743 nsVariantBase::SetAsACString(const nsACString& aValue) {
1744 if (!mWritable) {
1745 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1747 mData.SetFromACString(aValue);
1748 return NS_OK;
1751 NS_IMETHODIMP
1752 nsVariantBase::SetAsAUTF8String(const nsAUTF8String& aValue) {
1753 if (!mWritable) {
1754 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1756 mData.SetFromAUTF8String(aValue);
1757 return NS_OK;
1760 NS_IMETHODIMP
1761 nsVariantBase::SetAsString(const char* aValue) {
1762 if (!mWritable) {
1763 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1765 return mData.SetFromString(aValue);
1768 NS_IMETHODIMP
1769 nsVariantBase::SetAsWString(const char16_t* aValue) {
1770 if (!mWritable) {
1771 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1773 return mData.SetFromWString(aValue);
1776 NS_IMETHODIMP
1777 nsVariantBase::SetAsISupports(nsISupports* aValue) {
1778 if (!mWritable) {
1779 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1781 mData.SetFromISupports(aValue);
1782 return NS_OK;
1785 NS_IMETHODIMP
1786 nsVariantBase::SetAsInterface(const nsIID& aIID, void* aInterface) {
1787 if (!mWritable) {
1788 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1790 mData.SetFromInterface(aIID, (nsISupports*)aInterface);
1791 return NS_OK;
1794 NS_IMETHODIMP
1795 nsVariantBase::SetAsArray(uint16_t aType, const nsIID* aIID, uint32_t aCount,
1796 void* aPtr) {
1797 if (!mWritable) {
1798 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1800 return mData.SetFromArray(aType, aIID, aCount, aPtr);
1803 NS_IMETHODIMP
1804 nsVariantBase::SetAsStringWithSize(uint32_t aSize, const char* aStr) {
1805 if (!mWritable) {
1806 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1808 return mData.SetFromStringWithSize(aSize, aStr);
1811 NS_IMETHODIMP
1812 nsVariantBase::SetAsWStringWithSize(uint32_t aSize, const char16_t* aStr) {
1813 if (!mWritable) {
1814 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1816 return mData.SetFromWStringWithSize(aSize, aStr);
1819 NS_IMETHODIMP
1820 nsVariantBase::SetAsVoid() {
1821 if (!mWritable) {
1822 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1824 mData.SetToVoid();
1825 return NS_OK;
1828 NS_IMETHODIMP
1829 nsVariantBase::SetAsEmpty() {
1830 if (!mWritable) {
1831 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1833 mData.SetToEmpty();
1834 return NS_OK;
1837 NS_IMETHODIMP
1838 nsVariantBase::SetAsEmptyArray() {
1839 if (!mWritable) {
1840 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1842 mData.SetToEmptyArray();
1843 return NS_OK;
1846 NS_IMETHODIMP
1847 nsVariantBase::SetFromVariant(nsIVariant* aValue) {
1848 if (!mWritable) {
1849 return NS_ERROR_OBJECT_IS_IMMUTABLE;
1851 return mData.SetFromVariant(aValue);
1854 /* nsVariant implementation */
1856 NS_IMPL_ISUPPORTS(nsVariant, nsIVariant, nsIWritableVariant)
1858 /* nsVariantCC implementation */
1859 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsVariantCC)
1860 NS_INTERFACE_MAP_ENTRY(nsISupports)
1861 NS_INTERFACE_MAP_ENTRY(nsIVariant)
1862 NS_INTERFACE_MAP_ENTRY(nsIWritableVariant)
1863 NS_INTERFACE_MAP_END
1865 NS_IMPL_CYCLE_COLLECTION_CLASS(nsVariantCC)
1867 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsVariantCC)
1868 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsVariantCC)
1870 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsVariantCC)
1871 tmp->mData.Traverse(cb);
1872 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
1874 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsVariantCC)
1875 tmp->mData.Cleanup();
1876 NS_IMPL_CYCLE_COLLECTION_UNLINK_END