forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSMapImpl.cpp
More file actions
204 lines (172 loc) · 6.33 KB
/
JSMapImpl.cpp
File metadata and controls
204 lines (172 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/JSMapImpl.h"
#include "hermes/VM/BuildMetadata.h"
#include "hermes/VM/Operations.h"
#include "llvh/Support/Debug.h"
#define DEBUG_TYPE "serialize"
namespace hermes {
namespace vm {
//===----------------------------------------------------------------------===//
// class JSMapImpl
template <CellKind C>
void JSMapImpl<C>::MapOrSetBuildMeta(
const GCCell *cell,
Metadata::Builder &mb) {
mb.addJSObjectOverlapSlots(JSObject::numOverlapSlots<JSMapImpl<C>>());
ObjectBuildMeta(cell, mb);
const auto *self = static_cast<const JSMapImpl<C> *>(cell);
mb.addField("storage", &self->storage_);
}
void MapBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
JSMapImpl<CellKind::MapKind>::MapOrSetBuildMeta(cell, mb);
mb.setVTable(&JSMap::vt.base);
}
void SetBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
JSMapImpl<CellKind::SetKind>::MapOrSetBuildMeta(cell, mb);
mb.setVTable(&JSSet::vt.base);
}
#ifdef HERMESVM_SERIALIZE
template <CellKind C>
void JSMapImpl<C>::serializeMapOrSetImpl(Serializer &s, const GCCell *cell) {
auto *self = vmcast<const JSMapImpl<C>>(cell);
JSObject::serializeObjectImpl(
s, cell, JSObject::numOverlapSlots<JSMapImpl<C>>());
s.writeRelocation(self->storage_.get(s.getRuntime()));
}
template <CellKind C>
JSMapImpl<C>::JSMapImpl(Deserializer &d, const VTable *vt) : JSObject(d, vt) {
d.readRelocation(&storage_, RelocationKind::GCPointer);
}
void MapSerialize(Serializer &s, const GCCell *cell) {
JSMap::serializeMapOrSetImpl(s, cell);
s.endObject(cell);
}
void SetSerialize(Serializer &s, const GCCell *cell) {
JSSet::serializeMapOrSetImpl(s, cell);
s.endObject(cell);
}
void MapDeserialize(Deserializer &d, CellKind kind) {
assert(kind == CellKind::MapKind && "Expected Map");
auto *cell = d.getRuntime()->makeAFixed<JSMap>(d, &JSMap::vt.base);
d.endObject(cell);
}
void SetDeserialize(Deserializer &d, CellKind kind) {
assert(kind == CellKind::SetKind && "Expected Set");
auto *cell = d.getRuntime()->makeAFixed<JSSet>(d, &JSSet::vt.base);
d.endObject(cell);
}
#endif
template <CellKind C>
const ObjectVTable JSMapImpl<C>::vt{
VTable(C, cellSize<JSMapImpl<C>>()),
JSMapImpl::_getOwnIndexedRangeImpl,
JSMapImpl::_haveOwnIndexedImpl,
JSMapImpl::_getOwnIndexedPropertyFlagsImpl,
JSMapImpl::_getOwnIndexedImpl,
JSMapImpl::_setOwnIndexedImpl,
JSMapImpl::_deleteOwnIndexedImpl,
JSMapImpl::_checkAllOwnIndexedImpl,
};
template <CellKind C>
PseudoHandle<JSMapImpl<C>> JSMapImpl<C>::create(
Runtime *runtime,
Handle<JSObject> parentHandle) {
auto *cell = runtime->makeAFixed<JSMapImpl>(
runtime,
parentHandle,
runtime->getHiddenClassForPrototype(
*parentHandle, numOverlapSlots<JSMapImpl>()));
return JSObjectInit::initToPseudoHandle(runtime, cell);
}
template class JSMapImpl<CellKind::SetKind>;
template class JSMapImpl<CellKind::MapKind>;
//===----------------------------------------------------------------------===//
// class JSSetIterator
template <CellKind C>
void JSMapIteratorImpl<C>::MapOrSetIteratorBuildMeta(
const GCCell *cell,
Metadata::Builder &mb) {
mb.addJSObjectOverlapSlots(JSObject::numOverlapSlots<JSMapIteratorImpl<C>>());
ObjectBuildMeta(cell, mb);
const auto *self = static_cast<const JSMapIteratorImpl<C> *>(cell);
mb.addField("data", &self->data_);
mb.addField("itr", &self->itr_);
}
void MapIteratorBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
JSMapIteratorImpl<CellKind::MapIteratorKind>::MapOrSetIteratorBuildMeta(
cell, mb);
mb.setVTable(&JSMapIterator::vt.base);
}
void SetIteratorBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
JSMapIteratorImpl<CellKind::SetIteratorKind>::MapOrSetIteratorBuildMeta(
cell, mb);
mb.setVTable(&JSSetIterator::vt.base);
}
#ifdef HERMESVM_SERIALIZE
template <CellKind C>
void serializeMapOrSetIteratorImpl(Serializer &s, const GCCell *cell) {
auto *self = vmcast<const JSMapIteratorImpl<C>>(cell);
JSObject::serializeObjectImpl(
s, cell, JSObject::numOverlapSlots<JSMapIteratorImpl<C>>());
s.writeRelocation(self->data_.get(s.getRuntime()));
s.writeRelocation(self->itr_.get(s.getRuntime()));
s.writeInt<uint8_t>((uint8_t)self->iterationKind_);
s.writeInt<uint8_t>(self->iterationFinished_);
}
template <CellKind C>
JSMapIteratorImpl<C>::JSMapIteratorImpl(Deserializer &d)
: JSObject(d, &vt.base) {
d.readRelocation(&data_, RelocationKind::GCPointer);
d.readRelocation(&itr_, RelocationKind::GCPointer);
iterationKind_ = (IterationKind)d.readInt<uint8_t>();
iterationFinished_ = d.readInt<uint8_t>();
}
void MapIteratorSerialize(Serializer &s, const GCCell *cell) {
serializeMapOrSetIteratorImpl<CellKind::MapIteratorKind>(s, cell);
s.endObject(cell);
}
void SetIteratorSerialize(Serializer &s, const GCCell *cell) {
serializeMapOrSetIteratorImpl<CellKind::SetIteratorKind>(s, cell);
s.endObject(cell);
}
void MapIteratorDeserialize(Deserializer &d, CellKind kind) {
auto *cell = d.getRuntime()->makeAFixed<JSMapIterator>(d);
d.endObject(cell);
}
void SetIteratorDeserialize(Deserializer &d, CellKind kind) {
auto *cell = d.getRuntime()->makeAFixed<JSSetIterator>(d);
d.endObject(cell);
}
#endif
template <CellKind C>
const ObjectVTable JSMapIteratorImpl<C>::vt = {
VTable(C, cellSize<JSMapIteratorImpl<C>>()),
JSMapIteratorImpl::_getOwnIndexedRangeImpl,
JSMapIteratorImpl::_haveOwnIndexedImpl,
JSMapIteratorImpl::_getOwnIndexedPropertyFlagsImpl,
JSMapIteratorImpl::_getOwnIndexedImpl,
JSMapIteratorImpl::_setOwnIndexedImpl,
JSMapIteratorImpl::_deleteOwnIndexedImpl,
JSMapIteratorImpl::_checkAllOwnIndexedImpl,
};
template <CellKind C>
PseudoHandle<JSMapIteratorImpl<C>> JSMapIteratorImpl<C>::create(
Runtime *runtime,
Handle<JSObject> prototype) {
auto *cell = runtime->makeAFixed<JSMapIteratorImpl<C>>(
runtime,
prototype,
runtime->getHiddenClassForPrototype(
*prototype, numOverlapSlots<JSMapIteratorImpl>()));
return JSObjectInit::initToPseudoHandle(runtime, cell);
}
template class JSMapIteratorImpl<CellKind::MapIteratorKind>;
template class JSMapIteratorImpl<CellKind::SetIteratorKind>;
} // namespace vm
} // namespace hermes
#undef DEBUG_TYPE