2012-09-28 10:43:49 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-03-28 08:53:56 -07:00
|
|
|
#ifndef __NSTARRAYHELPERS_H__
|
2012-09-28 10:43:49 -07:00
|
|
|
#define __NSTARRAYHELPERS_H__
|
|
|
|
|
2013-08-13 23:56:21 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2012-03-28 08:53:56 -07:00
|
|
|
template <class T>
|
|
|
|
inline nsresult
|
|
|
|
nsTArrayToJSArray(JSContext* aCx, const nsTArray<T>& aSourceArray,
|
|
|
|
JSObject** aResultArray)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2013-05-20 05:40:06 -07:00
|
|
|
JS::Rooted<JSObject*> arrayObj(aCx,
|
2014-02-12 02:50:46 -08:00
|
|
|
JS_NewArrayObject(aCx, aSourceArray.Length()));
|
2012-03-28 08:53:56 -07:00
|
|
|
if (!arrayObj) {
|
|
|
|
NS_WARNING("JS_NewArrayObject failed!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
|
2012-03-28 08:53:56 -07:00
|
|
|
nsCOMPtr<nsISupports> obj;
|
2012-09-28 10:43:49 -07:00
|
|
|
nsresult rv = aSourceArray[index]->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(obj));
|
2012-03-28 08:53:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-10-11 22:02:39 -07:00
|
|
|
JS::RootedValue wrappedVal(aCx);
|
2014-04-09 21:58:41 -07:00
|
|
|
rv = nsContentUtils::WrapNative(aCx, obj, &wrappedVal);
|
2012-03-28 08:53:56 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-01-25 01:31:17 -08:00
|
|
|
if (!JS_SetElement(aCx, arrayObj, index, wrappedVal)) {
|
2012-03-28 08:53:56 -07:00
|
|
|
NS_WARNING("JS_SetElement failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_FreezeObject(aCx, arrayObj)) {
|
|
|
|
NS_WARNING("JS_FreezeObject failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResultArray = arrayObj;
|
|
|
|
return NS_OK;
|
2012-09-28 10:43:49 -07:00
|
|
|
}
|
|
|
|
|
2012-10-18 06:20:54 -07:00
|
|
|
template <>
|
|
|
|
inline nsresult
|
|
|
|
nsTArrayToJSArray<nsString>(JSContext* aCx,
|
|
|
|
const nsTArray<nsString>& aSourceArray,
|
|
|
|
JSObject** aResultArray)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2013-05-20 05:40:06 -07:00
|
|
|
JS::Rooted<JSObject*> arrayObj(aCx,
|
2014-02-12 02:50:46 -08:00
|
|
|
JS_NewArrayObject(aCx, aSourceArray.Length()));
|
2012-10-18 06:20:54 -07:00
|
|
|
if (!arrayObj) {
|
|
|
|
NS_WARNING("JS_NewArrayObject failed!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:31:17 -08:00
|
|
|
JS::Rooted<JSString*> s(aCx);
|
2012-10-18 06:20:54 -07:00
|
|
|
for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
|
2014-01-25 01:31:17 -08:00
|
|
|
s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(),
|
|
|
|
aSourceArray[index].Length());
|
2012-10-18 06:20:54 -07:00
|
|
|
|
|
|
|
if(!s) {
|
|
|
|
NS_WARNING("Memory allocation error!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:31:17 -08:00
|
|
|
if (!JS_SetElement(aCx, arrayObj, index, s)) {
|
2012-10-18 06:20:54 -07:00
|
|
|
NS_WARNING("JS_SetElement failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_FreezeObject(aCx, arrayObj)) {
|
|
|
|
NS_WARNING("JS_FreezeObject failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResultArray = arrayObj;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-28 10:43:49 -07:00
|
|
|
#endif /* __NSTARRAYHELPERS_H__ */
|