Bumping manifests a=b2g-bump
[gecko.git] / dom / bindings / ToJSValue.cpp
blobcbefc0218f09305710432e0e7fadb79f2377dcd9
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"
15 namespace mozilla {
16 namespace dom {
18 bool
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,
29 aValue)) {
30 return false;
33 if (sharedBuffer) {
34 NS_ADDREF(sharedBuffer);
37 return true;
41 namespace tojsvalue_detail {
43 bool
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
54 bool
55 ToJSValue(JSContext* aCx,
56 nsresult aArgument,
57 JS::MutableHandle<JS::Value> aValue)
59 nsRefPtr<Exception> exception = CreateException(aCx, aArgument);
60 return ToJSValue(aCx, exception, aValue);
63 bool
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);
74 return true;
77 } // namespace dom
78 } // namespace mozilla