aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/QML/Debugging/AD7/QmlDebugAD7Property.cs
blob: 9859c19620156ca095062571e3332d0ef538b0d3 (plain)
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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Debugger.Interop;

namespace QtVsTools.Qml.Debug.AD7
{
    using V4;

    sealed partial class Property : Concurrent,

        IDebugProperty2 // "This interface represents a stack frame property, a program document
                        //  property, or some other property. The property is usually the result of
                        //  an expression evaluation."
    {
        private QmlDebugger Debugger { get; set; }

        private QmlEngine Engine { get; set; }
        private Program Program { get; set; }
        private StackFrame StackFrame { get; set; }
        private CodeContext CodeContext { get; set; }

        private Property Parent { get; set; }
        private SortedDictionary<string, Property> Children { get; set; }

        private int FrameNumber { get; set; }
        private int ScopeNumber { get; set; }
        private JsValue JsValue { get; set; }
        private string Name { get; set; }
        private string FullName { get; set; }
        private string Type { get; set; }
        private string Value { get; set; }

        public static Property Create(
            StackFrame frame,
            int scopeNumber,
            JsValue value,
            Property parent = null)
        {
            var _this = new Property();
            return _this.Initialize(frame, scopeNumber, value, parent) ? _this : null;
        }

        private Property()
        { }

        private bool Initialize(
            StackFrame frame,
            int scopeNumber,
            JsValue value,
            Property parent)
        {
            StackFrame = frame;
            Engine = frame.Engine;
            Program = frame.Program;
            Debugger = frame.Debugger;
            CodeContext = frame.Context;
            FrameNumber = frame.FrameNumber;
            ScopeNumber = scopeNumber;
            Parent = parent;
            JsValue = value;

            if (Parent != null && Parent.JsValue is JsObject && ((JsObject)Parent.JsValue).IsArray)
                Name = string.Format("[{0}]", JsValue.Name);
            else
                Name = JsValue.Name;

            var nameParts = new Stack<string>(new[] { Name });
            for (var p = Parent; p != null && !string.IsNullOrEmpty(p.Name); p = p.Parent) {
                if (!nameParts.Peek().StartsWith("["))
                    nameParts.Push(".");
                nameParts.Push(p.Name);
            }
            FullName = string.Join("", nameParts);

            Type = JsValue.Type.ToString();
            Value = JsValue.ToString();

            Children = new SortedDictionary<string, Property>();
            if (JsValue is JsObject) {
                var obj = JsValue as JsObject;
                foreach (JsValue objProp in obj.Properties.Where(x => x.HasData)) {
                    Children[GetChildKey(objProp.Name)]
                        = Create(StackFrame, ScopeNumber, objProp, this);
                }
            }

            return true;
        }

        static string GetChildKey(string childName)
        {
            int childIndex;
            if (int.TryParse(childName, out childIndex))
                return string.Format("{0:D9}", childIndex);
            else
                return childName;
        }

        int IDebugProperty2.SetValueAsString(string pszValue, uint dwRadix, uint dwTimeout)
        {
            string expr = string.Format("{0}=({1})", FullName, pszValue);

            var value = Debugger.Evaluate(FrameNumber, expr);
            if (value == null || value is JsError)
                return VSConstants.S_FALSE;

            Program.Refresh();
            return VSConstants.S_OK;
        }

        int IDebugProperty2.EnumChildren(
            enum_DEBUGPROP_INFO_FLAGS dwFields,
            uint dwRadix,
            ref Guid guidFilter,
            enum_DBG_ATTRIB_FLAGS dwAttribFilter,
            string pszNameFilter,
            uint dwTimeout,
            out IEnumDebugPropertyInfo2 ppEnum)
        {
            ppEnum = null;
            if (guidFilter != Guid.Empty && !Filter.LocalsSelected(ref guidFilter))
                return VSConstants.S_OK;

            if (JsValue is JsObjectRef) {
                var obj = Debugger.Lookup(FrameNumber, ScopeNumber, JsValue as JsObjectRef);
                if (obj == null)
                    return VSConstants.S_OK;

                JsValue = obj;
                foreach (JsValue objProp in obj.Properties.Where(x => x.HasData)) {
                    Children[GetChildKey(objProp.Name)]
                        = Create(StackFrame, ScopeNumber, objProp, this);
                }
            }

            if (!Children.Any())
                return VSConstants.S_OK;

            ppEnum = PropertyEnum.Create(Children.Select(x =>
            {
                var info = new DEBUG_PROPERTY_INFO[1];
                (x.Value as IDebugProperty2).GetPropertyInfo(dwFields, dwRadix, 0,
                    new IDebugReference2[0], 0, info);
                return info[0];
            }));
            return VSConstants.S_OK;
        }


        #region //////////////////// Info /////////////////////////////////////////////////////////

        class PropertyInfo : InfoHelper<PropertyInfo>
        {
            public string FullName { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
            public string Value { get; set; }
            public enum_DBG_ATTRIB_FLAGS? Attribs { get; set; }
            public IDebugProperty2 Property { get; set; }
        }

        PropertyInfo Info
        {
            get
            {
                return new PropertyInfo
                {
                    Name = Name,
                    FullName = FullName,
                    Type = Type,
                    Value = Value,
                    Property = this,
                    Attribs = ((Children.Any() || JsValue.Type == JsValue.DataType.Object)
                        ? enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_OBJ_IS_EXPANDABLE : 0),
                };
            }
        }

        static readonly PropertyInfo.Mapping MappingToDEBUG_PROPERTY_INFO =

        #region //////////////////// DEBUG_PROPERTY_INFO <-- PropertyInfo /////////////////////////
            // r: Ref<DEBUG_PROPERTY_INFO>
            // f: enum_DEBUGPROP_INFO_FLAGS
            // i: PropertyInfo
            // v: value of i.<<property>>

            new PropertyInfo.Mapping<DEBUG_PROPERTY_INFO, enum_DEBUGPROP_INFO_FLAGS>
                ((r, bit) => r.s.dwFields |= bit)
            {
                { enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME,
                    (r, v) => r.s.bstrFullName = v, i => i.FullName },

                { enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME,
                    (r, v) => r.s.bstrName = v, i => i.Name },

                { enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE,
                    (r, v) => r.s.bstrType = v, i => i.Type },

                { enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE,
                    (r, v) => r.s.bstrValue = v, i => i.Value },

                { enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB,
                    (r, v) => r.s.dwAttrib |= v, i => i.Attribs },

                { enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_PROP,
                    (r, v) => r.s.pProperty = v, i => i.Property },
            };

        #endregion //////////////////// DEBUG_PROPERTY_INFO <-- PropertyInfo //////////////////////


        public DEBUG_PROPERTY_INFO GetInfo(enum_DEBUGPROP_INFO_FLAGS dwFields)
        {
            DEBUG_PROPERTY_INFO info;
            Info.Map(MappingToDEBUG_PROPERTY_INFO, dwFields, out info);
            return info;
        }

        int IDebugProperty2.GetPropertyInfo(
            enum_DEBUGPROP_INFO_FLAGS dwFields,
            uint dwRadix,
            uint dwTimeout,
            IDebugReference2[] rgpArgs,
            uint dwArgCount,
            DEBUG_PROPERTY_INFO[] pPropertyInfo)
        {
            Info.Map(MappingToDEBUG_PROPERTY_INFO, dwFields, out pPropertyInfo[0]);
            return VSConstants.S_OK;
        }

        int IDebugProperty2.GetParent(out IDebugProperty2 ppParent)
        {
            ppParent = Parent;
            return (Parent != null) ? VSConstants.S_OK : VSConstants.S_FALSE;
        }

        #endregion //////////////////// Info //////////////////////////////////////////////////////


        #region //////////////////// Filter ///////////////////////////////////////////////////////

        public static class Filter
        {
            public static readonly Guid Registers
                = new Guid("223ae797-bd09-4f28-8241-2763bdc5f713");

            private static readonly Guid Locals
                = new Guid("b200f725-e725-4c53-b36a-1ec27aef12ef");

            private static readonly Guid AllLocals
                = new Guid("196db21f-5f22-45a9-b5a3-32cddb30db06");

            public static readonly Guid Args
                = new Guid("804bccea-0475-4ae7-8a46-1862688ab863");

            private static readonly Guid LocalsPlusArgs
                = new Guid("e74721bb-10c0-40f5-807f-920d37f95419");

            private static readonly Guid AllLocalsPlusArgs
                = new Guid("939729a8-4cb0-4647-9831-7ff465240d5f");

            public static bool LocalsSelected(ref Guid guidFilter)
            {
                return guidFilter == Locals
                    || guidFilter == AllLocals
                    || guidFilter == LocalsPlusArgs
                    || guidFilter == AllLocalsPlusArgs;
            }
        }

        #endregion //////////////////// Filter ////////////////////////////////////////////////////
    }
}