forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter-slowpaths.cpp
More file actions
339 lines (306 loc) · 12.3 KB
/
Interpreter-slowpaths.cpp
File metadata and controls
339 lines (306 loc) · 12.3 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* 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.
*/
#define DEBUG_TYPE "vm"
#include "JSLib/JSLibInternal.h"
#include "hermes/VM/Casting.h"
#include "hermes/VM/Interpreter.h"
#include "hermes/VM/PropertyAccessor.h"
#include "hermes/VM/Runtime-inline.h"
#include "hermes/VM/StackFrame-inline.h"
#include "hermes/VM/StringPrimitive.h"
#include "Interpreter-internal.h"
using namespace hermes::inst;
namespace hermes {
namespace vm {
void Interpreter::saveGenerator(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const Inst *resumeIP) {
auto *innerFn = vmcast<GeneratorInnerFunction>(FRAME.getCalleeClosure());
innerFn->saveStack(runtime);
innerFn->setNextIP(resumeIP);
innerFn->setState(GeneratorInnerFunction::State::SuspendedYield);
}
ExecutionStatus Interpreter::caseDirectEval(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const Inst *ip) {
auto *result = &O1REG(DirectEval);
auto *input = &O2REG(DirectEval);
GCScopeMarkerRAII gcMarker{runtime};
// Check to see if global eval() has been overriden, in which case call it as
// as normal function.
auto global = runtime->getGlobal();
auto existingEval = global->getNamed_RJS(
global, runtime, Predefined::getSymbolID(Predefined::eval));
if (LLVM_UNLIKELY(existingEval == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto *nativeExistingEval = dyn_vmcast<NativeFunction>(existingEval->get());
if (LLVM_UNLIKELY(
!nativeExistingEval ||
nativeExistingEval->getFunctionPtr() != hermes::vm::eval)) {
if (auto *existingEvalCallable =
dyn_vmcast<Callable>(existingEval->get())) {
auto evalRes = existingEvalCallable->executeCall1(
runtime->makeHandle<Callable>(existingEvalCallable),
runtime,
Runtime::getUndefinedValue(),
*input);
if (LLVM_UNLIKELY(evalRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
*result = evalRes->get();
evalRes->invalidate();
return ExecutionStatus::RETURNED;
}
return runtime->raiseTypeErrorForValue(
runtime->makeHandle(std::move(*existingEval)), " is not a function");
}
if (!input->isString()) {
*result = *input;
return ExecutionStatus::RETURNED;
}
// Create a dummy scope, so that the local eval executes in its own scope
// (as per the spec for strict callers, which is the only thing we support).
ScopeChain scopeChain{};
scopeChain.functions.emplace_back();
auto cr = vm::directEval(
runtime, Handle<StringPrimitive>::vmcast(input), scopeChain, false);
if (cr == ExecutionStatus::EXCEPTION)
return ExecutionStatus::EXCEPTION;
*result = *cr;
return ExecutionStatus::RETURNED;
}
ExecutionStatus Interpreter::casePutOwnByVal(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const Inst *ip) {
return JSObject::defineOwnComputed(
Handle<JSObject>::vmcast(&O1REG(PutOwnByVal)),
runtime,
Handle<>(&O3REG(PutOwnByVal)),
ip->iPutOwnByVal.op4
? DefinePropertyFlags::getDefaultNewPropertyFlags()
: DefinePropertyFlags::getNewNonEnumerableFlags(),
Handle<>(&O2REG(PutOwnByVal)))
.getStatus();
}
ExecutionStatus Interpreter::casePutOwnGetterSetterByVal(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const inst::Inst *ip) {
DefinePropertyFlags dpFlags{};
dpFlags.setConfigurable = 1;
dpFlags.configurable = 1;
dpFlags.setEnumerable = 1;
dpFlags.enumerable = ip->iPutOwnGetterSetterByVal.op5;
MutableHandle<Callable> getter(runtime);
MutableHandle<Callable> setter(runtime);
if (LLVM_LIKELY(!O3REG(PutOwnGetterSetterByVal).isUndefined())) {
dpFlags.setGetter = 1;
getter = vmcast<Callable>(O3REG(PutOwnGetterSetterByVal));
}
if (LLVM_LIKELY(!O4REG(PutOwnGetterSetterByVal).isUndefined())) {
dpFlags.setSetter = 1;
setter = vmcast<Callable>(O4REG(PutOwnGetterSetterByVal));
}
assert(
(dpFlags.setSetter || dpFlags.setGetter) &&
"No accessor set in PutOwnGetterSetterByVal");
auto res = PropertyAccessor::create(runtime, getter, setter);
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION))
return ExecutionStatus::EXCEPTION;
auto accessor = runtime->makeHandle<PropertyAccessor>(*res);
return JSObject::defineOwnComputed(
Handle<JSObject>::vmcast(&O1REG(PutOwnGetterSetterByVal)),
runtime,
Handle<>(&O2REG(PutOwnGetterSetterByVal)),
dpFlags,
accessor)
.getStatus();
}
ExecutionStatus Interpreter::caseIteratorBegin(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const inst::Inst *ip) {
if (LLVM_LIKELY(vmisa<JSArray>(O2REG(IteratorBegin)))) {
// Attempt to get the fast path for array iteration.
NamedPropertyDescriptor desc;
JSObject *propObj = JSObject::getNamedDescriptorPredefined(
Handle<JSArray>::vmcast(&O2REG(IteratorBegin)),
runtime,
Predefined::SymbolIterator,
desc);
if (LLVM_LIKELY(propObj)) {
auto slotValueRes = JSObject::getNamedSlotValue(
createPseudoHandle(propObj), runtime, desc);
if (LLVM_UNLIKELY(slotValueRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
PseudoHandle<> slotValue = std::move(*slotValueRes);
if (LLVM_LIKELY(
slotValue->getRaw() == runtime->arrayPrototypeValues.getRaw())) {
O1REG(IteratorBegin) = HermesValue::encodeNumberValue(0);
return ExecutionStatus::RETURNED;
}
}
}
GCScopeMarkerRAII marker{runtime};
CallResult<IteratorRecord> iterRecord =
getIterator(runtime, Handle<>(&O2REG(IteratorBegin)));
if (LLVM_UNLIKELY(iterRecord == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
O1REG(IteratorBegin) = iterRecord->iterator.getHermesValue();
O2REG(IteratorBegin) = iterRecord->nextMethod.getHermesValue();
return ExecutionStatus::RETURNED;
}
ExecutionStatus Interpreter::caseIteratorNext(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const inst::Inst *ip) {
if (LLVM_LIKELY(O2REG(IteratorNext).isNumber())) {
JSArray::size_type i =
O2REG(IteratorNext).getNumberAs<JSArray::size_type>();
if (i >=
JSArray::getLength(vmcast<JSArray>(O3REG(IteratorNext)), runtime)) {
// Finished iterating the array, stop.
O2REG(IteratorNext) = HermesValue::encodeUndefinedValue();
O1REG(IteratorNext) = HermesValue::encodeUndefinedValue();
return ExecutionStatus::RETURNED;
}
Handle<JSArray> arr = Handle<JSArray>::vmcast(&O3REG(IteratorNext));
{
// Fast path: look up the property in indexed storage.
// Runs when there is no hole and a regular non-accessor property exists
// at the current index, because those are the only properties stored
// in indexed storage.
// If there is another kind of property we have to call getComputed_RJS.
// No need to check the fastIndexProperties flag because the indexed
// storage would be deleted and at() would return empty in that case.
NoAllocScope noAlloc{runtime};
HermesValue value = arr->at(runtime, i);
if (LLVM_LIKELY(!value.isEmpty())) {
O1REG(IteratorNext) = value;
O2REG(IteratorNext) = HermesValue::encodeNumberValue(i + 1);
return ExecutionStatus::RETURNED;
}
}
// Slow path, just run the full getComputedPropertyValue_RJS path.
GCScopeMarkerRAII marker{runtime};
Handle<> idxHandle{&O2REG(IteratorNext)};
CallResult<PseudoHandle<>> valueRes =
JSObject::getComputed_RJS(arr, runtime, idxHandle);
if (LLVM_UNLIKELY(valueRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
O1REG(IteratorNext) = valueRes->get();
O2REG(IteratorNext) = HermesValue::encodeNumberValue(i + 1);
return ExecutionStatus::RETURNED;
}
if (LLVM_UNLIKELY(O2REG(IteratorNext).isUndefined())) {
// In all current use cases of IteratorNext, we check and branch away
// from IteratorNext in the case that iterStorage was set to undefined
// (which indicates completion of iteration).
// If we introduce a use case which allows calling IteratorNext,
// then this assert can be removed. For now, this branch just returned
// undefined in NDEBUG mode.
assert(false && "IteratorNext called on completed iterator");
O1REG(IteratorNext) = HermesValue::encodeUndefinedValue();
return ExecutionStatus::RETURNED;
}
GCScopeMarkerRAII marker{runtime};
IteratorRecord iterRecord{
Handle<JSObject>::vmcast(&O2REG(IteratorNext)),
Handle<Callable>::vmcast(&O3REG(IteratorNext))};
CallResult<PseudoHandle<JSObject>> resultObjRes =
iteratorNext(runtime, iterRecord, llvh::None);
if (LLVM_UNLIKELY(resultObjRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> resultObj = runtime->makeHandle(std::move(*resultObjRes));
CallResult<PseudoHandle<>> doneRes = JSObject::getNamed_RJS(
resultObj, runtime, Predefined::getSymbolID(Predefined::done));
if (LLVM_UNLIKELY(doneRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
if (toBoolean(doneRes->get())) {
// Done with iteration. Clear the iterator so that subsequent
// instructions do not call next() or return().
O2REG(IteratorNext) = HermesValue::encodeUndefinedValue();
O1REG(IteratorNext) = HermesValue::encodeUndefinedValue();
} else {
// Not done iterating, so get the `value` property and store it
// as the result.
CallResult<PseudoHandle<>> propRes = JSObject::getNamed_RJS(
resultObj, runtime, Predefined::getSymbolID(Predefined::value));
if (LLVM_UNLIKELY(propRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
O1REG(IteratorNext) = propRes->get();
propRes->invalidate();
}
return ExecutionStatus::RETURNED;
}
ExecutionStatus Interpreter::caseGetPNameList(
Runtime *runtime,
PinnedHermesValue *frameRegs,
const Inst *ip) {
if (O2REG(GetPNameList).isUndefined() || O2REG(GetPNameList).isNull()) {
// Set the iterator to be undefined value.
O1REG(GetPNameList) = HermesValue::encodeUndefinedValue();
return ExecutionStatus::RETURNED;
}
// Convert to object and store it back to the register.
auto res = toObject(runtime, Handle<>(&O2REG(GetPNameList)));
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
O2REG(GetPNameList) = res.getValue();
auto obj = runtime->makeMutableHandle(vmcast<JSObject>(res.getValue()));
uint32_t beginIndex;
uint32_t endIndex;
auto cr = getForInPropertyNames(runtime, obj, beginIndex, endIndex);
if (cr == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
auto arr = *cr;
O1REG(GetPNameList) = arr.getHermesValue();
O3REG(GetPNameList) = HermesValue::encodeNumberValue(beginIndex);
O4REG(GetPNameList) = HermesValue::encodeNumberValue(endIndex);
return ExecutionStatus::RETURNED;
}
ExecutionStatus Interpreter::implCallBuiltin(
Runtime *runtime,
PinnedHermesValue *frameRegs,
CodeBlock *curCodeBlock,
uint32_t op3) {
const Inst *ip = runtime->getCurrentIP();
uint8_t methodIndex = ip->iCallBuiltin.op2;
Callable *callable = runtime->getBuiltinCallable(methodIndex);
assert(
isNativeBuiltin(methodIndex) &&
"CallBuiltin must take a native builtin.");
NativeFunction *nf = vmcast<NativeFunction>(callable);
auto newFrame = StackFramePtr::initFrame(
runtime->stackPointer_, FRAME, ip, curCodeBlock, op3 - 1, nf, false);
// "thisArg" is implicitly assumed to "undefined".
newFrame.getThisArgRef() = HermesValue::encodeUndefinedValue();
SLOW_DEBUG(dumpCallArguments(llvh::dbgs(), runtime, newFrame));
auto resPH = NativeFunction::_nativeCall(nf, runtime);
if (LLVM_UNLIKELY(resPH == ExecutionStatus::EXCEPTION))
return ExecutionStatus::EXCEPTION;
O1REG(CallBuiltin) = std::move(resPH->get());
SLOW_DEBUG(
llvh::dbgs() << "native return value r" << (unsigned)ip->iCallBuiltin.op1
<< "=" << DumpHermesValue(O1REG(CallBuiltin)) << "\n");
return ExecutionStatus::RETURNED;
}
} // namespace vm
} // namespace hermes
#undef DEBUG_TYPE