forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVBScriptEngine.cs
More file actions
394 lines (371 loc) · 23.1 KB
/
VBScriptEngine.cs
File metadata and controls
394 lines (371 loc) · 23.1 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript.Windows
{
/// <summary>
/// Represents an instance of the VBScript engine.
/// </summary>
[Guid(TypeGuids.VBScriptEngine)]
public class VBScriptEngine : WindowsScriptEngine
{
#region data
private static readonly Dictionary<int, string> runtimeErrorMap = new Dictionary<int, string>
{
// https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/xe43cc8d(v=vs.84)
{ 429, "ActiveX component can't create object" },
{ 507, "An exception occurred" },
{ 449, "Argument not optional" },
{ 17, "Can't perform requested operation" },
{ 430, "Class doesn't support Automation" },
{ 506, "Class not defined" },
{ 11, "Division by zero" },
{ 48, "Error in loading DLL" },
{ 5020, "Expected ')' in regular expression" },
{ 5019, "Expected ']' in regular expression" },
{ 432, "File name or class name not found during Automation operation" },
{ 92, "For loop not initialized" },
{ 5008, "Illegal assignment" },
{ 51, "Internal error" },
{ 505, "Invalid or unqualified reference" },
{ 481, "Invalid picture" },
{ 5, "Invalid procedure call or argument" },
{ 5021, "Invalid range in character set" },
{ 94, "Invalid use of Null" },
{ 448, "Named argument not found" },
{ 447, "Object doesn't support current locale setting" },
{ 445, "Object doesn't support this action" },
{ 438, "Object doesn't support this property or method" },
{ 451, "Object not a collection" },
{ 504, "Object not safe for creating" },
{ 503, "Object not safe for initializing" },
{ 502, "Object not safe for scripting" },
{ 424, "Object required" },
{ 91, "Object variable not set" },
{ 7, "Out of Memory" },
{ 28, "Out of stack space" },
{ 14, "Out of string space" },
{ 6, "Overflow" },
{ 35, "Sub or function not defined" },
{ 9, "Subscript out of range" },
{ 5017, "Syntax error in regular expression" },
{ 462, "The remote server machine does not exist or is unavailable" },
{ 10, "This array is fixed or temporarily locked" },
{ 13, "Type mismatch" },
{ 5018, "Unexpected quantifier" },
{ 500, "Variable is undefined" },
{ 458, "Variable uses an Automation type not supported in VBScript" },
{ 450, "Wrong number of arguments or invalid property assignment" }
};
private static readonly Dictionary<int, string> syntaxErrorMap = new Dictionary<int, string>
{
// https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/2z84dwk8(v=vs.84)
{ 1052, "Cannot have multiple default property/method in a Class" },
{ 1044, "Cannot use parentheses when calling a Sub" },
{ 1053, "Class initialize or terminate do not have arguments" },
{ 1058, "'Default' specification can only be on Property Get" },
{ 1057, "'Default' specification must also specify 'Public'" },
{ 1005, "Expected '('" },
{ 1006, "Expected ')'" },
{ 1007, "Expected ']'" }, // missing in the online documentation
{ 1011, "Expected '='" },
{ 1021, "Expected 'Case'" },
{ 1047, "Expected 'Class'" },
{ 1025, "Expected end of statement" },
{ 1014, "Expected 'End'" },
{ 1023, "Expected expression" },
{ 1015, "Expected 'Function'" },
{ 1010, "Expected identifier" },
{ 1012, "Expected 'If'" },
{ 1046, "Expected 'In'" },
{ 1026, "Expected integer constant" },
{ 1049, "Expected Let or Set or Get in property declaration" },
{ 1045, "Expected literal constant" },
{ 1019, "Expected 'Loop'" },
{ 1020, "Expected 'Next'" },
{ 1050, "Expected 'Property'" },
{ 1022, "Expected 'Select'" },
{ 1024, "Expected statement" },
{ 1016, "Expected 'Sub'" },
{ 1017, "Expected 'Then'" },
{ 1013, "Expected 'To'" },
{ 1018, "Expected 'Wend'" },
{ 1027, "Expected 'While' or 'Until'" },
{ 1028, "Expected 'While,' 'Until,' or end of statement" },
{ 1029, "Expected 'With'" },
{ 1030, "Identifier too long" },
{ 1032, "Invalid character" }, // incorrectly listed as 1014 in the online documentation
{ 1039, "Invalid 'exit' statement" },
{ 1040, "Invalid 'for' loop control variable" },
{ 1031, "Invalid number" }, // incorrectly listed as 1013 in the online documentation
{ 1037, "Invalid use of 'Me' keyword" },
{ 1038, "'loop' without 'do'" },
{ 1048, "Must be defined inside a Class" },
{ 1042, "Must be first statement on the line" },
{ 1041, "Name redefined" },
{ 1051, "Number of arguments must be consistent across properties specification" },
{ 1001, "Out of Memory" },
{ 1054, "Property Set or Let must have at least one argument" },
{ 1002, "Syntax error" },
{ 1055, "Unexpected 'Next'" },
{ 1033, "Unterminated string constant" } // incorrectly listed as 1015 in the online documentation
};
#endregion
#region constructors
/// <summary>
/// Initializes a new VBScript engine instance.
/// </summary>
public VBScriptEngine()
: this(null)
{
}
/// <summary>
/// Initializes a new VBScript engine instance with the specified name.
/// </summary>
/// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
public VBScriptEngine(string name)
: this(name, WindowsScriptEngineFlags.None)
{
}
/// <summary>
/// Initializes a new VBScript engine instance with the specified options.
/// </summary>
/// <param name="flags">A value that selects options for the operation.</param>
public VBScriptEngine(WindowsScriptEngineFlags flags)
: this(null, flags)
{
}
/// <summary>
/// Initializes a new VBScript engine instance with the specified name and options.
/// </summary>
/// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
/// <param name="flags">A value that selects options for the operation.</param>
public VBScriptEngine(string name, WindowsScriptEngineFlags flags)
: this("VBScript", name, flags)
{
}
/// <summary>
/// Initializes a new VBScript engine instance with the specified programmatic
/// identifier, name, and options.
/// </summary>
/// <param name="progID">The programmatic identifier (ProgID) of the VBScript engine class.</param>
/// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
/// <param name="flags">A value that selects options for the operation.</param>
/// <remarks>
/// The <paramref name="progID"/> argument can be a class identifier (CLSID) in standard
/// GUID format with braces (e.g., "{F414C260-6AC0-11CF-B6D1-00AA00BBBB58}").
/// </remarks>
protected VBScriptEngine(string progID, string name, WindowsScriptEngineFlags flags)
: this(progID, name, "vbs", flags)
{
}
/// <summary>
/// Initializes a new VBScript engine instance with the specified programmatic
/// identifier, name, list of supported file name extensions, and options.
/// </summary>
/// <param name="progID">The programmatic identifier (ProgID) of the VBScript engine class.</param>
/// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
/// <param name="fileNameExtensions">A semicolon-delimited list of supported file name extensions.</param>
/// <param name="flags">A value that selects options for the operation.</param>
/// <remarks>
/// The <paramref name="progID"/> argument can be a class identifier (CLSID) in standard
/// GUID format with braces (e.g., "{F414C260-6AC0-11CF-B6D1-00AA00BBBB58}").
/// </remarks>
protected VBScriptEngine(string progID, string name, string fileNameExtensions, WindowsScriptEngineFlags flags)
: base(progID, name, fileNameExtensions, flags)
{
Execute(
MiscHelpers.FormatInvariant("{0} [internal]", GetType().Name),
@"
class EngineInternalImpl
public function getCommandResult(value)
if IsObject(value) then
if value is nothing then
getCommandResult = ""[nothing]""
else
dim valueTypeName
valueTypeName = TypeName(value)
if (valueTypeName = ""Object"" or valueTypeName = ""Unknown"") then
set getCommandResult = value
else
getCommandResult = ""[ScriptObject:"" & valueTypeName & ""]""
end if
end if
elseif IsArray(value) then
getCommandResult = ""[array]""
elseif IsNull(value) then
getCommandResult = ""[null]""
elseif IsEmpty(value) then
getCommandResult = ""[empty]""
else
getCommandResult = CStr(value)
end if
end function
public function invokeConstructor(constructor, args)
Err.Raise 445
end function
public function invokeMethod(target, method, args)
if IsObject(target) then
if target is nothing then
else
Err.Raise 445
end if
elseif IsNull(target) then
elseif IsEmpty(target) then
else
Err.Raise 445
end if
dim count
if IsArray(args) then
count = UBound(args) + 1
if count < 1 then
invokeMethod = method()
elseif count = 1 then
invokeMethod = method(args(0))
elseif count = 2 then
invokeMethod = method(args(0), args(1))
elseif count = 3 then
invokeMethod = method(args(0), args(1), args(2))
elseif count = 4 then
invokeMethod = method(args(0), args(1), args(2), args(3))
elseif count = 5 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4))
elseif count = 6 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5))
elseif count = 7 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6))
elseif count = 8 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7))
elseif count = 9 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8))
elseif count = 10 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9))
elseif count = 11 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9), args(10))
elseif count = 12 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9), args(10), args(11))
elseif count = 13 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9), args(10), args(11), args(12))
elseif count = 14 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9), args(10), args(11), args(12), args(13))
elseif count = 15 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9), args(10), args(11), args(12), args(13), args(14))
elseif count = 16 then
invokeMethod = method(args(0), args(1), args(2), args(3), args(4), args(5), args(6), args(7), args(8), args(9), args(10), args(11), args(12), args(13), args(14), args(15))
else
Err.Raise 450
end if
else
count = args.Length
if count < 1 then
invokeMethod = method()
elseif count = 1 then
invokeMethod = method(args.GetValue(0))
elseif count = 2 then
invokeMethod = method(args.GetValue(0), args.GetValue(1))
elseif count = 3 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2))
elseif count = 4 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3))
elseif count = 5 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4))
elseif count = 6 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5))
elseif count = 7 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6))
elseif count = 8 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7))
elseif count = 9 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8))
elseif count = 10 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8), args.GetValue(9))
elseif count = 11 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8), args.GetValue(9), args.GetValue(10))
elseif count = 12 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8), args.GetValue(9), args.GetValue(10), args.GetValue(11))
elseif count = 13 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8), args.GetValue(9), args.GetValue(10), args.GetValue(11), args.GetValue(12))
elseif count = 14 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8), args.GetValue(9), args.GetValue(10), args.GetValue(11), args.GetValue(12), args.GetValue(13))
elseif count = 15 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8), args.GetValue(9), args.GetValue(10), args.GetValue(11), args.GetValue(12), args.GetValue(13), args.GetValue(14))
elseif count = 16 then
invokeMethod = method(args.GetValue(0), args.GetValue(1), args.GetValue(2), args.GetValue(3), args.GetValue(4), args.GetValue(5), args.GetValue(6), args.GetValue(7), args.GetValue(8), args.GetValue(9), args.GetValue(10), args.GetValue(11), args.GetValue(12), args.GetValue(13), args.GetValue(14), args.GetValue(15))
else
Err.Raise 450
end if
end if
end function
public function isPromise(value)
isPromise = false
end function
public function throwValue(value)
Err.Raise 445
end function
end class
set EngineInternal = new EngineInternalImpl
"
);
}
#endregion
#region ScriptEngine overrides
/// <summary>
/// Gets the script engine's recommended file name extension for script files.
/// </summary>
/// <remarks>
/// <see cref="VBScriptEngine"/> instances return "vbs" for this property.
/// </remarks>
public override string FileNameExtension => "vbs";
/// <summary>
/// Executes script code as a command.
/// </summary>
/// <param name="command">The script command to execute.</param>
/// <returns>The command output.</returns>
/// <remarks>
/// <para>
/// This method is similar to <see cref="ScriptEngine.Evaluate(string)"/> but optimized for
/// command consoles. The specified command must be limited to a single expression or
/// statement. Script engines can override this method to customize command execution as
/// well as the process of converting the result to a string for console output.
/// </para>
/// <para>
/// The <see cref="VBScriptEngine"/> version of this method supports both expressions and
/// statements. If the specified command begins with "eval " (not case-sensitive), the
/// engine executes the remainder as an expression and attempts to use
/// <see href="https://docs.microsoft.com/en-us/previous-versions//0zk841e9(v=vs.85)">CStr</see>
/// to convert the result value. Otherwise, it executes the command as a statement and does
/// not return a value.
/// </para>
/// </remarks>
public override string ExecuteCommand(string command)
{
var trimmedCommand = command.Trim();
if (trimmedCommand.StartsWith("eval ", StringComparison.OrdinalIgnoreCase))
{
var expression = MiscHelpers.FormatInvariant("EngineInternal.getCommandResult({0})", trimmedCommand.Substring(5));
var documentInfo = new DocumentInfo("Expression") { Flags = DocumentFlags.IsTransient };
return GetCommandResultString(Evaluate(documentInfo.MakeUnique(this), expression, false));
}
Execute("Command", true, trimmedCommand);
return null;
}
internal override IDictionary<int, string> RuntimeErrorMap => runtimeErrorMap;
internal override IDictionary<int, string> SyntaxErrorMap => syntaxErrorMap;
internal override object Execute(UniqueDocumentInfo documentInfo, string code, bool evaluate)
{
if (FormatCode)
{
code = MiscHelpers.FormatCode(code);
}
if (documentInfo.Category != DocumentCategory.Script)
{
throw new NotSupportedException("The script engine cannot execute documents of type '" + documentInfo.Category + "'");
}
return base.Execute(documentInfo, code, evaluate);
}
#endregion
}
}