|
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/. */ |
|
6 |
|
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" |
|
14 |
|
15 namespace mozilla { |
|
16 namespace dom { |
|
17 |
|
18 bool |
|
19 ToJSValue(JSContext* aCx, const nsAString& aArgument, |
|
20 JS::MutableHandle<JS::Value> aValue) |
|
21 { |
|
22 // Make sure we're called in a compartment |
|
23 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx)); |
|
24 |
|
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; |
|
31 } |
|
32 |
|
33 if (sharedBuffer) { |
|
34 NS_ADDREF(sharedBuffer); |
|
35 } |
|
36 |
|
37 return true; |
|
38 } |
|
39 |
|
40 |
|
41 namespace tojsvalue_detail { |
|
42 |
|
43 bool |
|
44 ISupportsToJSValue(JSContext* aCx, |
|
45 nsISupports* aArgument, |
|
46 JS::MutableHandle<JS::Value> aValue) |
|
47 { |
|
48 nsresult rv = nsContentUtils::WrapNative(aCx, aArgument, aValue); |
|
49 return NS_SUCCEEDED(rv); |
|
50 } |
|
51 |
|
52 } // namespace tojsvalue_detail |
|
53 |
|
54 bool |
|
55 ToJSValue(JSContext* aCx, |
|
56 nsresult aArgument, |
|
57 JS::MutableHandle<JS::Value> aValue) |
|
58 { |
|
59 nsRefPtr<Exception> exception = CreateException(aCx, aArgument); |
|
60 return ToJSValue(aCx, exception, aValue); |
|
61 } |
|
62 |
|
63 } // namespace dom |
|
64 } // namespace mozilla |