Source code

Revision control

Other Tools

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
*
4
* Copyright 2016 Mozilla Foundation
5
*
6
* Licensed under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
9
*
11
*
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
17
*/
18
19
#ifndef wasm_table_h
20
#define wasm_table_h
21
22
#include "gc/Policy.h"
23
#include "wasm/WasmCode.h"
24
25
namespace js {
26
namespace wasm {
27
28
// A Table is an indexable array of opaque values. Tables are first-class
29
// stateful objects exposed to WebAssembly. asm.js also uses Tables to represent
30
// its homogeneous function-pointer tables.
31
//
32
// A table of FuncRef holds FunctionTableElems, which are (code*,tls*) pairs,
33
// where the tls must be traced.
34
//
35
// A table of AnyRef holds JSObject pointers, which must be traced.
36
37
// TODO/AnyRef-boxing: With boxed immediates and strings, JSObject* is no longer
38
// the most appropriate representation for Cell::anyref.
39
STATIC_ASSERT_ANYREF_IS_JSOBJECT;
40
41
typedef GCVector<HeapPtr<JSObject*>, 0, SystemAllocPolicy> TableAnyRefVector;
42
43
class Table : public ShareableBase<Table> {
44
using InstanceSet =
45
JS::WeakCache<GCHashSet<WeakHeapPtrWasmInstanceObject,
46
MovableCellHasher<WeakHeapPtrWasmInstanceObject>,
47
SystemAllocPolicy>>;
48
using UniqueFuncRefArray = UniquePtr<FunctionTableElem[], JS::FreePolicy>;
49
50
WeakHeapPtrWasmTableObject maybeObject_;
51
InstanceSet observers_;
52
UniqueFuncRefArray functions_; // either functions_ has data
53
TableAnyRefVector objects_; // or objects_, but not both
54
const TableKind kind_;
55
uint32_t length_;
56
const Maybe<uint32_t> maximum_;
57
58
template <class>
59
friend struct js::MallocProvider;
60
Table(JSContext* cx, const TableDesc& td, HandleWasmTableObject maybeObject,
61
UniqueFuncRefArray functions);
62
Table(JSContext* cx, const TableDesc& td, HandleWasmTableObject maybeObject,
63
TableAnyRefVector&& objects);
64
65
void tracePrivate(JSTracer* trc);
66
friend class js::WasmTableObject;
67
68
public:
69
static RefPtr<Table> create(JSContext* cx, const TableDesc& desc,
70
HandleWasmTableObject maybeObject);
71
void trace(JSTracer* trc);
72
73
TableKind kind() const { return kind_; }
74
bool isFunction() const {
75
return kind_ == TableKind::FuncRef || kind_ == TableKind::AsmJS;
76
}
77
uint32_t length() const { return length_; }
78
Maybe<uint32_t> maximum() const { return maximum_; }
79
80
// Only for function values. Raw pointer to the table.
81
uint8_t* functionBase() const;
82
83
// set/get/fillFuncRef is allowed only on table-of-funcref.
84
// get/fillAnyRef is allowed only on table-of-anyref.
85
// setNull is allowed on either.
86
87
const FunctionTableElem& getFuncRef(uint32_t index) const;
88
bool getFuncRef(JSContext* cx, uint32_t index,
89
MutableHandleFunction fun) const;
90
void setFuncRef(uint32_t index, void* code, const Instance* instance);
91
void fillFuncRef(uint32_t index, uint32_t fillCount, FuncRef ref,
92
JSContext* cx);
93
94
AnyRef getAnyRef(uint32_t index) const;
95
void fillAnyRef(uint32_t index, uint32_t fillCount, AnyRef ref);
96
97
void setNull(uint32_t index);
98
99
// Copy entry from |srcTable| at |srcIndex| to this table at |dstIndex|. Used
100
// by table.copy. May OOM if it needs to box up a function during an upcast.
101
bool copy(const Table& srcTable, uint32_t dstIndex, uint32_t srcIndex);
102
103
// grow() returns (uint32_t)-1 if it could not grow.
104
uint32_t grow(uint32_t delta);
105
bool movingGrowable() const;
106
bool addMovingGrowObserver(JSContext* cx, WasmInstanceObject* instance);
107
108
// about:memory reporting:
109
110
size_t sizeOfExcludingThis(MallocSizeOf mallocSizeOf) const;
111
112
size_t gcMallocBytes() const;
113
};
114
115
typedef RefPtr<Table> SharedTable;
116
typedef Vector<SharedTable, 0, SystemAllocPolicy> SharedTableVector;
117
118
} // namespace wasm
119
} // namespace js
120
121
#endif // wasm_table_h