/src/mozilla-central/dom/bindings/ToJSValue.cpp
Line | Count | Source (jump to first uncovered line) |
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 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 "mozilla/dom/Promise.h" |
11 | | #include "nsAString.h" |
12 | | #include "nsContentUtils.h" |
13 | | #include "nsStringBuffer.h" |
14 | | #include "xpcpublic.h" |
15 | | |
16 | | namespace mozilla { |
17 | | namespace dom { |
18 | | |
19 | | bool |
20 | | ToJSValue(JSContext* aCx, const nsAString& aArgument, |
21 | | JS::MutableHandle<JS::Value> aValue) |
22 | 0 | { |
23 | 0 | // Make sure we're called in a compartment |
24 | 0 | MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx)); |
25 | 0 |
|
26 | 0 | // XXXkhuey I'd love to use xpc::NonVoidStringToJsval here, but it requires |
27 | 0 | // a non-const nsAString for silly reasons. |
28 | 0 | nsStringBuffer* sharedBuffer; |
29 | 0 | if (!XPCStringConvert::ReadableToJSVal(aCx, aArgument, &sharedBuffer, |
30 | 0 | aValue)) { |
31 | 0 | return false; |
32 | 0 | } |
33 | 0 | |
34 | 0 | if (sharedBuffer) { |
35 | 0 | NS_ADDREF(sharedBuffer); |
36 | 0 | } |
37 | 0 |
|
38 | 0 | return true; |
39 | 0 | } |
40 | | |
41 | | |
42 | | bool |
43 | | ToJSValue(JSContext* aCx, |
44 | | nsresult aArgument, |
45 | | JS::MutableHandle<JS::Value> aValue) |
46 | 0 | { |
47 | 0 | RefPtr<Exception> exception = CreateException(aArgument); |
48 | 0 | return ToJSValue(aCx, exception, aValue); |
49 | 0 | } |
50 | | |
51 | | bool |
52 | | ToJSValue(JSContext* aCx, |
53 | | ErrorResult& aArgument, |
54 | | JS::MutableHandle<JS::Value> aValue) |
55 | 0 | { |
56 | 0 | MOZ_ASSERT(aArgument.Failed()); |
57 | 0 | MOZ_ASSERT(!aArgument.IsUncatchableException(), |
58 | 0 | "Doesn't make sense to convert uncatchable exception to a JS value!"); |
59 | 0 | MOZ_ALWAYS_TRUE(aArgument.MaybeSetPendingException(aCx)); |
60 | 0 | MOZ_ALWAYS_TRUE(JS_GetPendingException(aCx, aValue)); |
61 | 0 | JS_ClearPendingException(aCx); |
62 | 0 | return true; |
63 | 0 | } |
64 | | |
65 | | bool |
66 | | ToJSValue(JSContext* aCx, Promise& aArgument, |
67 | | JS::MutableHandle<JS::Value> aValue) |
68 | 0 | { |
69 | 0 | aValue.setObject(*aArgument.PromiseObj()); |
70 | 0 | return MaybeWrapObjectValue(aCx, aValue); |
71 | 0 | } |
72 | | |
73 | | } // namespace dom |
74 | | } // namespace mozilla |