-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathDiagObjectModel.cpp
More file actions
4213 lines (3566 loc) · 163 KB
/
DiagObjectModel.cpp
File metadata and controls
4213 lines (3566 loc) · 163 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 "RuntimeDebugPch.h"
#ifdef ENABLE_SCRIPT_DEBUGGING
// Parser includes
#include "CharClassifier.h"
// TODO: clean up the need of these regex related header here just for GroupInfo needed in JavascriptRegExpConstructor
#include "RegexCommon.h"
// Runtime includes
#include "Library/ObjectPrototypeObject.h"
#include "Library/JavascriptNumberObject.h"
#include "Library/Functions/BoundFunction.h"
#include "Library/Regex/JavascriptRegExpConstructor.h"
#include "Library/SameValueComparer.h"
#include "Library/MapOrSetDataList.h"
#include "Library/JavascriptPromise.h"
#include "Library/JavascriptProxy.h"
#include "Library/JavascriptMap.h"
#include "Library/JavascriptSet.h"
#include "Library/JavascriptWeakMap.h"
#include "Library/JavascriptWeakSet.h"
#include "Library/ArgumentsObject.h"
#include "Types/DynamicObjectPropertyEnumerator.h"
#include "Types/JavascriptStaticEnumerator.h"
#include "Library/ForInObjectEnumerator.h"
#include "Library/Array/ES5Array.h"
namespace Js
{
#define RETURN_VALUE_MAX_NAME 255
#define PENDING_MUTATION_VALUE_MAX_NAME 255
ArenaAllocator *GetArenaFromContext(ScriptContext *scriptContext)
{
Assert(scriptContext);
return scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena()->Arena();
}
template <class T>
WeakArenaReference<IDiagObjectModelWalkerBase>* CreateAWalker(ScriptContext * scriptContext, Var instance, Var originalInstance)
{
ReferencedArenaAdapter* pRefArena = scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena();
if (pRefArena)
{
IDiagObjectModelWalkerBase* pOMWalker = Anew(pRefArena->Arena(), T, scriptContext, instance, originalInstance);
return HeapNew(WeakArenaReference<IDiagObjectModelWalkerBase>,pRefArena, pOMWalker);
}
return nullptr;
}
//-----------------------
// ResolvedObject
WeakArenaReference<IDiagObjectModelDisplay>* ResolvedObject::GetObjectDisplay()
{
AssertMsg(typeId != TypeIds_HostDispatch, "Bad usage of ResolvedObject::GetObjectDisplay");
IDiagObjectModelDisplay* pOMDisplay = (this->objectDisplay != nullptr) ? this->objectDisplay : CreateDisplay();
Assert(pOMDisplay);
return HeapNew(WeakArenaReference<IDiagObjectModelDisplay>, scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena(), pOMDisplay);
}
IDiagObjectModelDisplay * ResolvedObject::CreateDisplay()
{
IDiagObjectModelDisplay* pOMDisplay = nullptr;
ReferencedArenaAdapter* pRefArena = scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena();
if (Js::VarIs<Js::TypedArrayBase>(obj))
{
pOMDisplay = Anew(pRefArena->Arena(), RecyclableTypedArrayDisplay, this);
}
else if (Js::VarIs<Js::ES5Array>(obj))
{
pOMDisplay = Anew(pRefArena->Arena(), RecyclableES5ArrayDisplay, this);
}
else if (Js::JavascriptArray::IsNonES5Array(obj))
{
// DisableJIT-TODO: Review- is this correct?
#if ENABLE_COPYONACCESS_ARRAY
// Make sure any NativeIntArrays are converted
Js::JavascriptLibrary::CheckAndConvertCopyOnAccessNativeIntArray<Var>(obj);
#endif
pOMDisplay = Anew(pRefArena->Arena(), RecyclableArrayDisplay, this);
}
else
{
pOMDisplay = Anew(pRefArena->Arena(), RecyclableObjectDisplay, this);
}
if (this->isConst || this->propId == Js::PropertyIds::_super || this->propId == Js::PropertyIds::_superConstructor)
{
pOMDisplay->SetDefaultTypeAttribute(DBGPROP_ATTRIB_VALUE_READONLY);
}
return pOMDisplay;
}
bool ResolvedObject::IsInDeadZone() const
{
Assert(scriptContext);
return this->obj == scriptContext->GetLibrary()->GetDebuggerDeadZoneBlockVariableString();
}
//-----------------------
// LocalsDisplay
LocalsDisplay::LocalsDisplay(DiagStackFrame* _frame)
: pFrame(_frame)
{
}
LPCWSTR LocalsDisplay::Name()
{
return _u("Locals");
}
LPCWSTR LocalsDisplay::Type()
{
return _u("");
}
LPCWSTR LocalsDisplay::Value(int radix)
{
return _u("Locals");
}
BOOL LocalsDisplay::HasChildren()
{
Js::JavascriptFunction* func = pFrame->GetJavascriptFunction();
FunctionBody* function = func->GetFunctionBody();
return function && function->GetLocalsCount() != 0;
}
DBGPROP_ATTRIB_FLAGS LocalsDisplay::GetTypeAttribute()
{
return DBGPROP_ATTRIB_NO_ATTRIB;
}
BOOL LocalsDisplay::Set(Var updateObject)
{
// This is the hidden root object for Locals it doesn't get updated.
return FALSE;
}
WeakArenaReference<IDiagObjectModelWalkerBase>* LocalsDisplay::CreateWalker()
{
ReferencedArenaAdapter* pRefArena = pFrame->GetScriptContext()->GetThreadContext()->GetDebugManager()->GetDiagnosticArena();
if (pRefArena)
{
IDiagObjectModelWalkerBase * pOMWalker = nullptr;
IGNORE_STACKWALK_EXCEPTION(scriptContext);
pOMWalker = Anew(pRefArena->Arena(), LocalsWalker, pFrame, FrameWalkerFlags::FW_MakeGroups);
return HeapNew(WeakArenaReference<IDiagObjectModelWalkerBase>,pRefArena, pOMWalker);
}
return nullptr;
}
// Variables on the scope or in current function.
/*static*/
BOOL VariableWalkerBase::GetExceptionObject(int &index, DiagStackFrame* frame, ResolvedObject* pResolvedObject)
{
Assert(pResolvedObject);
Assert(pResolvedObject->scriptContext);
Assert(frame);
Assert(index >= 0);
if (HasExceptionObject(frame))
{
if (index == 0)
{
pResolvedObject->name = _u("{exception}");
pResolvedObject->typeId = TypeIds_Error;
pResolvedObject->address = nullptr;
pResolvedObject->obj = pResolvedObject->scriptContext->GetDebugContext()->GetProbeContainer()->GetExceptionObject();
if (pResolvedObject->obj == nullptr)
{
Assert(false);
pResolvedObject->obj = pResolvedObject->scriptContext->GetLibrary()->GetUndefined();
}
return TRUE;
}
// Adjust the index
index -= 1;
}
return FALSE;
}
/*static*/
bool VariableWalkerBase::HasExceptionObject(DiagStackFrame* frame)
{
Assert(frame);
Assert(frame->GetScriptContext());
return frame->GetScriptContext()->GetDebugContext()->GetProbeContainer()->GetExceptionObject() != nullptr;
}
/*static*/
void VariableWalkerBase::GetReturnedValueResolvedObject(ReturnedValue * returnValue, DiagStackFrame* frame, ResolvedObject* pResolvedObject)
{
DBGPROP_ATTRIB_FLAGS defaultAttributes = DBGPROP_ATTRIB_VALUE_IS_RETURN_VALUE | DBGPROP_ATTRIB_VALUE_IS_FAKE;
WCHAR * finalName = AnewArray(GetArenaFromContext(pResolvedObject->scriptContext), WCHAR, RETURN_VALUE_MAX_NAME);
if (returnValue->isValueOfReturnStatement)
{
swprintf_s(finalName, RETURN_VALUE_MAX_NAME, _u("[Return value]"));
pResolvedObject->obj = frame->GetRegValue(Js::FunctionBody::ReturnValueRegSlot);
pResolvedObject->address = Anew(frame->GetArena(), LocalObjectAddressForRegSlot, frame, Js::FunctionBody::ReturnValueRegSlot, pResolvedObject->obj);
}
else
{
if (returnValue->calledFunction->IsScriptFunction())
{
swprintf_s(finalName, RETURN_VALUE_MAX_NAME, _u("[%s returned]"), returnValue->calledFunction->GetFunctionBody()->GetDisplayName());
}
else
{
ENTER_PINNED_SCOPE(JavascriptString, displayName);
displayName = returnValue->calledFunction->GetDisplayName();
const char16 *builtInName = ParseFunctionName(displayName->GetString(), displayName->GetLength(), pResolvedObject->scriptContext);
swprintf_s(finalName, RETURN_VALUE_MAX_NAME, _u("[%s returned]"), builtInName);
LEAVE_PINNED_SCOPE();
}
pResolvedObject->obj = returnValue->returnedValue;
defaultAttributes |= DBGPROP_ATTRIB_VALUE_READONLY;
pResolvedObject->address = nullptr;
}
Assert(pResolvedObject->obj != nullptr);
pResolvedObject->name = finalName;
pResolvedObject->typeId = TypeIds_Object;
pResolvedObject->objectDisplay = pResolvedObject->CreateDisplay();
pResolvedObject->objectDisplay->SetDefaultTypeAttribute(defaultAttributes);
}
// The debugger uses the functionNameId field instead of the "name" property to get the name of the funtion. The functionNameId field is overloaded and may contain the display name if
// toString() has been called on the function object. For built-in or external functions the display name can be something like "function Echo() { native code }". We will try to parse the
// function name out of the display name so the user will see just the function name e.g. "Echo" instead of the full display name in debugger.
const char16 * VariableWalkerBase::ParseFunctionName(const char16 * displayNameBuffer, const charcount_t displayNameBufferLength, ScriptContext* scriptContext)
{
const charcount_t funcStringLength = _countof(JS_DISPLAY_STRING_FUNCTION_HEADER) - 1; // discount the ending null character in string literal
const charcount_t templateStringLength = funcStringLength + _countof(JS_DISPLAY_STRING_FUNCTION_BODY) - 1; // discount the ending null character in string literal
// If the string doesn't meet our expected format; return the original string.
if (displayNameBufferLength <= templateStringLength || (wmemcmp(displayNameBuffer, JS_DISPLAY_STRING_FUNCTION_HEADER, funcStringLength) != 0))
{
return displayNameBuffer;
}
// Look for the left parenthesis, if we don't find one; return the original string.
const char16* parenChar = wcschr(displayNameBuffer, '(');
if (parenChar == nullptr)
{
return displayNameBuffer;
}
charcount_t actualFunctionNameLength = displayNameBufferLength - templateStringLength;
uint byteLengthForCopy = sizeof(char16) * actualFunctionNameLength;
char16 * actualFunctionNameBuffer = AnewArray(GetArenaFromContext(scriptContext), char16, actualFunctionNameLength + 1); // The last character will be the null character.
if (actualFunctionNameBuffer == nullptr)
{
return displayNameBuffer;
}
js_memcpy_s(actualFunctionNameBuffer, byteLengthForCopy, displayNameBuffer + funcStringLength, byteLengthForCopy);
actualFunctionNameBuffer[actualFunctionNameLength] = _u('\0');
return actualFunctionNameBuffer;
}
/*static*/
BOOL VariableWalkerBase::GetReturnedValue(int &index, DiagStackFrame* frame, ResolvedObject* pResolvedObject)
{
Assert(pResolvedObject);
Assert(pResolvedObject->scriptContext);
Assert(frame);
Assert(index >= 0);
ReturnedValueList *returnedValueList = frame->GetScriptContext()->GetDebugContext()->GetProbeContainer()->GetReturnedValueList();
if (returnedValueList != nullptr && returnedValueList->Count() > 0 && frame->IsTopFrame())
{
if (index < returnedValueList->Count())
{
ReturnedValue * returnValue = returnedValueList->Item(index);
VariableWalkerBase::GetReturnedValueResolvedObject(returnValue, frame, pResolvedObject);
return TRUE;
}
// Adjust the index
index -= returnedValueList->Count();
}
return FALSE;
}
/*static*/
int VariableWalkerBase::GetReturnedValueCount(DiagStackFrame* frame)
{
Assert(frame);
Assert(frame->GetScriptContext());
ReturnedValueList *returnedValueList = frame->GetScriptContext()->GetDebugContext()->GetProbeContainer()->GetReturnedValueList();
return returnedValueList != nullptr && frame->IsTopFrame() ? returnedValueList->Count() : 0;
}
#ifdef ENABLE_MUTATION_BREAKPOINT
BOOL VariableWalkerBase::GetBreakMutationBreakpointValue(int &index, DiagStackFrame* frame, ResolvedObject* pResolvedObject)
{
Assert(pResolvedObject);
Assert(pResolvedObject->scriptContext);
Assert(frame);
Assert(index >= 0);
Js::MutationBreakpoint *mutationBreakpoint = frame->GetScriptContext()->GetDebugContext()->GetProbeContainer()->GetDebugManager()->GetActiveMutationBreakpoint();
if (mutationBreakpoint != nullptr)
{
if (index == 0)
{
pResolvedObject->name = _u("[Pending Mutation]");
pResolvedObject->typeId = TypeIds_Object;
pResolvedObject->address = nullptr;
pResolvedObject->obj = mutationBreakpoint->GetMutationObjectVar();
ReferencedArenaAdapter* pRefArena = pResolvedObject->scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena();
pResolvedObject->objectDisplay = Anew(pRefArena->Arena(), PendingMutationBreakpointDisplay, pResolvedObject, mutationBreakpoint->GetBreakMutationType());
pResolvedObject->objectDisplay->SetDefaultTypeAttribute(DBGPROP_ATTRIB_VALUE_PENDING_MUTATION | DBGPROP_ATTRIB_VALUE_READONLY | DBGPROP_ATTRIB_VALUE_IS_FAKE);
return TRUE;
}
index -= 1; // Adjust the index
}
return FALSE;
}
uint VariableWalkerBase::GetBreakMutationBreakpointsCount(DiagStackFrame* frame)
{
Assert(frame);
Assert(frame->GetScriptContext());
return frame->GetScriptContext()->GetDebugContext()->GetProbeContainer()->GetDebugManager()->GetActiveMutationBreakpoint() != nullptr ? 1 : 0;
}
#endif
BOOL VariableWalkerBase::Get(int i, ResolvedObject* pResolvedObject)
{
AssertMsg(pResolvedObject, "Bad usage of VariableWalkerBase::Get");
Assert(pFrame);
pResolvedObject->scriptContext = pFrame->GetScriptContext();
if (i < 0)
{
return FALSE;
}
if (GetMemberCount() > i)
{
pResolvedObject->propId = pMembersList->Item(i)->propId;
Assert(pResolvedObject->propId != Js::Constants::NoProperty);
Assert(!Js::IsInternalPropertyId(pResolvedObject->propId));
if (pResolvedObject->propId == Js::PropertyIds::_super || pResolvedObject->propId == Js::PropertyIds::_superConstructor)
{
pResolvedObject->name = _u("super");
}
else
{
const Js::PropertyRecord* propertyRecord = pResolvedObject->scriptContext->GetPropertyName(pResolvedObject->propId);
pResolvedObject->name = propertyRecord->GetBuffer();
}
pResolvedObject->obj = GetVarObjectAt(i);
Assert(pResolvedObject->obj);
pResolvedObject->typeId = JavascriptOperators::GetTypeId(pResolvedObject->obj);
pResolvedObject->address = GetObjectAddress(i);
pResolvedObject->isConst = IsConstAt(i);
pResolvedObject->objectDisplay = nullptr;
return TRUE;
}
return FALSE;
}
Var VariableWalkerBase::GetVarObjectAt(int index)
{
Assert(index < pMembersList->Count());
return pMembersList->Item(index)->aVar;
}
bool VariableWalkerBase::IsConstAt(int index)
{
Assert(index < pMembersList->Count());
DebuggerPropertyDisplayInfo* displayInfo = pMembersList->Item(index);
// Dead zone variables are also displayed as read only.
return displayInfo->IsConst() || displayInfo->IsInDeadZone();
}
uint32 VariableWalkerBase::GetChildrenCount()
{
PopulateMembers();
return GetMemberCount();
}
BOOL VariableWalkerBase::GetGroupObject(ResolvedObject* pResolvedObject)
{
if (!IsInGroup()) return FALSE;
Assert(pResolvedObject);
// This is fake [Methods] object.
pResolvedObject->name = groupType == UIGroupType_Scope ? _u("[Scope]") : _u("[Globals]");
pResolvedObject->obj = Js::VarTo<Js::RecyclableObject>(instance);
pResolvedObject->typeId = TypeIds_Function;
pResolvedObject->address = nullptr; // Scope object should not be editable
ArenaAllocator *arena = GetArenaFromContext(pResolvedObject->scriptContext);
Assert(arena);
if (groupType == UIGroupType_Scope)
{
pResolvedObject->objectDisplay = Anew(arena, ScopeVariablesGroupDisplay, this, pResolvedObject);
}
else
{
pResolvedObject->objectDisplay = Anew(arena, GlobalsScopeVariablesGroupDisplay, this, pResolvedObject);
}
return TRUE;
}
IDiagObjectAddress *VariableWalkerBase::FindPropertyAddress(PropertyId propId, bool& isConst)
{
PopulateMembers();
if (pMembersList)
{
for (int i = 0; i < pMembersList->Count(); i++)
{
DebuggerPropertyDisplayInfo *pair = pMembersList->Item(i);
Assert(pair);
if (pair->propId == propId)
{
isConst = pair->IsConst();
return GetObjectAddress(i);
}
}
}
return nullptr;
}
// Determines if the given property is valid for display in the locals window.
// Cases in which the property is valid are:
// 1. It is not represented by an internal property.
// 2. It is a var property.
// 3. It is a let/const property in scope and is not in a dead zone (assuming isInDeadZone is nullptr).
// (Determines if the given property is currently in block scope and not in a dead zone.)
bool VariableWalkerBase::IsPropertyValid(PropertyId propertyId, RegSlot location, bool *isPropertyInDebuggerScope, bool* isConst, bool* isInDeadZone) const
{
Assert(isPropertyInDebuggerScope);
Assert(isConst);
*isPropertyInDebuggerScope = false;
// Default to writable (for the case of vars and internal properties).
*isConst = false;
if (!allowLexicalThis && (propertyId == Js::PropertyIds::_this || propertyId == Js::PropertyIds::_newTarget))
{
return false;
}
if (!allowSuperReference && (propertyId == Js::PropertyIds::_super || propertyId == Js::PropertyIds::_superConstructor))
{
return false;
}
if (Js::IsInternalPropertyId(propertyId))
{
return false;
}
Assert(pFrame);
Js::FunctionBody *pFBody = pFrame->GetJavascriptFunction()->GetFunctionBody();
if (pFBody && pFBody->GetScopeObjectChain())
{
int offset = GetAdjustedByteCodeOffset();
if (pFBody->GetScopeObjectChain()->TryGetDebuggerScopePropertyInfo(
propertyId,
location,
offset,
isPropertyInDebuggerScope,
isConst,
isInDeadZone))
{
return true;
}
}
// If the register was not found in any scopes, then it's a var and should be in scope.
return !*isPropertyInDebuggerScope;
}
int VariableWalkerBase::GetAdjustedByteCodeOffset() const
{
return LocalsWalker::GetAdjustedByteCodeOffset(pFrame);
}
DebuggerScope * VariableWalkerBase::GetScopeWhenHaltAtFormals()
{
if (IsWalkerForCurrentFrame())
{
return LocalsWalker::GetScopeWhenHaltAtFormals(pFrame);
}
return nullptr;
}
bool VariableWalkerBase::IsInParamScope(DebuggerScope* scope, DiagStackFrame* pFrame)
{
return scope != nullptr && scope->GetEnd() > LocalsWalker::GetAdjustedByteCodeOffset(pFrame);
}
// Allocates and returns a property display info.
DebuggerPropertyDisplayInfo* VariableWalkerBase::AllocateNewPropertyDisplayInfo(PropertyId propertyId, Var value, bool isConst, bool isInDeadZone)
{
Assert(pFrame);
Assert(value);
Assert(isInDeadZone || !pFrame->GetScriptContext()->IsUndeclBlockVar(value));
DWORD flags = DebuggerPropertyDisplayInfoFlags_None;
flags |= isConst ? DebuggerPropertyDisplayInfoFlags_Const : 0;
flags |= isInDeadZone ? DebuggerPropertyDisplayInfoFlags_InDeadZone : 0;
ArenaAllocator *arena = pFrame->GetArena();
if (isInDeadZone)
{
value = pFrame->GetScriptContext()->GetLibrary()->GetDebuggerDeadZoneBlockVariableString();
}
return Anew(arena, DebuggerPropertyDisplayInfo, propertyId, value, flags);
}
/// Slot array
void SlotArrayVariablesWalker::PopulateMembers()
{
if (pMembersList == nullptr && instance != nullptr)
{
ArenaAllocator *arena = pFrame->GetArena();
ScopeSlots slotArray = GetSlotArray();
if (!slotArray.IsDebuggerScopeSlotArray())
{
DebuggerScope *formalScope = GetScopeWhenHaltAtFormals();
bool isInParamScope = IsInParamScope(formalScope, pFrame);
Js::FunctionBody *pFBody = slotArray.GetFunctionInfo()->GetFunctionBody();
if (this->groupType & UIGroupType_Param)
{
Assert(formalScope != nullptr && pFBody->paramScopeSlotArraySize > 0);
uint slotArrayCount = pFBody->paramScopeSlotArraySize;
pMembersList = JsUtil::List<DebuggerPropertyDisplayInfo *, ArenaAllocator>::New(arena, slotArrayCount);
for (uint32 i = 0; i < slotArrayCount; i++)
{
Js::DebuggerScopeProperty scopeProperty = formalScope->scopeProperties->Item(i);
Var value = slotArray.Get(i);
bool isInDeadZone = pFrame->GetScriptContext()->IsUndeclBlockVar(value);
DebuggerPropertyDisplayInfo *pair = AllocateNewPropertyDisplayInfo(
scopeProperty.propId,
value,
false/*isConst*/,
isInDeadZone);
Assert(pair != nullptr);
pMembersList->Add(pair);
}
}
else if (pFBody->GetPropertyIdsForScopeSlotArray() != nullptr)
{
uint slotArrayCount = static_cast<uint>(slotArray.GetCount());
pMembersList = JsUtil::List<DebuggerPropertyDisplayInfo *, ArenaAllocator>::New(arena, slotArrayCount);
for (uint32 i = 0; i < slotArrayCount; i++)
{
Js::PropertyId propertyId = pFBody->GetPropertyIdsForScopeSlotArray()[i];
bool isConst = false;
bool isPropertyInDebuggerScope = false;
bool isInDeadZone = false;
if (propertyId != Js::Constants::NoProperty && IsPropertyValid(propertyId, i, &isPropertyInDebuggerScope, &isConst, &isInDeadZone))
{
if (!isInParamScope || formalScope->HasProperty(propertyId))
{
Var value = slotArray.Get(i);
if (pFrame->GetScriptContext()->IsUndeclBlockVar(value))
{
isInDeadZone = true;
}
DebuggerPropertyDisplayInfo *pair = AllocateNewPropertyDisplayInfo(
propertyId,
value,
isConst,
isInDeadZone);
Assert(pair != nullptr);
pMembersList->Add(pair);
}
}
}
}
}
else
{
DebuggerScope* debuggerScope = slotArray.GetDebuggerScope();
AssertMsg(debuggerScope, "Slot array debugger scope is missing but should be created.");
pMembersList = JsUtil::List<DebuggerPropertyDisplayInfo *, ArenaAllocator>::New(arena);
if (debuggerScope->HasProperties())
{
debuggerScope->scopeProperties->Map([&] (int i, Js::DebuggerScopeProperty& scopeProperty)
{
Var value = slotArray.Get(scopeProperty.location);
bool isConst = scopeProperty.IsConst();
bool isInDeadZone = false;
if (pFrame->GetScriptContext()->IsUndeclBlockVar(value))
{
isInDeadZone = true;
}
DebuggerPropertyDisplayInfo *pair = AllocateNewPropertyDisplayInfo(
scopeProperty.propId,
value,
isConst,
isInDeadZone);
Assert(pair != nullptr);
pMembersList->Add(pair);
});
}
}
}
}
IDiagObjectAddress * SlotArrayVariablesWalker::GetObjectAddress(int index)
{
Assert(index < pMembersList->Count());
ScopeSlots slotArray = GetSlotArray();
return Anew(pFrame->GetArena(), LocalObjectAddressForSlot, slotArray, index, pMembersList->Item(index)->aVar);
}
// Regslot
void RegSlotVariablesWalker::PopulateMembers()
{
if (pMembersList == nullptr)
{
Js::FunctionBody *pFBody = pFrame->GetJavascriptFunction()->GetFunctionBody();
ArenaAllocator *arena = pFrame->GetArena();
PropertyIdOnRegSlotsContainer *propIdContainer = pFBody->GetPropertyIdOnRegSlotsContainer();
DebuggerScope *formalScope = GetScopeWhenHaltAtFormals();
// this container can be nullptr if there is no locals in current function.
if (propIdContainer != nullptr)
{
RegSlot limit = propIdContainer->formalsUpperBound == Js::Constants::NoRegister ? Js::Constants::NoRegister : pFBody->MapRegSlot(propIdContainer->formalsUpperBound);
pMembersList = JsUtil::List<DebuggerPropertyDisplayInfo *, ArenaAllocator>::New(arena);
for (uint i = 0; i < propIdContainer->length; i++)
{
Js::PropertyId propertyId;
RegSlot reg;
propIdContainer->FetchItemAt(i, pFBody, &propertyId, ®);
bool shouldInsert = false;
bool isConst = false;
bool isInDeadZone = false;
if (this->debuggerScope)
{
DebuggerScopeProperty debuggerScopeProperty;
if (this->debuggerScope->TryGetValidProperty(propertyId, reg, GetAdjustedByteCodeOffset(), &debuggerScopeProperty, &isInDeadZone))
{
isConst = debuggerScopeProperty.IsConst();
shouldInsert = true;
}
}
else
{
bool isPropertyInDebuggerScope = false;
shouldInsert = IsPropertyValid(propertyId, reg, &isPropertyInDebuggerScope, &isConst, &isInDeadZone) && !isPropertyInDebuggerScope;
}
if (shouldInsert)
{
if (IsInParamScope(formalScope, pFrame))
{
if (limit != Js::Constants::NoRegister)
{
shouldInsert = reg <= limit;
}
else
{
shouldInsert = formalScope->HasProperty(propertyId);
}
}
else if (!pFBody->IsParamAndBodyScopeMerged() && formalScope->HasProperty(propertyId))
{
DebuggerScopeProperty prop;
prop.flags = DebuggerScopePropertyFlags_None;
prop.propId = 0;
formalScope->TryGetProperty(propertyId, reg, &prop);
if (prop.flags & DebuggerScopePropertyFlags_HasDuplicateInBody)
{
shouldInsert = false;
}
}
}
if (shouldInsert)
{
Var value = pFrame->GetRegValue(reg);
// If the user didn't supply an arguments object, a fake one will
// be created when evaluating LocalsWalker::ShouldInsertFakeArguments().
if (!(propertyId == PropertyIds::arguments && value == nullptr))
{
if (pFrame->GetScriptContext()->IsUndeclBlockVar(value))
{
isInDeadZone = true;
}
DebuggerPropertyDisplayInfo *info = AllocateNewPropertyDisplayInfo(
propertyId,
(Var)reg,
isConst,
isInDeadZone);
Assert(info != nullptr);
pMembersList->Add(info);
}
}
}
}
}
}
Var RegSlotVariablesWalker::GetVarObjectAndRegAt(int index, RegSlot* reg /*= nullptr*/)
{
Assert(index < pMembersList->Count());
Var returnedVar = nullptr;
RegSlot returnedReg = Js::Constants::NoRegister;
DebuggerPropertyDisplayInfo* displayInfo = pMembersList->Item(index);
if (displayInfo->IsInDeadZone())
{
// The uninitialized string is already set in the var for the dead zone display.
Assert(VarIs<JavascriptString>(displayInfo->aVar));
returnedVar = displayInfo->aVar;
}
else
{
returnedReg = ::Math::PointerCastToIntegral<RegSlot>(displayInfo->aVar);
returnedVar = pFrame->GetRegValue(returnedReg);
}
if (reg != nullptr)
{
*reg = returnedReg;
}
AssertMsg(returnedVar, "Var should be replaced with the dead zone string object.");
return returnedVar;
}
Var RegSlotVariablesWalker::GetVarObjectAt(int index)
{
return GetVarObjectAndRegAt(index);
}
IDiagObjectAddress * RegSlotVariablesWalker::GetObjectAddress(int index)
{
RegSlot reg = Js::Constants::NoRegister;
Var obj = GetVarObjectAndRegAt(index, ®);
return Anew(pFrame->GetArena(), LocalObjectAddressForRegSlot, pFrame, reg, obj);
}
// For an activation object.
void ObjectVariablesWalker::PopulateMembers()
{
if (pMembersList == nullptr && instance != nullptr)
{
ScriptContext * scriptContext = pFrame->GetScriptContext();
ArenaAllocator *arena = GetArenaFromContext(scriptContext);
Assert(Js::VarIs<Js::RecyclableObject>(instance));
Js::RecyclableObject* object = Js::VarTo<Js::RecyclableObject>(instance);
Assert(JavascriptOperators::IsObject(object));
int count = object->GetPropertyCount();
pMembersList = JsUtil::List<DebuggerPropertyDisplayInfo *, ArenaAllocator>::New(arena, count);
AddObjectProperties(count, object);
}
}
void ObjectVariablesWalker::AddObjectProperties(int count, Js::RecyclableObject* object)
{
ScriptContext * scriptContext = pFrame->GetScriptContext();
DebuggerScope *formalScope = LocalsWalker::GetScopeWhenHaltAtFormals(pFrame);
// For the scopes and locals only enumerable properties will be shown.
for (int i = 0; i < count; i++)
{
Js::PropertyId propertyId = object->GetPropertyId((PropertyIndex)i);
bool isConst = false;
bool isPropertyInDebuggerScope = false;
bool isInDeadZone = false;
if (propertyId != Js::Constants::NoProperty
&& IsPropertyValid(propertyId, Js::Constants::NoRegister, &isPropertyInDebuggerScope, &isConst, &isInDeadZone)
&& object->IsEnumerable(propertyId))
{
Var itemObj = RecyclableObjectWalker::GetObject(object, object, propertyId, scriptContext);
if (itemObj == nullptr)
{
itemObj = scriptContext->GetLibrary()->GetUndefined();
}
if (IsInParamScope(formalScope, pFrame) && pFrame->GetScriptContext()->IsUndeclBlockVar(itemObj))
{
itemObj = scriptContext->GetLibrary()->GetUndefined();
}
AssertMsg(!VarIs<RootObjectBase>(object) || !isConst, "root object shouldn't produce const properties through IsPropertyValid");
DebuggerPropertyDisplayInfo *info = AllocateNewPropertyDisplayInfo(
propertyId,
itemObj,
isConst,
isInDeadZone);
Assert(info);
pMembersList->Add(info);
}
}
}
IDiagObjectAddress * ObjectVariablesWalker::GetObjectAddress(int index)
{
Assert(index < pMembersList->Count());
DebuggerPropertyDisplayInfo* info = pMembersList->Item(index);
return Anew(pFrame->GetArena(), RecyclableObjectAddress, instance, info->propId, info->aVar, info->IsInDeadZone() ? TRUE : FALSE);
}
// For root access on the Global object (adds let/const variables before properties)
void RootObjectVariablesWalker::PopulateMembers()
{
if (pMembersList == nullptr && instance != nullptr)
{
ScriptContext * scriptContext = pFrame->GetScriptContext();
ArenaAllocator *arena = GetArenaFromContext(scriptContext);
Assert(Js::VarIs<Js::RootObjectBase>(instance));
Js::RootObjectBase* object = Js::VarTo<Js::RootObjectBase>(instance);
int count = object->GetPropertyCount();
pMembersList = JsUtil::List<DebuggerPropertyDisplayInfo *, ArenaAllocator>::New(arena, count);
// Add let/const globals first so that they take precedence over the global properties. Then
// VariableWalkerBase::FindPropertyAddress will correctly find let/const globals that shadow
// global properties of the same name.
object->MapLetConstGlobals([&](const PropertyRecord* propertyRecord, Var value, bool isConst) {
if (!scriptContext->IsUndeclBlockVar(value))
{
// Let/const are always enumerable and valid
DebuggerPropertyDisplayInfo *info = AllocateNewPropertyDisplayInfo(propertyRecord->GetPropertyId(), value, isConst, false /*isInDeadZone*/);
pMembersList->Add(info);
}
});
AddObjectProperties(count, object);
}
}
// DiagScopeVariablesWalker
DiagScopeVariablesWalker::DiagScopeVariablesWalker(DiagStackFrame* _pFrame, Var _instance, IDiagObjectModelWalkerBase* innerWalker)
: VariableWalkerBase(_pFrame, _instance, UIGroupType_InnerScope, /* allowLexicalThis */ false),
pDiagScopeObjects(nullptr),
diagScopeVarCount(0),
scopeIsInitialized(false), // false until end of method
enumWithScopeAlso(false)
{
ScriptContext * scriptContext = _pFrame->GetScriptContext();
ArenaAllocator *arena = GetArenaFromContext(scriptContext);
pDiagScopeObjects = JsUtil::List<IDiagObjectModelWalkerBase *, ArenaAllocator>::New(arena);
pDiagScopeObjects->Add(innerWalker);
diagScopeVarCount = innerWalker->GetChildrenCount();
scopeIsInitialized = true;
}
uint32 DiagScopeVariablesWalker::GetChildrenCount()
{
if (scopeIsInitialized)
{
return diagScopeVarCount;
}
Assert(pFrame);
Js::FunctionBody *pFBody = pFrame->GetJavascriptFunction()->GetFunctionBody();
if (pFBody->GetScopeObjectChain())
{
int bytecodeOffset = GetAdjustedByteCodeOffset();
ScriptContext * scriptContext = pFrame->GetScriptContext();
ArenaAllocator *arena = GetArenaFromContext(scriptContext);
pDiagScopeObjects = JsUtil::List<IDiagObjectModelWalkerBase *, ArenaAllocator>::New(arena);
// Look for catch/with/block scopes which encompass current offset (skip block scopes as
// they are only used for lookup within the RegSlotVariablesWalker).
// Go the reverse way so that we find the innermost scope first;
Js::ScopeObjectChain * pScopeObjectChain = pFBody->GetScopeObjectChain();
for (int i = pScopeObjectChain->pScopeChain->Count() - 1 ; i >= 0; i--)
{
Js::DebuggerScope *debuggerScope = pScopeObjectChain->pScopeChain->Item(i);
bool isScopeInRange = debuggerScope->IsOffsetInScope(bytecodeOffset);
if (isScopeInRange
&& !debuggerScope->IsParamScope()
&& (debuggerScope->IsOwnScope() || (debuggerScope->scopeType == DiagBlockScopeDirect && debuggerScope->HasProperties())))
{
switch (debuggerScope->scopeType)
{
case DiagWithScope:
{
if (enumWithScopeAlso)
{
RecyclableObjectWalker* recylableObjectWalker = Anew(arena, RecyclableObjectWalker, scriptContext,
(Var)pFrame->GetRegValue(debuggerScope->GetLocation(), true));
pDiagScopeObjects->Add(recylableObjectWalker);
diagScopeVarCount += recylableObjectWalker->GetChildrenCount();
}
}
break;
case DiagCatchScopeDirect:
case DiagCatchScopeInObject:
{
CatchScopeWalker* catchScopeWalker = Anew(arena, CatchScopeWalker, pFrame, debuggerScope);
pDiagScopeObjects->Add(catchScopeWalker);
diagScopeVarCount += catchScopeWalker->GetChildrenCount();
}
break;
case DiagCatchScopeInSlot:
case DiagBlockScopeInSlot:
{
SlotArrayVariablesWalker* blockScopeWalker = Anew(arena, SlotArrayVariablesWalker, pFrame,
(Var)pFrame->GetInnerScopeFromRegSlot(debuggerScope->GetLocation()), UIGroupType_InnerScope, /* allowLexicalThis */ false);
pDiagScopeObjects->Add(blockScopeWalker);
diagScopeVarCount += blockScopeWalker->GetChildrenCount();
}
break;
case DiagBlockScopeDirect:
{
RegSlotVariablesWalker *pObjWalker = Anew(arena, RegSlotVariablesWalker, pFrame, debuggerScope, UIGroupType_InnerScope);
pDiagScopeObjects->Add(pObjWalker);
diagScopeVarCount += pObjWalker->GetChildrenCount();
}
break;