-
Notifications
You must be signed in to change notification settings - Fork 30.2k
/
Copy pathobjects.cc
6654 lines (6031 loc) Β· 255 KB
/
objects.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 2015 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/objects/objects.h"
#include <algorithm>
#include <cmath>
#include <memory>
#include <optional>
#include <sstream>
#include <vector>
#include "src/api/api-arguments-inl.h"
#include "src/api/api-natives.h"
#include "src/api/api.h"
#include "src/ast/ast.h"
#include "src/ast/scopes.h"
#include "src/base/bits.h"
#include "src/base/debug/stack_trace.h"
#include "src/base/logging.h"
#include "src/base/overflowing-math.h"
#include "src/base/utils/random-number-generator.h"
#include "src/builtins/accessors.h"
#include "src/builtins/builtins.h"
#include "src/codegen/compiler.h"
#include "src/codegen/source-position-table.h"
#include "src/common/globals.h"
#include "src/common/message-template.h"
#include "src/date/date.h"
#include "src/debug/debug.h"
#include "src/diagnostics/code-tracer.h"
#include "src/execution/arguments.h"
#include "src/execution/execution.h"
#include "src/execution/frames-inl.h"
#include "src/execution/isolate-inl.h"
#include "src/execution/isolate-utils-inl.h"
#include "src/execution/isolate-utils.h"
#include "src/execution/microtask-queue.h"
#include "src/execution/protectors-inl.h"
#include "src/heap/factory-inl.h"
#include "src/heap/heap-inl.h"
#include "src/heap/local-factory-inl.h"
#include "src/heap/read-only-heap.h"
#include "src/ic/ic.h"
#include "src/init/bootstrapper.h"
#include "src/logging/counters.h"
#include "src/logging/log.h"
#include "src/logging/runtime-call-stats-scope.h"
#include "src/objects/allocation-site-inl.h"
#include "src/objects/allocation-site-scopes.h"
#include "src/objects/api-callbacks.h"
#include "src/objects/arguments-inl.h"
#include "src/objects/bigint.h"
#include "src/objects/call-site-info-inl.h"
#include "src/objects/cell-inl.h"
#include "src/objects/code-inl.h"
#include "src/objects/compilation-cache-table-inl.h"
#include "src/objects/debug-objects-inl.h"
#include "src/objects/dictionary.h"
#include "src/objects/elements.h"
#include "src/objects/embedder-data-array-inl.h"
#include "src/objects/field-index-inl.h"
#include "src/objects/field-index.h"
#include "src/objects/field-type.h"
#include "src/objects/foreign.h"
#include "src/objects/free-space-inl.h"
#include "src/objects/function-kind.h"
#include "src/objects/hash-table-inl.h"
#include "src/objects/heap-object-inl.h"
#include "src/objects/instance-type.h"
#include "src/objects/js-array-buffer-inl.h"
#include "src/objects/js-array-inl.h"
#include "src/objects/js-collection-inl.h"
#include "src/objects/js-disposable-stack-inl.h"
#include "src/objects/js-generator-inl.h"
#include "src/objects/js-regexp-inl.h"
#include "src/objects/js-regexp-string-iterator.h"
#include "src/objects/js-weak-refs-inl.h"
#include "src/objects/keys.h"
#include "src/objects/literal-objects-inl.h"
#include "src/objects/lookup-inl.h"
#include "src/objects/map-inl.h"
#include "src/objects/map-updater.h"
#include "src/objects/map.h"
#include "src/objects/megadom-handler-inl.h"
#include "src/objects/microtask-inl.h"
#include "src/objects/module-inl.h"
#include "src/objects/objects-body-descriptors-inl.h"
#include "src/objects/objects-inl.h"
#include "src/objects/promise-inl.h"
#include "src/objects/promise.h"
#include "src/objects/property-descriptor-object-inl.h"
#include "src/objects/property-descriptor.h"
#include "src/objects/property-details.h"
#include "src/objects/prototype.h"
#include "src/objects/slots-atomic-inl.h"
#include "src/objects/string-comparator.h"
#include "src/objects/string-set-inl.h"
#include "src/objects/struct-inl.h"
#include "src/objects/template-objects-inl.h"
#include "src/objects/transitions-inl.h"
#include "src/parsing/preparse-data.h"
#include "src/regexp/regexp.h"
#include "src/roots/roots.h"
#include "src/snapshot/deserializer.h"
#include "src/strings/string-builder-inl.h"
#include "src/strings/string-search.h"
#include "src/strings/string-stream.h"
#include "src/strings/unicode-decoder.h"
#include "src/strings/unicode-inl.h"
#include "src/utils/hex-format.h"
#include "src/utils/identity-map.h"
#include "src/utils/ostreams.h"
#include "src/utils/sha-256.h"
#include "src/utils/utils-inl.h"
#include "src/zone/zone.h"
#if V8_ENABLE_WEBASSEMBLY
#include "src/wasm/wasm-objects.h"
#endif // V8_ENABLE_WEBASSEMBLY
#ifdef V8_INTL_SUPPORT
#include "src/objects/js-break-iterator.h"
#include "src/objects/js-collator.h"
#include "src/objects/js-date-time-format.h"
#include "src/objects/js-list-format.h"
#include "src/objects/js-locale.h"
#include "src/objects/js-number-format.h"
#include "src/objects/js-plural-rules.h"
#include "src/objects/js-relative-time-format.h"
#include "src/objects/js-segment-iterator.h"
#include "src/objects/js-segmenter.h"
#include "src/objects/js-segments.h"
#endif // V8_INTL_SUPPORT
namespace v8::internal {
ShouldThrow GetShouldThrow(Isolate* isolate, Maybe<ShouldThrow> should_throw) {
if (should_throw.IsJust()) return should_throw.FromJust();
LanguageMode mode = isolate->context()->scope_info()->language_mode();
if (mode == LanguageMode::kStrict) return kThrowOnError;
for (StackFrameIterator it(isolate); !it.done(); it.Advance()) {
if (!it.frame()->is_java_script()) continue;
// Get the language mode from closure.
JavaScriptFrame* js_frame = static_cast<JavaScriptFrame*>(it.frame());
std::vector<Tagged<SharedFunctionInfo>> functions;
js_frame->GetFunctions(&functions);
LanguageMode closure_language_mode = functions.back()->language_mode();
if (closure_language_mode > mode) {
mode = closure_language_mode;
}
break;
}
return is_sloppy(mode) ? kDontThrow : kThrowOnError;
}
bool ComparisonResultToBool(Operation op, ComparisonResult result) {
switch (op) {
case Operation::kLessThan:
return result == ComparisonResult::kLessThan;
case Operation::kLessThanOrEqual:
return result == ComparisonResult::kLessThan ||
result == ComparisonResult::kEqual;
case Operation::kGreaterThan:
return result == ComparisonResult::kGreaterThan;
case Operation::kGreaterThanOrEqual:
return result == ComparisonResult::kGreaterThan ||
result == ComparisonResult::kEqual;
default:
break;
}
UNREACHABLE();
}
std::ostream& operator<<(std::ostream& os, InstanceType instance_type) {
if (InstanceTypeChecker::IsJSApiObject(instance_type)) {
return os << "[api object] "
<< static_cast<int16_t>(instance_type) -
i::Internals::kFirstJSApiObjectType;
}
switch (instance_type) {
#define WRITE_TYPE(TYPE) \
case TYPE: \
return os << #TYPE;
INSTANCE_TYPE_LIST(WRITE_TYPE)
#undef WRITE_TYPE
}
return os << "[unknown instance type " << static_cast<int16_t>(instance_type)
<< "]";
}
std::ostream& operator<<(std::ostream& os, PropertyCellType type) {
switch (type) {
case PropertyCellType::kUndefined:
return os << "Undefined";
case PropertyCellType::kConstant:
return os << "Constant";
case PropertyCellType::kConstantType:
return os << "ConstantType";
case PropertyCellType::kMutable:
return os << "Mutable";
case PropertyCellType::kInTransition:
return os << "InTransition";
}
UNREACHABLE();
}
// static
Handle<FieldType> Object::OptimalType(Tagged<Object> obj, Isolate* isolate,
Representation representation) {
if (representation.IsNone()) return FieldType::None(isolate);
if (v8_flags.track_field_types) {
if (representation.IsHeapObject() && IsHeapObject(obj)) {
// We can track only JavaScript objects with stable maps.
DirectHandle<Map> map(Cast<HeapObject>(obj)->map(), isolate);
if (map->is_stable() && IsJSReceiverMap(*map)) {
return FieldType::Class(map, isolate);
}
}
}
return FieldType::Any(isolate);
}
Handle<UnionOf<JSAny, Hole>> Object::NewStorageFor(
Isolate* isolate, Handle<UnionOf<JSAny, Hole>> object,
Representation representation) {
if (!representation.IsDouble()) return object;
Handle<HeapNumber> result = isolate->factory()->NewHeapNumberWithHoleNaN();
if (IsUninitialized(*object, isolate)) {
result->set_value_as_bits(kHoleNanInt64);
} else if (IsHeapNumber(*object)) {
// Ensure that all bits of the double value are preserved.
result->set_value_as_bits(Cast<HeapNumber>(*object)->value_as_bits());
} else {
result->set_value(Cast<Smi>(*object).value());
}
return result;
}
template <AllocationType allocation_type, typename IsolateT>
Handle<JSAny> Object::WrapForRead(IsolateT* isolate, Handle<JSAny> object,
Representation representation) {
DCHECK(!IsUninitialized(*object, isolate));
if (!representation.IsDouble()) {
DCHECK(Object::FitsRepresentation(*object, representation));
return object;
}
return isolate->factory()->template NewHeapNumberFromBits<allocation_type>(
Cast<HeapNumber>(*object)->value_as_bits());
}
template Handle<JSAny> Object::WrapForRead<AllocationType::kYoung>(
Isolate* isolate, Handle<JSAny> object, Representation representation);
template Handle<JSAny> Object::WrapForRead<AllocationType::kOld>(
LocalIsolate* isolate, Handle<JSAny> object, Representation representation);
MaybeHandle<JSReceiver> Object::ToObjectImpl(Isolate* isolate,
DirectHandle<Object> object,
const char* method_name) {
DCHECK(!IsJSReceiver(*object)); // Use ToObject() for fast path.
DirectHandle<Context> native_context = isolate->native_context();
Handle<JSFunction> constructor;
if (IsSmi(*object)) {
constructor = handle(native_context->number_function(), isolate);
} else {
int constructor_function_index =
Cast<HeapObject>(object)->map()->GetConstructorFunctionIndex();
if (constructor_function_index == Map::kNoConstructorFunctionIndex) {
if (method_name != nullptr) {
THROW_NEW_ERROR(
isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined,
isolate->factory()->NewStringFromAsciiChecked(
method_name)));
}
THROW_NEW_ERROR(isolate,
NewTypeError(MessageTemplate::kUndefinedOrNullToObject));
}
constructor = handle(
Cast<JSFunction>(native_context->get(constructor_function_index)),
isolate);
}
Handle<JSObject> result = isolate->factory()->NewJSObject(constructor);
Cast<JSPrimitiveWrapper>(result)->set_value(Cast<JSAny>(*object));
return result;
}
// ES6 section 9.2.1.2, OrdinaryCallBindThis for sloppy callee.
// static
MaybeHandle<JSReceiver> Object::ConvertReceiver(Isolate* isolate,
Handle<Object> object) {
if (IsJSReceiver(*object)) return Cast<JSReceiver>(object);
if (IsNullOrUndefined(*object, isolate)) {
return isolate->global_proxy();
}
return Object::ToObject(isolate, object);
}
// static
MaybeHandle<Number> Object::ConvertToNumber(Isolate* isolate,
Handle<Object> input) {
while (true) {
if (IsNumber(*input)) {
return Cast<Number>(input);
}
if (IsString(*input)) {
return String::ToNumber(isolate, Cast<String>(input));
}
if (IsOddball(*input)) {
return Oddball::ToNumber(isolate, Cast<Oddball>(input));
}
if (IsSymbol(*input)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber));
}
if (IsBigInt(*input)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kBigIntToNumber));
}
ASSIGN_RETURN_ON_EXCEPTION(
isolate, input,
JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(input),
ToPrimitiveHint::kNumber));
}
}
// static
MaybeHandle<Numeric> Object::ConvertToNumeric(Isolate* isolate,
Handle<Object> input) {
while (true) {
if (IsNumber(*input)) {
return Cast<Number>(input);
}
if (IsString(*input)) {
return String::ToNumber(isolate, Cast<String>(input));
}
if (IsOddball(*input)) {
return Oddball::ToNumber(isolate, Cast<Oddball>(input));
}
if (IsSymbol(*input)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber));
}
if (IsBigInt(*input)) {
return Cast<BigInt>(input);
}
ASSIGN_RETURN_ON_EXCEPTION(
isolate, input,
JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(input),
ToPrimitiveHint::kNumber));
}
}
// static
MaybeHandle<Number> Object::ConvertToInteger(Isolate* isolate,
Handle<Object> input) {
ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ConvertToNumber(isolate, input));
if (IsSmi(*input)) return Cast<Smi>(input);
return isolate->factory()->NewNumber(
DoubleToInteger(Cast<HeapNumber>(*input)->value()));
}
// static
MaybeHandle<Number> Object::ConvertToInt32(Isolate* isolate,
Handle<Object> input) {
ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ConvertToNumber(isolate, input));
if (IsSmi(*input)) return Cast<Smi>(input);
return isolate->factory()->NewNumberFromInt(
DoubleToInt32(Cast<HeapNumber>(*input)->value()));
}
// static
MaybeHandle<Number> Object::ConvertToUint32(Isolate* isolate,
Handle<Object> input) {
ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ConvertToNumber(isolate, input));
if (IsSmi(*input))
return handle(Smi::ToUint32Smi(Cast<Smi>(*input)), isolate);
return isolate->factory()->NewNumberFromUint(
DoubleToUint32(Cast<HeapNumber>(*input)->value()));
}
// static
MaybeHandle<Name> Object::ConvertToName(Isolate* isolate,
Handle<Object> input) {
ASSIGN_RETURN_ON_EXCEPTION(
isolate, input,
Object::ToPrimitive(isolate, input, ToPrimitiveHint::kString));
if (IsName(*input)) return Cast<Name>(input);
return ToString(isolate, input);
}
// ES6 7.1.14
// static
MaybeHandle<Object> Object::ConvertToPropertyKey(Isolate* isolate,
Handle<Object> value) {
// 1. Let key be ToPrimitive(argument, hint String).
MaybeHandle<Object> maybe_key =
Object::ToPrimitive(isolate, value, ToPrimitiveHint::kString);
// 2. ReturnIfAbrupt(key).
Handle<Object> key;
if (!maybe_key.ToHandle(&key)) return key;
// 3. If Type(key) is Symbol, then return key.
if (IsSymbol(*key)) return key;
// 4. Return ToString(key).
// Extending spec'ed behavior, we'd be happy to return an element index.
if (IsSmi(*key)) return key;
if (IsHeapNumber(*key)) {
uint32_t uint_value;
if (Object::ToArrayLength(*value, &uint_value) &&
uint_value <= static_cast<uint32_t>(Smi::kMaxValue)) {
return handle(Smi::FromInt(static_cast<int>(uint_value)), isolate);
}
}
return Object::ToString(isolate, key);
}
// static
MaybeHandle<String> Object::ConvertToString(Isolate* isolate,
Handle<Object> input) {
while (true) {
if (IsOddball(*input)) {
return handle(Cast<Oddball>(input)->to_string(), isolate);
}
if (IsNumber(*input)) {
return isolate->factory()->NumberToString(input);
}
if (IsSymbol(*input)) {
THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToString));
}
if (IsBigInt(*input)) {
return BigInt::ToString(isolate, Cast<BigInt>(input));
}
#if V8_ENABLE_WEBASSEMBLY
// We generally don't let the WasmNull escape into the JavaScript world,
// but some builtins may encounter it when called directly from Wasm code.
if (IsWasmNull(*input)) {
return isolate->factory()->null_string();
}
#endif
ASSIGN_RETURN_ON_EXCEPTION(
isolate, input,
JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(input),
ToPrimitiveHint::kString));
// The previous isString() check happened in Object::ToString and thus we
// put it at the end of the loop in this helper.
if (IsString(*input)) {
return Cast<String>(input);
}
}
}
namespace {
bool IsErrorObject(Isolate* isolate, Handle<Object> object) {
if (!IsJSObject(*object)) return false;
return ErrorUtils::HasErrorStackSymbolOwnProperty(isolate,
Cast<JSObject>(object));
}
Handle<String> AsStringOrEmpty(Isolate* isolate, Handle<Object> object) {
return IsString(*object) ? Cast<String>(object)
: isolate->factory()->empty_string();
}
DirectHandle<String> NoSideEffectsErrorToString(Isolate* isolate,
Handle<JSReceiver> error) {
Handle<Name> name_key = isolate->factory()->name_string();
Handle<Object> name = JSReceiver::GetDataProperty(isolate, error, name_key);
Handle<String> name_str = AsStringOrEmpty(isolate, name);
Handle<Name> msg_key = isolate->factory()->message_string();
Handle<Object> msg = JSReceiver::GetDataProperty(isolate, error, msg_key);
DirectHandle<String> msg_str = AsStringOrEmpty(isolate, msg);
if (name_str->length() == 0) return msg_str;
if (msg_str->length() == 0) return name_str;
constexpr const char error_suffix[] = "<a very large string>";
constexpr int error_suffix_size = sizeof(error_suffix);
int suffix_size = std::min(error_suffix_size, msg_str->length());
IncrementalStringBuilder builder(isolate);
if (name_str->length() + suffix_size + 2 /* ": " */ > String::kMaxLength) {
constexpr const char connector[] = "... : ";
int connector_size = sizeof(connector);
DirectHandle<String> truncated_name =
isolate->factory()->NewProperSubString(
name_str, 0,
name_str->length() - error_suffix_size - connector_size);
builder.AppendString(truncated_name);
builder.AppendCStringLiteral(connector);
builder.AppendCStringLiteral(error_suffix);
} else {
builder.AppendString(name_str);
builder.AppendCStringLiteral(": ");
if (builder.Length() + msg_str->length() <= String::kMaxLength) {
builder.AppendString(msg_str);
} else {
builder.AppendCStringLiteral(error_suffix);
}
}
return builder.Finish().ToHandleChecked();
}
} // namespace
// static
MaybeDirectHandle<String> Object::NoSideEffectsToMaybeString(
Isolate* isolate, DirectHandle<Object> input) {
DisallowJavascriptExecution no_js(isolate);
if (IsString(*input) || IsNumber(*input) || IsOddball(*input)) {
return Object::ToString(isolate, input).ToHandleChecked();
} else if (IsJSProxy(*input)) {
DirectHandle<Object> currInput = input;
do {
Tagged<HeapObject> target = Cast<JSProxy>(currInput)->target(isolate);
currInput = direct_handle(target, isolate);
} while (IsJSProxy(*currInput));
return NoSideEffectsToString(isolate, currInput);
} else if (IsBigInt(*input)) {
return BigInt::NoSideEffectsToString(isolate, Cast<BigInt>(input));
} else if (IsJSFunctionOrBoundFunctionOrWrappedFunction(*input)) {
// -- F u n c t i o n
Handle<String> fun_str;
if (IsJSBoundFunction(*input)) {
fun_str = JSBoundFunction::ToString(Cast<JSBoundFunction>(input));
} else if (IsJSWrappedFunction(*input)) {
fun_str = JSWrappedFunction::ToString(Cast<JSWrappedFunction>(input));
} else {
DCHECK(IsJSFunction(*input));
fun_str = JSFunction::ToString(Cast<JSFunction>(input));
}
if (fun_str->length() > 128) {
IncrementalStringBuilder builder(isolate);
builder.AppendString(isolate->factory()->NewSubString(fun_str, 0, 111));
builder.AppendCStringLiteral("...<omitted>...");
builder.AppendString(isolate->factory()->NewSubString(
fun_str, fun_str->length() - 2, fun_str->length()));
return builder.Finish().ToHandleChecked();
}
return fun_str;
} else if (IsSymbol(*input)) {
// -- S y m b o l
DirectHandle<Symbol> symbol = Cast<Symbol>(input);
if (symbol->is_private_name()) {
return DirectHandle<String>(Cast<String>(symbol->description()), isolate);
}
IncrementalStringBuilder builder(isolate);
builder.AppendCStringLiteral("Symbol(");
if (IsString(symbol->description())) {
Handle<String> description =
handle(Cast<String>(symbol->description()), isolate);
if (description->length() > 128) {
builder.AppendString(
isolate->factory()->NewSubString(description, 0, 56));
builder.AppendCStringLiteral("...<omitted>...");
builder.AppendString(isolate->factory()->NewSubString(
description, description->length() - 56, description->length()));
} else {
builder.AppendString(description);
}
}
builder.AppendCharacter(')');
return builder.Finish().ToHandleChecked();
} else if (IsJSReceiver(*input)) {
// -- J S R e c e i v e r
Handle<Object> indirect_input = indirect_handle(input, isolate);
Handle<JSReceiver> receiver = Cast<JSReceiver>(indirect_input);
DirectHandle<Object> to_string = JSReceiver::GetDataProperty(
isolate, receiver, isolate->factory()->toString_string());
if (IsErrorObject(isolate, indirect_input) ||
*to_string == *isolate->error_to_string()) {
// When internally formatting error objects, use a side-effects-free
// version of Error.prototype.toString independent of the actually
// installed toString method.
return NoSideEffectsErrorToString(isolate, receiver);
} else if (*to_string == *isolate->object_to_string()) {
Handle<Object> ctor = JSReceiver::GetDataProperty(
isolate, receiver, isolate->factory()->constructor_string());
if (IsJSFunctionOrBoundFunctionOrWrappedFunction(*ctor)) {
DirectHandle<String> ctor_name;
if (IsJSBoundFunction(*ctor)) {
ctor_name =
JSBoundFunction::GetName(isolate, Cast<JSBoundFunction>(ctor))
.ToHandleChecked();
} else if (IsJSFunction(*ctor)) {
ctor_name = JSFunction::GetName(isolate, Cast<JSFunction>(ctor));
}
if (ctor_name->length() != 0) {
IncrementalStringBuilder builder(isolate);
builder.AppendCStringLiteral("#<");
builder.AppendString(ctor_name);
builder.AppendCharacter('>');
return builder.Finish().ToHandleChecked();
}
}
}
}
return {};
}
// static
DirectHandle<String> Object::NoSideEffectsToString(Isolate* isolate,
DirectHandle<Object> input) {
DisallowJavascriptExecution no_js(isolate);
// Try to convert input to a meaningful string.
MaybeDirectHandle<String> maybe_string =
NoSideEffectsToMaybeString(isolate, input);
DirectHandle<String> string_handle;
if (maybe_string.ToHandle(&string_handle)) {
return string_handle;
}
// At this point, input is either none of the above or a JSReceiver.
Handle<JSReceiver> receiver;
if (IsJSReceiver(*input)) {
receiver = indirect_handle(Cast<JSReceiver>(input), isolate);
} else {
// This is the only case where Object::ToObject throws.
DCHECK(!IsSmi(*input));
int constructor_function_index =
Cast<HeapObject>(input)->map()->GetConstructorFunctionIndex();
if (constructor_function_index == Map::kNoConstructorFunctionIndex) {
return isolate->factory()->NewStringFromAsciiChecked("[object Unknown]");
}
receiver = Object::ToObjectImpl(isolate, input).ToHandleChecked();
}
DirectHandle<String> builtin_tag =
direct_handle(receiver->class_name(), isolate);
DirectHandle<Object> tag_obj = JSReceiver::GetDataProperty(
isolate, receiver, isolate->factory()->to_string_tag_symbol());
DirectHandle<String> tag =
IsString(*tag_obj) ? Cast<String>(tag_obj) : builtin_tag;
IncrementalStringBuilder builder(isolate);
builder.AppendCStringLiteral("[object ");
builder.AppendString(tag);
builder.AppendCharacter(']');
return builder.Finish().ToHandleChecked();
}
// static
MaybeHandle<Number> Object::ConvertToLength(Isolate* isolate,
Handle<Object> input) {
ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(isolate, input));
if (IsSmi(*input)) {
int value = std::max(Smi::ToInt(*input), 0);
return handle(Smi::FromInt(value), isolate);
}
double len = DoubleToInteger(Cast<HeapNumber>(*input)->value());
if (len <= 0.0) {
return handle(Smi::zero(), isolate);
} else if (len >= kMaxSafeInteger) {
len = kMaxSafeInteger;
}
return isolate->factory()->NewNumber(len);
}
// static
MaybeHandle<Number> Object::ConvertToIndex(Isolate* isolate,
Handle<Object> input,
MessageTemplate error_index) {
if (IsUndefined(*input, isolate)) return handle(Smi::zero(), isolate);
ASSIGN_RETURN_ON_EXCEPTION(isolate, input, ToNumber(isolate, input));
if (IsSmi(*input) && Smi::ToInt(*input) >= 0) return Cast<Smi>(input);
double len = DoubleToInteger(Object::NumberValue(Cast<Number>(*input)));
Handle<Number> js_len = isolate->factory()->NewNumber(len);
if (len < 0.0 || len > kMaxSafeInteger) {
THROW_NEW_ERROR(isolate, NewRangeError(error_index, js_len));
}
return js_len;
}
template <typename IsolateT>
// static
bool Object::BooleanValue(Tagged<Object> obj, IsolateT* isolate) {
if (IsSmi(obj)) return Smi::ToInt(obj) != 0;
DCHECK(IsHeapObject(obj));
if (IsBoolean(obj)) return IsTrue(obj, isolate);
if (IsNullOrUndefined(obj, isolate)) return false;
#ifdef V8_ENABLE_WEBASSEMBLY
if (IsWasmNull(obj)) return false;
#endif
if (IsUndetectable(obj)) return false; // Undetectable object is false.
if (IsString(obj)) return Cast<String>(obj)->length() != 0;
if (IsHeapNumber(obj)) return DoubleToBoolean(Cast<HeapNumber>(obj)->value());
if (IsBigInt(obj)) return Cast<BigInt>(obj)->ToBoolean();
return true;
}
template bool Object::BooleanValue(Tagged<Object>, Isolate*);
template bool Object::BooleanValue(Tagged<Object>, LocalIsolate*);
// static
Tagged<Object> Object::ToBoolean(Tagged<Object> obj, Isolate* isolate) {
if (IsBoolean(obj)) return obj;
return isolate->heap()->ToBoolean(Object::BooleanValue(obj, isolate));
}
namespace {
// TODO(bmeurer): Maybe we should introduce a marker interface Number,
// where we put all these methods at some point?
ComparisonResult StrictNumberCompare(double x, double y) {
if (std::isnan(x) || std::isnan(y)) {
return ComparisonResult::kUndefined;
} else if (x < y) {
return ComparisonResult::kLessThan;
} else if (x > y) {
return ComparisonResult::kGreaterThan;
} else {
return ComparisonResult::kEqual;
}
}
// See Number case of ES6#sec-strict-equality-comparison
// Returns false if x or y is NaN, treats -0.0 as equal to 0.0.
bool StrictNumberEquals(double x, double y) {
// Must check explicitly for NaN's on Windows, but -0 works fine.
if (std::isnan(x) || std::isnan(y)) return false;
return x == y;
}
bool StrictNumberEquals(const Tagged<Number> x, const Tagged<Number> y) {
return StrictNumberEquals(Object::NumberValue(x), Object::NumberValue(y));
}
bool StrictNumberEquals(DirectHandle<Number> x, DirectHandle<Number> y) {
return StrictNumberEquals(*x, *y);
}
ComparisonResult Reverse(ComparisonResult result) {
if (result == ComparisonResult::kLessThan) {
return ComparisonResult::kGreaterThan;
}
if (result == ComparisonResult::kGreaterThan) {
return ComparisonResult::kLessThan;
}
return result;
}
} // anonymous namespace
// static
Maybe<ComparisonResult> Object::Compare(Isolate* isolate, Handle<Object> x,
Handle<Object> y) {
// ES6 section 7.2.11 Abstract Relational Comparison step 3 and 4.
if (!Object::ToPrimitive(isolate, x, ToPrimitiveHint::kNumber).ToHandle(&x) ||
!Object::ToPrimitive(isolate, y, ToPrimitiveHint::kNumber).ToHandle(&y)) {
return Nothing<ComparisonResult>();
}
if (IsString(*x) && IsString(*y)) {
// ES6 section 7.2.11 Abstract Relational Comparison step 5.
return Just(String::Compare(isolate, Cast<String>(x), Cast<String>(y)));
}
if (IsBigInt(*x) && IsString(*y)) {
return BigInt::CompareToString(isolate, Cast<BigInt>(x), Cast<String>(y));
}
if (IsString(*x) && IsBigInt(*y)) {
Maybe<ComparisonResult> maybe_result =
BigInt::CompareToString(isolate, Cast<BigInt>(y), Cast<String>(x));
ComparisonResult result;
if (maybe_result.To(&result)) {
return Just(Reverse(result));
} else {
return Nothing<ComparisonResult>();
}
}
// ES6 section 7.2.11 Abstract Relational Comparison step 6.
if (!Object::ToNumeric(isolate, x).ToHandle(&x) ||
!Object::ToNumeric(isolate, y).ToHandle(&y)) {
return Nothing<ComparisonResult>();
}
bool x_is_number = IsNumber(*x);
bool y_is_number = IsNumber(*y);
if (x_is_number && y_is_number) {
return Just(
StrictNumberCompare(Object::NumberValue(*x), Object::NumberValue(*y)));
} else if (!x_is_number && !y_is_number) {
return Just(BigInt::CompareToBigInt(Cast<BigInt>(x), Cast<BigInt>(y)));
} else if (x_is_number) {
return Just(Reverse(BigInt::CompareToNumber(Cast<BigInt>(y), x)));
} else {
return Just(BigInt::CompareToNumber(Cast<BigInt>(x), y));
}
}
Maybe<bool> Object::Equals(Isolate* isolate, Handle<Object> x,
Handle<Object> y) {
// This is the generic version of Abstract Equality Comparison. Must be in
// sync with CodeStubAssembler::Equal.
while (true) {
if (IsNumber(*x)) {
if (IsNumber(*y)) {
return Just(StrictNumberEquals(Cast<Number>(*x), Cast<Number>(*y)));
} else if (IsBoolean(*y)) {
return Just(StrictNumberEquals(Cast<Number>(*x),
Cast<Oddball>(y)->to_number()));
} else if (IsString(*y)) {
return Just(StrictNumberEquals(
Cast<Number>(x), String::ToNumber(isolate, Cast<String>(y))));
} else if (IsBigInt(*y)) {
return Just(BigInt::EqualToNumber(Cast<BigInt>(y), Cast<Number>(x)));
} else if (IsJSReceiver(*y)) {
if (!JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(y))
.ToHandle(&y)) {
return Nothing<bool>();
}
} else {
return Just(false);
}
} else if (IsString(*x)) {
if (IsString(*y)) {
return Just(String::Equals(isolate, Cast<String>(x), Cast<String>(y)));
} else if (IsNumber(*y)) {
x = String::ToNumber(isolate, Cast<String>(x));
return Just(StrictNumberEquals(Cast<Number>(*x), Cast<Number>(*y)));
} else if (IsBoolean(*y)) {
x = String::ToNumber(isolate, Cast<String>(x));
return Just(StrictNumberEquals(Cast<Number>(*x),
Cast<Oddball>(y)->to_number()));
} else if (IsBigInt(*y)) {
return BigInt::EqualToString(isolate, Cast<BigInt>(y), Cast<String>(x));
} else if (IsJSReceiver(*y)) {
if (!JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(y))
.ToHandle(&y)) {
return Nothing<bool>();
}
} else {
return Just(false);
}
} else if (IsBoolean(*x)) {
if (IsOddball(*y)) {
return Just(x.is_identical_to(y));
} else if (IsNumber(*y)) {
return Just(StrictNumberEquals(Cast<Oddball>(x)->to_number(),
Cast<Number>(*y)));
} else if (IsString(*y)) {
y = String::ToNumber(isolate, Cast<String>(y));
return Just(StrictNumberEquals(Cast<Oddball>(x)->to_number(),
Cast<Number>(*y)));
} else if (IsBigInt(*y)) {
x = Oddball::ToNumber(isolate, Cast<Oddball>(x));
return Just(BigInt::EqualToNumber(Cast<BigInt>(y), x));
} else if (IsJSReceiver(*y)) {
if (!JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(y))
.ToHandle(&y)) {
return Nothing<bool>();
}
x = Oddball::ToNumber(isolate, Cast<Oddball>(x));
} else {
return Just(false);
}
} else if (IsSymbol(*x)) {
if (IsSymbol(*y)) {
return Just(x.is_identical_to(y));
} else if (IsJSReceiver(*y)) {
if (!JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(y))
.ToHandle(&y)) {
return Nothing<bool>();
}
} else {
return Just(false);
}
} else if (IsBigInt(*x)) {
if (IsBigInt(*y)) {
return Just(BigInt::EqualToBigInt(Cast<BigInt>(*x), Cast<BigInt>(*y)));
}
return Equals(isolate, y, x);
} else if (IsJSReceiver(*x)) {
if (IsJSReceiver(*y)) {
return Just(x.is_identical_to(y));
} else if (IsUndetectable(*y)) {
return Just(IsUndetectable(*x));
} else if (IsBoolean(*y)) {
y = Oddball::ToNumber(isolate, Cast<Oddball>(y));
} else if (!JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(x))
.ToHandle(&x)) {
return Nothing<bool>();
}
} else {
return Just(IsUndetectable(*x) && IsUndetectable(*y));
}
}
}
// static
bool Object::StrictEquals(Tagged<Object> obj, Tagged<Object> that) {
if (IsNumber(obj)) {
if (!IsNumber(that)) return false;
return StrictNumberEquals(Cast<Number>(obj), Cast<Number>(that));
} else if (IsString(obj)) {
if (!IsString(that)) return false;
return Cast<String>(obj)->Equals(Cast<String>(that));
} else if (IsBigInt(obj)) {
if (!IsBigInt(that)) return false;
return BigInt::EqualToBigInt(Cast<BigInt>(obj), Cast<BigInt>(that));
}
return obj == that;
}
// static
Handle<String> Object::TypeOf(Isolate* isolate, DirectHandle<Object> object) {
if (IsNumber(*object)) return isolate->factory()->number_string();
if (IsOddball(*object))
return handle(Cast<Oddball>(*object)->type_of(), isolate);
if (IsUndetectable(*object)) {
return isolate->factory()->undefined_string();
}
if (IsString(*object)) return isolate->factory()->string_string();
if (IsSymbol(*object)) return isolate->factory()->symbol_string();
if (IsBigInt(*object)) return isolate->factory()->bigint_string();
if (IsCallable(*object)) return isolate->factory()->function_string();
return isolate->factory()->object_string();
}
// static
MaybeHandle<Object> Object::Add(Isolate* isolate, Handle<Object> lhs,
Handle<Object> rhs) {
if (IsNumber(*lhs) && IsNumber(*rhs)) {
return isolate->factory()->NewNumber(
Object::NumberValue(Cast<Number>(*lhs)) +
Object::NumberValue(Cast<Number>(*rhs)));
} else if (IsString(*lhs) && IsString(*rhs)) {
return isolate->factory()->NewConsString(Cast<String>(lhs),
Cast<String>(rhs));
}
ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToPrimitive(isolate, lhs));
ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToPrimitive(isolate, rhs));
if (IsString(*lhs) || IsString(*rhs)) {
ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs, Object::ToString(isolate, rhs));
ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs, Object::ToString(isolate, lhs));
return isolate->factory()->NewConsString(Cast<String>(lhs),
Cast<String>(rhs));
}
Handle<Number> lhs_number;
Handle<Number> rhs_number;
ASSIGN_RETURN_ON_EXCEPTION(isolate, rhs_number,
Object::ToNumber(isolate, rhs));
ASSIGN_RETURN_ON_EXCEPTION(isolate, lhs_number,
Object::ToNumber(isolate, lhs));
return isolate->factory()->NewNumber(Object::NumberValue(*lhs_number) +
Object::NumberValue(*rhs_number));
}
// static
MaybeHandle<Object> Object::OrdinaryHasInstance(Isolate* isolate,
Handle<Object> callable,
Handle<Object> object) {
// The {callable} must have a [[Call]] internal method.
if (!IsCallable(*callable)) return isolate->factory()->false_value();
// Check if {callable} is a bound function, and if so retrieve its
// [[BoundTargetFunction]] and use that instead of {callable}.
if (IsJSBoundFunction(*callable)) {
// Since there is a mutual recursion here, we might run out of stack
// space for long chains of bound functions.
STACK_CHECK(isolate, MaybeHandle<Object>());
Handle<Object> bound_callable(
Cast<JSBoundFunction>(callable)->bound_target_function(), isolate);
return Object::InstanceOf(isolate, object, bound_callable);
}
// If {object} is not a receiver, return false.
if (!IsJSReceiver(*object)) return isolate->factory()->false_value();
// Get the "prototype" of {callable}; raise an error if it's not a receiver.
Handle<Object> prototype;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, prototype,
Object::GetProperty(isolate, callable,
isolate->factory()->prototype_string()));
if (!IsJSReceiver(*prototype)) {
THROW_NEW_ERROR(
isolate,
NewTypeError(MessageTemplate::kInstanceofNonobjectProto, prototype));
}
// Return whether or not {prototype} is in the prototype chain of {object}.
Maybe<bool> result = JSReceiver::HasInPrototypeChain(
isolate, Cast<JSReceiver>(object), prototype);
if (result.IsNothing()) return MaybeHandle<Object>();
return isolate->factory()->ToBoolean(result.FromJust());
}