forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJScriptEngine.cs
More file actions
319 lines (278 loc) · 13.7 KB
/
JScriptEngine.cs
File metadata and controls
319 lines (278 loc) · 13.7 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript.Windows
{
/// <summary>
/// Represents an instance of the JScript engine.
/// </summary>
public class JScriptEngine : WindowsScriptEngine, IJavaScriptEngine
{
#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/1dk3k160(v=vs.84)
{ 5029, "Array length must be a finite positive integer" },
{ 5030, "Array length must be assigned a finite positive number" },
{ 5028, "Array or arguments object expected" },
{ 5010, "Boolean expected" },
{ 5003, "Cannot assign to a function result" },
{ 5000, "Cannot assign to 'this'" },
{ 5034, "Circular reference in value argument not supported" },
{ 5006, "Date object expected" },
{ 5015, "Enumerator object expected" },
{ 5022, "Exception thrown and not caught" },
{ 5020, "Expected ')' in regular expression" },
{ 5019, "Expected ']' in regular expression" },
{ 5023, "Function does not have a valid prototype object" },
{ 5002, "Function expected" },
{ 5008, "Illegal assignment" },
{ 5021, "Invalid range in character set" },
{ 5035, "Invalid replacer argument" },
{ 5014, "JScript object expected" },
{ 5001, "Number expected" },
{ 5007, "Object expected" },
{ 5012, "Object member expected" },
{ 5016, "Regular Expression object expected" },
{ 5005, "String expected" },
{ 5017, "Syntax error in regular expression" },
{ 5026, "The number of fractional digits is out of range" },
{ 5027, "The precision is out of range" },
{ 5025, "The URI to be decoded is not a valid encoding" },
{ 5024, "The URI to be encoded contains an invalid character" },
{ 5009, "Undefined identifier" },
{ 5018, "Unexpected quantifier" },
{ 5013, "VBArray expected" }
};
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/6bby3x2e(v=vs.84)
{ 1019, "Can't have 'break' outside of loop" },
{ 1020, "Can't have 'continue' outside of loop" },
{ 1030, "Conditional compilation is turned off" },
{ 1027, "'default' can only appear once in a 'switch' statement" },
{ 1005, "Expected '('" },
{ 1006, "Expected ')'" },
{ 1012, "Expected '/'" },
{ 1003, "Expected ':'" },
{ 1004, "Expected ';'" },
{ 1032, "Expected '@'" },
{ 1029, "Expected '@end'" },
{ 1007, "Expected ']'" },
{ 1008, "Expected '{'" },
{ 1009, "Expected '}'" },
{ 1011, "Expected '='" },
{ 1033, "Expected 'catch'" },
{ 1031, "Expected constant" },
{ 1023, "Expected hexadecimal digit" },
{ 1010, "Expected identifier" },
{ 1028, "Expected identifier, string or number" },
{ 1024, "Expected 'while'" },
{ 1014, "Invalid character" },
{ 1026, "Label not found" },
{ 1025, "Label redefined" },
{ 1018, "'return' statement outside of function" },
{ 1002, "Syntax error" },
{ 1035, "Throw must be followed by an expression on the same source line" },
{ 1016, "Unterminated comment" },
{ 1015, "Unterminated string constant" }
};
private CommonJSManager commonJSManager;
#endregion
#region constructors
/// <summary>
/// Initializes a new JScript engine instance.
/// </summary>
public JScriptEngine()
: this(null)
{
}
/// <summary>
/// Initializes a new JScript 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 JScriptEngine(string name)
: this(name, WindowsScriptEngineFlags.None)
{
}
/// <summary>
/// Initializes a new JScript engine instance with the specified options.
/// </summary>
/// <param name="flags">A value that selects options for the operation.</param>
public JScriptEngine(WindowsScriptEngineFlags flags)
: this(null, flags)
{
}
/// <summary>
/// Initializes a new JScript 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 JScriptEngine(string name, WindowsScriptEngineFlags flags)
: this("JScript", name, flags)
{
}
/// <summary>
/// Initializes a new JScript engine instance with the specified programmatic
/// identifier, name, and options.
/// </summary>
/// <param name="progID">The programmatic identifier (ProgID) of the JScript 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 JScriptEngine(string progID, string name, WindowsScriptEngineFlags flags)
: this(progID, name, "js", flags)
{
}
/// <summary>
/// Initializes a new JScript 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 JScript 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 JScriptEngine(string progID, string name, string fileNameExtensions, WindowsScriptEngineFlags flags)
: base(progID, name, fileNameExtensions, flags)
{
Execute(
MiscHelpers.FormatInvariant("{0} [internal]", GetType().Name),
@"
EngineInternal = (function () {
function convertArgs(args) {
var result = [];
if (args.GetValue) {
var count = args.Length;
for (var i = 0; i < count; i++) {
result.push(args[i]);
}
}
else {
args = new VBArray(args);
var count = args.ubound(1) + 1;
for (var i = 0; i < count; i++) {
result.push(args.getItem(i));
}
}
return result;
}
function construct(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15) {
return new this(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
}
return {
getCommandResult: function (value) {
if (value != null) {
if ((typeof(value) == 'object') || (typeof(value) == 'function')) {
if (typeof(value.toString) == 'function') {
return value.toString();
}
}
}
return value;
},
invokeConstructor: function (constructor, args) {
if (typeof(constructor) != 'function') {
throw new Error('Function expected');
}
return construct.apply(constructor, convertArgs(args));
},
invokeMethod: function (target, method, args) {
if (typeof(method) != 'function') {
throw new Error('Function expected');
}
return method.apply(target, convertArgs(args));
},
isPromise: function (value) {
return false;
},
throwValue: function (value) {
throw value;
}
};
})();
"
);
}
#endregion
#region internal members
private CommonJSManager CommonJSManager => commonJSManager ?? (commonJSManager = new CommonJSManager(this));
#endregion
#region ScriptEngine overrides
/// <summary>
/// Gets the script engine's recommended file name extension for script files.
/// </summary>
/// <remarks>
/// <see cref="JScriptEngine"/> instances return "js" for this property.
/// </remarks>
public override string FileNameExtension => "js";
/// <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="JScriptEngine"/> version of this method attempts to use
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/tostring">toString</see>
/// to convert the return value.
/// </para>
/// </remarks>
public override string ExecuteCommand(string command)
{
Script.EngineInternal.command = command;
return base.ExecuteCommand("EngineInternal.getCommandResult(eval(EngineInternal.command))");
}
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 == ModuleCategory.CommonJS)
{
var module = CommonJSManager.GetOrCreateModule(documentInfo, code);
return ScriptInvoke(() => module.Process());
}
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
#region IJavaScriptEngine implementation
uint IJavaScriptEngine.BaseLanguageVersion => 3;
object IJavaScriptEngine.CreatePromiseForTask<T>(Task<T> task)
{
throw new NotImplementedException();
}
object IJavaScriptEngine.CreatePromiseForTask(Task task)
{
throw new NotImplementedException();
}
Task<object> IJavaScriptEngine.CreateTaskForPromise(ScriptObject promise)
{
throw new NotImplementedException();
}
#endregion
}
}