forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindings.cpp
4685 lines (3924 loc) · 169 KB
/
bindings.cpp
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
#include "root.h"
#include "headers.h"
#include "BunClientData.h"
#include "GCDefferalContext.h"
#include "JavaScriptCore/AggregateError.h"
#include "JavaScriptCore/BytecodeIndex.h"
#include "JavaScriptCore/CodeBlock.h"
#include "JavaScriptCore/Completion.h"
#include "JavaScriptCore/ErrorInstance.h"
#include "JavaScriptCore/ExceptionHelpers.h"
#include "JavaScriptCore/ExceptionScope.h"
#include "JavaScriptCore/FunctionConstructor.h"
#include "JavaScriptCore/HeapSnapshotBuilder.h"
#include "JavaScriptCore/Identifier.h"
#include "JavaScriptCore/IteratorOperations.h"
#include "JavaScriptCore/JSArray.h"
#include "JavaScriptCore/JSArrayBuffer.h"
#include "JavaScriptCore/JSArrayInlines.h"
#include "JavaScriptCore/JSCallbackObject.h"
#include "JavaScriptCore/JSClassRef.h"
#include "JavaScriptCore/JSInternalPromise.h"
#include "JavaScriptCore/JSMap.h"
#include "JavaScriptCore/JSModuleLoader.h"
#include "JavaScriptCore/JSModuleRecord.h"
#include "JavaScriptCore/JSNativeStdFunction.h"
#include "JavaScriptCore/JSONObject.h"
#include "JavaScriptCore/JSObject.h"
#include "JavaScriptCore/JSSet.h"
#include "JavaScriptCore/JSString.h"
#include "JavaScriptCore/Microtask.h"
#include "JavaScriptCore/ObjectConstructor.h"
#include "JavaScriptCore/ParserError.h"
#include "JavaScriptCore/ScriptExecutable.h"
#include "JavaScriptCore/StackFrame.h"
#include "JavaScriptCore/StackVisitor.h"
#include "JavaScriptCore/VM.h"
#include "JavaScriptCore/WasmFaultSignalHandler.h"
#include "JavaScriptCore/Watchdog.h"
#include "ZigGlobalObject.h"
#include "helpers.h"
#include "wtf/text/ExternalStringImpl.h"
#include "wtf/text/StringCommon.h"
#include "wtf/text/StringImpl.h"
#include "wtf/text/StringView.h"
#include "wtf/text/WTFString.h"
#include "JavaScriptCore/FunctionPrototype.h"
#include "JSFetchHeaders.h"
#include "FetchHeaders.h"
#include "DOMURL.h"
#include "JSDOMURL.h"
#include <string_view>
#include <uws/src/App.h>
#include <uws/uSockets/src/internal/internal.h>
#include "IDLTypes.h"
#include "JSDOMBinding.h"
#include "JSDOMConstructor.h"
#include "JSDOMConvertBase.h"
#include "JSDOMConvertBoolean.h"
#include "JSDOMConvertInterface.h"
#include "JSDOMConvertNullable.h"
#include "JSDOMConvertRecord.h"
#include "JSDOMConvertSequences.h"
#include "JSDOMConvertStrings.h"
#include "JSDOMConvertUnion.h"
#include "JSDOMExceptionHandling.h"
#include "JSDOMGlobalObjectInlines.h"
#include "JSDOMIterator.h"
#include "JSDOMOperation.h"
#include "JSDOMWrapperCache.h"
#include "wtf/text/AtomString.h"
#include "HTTPHeaderNames.h"
#include "JSDOMPromiseDeferred.h"
#include "JavaScriptCore/TestRunnerUtils.h"
#include "JavaScriptCore/DateInstance.h"
#include "JavaScriptCore/RegExpObject.h"
#include "JavaScriptCore/PropertyNameArray.h"
#include "JavaScriptCore/HashMapImpl.h"
#include "JavaScriptCore/HashMapImplInlines.h"
#include "webcore/JSAbortSignal.h"
#include "JSAbortAlgorithm.h"
#include "DOMFormData.h"
#include "JSDOMFormData.h"
#include "ZigGeneratedClasses.h"
#include "JavaScriptCore/JSMapInlines.h"
#include <JavaScriptCore/JSWeakMap.h>
#include "JSURLSearchParams.h"
#include "AsyncContextFrame.h"
#include "JavaScriptCore/InternalFieldTuple.h"
template<typename UWSResponse>
static void copyToUWS(WebCore::FetchHeaders* headers, UWSResponse* res)
{
auto& internalHeaders = headers->internalHeaders();
for (auto& value : internalHeaders.getSetCookieHeaders()) {
res->writeHeader(std::string_view("set-cookie", 10), std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length()));
}
for (auto& header : internalHeaders.commonHeaders()) {
const auto& name = WebCore::httpHeaderNameString(header.key);
auto& value = header.value;
res->writeHeader(
std::string_view(name.is8Bit() ? reinterpret_cast<const char*>(name.characters8()) : name.utf8().data(), name.length()),
std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length()));
}
for (auto& header : internalHeaders.uncommonHeaders()) {
auto& name = header.key;
auto& value = header.value;
res->writeHeader(
std::string_view(name.is8Bit() ? reinterpret_cast<const char*>(name.characters8()) : name.utf8().data(), name.length()),
std::string_view(value.is8Bit() ? reinterpret_cast<const char*>(value.characters8()) : value.utf8().data(), value.length()));
}
}
using namespace JSC;
using namespace WebCore;
enum class AsymmetricMatcherResult : uint8_t {
PASS,
FAIL,
NOT_MATCHER,
};
AsymmetricMatcherResult matchAsymmetricMatcher(JSGlobalObject* globalObject, JSCell* matcherPropCell, JSValue otherProp, ThrowScope* throwScope)
{
VM& vm = globalObject->vm();
if (auto* expectAnything = jsDynamicCast<JSExpectAnything*>(matcherPropCell)) {
if (otherProp.isUndefinedOrNull()) {
return AsymmetricMatcherResult::FAIL;
}
return AsymmetricMatcherResult::PASS;
} else if (auto* expectAny = jsDynamicCast<JSExpectAny*>(matcherPropCell)) {
JSValue constructorValue = expectAny->m_constructorValue.get();
JSObject* constructorObject = constructorValue.getObject();
if (otherProp.isPrimitive()) {
if (otherProp.isNumber() && globalObject->numberObjectConstructor() == constructorObject) {
return AsymmetricMatcherResult::PASS;
} else if (otherProp.isBoolean() && globalObject->booleanObjectConstructor() == constructorObject) {
return AsymmetricMatcherResult::PASS;
} else if (otherProp.isSymbol() && globalObject->symbolObjectConstructor() == constructorObject) {
return AsymmetricMatcherResult::PASS;
} else if (otherProp.isString()) {
if (auto* constructorFunction = jsDynamicCast<JSFunction*>(constructorObject)) {
String name = constructorFunction->name(vm);
if (name == "String"_s) {
return AsymmetricMatcherResult::PASS;
}
} else if (auto* internalConstructorFunction = jsDynamicCast<InternalFunction*>(constructorObject)) {
String name = internalConstructorFunction->name();
if (name == "String"_s) {
return AsymmetricMatcherResult::PASS;
}
}
} else if (otherProp.isBigInt()) {
if (auto* constructorFunction = jsDynamicCast<JSFunction*>(constructorObject)) {
String name = constructorFunction->name(vm);
if (name == "BigInt"_s) {
return AsymmetricMatcherResult::PASS;
}
} else if (auto* internalConstructorFunction = jsDynamicCast<InternalFunction*>(constructorObject)) {
String name = internalConstructorFunction->name();
if (name == "BigInt"_s) {
return AsymmetricMatcherResult::PASS;
}
}
}
return AsymmetricMatcherResult::FAIL;
}
if (constructorObject->hasInstance(globalObject, otherProp)) {
return AsymmetricMatcherResult::PASS;
}
return AsymmetricMatcherResult::FAIL;
} else if (auto* expectStringContaining = jsDynamicCast<JSExpectStringContaining*>(matcherPropCell)) {
JSValue expectedSubstring = expectStringContaining->m_stringValue.get();
if (otherProp.isString()) {
String otherString = otherProp.toWTFString(globalObject);
RETURN_IF_EXCEPTION(*throwScope, AsymmetricMatcherResult::FAIL);
String substring = expectedSubstring.toWTFString(globalObject);
RETURN_IF_EXCEPTION(*throwScope, AsymmetricMatcherResult::FAIL);
if (otherString.find(substring) != WTF::notFound) {
return AsymmetricMatcherResult::PASS;
}
}
return AsymmetricMatcherResult::FAIL;
} else if (auto* expectStringMatching = jsDynamicCast<JSExpectStringMatching*>(matcherPropCell)) {
JSValue expectedTestValue = expectStringMatching->m_testValue.get();
if (otherProp.isString()) {
if (expectedTestValue.isString()) {
String otherString = otherProp.toWTFString(globalObject);
RETURN_IF_EXCEPTION(*throwScope, AsymmetricMatcherResult::FAIL);
String substring = expectedTestValue.toWTFString(globalObject);
RETURN_IF_EXCEPTION(*throwScope, AsymmetricMatcherResult::FAIL);
if (otherString.find(substring) != WTF::notFound) {
return AsymmetricMatcherResult::PASS;
}
} else if (expectedTestValue.isCell() and expectedTestValue.asCell()->type() == RegExpObjectType) {
if (auto* regex = jsDynamicCast<RegExpObject*>(expectedTestValue)) {
JSString* otherString = otherProp.toString(globalObject);
if (regex->match(globalObject, otherString)) {
return AsymmetricMatcherResult::PASS;
}
}
}
}
return AsymmetricMatcherResult::FAIL;
}
return AsymmetricMatcherResult::NOT_MATCHER;
}
template<typename PromiseType, bool isInternal>
static void handlePromise(PromiseType* promise, JSC__JSGlobalObject* globalObject, JSC::EncodedJSValue ctx, JSC__JSValue (*resolverFunction)(JSC__JSGlobalObject* arg0, JSC__CallFrame* callFrame), JSC__JSValue (*rejecterFunction)(JSC__JSGlobalObject* arg0, JSC__CallFrame* callFrame))
{
auto globalThis = reinterpret_cast<Zig::GlobalObject*>(globalObject);
if constexpr (!isInternal) {
JSFunction* performPromiseThenFunction = globalObject->performPromiseThenFunction();
auto callData = JSC::getCallData(performPromiseThenFunction);
ASSERT(callData.type != CallData::Type::None);
MarkedArgumentBuffer arguments;
arguments.append(promise);
arguments.append(globalThis->thenable(resolverFunction));
arguments.append(globalThis->thenable(rejecterFunction));
arguments.append(jsUndefined());
arguments.append(JSValue::decode(ctx));
ASSERT(!arguments.hasOverflowed());
// async context tracking is handled by performPromiseThenFunction internally.
JSC::profiledCall(globalThis, JSC::ProfilingReason::Microtask, performPromiseThenFunction, callData, jsUndefined(), arguments);
} else {
promise->then(globalThis, resolverFunction, rejecterFunction);
}
}
static bool canPerformFastPropertyEnumerationForIterationBun(Structure* s)
{
if (s->hasNonReifiedStaticProperties()) {
return false;
}
if (s->typeInfo().overridesGetOwnPropertySlot())
return false;
if (s->typeInfo().overridesAnyFormOfGetOwnPropertyNames())
return false;
// FIXME: Indexed properties can be handled.
// https://bugs.webkit.org/show_bug.cgi?id=185358
if (hasIndexedProperties(s->indexingType()))
return false;
if (s->hasAnyKindOfGetterSetterProperties())
return false;
if (s->isUncacheableDictionary())
return false;
// Cannot perform fast [[Put]] to |target| if the property names of the |source| contain "__proto__".
if (s->hasUnderscoreProtoPropertyExcludingOriginalProto())
return false;
return true;
}
JSValue getIndexWithoutAccessors(JSGlobalObject* globalObject, JSObject* obj, uint64_t i)
{
if (obj->canGetIndexQuickly(i)) {
return obj->tryGetIndexQuickly(i);
}
PropertySlot slot(obj, PropertySlot::InternalMethodType::Get);
if (obj->methodTable()->getOwnPropertySlotByIndex(obj, globalObject, i, slot)) {
if (!slot.isAccessor()) {
return slot.getValue(globalObject, i);
}
}
return JSValue();
}
template<bool isStrict, bool enableAsymmetricMatchers>
bool Bun__deepEquals(JSC__JSGlobalObject* globalObject, JSValue v1, JSValue v2, Vector<std::pair<JSC::JSValue, JSC::JSValue>, 16>& stack, ThrowScope* scope, bool addToStack)
{
VM& vm = globalObject->vm();
// need to check this before primitives, asymmetric matchers
// can match against any type of value.
if constexpr (enableAsymmetricMatchers) {
JSCell* c1 = v1.asCell();
JSCell* c2 = v2.asCell();
if (v2.isCell() && !v2.isEmpty() && c2->type() == JSC::JSType(JSDOMWrapperType)) {
switch (matchAsymmetricMatcher(globalObject, c2, v1, scope)) {
case AsymmetricMatcherResult::FAIL:
return false;
case AsymmetricMatcherResult::PASS:
return true;
case AsymmetricMatcherResult::NOT_MATCHER:
// continue comparison
break;
}
} else if (v1.isCell() && !v1.isEmpty() && c1->type() == JSC::JSType(JSDOMWrapperType)) {
switch (matchAsymmetricMatcher(globalObject, c1, v2, scope)) {
case AsymmetricMatcherResult::FAIL:
return false;
case AsymmetricMatcherResult::PASS:
return true;
case AsymmetricMatcherResult::NOT_MATCHER:
// continue comparison
break;
}
}
}
if (!v1.isEmpty() && !v2.isEmpty() && JSC::sameValue(globalObject, v1, v2)) {
return true;
}
if (v1.isEmpty() || v2.isEmpty())
return v1.isEmpty() == v2.isEmpty();
if (v1.isPrimitive() || v2.isPrimitive())
return false;
RELEASE_ASSERT(v1.isCell());
RELEASE_ASSERT(v2.isCell());
size_t length = stack.size();
size_t originalLength = length;
for (size_t i = 0; i < length; i++) {
auto values = stack.at(i);
if (JSC::JSValue::strictEqual(globalObject, values.first, v1)) {
return JSC::JSValue::strictEqual(globalObject, values.second, v2);
} else if (JSC::JSValue::strictEqual(globalObject, values.second, v2))
return false;
}
if (addToStack) {
stack.append({ v1, v2 });
}
JSCell* c1 = v1.asCell();
JSCell* c2 = v2.asCell();
JSObject* o1 = v1.getObject();
JSObject* o2 = v2.getObject();
// We use additional values outside the enum
// so the warning here is unnecessary
uint8_t c1Type = c1->type();
uint8_t c2Type = c2->type();
switch (c1Type) {
case JSSetType: {
if (c2Type != JSSetType) {
return false;
}
JSSet* set1 = jsCast<JSSet*>(c1);
JSSet* set2 = jsCast<JSSet*>(c2);
if (set1->size() != set2->size()) {
return false;
}
IterationRecord iterationRecord1 = iteratorForIterable(globalObject, v1);
bool isEqual = true;
while (true) {
JSValue next1 = iteratorStep(globalObject, iterationRecord1);
if (next1.isFalse()) {
break;
}
JSValue nextValue1 = iteratorValue(globalObject, next1);
RETURN_IF_EXCEPTION(*scope, false);
bool found = false;
IterationRecord iterationRecord2 = iteratorForIterable(globalObject, v2);
while (true) {
JSValue next2 = iteratorStep(globalObject, iterationRecord2);
if (UNLIKELY(next2.isFalse())) {
break;
}
JSValue nextValue2 = iteratorValue(globalObject, next2);
RETURN_IF_EXCEPTION(*scope, false);
// set has unique values, no need to count
if (Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, nextValue1, nextValue2, stack, scope, false)) {
found = true;
if (!nextValue1.isPrimitive()) {
stack.append({ nextValue1, nextValue2 });
}
break;
}
}
if (!found) {
isEqual = false;
break;
}
}
if (!isEqual) {
return false;
}
return true;
}
case JSMapType: {
if (c2Type != JSMapType) {
return false;
}
JSMap* map1 = jsCast<JSMap*>(c1);
JSMap* map2 = jsCast<JSMap*>(c2);
size_t leftSize = map1->size();
if (leftSize != map2->size()) {
return false;
}
IterationRecord iterationRecord1 = iteratorForIterable(globalObject, v1);
bool isEqual = true;
while (true) {
JSValue next1 = iteratorStep(globalObject, iterationRecord1);
if (next1.isFalse()) {
break;
}
JSValue nextValue1 = iteratorValue(globalObject, next1);
RETURN_IF_EXCEPTION(*scope, false);
if (UNLIKELY(!nextValue1.isObject())) {
return false;
}
JSObject* nextValueObject1 = asObject(nextValue1);
JSValue key1 = nextValueObject1->getIndex(globalObject, static_cast<unsigned>(0));
RETURN_IF_EXCEPTION(*scope, false);
JSValue value1 = nextValueObject1->getIndex(globalObject, static_cast<unsigned>(1));
RETURN_IF_EXCEPTION(*scope, false);
bool found = false;
IterationRecord iterationRecord2 = iteratorForIterable(globalObject, v2);
while (true) {
JSValue next2 = iteratorStep(globalObject, iterationRecord2);
if (UNLIKELY(next2.isFalse())) {
break;
}
JSValue nextValue2 = iteratorValue(globalObject, next2);
RETURN_IF_EXCEPTION(*scope, false);
if (UNLIKELY(!nextValue2.isObject())) {
return false;
}
JSObject* nextValueObject2 = asObject(nextValue2);
JSValue key2 = nextValueObject2->getIndex(globalObject, static_cast<unsigned>(0));
RETURN_IF_EXCEPTION(*scope, false);
JSValue value2 = nextValueObject2->getIndex(globalObject, static_cast<unsigned>(1));
RETURN_IF_EXCEPTION(*scope, false);
if (Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, key1, key2, stack, scope, false)) {
if (Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, nextValue1, nextValue2, stack, scope, false)) {
found = true;
if (!nextValue1.isPrimitive()) {
stack.append({ nextValue1, nextValue2 });
}
break;
}
}
}
if (!found) {
isEqual = false;
break;
}
}
if (!isEqual) {
return false;
}
return true;
}
case ArrayBufferType: {
if (c2Type != ArrayBufferType) {
return false;
}
JSC::ArrayBuffer* left = jsCast<JSArrayBuffer*>(v1)->impl();
JSC::ArrayBuffer* right = jsCast<JSArrayBuffer*>(v2)->impl();
size_t byteLength = left->byteLength();
if (right->byteLength() != byteLength) {
return false;
}
if (byteLength == 0)
return true;
if (UNLIKELY(right->isDetached() || left->isDetached())) {
return false;
}
const void* vector = left->data();
const void* rightVector = right->data();
if (UNLIKELY(!vector || !rightVector)) {
return false;
}
if (UNLIKELY(vector == rightVector))
return true;
return (memcmp(vector, rightVector, byteLength) == 0);
}
case JSDateType: {
if (c2Type != JSDateType) {
return false;
}
JSC::DateInstance* left = jsCast<DateInstance*>(v1);
JSC::DateInstance* right = jsCast<DateInstance*>(v2);
return left->internalNumber() == right->internalNumber();
}
case RegExpObjectType: {
if (c2Type != RegExpObjectType) {
return false;
}
if (JSC::RegExpObject* left = jsDynamicCast<JSC::RegExpObject*>(v1)) {
JSC::RegExpObject* right = jsDynamicCast<JSC::RegExpObject*>(v2);
if (UNLIKELY(!right)) {
return false;
}
return left->regExp()->key() == right->regExp()->key();
}
return false;
}
case Int8ArrayType:
case Uint8ArrayType:
case Uint8ClampedArrayType:
case Int16ArrayType:
case Uint16ArrayType:
case Int32ArrayType:
case Uint32ArrayType:
case Float32ArrayType:
case Float64ArrayType:
case BigInt64ArrayType:
case BigUint64ArrayType: {
if (!isTypedArrayType(static_cast<JSC::JSType>(c2Type))) {
return false;
}
JSC::JSArrayBufferView* left = jsCast<JSArrayBufferView*>(v1);
JSC::JSArrayBufferView* right = jsCast<JSArrayBufferView*>(v2);
size_t byteLength = left->byteLength();
if (right->byteLength() != byteLength) {
return false;
}
if (byteLength == 0)
return true;
if (UNLIKELY(right->isDetached() || left->isDetached())) {
return false;
}
const void* vector = left->vector();
const void* rightVector = right->vector();
if (UNLIKELY(!vector || !rightVector)) {
return false;
}
if (UNLIKELY(vector == rightVector))
return true;
return (memcmp(vector, rightVector, byteLength) == 0);
}
case StringObjectType: {
if (c2Type != StringObjectType) {
return false;
}
if (!equal(JSObject::calculatedClassName(o1), JSObject::calculatedClassName(o2))) {
return false;
}
JSString* s1 = c1->toStringInline(globalObject);
JSString* s2 = c2->toStringInline(globalObject);
return s1->equal(globalObject, s2);
}
case JSFunctionType: {
return false;
}
case JSDOMWrapperType: {
if (c2Type == JSDOMWrapperType) {
// https://github.com/oven-sh/bun/issues/4089
auto* url2 = jsDynamicCast<JSDOMURL*>(v2);
auto* url1 = jsDynamicCast<JSDOMURL*>(v1);
if constexpr (isStrict) {
// if one is a URL and the other is not a URL, toStrictEqual returns false.
if ((url2 == nullptr) != (url1 == nullptr)) {
return false;
}
if (url2 && url1) {
return url1->wrapped().href() != url2->wrapped().href();
}
} else {
if (url2 && url1) {
// toEqual should return false when the URLs' href is not equal
// But you could have added additional properties onto the
// url object itself, so we must check those as well
// But it's definitely not equal if the href() is not the same
if (url1->wrapped().href() != url2->wrapped().href()) {
return false;
}
}
}
}
break;
}
default: {
break;
}
}
bool v1Array = isArray(globalObject, v1);
RETURN_IF_EXCEPTION(*scope, false);
bool v2Array = isArray(globalObject, v2);
RETURN_IF_EXCEPTION(*scope, false);
if (v1Array != v2Array)
return false;
if (v1Array && v2Array) {
JSC::JSArray* array1 = JSC::jsCast<JSC::JSArray*>(v1);
JSC::JSArray* array2 = JSC::jsCast<JSC::JSArray*>(v2);
size_t array1Length = array1->length();
size_t array2Length = array2->length();
if constexpr (isStrict) {
if (array1Length != array2Length) {
return false;
}
}
uint64_t i = 0;
for (; i < array1Length; i++) {
JSValue left = getIndexWithoutAccessors(globalObject, o1, i);
RETURN_IF_EXCEPTION(*scope, false);
JSValue right = getIndexWithoutAccessors(globalObject, o2, i);
RETURN_IF_EXCEPTION(*scope, false);
if constexpr (isStrict) {
if (left.isEmpty() && right.isEmpty()) {
continue;
}
if (left.isEmpty() || right.isEmpty()) {
return false;
}
}
if constexpr (!isStrict) {
if (((left.isEmpty() || right.isEmpty()) && (left.isUndefined() || right.isUndefined()))) {
continue;
}
}
if (!Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, left, right, stack, scope, true)) {
return false;
}
RETURN_IF_EXCEPTION(*scope, false);
}
for (; i < array2Length; i++) {
JSValue right = getIndexWithoutAccessors(globalObject, o2, i);
RETURN_IF_EXCEPTION(*scope, false);
if (((right.isEmpty() || right.isUndefined()))) {
continue;
}
return false;
}
JSC::PropertyNameArray a1(vm, PropertyNameMode::Symbols, PrivateSymbolMode::Exclude);
JSC::PropertyNameArray a2(vm, PropertyNameMode::Symbols, PrivateSymbolMode::Exclude);
JSObject::getOwnPropertyNames(o1, globalObject, a1, DontEnumPropertiesMode::Exclude);
JSObject::getOwnPropertyNames(o2, globalObject, a2, DontEnumPropertiesMode::Exclude);
size_t propertyLength = a1.size();
if constexpr (isStrict) {
if (propertyLength != a2.size()) {
return false;
}
}
// take a property name from one, try to get it from both
for (size_t i = 0; i < propertyLength; i++) {
Identifier i1 = a1[i];
PropertyName propertyName1 = PropertyName(i1);
JSValue prop1 = o1->get(globalObject, propertyName1);
RETURN_IF_EXCEPTION(*scope, false);
if (UNLIKELY(!prop1)) {
return false;
}
JSValue prop2 = o2->getIfPropertyExists(globalObject, propertyName1);
RETURN_IF_EXCEPTION(*scope, false);
if constexpr (!isStrict) {
if (prop1.isUndefined() && prop2.isEmpty()) {
continue;
}
}
if (!prop2) {
return false;
}
if (!Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, prop1, prop2, stack, scope, true)) {
return false;
}
RETURN_IF_EXCEPTION(*scope, false);
}
if (addToStack) {
stack.remove(originalLength);
}
RETURN_IF_EXCEPTION(*scope, false);
return true;
}
if constexpr (isStrict) {
if (!equal(JSObject::calculatedClassName(o1), JSObject::calculatedClassName(o2))) {
return false;
}
}
JSC::Structure* o1Structure = o1->structure();
if (!o1Structure->hasNonReifiedStaticProperties() && o1Structure->canPerformFastPropertyEnumeration()) {
JSC::Structure* o2Structure = o2->structure();
if (!o2Structure->hasNonReifiedStaticProperties() && o2Structure->canPerformFastPropertyEnumeration()) {
size_t count1 = 0;
bool result = true;
if constexpr (isStrict) {
if (o2Structure->inlineSize() + o2Structure->outOfLineSize() != o1Structure->inlineSize() + o1Structure->outOfLineSize()) {
return false;
}
}
bool sameStructure = o2Structure->id() == o1Structure->id();
if (sameStructure) {
o1Structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool {
if (entry.attributes() & PropertyAttribute::DontEnum || PropertyName(entry.key()).isPrivateName()) {
return true;
}
count1++;
JSValue left = o1->getDirect(entry.offset());
JSValue right = o2->getDirect(entry.offset());
if constexpr (!isStrict) {
if (left.isUndefined() && right.isEmpty()) {
return true;
}
}
if (!right) {
result = false;
return false;
}
if (left == right || JSC::sameValue(globalObject, left, right)) {
return true;
}
if (!Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, left, right, stack, scope, true)) {
result = false;
return false;
}
return true;
});
} else {
o1Structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool {
if (entry.attributes() & PropertyAttribute::DontEnum || PropertyName(entry.key()).isPrivateName()) {
return true;
}
count1++;
JSValue left = o1->getDirect(entry.offset());
JSValue right = o2->getDirect(vm, JSC::PropertyName(entry.key()));
if constexpr (!isStrict) {
if (left.isUndefined() && right.isEmpty()) {
return true;
}
}
if (!right) {
result = false;
return false;
}
if (left == right || JSC::sameValue(globalObject, left, right)) {
return true;
}
if (!Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, left, right, stack, scope, true)) {
result = false;
return false;
}
return true;
});
if (result) {
size_t remain = count1;
o2Structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool {
if (entry.attributes() & PropertyAttribute::DontEnum || PropertyName(entry.key()).isPrivateName()) {
return true;
}
if constexpr (!isStrict) {
if (o2->getDirect(entry.offset()).isUndefined()) {
return true;
}
}
if (remain == 0) {
result = false;
return false;
}
remain--;
return true;
});
}
}
if (addToStack) {
stack.remove(originalLength);
}
return result;
}
}
JSC::PropertyNameArray a1(vm, PropertyNameMode::StringsAndSymbols, PrivateSymbolMode::Exclude);
JSC::PropertyNameArray a2(vm, PropertyNameMode::StringsAndSymbols, PrivateSymbolMode::Exclude);
o1->getPropertyNames(globalObject, a1, DontEnumPropertiesMode::Exclude);
RETURN_IF_EXCEPTION(*scope, false);
o2->getPropertyNames(globalObject, a2, DontEnumPropertiesMode::Exclude);
RETURN_IF_EXCEPTION(*scope, false);
const size_t propertyArrayLength = a1.size();
if (propertyArrayLength != a2.size()) {
return false;
}
// take a property name from one, try to get it from both
for (size_t i = 0; i < propertyArrayLength; i++) {
Identifier i1 = a1[i];
PropertyName propertyName1 = PropertyName(i1);
JSValue prop1 = o1->get(globalObject, propertyName1);
RETURN_IF_EXCEPTION(*scope, false);
if (UNLIKELY(!prop1)) {
return false;
}
JSValue prop2 = o2->getIfPropertyExists(globalObject, propertyName1);
RETURN_IF_EXCEPTION(*scope, false);
if constexpr (!isStrict) {
if (prop1.isUndefined() && prop2.isEmpty()) {
continue;
}
}
if (!prop2) {
return false;
}
if (!Bun__deepEquals<isStrict, enableAsymmetricMatchers>(globalObject, prop1, prop2, stack, scope, true)) {
return false;
}
RETURN_IF_EXCEPTION(*scope, false);
}
if (addToStack) {
stack.remove(originalLength);
}
return true;
}
template<bool enableAsymmetricMatchers>
bool Bun__deepMatch(JSValue objValue, JSValue subsetValue, JSGlobalObject* globalObject, ThrowScope* throwScope, bool replacePropsWithAsymmetricMatchers)
{
VM& vm = globalObject->vm();
JSObject* obj = objValue.getObject();
JSObject* subsetObj = subsetValue.getObject();
PropertyNameArray subsetProps(vm, PropertyNameMode::StringsAndSymbols, PrivateSymbolMode::Include);
subsetObj->getPropertyNames(globalObject, subsetProps, DontEnumPropertiesMode::Exclude);
// TODO: add fast paths for:
// - two "simple" objects (using ->forEachProperty in both)
// - two "simple" arrays
// similar to what is done in deepEquals (canPerformFastPropertyEnumerationForIterationBun)
// arrays should match exactly
if (isArray(globalObject, objValue) && isArray(globalObject, subsetValue)) {
if (obj->getArrayLength() != subsetObj->getArrayLength()) {
return false;
}
PropertyNameArray objProps(vm, PropertyNameMode::StringsAndSymbols, PrivateSymbolMode::Include);
obj->getPropertyNames(globalObject, objProps, DontEnumPropertiesMode::Exclude);
if (objProps.size() != subsetProps.size()) {
return false;
}
}
for (size_t i = 0; i < subsetProps.size(); i++) {
JSValue prop = obj->getIfPropertyExists(globalObject, subsetProps[i]);
RETURN_IF_EXCEPTION(*throwScope, false);
if (prop.isEmpty()) {
return false;
}
JSValue subsetProp = subsetObj->get(globalObject, subsetProps[i]);
RETURN_IF_EXCEPTION(*throwScope, false);
JSCell* subsetPropCell = subsetProp.asCell();
JSCell* propCell = prop.asCell();
if constexpr (enableAsymmetricMatchers) {
if (subsetProp.isCell() && !subsetProp.isEmpty() && subsetPropCell->type() == JSC::JSType(JSDOMWrapperType)) {
switch (matchAsymmetricMatcher(globalObject, subsetPropCell, prop, throwScope)) {
case AsymmetricMatcherResult::FAIL:
return false;
case AsymmetricMatcherResult::PASS:
if (replacePropsWithAsymmetricMatchers) {
obj->putDirect(vm, subsetProps[i], subsetProp);
}
// continue to next subset prop
continue;
case AsymmetricMatcherResult::NOT_MATCHER:
break;
}
} else if (prop.isCell() && !prop.isEmpty() && propCell->type() == JSC::JSType(JSDOMWrapperType)) {