| 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 |
/** GC-safe representations of consecutive JS::Value in memory. */ |
| 8 |
|
| 9 |
#ifndef js_ValueArray_h |
| 10 |
#define js_ValueArray_h |
| 11 |
|
| 12 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 13 |
#include "mozilla/Attributes.h" // MOZ_IMPLICIT, MOZ_RAII |
| 14 |
|
| 15 |
#include <stdint.h> // size_t |
| 16 |
|
| 17 |
#include "js/CallArgs.h" // JS::CallArgs |
| 18 |
#include "js/GCVector.h" // JS::RootedVector |
| 19 |
#include "js/RootingAPI.h" // JS::AutoGCRooter, JS::{,Mutable}Handle |
| 20 |
#include "js/Value.h" // JS::Value |
| 21 |
|
| 22 |
namespace js { |
| 23 |
JS_PUBLIC_API void TraceValueArray(JSTracer* trc, size_t length, |
| 24 |
JS::Value* elements); |
| 25 |
} // namespace js |
| 26 |
|
| 27 |
namespace JS { |
| 28 |
|
| 29 |
/* A fixed-size array of values, for use inside Rooted<>. */ |
| 30 |
template <size_t N> |
| 31 |
struct ValueArray { |
| 32 |
Value elements[N]; |
| 33 |
void trace(JSTracer* trc) { js::TraceValueArray(trc, N, elements); } |
| 34 |
}; |
| 35 |
|
| 36 |
/** RootedValueArray roots an internal fixed-size array of Values. */ |
| 37 |
template <size_t N> |
| 38 |
using RootedValueArray = Rooted<ValueArray<N>>; |
| 39 |
|
| 40 |
/** |
| 41 |
* A generic handle to an array of rooted values. |
| 42 |
* |
| 43 |
* The rooted array refernced can take several forms, therfore this is not the |
| 44 |
* same as Handle<js::ValueArray>. |
| 45 |
*/ |
| 46 |
class HandleValueArray { |
| 47 |
const size_t length_; |
| 48 |
const Value* const elements_; |
| 49 |
|
| 50 |
HandleValueArray(size_t len, const Value* elements) |
| 51 |
: length_(len), elements_(elements) {} |
| 52 |
|
| 53 |
public: |
| 54 |
explicit HandleValueArray(Handle<Value> value) |
| 55 |
: length_(1), elements_(value.address()) {} |
| 56 |
|
| 57 |
MOZ_IMPLICIT HandleValueArray(const RootedVector<Value>& values) |
| 58 |
: length_(values.length()), elements_(values.begin()) {} |
| 59 |
|
| 60 |
MOZ_IMPLICIT HandleValueArray(const PersistentRootedVector<Value>& values) |
| 61 |
: length_(values.length()), elements_(values.begin()) {} |
| 62 |
|
| 63 |
template <size_t N> |
| 64 |
MOZ_IMPLICIT HandleValueArray(const RootedValueArray<N>& values) |
| 65 |
: length_(N), elements_(values.begin()) {} |
| 66 |
|
| 67 |
/** CallArgs must already be rooted somewhere up the stack. */ |
| 68 |
MOZ_IMPLICIT HandleValueArray(const JS::CallArgs& args) |
| 69 |
: length_(args.length()), elements_(args.array()) {} |
| 70 |
|
| 71 |
/** Use with care! Only call this if the data is guaranteed to be marked. */ |
| 72 |
static HandleValueArray fromMarkedLocation(size_t len, |
| 73 |
const Value* elements) { |
| 74 |
return HandleValueArray(len, elements); |
| 75 |
} |
| 76 |
|
| 77 |
static HandleValueArray subarray(const HandleValueArray& values, |
| 78 |
size_t startIndex, size_t len) { |
| 79 |
MOZ_ASSERT(startIndex + len <= values.length()); |
| 80 |
return HandleValueArray(len, values.begin() + startIndex); |
| 81 |
} |
| 82 |
|
| 83 |
static HandleValueArray empty() { return HandleValueArray(0, nullptr); } |
| 84 |
|
| 85 |
size_t length() const { return length_; } |
| 86 |
const Value* begin() const { return elements_; } |
| 87 |
|
| 88 |
Handle<Value> operator[](size_t i) const { |
| 89 |
MOZ_ASSERT(i < length_); |
| 90 |
return Handle<Value>::fromMarkedLocation(&elements_[i]); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
} // namespace JS |
| 95 |
|
| 96 |
namespace js { |
| 97 |
|
| 98 |
template <size_t N, typename Container> |
| 99 |
class WrappedPtrOperations<JS::ValueArray<N>, Container> { |
| 100 |
const JS::ValueArray<N>& array() const { |
| 101 |
return static_cast<const Container*>(this)->get(); |
| 102 |
} |
| 103 |
|
| 104 |
public: |
| 105 |
size_t length() const { return N; } |
| 106 |
const JS::Value* begin() const { return array().elements; } |
| 107 |
|
| 108 |
JS::HandleValue operator[](size_t i) const { |
| 109 |
MOZ_ASSERT(i < N); |
| 110 |
return JS::HandleValue::fromMarkedLocation(&array().elements[i]); |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
template <size_t N, typename Container> |
| 115 |
class MutableWrappedPtrOperations<JS::ValueArray<N>, Container> |
| 116 |
: public WrappedPtrOperations<JS::ValueArray<N>, Container> { |
| 117 |
using Base = WrappedPtrOperations<JS::ValueArray<N>, Container>; |
| 118 |
JS::ValueArray<N>& array() { return static_cast<Container*>(this)->get(); } |
| 119 |
|
| 120 |
public: |
| 121 |
using Base::begin; |
| 122 |
JS::Value* begin() { return array().elements; } |
| 123 |
|
| 124 |
using Base::operator[]; |
| 125 |
JS::MutableHandleValue operator[](size_t i) { |
| 126 |
MOZ_ASSERT(i < N); |
| 127 |
return JS::MutableHandleValue::fromMarkedLocation(&array().elements[i]); |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace js |
| 132 |
|
| 133 |
#endif // js_ValueArray_h |