-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathJavascriptOperators.cpp
11094 lines (9819 loc) · 503 KB
/
JavascriptOperators.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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLanguagePch.h"
#include "Types/PathTypeHandler.h"
#include "Types/PropertyIndexRanges.h"
#include "Types/WithScopeObject.h"
#include "Types/SpreadArgument.h"
#include "Library/JavascriptPromise.h"
#include "Library/JavascriptRegularExpression.h"
#include "Library/ThrowErrorObject.h"
#include "Library/JavascriptGeneratorFunction.h"
#include "Library/ForInObjectEnumerator.h"
#include "Library/ES5Array.h"
#include "Types/SimpleDictionaryPropertyDescriptor.h"
#include "Types/SimpleDictionaryTypeHandler.h"
#include "Language/ModuleNamespace.h"
#ifndef SCRIPT_DIRECT_TYPE
typedef enum JsNativeValueType: int
{
JsInt8Type,
JsUint8Type,
JsInt16Type,
JsUint16Type,
JsInt32Type,
JsUint32Type,
JsInt64Type,
JsUint64Type,
JsFloatType,
JsDoubleType,
JsNativeStringType
} JsNativeValueType;
typedef struct JsNativeString
{
unsigned int length;
LPCWSTR str;
} JsNativeString;
#endif
using namespace Js;
DEFINE_RECYCLER_TRACKER_ARRAY_PERF_COUNTER(Var);
DEFINE_RECYCLER_TRACKER_PERF_COUNTER(FrameDisplay);
enum IndexType
{
IndexType_Number,
IndexType_PropertyId,
IndexType_JavascriptString
};
IndexType GetIndexTypeFromString(char16 const * propertyName, charcount_t propertyLength, ScriptContext* scriptContext, uint32* index, PropertyRecord const** propertyRecord, bool createIfNotFound)
{
if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
(*index != JavascriptArray::InvalidIndex))
{
return IndexType_Number;
}
else
{
if (createIfNotFound)
{
scriptContext->GetOrAddPropertyRecord(propertyName, propertyLength, propertyRecord);
}
else
{
scriptContext->FindPropertyRecord(propertyName, propertyLength, propertyRecord);
}
return IndexType_PropertyId;
}
}
IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
{
// CONSIDER: Only OP_SetElementI and OP_GetElementI use and take advantage of the
// IndexType_JavascriptString result. Consider modifying other callers of GetIndexType to take
// advantage of non-interned property strings where appropriate.
if (TaggedInt::Is(indexVar))
{
int indexInt = TaggedInt::ToInt32(indexVar);
if (indexInt >= 0)
{
*index = (uint)indexInt;
return IndexType_Number;
}
else
{
char16 stringBuffer[22];
int pos = TaggedInt::ToBuffer(indexInt, stringBuffer, _countof(stringBuffer));
charcount_t length = (_countof(stringBuffer) - 1) - pos;
if (createIfNotFound || preferJavascriptStringOverPropertyRecord)
{
// When preferring JavascriptString objects, just return a PropertyRecord instead
// of creating temporary JavascriptString objects for every negative integer that
// comes through here.
scriptContext->GetOrAddPropertyRecord(stringBuffer + pos, length, propertyRecord);
}
else
{
scriptContext->FindPropertyRecord(stringBuffer + pos, length, propertyRecord);
}
return IndexType_PropertyId;
}
}
if (JavascriptNumber::Is_NoTaggedIntCheck(indexVar))
{
// If this double can be a positive integer index, convert it.
int32 value = 0;
bool isInt32 = false;
if (JavascriptNumber::TryGetInt32OrUInt32Value(JavascriptNumber::GetValue(indexVar), &value, &isInt32)
&& !isInt32
&& static_cast<uint32>(value) < JavascriptArray::InvalidIndex)
{
*index = static_cast<uint32>(value);
return IndexType_Number;
}
// Fall through to slow string conversion.
}
JavascriptSymbol * symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(indexVar);
if (symbol)
{
// JavascriptSymbols cannot add a new PropertyRecord - they correspond to one and only one existing PropertyRecord.
// We already know what the PropertyRecord is since it is stored in the JavascriptSymbol itself so just return it.
*propertyRecord = symbol->GetValue();
return IndexType_PropertyId;
}
else
{
JavascriptString* indexStr = JavascriptConversion::ToString(indexVar, scriptContext);
char16 const * propertyName = indexStr->GetString();
charcount_t const propertyLength = indexStr->GetLength();
if (!createIfNotFound && preferJavascriptStringOverPropertyRecord)
{
if (JavascriptOperators::TryConvertToUInt32(propertyName, propertyLength, index) &&
(*index != JavascriptArray::InvalidIndex))
{
return IndexType_Number;
}
*propertyNameString = indexStr;
return IndexType_JavascriptString;
}
return GetIndexTypeFromString(propertyName, propertyLength, scriptContext, index, propertyRecord, createIfNotFound);
}
}
IndexType GetIndexTypeFromPrimitive(Var indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
{
return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
}
IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, JavascriptString ** propertyNameString, bool createIfNotFound, bool preferJavascriptStringOverPropertyRecord)
{
indexVar = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(indexVar, scriptContext);
return GetIndexTypeFromPrimitive(indexVar, scriptContext, index, propertyRecord, propertyNameString, createIfNotFound, preferJavascriptStringOverPropertyRecord);
}
IndexType GetIndexType(Var& indexVar, ScriptContext* scriptContext, uint32* index, PropertyRecord const ** propertyRecord, bool createIfNotFound)
{
return GetIndexType(indexVar, scriptContext, index, propertyRecord, nullptr, createIfNotFound, false);
}
BOOL FEqualDbl(double dbl1, double dbl2)
{
// If the low ulongs don't match, they can't be equal.
if (Js::NumberUtilities::LuLoDbl(dbl1) != Js::NumberUtilities::LuLoDbl(dbl2))
return FALSE;
// If the high ulongs don't match, they can be equal iff one is -0 and
// the other is +0.
if (Js::NumberUtilities::LuHiDbl(dbl1) != Js::NumberUtilities::LuHiDbl(dbl2))
{
return 0x80000000 == (Js::NumberUtilities::LuHiDbl(dbl1) | Js::NumberUtilities::LuHiDbl(dbl2)) &&
0 == Js::NumberUtilities::LuLoDbl(dbl1);
}
// The bit patterns match. They are equal iff they are not Nan.
return !Js::NumberUtilities::IsNan(dbl1);
}
Var JavascriptOperators::OP_ApplyArgs(Var func, Var instance, __in_xcount(8) void** stackPtr, CallInfo callInfo, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_OP_ApplyArgs);
int argCount = callInfo.Count;
///
/// Check func has internal [[Call]] property
/// If not, throw TypeError
///
if (!JavascriptConversion::IsCallable(func)) {
JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction);
}
// Fix callInfo: expect result/value, and none of other flags are currently applicable.
// OP_ApplyArgs expects a result. Neither of {jit, interpreted} mode sends correct callFlags:
// LdArgCnt -- jit sends whatever was passed to current function, interpreter always sends 0.
// See Win8 bug 490489.
callInfo.Flags = CallFlags_Value;
RecyclableObject *funcPtr = UnsafeVarTo<RecyclableObject>(func);
PROBE_STACK(scriptContext, Js::Constants::MinStackDefault + argCount * 4);
JavascriptMethod entryPoint = funcPtr->GetEntryPoint();
Var ret;
switch (argCount) {
case 0:
Assert(false);
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo);
break;
case 1:
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance);
break;
case 2:
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0]);
break;
case 3:
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1]);
break;
case 4:
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2]);
break;
case 5:
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3]);
break;
case 6:
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4]);
break;
case 7:
ret = CALL_ENTRYPOINT_NOASSERT(entryPoint, funcPtr, callInfo, instance, stackPtr[0], stackPtr[1], stackPtr[2], stackPtr[3], stackPtr[4], stackPtr[5]);
break;
default:
{
// Don't need stack probe here- we just did so above
Arguments args(callInfo, stackPtr - 1);
BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
{
ret = JavascriptFunction::CallFunction<false>(funcPtr, entryPoint, args);
}
END_SAFE_REENTRANT_CALL
break;
}
}
return ret;
JIT_HELPER_END(Op_OP_ApplyArgs);
}
#ifdef _M_IX86
// Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
Var JavascriptOperators::Int32ToVar(int32 value, ScriptContext* scriptContext)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtom);
return JavascriptNumber::ToVar(value, scriptContext);
JIT_HELPER_END(Op_Int32ToAtom);
}
// Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
Var JavascriptOperators::Int32ToVarInPlace(int32 value, ScriptContext* scriptContext, JavascriptNumber* result)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Int32ToAtomInPlace);
return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
JIT_HELPER_END(Op_Int32ToAtomInPlace);
}
// Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
Var JavascriptOperators::UInt32ToVar(uint32 value, ScriptContext* scriptContext)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtom);
return JavascriptNumber::ToVar(value, scriptContext);
JIT_HELPER_END(Op_UInt32ToAtom);
}
// Alias for overloaded JavascriptNumber::ToVar so it can be called unambiguously from native code
Var JavascriptOperators::UInt32ToVarInPlace(uint32 value, ScriptContext* scriptContext, JavascriptNumber* result)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_UInt32ToAtomInPlace);
return JavascriptNumber::ToVarInPlace(value, scriptContext, result);
JIT_HELPER_END(Op_UInt32ToAtomInPlace);
}
#endif
Var JavascriptOperators::OP_FinishOddDivBy2(uint32 value, ScriptContext *scriptContext)
{
return JavascriptNumber::New((double)(value + 0.5), scriptContext);
}
Var JavascriptOperators::ToNumberInPlace(Var aRight, ScriptContext* scriptContext, JavascriptNumber* result)
{
JIT_HELPER_REENTRANT_HEADER(Op_ConvNumberInPlace);
if (TaggedInt::Is(aRight) || JavascriptNumber::Is_NoTaggedIntCheck(aRight))
{
return aRight;
}
return JavascriptNumber::ToVarInPlace(JavascriptConversion::ToNumber(aRight, scriptContext), scriptContext, result);
JIT_HELPER_END(Op_ConvNumberInPlace);
}
Var JavascriptOperators::Typeof(Var var, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_Typeof);
switch (JavascriptOperators::GetTypeId(var))
{
case TypeIds_Undefined:
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
case TypeIds_Null:
//null
return scriptContext->GetLibrary()->GetObjectTypeDisplayString();
case TypeIds_Integer:
case TypeIds_Number:
case TypeIds_Int64Number:
case TypeIds_UInt64Number:
return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
default:
// Falsy objects are typeof 'undefined'.
if (VarTo<RecyclableObject>(var)->GetType()->IsFalsy())
{
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
}
else
{
return VarTo<RecyclableObject>(var)->GetTypeOfString(scriptContext);
}
}
JIT_HELPER_END(Op_Typeof);
}
Var JavascriptOperators::TypeofFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
{
return TypeofFld_Internal(instance, false, propertyId, scriptContext);
}
Var JavascriptOperators::TypeofRootFld(Var instance, PropertyId propertyId, ScriptContext* scriptContext)
{
return TypeofFld_Internal(instance, true, propertyId, scriptContext);
}
Var JavascriptOperators::TypeofFld_Internal(Var instance, const bool isRoot, PropertyId propertyId, ScriptContext* scriptContext)
{
RecyclableObject* object = nullptr;
if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined , scriptContext->GetPropertyName(propertyId)->GetBuffer());
}
Var value = nullptr;
try
{
Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
// In edge mode, spec compat is more important than backward compat. Use spec/web behavior here
if (isRoot
? !JavascriptOperators::GetRootProperty(instance, propertyId, &value, scriptContext)
: !JavascriptOperators::GetProperty(instance, object, propertyId, &value, scriptContext))
{
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
}
if (!scriptContext->IsUndeclBlockVar(value))
{
return JavascriptOperators::Typeof(value, scriptContext);
}
}
catch(const JavascriptException& err)
{
err.GetAndClear(); // discard exception object
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
}
Assert(scriptContext->IsUndeclBlockVar(value));
JavascriptError::ThrowReferenceError(scriptContext, JSERR_UseBeforeDeclaration);
}
Var JavascriptOperators::TypeofElem_UInt32(Var instance, uint32 index, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_UInt32);
if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
#if FLOATVAR
return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
#else
char buffer[sizeof(Js::JavascriptNumber)];
return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
(Js::JavascriptNumber *)buffer), scriptContext);
#endif
JIT_HELPER_END(Op_TypeofElem_UInt32);
}
Var JavascriptOperators::TypeofElem_Int32(Var instance, int32 index, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem_Int32);
if (JavascriptOperators::IsNumberFromNativeArray(instance, index, scriptContext))
return scriptContext->GetLibrary()->GetNumberTypeDisplayString();
#if FLOATVAR
return TypeofElem(instance, Js::JavascriptNumber::ToVar(index, scriptContext), scriptContext);
#else
char buffer[sizeof(Js::JavascriptNumber)];
return TypeofElem(instance, Js::JavascriptNumber::ToVarInPlace(index, scriptContext,
(Js::JavascriptNumber *)buffer), scriptContext);
#endif
JIT_HELPER_END(Op_TypeofElem_Int32);
}
Js::JavascriptString* GetPropertyDisplayNameForError(Var prop, ScriptContext* scriptContext)
{
JavascriptString* str;
JavascriptSymbol *symbol = JavascriptOperators::TryFromVar<JavascriptSymbol>(prop);
if (symbol)
{
str = JavascriptSymbol::ToString(symbol->GetValue(), scriptContext);
}
else
{
str = JavascriptConversion::ToString(prop, scriptContext);
}
return str;
}
Var JavascriptOperators::TypeofElem(Var instance, Var index, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_TypeofElem);
RecyclableObject* object = nullptr;
if (FALSE == JavascriptOperators::GetPropertyObject(instance, scriptContext, &object))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_CannotGet_NullOrUndefined, GetPropertyDisplayNameForError(index, scriptContext));
}
Var member = nullptr;
uint32 indexVal;
PropertyRecord const * propertyRecord = nullptr;
ThreadContext* threadContext = scriptContext->GetThreadContext();
ImplicitCallFlags savedImplicitCallFlags = threadContext->GetImplicitCallFlags();
threadContext->ClearImplicitCallFlags();
try
{
Js::JavascriptExceptionOperators::AutoCatchHandlerExists autoCatchHandlerExists(scriptContext);
IndexType indexType = GetIndexType(index, scriptContext, &indexVal, &propertyRecord, false);
// For JS Objects, don't create the propertyId if not already added
if (indexType == IndexType_Number)
{
// In edge mode, we don't need to worry about the special "unknown" behavior. If the item is not available from Get,
// just return undefined.
if (!JavascriptOperators::GetItem(instance, object, indexVal, &member, scriptContext))
{
// If the instance doesn't have the item, typeof result is "undefined".
threadContext->CheckAndResetImplicitCallAccessorFlag();
threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
}
}
else
{
Assert(indexType == IndexType_PropertyId);
if (propertyRecord == nullptr && !JavascriptOperators::CanShortcutOnUnknownPropertyName(object))
{
indexType = GetIndexTypeFromPrimitive(index, scriptContext, &indexVal, &propertyRecord, true);
Assert(indexType == IndexType_PropertyId);
Assert(propertyRecord != nullptr);
}
if (propertyRecord != nullptr)
{
if (!JavascriptOperators::GetProperty(instance, object, propertyRecord->GetPropertyId(), &member, scriptContext))
{
// If the instance doesn't have the property, typeof result is "undefined".
threadContext->CheckAndResetImplicitCallAccessorFlag();
threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
}
}
else
{
#if DBG
JavascriptString* indexStr = JavascriptConversion::ToString(index, scriptContext);
PropertyRecord const * debugPropertyRecord;
scriptContext->GetOrAddPropertyRecord(indexStr, &debugPropertyRecord);
AssertMsg(!JavascriptOperators::GetProperty(instance, object, debugPropertyRecord->GetPropertyId(), &member, scriptContext), "how did this property come? See OS Bug 2727708 if you see this come from the web");
#endif
// If the instance doesn't have the property, typeof result is "undefined".
threadContext->CheckAndResetImplicitCallAccessorFlag();
threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
}
}
threadContext->CheckAndResetImplicitCallAccessorFlag();
threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
return JavascriptOperators::Typeof(member, scriptContext);
}
catch(const JavascriptException& err)
{
err.GetAndClear(); // discard exception object
threadContext->CheckAndResetImplicitCallAccessorFlag();
threadContext->AddImplicitCallFlags(savedImplicitCallFlags);
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
}
JIT_HELPER_END(Op_TypeofElem);
}
//
// Delete the given Var
//
Var JavascriptOperators::Delete(Var var, ScriptContext* scriptContext)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_Delete);
return scriptContext->GetLibrary()->GetTrue();
JIT_HELPER_END(Op_Delete);
}
BOOL JavascriptOperators::Equal_Full(Var aLeft, Var aRight, ScriptContext* requestContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_Equal_Full);
//
// Fast-path SmInts and paired Number combinations.
//
if (aLeft == aRight)
{
if (JavascriptNumber::Is(aLeft) && JavascriptNumber::IsNan(JavascriptNumber::GetValue(aLeft)))
{
return false;
}
else if (VarIs<JavascriptVariantDate>(aLeft) == false) // only need to check on aLeft - since they are the same var, aRight would do the same
{
return true;
}
else
{
//In ES5 mode strict equals (===) on same instance of object type VariantDate succeeds.
//Hence equals needs to succeed.
return true;
}
}
BOOL result = false;
if (TaggedInt::Is(aLeft))
{
if (TaggedInt::Is(aRight))
{
// If aLeft == aRight, we would already have returned true above.
return false;
}
else if (JavascriptNumber::Is_NoTaggedIntCheck(aRight))
{
return TaggedInt::ToDouble(aLeft) == JavascriptNumber::GetValue(aRight);
}
else
{
BOOL res = UnsafeVarTo<RecyclableObject>(aRight)->Equals(aLeft, &result, requestContext);
AssertMsg(res, "Should have handled this");
return result;
}
}
else if (JavascriptNumber::Is_NoTaggedIntCheck(aLeft))
{
if (TaggedInt::Is(aRight))
{
return TaggedInt::ToDouble(aRight) == JavascriptNumber::GetValue(aLeft);
}
else if(JavascriptNumber::Is_NoTaggedIntCheck(aRight))
{
return JavascriptNumber::GetValue(aLeft) == JavascriptNumber::GetValue(aRight);
}
else
{
BOOL res = UnsafeVarTo<RecyclableObject>(aRight)->Equals(aLeft, &result, requestContext);
AssertMsg(res, "Should have handled this");
return result;
}
}
if (UnsafeVarTo<RecyclableObject>(aLeft)->Equals(aRight, &result, requestContext))
{
return result;
}
else
{
return false;
}
JIT_HELPER_END(Op_Equal_Full);
}
BOOL JavascriptOperators::Greater_Full(Var aLeft,Var aRight,ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_Greater_Full);
return RelationalComparisonHelper(aRight, aLeft, scriptContext, false, false);
JIT_HELPER_END(Op_Greater_Full);
}
BOOL JavascriptOperators::Less_Full(Var aLeft, Var aRight, ScriptContext* scriptContext)
{
return RelationalComparisonHelper(aLeft, aRight, scriptContext, true, false);
}
BOOL JavascriptOperators::RelationalComparisonHelper(Var aLeft, Var aRight, ScriptContext* scriptContext, bool leftFirst, bool undefinedAs)
{
TypeId typeId = JavascriptOperators::GetTypeId(aLeft);
if (typeId == TypeIds_Null)
{
aLeft=TaggedInt::ToVarUnchecked(0);
}
else if (typeId == TypeIds_Undefined)
{
aLeft=scriptContext->GetLibrary()->GetNaN();
}
typeId = JavascriptOperators::GetTypeId(aRight);
if (typeId == TypeIds_Null)
{
aRight=TaggedInt::ToVarUnchecked(0);
}
else if (typeId == TypeIds_Undefined)
{
aRight=scriptContext->GetLibrary()->GetNaN();
}
double dblLeft, dblRight;
TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
TypeId rightType = JavascriptOperators::GetTypeId(aRight);
switch (leftType)
{
case TypeIds_Integer:
dblLeft = TaggedInt::ToDouble(aLeft);
switch (rightType)
{
case TypeIds_Integer:
dblRight = TaggedInt::ToDouble(aRight);
break;
case TypeIds_Number:
dblRight = JavascriptNumber::GetValue(aRight);
break;
default:
dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
break;
}
break;
case TypeIds_Number:
dblLeft = JavascriptNumber::GetValue(aLeft);
switch (rightType)
{
case TypeIds_Integer:
dblRight = TaggedInt::ToDouble(aRight);
break;
case TypeIds_Number:
dblRight = JavascriptNumber::GetValue(aRight);
break;
default:
dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
break;
}
break;
case TypeIds_Int64Number:
{
switch (rightType)
{
case TypeIds_Int64Number:
{
__int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
__int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
return leftValue < rightValue;
}
break;
case TypeIds_UInt64Number:
{
__int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
unsigned __int64 rightValue = UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
if (rightValue <= INT_MAX && leftValue >= 0)
{
return leftValue < (__int64)rightValue;
}
}
break;
}
dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
}
break;
// we cannot do double conversion between 2 int64 numbers as we can get wrong result after conversion
// i.e., two different numbers become the same after losing precision. We'll continue dbl comparison
// if either number is not an int64 number.
case TypeIds_UInt64Number:
{
switch (rightType)
{
case TypeIds_Int64Number:
{
unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
__int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
if (leftValue < INT_MAX && rightValue >= 0)
{
return (__int64)leftValue < rightValue;
}
}
break;
case TypeIds_UInt64Number:
{
unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
unsigned __int64 rightValue = UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
return leftValue < rightValue;
}
break;
}
dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
}
break;
case TypeIds_String:
switch (rightType)
{
case TypeIds_Integer:
case TypeIds_Number:
case TypeIds_Boolean:
break;
default:
aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
rightType = JavascriptOperators::GetTypeId(aRight);
if (rightType != TypeIds_String)
{
dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
break;
}
case TypeIds_String:
return JavascriptString::LessThan(aLeft, aRight);
}
dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
break;
case TypeIds_Boolean:
case TypeIds_Null:
case TypeIds_Undefined:
case TypeIds_Symbol:
dblLeft = JavascriptConversion::ToNumber(aLeft, scriptContext);
dblRight = JavascriptConversion::ToNumber(aRight, scriptContext);
break;
default:
if (leftFirst)
{
aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
}
else
{
aRight = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aRight, scriptContext);
aLeft = JavascriptConversion::ToPrimitive<JavascriptHint::HintNumber>(aLeft, scriptContext);
}
//BugFix: When @@ToPrimitive of an object is overridden with a function that returns null/undefined
//this helper will fall into a inescapable goto loop as the checks for null/undefined were outside of the path
return RelationalComparisonHelper(aLeft, aRight, scriptContext, leftFirst, undefinedAs);
}
//
// And +0,-0 that is not implemented fully
//
if (JavascriptNumber::IsNan(dblLeft) || JavascriptNumber::IsNan(dblRight))
{
return undefinedAs;
}
// this will succeed for -0.0 == 0.0 case as well
if (dblLeft == dblRight)
{
return false;
}
return dblLeft < dblRight;
}
BOOL JavascriptOperators::StrictEqualString(Var aLeft, JavascriptString* aRight)
{
JavascriptString* leftStr = TryFromVar<JavascriptString>(aLeft);
if (!leftStr)
{
return false;
}
JIT_HELPER_REENTRANT_HEADER(Op_StrictEqualString);
JIT_HELPER_SAME_ATTRIBUTES(Op_StrictEqualString, Op_StrictEqual);
return JavascriptString::Equals(leftStr, aRight);
JIT_HELPER_END(Op_StrictEqualString);
}
BOOL JavascriptOperators::StrictEqualEmptyString(Var aLeft)
{
JIT_HELPER_NOT_REENTRANT_NOLOCK_HEADER(Op_StrictEqualEmptyString);
JavascriptString * string = JavascriptOperators::TryFromVar<JavascriptString>(aLeft);
if (!string)
{
return false;
}
Assert(string);
return string->GetLength() == 0;
JIT_HELPER_END(Op_StrictEqualEmptyString);
}
BOOL JavascriptOperators::StrictEqual(Var aLeft, Var aRight, ScriptContext* requestContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_StrictEqual);
double dblLeft, dblRight;
TypeId rightType, leftType;
leftType = JavascriptOperators::GetTypeId(aLeft);
// Because NaN !== NaN, we may not return TRUE when typeId is Number
if (aLeft == aRight && leftType != TypeIds_Number) return TRUE;
rightType = JavascriptOperators::GetTypeId(aRight);
switch (leftType)
{
case TypeIds_String:
switch (rightType)
{
case TypeIds_String:
return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
}
return FALSE;
case TypeIds_Integer:
switch (rightType)
{
case TypeIds_Integer:
return aLeft == aRight;
// we don't need to worry about int64: it cannot equal as we create
// JavascriptInt64Number only in overflow scenarios.
case TypeIds_Number:
dblLeft = TaggedInt::ToDouble(aLeft);
dblRight = JavascriptNumber::GetValue(aRight);
goto CommonNumber;
}
return FALSE;
case TypeIds_Int64Number:
switch (rightType)
{
case TypeIds_Int64Number:
{
__int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
__int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
return leftValue == rightValue;
}
case TypeIds_UInt64Number:
{
__int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
return ((unsigned __int64)leftValue == rightValue);
}
case TypeIds_Number:
dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
dblRight = JavascriptNumber::GetValue(aRight);
goto CommonNumber;
}
return FALSE;
case TypeIds_UInt64Number:
switch (rightType)
{
case TypeIds_Int64Number:
{
unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
__int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
return (leftValue == (unsigned __int64)rightValue);
}
case TypeIds_UInt64Number:
{
unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
unsigned __int64 rightValue = VarTo<JavascriptUInt64Number>(aRight)->GetValue();
return leftValue == rightValue;
}
case TypeIds_Number:
dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
dblRight = JavascriptNumber::GetValue(aRight);
goto CommonNumber;
}
return FALSE;
case TypeIds_Number:
switch (rightType)
{
case TypeIds_Integer:
dblLeft = JavascriptNumber::GetValue(aLeft);
dblRight = TaggedInt::ToDouble(aRight);
goto CommonNumber;
case TypeIds_Int64Number:
dblLeft = JavascriptNumber::GetValue(aLeft);
dblRight = (double)VarTo<JavascriptInt64Number>(aRight)->GetValue();
goto CommonNumber;
case TypeIds_UInt64Number:
dblLeft = JavascriptNumber::GetValue(aLeft);
dblRight = (double)UnsafeVarTo<JavascriptUInt64Number>(aRight)->GetValue();
goto CommonNumber;
case TypeIds_Number:
dblLeft = JavascriptNumber::GetValue(aLeft);
dblRight = JavascriptNumber::GetValue(aRight);
CommonNumber:
return FEqualDbl(dblLeft, dblRight);
}
return FALSE;
case TypeIds_Boolean:
switch (rightType)
{
case TypeIds_Boolean:
return aLeft == aRight;
}
return FALSE;
case TypeIds_Undefined:
return rightType == TypeIds_Undefined;
case TypeIds_Null:
return rightType == TypeIds_Null;
case TypeIds_Array:
return (rightType == TypeIds_Array && aLeft == aRight);
#if DBG
case TypeIds_Symbol:
if (rightType == TypeIds_Symbol)
{
const PropertyRecord* leftValue = UnsafeVarTo<JavascriptSymbol>(aLeft)->GetValue();
const PropertyRecord* rightValue = UnsafeVarTo<JavascriptSymbol>(aRight)->GetValue();
Assert(leftValue != rightValue);
}
break;
#endif
case TypeIds_GlobalObject:
case TypeIds_HostDispatch:
switch (rightType)
{
case TypeIds_HostDispatch:
case TypeIds_GlobalObject:
{
BOOL result;
if(UnsafeVarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
{
return result;
}
return false;
}
}
break;
}
if (VarTo<RecyclableObject>(aLeft)->IsExternal())
{
BOOL result;
if (VarTo<RecyclableObject>(aLeft)->StrictEquals(aRight, &result, requestContext))
{
if (result)
{
return TRUE;
}
}
}
if (!TaggedNumber::Is(aRight) && VarTo<RecyclableObject>(aRight)->IsExternal())
{
BOOL result;
if (VarTo<RecyclableObject>(aRight)->StrictEquals(aLeft, &result, requestContext))
{
if (result)
{
return TRUE;
}
}
}
return aLeft == aRight;
JIT_HELPER_END(Op_StrictEqual);
}
BOOL JavascriptOperators::HasOwnProperty(
Var instance,
PropertyId propertyId,
_In_ ScriptContext* requestContext,
_In_opt_ PropertyString* propString)
{
if (TaggedNumber::Is(instance))
{
return FALSE;