Line data Source code
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 17 : ToJSValue(JSContext* aCx, const nsAString& aArgument,
21 : JS::MutableHandle<JS::Value> aValue)
22 : {
23 : // Make sure we're called in a compartment
24 17 : MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
25 :
26 : // XXXkhuey I'd love to use xpc::NonVoidStringToJsval here, but it requires
27 : // a non-const nsAString for silly reasons.
28 : nsStringBuffer* sharedBuffer;
29 17 : if (!XPCStringConvert::ReadableToJSVal(aCx, aArgument, &sharedBuffer,
30 : aValue)) {
31 0 : return false;
32 : }
33 :
34 17 : if (sharedBuffer) {
35 17 : NS_ADDREF(sharedBuffer);
36 : }
37 :
38 17 : return true;
39 : }
40 :
41 :
42 : bool
43 0 : ToJSValue(JSContext* aCx,
44 : nsresult aArgument,
45 : JS::MutableHandle<JS::Value> aValue)
46 : {
47 0 : RefPtr<Exception> exception = CreateException(aArgument);
48 0 : return ToJSValue(aCx, exception, aValue);
49 : }
50 :
51 : bool
52 0 : ToJSValue(JSContext* aCx,
53 : ErrorResult& aArgument,
54 : JS::MutableHandle<JS::Value> aValue)
55 : {
56 0 : MOZ_ASSERT(aArgument.Failed());
57 0 : MOZ_ASSERT(!aArgument.IsUncatchableException(),
58 : "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 : }
64 :
65 : bool
66 1 : ToJSValue(JSContext* aCx, Promise& aArgument,
67 : JS::MutableHandle<JS::Value> aValue)
68 : {
69 1 : aValue.setObject(*aArgument.PromiseObj());
70 1 : return MaybeWrapObjectValue(aCx, aValue);
71 : }
72 :
73 : } // namespace dom
74 : } // namespace mozilla
|