js/xpconnect/public/nsTArrayHelpers.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef __NSTARRAYHELPERS_H__
michael@0 8 #define __NSTARRAYHELPERS_H__
michael@0 9
michael@0 10 #include "jsapi.h"
michael@0 11 #include "nsContentUtils.h"
michael@0 12 #include "nsTArray.h"
michael@0 13
michael@0 14 template <class T>
michael@0 15 inline nsresult
michael@0 16 nsTArrayToJSArray(JSContext* aCx, const nsTArray<T>& aSourceArray,
michael@0 17 JSObject** aResultArray)
michael@0 18 {
michael@0 19 MOZ_ASSERT(aCx);
michael@0 20
michael@0 21 JS::Rooted<JSObject*> arrayObj(aCx,
michael@0 22 JS_NewArrayObject(aCx, aSourceArray.Length()));
michael@0 23 if (!arrayObj) {
michael@0 24 NS_WARNING("JS_NewArrayObject failed!");
michael@0 25 return NS_ERROR_OUT_OF_MEMORY;
michael@0 26 }
michael@0 27
michael@0 28 for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
michael@0 29 nsCOMPtr<nsISupports> obj;
michael@0 30 nsresult rv = aSourceArray[index]->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(obj));
michael@0 31 NS_ENSURE_SUCCESS(rv, rv);
michael@0 32
michael@0 33 JS::RootedValue wrappedVal(aCx);
michael@0 34 rv = nsContentUtils::WrapNative(aCx, obj, &wrappedVal);
michael@0 35 NS_ENSURE_SUCCESS(rv, rv);
michael@0 36
michael@0 37 if (!JS_SetElement(aCx, arrayObj, index, wrappedVal)) {
michael@0 38 NS_WARNING("JS_SetElement failed!");
michael@0 39 return NS_ERROR_FAILURE;
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 if (!JS_FreezeObject(aCx, arrayObj)) {
michael@0 44 NS_WARNING("JS_FreezeObject failed!");
michael@0 45 return NS_ERROR_FAILURE;
michael@0 46 }
michael@0 47
michael@0 48 *aResultArray = arrayObj;
michael@0 49 return NS_OK;
michael@0 50 }
michael@0 51
michael@0 52 template <>
michael@0 53 inline nsresult
michael@0 54 nsTArrayToJSArray<nsString>(JSContext* aCx,
michael@0 55 const nsTArray<nsString>& aSourceArray,
michael@0 56 JSObject** aResultArray)
michael@0 57 {
michael@0 58 MOZ_ASSERT(aCx);
michael@0 59
michael@0 60 JS::Rooted<JSObject*> arrayObj(aCx,
michael@0 61 JS_NewArrayObject(aCx, aSourceArray.Length()));
michael@0 62 if (!arrayObj) {
michael@0 63 NS_WARNING("JS_NewArrayObject failed!");
michael@0 64 return NS_ERROR_OUT_OF_MEMORY;
michael@0 65 }
michael@0 66
michael@0 67 JS::Rooted<JSString*> s(aCx);
michael@0 68 for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
michael@0 69 s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(),
michael@0 70 aSourceArray[index].Length());
michael@0 71
michael@0 72 if(!s) {
michael@0 73 NS_WARNING("Memory allocation error!");
michael@0 74 return NS_ERROR_OUT_OF_MEMORY;
michael@0 75 }
michael@0 76
michael@0 77 if (!JS_SetElement(aCx, arrayObj, index, s)) {
michael@0 78 NS_WARNING("JS_SetElement failed!");
michael@0 79 return NS_ERROR_FAILURE;
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 if (!JS_FreezeObject(aCx, arrayObj)) {
michael@0 84 NS_WARNING("JS_FreezeObject failed!");
michael@0 85 return NS_ERROR_FAILURE;
michael@0 86 }
michael@0 87
michael@0 88 *aResultArray = arrayObj;
michael@0 89 return NS_OK;
michael@0 90 }
michael@0 91
michael@0 92 #endif /* __NSTARRAYHELPERS_H__ */

mercurial