-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathJavascriptConversion.cpp
More file actions
1442 lines (1272 loc) · 59.7 KB
/
JavascriptConversion.cpp
File metadata and controls
1442 lines (1272 loc) · 59.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLanguagePch.h"
#include "RuntimeMathPch.h"
#include "Library/JavascriptNumberObject.h"
#include "Library/String/JavascriptStringObject.h"
#include "Library/DateImplementation.h"
#include "Library/JavascriptDate.h"
using namespace Js;
static const double k_2to16 = 65536.0;
static const double k_2to31 = 2147483648.0;
static const double k_2to32 = 4294967296.0;
// ES5 9.10 indicates that this method should throw a TypeError if the supplied value is Undefined or Null.
// Our implementation returns FALSE in this scenario, expecting the caller to throw the TypeError.
// This allows the caller to provide more context in the error message without having to unnecessarily
// construct the message string before knowing whether or not the object is coercible.
BOOL JavascriptConversion::CheckObjectCoercible(Var aValue, ScriptContext* scriptContext)
{
return !JavascriptOperators::IsUndefinedOrNull(aValue);
}
//ES5 9.11 Undefined, Null, Boolean, Number, String - return false
//If Object has a [[Call]] internal method, then return true, otherwise return false
bool JavascriptConversion::IsCallable(Var aValue)
{
if (!VarIs<RecyclableObject>(aValue))
{
return false;
}
return IsCallable(UnsafeVarTo<RecyclableObject>(aValue));
}
bool JavascriptConversion::IsCallable(_In_ RecyclableObject* aValue)
{
Assert(VarIsCorrectType(aValue));
JavascriptMethod entryPoint = aValue->GetEntryPoint();
return RecyclableObject::DefaultEntryPoint != entryPoint;
}
//----------------------------------------------------------------------------
// ES5 9.12 SameValue algorithm implementation.
// 1. If Type(x) is different from Type(y), return false.
// 2. If Type(x) is Undefined, return true.
// 3. If Type(x) is Null, return true.
// 4. If Type(x) is Number, then.
// a. If x is NaN and y is NaN, return true.
// b. If x is +0 and y is -0, return false.
// c. If x is -0 and y is +0, return false.
// d. If x is the same number value as y, return true.
// e. Return false.
// 5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
// 6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
// 7. Return true if x and y refer to the same object. Otherwise, return false.
//----------------------------------------------------------------------------
template<bool zero>
bool JavascriptConversion::SameValueCommon(Var aLeft, Var aRight)
{
if (aLeft == aRight)
{
return true;
}
TypeId leftType = JavascriptOperators::GetTypeId(aLeft);
if (JavascriptOperators::IsUndefinedOrNullType(leftType))
{
return false;
}
TypeId rightType = JavascriptOperators::GetTypeId(aRight);
double dblLeft, dblRight;
switch (leftType)
{
case TypeIds_Integer:
switch (rightType)
{
case TypeIds_Integer:
return false;
case TypeIds_Number:
dblLeft = TaggedInt::ToDouble(aLeft);
dblRight = JavascriptNumber::GetValue(aRight);
goto CommonNumber;
case TypeIds_Int64Number:
{
int leftValue = TaggedInt::ToInt32(aLeft);
__int64 rightValue = UnsafeVarTo<JavascriptInt64Number>(aRight)->GetValue();
return leftValue == rightValue;
}
case TypeIds_UInt64Number:
{
int leftValue = TaggedInt::ToInt32(aLeft);
unsigned __int64 rightValue = VarTo<JavascriptInt64Number>(aRight)->GetValue();
return leftValue == rightValue;
}
}
break;
case TypeIds_Int64Number:
switch (rightType)
{
case TypeIds_Integer:
{
__int64 leftValue = UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
int rightValue = TaggedInt::ToInt32(aRight);
return leftValue == rightValue;
}
case TypeIds_Number:
dblLeft = (double)UnsafeVarTo<JavascriptInt64Number>(aLeft)->GetValue();
dblRight = JavascriptNumber::GetValue(aRight);
goto CommonNumber;
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<JavascriptInt64Number>(aRight)->GetValue();
return ((unsigned __int64)leftValue == rightValue);
}
}
break;
case TypeIds_UInt64Number:
switch (rightType)
{
case TypeIds_Integer:
{
unsigned __int64 leftValue = UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
__int64 rightValue = TaggedInt::ToInt32(aRight);
return (leftValue == (unsigned __int64)rightValue);
}
case TypeIds_Number:
dblLeft = (double)UnsafeVarTo<JavascriptUInt64Number>(aLeft)->GetValue();
dblRight = JavascriptNumber::GetValue(aRight);
goto CommonNumber;
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<JavascriptInt64Number>(aRight)->GetValue();
return leftValue == rightValue;
}
}
break;
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)UnsafeVarTo<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:
if (JavascriptNumber::IsNan(dblLeft) && JavascriptNumber::IsNan(dblRight))
{
return true;
}
if (zero)
{
// SameValueZero(+0,-0) returns true;
return dblLeft == dblRight;
}
else
{
// SameValue(+0,-0) returns false;
return (NumberUtilities::LuLoDbl(dblLeft) == NumberUtilities::LuLoDbl(dblRight) &&
NumberUtilities::LuHiDbl(dblLeft) == NumberUtilities::LuHiDbl(dblRight));
}
}
break;
case TypeIds_Boolean:
return false;
case TypeIds_String:
switch (rightType)
{
case TypeIds_String:
return JavascriptString::Equals(UnsafeVarTo<JavascriptString>(aLeft), UnsafeVarTo<JavascriptString>(aRight));
}
break;
#if DBG
case TypeIds_Symbol:
if (rightType == TypeIds_Symbol)
{
JavascriptSymbol* leftSymbol = UnsafeVarTo<JavascriptSymbol>(aLeft);
JavascriptSymbol* rightSymbol = UnsafeVarTo<JavascriptSymbol>(aRight);
Assert(leftSymbol->GetValue() != rightSymbol->GetValue());
}
#endif
default:
break;
}
return false;
}
template bool JavascriptConversion::SameValueCommon<false>(Var aLeft, Var aRight);
template bool JavascriptConversion::SameValueCommon<true>(Var aLeft, Var aRight);
//----------------------------------------------------------------------------
// ToObject() takes a value and converts it to an Object type
// Implementation of ES5 9.9
// The spec indicates that this method should throw a TypeError if the supplied value is Undefined or Null.
// Our implementation returns FALSE in this scenario, expecting the caller to throw the TypeError.
// This allows the caller to provide more context in the error message without having to unnecessarily
// construct the message string before knowing whether or not the value can be converted to an object.
//
// Undefined Return FALSE.
// Null Return FALSE.
// Boolean Create a new Boolean object whose [[PrimitiveValue]]
// internal property is set to the value of the boolean.
// See 15.6 for a description of Boolean objects.
// Return TRUE.
// Number Create a new Number object whose [[PrimitiveValue]]
// internal property is set to the value of the number.
// See 15.7 for a description of Number objects.
// Return TRUE.
// String Create a new String object whose [[PrimitiveValue]]
// internal property is set to the value of the string.
// See 15.5 for a description of String objects.
// Return TRUE.
// Object The result is the input argument (no conversion).
// Return TRUE.
//----------------------------------------------------------------------------
BOOL JavascriptConversion::ToObject(Var aValue, ScriptContext* scriptContext, RecyclableObject** object)
{
Assert(object);
switch (JavascriptOperators::GetTypeId(aValue))
{
case TypeIds_Undefined:
case TypeIds_Null:
return FALSE;
case TypeIds_Number:
case TypeIds_Integer:
*object = scriptContext->GetLibrary()->CreateNumberObject(aValue);
return TRUE;
default:
*object = VarTo<RecyclableObject>(aValue)->ToObject(scriptContext);
return TRUE;
}
}
//----------------------------------------------------------------------------
// ToPropertyKey() takes a value and converts it to a property key
// Implementation of ES6 7.1.14
//----------------------------------------------------------------------------
Var JavascriptConversion::ToPropertyKey(
Var argument,
_In_ ScriptContext* scriptContext,
_Out_ const PropertyRecord** propertyRecord,
_Out_opt_ PropertyString** propString)
{
Var key = JavascriptConversion::ToPrimitive<JavascriptHint::HintString>(argument, scriptContext);
PropertyString * propertyString = nullptr;
if (VarIs<JavascriptSymbol>(key))
{
// If we are looking up a property keyed by a symbol, we already have the PropertyId in the symbol
*propertyRecord = UnsafeVarTo<JavascriptSymbol>(key)->GetValue();
}
else
{
// For all other types, convert the key into a string and use that as the property name
JavascriptString * propName = JavascriptConversion::ToString(key, scriptContext);
propName->GetPropertyRecord(propertyRecord);
if (VarIs<PropertyString>(propName))
{
propertyString = UnsafeVarTo<PropertyString>(propName);
}
key = propName;
}
if (propString)
{
*propString = propertyString;
}
return key;
}
//----------------------------------------------------------------------------
// ToPrimitive() takes a value and an optional argument and converts it to a non Object type
// Implementation of ES5 9.1
//
// Undefined:The result equals the input argument (no conversion).
// Null: The result equals the input argument (no conversion).
// Boolean: The result equals the input argument (no conversion).
// Number: The result equals the input argument (no conversion).
// String: The result equals the input argument (no conversion).
// Object: Return a default value for the Object.
// The default value of an object is retrieved by calling the [[DefaultValue]]
// internal method of the object, passing the optional hint PreferredType.
// The behavior of the [[DefaultValue]] internal method is defined by this specification
// for all native ECMAScript objects (8.12.9).
//----------------------------------------------------------------------------
template <JavascriptHint hint>
Var JavascriptConversion::ToPrimitive(_In_ Var aValue, _In_ ScriptContext * requestContext)
{
switch (JavascriptOperators::GetTypeId(aValue))
{
case TypeIds_Undefined:
case TypeIds_Null:
case TypeIds_Integer:
case TypeIds_Boolean:
case TypeIds_Number:
case TypeIds_String:
case TypeIds_Symbol:
case TypeIds_BigInt:
return aValue;
case TypeIds_StringObject:
{
JavascriptStringObject * stringObject = UnsafeVarTo<JavascriptStringObject>(aValue);
ScriptContext * objectScriptContext = stringObject->GetScriptContext();
if (objectScriptContext->optimizationOverrides.GetSideEffects() & (hint == JavascriptHint::HintString ? SideEffects_ToString : SideEffects_ValueOf))
{
return MethodCallToPrimitive<hint>(stringObject, requestContext);
}
return CrossSite::MarshalVar(requestContext, stringObject->Unwrap(), objectScriptContext);
}
case TypeIds_NumberObject:
{
JavascriptNumberObject * numberObject = UnsafeVarTo<JavascriptNumberObject>(aValue);
ScriptContext * objectScriptContext = numberObject->GetScriptContext();
if (hint == JavascriptHint::HintString)
{
if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
{
return MethodCallToPrimitive<hint>(numberObject, requestContext);
}
return JavascriptNumber::ToStringRadix10(numberObject->GetValue(), requestContext);
}
else
{
if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
{
return MethodCallToPrimitive<hint>(numberObject, requestContext);
}
return CrossSite::MarshalVar(requestContext, numberObject->Unwrap(), objectScriptContext);
}
}
case TypeIds_SymbolObject:
{
JavascriptSymbolObject* symbolObject = UnsafeVarTo<JavascriptSymbolObject>(aValue);
ScriptContext* objectScriptContext = symbolObject->GetScriptContext();
if (objectScriptContext->optimizationOverrides.GetSideEffects() & SideEffects_ToPrimitive)
{
return MethodCallToPrimitive<hint>(symbolObject, requestContext);
}
return CrossSite::MarshalVar(requestContext, symbolObject->Unwrap(), objectScriptContext);
}
case TypeIds_Date:
{
JavascriptDate* dateObject = UnsafeVarTo<JavascriptDate>(aValue);
if(hint == JavascriptHint::HintNumber)
{
if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ValueOf)
{
// if no Method exists this function falls back to OrdinaryToPrimitive
// if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
return MethodCallToPrimitive<hint>(dateObject, requestContext);
}
return JavascriptNumber::ToVarNoCheck(dateObject->GetTime(), requestContext);
}
else
{
if (dateObject->GetScriptContext()->optimizationOverrides.GetSideEffects() & SideEffects_ToString)
{
// if no Method exists this function falls back to OrdinaryToPrimitive
// if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
return MethodCallToPrimitive<hint>(dateObject, requestContext);
}
return JavascriptDate::ToString(dateObject, requestContext);
}
}
// convert to JavascriptNumber
case TypeIds_Int64Number:
return UnsafeVarTo<JavascriptInt64Number>(aValue)->ToJavascriptNumber();
case TypeIds_UInt64Number:
return UnsafeVarTo<JavascriptUInt64Number>(aValue)->ToJavascriptNumber();
default:
// if no Method exists this function falls back to OrdinaryToPrimitive
// if IsES6ToPrimitiveEnabled flag is off we also fall back to OrdinaryToPrimitive
return MethodCallToPrimitive<hint>(UnsafeVarTo<RecyclableObject>(aValue), requestContext);
}
}
//----------------------------------------------------------------------------
//https://tc39.github.io/ecma262/#sec-canonicalnumericindexstring
//1. Assert : Type(argument) is String.
//2. If argument is "-0", then return -0.
//3. Let n be ToNumber(argument).
//4. If SameValue(ToString(n), argument) is false, then return undefined.
//5. Return n.
//----------------------------------------------------------------------------
BOOL JavascriptConversion::CanonicalNumericIndexString(JavascriptString *aValue, double *indexValue, ScriptContext * scriptContext)
{
if (JavascriptString::IsNegZero(aValue))
{
*indexValue = -0;
return TRUE;
}
double indexDoubleValue = aValue->ToDouble();
if (JavascriptString::Equals(JavascriptNumber::ToStringRadix10(indexDoubleValue, scriptContext), aValue))
{
*indexValue = indexDoubleValue;
return TRUE;
}
return FALSE;
}
template <JavascriptHint hint>
Var JavascriptConversion::MethodCallToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext * requestContext)
{
Var result = nullptr;
ScriptContext *const scriptContext = value->GetScriptContext();
//7.3.9 GetMethod(V, P)
// The abstract operation GetMethod is used to get the value of a specific property of an ECMAScript language value when the value of the
// property is expected to be a function. The operation is called with arguments V and P where V is the ECMAScript language value, P is the
// property key. This abstract operation performs the following steps:
// 1. Assert: IsPropertyKey(P) is true.
// 2. Let func be ? GetV(V, P).
// 3. If func is either undefined or null, return undefined.
// 4. If IsCallable(func) is false, throw a TypeError exception.
// 5. Return func.
Var varMethod = nullptr;
if (!requestContext->GetConfig()->IsES6ToPrimitiveEnabled()
|| JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(value)
|| !JavascriptOperators::GetPropertyReference(value, PropertyIds::_symbolToPrimitive, &varMethod, requestContext)
|| JavascriptOperators::IsUndefinedOrNull(varMethod))
{
return OrdinaryToPrimitive<hint>(value, requestContext);
}
if (!VarIs<JavascriptFunction>(varMethod))
{
// Don't error if we disabled implicit calls
JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_Property_NeedFunction, requestContext->GetPropertyName(PropertyIds::_symbolToPrimitive)->GetBuffer());
return requestContext->GetLibrary()->GetNull();
}
// Let exoticToPrim be GetMethod(input, @@toPrimitive).
JavascriptFunction* exoticToPrim = UnsafeVarTo<JavascriptFunction>(varMethod);
JavascriptString* hintString = nullptr;
if (hint == JavascriptHint::HintString)
{
hintString = requestContext->GetLibrary()->GetStringTypeDisplayString();
}
else if (hint == JavascriptHint::HintNumber)
{
hintString = requestContext->GetLibrary()->GetNumberTypeDisplayString();
}
else
{
hintString = requestContext->GetPropertyString(PropertyIds::default_);
}
// If exoticToPrim is not undefined, then
Assert(nullptr != exoticToPrim);
ThreadContext * threadContext = requestContext->GetThreadContext();
result = threadContext->ExecuteImplicitCall(exoticToPrim, ImplicitCall_ToPrimitive, [=]()->Js::Var
{
// Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
Assert(!ThreadContext::IsOnStack(value));
// Let result be the result of calling the[[Call]] internal method of exoticToPrim, with input as thisArgument and(hint) as argumentsList.
return CALL_FUNCTION(threadContext, exoticToPrim, CallInfo(CallFlags_Value, 2), value, hintString);
});
if (!result)
{
// There was an implicit call and implicit calls are disabled. This would typically cause a bailout.
Assert(threadContext->IsDisableImplicitCall());
return requestContext->GetLibrary()->GetNull();
}
Assert(!CrossSite::NeedMarshalVar(result, requestContext));
// If result is an ECMAScript language value and Type(result) is not Object, then return result.
if (TaggedInt::Is(result) || !JavascriptOperators::IsObjectType(JavascriptOperators::GetTypeId(result)))
{
return result;
}
// Else, throw a TypeError exception.
else
{
// Don't error if we disabled implicit calls
JavascriptError::TryThrowTypeError(scriptContext, requestContext, JSERR_FunctionArgument_Invalid, _u("[Symbol.toPrimitive]"));
return requestContext->GetLibrary()->GetNull();
}
}
template <JavascriptHint hint>
Var JavascriptConversion::OrdinaryToPrimitive(_In_ RecyclableObject* value, _In_ ScriptContext* requestContext)
{
Var result;
if (!value->ToPrimitive(hint, &result, requestContext))
{
ScriptContext *const scriptContext = value->GetScriptContext();
int32 hCode;
switch (hint)
{
case JavascriptHint::HintNumber:
hCode = JSERR_NeedNumber;
break;
case JavascriptHint::HintString:
hCode = JSERR_NeedString;
break;
default:
hCode = VBSERR_OLENoPropOrMethod;
break;
}
JavascriptError::TryThrowTypeError(scriptContext, scriptContext, hCode);
return requestContext->GetLibrary()->GetNull();
}
return result;
}
template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintNumber>(RecyclableObject* value, ScriptContext* requestContext);
template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::HintString>(RecyclableObject* value, ScriptContext* requestContext);
template Var JavascriptConversion::OrdinaryToPrimitive<JavascriptHint::None>(RecyclableObject* value, ScriptContext* requestContext);
JavascriptString *JavascriptConversion::CoerseString(Var aValue, ScriptContext* scriptContext, const char16* apiNameForErrorMsg)
{
JIT_HELPER_REENTRANT_HEADER(Op_CoerseString);
if (!JavascriptConversion::CheckObjectCoercible(aValue, scriptContext))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, apiNameForErrorMsg);
}
return ToString(aValue, scriptContext);
JIT_HELPER_END(Op_CoerseString);
}
//----------------------------------------------------------------------------
// ToString - abstract operation
// ES5 9.8
//Input Type Result
// Undefined
// "undefined"
// Null
// "null"
// Boolean
// If the argument is true, then the result is "true". If the argument is false, then the result is "false".
// Number
// See 9.8.1 below.
// String
// Return the input argument (no conversion)
// Object
// Apply the following steps:
// 1. Let primValue be ToPrimitive(input argument, hint String).
// 2. Return ToString(primValue).
//----------------------------------------------------------------------------
JavascriptString *JavascriptConversion::ToString(Var aValue, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_ConvString);
Assert(scriptContext->GetThreadContext()->IsScriptActive());
BOOL fPrimitiveOnly = false;
while(true)
{
switch (JavascriptOperators::GetTypeId(aValue))
{
case TypeIds_Undefined:
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
case TypeIds_Null:
return scriptContext->GetLibrary()->GetNullDisplayString();
case TypeIds_Integer:
return scriptContext->GetIntegerString(aValue);
case TypeIds_Boolean:
return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
case TypeIds_Number:
return JavascriptNumber::ToStringRadix10(JavascriptNumber::GetValue(aValue), scriptContext);
case TypeIds_Int64Number:
{
__int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
if (!TaggedInt::IsOverflow(value))
{
return scriptContext->GetIntegerString((int)value);
}
else
{
return JavascriptInt64Number::ToString(aValue, scriptContext);
}
}
case TypeIds_UInt64Number:
{
unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
if (!TaggedInt::IsOverflow(value))
{
return scriptContext->GetIntegerString((uint)value);
}
else
{
return JavascriptUInt64Number::ToString(aValue, scriptContext);
}
}
case TypeIds_String:
{
ScriptContext* aValueScriptContext = Js::UnsafeVarTo<Js::RecyclableObject>(aValue)->GetScriptContext();
return UnsafeVarTo<JavascriptString>(CrossSite::MarshalVar(scriptContext,
aValue, aValueScriptContext));
}
case TypeIds_Symbol:
return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
case TypeIds_SymbolObject:
return JavascriptSymbol::ToString(UnsafeVarTo<JavascriptSymbolObject>(aValue)->GetValue(), scriptContext);
case TypeIds_GlobalObject:
aValue = static_cast<Js::GlobalObject*>(aValue)->ToThis();
// fall through
default:
{
AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToString");
if(fPrimitiveOnly)
{
AssertMsg(FALSE, "wrong call in ToString, no dynamic objects should get here");
JavascriptError::ThrowError(scriptContext, VBSERR_InternalError);
}
fPrimitiveOnly = true;
aValue = ToPrimitive<JavascriptHint::HintString>(aValue, scriptContext);
}
}
}
JIT_HELPER_END(Op_ConvString);
}
JavascriptString *JavascriptConversion::ToLocaleString(Var aValue, ScriptContext* scriptContext)
{
switch (JavascriptOperators::GetTypeId(aValue))
{
case TypeIds_Undefined:
return scriptContext->GetLibrary()->GetUndefinedDisplayString();
case TypeIds_Null:
return scriptContext->GetLibrary()->GetNullDisplayString();
case TypeIds_Integer:
return JavascriptNumber::ToLocaleString(TaggedInt::ToInt32(aValue), scriptContext);
case TypeIds_Boolean:
return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? scriptContext->GetLibrary()->GetTrueDisplayString() : scriptContext->GetLibrary()->GetFalseDisplayString();
case TypeIds_Int64Number:
return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue(), scriptContext);
case TypeIds_UInt64Number:
return JavascriptNumber::ToLocaleString((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue(), scriptContext);
case TypeIds_Number:
return JavascriptNumber::ToLocaleString(JavascriptNumber::GetValue(aValue), scriptContext);
case TypeIds_Symbol:
return UnsafeVarTo<JavascriptSymbol>(aValue)->ToString(scriptContext);
default:
{
RecyclableObject* object = VarTo<RecyclableObject>(aValue);
Var value = JavascriptOperators::GetProperty(object, PropertyIds::toLocaleString, scriptContext, NULL);
if (JavascriptConversion::IsCallable(value))
{
RecyclableObject* toLocaleStringFunction = VarTo<RecyclableObject>(value);
Var aResult = scriptContext->GetThreadContext()->ExecuteImplicitCall(toLocaleStringFunction, Js::ImplicitCall_ToPrimitive, [=]()->Js::Var
{
return CALL_FUNCTION(scriptContext->GetThreadContext(), toLocaleStringFunction, CallInfo(1), aValue);
});
if (VarIs<JavascriptString>(aResult))
{
return UnsafeVarTo<JavascriptString>(aResult);
}
else
{
return JavascriptConversion::ToString(aResult, scriptContext);
}
}
JavascriptError::ThrowTypeError(scriptContext, JSERR_Property_NeedFunction, scriptContext->GetPropertyName(PropertyIds::toLocaleString)->GetBuffer());
}
}
}
//----------------------------------------------------------------------------
// ToBoolean_Full:
// (ES3.0: S9.2):
//
// Input Output
// ----- ------
// 'undefined' 'false'
// 'null' 'false'
// Boolean Value
// Number 'false' if +0, -0, or Nan
// 'true' otherwise
// String 'false' if argument is ""
// 'true' otherwise
// Object 'true'
// Falsy Object 'false'
//----------------------------------------------------------------------------
BOOL JavascriptConversion::ToBoolean_Full(Var aValue, ScriptContext* scriptContext)
{
AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
AssertMsg(VarIs<RecyclableObject>(aValue), "Should be handled already");
auto type = UnsafeVarTo<RecyclableObject>(aValue)->GetType();
switch (type->GetTypeId())
{
case TypeIds_Undefined:
case TypeIds_Null:
return false;
case TypeIds_Symbol:
return true;
#if !FLOATVAR
case TypeIds_Number:
{
double value = JavascriptNumber::GetValue(aValue);
return (!JavascriptNumber::IsNan(value)) && (!JavascriptNumber::IsZero(value));
}
#endif
case TypeIds_Int64Number:
{
__int64 value = UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
return value != 0;
}
case TypeIds_UInt64Number:
{
unsigned __int64 value = UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
return value != 0;
}
case TypeIds_String:
{
JavascriptString * pstValue = UnsafeVarTo<JavascriptString>(aValue);
return pstValue->GetLength() > 0;
}
default:
{
AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToBoolean");
// Falsy objects evaluate to false when converted to Boolean.
return !type->IsFalsy();
}
}
}
void JavascriptConversion::ToFloat_Helper(Var aValue, float *pResult, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_ConvFloat_Helper);
*pResult = (float)ToNumber_Full(aValue, scriptContext);
JIT_HELPER_END(Op_ConvFloat_Helper);
}
void JavascriptConversion::ToNumber_Helper(Var aValue, double *pResult, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_Helper);
Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
*pResult = ToNumber_Full(aValue, scriptContext);
JIT_HELPER_END(Op_ConvNumber_Helper);
}
// Used for the JIT's float type specialization
// Convert aValue to double, but only allow primitives. Return false otherwise.
BOOL JavascriptConversion::ToNumber_FromPrimitive(Var aValue, double *pResult, BOOL allowUndefined, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Op_ConvNumber_FromPrimitive);
Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
Assert(!TaggedNumber::Is(aValue));
RecyclableObject *obj = VarTo<RecyclableObject>(aValue);
// NOTE: Don't allow strings, otherwise JIT's float type specialization has to worry about concats
if (obj->GetTypeId() >= TypeIds_String)
{
return false;
}
if (!allowUndefined && obj->GetTypeId() == TypeIds_Undefined)
{
return false;
}
*pResult = ToNumber_Full(aValue, scriptContext);
return true;
JIT_HELPER_END(Op_ConvNumber_FromPrimitive);
}
//----------------------------------------------------------------------------
// ToNumber_Full:
// Implements ES6 Draft Rev 26 July 18, 2014
//
// Undefined: NaN
// Null: 0
// boolean: v==true ? 1 : 0 ;
// number: v (original number)
// String: conversion by spec algorithm
// object: ToNumber(PrimitiveValue(v, hint_number))
// Symbol: TypeError
//----------------------------------------------------------------------------
double JavascriptConversion::ToNumber_Full(Var aValue,ScriptContext* scriptContext)
{
AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
BOOL fPrimitiveOnly = false;
while(true)
{
switch (JavascriptOperators::GetTypeId(aValue))
{
case TypeIds_Symbol:
JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
// Fallthrough to return NaN if exceptions are disabled
case TypeIds_Undefined:
return JavascriptNumber::GetValue(scriptContext->GetLibrary()->GetNaN());
case TypeIds_Null:
return 0;
case TypeIds_Integer:
return TaggedInt::ToDouble(aValue);
case TypeIds_Boolean:
return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
case TypeIds_Number:
return JavascriptNumber::GetValue(aValue);
case TypeIds_Int64Number:
return (double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue();
case TypeIds_UInt64Number:
return (double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue();
case TypeIds_String:
return UnsafeVarTo<JavascriptString>(aValue)->ToDouble();
default:
{
AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
if(fPrimitiveOnly)
{
JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
}
fPrimitiveOnly = true;
aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
}
}
}
}
//----------------------------------------------------------------------------
// second part of the ToInteger() implementation.(ES5.0: S9.4).
//----------------------------------------------------------------------------
double JavascriptConversion::ToInteger_Full(Var aValue,ScriptContext* scriptContext)
{
AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
BOOL fPrimitiveOnly = false;
while(true)
{
switch (JavascriptOperators::GetTypeId(aValue))
{
case TypeIds_Symbol:
JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
// Fallthrough to return 0 if exceptions are disabled
case TypeIds_Undefined:
case TypeIds_Null:
return 0;
case TypeIds_Integer:
return TaggedInt::ToInt32(aValue);
case TypeIds_Boolean:
return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
case TypeIds_Number:
return ToInteger(JavascriptNumber::GetValue(aValue));
case TypeIds_Int64Number:
return ToInteger((double)UnsafeVarTo<JavascriptInt64Number>(aValue)->GetValue());
case TypeIds_UInt64Number:
return ToInteger((double)UnsafeVarTo<JavascriptUInt64Number>(aValue)->GetValue());
case TypeIds_String:
return ToInteger(UnsafeVarTo<JavascriptString>(aValue)->ToDouble());
default:
{
AssertMsg(JavascriptOperators::IsObject(aValue), "bad type object in conversion ToInteger");
if(fPrimitiveOnly)
{
AssertMsg(FALSE, "wrong call in ToInteger_Full, no dynamic objects should get here");
JavascriptError::ThrowError(scriptContext, VBSERR_OLENoPropOrMethod);
}
fPrimitiveOnly = true;
aValue = ToPrimitive<JavascriptHint::HintNumber>(aValue, scriptContext);
}
}
}
}
double JavascriptConversion::ToInteger(double val)
{
if(JavascriptNumber::IsNan(val))
return 0;
if(JavascriptNumber::IsPosInf(val) || JavascriptNumber::IsNegInf(val) ||
JavascriptNumber::IsZero(val))
{
return val;
}
return ( ((val < 0) ? -1 : 1 ) * floor(fabs(val)));
}
//----------------------------------------------------------------------------
// ToInt32() converts the given Var to an Int32 value, as described in
// (ES3.0: S9.5).
//----------------------------------------------------------------------------
int32 JavascriptConversion::ToInt32_Full(Var aValue, ScriptContext* scriptContext)
{
JIT_HELPER_REENTRANT_HEADER(Conv_ToInt32_Full);
Assert(Js::JavascriptStackWalker::ValidateTopJitFrame(scriptContext));
AssertMsg(!TaggedInt::Is(aValue), "Should be detected");
ScriptContext * objectScriptContext = VarIs<RecyclableObject>(aValue) ? UnsafeVarTo<RecyclableObject>(aValue)->GetScriptContext() : nullptr;
// This is used when TaggedInt's overflow but remain under int32
// so Number is our most critical case:
TypeId typeId = JavascriptOperators::GetTypeId(aValue);
if (typeId == TypeIds_Number)
{
return JavascriptMath::ToInt32Core(JavascriptNumber::GetValue(aValue));
}
switch (typeId)
{
case TypeIds_Symbol:
JavascriptError::TryThrowTypeError(objectScriptContext, scriptContext, JSERR_NeedNumber);
// Fallthrough to return 0 if exceptions are disabled
case TypeIds_Undefined:
case TypeIds_Null:
return 0;
case TypeIds_Integer:
return TaggedInt::ToInt32(aValue);
case TypeIds_Boolean:
return UnsafeVarTo<JavascriptBoolean>(aValue)->GetValue() ? 1 : +0;
case TypeIds_Int64Number: