LCOV - code coverage report
Current view: top level - js/xpconnect/public - nsTArrayHelpers.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 21 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 1 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13