js/xpconnect/public/nsTArrayHelpers.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/public/nsTArrayHelpers.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,92 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     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
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef __NSTARRAYHELPERS_H__
    1.11 +#define __NSTARRAYHELPERS_H__
    1.12 +
    1.13 +#include "jsapi.h"
    1.14 +#include "nsContentUtils.h"
    1.15 +#include "nsTArray.h"
    1.16 +
    1.17 +template <class T>
    1.18 +inline nsresult
    1.19 +nsTArrayToJSArray(JSContext* aCx, const nsTArray<T>& aSourceArray,
    1.20 +                  JSObject** aResultArray)
    1.21 +{
    1.22 +  MOZ_ASSERT(aCx);
    1.23 +
    1.24 +  JS::Rooted<JSObject*> arrayObj(aCx,
    1.25 +    JS_NewArrayObject(aCx, aSourceArray.Length()));
    1.26 +  if (!arrayObj) {
    1.27 +    NS_WARNING("JS_NewArrayObject failed!");
    1.28 +    return NS_ERROR_OUT_OF_MEMORY;
    1.29 +  }
    1.30 +
    1.31 +  for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
    1.32 +    nsCOMPtr<nsISupports> obj;
    1.33 +    nsresult rv = aSourceArray[index]->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(obj));
    1.34 +    NS_ENSURE_SUCCESS(rv, rv);
    1.35 +
    1.36 +    JS::RootedValue wrappedVal(aCx);
    1.37 +    rv = nsContentUtils::WrapNative(aCx, obj, &wrappedVal);
    1.38 +    NS_ENSURE_SUCCESS(rv, rv);
    1.39 +
    1.40 +    if (!JS_SetElement(aCx, arrayObj, index, wrappedVal)) {
    1.41 +      NS_WARNING("JS_SetElement failed!");
    1.42 +      return NS_ERROR_FAILURE;
    1.43 +    }
    1.44 +  }
    1.45 +
    1.46 +  if (!JS_FreezeObject(aCx, arrayObj)) {
    1.47 +    NS_WARNING("JS_FreezeObject failed!");
    1.48 +    return NS_ERROR_FAILURE;
    1.49 +  }
    1.50 +
    1.51 +  *aResultArray = arrayObj;
    1.52 +  return NS_OK;
    1.53 +}
    1.54 +
    1.55 +template <>
    1.56 +inline nsresult
    1.57 +nsTArrayToJSArray<nsString>(JSContext* aCx,
    1.58 +                            const nsTArray<nsString>& aSourceArray,
    1.59 +                            JSObject** aResultArray)
    1.60 +{
    1.61 +  MOZ_ASSERT(aCx);
    1.62 +
    1.63 +  JS::Rooted<JSObject*> arrayObj(aCx,
    1.64 +    JS_NewArrayObject(aCx, aSourceArray.Length()));
    1.65 +  if (!arrayObj) {
    1.66 +    NS_WARNING("JS_NewArrayObject failed!");
    1.67 +    return NS_ERROR_OUT_OF_MEMORY;
    1.68 +  }
    1.69 +
    1.70 +  JS::Rooted<JSString*> s(aCx);
    1.71 +  for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
    1.72 +    s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(),
    1.73 +                            aSourceArray[index].Length());
    1.74 +
    1.75 +    if(!s) {
    1.76 +      NS_WARNING("Memory allocation error!");
    1.77 +      return NS_ERROR_OUT_OF_MEMORY;
    1.78 +    }
    1.79 +
    1.80 +    if (!JS_SetElement(aCx, arrayObj, index, s)) {
    1.81 +      NS_WARNING("JS_SetElement failed!");
    1.82 +      return NS_ERROR_FAILURE;
    1.83 +    }
    1.84 +  }
    1.85 +
    1.86 +  if (!JS_FreezeObject(aCx, arrayObj)) {
    1.87 +    NS_WARNING("JS_FreezeObject failed!");
    1.88 +    return NS_ERROR_FAILURE;
    1.89 +  }
    1.90 +
    1.91 +  *aResultArray = arrayObj;
    1.92 +  return NS_OK;
    1.93 +}
    1.94 +
    1.95 +#endif /* __NSTARRAYHELPERS_H__ */

mercurial