forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsScriptEngine.Site.cs
More file actions
357 lines (293 loc) · 13.9 KB
/
WindowsScriptEngine.Site.cs
File metadata and controls
357 lines (293 loc) · 13.9 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.ClearScript.Util;
using EXCEPINFO = System.Runtime.InteropServices.ComTypes.EXCEPINFO;
namespace Microsoft.ClearScript.Windows
{
public abstract partial class WindowsScriptEngine
{
#region Nested type: ScriptSite
private class ScriptSite : IActiveScriptSite, IActiveScriptSiteInterruptPoll, IActiveScriptSiteWindow, IActiveScriptSiteDebug32, IActiveScriptSiteDebug64, IActiveScriptSiteDebugEx, ICustomQueryInterface
{
private readonly WindowsScriptEngine engine;
public ScriptSite(WindowsScriptEngine engine)
{
this.engine = engine;
}
private string GetDetails(object error, string message)
{
if (engine.processDebugManager != null)
{
try
{
var syntaxError = false;
var scriptError = error as IActiveScriptError;
if (scriptError != null)
{
EXCEPINFO excepInfo;
scriptError.GetExceptionInfo(out excepInfo);
if (RawCOMHelpers.HResult.GetFacility(excepInfo.scode) == RawCOMHelpers.HResult.FACILITY_CONTROL)
{
syntaxError = engine.SyntaxErrorMap.ContainsKey(RawCOMHelpers.HResult.GetCode(excepInfo.scode));
}
}
if (syntaxError)
{
var details = message;
var errorLocation = GetErrorLocation(error);
if (!string.IsNullOrWhiteSpace(errorLocation))
{
details += "\n" + errorLocation;
}
var stackTrace = engine.GetStackTraceInternal();
if (!string.IsNullOrWhiteSpace(stackTrace))
{
details += "\n" + stackTrace;
}
return details;
}
else
{
var stackTrace = engine.GetStackTraceInternal();
if (!string.IsNullOrWhiteSpace(stackTrace))
{
return message + "\n" + stackTrace;
}
var errorLocation = GetErrorLocation(error);
if (!string.IsNullOrWhiteSpace(errorLocation))
{
return message + "\n" + errorLocation;
}
}
}
catch (Exception exception)
{
Debug.Assert(false, "Exception caught during error processing", exception.ToString());
}
}
return message;
}
private string GetErrorLocation(object error)
{
var scriptError = error as IActiveScriptError;
if (scriptError != null)
{
uint sourceContext;
uint lineNumber;
int offsetInLine;
scriptError.GetSourcePosition(out sourceContext, out lineNumber, out offsetInLine);
DebugDocument document;
if (engine.debugDocumentMap.TryGetValue(new UIntPtr(sourceContext), out document))
{
string documentName;
document.GetName(DocumentNameType.Title, out documentName);
int position;
if (lineNumber > 0)
{
uint linePosition;
document.GetPositionOfLine(lineNumber, out linePosition);
position = (int)linePosition + offsetInLine;
}
else
{
position = offsetInLine;
}
var text = new string(document.Code.Skip(position).TakeWhile(ch => ch != '\n').ToArray());
return MiscHelpers.FormatInvariant(" at ({0}:{1}:{2}) -> {3}", documentName, lineNumber, offsetInLine, text);
}
}
return null;
}
#region IActiveScriptSite implementation
public void GetLCID(out uint lcid)
{
lcid = (uint)CultureInfo.CurrentCulture.LCID;
}
public void GetItemInfo(string name, ScriptInfoFlags mask, ref IntPtr pUnkItem, ref IntPtr pTypeInfo)
{
var item = engine.hostItemMap[name];
if (mask.HasFlag(ScriptInfoFlags.IUnknown))
{
pUnkItem = Marshal.GetIDispatchForObject(item);
}
if (mask.HasFlag(ScriptInfoFlags.ITypeInfo))
{
pTypeInfo = Marshal.GetITypeInfoForType(item.GetType());
}
}
public void GetDocVersionString(out string version)
{
throw new NotImplementedException();
}
public void OnScriptTerminate(IntPtr pVarResult, ref EXCEPINFO excepInfo)
{
}
public void OnStateChange(ScriptState state)
{
}
public void OnScriptError(IActiveScriptError error)
{
if ((engine.CurrentScriptFrame != null) && (error != null))
{
EXCEPINFO excepInfo;
error.GetExceptionInfo(out excepInfo);
if (excepInfo.scode == RawCOMHelpers.HResult.E_ABORT)
{
// Script execution was interrupted explicitly. At this point the script
// engine might be in an odd state; the following call seems to get it back
// to normal.
engine.activeScript.SetScriptState(ScriptState.Started);
var description = excepInfo.bstrDescription ?? "Script execution interrupted by host";
engine.CurrentScriptFrame.ScriptError = new ScriptInterruptedException(engine.Name, description, GetDetails(error, description), excepInfo.scode, false, true, null);
}
else
{
var description = excepInfo.bstrDescription;
Exception innerException;
if (excepInfo.scode != RawCOMHelpers.HResult.CLEARSCRIPT_E_HOSTEXCEPTION)
{
innerException = null;
}
else
{
innerException = engine.CurrentScriptFrame.HostException;
if ((innerException != null) && string.IsNullOrWhiteSpace(description))
{
description = innerException.Message;
}
}
engine.CurrentScriptFrame.ScriptError = new ScriptEngineException(engine.Name, description, GetDetails(error, description), excepInfo.scode, false, true, innerException);
}
}
}
public void OnEnterScript()
{
Debug.Assert(engine.CheckAccess());
}
public void OnLeaveScript()
{
}
#endregion
#region IActiveScriptSiteInterruptPoll implementation
public uint QueryContinue()
{
var callback = engine.ContinuationCallback;
var keepGoing = ((callback == null) || callback());
if (engine.CurrentScriptFrame != null)
{
keepGoing = keepGoing && !engine.CurrentScriptFrame.InterruptRequested;
}
return keepGoing ? RawCOMHelpers.HResult.S_OK : RawCOMHelpers.HResult.E_ABORT.ToUnsigned();
}
#endregion
#region IActiveScriptSiteWindow implementation
public void GetWindow(out IntPtr hwnd)
{
var hostWindow = engine.HostWindow;
hwnd = (hostWindow != null) ? hostWindow.OwnerHandle : IntPtr.Zero;
}
public void EnableModeless(bool enable)
{
var hostWindow = engine.HostWindow;
if (hostWindow != null)
hostWindow.EnableModeless(enable);
}
#endregion
#region IActiveScriptSiteDebug32 implementation
public void GetDocumentContextFromPosition(uint sourceContext, uint offset, uint length, out IDebugDocumentContext context)
{
context = null;
DebugDocument document;
if (engine.debugDocumentMap.TryGetValue(new UIntPtr(sourceContext), out document))
{
document.GetContextOfPosition(offset, length, out context);
}
}
public void GetApplication(out IDebugApplication32 application)
{
application = DebugApplicationWrapper32.Unwrap(engine.debugApplication);
}
public void GetRootApplicationNode(out IDebugApplicationNode node)
{
engine.debugApplication.GetRootNode(out node);
}
public void OnScriptErrorDebug(IActiveScriptErrorDebug errorDebug, out bool enterDebugger, out bool callOnScriptErrorWhenContinuing)
{
if ((engine.CurrentScriptFrame != null) && (errorDebug != null))
{
EXCEPINFO excepInfo;
errorDebug.GetExceptionInfo(out excepInfo);
if (excepInfo.scode == RawCOMHelpers.HResult.E_ABORT)
{
var description = excepInfo.bstrDescription ?? "Script execution interrupted by host";
engine.CurrentScriptFrame.PendingScriptError = new ScriptInterruptedException(engine.Name, description, GetDetails(errorDebug, description), excepInfo.scode, false, true, null);
}
else
{
var description = excepInfo.bstrDescription;
Exception innerException;
if (excepInfo.scode != RawCOMHelpers.HResult.CLEARSCRIPT_E_HOSTEXCEPTION)
{
innerException = null;
}
else
{
innerException = engine.CurrentScriptFrame.HostException;
if ((innerException != null) && string.IsNullOrWhiteSpace(description))
{
description = innerException.Message;
}
}
engine.CurrentScriptFrame.PendingScriptError = new ScriptEngineException(engine.Name, description, GetDetails(errorDebug, description), excepInfo.scode, false, true, innerException);
}
}
enterDebugger = engine.engineFlags.HasFlag(WindowsScriptEngineFlags.EnableJITDebugging);
callOnScriptErrorWhenContinuing = true;
}
#endregion
#region IActiveScriptSiteDebug64 implementation
public void GetDocumentContextFromPosition(ulong sourceContext, uint offset, uint length, out IDebugDocumentContext context)
{
context = null;
DebugDocument document;
if (engine.debugDocumentMap.TryGetValue(new UIntPtr(sourceContext), out document))
{
document.GetContextOfPosition(offset, length, out context);
}
}
public void GetApplication(out IDebugApplication64 application)
{
application = DebugApplicationWrapper64.Unwrap(engine.debugApplication);
}
#endregion
#region IActiveScriptSiteDebugEx implementation
public void OnCanNotJITScriptErrorDebug(IActiveScriptErrorDebug errorDebug, out bool callOnScriptErrorWhenContinuing)
{
bool enterDebugger;
OnScriptErrorDebug(errorDebug, out enterDebugger, out callOnScriptErrorWhenContinuing);
}
#endregion
#region ICustomQueryInterface implementation
public CustomQueryInterfaceResult GetInterface(ref Guid iid, out IntPtr pInterface)
{
pInterface = IntPtr.Zero;
if ((iid == typeof(IActiveScriptSiteDebug32).GUID) || (iid == typeof(IActiveScriptSiteDebug64).GUID) || (iid == typeof(IActiveScriptSiteDebugEx).GUID))
{
return (engine.processDebugManager != null) ? CustomQueryInterfaceResult.NotHandled : CustomQueryInterfaceResult.Failed;
}
if (iid == typeof(IActiveScriptSiteWindow).GUID)
{
return (engine.HostWindow != null) ? CustomQueryInterfaceResult.NotHandled : CustomQueryInterfaceResult.Failed;
}
return CustomQueryInterfaceResult.NotHandled;
}
#endregion
}
#endregion
}
}