forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
3665 lines (3384 loc) · 136 KB
/
Interpreter.cpp
File metadata and controls
3665 lines (3384 loc) · 136 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "hermes/VM/Interpreter.h"
#include "hermes/VM/Runtime.h"
#include "hermes/Inst/InstDecode.h"
#include "hermes/Support/Conversions.h"
#include "hermes/Support/SlowAssert.h"
#include "hermes/Support/Statistic.h"
#include "hermes/VM/Callable.h"
#include "hermes/VM/CodeBlock.h"
#include "hermes/VM/HandleRootOwner-inline.h"
#include "hermes/VM/JIT/JIT.h"
#include "hermes/VM/JSArray.h"
#include "hermes/VM/JSError.h"
#include "hermes/VM/JSGenerator.h"
#include "hermes/VM/JSProxy.h"
#include "hermes/VM/JSRegExp.h"
#include "hermes/VM/Operations.h"
#include "hermes/VM/Profiler.h"
#include "hermes/VM/Profiler/CodeCoverageProfiler.h"
#include "hermes/VM/Runtime-inline.h"
#include "hermes/VM/RuntimeModule-inline.h"
#include "hermes/VM/StackFrame-inline.h"
#include "hermes/VM/StringPrimitive.h"
#include "hermes/VM/StringView.h"
#include "llvh/ADT/SmallSet.h"
#include "llvh/Support/Debug.h"
#include "llvh/Support/Format.h"
#include "llvh/Support/raw_ostream.h"
#include "Interpreter-internal.h"
using llvh::dbgs;
using namespace hermes::inst;
HERMES_SLOW_STATISTIC(
NumGetById,
"NumGetById: Number of property 'read by id' accesses");
HERMES_SLOW_STATISTIC(
NumGetByIdCacheHits,
"NumGetByIdCacheHits: Number of property 'read by id' cache hits");
HERMES_SLOW_STATISTIC(
NumGetByIdProtoHits,
"NumGetByIdProtoHits: Number of property 'read by id' cache hits for the prototype");
HERMES_SLOW_STATISTIC(
NumGetByIdCacheEvicts,
"NumGetByIdCacheEvicts: Number of property 'read by id' cache evictions");
HERMES_SLOW_STATISTIC(
NumGetByIdFastPaths,
"NumGetByIdFastPaths: Number of property 'read by id' fast paths");
HERMES_SLOW_STATISTIC(
NumGetByIdAccessor,
"NumGetByIdAccessor: Number of property 'read by id' accessors");
HERMES_SLOW_STATISTIC(
NumGetByIdProto,
"NumGetByIdProto: Number of property 'read by id' in the prototype chain");
HERMES_SLOW_STATISTIC(
NumGetByIdNotFound,
"NumGetByIdNotFound: Number of property 'read by id' not found");
HERMES_SLOW_STATISTIC(
NumGetByIdTransient,
"NumGetByIdTransient: Number of property 'read by id' of non-objects");
HERMES_SLOW_STATISTIC(
NumGetByIdDict,
"NumGetByIdDict: Number of property 'read by id' of dictionaries");
HERMES_SLOW_STATISTIC(
NumGetByIdSlow,
"NumGetByIdSlow: Number of property 'read by id' slow path");
HERMES_SLOW_STATISTIC(
NumPutById,
"NumPutById: Number of property 'write by id' accesses");
HERMES_SLOW_STATISTIC(
NumPutByIdCacheHits,
"NumPutByIdCacheHits: Number of property 'write by id' cache hits");
HERMES_SLOW_STATISTIC(
NumPutByIdCacheEvicts,
"NumPutByIdCacheEvicts: Number of property 'write by id' cache evictions");
HERMES_SLOW_STATISTIC(
NumPutByIdFastPaths,
"NumPutByIdFastPaths: Number of property 'write by id' fast paths");
HERMES_SLOW_STATISTIC(
NumPutByIdTransient,
"NumPutByIdTransient: Number of property 'write by id' to non-objects");
HERMES_SLOW_STATISTIC(
NumNativeFunctionCalls,
"NumNativeFunctionCalls: Number of native function calls");
HERMES_SLOW_STATISTIC(
NumBoundFunctionCalls,
"NumBoundCalls: Number of bound function calls");
// Ensure that instructions declared as having matching layouts actually do.
#include "InstLayout.inc"
#if defined(HERMESVM_PROFILER_EXTERN)
// External profiler mode wraps calls to each JS function with a unique native
// function that recusively calls the interpreter. See Profiler.{h,cpp} for how
// these symbols are subsequently patched with JS function names.
#define INTERP_WRAPPER(name) \
__attribute__((__noinline__)) static llvh::CallResult<llvh::HermesValue> \
name(hermes::vm::Runtime *runtime, hermes::vm::CodeBlock *newCodeBlock) { \
return runtime->interpretFunctionImpl(newCodeBlock); \
}
PROFILER_SYMBOLS(INTERP_WRAPPER)
#endif
namespace hermes {
namespace vm {
#if defined(HERMESVM_PROFILER_EXTERN)
typedef CallResult<HermesValue> (*WrapperFunc)(Runtime *, CodeBlock *);
#define LIST_ITEM(name) name,
static const WrapperFunc interpWrappers[] = {PROFILER_SYMBOLS(LIST_ITEM)};
#endif
/// Initialize the state of some internal variables based on the current
/// code block.
#define INIT_STATE_FOR_CODEBLOCK(codeBlock) \
do { \
strictMode = (codeBlock)->isStrictMode(); \
defaultPropOpFlags = DEFAULT_PROP_OP_FLAGS(strictMode); \
} while (0)
CallResult<PseudoHandle<JSGeneratorFunction>>
Interpreter::createGeneratorClosure(
Runtime *runtime,
RuntimeModule *runtimeModule,
unsigned funcIndex,
Handle<Environment> envHandle) {
return JSGeneratorFunction::create(
runtime,
runtimeModule->getDomain(runtime),
Handle<JSObject>::vmcast(&runtime->generatorFunctionPrototype),
envHandle,
runtimeModule->getCodeBlockMayAllocate(funcIndex));
}
CallResult<PseudoHandle<JSGenerator>> Interpreter::createGenerator_RJS(
Runtime *runtime,
RuntimeModule *runtimeModule,
unsigned funcIndex,
Handle<Environment> envHandle,
NativeArgs args) {
auto gifRes = GeneratorInnerFunction::create(
runtime,
runtimeModule->getDomain(runtime),
Handle<JSObject>::vmcast(&runtime->functionPrototype),
envHandle,
runtimeModule->getCodeBlockMayAllocate(funcIndex),
args);
if (LLVM_UNLIKELY(gifRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto generatorFunction = runtime->makeHandle(vmcast<JSGeneratorFunction>(
runtime->getCurrentFrame().getCalleeClosure()));
auto prototypeProp = JSObject::getNamed_RJS(
generatorFunction,
runtime,
Predefined::getSymbolID(Predefined::prototype));
if (LLVM_UNLIKELY(prototypeProp == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSObject> prototype = vmisa<JSObject>(prototypeProp->get())
? runtime->makeHandle<JSObject>(prototypeProp->get())
: Handle<JSObject>::vmcast(&runtime->generatorPrototype);
return JSGenerator::create(runtime, *gifRes, prototype);
}
CallResult<Handle<Arguments>> Interpreter::reifyArgumentsSlowPath(
Runtime *runtime,
Handle<Callable> curFunction,
bool strictMode) {
auto frame = runtime->getCurrentFrame();
uint32_t argCount = frame.getArgCount();
// Define each JavaScript argument.
auto argRes = Arguments::create(runtime, argCount, curFunction, strictMode);
if (LLVM_UNLIKELY(argRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<Arguments> args = *argRes;
for (uint32_t argIndex = 0; argIndex < argCount; ++argIndex) {
Arguments::unsafeSetExistingElementAt(
*args, runtime, argIndex, frame.getArgRef(argIndex));
}
// The returned value should already be set from the create call.
return args;
}
CallResult<PseudoHandle<>> Interpreter::getArgumentsPropByValSlowPath_RJS(
Runtime *runtime,
PinnedHermesValue *lazyReg,
PinnedHermesValue *valueReg,
Handle<Callable> curFunction,
bool strictMode) {
auto frame = runtime->getCurrentFrame();
// If the arguments object has already been created.
if (!lazyReg->isUndefined()) {
// The arguments object has been created, so this is a regular property
// get.
assert(lazyReg->isObject() && "arguments lazy register is not an object");
return JSObject::getComputed_RJS(
Handle<JSObject>::vmcast(lazyReg), runtime, Handle<>(valueReg));
}
if (!valueReg->isSymbol()) {
// Attempt a fast path in the case that the key is not a symbol.
// If it is a symbol, force reification for now.
// Convert the value to a string.
auto strRes = toString_RJS(runtime, Handle<>(valueReg));
if (strRes == ExecutionStatus::EXCEPTION)
return ExecutionStatus::EXCEPTION;
auto strPrim = runtime->makeHandle(std::move(*strRes));
// Check if the string is a valid argument index.
if (auto index = toArrayIndex(runtime, strPrim)) {
if (*index < frame.getArgCount()) {
return createPseudoHandle(frame.getArgRef(*index));
}
auto objectPrototype =
Handle<JSObject>::vmcast(&runtime->objectPrototype);
// OK, they are requesting an index that either doesn't exist or is
// somewhere up in the prototype chain. Since we want to avoid reifying,
// check which it is:
MutableHandle<JSObject> inObject{runtime};
ComputedPropertyDescriptor desc;
JSObject::getComputedPrimitiveDescriptor(
objectPrototype, runtime, strPrim, inObject, desc);
// If we couldn't find the property, just return 'undefined'.
if (!inObject)
return createPseudoHandle(HermesValue::encodeUndefinedValue());
// If the property isn't an accessor, we can just return it without
// reifying.
if (!desc.flags.accessor) {
return createPseudoHandle(
JSObject::getComputedSlotValue(inObject.get(), runtime, desc));
}
}
// Are they requesting "arguments.length"?
if (runtime->symbolEqualsToStringPrim(
Predefined::getSymbolID(Predefined::length), *strPrim)) {
return createPseudoHandle(
HermesValue::encodeDoubleValue(frame.getArgCount()));
}
}
// Looking for an accessor or a property that needs reification.
auto argRes = reifyArgumentsSlowPath(runtime, curFunction, strictMode);
if (argRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// Update the register with the reified value.
*lazyReg = argRes->getHermesValue();
// For simplicity, call ourselves again.
return getArgumentsPropByValSlowPath_RJS(
runtime, lazyReg, valueReg, curFunction, strictMode);
}
ExecutionStatus Interpreter::handleGetPNameList(
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;
}
CallResult<PseudoHandle<>> Interpreter::handleCallSlowPath(
Runtime *runtime,
PinnedHermesValue *callTarget) {
if (auto *native = dyn_vmcast<NativeFunction>(*callTarget)) {
++NumNativeFunctionCalls;
// Call the native function directly
return NativeFunction::_nativeCall(native, runtime);
} else if (auto *bound = dyn_vmcast<BoundFunction>(*callTarget)) {
++NumBoundFunctionCalls;
// Call the bound function.
return BoundFunction::_boundCall(bound, runtime->getCurrentIP(), runtime);
} else {
return runtime->raiseTypeErrorForValue(
Handle<>(callTarget), " is not a function");
}
}
inline PseudoHandle<> Interpreter::tryGetPrimitiveOwnPropertyById(
Runtime *runtime,
Handle<> base,
SymbolID id) {
if (base->isString() && id == Predefined::getSymbolID(Predefined::length)) {
return createPseudoHandle(
HermesValue::encodeNumberValue(base->getString()->getStringLength()));
}
return createPseudoHandle(HermesValue::encodeEmptyValue());
}
CallResult<PseudoHandle<>> Interpreter::getByIdTransient_RJS(
Runtime *runtime,
Handle<> base,
SymbolID id) {
// This is similar to what ES5.1 8.7.1 special [[Get]] internal
// method did, but that section doesn't exist in ES9 anymore.
// Instead, the [[Get]] Receiver argument serves a similar purpose.
// Fast path: try to get primitive own property directly first.
PseudoHandle<> valOpt = tryGetPrimitiveOwnPropertyById(runtime, base, id);
if (!valOpt->isEmpty()) {
return valOpt;
}
// get the property descriptor from primitive prototype without
// boxing with vm::toObject(). This is where any properties will
// be.
CallResult<Handle<JSObject>> primitivePrototypeResult =
getPrimitivePrototype(runtime, base);
if (primitivePrototypeResult == ExecutionStatus::EXCEPTION) {
// If an exception is thrown, likely we are trying to read property on
// undefined/null. Passing over the name of the property
// so that we could emit more meaningful error messages.
return amendPropAccessErrorMsgWithPropName(runtime, base, "read", id);
}
return JSObject::getNamedWithReceiver_RJS(
*primitivePrototypeResult, runtime, id, base);
}
PseudoHandle<> Interpreter::getByValTransientFast(
Runtime *runtime,
Handle<> base,
Handle<> nameHandle) {
if (base->isString()) {
// Handle most common fast path -- array index property for string
// primitive.
// Since primitive string cannot have index like property we can
// skip ObjectFlags::fastIndexProperties checking and directly
// checking index storage from StringPrimitive.
OptValue<uint32_t> arrayIndex = toArrayIndexFastPath(*nameHandle);
// Get character directly from primitive if arrayIndex is within range.
// Otherwise we need to fall back to prototype lookup.
if (arrayIndex &&
arrayIndex.getValue() < base->getString()->getStringLength()) {
return createPseudoHandle(
runtime
->getCharacterString(base->getString()->at(arrayIndex.getValue()))
.getHermesValue());
}
}
return createPseudoHandle(HermesValue::encodeEmptyValue());
}
CallResult<PseudoHandle<>> Interpreter::getByValTransient_RJS(
Runtime *runtime,
Handle<> base,
Handle<> name) {
// This is similar to what ES5.1 8.7.1 special [[Get]] internal
// method did, but that section doesn't exist in ES9 anymore.
// Instead, the [[Get]] Receiver argument serves a similar purpose.
// Optimization: check fast path first.
PseudoHandle<> fastRes = getByValTransientFast(runtime, base, name);
if (!fastRes->isEmpty()) {
return fastRes;
}
auto res = toObject(runtime, base);
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION))
return ExecutionStatus::EXCEPTION;
return JSObject::getComputedWithReceiver_RJS(
runtime->makeHandle<JSObject>(res.getValue()), runtime, name, base);
}
static ExecutionStatus
transientObjectPutErrorMessage(Runtime *runtime, Handle<> base, SymbolID id) {
// Emit an error message that looks like:
// "Cannot create property '%{id}' on ${typeof base} '${String(base)}'".
StringView propName =
runtime->getIdentifierTable().getStringView(runtime, id);
Handle<StringPrimitive> baseType =
runtime->makeHandle(vmcast<StringPrimitive>(typeOf(runtime, base)));
StringView baseTypeAsString =
StringPrimitive::createStringView(runtime, baseType);
MutableHandle<StringPrimitive> valueAsString{runtime};
if (base->isSymbol()) {
// Special workaround for Symbol which can't be stringified.
auto str = symbolDescriptiveString(runtime, Handle<SymbolID>::vmcast(base));
if (str != ExecutionStatus::EXCEPTION) {
valueAsString = *str;
} else {
runtime->clearThrownValue();
valueAsString = StringPrimitive::createNoThrow(
runtime, "<<Exception occurred getting the value>>");
}
} else {
auto str = toString_RJS(runtime, base);
assert(
str != ExecutionStatus::EXCEPTION &&
"Primitives should be convertible to string without exceptions");
valueAsString = std::move(*str);
}
StringView valueAsStringPrintable =
StringPrimitive::createStringView(runtime, valueAsString);
SmallU16String<32> tmp1;
SmallU16String<32> tmp2;
return runtime->raiseTypeError(
TwineChar16("Cannot create property '") + propName + "' on " +
baseTypeAsString.getUTF16Ref(tmp1) + " '" +
valueAsStringPrintable.getUTF16Ref(tmp2) + "'");
}
ExecutionStatus Interpreter::putByIdTransient_RJS(
Runtime *runtime,
Handle<> base,
SymbolID id,
Handle<> value,
bool strictMode) {
// ES5.1 8.7.2 special [[Get]] internal method.
// TODO: avoid boxing primitives unless we are calling an accessor.
// 1. Let O be ToObject(base)
auto res = toObject(runtime, base);
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
// If an exception is thrown, likely we are trying to convert
// undefined/null to an object. Passing over the name of the property
// so that we could emit more meaningful error messages.
return amendPropAccessErrorMsgWithPropName(runtime, base, "set", id);
}
auto O = runtime->makeHandle<JSObject>(res.getValue());
NamedPropertyDescriptor desc;
JSObject *propObj = JSObject::getNamedDescriptor(O, runtime, id, desc);
// Is this a missing property, or a data property defined in the prototype
// chain? In both cases we would need to create an own property on the
// transient object, which is prohibited.
if (!propObj ||
(propObj != O.get() &&
(!desc.flags.accessor && !desc.flags.proxyObject))) {
if (strictMode) {
return transientObjectPutErrorMessage(runtime, base, id);
}
return ExecutionStatus::RETURNED;
}
// Modifying an own data property in a transient object is prohibited.
if (!desc.flags.accessor && !desc.flags.proxyObject) {
if (strictMode) {
return runtime->raiseTypeError(
"Cannot modify a property in a transient object");
}
return ExecutionStatus::RETURNED;
}
if (desc.flags.accessor) {
// This is an accessor.
auto *accessor = vmcast<PropertyAccessor>(
JSObject::getNamedSlotValue(propObj, runtime, desc));
// It needs to have a setter.
if (!accessor->setter) {
if (strictMode) {
return runtime->raiseTypeError("Cannot modify a read-only accessor");
}
return ExecutionStatus::RETURNED;
}
CallResult<PseudoHandle<>> setRes =
accessor->setter.get(runtime)->executeCall1(
runtime->makeHandle(accessor->setter), runtime, base, *value);
if (setRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
} else {
assert(desc.flags.proxyObject && "descriptor flags are impossible");
CallResult<bool> setRes = JSProxy::setNamed(
runtime->makeHandle(propObj), runtime, id, value, base);
if (setRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
if (!*setRes && strictMode) {
return runtime->raiseTypeError("transient proxy set returned false");
}
}
return ExecutionStatus::RETURNED;
}
ExecutionStatus Interpreter::putByValTransient_RJS(
Runtime *runtime,
Handle<> base,
Handle<> name,
Handle<> value,
bool strictMode) {
auto idRes = valueToSymbolID(runtime, name);
if (idRes == ExecutionStatus::EXCEPTION)
return ExecutionStatus::EXCEPTION;
return putByIdTransient_RJS(runtime, base, **idRes, value, strictMode);
}
CallResult<PseudoHandle<>> Interpreter::createObjectFromBuffer(
Runtime *runtime,
CodeBlock *curCodeBlock,
unsigned numLiterals,
unsigned keyBufferIndex,
unsigned valBufferIndex) {
// Fetch any cached hidden class first.
auto *runtimeModule = curCodeBlock->getRuntimeModule();
const llvh::Optional<Handle<HiddenClass>> optCachedHiddenClassHandle =
runtimeModule->findCachedLiteralHiddenClass(
runtime, keyBufferIndex, numLiterals);
// Create a new object using the built-in constructor or cached hidden class.
// Note that the built-in constructor is empty, so we don't actually need to
// call it.
auto obj = runtime->makeHandle(
optCachedHiddenClassHandle.hasValue()
? JSObject::create(runtime, optCachedHiddenClassHandle.getValue())
: JSObject::create(runtime, numLiterals));
MutableHandle<> tmpHandleKey(runtime);
MutableHandle<> tmpHandleVal(runtime);
auto &gcScope = *runtime->getTopGCScope();
auto marker = gcScope.createMarker();
auto genPair = curCodeBlock->getObjectBufferIter(
keyBufferIndex, valBufferIndex, numLiterals);
auto keyGen = genPair.first;
auto valGen = genPair.second;
if (optCachedHiddenClassHandle.hasValue()) {
uint32_t propIndex = 0;
// keyGen should always have the same amount of elements as valGen
while (valGen.hasNext()) {
#ifndef NDEBUG
{
// keyGen points to an element in the key buffer, which means it will
// only ever generate a Number or a Symbol. This means it will never
// allocate memory, and it is safe to not use a Handle.
SymbolID stringIdResult{};
auto key = keyGen.get(runtime);
if (key.isSymbol()) {
stringIdResult = ID(key.getSymbol().unsafeGetIndex());
} else {
tmpHandleKey = HermesValue::encodeDoubleValue(key.getNumber());
auto idRes = valueToSymbolID(runtime, tmpHandleKey);
assert(
idRes != ExecutionStatus::EXCEPTION &&
"valueToIdentifier() failed for uint32_t value");
stringIdResult = **idRes;
}
NamedPropertyDescriptor desc;
auto pos = HiddenClass::findProperty(
optCachedHiddenClassHandle.getValue(),
runtime,
stringIdResult,
PropertyFlags::defaultNewNamedPropertyFlags(),
desc);
assert(
pos &&
"Should find this property in cached hidden class property table.");
assert(
desc.slot == propIndex &&
"propIndex should be the same as recorded in hidden class table.");
}
#endif
// Explicitly make sure valGen.get() is called before obj.get() so that
// any allocation in valGen.get() won't invalidate the raw pointer
// retruned from obj.get().
auto val = valGen.get(runtime);
JSObject::setNamedSlotValue(obj.get(), runtime, propIndex, val);
gcScope.flushToMarker(marker);
++propIndex;
}
} else {
// keyGen should always have the same amount of elements as valGen
while (keyGen.hasNext()) {
// keyGen points to an element in the key buffer, which means it will
// only ever generate a Number or a Symbol. This means it will never
// allocate memory, and it is safe to not use a Handle.
auto key = keyGen.get(runtime);
tmpHandleVal = valGen.get(runtime);
if (key.isSymbol()) {
auto stringIdResult = ID(key.getSymbol().unsafeGetIndex());
if (LLVM_UNLIKELY(
JSObject::defineNewOwnProperty(
obj,
runtime,
stringIdResult,
PropertyFlags::defaultNewNamedPropertyFlags(),
tmpHandleVal) == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
} else {
tmpHandleKey = HermesValue::encodeDoubleValue(key.getNumber());
if (LLVM_UNLIKELY(
!JSObject::defineOwnComputedPrimitive(
obj,
runtime,
tmpHandleKey,
DefinePropertyFlags::getDefaultNewPropertyFlags(),
tmpHandleVal)
.getValue())) {
return ExecutionStatus::EXCEPTION;
}
}
gcScope.flushToMarker(marker);
}
}
tmpHandleKey.clear();
tmpHandleVal.clear();
// Hidden class in dictionary mode can't be shared.
HiddenClass *const clazz = obj->getClass(runtime);
if (!optCachedHiddenClassHandle.hasValue() && !clazz->isDictionary()) {
assert(
numLiterals == clazz->getNumProperties() &&
"numLiterals should match hidden class property count.");
assert(
clazz->getNumProperties() < 256 &&
"cached hidden class should have property count less than 256");
runtimeModule->tryCacheLiteralHiddenClass(runtime, keyBufferIndex, clazz);
}
return createPseudoHandle(HermesValue::encodeObjectValue(*obj));
}
CallResult<PseudoHandle<>> Interpreter::createArrayFromBuffer(
Runtime *runtime,
CodeBlock *curCodeBlock,
unsigned numElements,
unsigned numLiterals,
unsigned bufferIndex) {
// Create a new array using the built-in constructor, and initialize
// the elements from a literal array buffer.
auto arrRes = JSArray::create(runtime, numElements, numElements);
if (arrRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// Resize the array storage in advance.
auto arr = runtime->makeHandle(std::move(*arrRes));
JSArray::setStorageEndIndex(arr, runtime, numElements);
auto iter = curCodeBlock->getArrayBufferIter(bufferIndex, numLiterals);
JSArray::size_type i = 0;
while (iter.hasNext()) {
// NOTE: we must get the value in a separate step to guarantee ordering.
auto value = iter.get(runtime);
JSArray::unsafeSetExistingElementAt(*arr, runtime, i++, value);
}
return createPseudoHandle(HermesValue::encodeObjectValue(*arr));
}
#ifndef NDEBUG
namespace {
/// A tag used to instruct the output stream to dump more details about the
/// HermesValue, like the length of the string, etc.
struct DumpHermesValue {
const HermesValue hv;
DumpHermesValue(HermesValue hv) : hv(hv) {}
};
} // anonymous namespace.
static llvh::raw_ostream &operator<<(
llvh::raw_ostream &OS,
DumpHermesValue dhv) {
OS << dhv.hv;
// If it is a string, dump the contents, truncated to 8 characters.
if (dhv.hv.isString()) {
SmallU16String<32> str;
dhv.hv.getString()->appendUTF16String(str);
UTF16Ref ref = str.arrayRef();
if (str.size() <= 8) {
OS << ":'" << ref << "'";
} else {
OS << ":'" << ref.slice(0, 8) << "'";
OS << "...[" << str.size() << "]";
}
}
return OS;
}
/// Dump the arguments from a callee frame.
LLVM_ATTRIBUTE_UNUSED
static void dumpCallArguments(
llvh::raw_ostream &OS,
Runtime *runtime,
StackFramePtr calleeFrame) {
OS << "arguments:\n";
OS << " " << 0 << " " << DumpHermesValue(calleeFrame.getThisArgRef())
<< "\n";
for (unsigned i = 0; i < calleeFrame.getArgCount(); ++i) {
OS << " " << (i + 1) << " " << DumpHermesValue(calleeFrame.getArgRef(i))
<< "\n";
}
}
LLVM_ATTRIBUTE_UNUSED
static void printDebugInfo(
CodeBlock *curCodeBlock,
PinnedHermesValue *frameRegs,
const Inst *ip) {
// Check if LLVm debugging is enabled for us.
bool debug = false;
SLOW_DEBUG(debug = true);
if (!debug)
return;
DecodedInstruction decoded = decodeInstruction(ip);
dbgs() << llvh::format_decimal((const uint8_t *)ip - curCodeBlock->begin(), 4)
<< " OpCode::" << getOpCodeString(decoded.meta.opCode);
for (unsigned i = 0; i < decoded.meta.numOperands; ++i) {
auto operandType = decoded.meta.operandType[i];
auto value = decoded.operandValue[i];
dbgs() << (i == 0 ? " " : ", ");
dumpOperand(dbgs(), operandType, value);
if (operandType == OperandType::Reg8 || operandType == OperandType::Reg32) {
// Print the register value, if source.
if (i != 0 || decoded.meta.numOperands == 1)
dbgs() << "=" << DumpHermesValue(REG(value.integer));
}
}
dbgs() << "\n";
}
/// \return whether \p opcode is a call opcode (Call, CallDirect, Construct,
/// CallLongIndex, etc). Note CallBuiltin is not really a Call.
LLVM_ATTRIBUTE_UNUSED
static bool isCallType(OpCode opcode) {
switch (opcode) {
#define DEFINE_RET_TARGET(name) \
case OpCode::name: \
return true;
#include "hermes/BCGen/HBC/BytecodeList.def"
default:
return false;
}
}
#endif
/// \return the address of the next instruction after \p ip, which must be a
/// call-type instruction.
LLVM_ATTRIBUTE_ALWAYS_INLINE
static inline const Inst *nextInstCall(const Inst *ip) {
HERMES_SLOW_ASSERT(isCallType(ip->opCode) && "ip is not of call type");
// The following is written to elicit compares instead of table lookup.
// The idea is to present code like so:
// if (opcode <= 70) return ip + 4;
// if (opcode <= 71) return ip + 4;
// if (opcode <= 72) return ip + 4;
// if (opcode <= 73) return ip + 5;
// if (opcode <= 74) return ip + 5;
// ...
// and the compiler will retain only compares where the result changes (here,
// 72 and 74). This allows us to compute the next instruction using three
// compares, instead of a naive compare-per-call type (or lookup table).
//
// Statically verify that increasing call opcodes correspond to monotone
// instruction sizes; this enables the compiler to do a better job optimizing.
constexpr bool callSizesMonotoneIncreasing = monotoneIncreasing(
#define DEFINE_RET_TARGET(name) sizeof(inst::name##Inst),
#include "hermes/BCGen/HBC/BytecodeList.def"
SIZE_MAX // sentinel avoiding a trailing comma.
);
static_assert(
callSizesMonotoneIncreasing,
"Call instruction sizes are not monotone increasing");
#define DEFINE_RET_TARGET(name) \
if (ip->opCode <= OpCode::name) \
return NEXTINST(name);
#include "hermes/BCGen/HBC/BytecodeList.def"
llvm_unreachable("Not a call type");
}
CallResult<HermesValue> Runtime::interpretFunctionImpl(
CodeBlock *newCodeBlock) {
newCodeBlock->lazyCompile(this);
#if defined(HERMES_ENABLE_ALLOCATION_LOCATION_TRACES) || !defined(NDEBUG)
// We always call getCurrentIP() in a debug build as this has the effect
// of asserting the IP is correctly set (not invalidated) at this point.
// This allows us to leverage our whole test-suite to find missing cases
// of CAPTURE_IP* macros in the interpreter loop.
const inst::Inst *ip = getCurrentIP();
(void)ip;
#endif
#ifdef HERMES_ENABLE_ALLOCATION_LOCATION_TRACES
if (ip) {
const CodeBlock *codeBlock;
std::tie(codeBlock, ip) = getCurrentInterpreterLocation(ip);
// All functions end in a Ret so we must match this with a pushCallStack()
// before executing.
if (codeBlock) {
// Push a call entry at the last location we were executing bytecode.
// This will correctly attribute things like eval().
pushCallStack(codeBlock, ip);
} else {
// Push a call entry at the entry at the top of interpreted code.
pushCallStack(newCodeBlock, (const Inst *)newCodeBlock->begin());
}
} else {
// Push a call entry at the entry at the top of interpreted code.
pushCallStack(newCodeBlock, (const Inst *)newCodeBlock->begin());
}
#endif
InterpreterState state{newCodeBlock, 0};
return Interpreter::interpretFunction<false>(this, state);
}
CallResult<HermesValue> Runtime::interpretFunction(CodeBlock *newCodeBlock) {
#ifdef HERMESVM_PROFILER_EXTERN
auto id = getProfilerID(newCodeBlock);
if (id >= NUM_PROFILER_SYMBOLS) {
id = NUM_PROFILER_SYMBOLS - 1; // Overflow entry.
}
return interpWrappers[id](this, newCodeBlock);
#else
return interpretFunctionImpl(newCodeBlock);
#endif
}
#ifdef HERMES_ENABLE_DEBUGGER
ExecutionStatus Runtime::stepFunction(InterpreterState &state) {
return Interpreter::interpretFunction<true>(this, state).getStatus();
}
#endif
/// \return the quotient of x divided by y.
static double doDiv(double x, double y)
LLVM_NO_SANITIZE("float-divide-by-zero");
static inline double doDiv(double x, double y) {
// UBSan will complain about float divide by zero as our implementation
// of OpCode::Div depends on IEEE 754 float divide by zero. All modern
// compilers implement this and there is no trivial work-around without
// sacrificing performance and readability.
// NOTE: This was pulled out of the interpreter to avoid putting the sanitize
// silencer on the entire interpreter function.
return x / y;
}
/// \return the product of x multiplied by y.
static inline double doMult(double x, double y) {
return x * y;
}
/// \return the difference of y subtracted from x.
static inline double doSub(double x, double y) {
return x - y;
}
template <bool SingleStep>
CallResult<HermesValue> Interpreter::interpretFunction(
Runtime *runtime,
InterpreterState &state) {
// The interpreter is re-entrant and also saves/restores its IP via the
// runtime whenever a call out is made (see the CAPTURE_IP_* macros). As such,
// failure to preserve the IP across calls to interpreterFunction() disrupt
// interpreter calls further up the C++ callstack. The RAII utility class
// below makes sure we always do this correctly.
//
// TODO: The IPs stored in the C++ callstack via this holder will generally be
// the same as in the JS stack frames via the Saved IP field. We can probably
// get rid of one of these redundant stores. Doing this isn't completely
// trivial as there are currently cases where we re-enter the interpreter
// without calling Runtime::saveCallerIPInStackFrame(), and there are features
// (I think mostly the debugger + stack traces) which implicitly rely on
// this behavior. At least their tests break if this behavior is not
// preserved.
struct IPSaver {
IPSaver(Runtime *runtime)
: ip_(runtime->getCurrentIP()), runtime_(runtime) {}
~IPSaver() {
runtime_->setCurrentIP(ip_);
}
private:
const Inst *ip_;
Runtime *runtime_;
};
IPSaver ipSaver(runtime);
#ifndef HERMES_ENABLE_DEBUGGER
static_assert(!SingleStep, "can't use single-step mode without the debugger");
#endif
// Make sure that the cache can use an optimization by avoiding a branch to
// access the property storage.
static_assert(
HiddenClass::kDictionaryThreshold <=
SegmentedArray::kValueToSegmentThreshold,
"Cannot avoid branches in cache check if the dictionary "
"crossover point is larger than the inline storage");
CodeBlock *curCodeBlock = state.codeBlock;
const Inst *ip = nullptr;
// Points to the first local register in the current frame.
// This eliminates the indirect load from Runtime and the -1 offset.
PinnedHermesValue *frameRegs;
// Strictness of current function.
bool strictMode;
// Default flags when accessing properties.
PropOpFlags defaultPropOpFlags;
// These CAPTURE_IP* macros should wrap around any major calls out of the
// interpreter loop. They stash and retrieve the IP via the current Runtime
// allowing the IP to be externally observed and even altered to change the flow
// of execution. Explicitly saving AND restoring the IP from the Runtime in this
// way means the C++ compiler will keep IP in a register within the rest of the
// interpreter loop.
//
// When assertions are enabled we take the extra step of "invalidating" the IP
// between captures so we can detect if it's erroneously accessed.
//
// In some cases we explicitly don't want to invalidate the IP and instead want
// it to stay set. For this we use the *NO_INVALIDATE variants. This comes up
// when we're performing a call operation which may re-enter the interpreter
// loop, and so need the IP available for the saveCallerIPInStackFrame() call
// when we next enter.
#define CAPTURE_IP_ASSIGN_NO_INVALIDATE(dst, expr) \
runtime->setCurrentIP(ip); \
dst = expr; \
ip = runtime->getCurrentIP();
#ifdef NDEBUG
#define CAPTURE_IP(expr) \
runtime->setCurrentIP(ip); \
(void)expr; \
ip = runtime->getCurrentIP();
#define CAPTURE_IP_ASSIGN(dst, expr) CAPTURE_IP_ASSIGN_NO_INVALIDATE(dst, expr)
#else // !NDEBUG
#define CAPTURE_IP(expr) \
runtime->setCurrentIP(ip); \
(void)expr; \
ip = runtime->getCurrentIP(); \
runtime->invalidateCurrentIP();
#define CAPTURE_IP_ASSIGN(dst, expr) \
runtime->setCurrentIP(ip); \
dst = expr; \
ip = runtime->getCurrentIP(); \
runtime->invalidateCurrentIP();