forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSRegExpStringIterator.cpp
More file actions
186 lines (167 loc) · 6.56 KB
/
Copy pathJSRegExpStringIterator.cpp
File metadata and controls
186 lines (167 loc) · 6.56 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
/*
* 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/JSRegExpStringIterator.h"
#include "hermes/VM/BuildMetadata.h"
#include "hermes/VM/StringPrimitive.h"
#include "JSLib/JSLibInternal.h"
#include "llvh/Support/Debug.h"
#define DEBUG_TYPE "serialize"
namespace hermes {
namespace vm {
//===----------------------------------------------------------------------===//
// class JSRegExpStringIterator
const ObjectVTable JSRegExpStringIterator::vt{
VTable(
CellKind::RegExpStringIteratorKind,
cellSize<JSRegExpStringIterator>()),
JSRegExpStringIterator::_getOwnIndexedRangeImpl,
JSRegExpStringIterator::_haveOwnIndexedImpl,
JSRegExpStringIterator::_getOwnIndexedPropertyFlagsImpl,
JSRegExpStringIterator::_getOwnIndexedImpl,
JSRegExpStringIterator::_setOwnIndexedImpl,
JSRegExpStringIterator::_deleteOwnIndexedImpl,
JSRegExpStringIterator::_checkAllOwnIndexedImpl,
};
void RegExpStringIteratorBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
mb.addJSObjectOverlapSlots(
JSObject::numOverlapSlots<JSRegExpStringIterator>());
ObjectBuildMeta(cell, mb);
const auto *self = static_cast<const JSRegExpStringIterator *>(cell);
mb.setVTable(&JSRegExpStringIterator::vt.base);
mb.addField("iteratedRegExp", &self->iteratedRegExp_);
mb.addField("iteratedString", &self->iteratedString_);
}
#ifdef HERMESVM_SERIALIZE
JSRegExpStringIterator::JSRegExpStringIterator(Deserializer &d)
: JSObject(d, &vt.base) {
d.readRelocation(&iteratedRegExp_, RelocationKind::GCPointer);
d.readRelocation(&iteratedString_, RelocationKind::GCPointer);
global_ = d.readInt<bool>();
unicode_ = d.readInt<bool>();
done_ = d.readInt<bool>();
}
void RegExpStringIteratorSerialize(Serializer &s, const GCCell *cell) {
auto *self = vmcast<const JSRegExpStringIterator>(cell);
JSObject::serializeObjectImpl(
s, cell, JSObject::numOverlapSlots<JSRegExpStringIterator>());
s.writeRelocation(self->iteratedRegExp_.get(s.getRuntime()));
s.writeRelocation(self->iteratedString_.get(s.getRuntime()));
s.writeInt<bool>(self->global_);
s.writeInt<bool>(self->unicode_);
s.writeInt<bool>(self->done_);
s.endObject(cell);
}
void RegExpStringIteratorDeserialize(Deserializer &d, CellKind kind) {
assert(
kind == CellKind::RegExpStringIteratorKind &&
"Expected RegExpStringIterator");
auto *cell = d.getRuntime()->makeAFixed<JSRegExpStringIterator>(d);
d.endObject(cell);
}
#endif
/// ES11 21.2.5.8.1 CreateRegExpStringIterator ( R, S, global, fullUnicode )
PseudoHandle<JSRegExpStringIterator> JSRegExpStringIterator::create(
Runtime *runtime,
Handle<JSObject> R,
Handle<StringPrimitive> S,
bool global,
bool fullUnicode) {
auto proto =
Handle<JSObject>::vmcast(&runtime->regExpStringIteratorPrototype);
auto *cell = runtime->makeAFixed<JSRegExpStringIterator>(
runtime,
proto,
runtime->getHiddenClassForPrototype(
*proto, numOverlapSlots<JSRegExpStringIterator>()),
R,
S,
global,
fullUnicode);
return JSObjectInit::initToPseudoHandle(runtime, cell);
}
/// ES11 21.2.7.1.1 %RegExpStringIteratorPrototype%.next ( ) 4-11
CallResult<HermesValue> JSRegExpStringIterator::nextElement(
Handle<JSRegExpStringIterator> O,
Runtime *runtime) {
// 4. If O.[[Done]] is true, then
if (O->done_) {
// a. Return ! CreateIterResultObject(undefined, true).
return createIterResultObject(runtime, Runtime::getUndefinedValue(), true)
.getHermesValue();
}
// 5. Let R be O.[[IteratingRegExp]].
auto R = runtime->makeHandle(O->iteratedRegExp_);
// 6. Let S be O.[[IteratedString]].
auto S = runtime->makeHandle(O->iteratedString_);
// 7. Let global be O.[[Global]].
// 8. Let fullUnicode be O.[[Unicode]].
// we will just use JSRegExpStringIterator's fields for non pointer types.
// 9. Let match be ? RegExpExec(R, S).
auto matchRes = regExpExec(runtime, R, S);
if (LLVM_UNLIKELY(matchRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto match = matchRes.getValue();
// 10. If match is null, then
if (match.isNull()) {
// a. Set O.[[Done]] to true.
O->done_ = true;
// b. Return ! CreateIterResultObject(undefined, true).
return createIterResultObject(runtime, Runtime::getUndefinedValue(), true)
.getHermesValue();
} else {
// 11. Else,
auto matchObj = runtime->makeHandle<JSObject>(match);
// a. If global is true, then
if (O->global_) {
// i. Let matchStr be ? ToString(? Get(match, "0")).
Handle<> zeroHandle = HandleRootOwner::getZeroValue();
auto propRes = JSObject::getComputed_RJS(matchObj, runtime, zeroHandle);
if (LLVM_UNLIKELY(propRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto matchStrRes =
toString_RJS(runtime, runtime->makeHandle(std::move(*propRes)));
if (matchStrRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
auto matchStr = runtime->makeHandle(std::move(*matchStrRes));
// ii. If matchStr is the empty String, then
if (matchStr->getStringLength() == 0) {
// 1. Let thisIndex be ? ToLength(? Get(R, "lastIndex")).
auto lastIndexRes = runtime->getNamed(R, PropCacheID::RegExpLastIndex);
if (lastIndexRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
auto lastIndex = runtime->makeHandle(std::move(*lastIndexRes));
auto thisIndex = toLength(runtime, lastIndex);
if (thisIndex == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// 2. Let nextIndex be ! AdvanceStringIndex(S, thisIndex, fullUnicode).
double nextIndex = advanceStringIndex(
S.get(), thisIndex->getNumberAs<uint64_t>(), O->unicode_);
// 3. Perform ? Set(R, "lastIndex", nextIndex, true).
if (setLastIndex(R, runtime, nextIndex) == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
}
// iii. Return ! CreateIterResultObject(match, false).
return createIterResultObject(runtime, matchObj, false).getHermesValue();
}
// b. Else,
else {
// i. Set O.[[Done]] to true.
O->done_ = true;
// ii. Return ! CreateIterResultObject(match, false).
return createIterResultObject(runtime, matchObj, false).getHermesValue();
}
}
}
} // namespace vm
} // namespace hermes
#undef DEBUG_TYPE