1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
2 /* vim: set ts=2 sw=2 et tw=79: */
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ToJSValue.h"
8 #include "mozilla/dom/DOMException.h"
9 #include "mozilla/dom/Exceptions.h"
10 #include "nsAString.h"
11 #include "nsContentUtils.h"
12 #include "nsStringBuffer.h"
13 #include "xpcpublic.h"
19 ToJSValue(JSContext
* aCx
, const nsAString
& aArgument
,
20 JS::MutableHandle
<JS::Value
> aValue
)
22 // Make sure we're called in a compartment
23 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx
));
25 // XXXkhuey I'd love to use xpc::NonVoidStringToJsval here, but it requires
26 // a non-const nsAString for silly reasons.
27 nsStringBuffer
* sharedBuffer
;
28 if (!XPCStringConvert::ReadableToJSVal(aCx
, aArgument
, &sharedBuffer
,
34 NS_ADDREF(sharedBuffer
);
41 namespace tojsvalue_detail
{
44 ISupportsToJSValue(JSContext
* aCx
,
45 nsISupports
* aArgument
,
46 JS::MutableHandle
<JS::Value
> aValue
)
48 nsresult rv
= nsContentUtils::WrapNative(aCx
, aArgument
, aValue
);
49 return NS_SUCCEEDED(rv
);
52 } // namespace tojsvalue_detail
55 ToJSValue(JSContext
* aCx
,
57 JS::MutableHandle
<JS::Value
> aValue
)
59 nsRefPtr
<Exception
> exception
= CreateException(aCx
, aArgument
);
60 return ToJSValue(aCx
, exception
, aValue
);
64 ToJSValue(JSContext
* aCx
,
65 ErrorResult
& aArgument
,
66 JS::MutableHandle
<JS::Value
> aValue
)
68 MOZ_ASSERT(aArgument
.Failed());
69 DebugOnly
<bool> throwResult
= ThrowMethodFailedWithDetails(aCx
, aArgument
, "", "");
70 MOZ_ASSERT(!throwResult
);
71 DebugOnly
<bool> getPendingResult
= JS_GetPendingException(aCx
, aValue
);
72 MOZ_ASSERT(getPendingResult
);
73 JS_ClearPendingException(aCx
);
78 } // namespace mozilla