Repository navigation
Expand file tree
/
Copy pathSynthTrace.h
More file actions
1201 lines (1024 loc) · 35.9 KB
/
Copy pathSynthTrace.h
File metadata and controls
1201 lines (1024 loc) · 35.9 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) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifndef HERMES_SYNTHTRACE_H
#define HERMES_SYNTHTRACE_H
#include "hermes/Public/RuntimeConfig.h"
#include "hermes/Support/JSONEmitter.h"
#include "hermes/Support/SHA1.h"
#include "hermes/Support/StringSetVector.h"
#include "hermes/VM/GCExecTrace.h"
#include "hermes/VM/MockedEnvironment.h"
#include <chrono>
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
namespace llvh {
// Forward declaration to avoid including llvm headers.
class raw_ostream;
} // namespace llvh
namespace facebook {
namespace hermes {
namespace tracing {
/// A SynthTrace is a list of events that occur in a run of a JS file by a
/// runtime that uses JSI.
/// It can be serialized into JSON and written to a llvh::raw_ostream.
class SynthTrace {
public:
using ObjectID = uint64_t;
/// A tagged union representing different types available in the trace.
/// We use a an API very similar to HermesValue, but:
/// a) also represent the JSI type PropNameID, and
/// b) the "payloads" for some the types (Objects, Strings, and PropNameIDs)
/// are unique ObjectIDs, rather than actual values.
/// (This could probably become a std::variant when we could use C++17.)
class TraceValue {
public:
bool isUndefined() const {
return tag_ == Tag::Undefined;
}
bool isNull() const {
return tag_ == Tag::Null;
}
bool isNumber() const {
return tag_ == Tag::Number;
}
bool isBool() const {
return tag_ == Tag::Bool;
}
bool isObject() const {
return tag_ == Tag::Object;
}
bool isBigInt() const {
return tag_ == Tag::BigInt;
}
bool isString() const {
return tag_ == Tag::String;
}
bool isPropNameID() const {
return tag_ == Tag::PropNameID;
}
bool isSymbol() const {
return tag_ == Tag::Symbol;
}
bool isUID() const {
return isObject() || isBigInt() || isString() || isPropNameID() ||
isSymbol();
}
static TraceValue encodeUndefinedValue() {
return TraceValue(Tag::Undefined);
}
static TraceValue encodeNullValue() {
return TraceValue(Tag::Null);
}
static TraceValue encodeBoolValue(bool value) {
return TraceValue(value);
}
static TraceValue encodeNumberValue(double value) {
return TraceValue(value);
}
static TraceValue encodeObjectValue(uint64_t uid) {
return TraceValue(Tag::Object, uid);
}
static TraceValue encodeBigIntValue(uint64_t uid) {
return TraceValue(Tag::BigInt, uid);
}
static TraceValue encodeStringValue(uint64_t uid) {
return TraceValue(Tag::String, uid);
}
static TraceValue encodePropNameIDValue(uint64_t uid) {
return TraceValue(Tag::PropNameID, uid);
}
static TraceValue encodeSymbolValue(uint64_t uid) {
return TraceValue(Tag::Symbol, uid);
}
bool operator==(const TraceValue &that) const;
ObjectID getUID() const {
assert(isUID());
return val_.uid;
}
bool getBool() const {
assert(isBool());
return val_.b;
}
double getNumber() const {
assert(isNumber());
return val_.n;
}
private:
enum class Tag {
Undefined,
Null,
Bool,
Number,
Object,
String,
PropNameID,
Symbol,
BigInt,
};
explicit TraceValue(Tag tag) : tag_(tag) {}
TraceValue(bool b) : tag_(Tag::Bool) {
val_.b = b;
}
TraceValue(double n) : tag_(Tag::Number) {
val_.n = n;
}
TraceValue(Tag tag, uint64_t uid) : tag_(tag) {
val_.uid = uid;
}
Tag tag_;
union {
bool b;
double n;
ObjectID uid;
} val_;
};
/// A TimePoint is a time when some event occurred.
using TimePoint = std::chrono::steady_clock::time_point;
using TimeSinceStart = std::chrono::milliseconds;
static constexpr size_t kHashNumBytes = 20;
/// RecordType is a tag used to differentiate which type of record it is.
/// There should be a unique tag for each record type.
enum class RecordType {
BeginExecJS,
EndExecJS,
Marker,
CreateObject,
CreateString,
CreatePropNameID,
CreateHostObject,
CreateHostFunction,
DrainMicrotasks,
GetProperty,
SetProperty,
HasProperty,
GetPropertyNames,
CreateArray,
ArrayRead,
ArrayWrite,
CallFromNative,
ConstructFromNative,
ReturnFromNative,
ReturnToNative,
CallToNative,
GetPropertyNative,
GetPropertyNativeReturn,
SetPropertyNative,
SetPropertyNativeReturn,
GetNativePropertyNames,
GetNativePropertyNamesReturn,
CreateBigInt,
BigIntToString,
};
/// A Record is one element of a trace.
struct Record {
/// The time at which this event occurred with respect to the start of
/// execution.
/// NOTE: This is not compared in the \c operator= in order for tests to
/// pass.
const TimeSinceStart time_;
explicit Record() = delete;
explicit Record(TimeSinceStart time) : time_(time) {}
virtual ~Record() = default;
/// Write out a serialization of this Record.
/// \param json An emitter connected to an ostream which will write out
/// JSON.
void toJSON(::hermes::JSONEmitter &json) const;
virtual RecordType getType() const = 0;
// If \p val is an object (that is, an Object or String), push its
// decoding onto objs.
static void pushIfTrackedValue(
const TraceValue &val,
std::vector<ObjectID> &objs) {
if (val.isUID()) {
objs.push_back(val.getUID());
}
}
/// \return A list of object ids that are defined by this record.
/// Defined means that the record would produce that object,
/// string, or PropNameID as a locally accessible value if it were
/// executed.
virtual std::vector<ObjectID> defs() const {
return {};
}
/// \return A list of object ids that are used by this record.
/// Used means that the record would use that object, string, or
/// PropNameID as a value if it were executed.
/// If a record uses an object id, then some preceding record
/// (either in the same function invocation, or somewhere
/// globally) must provide a definition.
virtual std::vector<ObjectID> uses() const {
return {};
}
protected:
/// Compare records for equality. Derived classes should override this, call
/// their parent, and mark any public versions as "final".
virtual bool operator==(const Record &that) const {
return getType() == that.getType();
}
/// Emit JSON fields into \p os, excluding the closing curly brace.
/// NOTE: This is overridable, and non-abstract children should call the
/// parent.
virtual void toJSONInternal(::hermes::JSONEmitter &json) const;
};
/// If \p traceStream is non-null, the trace will be written to that
/// stream. Otherwise, no trace is written.
explicit SynthTrace(
ObjectID globalObjID,
const ::hermes::vm::RuntimeConfig &conf,
std::unique_ptr<llvh::raw_ostream> traceStream = nullptr);
template <typename T, typename... Args>
void emplace_back(Args &&...args) {
records_.emplace_back(new T(std::forward<Args>(args)...));
flushRecordsIfNecessary();
}
const std::vector<std::unique_ptr<Record>> &records() const {
return records_;
}
ObjectID globalObjID() const {
return globalObjID_;
}
/// Given a trace value, turn it into its typed string.
static std::string encode(TraceValue value);
/// Encode an undefined JS value for the trace.
static TraceValue encodeUndefined();
/// Encode a null JS value for the trace.
static TraceValue encodeNull();
/// Encode a boolean JS value for the trace.
static TraceValue encodeBool(bool value);
/// Encodes a numeric value for the trace.
static TraceValue encodeNumber(double value);
/// Encodes an object for the trace as a unique id.
static TraceValue encodeObject(ObjectID objID);
/// Encodes a bigint for the trace as a unique id.
static TraceValue encodeBigInt(ObjectID objID);
/// Encodes a string for the trace as a unique id.
static TraceValue encodeString(ObjectID objID);
/// Encodes a PropNameID for the trace as a unique id.
static TraceValue encodePropNameID(ObjectID objID);
/// Encodes a Symbol for the trace as a unique id.
static TraceValue encodeSymbol(ObjectID objID);
/// Decodes a string into a trace value.
static TraceValue decode(const std::string &);
/// The version of the Synth Benchmark
constexpr static uint32_t synthVersion() {
return 4;
}
static const char *nameFromReleaseUnused(::hermes::vm::ReleaseUnused ru);
static ::hermes::vm::ReleaseUnused releaseUnusedFromName(const char *name);
private:
llvh::raw_ostream &os() const {
return (*traceStream_);
}
/// If we're tracing to a file, and the number of accumulated
/// records has reached the limit kTraceRecordsToFlush, below,
/// flush the records to the file, and reset the accumulated records
/// to be empty.
void flushRecordsIfNecessary();
/// Assumes we're tracing to a file; flush accumulated records to
/// the file, and reset the accumulated records to be empty.
void flushRecords();
static constexpr unsigned kTraceRecordsToFlush = 100;
/// If we're tracing to a file, pointer to a stream onto
/// traceFilename_. Null otherwise.
std::unique_ptr<llvh::raw_ostream> traceStream_;
/// If we're tracing to a file, pointer to a JSONEmitter writting
/// into *traceStream_. Null otherwise.
std::unique_ptr<::hermes::JSONEmitter> json_;
/// The records currently being accumulated in the trace. If we are
/// tracing to a file, these will be only the records not yet
/// written to the file.
std::vector<std::unique_ptr<Record>> records_;
/// The id of the global object.
const ObjectID globalObjID_;
public:
/// @name Record classes
/// @{
/// A MarkerRecord is an event that simply records an interesting event that
/// is not necessarily meaningful to the interpreter. It comes with a tag that
/// says what type of marker it was.
struct MarkerRecord : public Record {
static constexpr RecordType type{RecordType::Marker};
const std::string tag_;
explicit MarkerRecord(TimeSinceStart time, const std::string &tag)
: Record(time), tag_(tag) {}
RecordType getType() const override {
return type;
}
protected:
void toJSONInternal(::hermes::JSONEmitter &json) const override;
bool operator==(const Record &that) const override;
};
/// A BeginExecJSRecord is an event where execution begins of JS source
/// code. This is not necessarily the first record, since native code can
/// inject values into the VM before any source code is run.
struct BeginExecJSRecord final : public Record {
static constexpr RecordType type{RecordType::BeginExecJS};
explicit BeginExecJSRecord(
TimeSinceStart time,
std::string sourceURL,
::hermes::SHA1 sourceHash,
bool sourceIsBytecode)
: Record(time),
sourceURL_(std::move(sourceURL)),
sourceHash_(std::move(sourceHash)),
sourceIsBytecode_(sourceIsBytecode) {}
RecordType getType() const override {
return type;
}
const std::string &sourceURL() const {
return sourceURL_;
}
const ::hermes::SHA1 &sourceHash() const {
return sourceHash_;
}
bool operator==(const Record &that) const override;
private:
void toJSONInternal(::hermes::JSONEmitter &json) const override;
/// The URL providing the source file mapping for the file being executed.
/// Can be empty.
std::string sourceURL_;
/// A hash of the source that was executed. The source hash must match up
/// when the file is replayed.
/// The hash is optional, and will be all zeros if not provided.
::hermes::SHA1 sourceHash_;
/// Whether the input file was source or bytecode.
bool sourceIsBytecode_;
};
struct ReturnMixin {
const TraceValue retVal_;
explicit ReturnMixin(TraceValue value) : retVal_(value) {}
void toJSONInternal(::hermes::JSONEmitter &json) const;
bool operator==(const ReturnMixin &that) const;
};
/// A EndExecJSRecord is an event where execution of JS source code stops.
/// This does not mean that the source code will never be entered again, just
/// that it has an entered a phase where it is waiting for native code to call
/// into the JS. This event is not guaranteed to be the last event, for the
/// aforementioned reason. The logged retVal is the result of the evaluation
/// ("undefined" in the majority of cases).
struct EndExecJSRecord final : public MarkerRecord, public ReturnMixin {
static constexpr RecordType type{RecordType::EndExecJS};
EndExecJSRecord(TimeSinceStart time, TraceValue retVal)
: MarkerRecord(time, "end_global_code"), ReturnMixin(retVal) {}
RecordType getType() const override {
return type;
}
bool operator==(const Record &that) const final;
virtual void toJSONInternal(::hermes::JSONEmitter &json) const final;
std::vector<ObjectID> defs() const override {
auto defs = MarkerRecord::defs();
pushIfTrackedValue(retVal_, defs);
return defs;
}
};
/// A CreateObjectRecord is an event where an empty object is created by the
/// native code.
struct CreateObjectRecord : public Record {
static constexpr RecordType type{RecordType::CreateObject};
const ObjectID objID_;
explicit CreateObjectRecord(TimeSinceStart time, ObjectID objID)
: Record(time), objID_(objID) {}
bool operator==(const Record &that) const override;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
return {objID_};
}
std::vector<ObjectID> uses() const override {
return {};
}
};
/// A CreateBigIntRecord is an event where a jsi::BigInt (and thus a
/// Hermes BigIntPrimitive) is created by the native code.
struct CreateBigIntRecord : public Record {
static constexpr RecordType type{RecordType::CreateBigInt};
const ObjectID objID_;
enum class Method {
FromInt64,
FromUint64,
};
Method method_;
uint64_t bits_;
CreateBigIntRecord(
TimeSinceStart time,
ObjectID objID,
Method m,
uint64_t bits)
: Record(time), objID_(objID), method_(m), bits_(bits) {}
bool operator==(const Record &that) const override;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
return {objID_};
}
std::vector<ObjectID> uses() const override {
return {};
}
};
/// A BigIntToString is an event where a jsi::BigInt is converted to a
/// string by native code
struct BigIntToStringRecord : public Record {
static constexpr RecordType type{RecordType::BigIntToString};
const ObjectID strID_;
const ObjectID bigintID_;
int radix_;
BigIntToStringRecord(
TimeSinceStart time,
ObjectID strID,
ObjectID bigintID,
int radix)
: Record(time), strID_(strID), bigintID_(bigintID), radix_(radix) {}
bool operator==(const Record &that) const override;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
return {strID_};
}
std::vector<ObjectID> uses() const override {
return {bigintID_};
}
};
/// A CreateStringRecord is an event where a jsi::String (and thus a
/// Hermes StringPrimitive) is created by the native code.
struct CreateStringRecord : public Record {
static constexpr RecordType type{RecordType::CreateString};
const ObjectID objID_;
std::string chars_;
bool ascii_;
// General UTF-8.
CreateStringRecord(
TimeSinceStart time,
ObjectID objID,
const uint8_t *chars,
size_t length)
: Record(time),
objID_(objID),
chars_(reinterpret_cast<const char *>(chars), length),
ascii_(false) {}
// Ascii.
CreateStringRecord(
TimeSinceStart time,
ObjectID objID,
const char *chars,
size_t length)
: Record(time), objID_(objID), chars_(chars, length), ascii_(true) {}
bool operator==(const Record &that) const override;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
return {objID_};
}
std::vector<ObjectID> uses() const override {
return {};
}
};
/// A CreatePropNameIDRecord is an event where a jsi::PropNameID is
/// created by the native code.
struct CreatePropNameIDRecord : public Record {
static constexpr RecordType type{RecordType::CreatePropNameID};
const ObjectID propNameID_;
std::string chars_;
const TraceValue traceValue_{TraceValue::encodeUndefinedValue()};
enum ValueType { ASCII, UTF8, TRACEVALUE } valueType_;
// General UTF-8.
CreatePropNameIDRecord(
TimeSinceStart time,
ObjectID propNameID,
const uint8_t *chars,
size_t length)
: Record(time),
propNameID_(propNameID),
chars_(reinterpret_cast<const char *>(chars), length),
valueType_(UTF8) {}
// Ascii.
CreatePropNameIDRecord(
TimeSinceStart time,
ObjectID propNameID,
const char *chars,
size_t length)
: Record(time),
propNameID_(propNameID),
chars_(chars, length),
valueType_(ASCII) {}
// jsi::String or jsi::Symbol.
CreatePropNameIDRecord(
TimeSinceStart time,
ObjectID propNameID,
TraceValue traceValue)
: Record(time),
propNameID_(propNameID),
traceValue_(traceValue),
valueType_(TRACEVALUE) {}
bool operator==(const Record &that) const override;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
return {propNameID_};
}
std::vector<ObjectID> uses() const override {
std::vector<ObjectID> vec;
pushIfTrackedValue(traceValue_, vec);
return vec;
}
};
struct CreateHostObjectRecord final : public CreateObjectRecord {
static constexpr RecordType type{RecordType::CreateHostObject};
using CreateObjectRecord::CreateObjectRecord;
RecordType getType() const override {
return type;
}
};
struct CreateHostFunctionRecord final : public CreateObjectRecord {
static constexpr RecordType type{RecordType::CreateHostFunction};
uint32_t propNameID_;
#ifdef HERMESVM_API_TRACE_DEBUG
const std::string functionName_;
#endif
const unsigned paramCount_;
CreateHostFunctionRecord(
TimeSinceStart time,
ObjectID objID,
ObjectID propNameID,
#ifdef HERMESVM_API_TRACE_DEBUG
std::string functionName,
#endif
unsigned paramCount)
: CreateObjectRecord(time, objID),
propNameID_(propNameID),
#ifdef HERMESVM_API_TRACE_DEBUG
functionName_(std::move(functionName)),
#endif
paramCount_(paramCount) {
}
bool operator==(const Record &that) const override;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> uses() const override {
return {propNameID_};
}
};
struct GetOrSetPropertyRecord : public Record {
const ObjectID objID_;
const TraceValue propID_;
#ifdef HERMESVM_API_TRACE_DEBUG
std::string propNameDbg_;
#endif
const TraceValue value_;
GetOrSetPropertyRecord(
TimeSinceStart time,
ObjectID objID,
TraceValue propID,
#ifdef HERMESVM_API_TRACE_DEBUG
const std::string &propNameDbg,
#endif
TraceValue value)
: Record(time),
objID_(objID),
propID_(propID),
#ifdef HERMESVM_API_TRACE_DEBUG
propNameDbg_(propNameDbg),
#endif
value_(value) {
}
bool operator==(const Record &that) const final;
std::vector<ObjectID> uses() const override {
std::vector<ObjectID> vec{objID_};
pushIfTrackedValue(propID_, vec);
return vec;
}
void toJSONInternal(::hermes::JSONEmitter &json) const override;
};
struct DrainMicrotasksRecord : public Record {
static constexpr RecordType type{RecordType::DrainMicrotasks};
int maxMicrotasksHint_;
DrainMicrotasksRecord(TimeSinceStart time, int tasksHint = -1)
: Record(time), maxMicrotasksHint_(tasksHint) {}
bool operator==(const Record &that) const final;
RecordType getType() const override {
return type;
}
void toJSONInternal(::hermes::JSONEmitter &json) const override;
};
/// A GetPropertyRecord is an event where native code accesses the property
/// of a JS object.
struct GetPropertyRecord : public GetOrSetPropertyRecord {
static constexpr RecordType type{RecordType::GetProperty};
using GetOrSetPropertyRecord::GetOrSetPropertyRecord;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
auto defs = GetOrSetPropertyRecord::defs();
pushIfTrackedValue(value_, defs);
return defs;
}
};
/// A SetPropertyRecord is an event where native code writes to the property
/// of a JS object.
struct SetPropertyRecord : public GetOrSetPropertyRecord {
static constexpr RecordType type{RecordType::SetProperty};
using GetOrSetPropertyRecord::GetOrSetPropertyRecord;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> uses() const override {
auto uses = GetOrSetPropertyRecord::uses();
pushIfTrackedValue(value_, uses);
return uses;
}
};
/// A HasPropertyRecord is an event where native code queries whether a
/// property exists on an object. (We don't care about the result because
/// it cannot influence the trace.)
struct HasPropertyRecord final : public Record {
static constexpr RecordType type{RecordType::HasProperty};
const ObjectID objID_;
#ifdef HERMESVM_API_TRACE_DEBUG
std::string propNameDbg_;
#endif
const TraceValue propID_;
HasPropertyRecord(
TimeSinceStart time,
ObjectID objID,
TraceValue propID
#ifdef HERMESVM_API_TRACE_DEBUG
,
const std::string &propNameDbg
#endif
)
: Record(time),
objID_(objID),
#ifdef HERMESVM_API_TRACE_DEBUG
propNameDbg_(propNameDbg),
#endif
propID_(propID) {
}
bool operator==(const Record &that) const final;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> uses() const override {
std::vector<ObjectID> vec{objID_};
pushIfTrackedValue(propID_, vec);
return vec;
}
};
struct GetPropertyNamesRecord final : public Record {
static constexpr RecordType type{RecordType::GetPropertyNames};
const ObjectID objID_;
// Since getPropertyNames always returns an array, this can be an object id
// rather than a TraceValue.
const ObjectID propNamesID_;
explicit GetPropertyNamesRecord(
TimeSinceStart time,
ObjectID objID,
ObjectID propNamesID)
: Record(time), objID_(objID), propNamesID_(propNamesID) {}
bool operator==(const Record &that) const final;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
return {propNamesID_};
}
std::vector<ObjectID> uses() const override {
return {objID_};
}
};
/// A CreateArrayRecord is an event where a new array is created of a specific
/// length.
struct CreateArrayRecord final : public Record {
static constexpr RecordType type{RecordType::CreateArray};
const ObjectID objID_;
const size_t length_;
explicit CreateArrayRecord(
TimeSinceStart time,
ObjectID objID,
size_t length)
: Record(time), objID_(objID), length_(length) {}
bool operator==(const Record &that) const final;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
return {objID_};
}
};
struct ArrayReadOrWriteRecord : public Record {
const ObjectID objID_;
const size_t index_;
const TraceValue value_;
explicit ArrayReadOrWriteRecord(
TimeSinceStart time,
ObjectID objID,
size_t index,
TraceValue value)
: Record(time), objID_(objID), index_(index), value_(value) {}
bool operator==(const Record &that) const final;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
std::vector<ObjectID> uses() const override {
return {objID_};
}
};
/// An ArrayReadRecord is an event where a value was read from an index
/// of an array.
/// It is modeled separately from GetProperty because it is more efficient to
/// read from a numeric index on an array than a string.
struct ArrayReadRecord final : public ArrayReadOrWriteRecord {
static constexpr RecordType type{RecordType::ArrayRead};
using ArrayReadOrWriteRecord::ArrayReadOrWriteRecord;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> defs() const override {
auto defs = ArrayReadOrWriteRecord::defs();
pushIfTrackedValue(value_, defs);
return defs;
}
};
/// An ArrayWriteRecord is an event where a value was written into an index
/// of an array.
struct ArrayWriteRecord final : public ArrayReadOrWriteRecord {
static constexpr RecordType type{RecordType::ArrayWrite};
using ArrayReadOrWriteRecord::ArrayReadOrWriteRecord;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> uses() const override {
auto uses = ArrayReadOrWriteRecord::uses();
pushIfTrackedValue(value_, uses);
return uses;
}
};
struct CallRecord : public Record {
/// The functionID_ is the id of the function JS object that is called from
/// JS.
const ObjectID functionID_;
const TraceValue thisArg_;
/// The arguments given to a call (excluding the this parameter),
/// already JSON stringified.
const std::vector<TraceValue> args_;
explicit CallRecord(
TimeSinceStart time,
ObjectID functionID,
TraceValue thisArg,
const std::vector<TraceValue> &args)
: Record(time),
functionID_(functionID),
thisArg_(thisArg),
args_(args) {}
bool operator==(const Record &that) const final;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
std::vector<ObjectID> uses() const override {
// The function is used regardless of direction.
return {functionID_};
}
protected:
std::vector<ObjectID> getArgTrackedIDs() const {
std::vector<ObjectID> objs;
pushIfTrackedValue(thisArg_, objs);
for (const auto &arg : args_) {
pushIfTrackedValue(arg, objs);
}
return objs;
}
};
/// A CallFromNativeRecord is an event where native code calls into a JS
/// function.
struct CallFromNativeRecord : public CallRecord {
static constexpr RecordType type{RecordType::CallFromNative};
using CallRecord::CallRecord;
RecordType getType() const override {
return type;
}
std::vector<ObjectID> uses() const override {
auto uses = CallRecord::uses();
auto objs = CallRecord::getArgTrackedIDs();
uses.insert(uses.end(), objs.begin(), objs.end());
return uses;
}
};
/// A ConstructFromNativeRecord is the same as \c CallFromNativeRecord, except
/// the function is called with the new operator.
struct ConstructFromNativeRecord final : public CallFromNativeRecord {
static constexpr RecordType type{RecordType::ConstructFromNative};
using CallFromNativeRecord::CallFromNativeRecord;
RecordType getType() const override {
return type;
}
};
/// A ReturnFromNativeRecord is an event where a native function returns to a
/// JS caller.
/// It pairs with \c CallToNativeRecord.
struct ReturnFromNativeRecord final : public Record, public ReturnMixin {
static constexpr RecordType type{RecordType::ReturnFromNative};
ReturnFromNativeRecord(TimeSinceStart time, TraceValue retVal)
: Record(time), ReturnMixin(retVal) {}
RecordType getType() const override {
return type;
}
std::vector<ObjectID> uses() const override {
auto uses = Record::uses();
pushIfTrackedValue(retVal_, uses);
return uses;
}
bool operator==(const Record &that) const final;
void toJSONInternal(::hermes::JSONEmitter &json) const override;
};
/// A ReturnToNativeRecord is an event where a JS function returns to a native
/// caller.