michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ michael@0: /* vim: set ts=2 sw=2 et tw=79: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/ToJSValue.h" michael@0: #include "mozilla/dom/DOMException.h" michael@0: #include "mozilla/dom/Exceptions.h" michael@0: #include "nsAString.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsStringBuffer.h" michael@0: #include "xpcpublic.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: bool michael@0: ToJSValue(JSContext* aCx, const nsAString& aArgument, michael@0: JS::MutableHandle aValue) michael@0: { michael@0: // Make sure we're called in a compartment michael@0: MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx)); michael@0: michael@0: // XXXkhuey I'd love to use xpc::NonVoidStringToJsval here, but it requires michael@0: // a non-const nsAString for silly reasons. michael@0: nsStringBuffer* sharedBuffer; michael@0: if (!XPCStringConvert::ReadableToJSVal(aCx, aArgument, &sharedBuffer, michael@0: aValue)) { michael@0: return false; michael@0: } michael@0: michael@0: if (sharedBuffer) { michael@0: NS_ADDREF(sharedBuffer); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: namespace tojsvalue_detail { michael@0: michael@0: bool michael@0: ISupportsToJSValue(JSContext* aCx, michael@0: nsISupports* aArgument, michael@0: JS::MutableHandle aValue) michael@0: { michael@0: nsresult rv = nsContentUtils::WrapNative(aCx, aArgument, aValue); michael@0: return NS_SUCCEEDED(rv); michael@0: } michael@0: michael@0: } // namespace tojsvalue_detail michael@0: michael@0: bool michael@0: ToJSValue(JSContext* aCx, michael@0: nsresult aArgument, michael@0: JS::MutableHandle aValue) michael@0: { michael@0: nsRefPtr exception = CreateException(aCx, aArgument); michael@0: return ToJSValue(aCx, exception, aValue); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla