Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/nsTArrayHelpers.h
Line
Count
Source (jump to first uncovered line)
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
nsTArrayToJSArray(JSContext* aCx, const nsTArray<T>& aSourceArray,
17
                  JS::MutableHandle<JSObject*> aResultArray)
18
0
{
19
0
  MOZ_ASSERT(aCx);
20
0
21
0
  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
0
  }
27
0
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
0
33
0
    JS::RootedValue wrappedVal(aCx);
34
0
    rv = nsContentUtils::WrapNative(aCx, obj, &wrappedVal);
35
0
    NS_ENSURE_SUCCESS(rv, rv);
36
0
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
0
    }
41
0
  }
42
0
43
0
  if (!JS_FreezeObject(aCx, arrayObj)) {
44
0
    NS_WARNING("JS_FreezeObject failed!");
45
0
    return NS_ERROR_FAILURE;
46
0
  }
47
0
48
0
  aResultArray.set(arrayObj);
49
0
  return NS_OK;
50
0
}
51
52
template <>
53
inline nsresult
54
nsTArrayToJSArray<nsString>(JSContext* aCx,
55
                            const nsTArray<nsString>& aSourceArray,
56
                            JS::MutableHandle<JSObject*> aResultArray)
57
0
{
58
0
  MOZ_ASSERT(aCx);
59
0
60
0
  JS::Rooted<JSObject*> arrayObj(aCx,
61
0
    JS_NewArrayObject(aCx, aSourceArray.Length()));
62
0
  if (!arrayObj) {
63
0
    NS_WARNING("JS_NewArrayObject failed!");
64
0
    return NS_ERROR_OUT_OF_MEMORY;
65
0
  }
66
0
67
0
  JS::Rooted<JSString*> s(aCx);
68
0
  for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
69
0
    s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(),
70
0
                            aSourceArray[index].Length());
71
0
72
0
    if(!s) {
73
0
      NS_WARNING("Memory allocation error!");
74
0
      return NS_ERROR_OUT_OF_MEMORY;
75
0
    }
76
0
77
0
    if (!JS_DefineElement(aCx, arrayObj, index, s, JSPROP_ENUMERATE)) {
78
0
      NS_WARNING("JS_DefineElement failed!");
79
0
      return NS_ERROR_FAILURE;
80
0
    }
81
0
  }
82
0
83
0
  if (!JS_FreezeObject(aCx, arrayObj)) {
84
0
    NS_WARNING("JS_FreezeObject failed!");
85
0
    return NS_ERROR_FAILURE;
86
0
  }
87
0
88
0
  aResultArray.set(arrayObj);
89
0
  return NS_OK;
90
0
}
91
92
#endif /* __NSTARRAYHELPERS_H__ */