2014-04-03 15:58:00 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-28 21:43:49 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2014-04-03 15:58:00 +04:00
|
|
|
* 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-09-28 21:43:49 +04:00
|
|
|
|
2012-03-28 19:53:56 +04:00
|
|
|
#ifndef __NSTARRAYHELPERS_H__
|
2012-09-28 21:43:49 +04:00
|
|
|
#define __NSTARRAYHELPERS_H__
|
|
|
|
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2012-03-28 19:53:56 +04:00
|
|
|
template <class T>
|
|
|
|
inline nsresult
|
|
|
|
nsTArrayToJSArray(JSContext* aCx, const nsTArray<T>& aSourceArray,
|
2014-06-02 20:40:13 +04:00
|
|
|
JS::MutableHandle<JSObject*> aResultArray)
|
2012-03-28 19:53:56 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2013-05-20 16:40:06 +04:00
|
|
|
JS::Rooted<JSObject*> arrayObj(aCx,
|
2014-02-12 14:50:46 +04:00
|
|
|
JS_NewArrayObject(aCx, aSourceArray.Length()));
|
2012-03-28 19:53:56 +04:00
|
|
|
if (!arrayObj) {
|
|
|
|
NS_WARNING("JS_NewArrayObject failed!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
|
2012-03-28 19:53:56 +04:00
|
|
|
nsCOMPtr<nsISupports> obj;
|
2012-09-28 21:43:49 +04:00
|
|
|
nsresult rv = aSourceArray[index]->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(obj));
|
2012-03-28 19:53:56 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-10-12 09:02:39 +04:00
|
|
|
JS::RootedValue wrappedVal(aCx);
|
2014-04-10 08:58:41 +04:00
|
|
|
rv = nsContentUtils::WrapNative(aCx, obj, &wrappedVal);
|
2012-03-28 19:53:56 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-03-02 16:31:15 +03:00
|
|
|
if (!JS_DefineElement(aCx, arrayObj, index, wrappedVal, JSPROP_ENUMERATE)) {
|
|
|
|
NS_WARNING("JS_DefineElement failed!");
|
2012-03-28 19:53:56 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_FreezeObject(aCx, arrayObj)) {
|
|
|
|
NS_WARNING("JS_FreezeObject failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-06-02 20:40:13 +04:00
|
|
|
aResultArray.set(arrayObj);
|
2012-03-28 19:53:56 +04:00
|
|
|
return NS_OK;
|
2012-09-28 21:43:49 +04:00
|
|
|
}
|
|
|
|
|
2012-10-18 17:20:54 +04:00
|
|
|
template <>
|
|
|
|
inline nsresult
|
|
|
|
nsTArrayToJSArray<nsString>(JSContext* aCx,
|
|
|
|
const nsTArray<nsString>& aSourceArray,
|
2014-06-02 20:40:13 +04:00
|
|
|
JS::MutableHandle<JSObject*> aResultArray)
|
2012-10-18 17:20:54 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2013-05-20 16:40:06 +04:00
|
|
|
JS::Rooted<JSObject*> arrayObj(aCx,
|
2014-02-12 14:50:46 +04:00
|
|
|
JS_NewArrayObject(aCx, aSourceArray.Length()));
|
2012-10-18 17:20:54 +04:00
|
|
|
if (!arrayObj) {
|
|
|
|
NS_WARNING("JS_NewArrayObject failed!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-01-25 13:31:17 +04:00
|
|
|
JS::Rooted<JSString*> s(aCx);
|
2012-10-18 17:20:54 +04:00
|
|
|
for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
|
2014-01-25 13:31:17 +04:00
|
|
|
s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(),
|
|
|
|
aSourceArray[index].Length());
|
2012-10-18 17:20:54 +04:00
|
|
|
|
|
|
|
if(!s) {
|
|
|
|
NS_WARNING("Memory allocation error!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:31:15 +03:00
|
|
|
if (!JS_DefineElement(aCx, arrayObj, index, s, JSPROP_ENUMERATE)) {
|
|
|
|
NS_WARNING("JS_DefineElement failed!");
|
2012-10-18 17:20:54 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_FreezeObject(aCx, arrayObj)) {
|
|
|
|
NS_WARNING("JS_FreezeObject failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-06-02 20:40:13 +04:00
|
|
|
aResultArray.set(arrayObj);
|
2012-10-18 17:20:54 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-28 21:43:49 +04:00
|
|
|
#endif /* __NSTARRAYHELPERS_H__ */
|