forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveXWrappers.cs
More file actions
610 lines (487 loc) · 22.2 KB
/
ActiveXWrappers.cs
File metadata and controls
610 lines (487 loc) · 22.2 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Runtime.InteropServices;
using Microsoft.ClearScript.Util;
using Microsoft.ClearScript.Util.COM;
using EXCEPINFO = System.Runtime.InteropServices.ComTypes.EXCEPINFO;
namespace Microsoft.ClearScript.Windows
{
#region ActiveScriptWrapper
internal abstract class ActiveScriptWrapper
{
public static ActiveScriptWrapper Create(string progID, WindowsScriptEngineFlags flags)
{
if (Environment.Is64BitProcess)
{
return new ActiveScriptWrapper64(progID, flags);
}
return new ActiveScriptWrapper32(progID, flags);
}
public abstract void SetScriptSite(IActiveScriptSite site);
public abstract void SetScriptState(ScriptState state);
public abstract void GetScriptState(out ScriptState state);
public abstract void InitNew();
public abstract void GetScriptDispatch(string itemName, out object dispatch);
public abstract void AddNamedItem(string name, ScriptItemFlags flags);
public abstract void ParseScriptText(string code, string itemName, object context, string delimiter, UIntPtr sourceContext, uint startingLineNumber, ScriptTextFlags flags, IntPtr pVarResult, out EXCEPINFO excepInfo);
public abstract void InterruptScriptThread(uint scriptThreadID, ref EXCEPINFO excepInfo, ScriptInterruptFlags flags);
public abstract void EnumCodeContextsOfPosition(UIntPtr sourceContext, uint offset, uint length, out IEnumDebugCodeContexts enumContexts);
public abstract void EnumStackFrames(out IEnumDebugStackFrames enumFrames);
public abstract void CollectGarbage(ScriptGCType type);
public abstract void Close();
#region Nested type: DummyEnumDebugStackFrames
protected class DummyEnumDebugStackFrames : IEnumDebugStackFrames
{
#region IEnumDebugStackFrames implementation
public void Next(uint count, out DebugStackFrameDescriptor descriptor, out uint countFetched)
{
descriptor = default(DebugStackFrameDescriptor);
countFetched = 0;
}
public void Skip(uint count)
{
}
public void Reset()
{
}
public void Clone(out IEnumDebugStackFrames enumFrames)
{
throw new NotImplementedException();
}
#endregion
}
#endregion
}
internal sealed class ActiveScriptWrapper32 : ActiveScriptWrapper
{
// ReSharper disable NotAccessedField.Local
private IntPtr pActiveScript;
private IntPtr pActiveScriptParse;
private IntPtr pActiveScriptDebug;
private IntPtr pActiveScriptGarbageCollector;
private IntPtr pDebugStackFrameSniffer;
private IActiveScript activeScript;
private IActiveScriptParse32 activeScriptParse;
private IActiveScriptDebug32 activeScriptDebug;
private IActiveScriptGarbageCollector activeScriptGarbageCollector;
private IDebugStackFrameSnifferEx32 debugStackFrameSniffer;
// ReSharper restore NotAccessedField.Local
private delegate uint RawInterruptScriptThread(
[In] IntPtr pThis,
[In] uint scriptThreadID,
[In] ref EXCEPINFO excepInfo,
[In] ScriptInterruptFlags flags
);
private delegate uint RawEnumCodeContextsOfPosition(
[In] IntPtr pThis,
[In] uint sourceContext,
[In] uint offset,
[In] uint length,
[Out] [MarshalAs(UnmanagedType.Interface)] out IEnumDebugCodeContexts enumContexts
);
public ActiveScriptWrapper32(string progID, WindowsScriptEngineFlags flags)
{
// ReSharper disable SuspiciousTypeConversion.Global
pActiveScript = ActivationHelpers.CreateInstance<IActiveScript>(progID);
pActiveScriptParse = UnknownHelpers.QueryInterface<IActiveScriptParse32>(pActiveScript);
pActiveScriptDebug = UnknownHelpers.QueryInterface<IActiveScriptDebug32>(pActiveScript);
pActiveScriptGarbageCollector = UnknownHelpers.QueryInterfaceNoThrow<IActiveScriptGarbageCollector>(pActiveScript);
pDebugStackFrameSniffer = UnknownHelpers.QueryInterfaceNoThrow<IDebugStackFrameSnifferEx32>(pActiveScript);
activeScript = (IActiveScript)Marshal.GetObjectForIUnknown(pActiveScript);
activeScriptParse = (IActiveScriptParse32)activeScript;
activeScriptDebug = (IActiveScriptDebug32)activeScript;
activeScriptGarbageCollector = activeScript as IActiveScriptGarbageCollector;
debugStackFrameSniffer = activeScript as IDebugStackFrameSnifferEx32;
if (flags.HasFlag(WindowsScriptEngineFlags.EnableStandardsMode))
{
var activeScriptProperty = activeScript as IActiveScriptProperty;
if (activeScriptProperty != null)
{
object name;
activeScriptProperty.GetProperty(ScriptProp.Name, IntPtr.Zero, out name);
if (Equals(name, "JScript"))
{
object value = ScriptLanguageVersion.Standards;
activeScriptProperty.SetProperty(ScriptProp.InvokeVersioning, IntPtr.Zero, ref value);
}
}
if (!flags.HasFlag(WindowsScriptEngineFlags.DoNotEnableVTablePatching) && MiscHelpers.IsX86InstructionSet())
{
HostItem.EnableVTablePatching = true;
}
}
// ReSharper restore SuspiciousTypeConversion.Global
}
public override void SetScriptSite(IActiveScriptSite site)
{
activeScript.SetScriptSite(site);
}
public override void SetScriptState(ScriptState state)
{
activeScript.SetScriptState(state);
}
public override void GetScriptState(out ScriptState state)
{
activeScript.GetScriptState(out state);
}
public override void InitNew()
{
activeScriptParse.InitNew();
}
public override void GetScriptDispatch(string itemName, out object dispatch)
{
activeScript.GetScriptDispatch(itemName, out dispatch);
}
public override void AddNamedItem(string name, ScriptItemFlags flags)
{
activeScript.AddNamedItem(name, flags);
}
public override void ParseScriptText(string code, string itemName, object context, string delimiter, UIntPtr sourceContext, uint startingLineNumber, ScriptTextFlags flags, IntPtr pVarResult, out EXCEPINFO excepInfo)
{
activeScriptParse.ParseScriptText(code, itemName, context, delimiter, sourceContext.ToUInt32(), startingLineNumber, flags, pVarResult, out excepInfo);
}
public override void InterruptScriptThread(uint scriptThreadID, ref EXCEPINFO excepInfo, ScriptInterruptFlags flags)
{
var del = VTableHelpers.GetMethodDelegate<RawInterruptScriptThread>(pActiveScript, 14);
del(pActiveScript, scriptThreadID, ref excepInfo, flags);
}
public override void EnumCodeContextsOfPosition(UIntPtr sourceContext, uint offset, uint length, out IEnumDebugCodeContexts enumContexts)
{
var del = VTableHelpers.GetMethodDelegate<RawEnumCodeContextsOfPosition>(pActiveScriptDebug, 5);
HResult.Check(del(pActiveScriptDebug, sourceContext.ToUInt32(), offset, length, out enumContexts));
}
public override void EnumStackFrames(out IEnumDebugStackFrames enumFrames)
{
if (debugStackFrameSniffer != null)
{
debugStackFrameSniffer.EnumStackFrames(out enumFrames);
}
else
{
enumFrames = new DummyEnumDebugStackFrames();
}
}
public override void CollectGarbage(ScriptGCType type)
{
if (activeScriptGarbageCollector != null)
{
activeScriptGarbageCollector.CollectGarbage(type);
}
}
public override void Close()
{
debugStackFrameSniffer = null;
activeScriptGarbageCollector = null;
activeScriptDebug = null;
activeScriptParse = null;
UnknownHelpers.ReleaseAndEmpty(ref pDebugStackFrameSniffer);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScriptGarbageCollector);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScriptDebug);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScriptParse);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScript);
activeScript.Close();
Marshal.FinalReleaseComObject(activeScript);
activeScript = null;
}
}
internal sealed class ActiveScriptWrapper64 : ActiveScriptWrapper
{
// ReSharper disable NotAccessedField.Local
private IntPtr pActiveScript;
private IntPtr pActiveScriptParse;
private IntPtr pActiveScriptDebug;
private IntPtr pActiveScriptGarbageCollector;
private IntPtr pDebugStackFrameSniffer;
private IActiveScript activeScript;
private IActiveScriptParse64 activeScriptParse;
private IActiveScriptDebug64 activeScriptDebug;
private IActiveScriptGarbageCollector activeScriptGarbageCollector;
private IDebugStackFrameSnifferEx64 debugStackFrameSniffer;
// ReSharper restore NotAccessedField.Local
private delegate uint RawInterruptScriptThread(
[In] IntPtr pThis,
[In] uint scriptThreadID,
[In] ref EXCEPINFO excepInfo,
[In] ScriptInterruptFlags flags
);
private delegate uint RawEnumCodeContextsOfPosition(
[In] IntPtr pThis,
[In] ulong sourceContext,
[In] uint offset,
[In] uint length,
[Out] [MarshalAs(UnmanagedType.Interface)] out IEnumDebugCodeContexts enumContexts
);
public ActiveScriptWrapper64(string progID, WindowsScriptEngineFlags flags)
{
// ReSharper disable SuspiciousTypeConversion.Global
pActiveScript = ActivationHelpers.CreateInstance<IActiveScript>(progID);
pActiveScriptParse = UnknownHelpers.QueryInterface<IActiveScriptParse64>(pActiveScript);
pActiveScriptDebug = UnknownHelpers.QueryInterface<IActiveScriptDebug64>(pActiveScript);
pActiveScriptGarbageCollector = UnknownHelpers.QueryInterfaceNoThrow<IActiveScriptGarbageCollector>(pActiveScript);
pDebugStackFrameSniffer = UnknownHelpers.QueryInterfaceNoThrow<IDebugStackFrameSnifferEx64>(pActiveScript);
activeScript = (IActiveScript)Marshal.GetObjectForIUnknown(pActiveScript);
activeScriptParse = (IActiveScriptParse64)activeScript;
activeScriptDebug = (IActiveScriptDebug64)activeScript;
activeScriptGarbageCollector = activeScript as IActiveScriptGarbageCollector;
debugStackFrameSniffer = activeScript as IDebugStackFrameSnifferEx64;
if (flags.HasFlag(WindowsScriptEngineFlags.EnableStandardsMode))
{
var activeScriptProperty = activeScript as IActiveScriptProperty;
if (activeScriptProperty != null)
{
object name;
activeScriptProperty.GetProperty(ScriptProp.Name, IntPtr.Zero, out name);
if (Equals(name, "JScript"))
{
object value = ScriptLanguageVersion.Standards;
activeScriptProperty.SetProperty(ScriptProp.InvokeVersioning, IntPtr.Zero, ref value);
}
}
if (!flags.HasFlag(WindowsScriptEngineFlags.DoNotEnableVTablePatching) && MiscHelpers.IsX86InstructionSet())
{
HostItem.EnableVTablePatching = true;
}
}
// ReSharper restore SuspiciousTypeConversion.Global
}
public override void SetScriptSite(IActiveScriptSite site)
{
activeScript.SetScriptSite(site);
}
public override void SetScriptState(ScriptState state)
{
activeScript.SetScriptState(state);
}
public override void GetScriptState(out ScriptState state)
{
activeScript.GetScriptState(out state);
}
public override void InitNew()
{
activeScriptParse.InitNew();
}
public override void GetScriptDispatch(string itemName, out object dispatch)
{
activeScript.GetScriptDispatch(itemName, out dispatch);
}
public override void AddNamedItem(string name, ScriptItemFlags flags)
{
activeScript.AddNamedItem(name, flags);
}
public override void ParseScriptText(string code, string itemName, object context, string delimiter, UIntPtr sourceContext, uint startingLineNumber, ScriptTextFlags flags, IntPtr pVarResult, out EXCEPINFO excepInfo)
{
activeScriptParse.ParseScriptText(code, itemName, context, delimiter, sourceContext.ToUInt64(), startingLineNumber, flags, pVarResult, out excepInfo);
}
public override void InterruptScriptThread(uint scriptThreadID, ref EXCEPINFO excepInfo, ScriptInterruptFlags flags)
{
var del = VTableHelpers.GetMethodDelegate<RawInterruptScriptThread>(pActiveScript, 14);
del(pActiveScript, scriptThreadID, ref excepInfo, flags);
}
public override void EnumCodeContextsOfPosition(UIntPtr sourceContext, uint offset, uint length, out IEnumDebugCodeContexts enumContexts)
{
var del = VTableHelpers.GetMethodDelegate<RawEnumCodeContextsOfPosition>(pActiveScriptDebug, 5);
HResult.Check(del(pActiveScriptDebug, sourceContext.ToUInt64(), offset, length, out enumContexts));
}
public override void EnumStackFrames(out IEnumDebugStackFrames enumFrames)
{
if (debugStackFrameSniffer != null)
{
debugStackFrameSniffer.EnumStackFrames(out enumFrames);
}
else
{
enumFrames = new DummyEnumDebugStackFrames();
}
}
public override void CollectGarbage(ScriptGCType type)
{
if (activeScriptGarbageCollector != null)
{
activeScriptGarbageCollector.CollectGarbage(type);
}
}
public override void Close()
{
debugStackFrameSniffer = null;
activeScriptGarbageCollector = null;
activeScriptDebug = null;
activeScriptParse = null;
UnknownHelpers.ReleaseAndEmpty(ref pDebugStackFrameSniffer);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScriptGarbageCollector);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScriptDebug);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScriptParse);
UnknownHelpers.ReleaseAndEmpty(ref pActiveScript);
activeScript.Close();
Marshal.FinalReleaseComObject(activeScript);
activeScript = null;
}
}
#endregion
#region ProcessDebugManagerWrapper
internal abstract class ProcessDebugManagerWrapper
{
public static bool TryCreate(out ProcessDebugManagerWrapper wrapper)
{
if (Environment.Is64BitProcess)
{
return ProcessDebugManagerWrapper64.TryCreate(out wrapper);
}
return ProcessDebugManagerWrapper32.TryCreate(out wrapper);
}
public abstract void CreateApplication(out DebugApplicationWrapper applicationWrapper);
public abstract bool TryAddApplication(DebugApplicationWrapper applicationWrapper, out uint cookie);
public abstract void RemoveApplication(uint cookie);
}
internal sealed class ProcessDebugManagerWrapper32 : ProcessDebugManagerWrapper
{
private readonly IProcessDebugManager32 processDebugManager;
public new static bool TryCreate(out ProcessDebugManagerWrapper wrapper)
{
IProcessDebugManager32 processDebugManager;
if (MiscHelpers.TryCreateCOMObject("ProcessDebugManager", null, out processDebugManager))
{
wrapper = new ProcessDebugManagerWrapper32(processDebugManager);
return true;
}
wrapper = null;
return false;
}
private ProcessDebugManagerWrapper32(IProcessDebugManager32 processDebugManager)
{
this.processDebugManager = processDebugManager;
}
public override void CreateApplication(out DebugApplicationWrapper applicationWrapper)
{
IDebugApplication32 debugApplication;
processDebugManager.CreateApplication(out debugApplication);
applicationWrapper = DebugApplicationWrapper.Create(debugApplication);
}
public override bool TryAddApplication(DebugApplicationWrapper applicationWrapper, out uint cookie)
{
return HResult.Succeeded(processDebugManager.AddApplication(DebugApplicationWrapper32.Unwrap(applicationWrapper), out cookie));
}
public override void RemoveApplication(uint cookie)
{
processDebugManager.RemoveApplication(cookie);
}
}
internal sealed class ProcessDebugManagerWrapper64 : ProcessDebugManagerWrapper
{
private readonly IProcessDebugManager64 processDebugManager;
public new static bool TryCreate(out ProcessDebugManagerWrapper wrapper)
{
IProcessDebugManager64 processDebugManager;
if (MiscHelpers.TryCreateCOMObject("ProcessDebugManager", null, out processDebugManager))
{
wrapper = new ProcessDebugManagerWrapper64(processDebugManager);
return true;
}
wrapper = null;
return false;
}
private ProcessDebugManagerWrapper64(IProcessDebugManager64 processDebugManager)
{
this.processDebugManager = processDebugManager;
}
public override void CreateApplication(out DebugApplicationWrapper applicationWrapper)
{
IDebugApplication64 debugApplication;
processDebugManager.CreateApplication(out debugApplication);
applicationWrapper = DebugApplicationWrapper.Create(debugApplication);
}
public override bool TryAddApplication(DebugApplicationWrapper applicationWrapper, out uint cookie)
{
return HResult.Succeeded(processDebugManager.AddApplication(DebugApplicationWrapper64.Unwrap(applicationWrapper), out cookie));
}
public override void RemoveApplication(uint cookie)
{
processDebugManager.RemoveApplication(cookie);
}
}
#endregion
#region DebugApplicationWrapper
internal abstract class DebugApplicationWrapper
{
public static DebugApplicationWrapper Create(IDebugApplication64 debugApplication)
{
return new DebugApplicationWrapper64(debugApplication);
}
public static DebugApplicationWrapper Create(IDebugApplication32 debugApplication)
{
return new DebugApplicationWrapper32(debugApplication);
}
public abstract void SetName(string name);
public abstract void GetRootNode(out IDebugApplicationNode node);
public abstract void CreateApplicationNode(out IDebugApplicationNode node);
public abstract uint GetDebugger(out IApplicationDebugger debugger);
public abstract void Close();
}
internal sealed class DebugApplicationWrapper32 : DebugApplicationWrapper
{
private readonly IDebugApplication32 debugApplication;
public DebugApplicationWrapper32(IDebugApplication32 debugApplication)
{
this.debugApplication = debugApplication;
}
public static IDebugApplication32 Unwrap(DebugApplicationWrapper wrapper)
{
var wrapper32 = wrapper as DebugApplicationWrapper32;
return (wrapper32 != null) ? wrapper32.debugApplication : null;
}
public override void SetName(string name)
{
debugApplication.SetName(name);
}
public override void GetRootNode(out IDebugApplicationNode node)
{
debugApplication.GetRootNode(out node);
}
public override void CreateApplicationNode(out IDebugApplicationNode node)
{
debugApplication.CreateApplicationNode(out node);
}
public override uint GetDebugger(out IApplicationDebugger debugger)
{
return debugApplication.GetDebugger(out debugger);
}
public override void Close()
{
debugApplication.Close();
}
}
internal sealed class DebugApplicationWrapper64 : DebugApplicationWrapper
{
private readonly IDebugApplication64 debugApplication;
public DebugApplicationWrapper64(IDebugApplication64 debugApplication)
{
this.debugApplication = debugApplication;
}
public static IDebugApplication64 Unwrap(DebugApplicationWrapper wrapper)
{
var wrapper64 = wrapper as DebugApplicationWrapper64;
return (wrapper64 != null) ? wrapper64.debugApplication : null;
}
public override void SetName(string name)
{
debugApplication.SetName(name);
}
public override void GetRootNode(out IDebugApplicationNode node)
{
debugApplication.GetRootNode(out node);
}
public override void CreateApplicationNode(out IDebugApplicationNode node)
{
debugApplication.CreateApplicationNode(out node);
}
public override uint GetDebugger(out IApplicationDebugger debugger)
{
return debugApplication.GetDebugger(out debugger);
}
public override void Close()
{
debugApplication.Close();
}
}
#endregion
}