-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathJavascriptObject.cpp
2299 lines (1910 loc) · 88.9 KB
/
JavascriptObject.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.
// 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 "RuntimeLibraryPch.h"
using namespace Js;
Var JavascriptObject::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
JavascriptLibrary* library = scriptContext->GetLibrary();
AssertMsg(args.HasArg(), "Should always have implicit 'this'");
Var newTarget = args.GetNewTarget();
if (JavascriptOperators::GetAndAssertIsConstructorSuperCall(args) &&
newTarget != function)
{
return JavascriptOperators::OrdinaryCreateFromConstructor(
VarTo<RecyclableObject>(newTarget),
library->CreateObject(true),
nullptr,
scriptContext);
}
Var arg = args.Info.Count > 1 ? args[1] : library->GetUndefined();
switch (JavascriptOperators::GetTypeId(arg))
{
case TypeIds_Undefined:
case TypeIds_Null:
// Null and undefined result in a new object
return (callInfo.Flags & CallFlags_NotUsed)
? arg
: library->CreateObject(true);
case TypeIds_StringObject:
case TypeIds_Function:
case TypeIds_Array:
case TypeIds_ES5Array:
case TypeIds_RegEx:
case TypeIds_NumberObject:
case TypeIds_SIMDObject:
case TypeIds_Date:
case TypeIds_BooleanObject:
case TypeIds_Error:
case TypeIds_Object:
case TypeIds_Arguments:
case TypeIds_ActivationObject:
case TypeIds_SymbolObject:
// Since we know this is an object, we can skip ToObject
return arg;
}
RecyclableObject* result = nullptr;
JavascriptConversion::ToObject(arg, scriptContext, &result);
Assert(result);
return result;
}
Var JavascriptObject::EntryHasOwnProperty(RecyclableObject* function, CallInfo callInfo, ...)
{
JIT_HELPER_REENTRANT_HEADER(Object_HasOwnProperty);
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
RecyclableObject* dynamicObject = nullptr;
if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.hasOwnProperty"));
}
Var propertyName = args.Info.Count == 1 ? scriptContext->GetLibrary()->GetUndefined() : args[1];
const PropertyRecord* propertyRecord;
PropertyString* propertyString;
JavascriptConversion::ToPropertyKey(propertyName, scriptContext, &propertyRecord, &propertyString);
if (JavascriptOperators::HasOwnProperty(dynamicObject, propertyRecord->GetPropertyId(), scriptContext, propertyString))
{
return scriptContext->GetLibrary()->GetTrue();
}
return scriptContext->GetLibrary()->GetFalse();
JIT_HELPER_END(Object_HasOwnProperty);
}
Var JavascriptObject::EntryHasOwn(RecyclableObject* function, CallInfo callInfo, ...)
{
JIT_HELPER_REENTRANT_HEADER(Object_HasOwn);
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
RecyclableObject* dynamicObject = nullptr;
// first parameter must exist and be an object coercible or throw type error
if (args.Info.Count < 2 || FALSE == JavascriptConversion::ToObject(args[1], scriptContext, &dynamicObject))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.hasOwn"));
}
// if there is only one parameter use undefined as the property to query
Var propertyName = args.Info.Count == 2 ? scriptContext->GetLibrary()->GetUndefined() : args[2];
const PropertyRecord* propertyRecord;
PropertyString* propertyString;
JavascriptConversion::ToPropertyKey(propertyName, scriptContext, &propertyRecord, &propertyString);
if (JavascriptOperators::HasOwnProperty(dynamicObject, propertyRecord->GetPropertyId(), scriptContext, propertyString))
{
return scriptContext->GetLibrary()->GetTrue();
}
return scriptContext->GetLibrary()->GetFalse();
JIT_HELPER_END(Object_HasOwn);
}
Var JavascriptObject::EntryPropertyIsEnumerable(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
RecyclableObject* dynamicObject = nullptr;
if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.propertyIsEnumerable"));
}
if (args.Info.Count >= 2)
{
const PropertyRecord* propertyRecord;
JavascriptConversion::ToPropertyKey(args[1], scriptContext, &propertyRecord, nullptr);
PropertyId propertyId = propertyRecord->GetPropertyId();
PropertyDescriptor currentDescriptor;
BOOL isCurrentDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(dynamicObject, propertyId, scriptContext, ¤tDescriptor);
if (isCurrentDescriptorDefined == TRUE)
{
if (currentDescriptor.IsEnumerable())
{
return scriptContext->GetLibrary()->GetTrue();
}
}
}
return scriptContext->GetLibrary()->GetFalse();
}
BOOL JavascriptObject::ChangePrototype(RecyclableObject* object, RecyclableObject* newPrototype, bool shouldThrow, ScriptContext* scriptContext)
{
// 8.3.2 [[SetInheritance]] (V)
// When the [[SetInheritance]] internal method of O is called with argument V the following steps are taken:
// 1. Assert: Either Type(V) is Object or Type(V) is Null.
Assert(JavascriptOperators::IsObject(object));
Assert(JavascriptOperators::IsObjectOrNull(newPrototype));
if (VarIs<JavascriptProxy>(object))
{
JavascriptProxy* proxy = VarTo<JavascriptProxy>(object);
CrossSite::ForceCrossSiteThunkOnPrototypeChain(newPrototype);
return proxy->SetPrototypeTrap(newPrototype, shouldThrow, scriptContext);
}
// 2. Let extensible be the value of the [[Extensible]] internal data property of O.
// 3. Let current be the value of the [[Prototype]] internal data property of O.
// 4. If SameValue(V, current), then return true.
if (newPrototype == JavascriptObject::GetPrototypeOf(object, scriptContext))
{
return TRUE;
}
// 5. If extensible is false, then return false.
if (!object->IsExtensible())
{
if (shouldThrow)
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_NonExtensibleObject);
}
return FALSE;
}
if (object->IsProtoImmutable())
{
// ES2016 19.1.3:
// The Object prototype object is the intrinsic object %ObjectPrototype%.
// The Object prototype object is an immutable prototype exotic object.
// ES2016 9.4.7:
// An immutable prototype exotic object is an exotic object that has an immutable [[Prototype]] internal slot.
JavascriptError::ThrowTypeError(scriptContext, JSERR_ImmutablePrototypeSlot);
}
// 6. If V is not null, then
// a. Let p be V.
// b. Repeat, while p is not null
// i. If SameValue(p, O) is true, then return false.
// ii. Let nextp be the result of calling the [[GetInheritance]] internal method of p with no arguments.
// iii. ReturnIfAbrupt(nextp).
// iv. Let p be nextp.
if (IsPrototypeOfStopAtProxy(object, newPrototype, scriptContext)) // Reject cycle
{
if (shouldThrow)
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_CyclicProtoValue);
}
return FALSE;
}
// 7. Set the value of the [[Prototype]] internal data property of O to V.
// 8. Return true.
bool isInvalidationOfInlineCacheNeeded = true;
DynamicObject * obj = VarTo<DynamicObject>(object);
// If this object was not prototype object, then no need to invalidate inline caches.
// Simply assign it a new type so if this object used protoInlineCache in past, it will
// be invalidated because of type mismatch and subsequently we will update its protoInlineCache
if (!(obj->GetDynamicType()->GetTypeHandler()->GetFlags() & DynamicTypeHandler::IsPrototypeFlag))
{
// If object has locked type, skip changing its type here as it will be changed anyway below
// when object gets newPrototype object.
if (!obj->HasLockedType())
{
obj->ChangeType();
}
Assert(!obj->GetScriptContext()->GetThreadContext()->IsObjectRegisteredInProtoInlineCaches(obj));
Assert(!obj->GetScriptContext()->GetThreadContext()->IsObjectRegisteredInStoreFieldInlineCaches(obj));
isInvalidationOfInlineCacheNeeded = false;
}
if (isInvalidationOfInlineCacheNeeded)
{
// Invalidate the "instanceof" cache
ThreadContext* threadContext = scriptContext->GetThreadContext();
threadContext->MapIsInstInlineCaches([threadContext, object](const Js::Var function, Js::IsInstInlineCache* inlineCacheList) {
Assert(inlineCacheList != nullptr);
JavascriptFunction* jsFunction = VarTo<JavascriptFunction>(function);
// Check if cached function type is same as the old prototype
bool clearCurrentCacheList = jsFunction->GetType() == object->GetType();
if (!clearCurrentCacheList)
{
// Check if function prototype contains old prototype
JavascriptOperators::MapObjectAndPrototypes<true>(jsFunction->GetPrototype(), [&](RecyclableObject* obj)
{
if (object->GetType() == obj->GetType())
clearCurrentCacheList = true;
});
}
if (clearCurrentCacheList)
{
threadContext->InvalidateIsInstInlineCachesForFunction(function);
return;
}
Js::IsInstInlineCache* curInlineCache;
Js::IsInstInlineCache* nextInlineCache;
for (curInlineCache = inlineCacheList; curInlineCache != nullptr; curInlineCache = nextInlineCache)
{
// Stash away the next cache before we potentially zero out current one
nextInlineCache = curInlineCache->next;
bool clearCurrentCache = curInlineCache->type == object->GetType();
if (!clearCurrentCache) {
// Check if function prototype contains old prototype
JavascriptOperators::MapObjectAndPrototypes<true>(curInlineCache->type->GetPrototype(), [&](RecyclableObject* obj)
{
if (object->GetType() == obj->GetType())
clearCurrentCache = true;
});
}
if (clearCurrentCache)
{
// Fix cache list
// Deletes empty entries
threadContext->UnregisterIsInstInlineCache(curInlineCache, function);
// Actually invalidate current cache
memset(curInlineCache, 0, sizeof(Js::IsInstInlineCache));
}
}
});
bool allProtoCachesInvalidated = false;
JavascriptOperators::MapObjectAndPrototypes<true>(newPrototype, [&](RecyclableObject* obj)
{
obj->ClearProtoCachesWereInvalidated();
});
// Notify old prototypes that they are being removed from a prototype chain. This triggers invalidating protocache, etc.
JavascriptOperators::MapObjectAndPrototypesUntil<true>(object->GetPrototype(), [&](RecyclableObject* obj)->bool
{
obj->RemoveFromPrototype(scriptContext, &allProtoCachesInvalidated);
return allProtoCachesInvalidated;
});
// Examine new prototype chain. If it brings in any special property, we need to invalidate related caches.
bool objectAndPrototypeChainHasNoSpecialProperties =
JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(newPrototype);
if (!objectAndPrototypeChainHasNoSpecialProperties
|| object->GetScriptContext() != newPrototype->GetScriptContext())
{
// The HaveNoSpecialProperties cache is cleared when a property is added or changed,
// but only for types in the same script context. Therefore, if the prototype is in another
// context, the object's cache won't be cleared when a property is added or changed on the prototype.
// Moreover, an object is added to the cache only when its whole prototype chain is in the same
// context.
//
// Since we don't have a way to find out which objects have a certain object as their prototype,
// we clear the cache here instead.
object->GetLibrary()->GetTypesWithNoSpecialPropertyProtoChainCache()->Clear();
}
// Examine new prototype chain. If it brings in any non-WritableData property, we need to invalidate related caches.
bool objectAndPrototypeChainHasOnlyWritableDataProperties =
JavascriptOperators::CheckIfObjectAndPrototypeChainHasOnlyWritableDataProperties(newPrototype);
if (!objectAndPrototypeChainHasOnlyWritableDataProperties
|| object->GetScriptContext() != newPrototype->GetScriptContext())
{
// The HaveOnlyWritableDataProperties cache is cleared when a property is added or changed,
// but only for types in the same script context. Therefore, if the prototype is in another
// context, the object's cache won't be cleared when a property is added or changed on the prototype.
// Moreover, an object is added to the cache only when its whole prototype chain is in the same
// context.
//
// Since we don't have a way to find out which objects have a certain object as their prototype,
// we clear the cache here instead.
// Invalidate fast prototype chain writable data test flag
object->GetLibrary()->GetTypesWithOnlyWritablePropertyProtoChainCache()->Clear();
}
if (!allProtoCachesInvalidated)
{
// Invalidate StoreField/PropertyGuards for any non-WritableData property in the new chain
JavascriptOperators::MapObjectAndPrototypesUntil<true>(newPrototype, [&](RecyclableObject* obj)->bool
{
obj->AddToPrototype(scriptContext, &allProtoCachesInvalidated);
return allProtoCachesInvalidated;
});
}
JavascriptOperators::MapObjectAndPrototypesUntil<true>(object->GetPrototype(), [](RecyclableObject* obj)->bool
{
return obj->ClearProtoCachesWereInvalidated();
});
}
// Set to new prototype
if (object->IsExternal() || (DynamicType::Is(object->GetTypeId()) && (UnsafeVarTo<DynamicObject>(object))->IsCrossSiteObject()))
{
CrossSite::ForceCrossSiteThunkOnPrototypeChain(newPrototype);
}
object->SetPrototype(newPrototype);
return TRUE;
}
Var JavascriptObject::EntryIsPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
// no property specified
if (args.Info.Count == 1 || !JavascriptOperators::IsObject(args[1]))
{
return scriptContext->GetLibrary()->GetFalse();
}
RecyclableObject* dynamicObject = nullptr;
if (FALSE == JavascriptConversion::ToObject(args[0], scriptContext, &dynamicObject))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.isPrototypeOf"));
}
RecyclableObject* value = VarTo<RecyclableObject>(args[1]);
if (dynamicObject->GetTypeId() == TypeIds_GlobalObject)
{
dynamicObject = VarTo<RecyclableObject>(static_cast<Js::GlobalObject*>(dynamicObject)->ToThis());
}
while (!JavascriptOperators::IsNull(value))
{
value = JavascriptOperators::GetPrototype(value);
if (dynamicObject == value)
{
return scriptContext->GetLibrary()->GetTrue();
}
}
return scriptContext->GetLibrary()->GetFalse();
}
// 19.1.3.5 - Object.prototype.toLocaleString as of ES6 (6.0)
Var JavascriptObject::EntryToLocaleString(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
AssertMsg(args.Info.Count, "Should always have implicit 'this'");
Var thisValue = args[0];
RecyclableObject* dynamicObject = nullptr;
if (FALSE == JavascriptConversion::ToObject(thisValue, scriptContext, &dynamicObject))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.toLocaleString"));
}
Var toStringVar = nullptr;
if (!JavascriptOperators::GetProperty(thisValue, dynamicObject, Js::PropertyIds::toString, &toStringVar, scriptContext) || !JavascriptConversion::IsCallable(toStringVar))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedFunction, _u("Object.prototype.toLocaleString"));
}
RecyclableObject* toStringFunc = VarTo<RecyclableObject>(toStringVar);
if (toStringFunc == scriptContext->GetLibrary()->GetObjectToStringFunction())
{
return ToStringHelper(thisValue, scriptContext);
}
else
{
return scriptContext->GetThreadContext()->ExecuteImplicitCall(toStringFunc, Js::ImplicitCall_ToPrimitive, [=]()->Js::Var
{
return CALL_FUNCTION(scriptContext->GetThreadContext(), toStringFunc, CallInfo(CallFlags_Value, 1), thisValue);
});
}
}
Var JavascriptObject::EntryToString(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
AssertMsg(args.Info.Count, "Should always have implicit 'this'");
return ToStringHelper(args[0], scriptContext);
}
Var JavascriptObject::GetToStringTagValue(RecyclableObject *thisArg, ScriptContext *scriptContext)
{
if (JavascriptOperators::CheckIfObjectAndProtoChainHasNoSpecialProperties(thisArg))
{
return nullptr;
}
const PropertyId toStringTagId(PropertyIds::_symbolToStringTag);
PolymorphicInlineCache *cache = scriptContext->GetLibrary()->GetToStringTagCache();
PropertyValueInfo info;
// We don't allow cache resizing, at least for the moment: it's more work, and since there's only one
// cache per script context, we can afford to create each cache with the maximum size.
PropertyValueInfo::SetCacheInfo(&info, cache, false);
Var value;
if (CacheOperators::TryGetProperty<
true, // CheckLocal
true, // CheckProto
true, // CheckAccessor
true, // CheckMissing
true, // CheckPolymorphicInlineCache
true, // CheckTypePropertyCache
!PolymorphicInlineCache::IsPolymorphic, // IsInlineCacheAvailable
PolymorphicInlineCache::IsPolymorphic, // IsPolymorphicInlineCacheAvailable
false, // ReturnOperationInfo
false> // OutputExistence
(thisArg, false, thisArg, toStringTagId, &value, scriptContext, nullptr, &info))
{
return value;
}
else
{
#if DBG_DUMP
if (PHASE_VERBOSE_TRACE1(Js::InlineCachePhase))
{
CacheOperators::TraceCache(cache, _u("PatchGetValue"), toStringTagId, scriptContext, thisArg);
}
#endif
return JavascriptOperators::GetProperty(thisArg, thisArg, toStringTagId, scriptContext, &info);
}
}
// ES2017 19.1.3.6 Object.prototype.toString()
JavascriptString* JavascriptObject::ToStringTagHelper(Var thisArg, ScriptContext *scriptContext, TypeId type)
{
JavascriptLibrary *library = scriptContext->GetLibrary();
// 1. If the this value is undefined, return "[object Undefined]".
if (type == TypeIds_Undefined)
{
return library->GetObjectUndefinedDisplayString();
}
// 2. If the this value is null, return "[object Null]".
if (type == TypeIds_Null)
{
return library->GetObjectNullDisplayString();
}
// 3. Let O be ToObject(this value).
RecyclableObject *thisArgAsObject = JavascriptOperators::ToObject(thisArg, scriptContext);
// 15. Let tag be ? Get(O, @@toStringTag).
Var tag = JavascriptObject::GetToStringTagValue(thisArgAsObject, scriptContext);
// 17. Return the String that is the result of concatenating "[object ", tag, and "]".
auto buildToString = [&scriptContext](Var tag) {
JavascriptString *tagStr = VarTo<JavascriptString>(tag);
const WCHAR objectStartString[9] = _u("[object ");
const WCHAR objectEndString[1] = { _u(']') };
CompoundString *const cs = CompoundString::NewWithCharCapacity(_countof(objectStartString)
+ _countof(objectEndString) + tagStr->GetLength(), scriptContext->GetLibrary());
cs->AppendChars(objectStartString, _countof(objectStartString) - 1 /* ditch \0 */);
cs->AppendChars(tagStr);
cs->AppendChars(objectEndString, _countof(objectEndString));
return cs;
};
if (tag != nullptr && VarIs<JavascriptString>(tag))
{
return buildToString(tag);
}
// 4. Let isArray be ? IsArray(O).
// There is an implicit check for a null proxy handler in IsArray, so use the operator.
BOOL isArray = JavascriptOperators::IsArray(thisArgAsObject);
// If we don't have a tag or it's not a string, use the 'built in tag'.
if (isArray)
{
// 5. If isArray is true, let builtinTag be "Array".
return library->GetObjectArrayDisplayString();
}
// callable proxy is considered as having [[Call]] internal method and should match #8 below
if (type == TypeIds_Proxy && JavascriptConversion::IsCallable(thisArgAsObject))
{
type = TypeIds_Function;
}
JavascriptString* builtInTag = nullptr;
switch (type)
{
// 6. Else if O is an exotic String object, let builtinTag be "String".
case TypeIds_String:
case TypeIds_StringObject:
builtInTag = library->GetObjectStringDisplayString();
break;
// 7. Else if O has an[[ParameterMap]] internal slot, let builtinTag be "Arguments".
case TypeIds_Arguments:
builtInTag = library->GetObjectArgumentsDisplayString();
break;
// 8. Else if O has a [[Call]] internal method, let builtinTag be "Function".
case TypeIds_Function:
builtInTag = library->GetObjectFunctionDisplayString();
break;
// 9. Else if O has an [[ErrorData]] internal slot, let builtinTag be "Error".
case TypeIds_Error:
builtInTag = library->GetObjectErrorDisplayString();
break;
// 10. Else if O has a [[BooleanData]] internal slot, let builtinTag be "Boolean".
case TypeIds_Boolean:
case TypeIds_BooleanObject:
builtInTag = library->GetObjectBooleanDisplayString();
break;
// 11. Else if O has a [[NumberData]] internal slot, let builtinTag be "Number".
case TypeIds_Number:
case TypeIds_Int64Number:
case TypeIds_UInt64Number:
case TypeIds_Integer:
case TypeIds_NumberObject:
builtInTag = library->GetObjectNumberDisplayString();
break;
// 12. Else if O has a [[DateValue]] internal slot, let builtinTag be "Date".
case TypeIds_Date:
builtInTag = library->GetObjectDateDisplayString();
break;
// 13. Else if O has a [[RegExpMatcher]] internal slot, let builtinTag be "RegExp".
case TypeIds_RegEx:
builtInTag = library->GetObjectRegExpDisplayString();
break;
// 14. Else, let builtinTag be "Object".
default:
{
if (thisArgAsObject->IsExternal())
{
builtInTag = buildToString(thisArgAsObject->GetClassName(scriptContext));
}
else
{
builtInTag = library->GetObjectDisplayString(); // [object Object]
}
break;
}
}
Assert(builtInTag != nullptr);
return builtInTag;
}
Var JavascriptObject::ToStringHelper(Var thisArg, ScriptContext* scriptContext)
{
TypeId type = JavascriptOperators::GetTypeId(thisArg);
// We first need to make sure we are in the right context.
if (type == TypeIds_HostDispatch)
{
RecyclableObject* hostDispatchObject = VarTo<RecyclableObject>(thisArg);
const DynamicObject* remoteObject = hostDispatchObject->GetRemoteObject();
if (!remoteObject)
{
Var result = nullptr;
Js::Var values[1];
Js::CallInfo info(Js::CallFlags_Value, 1);
Js::Arguments args(info, values);
values[0] = thisArg;
if (hostDispatchObject->InvokeBuiltInOperationRemotely(EntryToString, args, &result))
{
return result;
}
}
}
// Dispatch to @@toStringTag implementation.
if (type >= TypeIds_TypedArrayMin && type <= TypeIds_TypedArrayMax && !scriptContext->GetThreadContext()->IsScriptActive())
{
// Use external call for typedarray in the debugger.
Var toStringValue = nullptr;
BEGIN_JS_RUNTIME_CALL_EX(scriptContext, false);
toStringValue = ToStringTagHelper(thisArg, scriptContext, type);
END_JS_RUNTIME_CALL(scriptContext);
return toStringValue;
}
// By this point, we should be in the correct context, but the thisArg may still need to be marshalled (for to the implicit ToObject conversion call.)
return ToStringTagHelper(CrossSite::MarshalVar(scriptContext, thisArg), scriptContext, type);
}
// -----------------------------------------------------------
// Object.prototype.valueOf
// 1. Let O be the result of calling ToObject passing the this value as the argument.
// 2. If O is the result of calling the Object constructor with a host object (15.2.2.1), then
// a. Return either O or another value such as the host object originally passed to the constructor. The specific result that is returned is implementation-defined.
// 3. Return O.
// -----------------------------------------------------------
Var JavascriptObject::EntryValueOf(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
AssertMsg(args.Info.Count > 0, "Should always have implicit 'this'");
// throw a TypeError if TypeId is null or undefined, and apply ToObject to the 'this' value otherwise.
if (JavascriptOperators::IsUndefinedOrNull(args[0]))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NullOrUndefined, _u("Object.prototype.valueOf"));
}
else
{
return JavascriptOperators::ToObject(args[0], scriptContext);
}
}
Var JavascriptObject::EntryGetOwnPropertyDescriptor(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
RecyclableObject* obj = nullptr;
if (args.Info.Count < 2)
{
obj = JavascriptOperators::ToObject(scriptContext->GetLibrary()->GetUndefined(), scriptContext);
}
else
{
// Convert the argument to object first
obj = JavascriptOperators::ToObject(args[1], scriptContext);
}
// If the object is HostDispatch try to invoke the operation remotely
if (obj->GetTypeId() == TypeIds_HostDispatch)
{
Var result;
if (obj->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyDescriptor, args, &result))
{
return result;
}
}
Var propertyKey = args.Info.Count > 2 ? args[2] : obj->GetLibrary()->GetUndefined();
return JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyKey, scriptContext);
}
Var JavascriptObject::GetOwnPropertyDescriptorHelper(RecyclableObject* obj, Var propertyKey, ScriptContext* scriptContext)
{
const PropertyRecord* propertyRecord;
JavascriptConversion::ToPropertyKey(propertyKey, scriptContext, &propertyRecord, nullptr);
PropertyId propertyId = propertyRecord->GetPropertyId();
PropertyDescriptor propertyDescriptor;
BOOL isPropertyDescriptorDefined;
isPropertyDescriptorDefined = JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propertyId, scriptContext, propertyDescriptor);
if (!isPropertyDescriptorDefined)
{
return scriptContext->GetLibrary()->GetUndefined();
}
return JavascriptOperators::FromPropertyDescriptor(propertyDescriptor, scriptContext);
}
BOOL JavascriptObject::GetOwnPropertyDescriptorHelper(RecyclableObject* obj, PropertyId propertyId, ScriptContext* scriptContext, PropertyDescriptor& propertyDescriptor)
{
BOOL isPropertyDescriptorDefined;
if (obj->IsExternal())
{
isPropertyDescriptorDefined = obj->HasOwnProperty(propertyId) ?
JavascriptOperators::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, &propertyDescriptor) :
FALSE;
}
else
{
isPropertyDescriptorDefined = JavascriptOperators::GetOwnPropertyDescriptor(obj, propertyId, scriptContext, &propertyDescriptor);
}
return isPropertyDescriptorDefined;
}
Var JavascriptObject::EntryGetOwnPropertyDescriptors(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
RecyclableObject* obj = nullptr;
if (args.Info.Count < 2)
{
obj = JavascriptOperators::ToObject(scriptContext->GetLibrary()->GetUndefined(), scriptContext);
}
else
{
// Convert the argument to object first
obj = JavascriptOperators::ToObject(args[1], scriptContext);
}
// If the object is HostDispatch try to invoke the operation remotely
if (obj->GetTypeId() == TypeIds_HostDispatch)
{
Var result;
if (obj->InvokeBuiltInOperationRemotely(EntryGetOwnPropertyDescriptors, args, &result))
{
return result;
}
}
JavascriptArray* ownPropertyKeys = JavascriptOperators::GetOwnPropertyKeys(obj, scriptContext);
RecyclableObject* resultObj = scriptContext->GetLibrary()->CreateObject(true, (Js::PropertyIndex) ownPropertyKeys->GetLength());
PropertyDescriptor propDesc;
Var propKey = nullptr;
for (uint i = 0; i < ownPropertyKeys->GetLength(); i++)
{
BOOL getPropResult = ownPropertyKeys->DirectGetItemAt(i, &propKey);
Assert(getPropResult);
if (!getPropResult)
{
continue;
}
PropertyRecord const * propertyRecord;
JavascriptConversion::ToPropertyKey(propKey, scriptContext, &propertyRecord, nullptr);
Var newDescriptor = JavascriptObject::GetOwnPropertyDescriptorHelper(obj, propKey, scriptContext);
if (!JavascriptOperators::IsUndefined(newDescriptor))
{
resultObj->SetProperty(propertyRecord->GetPropertyId(), newDescriptor, PropertyOperation_None, nullptr);
}
}
return resultObj;
}
Var JavascriptObject::EntryGetPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_getPrototypeOf);
// 19.1.2.9
// Object.getPrototypeOf ( O )
// When the getPrototypeOf function is called with argument O, the following steps are taken:
RecyclableObject *object = nullptr;
// 1. Let obj be ToObject(O).
// 2. ReturnIfAbrupt(obj).
if (args.Info.Count < 2 || !JavascriptConversion::ToObject(args[1], scriptContext, &object))
{
JavascriptError::ThrowTypeError(scriptContext, JSERR_FunctionArgument_NeedObject, _u("Object.getPrototypeOf"));
}
// 3. Return obj.[[GetPrototypeOf]]().
return CrossSite::MarshalVar(scriptContext, GetPrototypeOf(object, scriptContext));
}
Var JavascriptObject::EntrySetPrototypeOf(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
Assert(!(callInfo.Flags & CallFlags_New));
ScriptContext* scriptContext = function->GetScriptContext();
// 19.1.2.18
// Object.setPrototypeOf ( O, proto )
// When the setPrototypeOf function is called with arguments O and proto, the following steps are taken:
// 1. Let O be RequireObjectCoercible(O).
// 2. ReturnIfAbrupt(O).
// 3. If Type(proto) is neither Object or Null, then throw a TypeError exception.
int32 errCode = NOERROR;
if (args.Info.Count < 2 || !JavascriptConversion::CheckObjectCoercible(args[1], scriptContext))
{
errCode = JSERR_FunctionArgument_NeedObject;
}
else if (args.Info.Count < 3 || !JavascriptOperators::IsObjectOrNull(args[2]))
{
errCode = JSERR_FunctionArgument_NotObjectOrNull;
}
if (errCode != NOERROR)
{
JavascriptError::ThrowTypeError(scriptContext, errCode, _u("Object.setPrototypeOf"));
}
// 4. If Type(O) is not Object, return O.
if (!JavascriptOperators::IsObject(args[1]))
{
return args[1];
}
#if ENABLE_COPYONACCESS_ARRAY
JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(args[1]);
#endif
RecyclableObject* object = VarTo<RecyclableObject>(args[1]);
RecyclableObject* newPrototype = VarTo<RecyclableObject>(args[2]);
// 5. Let status be O.[[SetPrototypeOf]](proto).
// 6. ReturnIfAbrupt(status).
// 7. If status is false, throw a TypeError exception.
ChangePrototype(object, newPrototype, /*shouldThrow*/true, scriptContext);
// 8. Return O.
return object;
}
Var JavascriptObject::EntrySeal(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_seal);
// Spec update in Rev29 under section 19.1.2.17
if (args.Info.Count < 2)
{
return scriptContext->GetLibrary()->GetUndefined();
}
else if (!JavascriptOperators::IsObject(args[1]))
{
return args[1];
}
RecyclableObject *object = VarTo<RecyclableObject>(args[1]);
GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
if (globalObject != object && globalObject && (globalObject->ToThis() == object))
{
globalObject->Seal();
}
object->Seal();
return object;
}
Var JavascriptObject::EntryFreeze(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_freeze);
// Spec update in Rev29 under section 19.1.2.5
if (args.Info.Count < 2)
{
return scriptContext->GetLibrary()->GetUndefined();
}
else if (!JavascriptOperators::IsObject(args[1]))
{
return args[1];
}
RecyclableObject *object = VarTo<RecyclableObject>(args[1]);
GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();
if (globalObject != object && globalObject && (globalObject->ToThis() == object))
{
globalObject->Freeze();
}
object->Freeze();
return object;
}
Var JavascriptObject::EntryPreventExtensions(RecyclableObject* function, CallInfo callInfo, ...)
{
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
ARGUMENTS(args, callInfo);
ScriptContext* scriptContext = function->GetScriptContext();
Assert(!(callInfo.Flags & CallFlags_New));
CHAKRATEL_LANGSTATS_INC_BUILTINCOUNT(Object_Constructor_preventExtensions);
// Spec update in Rev29 under section 19.1.2.15
if (args.Info.Count < 2)
{
return scriptContext->GetLibrary()->GetUndefined();
}
else if (!JavascriptOperators::IsObject(args[1]))
{
return args[1];
}
RecyclableObject *object = VarTo<RecyclableObject>(args[1]);
GlobalObject* globalObject = object->GetLibrary()->GetGlobalObject();