michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef __NSTARRAYHELPERS_H__ michael@0: #define __NSTARRAYHELPERS_H__ michael@0: michael@0: #include "jsapi.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsTArray.h" michael@0: michael@0: template michael@0: inline nsresult michael@0: nsTArrayToJSArray(JSContext* aCx, const nsTArray& aSourceArray, michael@0: JSObject** aResultArray) michael@0: { michael@0: MOZ_ASSERT(aCx); michael@0: michael@0: JS::Rooted arrayObj(aCx, michael@0: JS_NewArrayObject(aCx, aSourceArray.Length())); michael@0: if (!arrayObj) { michael@0: NS_WARNING("JS_NewArrayObject failed!"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: for (uint32_t index = 0; index < aSourceArray.Length(); index++) { michael@0: nsCOMPtr obj; michael@0: nsresult rv = aSourceArray[index]->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(obj)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: JS::RootedValue wrappedVal(aCx); michael@0: rv = nsContentUtils::WrapNative(aCx, obj, &wrappedVal); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!JS_SetElement(aCx, arrayObj, index, wrappedVal)) { michael@0: NS_WARNING("JS_SetElement failed!"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: michael@0: if (!JS_FreezeObject(aCx, arrayObj)) { michael@0: NS_WARNING("JS_FreezeObject failed!"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *aResultArray = arrayObj; michael@0: return NS_OK; michael@0: } michael@0: michael@0: template <> michael@0: inline nsresult michael@0: nsTArrayToJSArray(JSContext* aCx, michael@0: const nsTArray& aSourceArray, michael@0: JSObject** aResultArray) michael@0: { michael@0: MOZ_ASSERT(aCx); michael@0: michael@0: JS::Rooted arrayObj(aCx, michael@0: JS_NewArrayObject(aCx, aSourceArray.Length())); michael@0: if (!arrayObj) { michael@0: NS_WARNING("JS_NewArrayObject failed!"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: JS::Rooted s(aCx); michael@0: for (uint32_t index = 0; index < aSourceArray.Length(); index++) { michael@0: s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(), michael@0: aSourceArray[index].Length()); michael@0: michael@0: if(!s) { michael@0: NS_WARNING("Memory allocation error!"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (!JS_SetElement(aCx, arrayObj, index, s)) { michael@0: NS_WARNING("JS_SetElement failed!"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: michael@0: if (!JS_FreezeObject(aCx, arrayObj)) { michael@0: NS_WARNING("JS_FreezeObject failed!"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *aResultArray = arrayObj; michael@0: return NS_OK; michael@0: } michael@0: michael@0: #endif /* __NSTARRAYHELPERS_H__ */