forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 3
/
d8-test.cc
1505 lines (1353 loc) · 59.7 KB
/
d8-test.cc
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 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/d8/d8.h"
#include "include/v8-fast-api-calls.h"
#include "include/v8-template.h"
#include "src/api/api-inl.h"
// This file exposes a d8.test.fast_c_api object, which adds testing facility
// for writing mjsunit tests that exercise fast API calls. The fast_c_api object
// contains an `add_all` method with the following signature:
// double add_all(bool /*should_fallback*/, int32_t, uint32_t,
// int64_t, uint64_t, float, double), that is wired as a "fast API" method.
// The fast_c_api object also supports querying the number of fast/slow calls
// and resetting these counters.
// Make sure to sync the following with src/compiler/globals.h.
#if defined(V8_TARGET_ARCH_X64) || defined(V8_TARGET_ARCH_ARM64) || \
defined(V8_TARGET_ARCH_MIPS64) || defined(V8_TARGET_ARCH_LOONG64)
#define V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
#endif
namespace v8 {
namespace {
#define CHECK_SELF_OR_FALLBACK(return_value) \
if (!self) { \
options.fallback = 1; \
return return_value; \
}
#define CHECK_SELF_OR_THROW() \
if (!self) { \
args.GetIsolate()->ThrowError( \
"This method is not defined on objects inheriting from FastCAPI."); \
return; \
}
class FastCApiObject {
public:
static FastCApiObject& instance();
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType CopyStringFastCallbackPatch(AnyCType receiver,
AnyCType should_fallback,
AnyCType source, AnyCType out,
AnyCType options) {
AnyCType ret;
CopyStringFastCallback(receiver.object_value, should_fallback.bool_value,
*source.string_value, *out.uint8_ta_value,
*options.options_value);
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static void CopyStringFastCallback(Local<Object> receiver,
bool should_fallback,
const FastOneByteString& source,
const FastApiTypedArray<uint8_t>& out,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
} else {
options.fallback = false;
}
uint8_t* memory = nullptr;
CHECK(out.getStorageIfAligned(&memory));
memcpy(memory, source.data, source.length);
}
static void CopyStringSlowCallback(const FunctionCallbackInfo<Value>& args) {
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType AddAllFastCallbackPatch(AnyCType receiver,
AnyCType should_fallback,
AnyCType arg_i32, AnyCType arg_u32,
AnyCType arg_i64, AnyCType arg_u64,
AnyCType arg_f32, AnyCType arg_f64,
AnyCType options) {
AnyCType ret;
ret.double_value = AddAllFastCallback(
receiver.object_value, should_fallback.bool_value, arg_i32.int32_value,
arg_u32.uint32_value, arg_i64.int64_value, arg_u64.uint64_value,
arg_f32.float_value, arg_f64.double_value, *options.options_value);
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static double AddAllFastCallback(Local<Object> receiver, bool should_fallback,
int32_t arg_i32, uint32_t arg_u32,
int64_t arg_i64, uint64_t arg_u64,
float arg_f32, double arg_f64,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_SELF_OR_FALLBACK(0);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
return 0;
} else {
options.fallback = false;
}
return static_cast<double>(arg_i32) + static_cast<double>(arg_u32) +
static_cast<double>(arg_i64) + static_cast<double>(arg_u64) +
static_cast<double>(arg_f32) + arg_f64;
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType AddAllFastCallbackNoOptionsPatch(
AnyCType receiver, AnyCType should_fallback, AnyCType arg_i32,
AnyCType arg_u32, AnyCType arg_i64, AnyCType arg_u64, AnyCType arg_f32,
AnyCType arg_f64) {
AnyCType ret;
ret.double_value = AddAllFastCallbackNoOptions(
receiver.object_value, should_fallback.bool_value, arg_i32.int32_value,
arg_u32.uint32_value, arg_i64.int64_value, arg_u64.uint64_value,
arg_f32.float_value, arg_f64.double_value);
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static double AddAllFastCallbackNoOptions(Local<Object> receiver,
bool should_fallback,
int32_t arg_i32, uint32_t arg_u32,
int64_t arg_i64, uint64_t arg_u64,
float arg_f32, double arg_f64) {
FastCApiObject* self;
// For Wasm call, we don't pass FastCApiObject as the receiver, so we need
// to retrieve the FastCApiObject instance from a static variable.
if (Utils::OpenHandle(*receiver)->IsJSGlobalProxy() ||
Utils::OpenHandle(*receiver)->IsUndefined()) {
// Note: FastCApiObject::instance() returns the reference of an object
// allocated in thread-local storage, its value cannot be stored in a
// static variable here.
self = &FastCApiObject::instance();
} else {
// Fuzzing code can call this function from JS; in this case the receiver
// should be a FastCApiObject.
self = UnwrapObject(receiver);
CHECK_NOT_NULL(self);
}
self->fast_call_count_++;
return static_cast<double>(arg_i32) + static_cast<double>(arg_u32) +
static_cast<double>(arg_i64) + static_cast<double>(arg_u64) +
static_cast<double>(arg_f32) + arg_f64;
}
static void AddAllSlowCallback(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
HandleScope handle_scope(isolate);
double sum = 0;
if (args.Length() > 1 && args[1]->IsNumber()) {
sum += args[1]->Int32Value(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 2 && args[2]->IsNumber()) {
sum += args[2]->Uint32Value(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 3 && args[3]->IsNumber()) {
sum += args[3]->IntegerValue(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 4 && args[4]->IsNumber()) {
sum += args[4]->IntegerValue(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 5 && args[5]->IsNumber()) {
sum += args[5]->NumberValue(isolate->GetCurrentContext()).FromJust();
} else {
sum += std::numeric_limits<double>::quiet_NaN();
}
if (args.Length() > 6 && args[6]->IsNumber()) {
sum += args[6]->NumberValue(isolate->GetCurrentContext()).FromJust();
} else {
sum += std::numeric_limits<double>::quiet_NaN();
}
args.GetReturnValue().Set(Number::New(isolate, sum));
}
#ifdef V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
typedef double Type;
#else
typedef int32_t Type;
#endif // V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType AddAllSequenceFastCallbackPatch(AnyCType receiver,
AnyCType should_fallback,
AnyCType seq_arg,
AnyCType options) {
AnyCType ret;
#ifdef V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
ret.double_value = AddAllSequenceFastCallback(
receiver.object_value, should_fallback.bool_value,
seq_arg.sequence_value, *options.options_value);
#else
ret.int32_value = AddAllSequenceFastCallback(
receiver.object_value, should_fallback.bool_value,
seq_arg.sequence_value, *options.options_value);
#endif // V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static Type AddAllSequenceFastCallback(Local<Object> receiver,
bool should_fallback,
Local<Array> seq_arg,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_SELF_OR_FALLBACK(0);
self->fast_call_count_++;
CHECK_NULL(options.wasm_memory);
if (should_fallback) {
options.fallback = true;
return 0;
}
uint32_t length = seq_arg->Length();
if (length > 1024) {
options.fallback = true;
return 0;
}
Type buffer[1024];
bool result = TryToCopyAndConvertArrayToCppBuffer<
CTypeInfoBuilder<Type>::Build().GetId(), Type>(seq_arg, buffer, 1024);
if (!result) {
options.fallback = true;
return 0;
}
DCHECK_EQ(seq_arg->Length(), length);
Type sum = 0;
for (uint32_t i = 0; i < length; ++i) {
sum += buffer[i];
}
return sum;
}
static void AddAllSequenceSlowCallback(
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
HandleScope handle_scope(isolate);
if (args.Length() < 2) {
self->slow_call_count_++;
isolate->ThrowError("This method expects at least 2 arguments.");
return;
}
if (args[1]->IsTypedArray()) {
AddAllTypedArraySlowCallback(args);
return;
}
self->slow_call_count_++;
if (args[1]->IsUndefined()) {
Type dummy_result = 0;
args.GetReturnValue().Set(Number::New(isolate, dummy_result));
return;
}
if (!args[1]->IsArray()) {
isolate->ThrowError("This method expects an array as a second argument.");
return;
}
Local<Array> seq_arg = args[1].As<Array>();
uint32_t length = seq_arg->Length();
if (length > 1024) {
isolate->ThrowError(
"Invalid length of array, must be between 0 and 1024.");
return;
}
Type sum = 0;
for (uint32_t i = 0; i < length; ++i) {
v8::MaybeLocal<v8::Value> maybe_element =
seq_arg->Get(isolate->GetCurrentContext(),
v8::Integer::NewFromUnsigned(isolate, i));
if (maybe_element.IsEmpty()) {
isolate->ThrowError("invalid element in JSArray");
return;
}
v8::Local<v8::Value> element = maybe_element.ToLocalChecked();
if (element->IsNumber()) {
double value = element->ToNumber(isolate->GetCurrentContext())
.ToLocalChecked()
->Value();
sum += value;
} else if (element->IsUndefined()) {
// Hole: ignore the element.
} else {
isolate->ThrowError("unexpected element type in JSArray");
return;
}
}
args.GetReturnValue().Set(Number::New(isolate, sum));
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
template <typename T>
static const FastApiTypedArray<T>* AnyCTypeToTypedArray(AnyCType arg);
template <>
const FastApiTypedArray<uint8_t>* AnyCTypeToTypedArray<uint8_t>(
AnyCType arg) {
return arg.uint8_ta_value;
}
template <>
const FastApiTypedArray<int32_t>* AnyCTypeToTypedArray<int32_t>(
AnyCType arg) {
return arg.int32_ta_value;
}
template <>
const FastApiTypedArray<uint32_t>* AnyCTypeToTypedArray<uint32_t>(
AnyCType arg) {
return arg.uint32_ta_value;
}
template <>
const FastApiTypedArray<int64_t>* AnyCTypeToTypedArray<int64_t>(
AnyCType arg) {
return arg.int64_ta_value;
}
template <>
const FastApiTypedArray<uint64_t>* AnyCTypeToTypedArray<uint64_t>(
AnyCType arg) {
return arg.uint64_ta_value;
}
template <>
const FastApiTypedArray<float>* AnyCTypeToTypedArray<float>(AnyCType arg) {
return arg.float_ta_value;
}
template <>
const FastApiTypedArray<double>* AnyCTypeToTypedArray<double>(AnyCType arg) {
return arg.double_ta_value;
}
template <typename T>
static AnyCType AddAllTypedArrayFastCallbackPatch(AnyCType receiver,
AnyCType should_fallback,
AnyCType typed_array_arg,
AnyCType options) {
AnyCType ret;
#ifdef V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
ret.double_value = AddAllTypedArrayFastCallback(
receiver.object_value, should_fallback.bool_value,
*AnyCTypeToTypedArray<T>(typed_array_arg), *options.options_value);
#else
ret.int32_value = AddAllTypedArrayFastCallback(
receiver.object_value, should_fallback.bool_value,
*AnyCTypeToTypedArray<T>(typed_array_arg), *options.options_value);
#endif // V8_ENABLE_FP_PARAMS_IN_C_LINKAGE
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
template <typename T>
static Type AddAllTypedArrayFastCallback(
Local<Object> receiver, bool should_fallback,
const FastApiTypedArray<T>& typed_array_arg,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_SELF_OR_FALLBACK(0);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
return 0;
}
T sum = 0;
for (unsigned i = 0; i < typed_array_arg.length(); ++i) {
sum += typed_array_arg.get(i);
}
return static_cast<Type>(sum);
}
static void AddAllTypedArraySlowCallback(
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
HandleScope handle_scope(isolate);
if (args.Length() < 2) {
isolate->ThrowError("This method expects at least 2 arguments.");
return;
}
if (!args[1]->IsTypedArray()) {
isolate->ThrowError(
"This method expects a TypedArray as a second argument.");
return;
}
Local<TypedArray> typed_array_arg = args[1].As<TypedArray>();
size_t length = typed_array_arg->Length();
void* data = typed_array_arg->Buffer()->GetBackingStore()->Data();
if (typed_array_arg->IsUint8Array() || typed_array_arg->IsInt32Array() ||
typed_array_arg->IsUint32Array() ||
typed_array_arg->IsBigInt64Array() ||
typed_array_arg->IsBigUint64Array()) {
int64_t sum = 0;
for (unsigned i = 0; i < length; ++i) {
if (typed_array_arg->IsUint8Array()) {
sum += static_cast<uint8_t*>(data)[i];
} else if (typed_array_arg->IsInt32Array()) {
sum += static_cast<int32_t*>(data)[i];
} else if (typed_array_arg->IsUint32Array()) {
sum += static_cast<uint32_t*>(data)[i];
} else if (typed_array_arg->IsBigInt64Array()) {
sum += static_cast<int64_t*>(data)[i];
} else if (typed_array_arg->IsBigUint64Array()) {
sum += static_cast<uint64_t*>(data)[i];
}
}
args.GetReturnValue().Set(Number::New(isolate, sum));
} else if (typed_array_arg->IsFloat32Array() ||
typed_array_arg->IsFloat64Array()) {
double sum = 0;
for (unsigned i = 0; i < length; ++i) {
if (typed_array_arg->IsFloat32Array()) {
sum += static_cast<float*>(data)[i];
} else if (typed_array_arg->IsFloat64Array()) {
sum += static_cast<double*>(data)[i];
}
}
args.GetReturnValue().Set(Number::New(isolate, sum));
} else {
isolate->ThrowError("TypedArray type is not supported.");
return;
}
}
static int32_t AddAllIntInvalidCallback(Local<Object> receiver,
bool should_fallback, int32_t arg_i32,
FastApiCallbackOptions& options) {
// This should never be called
UNREACHABLE();
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType Add32BitIntFastCallbackPatch(AnyCType receiver,
AnyCType should_fallback,
AnyCType arg_i32,
AnyCType arg_u32,
AnyCType options) {
AnyCType ret;
ret.int32_value = Add32BitIntFastCallback(
receiver.object_value, should_fallback.bool_value, arg_i32.int32_value,
arg_u32.uint32_value, *options.options_value);
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static int Add32BitIntFastCallback(v8::Local<v8::Object> receiver,
bool should_fallback, int32_t arg_i32,
uint32_t arg_u32,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_SELF_OR_FALLBACK(0);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
return 0;
}
return arg_i32 + arg_u32;
}
static void Add32BitIntSlowCallback(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
HandleScope handle_scope(isolate);
double sum = 0;
if (args.Length() > 1 && args[1]->IsNumber()) {
sum += args[1]->Int32Value(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 2 && args[2]->IsNumber()) {
sum += args[2]->Uint32Value(isolate->GetCurrentContext()).FromJust();
}
args.GetReturnValue().Set(Number::New(isolate, sum));
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType AddAll32BitIntFastCallback_8ArgsPatch(
AnyCType receiver, AnyCType should_fallback, AnyCType arg1_i32,
AnyCType arg2_i32, AnyCType arg3_i32, AnyCType arg4_u32,
AnyCType arg5_u32, AnyCType arg6_u32, AnyCType arg7_u32,
AnyCType arg8_u32, AnyCType options) {
AnyCType ret;
ret.int32_value = AddAll32BitIntFastCallback_8Args(
receiver.object_value, should_fallback.bool_value, arg1_i32.int32_value,
arg2_i32.int32_value, arg3_i32.int32_value, arg4_u32.uint32_value,
arg5_u32.uint32_value, arg6_u32.uint32_value, arg7_u32.uint32_value,
arg8_u32.uint32_value, *options.options_value);
return ret;
}
static AnyCType AddAll32BitIntFastCallback_6ArgsPatch(
AnyCType receiver, AnyCType should_fallback, AnyCType arg1_i32,
AnyCType arg2_i32, AnyCType arg3_i32, AnyCType arg4_u32,
AnyCType arg5_u32, AnyCType arg6_u32, AnyCType options) {
AnyCType ret;
ret.int32_value = AddAll32BitIntFastCallback_6Args(
receiver.object_value, should_fallback.bool_value, arg1_i32.int32_value,
arg2_i32.int32_value, arg3_i32.int32_value, arg4_u32.uint32_value,
arg5_u32.uint32_value, arg6_u32.uint32_value, *options.options_value);
return ret;
}
static AnyCType AddAll32BitIntFastCallback_5ArgsPatch(
AnyCType receiver, AnyCType should_fallback, AnyCType arg1_i32,
AnyCType arg2_i32, AnyCType arg3_i32, AnyCType arg4_u32,
AnyCType arg5_u32, AnyCType options) {
AnyCType arg6;
arg6.uint32_value = 0;
return AddAll32BitIntFastCallback_6ArgsPatch(
receiver, should_fallback, arg1_i32, arg2_i32, arg3_i32, arg4_u32,
arg5_u32, arg6, options);
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static int AddAll32BitIntFastCallback_8Args(
Local<Object> receiver, bool should_fallback, int32_t arg1_i32,
int32_t arg2_i32, int32_t arg3_i32, uint32_t arg4_u32, uint32_t arg5_u32,
uint32_t arg6_u32, uint32_t arg7_u32, uint32_t arg8_u32,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_SELF_OR_FALLBACK(0);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
return 0;
}
int64_t result = static_cast<int64_t>(arg1_i32) + arg2_i32 + arg3_i32 +
arg4_u32 + arg5_u32 + arg6_u32 + arg7_u32 + arg8_u32;
if (result > INT_MAX) return INT_MAX;
if (result < INT_MIN) return INT_MIN;
return static_cast<int>(result);
}
static int AddAll32BitIntFastCallback_6Args(
Local<Object> receiver, bool should_fallback, int32_t arg1_i32,
int32_t arg2_i32, int32_t arg3_i32, uint32_t arg4_u32, uint32_t arg5_u32,
uint32_t arg6_u32, FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_SELF_OR_FALLBACK(0);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
return 0;
}
int64_t result = static_cast<int64_t>(arg1_i32) + arg2_i32 + arg3_i32 +
arg4_u32 + arg5_u32 + arg6_u32;
if (result > INT_MAX) return INT_MAX;
if (result < INT_MIN) return INT_MIN;
return static_cast<int>(result);
}
static int AddAll32BitIntFastCallback_5Args(
Local<Object> receiver, bool should_fallback, int32_t arg1_i32,
int32_t arg2_i32, int32_t arg3_i32, uint32_t arg4_u32, uint32_t arg5_u32,
FastApiCallbackOptions& options) {
return AddAll32BitIntFastCallback_6Args(receiver, should_fallback, arg1_i32,
arg2_i32, arg3_i32, arg4_u32,
arg5_u32, 0, options);
}
static void AddAll32BitIntSlowCallback(
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
HandleScope handle_scope(isolate);
Local<Context> context = isolate->GetCurrentContext();
double sum = 0;
if (args.Length() > 1 && args[1]->IsNumber()) {
sum += args[1]->Int32Value(context).FromJust();
}
if (args.Length() > 2 && args[2]->IsNumber()) {
sum += args[2]->Int32Value(context).FromJust();
}
if (args.Length() > 3 && args[3]->IsNumber()) {
sum += args[3]->Int32Value(context).FromJust();
}
if (args.Length() > 4 && args[4]->IsNumber()) {
sum += args[4]->Uint32Value(context).FromJust();
}
if (args.Length() > 5 && args[5]->IsNumber()) {
sum += args[5]->Uint32Value(context).FromJust();
}
if (args.Length() > 6 && args[6]->IsNumber()) {
sum += args[6]->Uint32Value(context).FromJust();
}
if (args.Length() > 7 && args[7]->IsNumber() && args[8]->IsNumber()) {
sum += args[7]->Uint32Value(context).FromJust();
sum += args[8]->Uint32Value(context).FromJust();
}
args.GetReturnValue().Set(Number::New(isolate, sum));
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
template <v8::CTypeInfo::Flags flags>
static AnyCType AddAllAnnotateFastCallbackPatch(
AnyCType receiver, AnyCType should_fallback, AnyCType arg_i32,
AnyCType arg_u32, AnyCType arg_i64, AnyCType arg_u64, AnyCType options) {
AnyCType ret;
ret.double_value = AddAllAnnotateFastCallback<flags>(
receiver.object_value, should_fallback.bool_value, arg_i32.int32_value,
arg_u32.uint32_value, arg_i64.int64_value, arg_u64.uint64_value,
*options.options_value);
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
template <v8::CTypeInfo::Flags flags>
static double AddAllAnnotateFastCallback(Local<Object> receiver,
bool should_fallback,
int32_t arg_i32, uint32_t arg_u32,
int64_t arg_i64, uint64_t arg_u64,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_NOT_NULL(self);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
return 0;
}
return static_cast<double>(arg_i32) + static_cast<double>(arg_u32) +
static_cast<double>(arg_i64) + static_cast<double>(arg_u64);
}
static void AddAllAnnotateSlowCallback(
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
HandleScope handle_scope(isolate);
double sum = 0;
if (args.Length() > 1 && args[1]->IsNumber()) {
sum += args[1]->Int32Value(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 2 && args[2]->IsNumber()) {
sum += args[2]->Uint32Value(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 3 && args[3]->IsNumber()) {
sum += args[3]->IntegerValue(isolate->GetCurrentContext()).FromJust();
}
if (args.Length() > 4 && args[4]->IsNumber()) {
sum += args[4]->IntegerValue(isolate->GetCurrentContext()).FromJust();
}
args.GetReturnValue().Set(Number::New(isolate, sum));
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType EnforceRangeCompareI32Patch(AnyCType receiver,
AnyCType in_range,
AnyCType real_arg,
AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.bool_value = EnforceRangeCompare<int32_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.int32_value, *options.options_value);
return ret;
}
static AnyCType EnforceRangeCompareU32Patch(AnyCType receiver,
AnyCType in_range,
AnyCType real_arg,
AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.bool_value = EnforceRangeCompare<uint32_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.uint32_value, *options.options_value);
return ret;
}
static AnyCType EnforceRangeCompareI64Patch(AnyCType receiver,
AnyCType in_range,
AnyCType real_arg,
AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.bool_value = EnforceRangeCompare<int64_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.int64_value, *options.options_value);
return ret;
}
static AnyCType EnforceRangeCompareU64Patch(AnyCType receiver,
AnyCType in_range,
AnyCType real_arg,
AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.bool_value = EnforceRangeCompare<uint64_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.uint64_value, *options.options_value);
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
template <typename IntegerT>
static bool EnforceRangeCompare(Local<Object> receiver, bool in_range,
double real_arg, IntegerT checked_arg,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_NOT_NULL(self);
self->fast_call_count_++;
if (!i::v8_flags.fuzzing) {
// Number is in range.
CHECK(in_range && "Number range should have been enforced");
if (!std::isnan(real_arg)) {
CHECK_EQ(static_cast<IntegerT>(real_arg), checked_arg);
}
}
return true;
}
template <typename IntegerT>
static void EnforceRangeCompareSlowCallback(
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
HandleScope handle_scope(isolate);
if (i::v8_flags.fuzzing) {
args.GetReturnValue().Set(Boolean::New(isolate, false));
return;
}
double real_arg = 0;
if (args.Length() > 1 && args[1]->IsNumber()) {
real_arg = args[1]->NumberValue(isolate->GetCurrentContext()).FromJust();
}
bool in_range =
args[0]->IsBoolean() && args[0]->BooleanValue(isolate) &&
!std::isnan(real_arg) &&
real_arg <= static_cast<double>(std::numeric_limits<IntegerT>::max()) &&
real_arg >= static_cast<double>(std::numeric_limits<IntegerT>::min());
if (in_range) {
IntegerT checked_arg = std::numeric_limits<IntegerT>::max();
if (args.Length() > 2 && args[2]->IsNumber()) {
checked_arg =
args[2]->NumberValue(isolate->GetCurrentContext()).FromJust();
}
CHECK_EQ(static_cast<IntegerT>(real_arg), checked_arg);
args.GetReturnValue().Set(Boolean::New(isolate, false));
} else {
args.GetIsolate()->ThrowError("Argument out of range.");
}
}
#ifdef V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
static AnyCType ClampCompareI32Patch(AnyCType receiver, AnyCType in_range,
AnyCType real_arg, AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.double_value = ClampCompare<int32_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.int32_value, *options.options_value);
return ret;
}
static AnyCType ClampCompareU32Patch(AnyCType receiver, AnyCType in_range,
AnyCType real_arg, AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.double_value = ClampCompare<uint32_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.uint32_value, *options.options_value);
return ret;
}
static AnyCType ClampCompareI64Patch(AnyCType receiver, AnyCType in_range,
AnyCType real_arg, AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.double_value = ClampCompare<int64_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.int64_value, *options.options_value);
return ret;
}
static AnyCType ClampCompareU64Patch(AnyCType receiver, AnyCType in_range,
AnyCType real_arg, AnyCType checked_arg,
AnyCType options) {
AnyCType ret;
ret.double_value = ClampCompare<uint64_t>(
receiver.object_value, in_range.bool_value, real_arg.double_value,
checked_arg.uint64_value, *options.options_value);
return ret;
}
#endif // V8_USE_SIMULATOR_WITH_GENERIC_C_CALLS
template <typename IntegerT>
static double ClampCompareCompute(bool in_range, double real_arg,
IntegerT checked_arg) {
if (i::v8_flags.fuzzing) {
return static_cast<double>(checked_arg);
}
if (!in_range) {
IntegerT lower_bound = std::numeric_limits<IntegerT>::min();
IntegerT upper_bound = std::numeric_limits<IntegerT>::max();
if (lower_bound < internal::kMinSafeInteger) {
lower_bound = static_cast<IntegerT>(internal::kMinSafeInteger);
}
if (upper_bound > internal::kMaxSafeInteger) {
upper_bound = static_cast<IntegerT>(internal::kMaxSafeInteger);
}
CHECK(!std::isnan(real_arg));
if (real_arg < static_cast<double>(lower_bound)) {
CHECK_EQ(lower_bound, checked_arg);
} else if (real_arg > static_cast<double>(upper_bound)) {
CHECK_EQ(upper_bound, checked_arg);
} else {
FATAL("Expected value to be out of range.");
}
} else if (!std::isnan(real_arg)) {
if (real_arg != checked_arg) {
// Check if rounding towards nearest even number happened.
double diff = std::fabs(real_arg - checked_arg);
CHECK_LE(diff, 0.5);
if (diff == 0) {
// Check if rounding towards nearest even number happened.
CHECK_EQ(0, checked_arg % 2);
} else if (checked_arg % 2 == 1) {
// Behave as if rounding towards nearest even number *has*
// happened (as it does on the fast path).
checked_arg += 1;
}
} else {
CHECK_EQ(static_cast<IntegerT>(real_arg), checked_arg);
}
}
return checked_arg;
}
template <typename IntegerT>
static double ClampCompare(Local<Object> receiver, bool in_range,
double real_arg, IntegerT checked_arg,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_NOT_NULL(self);
self->fast_call_count_++;
double result = ClampCompareCompute(in_range, real_arg, checked_arg);
return static_cast<double>(result);
}
template <typename IntegerT>
static bool IsInRange(double arg) {
return !std::isnan(arg) &&
arg <= static_cast<double>(std::numeric_limits<IntegerT>::max()) &&
arg >= static_cast<double>(std::numeric_limits<IntegerT>::min());
}
template <typename IntegerT>
static void ClampCompareSlowCallback(
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
double real_arg = 0;
if (args.Length() > 1 && args[1]->IsNumber()) {
real_arg = args[1]->NumberValue(isolate->GetCurrentContext()).FromJust();
}
double checked_arg_dbl = std::numeric_limits<double>::max();
if (args.Length() > 2 && args[2]->IsNumber()) {
checked_arg_dbl = args[2].As<Number>()->Value();
}
bool in_range = args[0]->IsBoolean() && args[0]->BooleanValue(isolate) &&
IsInRange<IntegerT>(real_arg) &&
IsInRange<IntegerT>(checked_arg_dbl);
IntegerT checked_arg = std::numeric_limits<IntegerT>::max();
if (in_range) {
if (checked_arg_dbl != std::numeric_limits<double>::max()) {
checked_arg = static_cast<IntegerT>(checked_arg_dbl);
}
double result = ClampCompareCompute(in_range, real_arg, checked_arg);
args.GetReturnValue().Set(Number::New(isolate, result));
} else {
IntegerT clamped = std::numeric_limits<IntegerT>::max();
if (std::isnan(checked_arg_dbl)) {
clamped = 0;
} else {
IntegerT lower_bound = std::numeric_limits<IntegerT>::min();
IntegerT upper_bound = std::numeric_limits<IntegerT>::max();
if (lower_bound < internal::kMinSafeInteger) {
lower_bound = static_cast<IntegerT>(internal::kMinSafeInteger);
}
if (upper_bound > internal::kMaxSafeInteger) {
upper_bound = static_cast<IntegerT>(internal::kMaxSafeInteger);
}
clamped = std::clamp(real_arg, static_cast<double>(lower_bound),
static_cast<double>(upper_bound));
}
args.GetReturnValue().Set(Number::New(isolate, clamped));
}
}
static bool IsFastCApiObjectFastCallback(v8::Local<v8::Object> receiver,
bool should_fallback,
v8::Local<v8::Value> arg,
FastApiCallbackOptions& options) {
FastCApiObject* self = UnwrapObject(receiver);
CHECK_SELF_OR_FALLBACK(false);
self->fast_call_count_++;
if (should_fallback) {
options.fallback = true;
return false;
}
if (!arg->IsObject()) {
return false;
}
Local<Object> object = arg.As<Object>();
if (!IsValidApiObject(object)) return false;
internal::Isolate* i_isolate =
internal::IsolateFromNeverReadOnlySpaceObject(
*reinterpret_cast<internal::Address*>(*object));
CHECK_NOT_NULL(i_isolate);
Isolate* isolate = reinterpret_cast<Isolate*>(i_isolate);
HandleScope handle_scope(isolate);
return PerIsolateData::Get(isolate)
->GetTestApiObjectCtor()
->IsLeafTemplateForApiObject(object);
}
static void IsFastCApiObjectSlowCallback(
const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
FastCApiObject* self = UnwrapObject(args.This());
CHECK_SELF_OR_THROW();
self->slow_call_count_++;
HandleScope handle_scope(isolate);
bool result = false;
if (args.Length() < 2) {
args.GetIsolate()->ThrowError(
"is_valid_api_object should be called with 2 arguments");
return;
}
if (args[1]->IsObject()) {
Local<Object> object = args[1].As<Object>();
if (!IsValidApiObject(object)) {
result = false;
} else {
result = PerIsolateData::Get(args.GetIsolate())
->GetTestApiObjectCtor()
->IsLeafTemplateForApiObject(object);
}
}