dom/bindings/ToJSValue.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/bindings/ToJSValue.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
     1.5 +/* vim: set ts=2 sw=2 et tw=79: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "mozilla/dom/ToJSValue.h"
    1.11 +#include "mozilla/dom/DOMException.h"
    1.12 +#include "mozilla/dom/Exceptions.h"
    1.13 +#include "nsAString.h"
    1.14 +#include "nsContentUtils.h"
    1.15 +#include "nsStringBuffer.h"
    1.16 +#include "xpcpublic.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +namespace dom {
    1.20 +
    1.21 +bool
    1.22 +ToJSValue(JSContext* aCx, const nsAString& aArgument,
    1.23 +          JS::MutableHandle<JS::Value> aValue)
    1.24 +{
    1.25 +  // Make sure we're called in a compartment
    1.26 +  MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
    1.27 +
    1.28 +// XXXkhuey I'd love to use xpc::NonVoidStringToJsval here, but it requires
    1.29 +  // a non-const nsAString for silly reasons.
    1.30 +  nsStringBuffer* sharedBuffer;
    1.31 +  if (!XPCStringConvert::ReadableToJSVal(aCx, aArgument, &sharedBuffer,
    1.32 +                                         aValue)) {
    1.33 +    return false;
    1.34 +  }
    1.35 +
    1.36 +  if (sharedBuffer) {
    1.37 +    NS_ADDREF(sharedBuffer);
    1.38 +  }
    1.39 +
    1.40 +  return true;
    1.41 +}
    1.42 +
    1.43 +
    1.44 +namespace tojsvalue_detail {
    1.45 +
    1.46 +bool
    1.47 +ISupportsToJSValue(JSContext* aCx,
    1.48 +                   nsISupports* aArgument,
    1.49 +                   JS::MutableHandle<JS::Value> aValue)
    1.50 +{
    1.51 +  nsresult rv = nsContentUtils::WrapNative(aCx, aArgument, aValue);
    1.52 +  return NS_SUCCEEDED(rv);
    1.53 +}
    1.54 +
    1.55 +} // namespace tojsvalue_detail
    1.56 +
    1.57 +bool
    1.58 +ToJSValue(JSContext* aCx,
    1.59 +          nsresult aArgument,
    1.60 +          JS::MutableHandle<JS::Value> aValue)
    1.61 +{
    1.62 +  nsRefPtr<Exception> exception = CreateException(aCx, aArgument);
    1.63 +  return ToJSValue(aCx, exception, aValue);
    1.64 +}
    1.65 +
    1.66 +} // namespace dom
    1.67 +} // namespace mozilla

mercurial