forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsScriptEngine.Debug.cs
More file actions
276 lines (219 loc) · 8.94 KB
/
WindowsScriptEngine.Debug.cs
File metadata and controls
276 lines (219 loc) · 8.94 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript.Windows
{
public abstract partial class WindowsScriptEngine
{
#region Nested type: DebugDocument
private class DebugDocument : IDebugDocumentInfo, IDebugDocumentProvider, IDebugDocument, IDebugDocumentText
{
private readonly WindowsScriptEngine engine;
private readonly UIntPtr sourceContext;
private readonly DocumentInfo documentInfo;
private readonly string code;
private string[] codeLines;
private IDebugApplicationNode node;
public DebugDocument(WindowsScriptEngine engine, UIntPtr sourceContext, DocumentInfo documentInfo, string code)
{
this.engine = engine;
this.sourceContext = sourceContext;
this.documentInfo = documentInfo;
this.code = code;
Initialize();
}
private void Initialize()
{
codeLines = code.Split('\n').Select(line => line + '\n').ToArray();
engine.debugApplication.CreateApplicationNode(out node);
node.SetDocumentProvider(this);
IDebugApplicationNode rootNode;
engine.debugApplication.GetRootNode(out rootNode);
node.Attach(rootNode);
}
public string Code
{
get { return code; }
}
public void Close()
{
node.Detach();
node.Close();
node = null;
}
private string GetFileName()
{
return Path.HasExtension(documentInfo.Name) ? documentInfo.Name : Path.ChangeExtension(documentInfo.Name, engine.FileNameExtension);
}
private string GetUrl()
{
if (documentInfo.Uri != null)
{
return documentInfo.Uri.IsAbsoluteUri ? documentInfo.Uri.AbsoluteUri : documentInfo.Uri.ToString();
}
return MiscHelpers.FormatInvariant("{0}/{1}", engine.Name, GetFileName());
}
private bool TryGetSourceMapUrl(out string sourceMapUrl)
{
if (documentInfo.SourceMapUri != null)
{
sourceMapUrl = documentInfo.SourceMapUri.IsAbsoluteUri ? documentInfo.SourceMapUri.AbsoluteUri : documentInfo.SourceMapUri.ToString();
return true;
}
sourceMapUrl = null;
return false;
}
#region IDebugDocumentInfo implementation
public uint GetName(DocumentNameType type, out string documentName)
{
switch (type)
{
case DocumentNameType.AppNode:
case DocumentNameType.Title:
documentName = documentInfo.Name;
return RawCOMHelpers.HResult.S_OK;
case DocumentNameType.FileTail:
documentName = GetFileName();
return RawCOMHelpers.HResult.S_OK;
case DocumentNameType.URL:
documentName = GetUrl();
return RawCOMHelpers.HResult.S_OK;
case DocumentNameType.UniqueTitle:
documentName = documentInfo.UniqueName;
return RawCOMHelpers.HResult.S_OK;
case DocumentNameType.SourceMapURL:
return TryGetSourceMapUrl(out documentName) ? RawCOMHelpers.HResult.S_OK : RawCOMHelpers.HResult.E_FAIL.ToUnsigned();
}
documentName = null;
return RawCOMHelpers.HResult.E_FAIL.ToUnsigned();
}
public void GetDocumentClassId(out Guid clsid)
{
clsid = Guid.Empty;
}
#endregion
#region IDebugDocumentProvider implementation
public void GetDocument(out IDebugDocument document)
{
document = this;
}
#endregion
#region IDebugDocument implementation
#endregion
#region IDebugDocumentText implementation
public void GetDocumentAttributes(out TextDocAttrs attrs)
{
attrs = TextDocAttrs.ReadOnly;
}
public void GetSize(out uint numLines, out uint length)
{
numLines = (uint)codeLines.Length;
length = (uint)code.Length;
}
public void GetPositionOfLine(uint lineNumber, out uint position)
{
if (lineNumber >= codeLines.Length)
{
throw new ArgumentOutOfRangeException("lineNumber");
}
position = 0;
for (var index = 0; index < lineNumber; index++)
{
position += (uint)codeLines[index].Length;
}
}
public void GetLineOfPosition(uint position, out uint lineNumber, out uint offsetInLine)
{
if (position >= code.Length)
{
throw new ArgumentOutOfRangeException("position");
}
offsetInLine = position;
for (lineNumber = 0; lineNumber < codeLines.Length; lineNumber++)
{
var lineLength = (uint)codeLines[lineNumber].Length;
if (offsetInLine < lineLength)
{
break;
}
offsetInLine -= lineLength;
}
}
public void GetText(uint position, IntPtr pChars, IntPtr pAttrs, ref uint length, uint maxChars)
{
var codeLength = (uint)code.Length;
if (position < codeLength)
{
length = Math.Min(codeLength - position, maxChars);
if (pChars != IntPtr.Zero)
{
Marshal.Copy(code.ToCharArray(), (int)position, pChars, (int)length);
}
if (pAttrs != IntPtr.Zero)
{
var attrs = Enumerable.Repeat((short)SourceTextAttrs.None, (int)length).ToArray();
Marshal.Copy(attrs, 0, pAttrs, (int)length);
}
}
}
public void GetPositionOfContext(IDebugDocumentContext context, out uint position, out uint length)
{
var documentContext = (DebugDocumentContext)context;
position = documentContext.Position;
length = documentContext.Length;
}
public void GetContextOfPosition(uint position, uint length, out IDebugDocumentContext context)
{
IEnumDebugCodeContexts enumCodeContexts;
engine.activeScript.EnumCodeContextsOfPosition(sourceContext, position, length, out enumCodeContexts);
context = new DebugDocumentContext(this, position, length, enumCodeContexts);
}
#endregion
}
#endregion
#region Nested type: DebugDocumentMap
private class DebugDocumentMap : Dictionary<UIntPtr, DebugDocument>
{
}
#endregion
#region Nested type: DebugDocumentContext
private class DebugDocumentContext : IDebugDocumentContext
{
private readonly DebugDocument document;
private readonly uint position;
private readonly uint length;
private readonly IEnumDebugCodeContexts enumCodeContexts;
public DebugDocumentContext(DebugDocument document, uint position, uint length, IEnumDebugCodeContexts enumCodeContexts)
{
this.document = document;
this.position = position;
this.length = length;
this.enumCodeContexts = enumCodeContexts;
}
public uint Position
{
get { return position; }
}
public uint Length
{
get { return length; }
}
#region IDebugDocumentContext implementation
public void GetDocument(out IDebugDocument debugDocument)
{
debugDocument = document;
}
public void EnumCodeContexts(out IEnumDebugCodeContexts enumContexts)
{
enumCodeContexts.Clone(out enumContexts);
}
#endregion
}
#endregion
}
}