forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptExtensions.cs
More file actions
320 lines (293 loc) · 15.4 KB
/
JavaScriptExtensions.cs
File metadata and controls
320 lines (293 loc) · 15.4 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript.JavaScript
{
// ReSharper disable once PartialTypeWithSinglePart
/// <summary>
/// Defines extension methods for use with JavaScript engines.
/// </summary>
public static partial class JavaScriptExtensions
{
/// <summary>
/// Converts a <c><see cref="Task{T}"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code currently running on the calling thread.
/// </summary>
/// <typeparam name="T">The task's result type.</typeparam>
/// <param name="task">The task to convert to a promise.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise<T>(this Task<T> task)
{
return task.ToPromise(ScriptEngine.Current);
}
/// <summary>
/// Converts a <c><see cref="Task{T}"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code running in the specified script engine.
/// </summary>
/// <typeparam name="T">The task's result type.</typeparam>
/// <param name="task">The task to convert to a promise.</param>
/// <param name="engine">The script engine in which the promise will be used.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise<T>(this Task<T> task, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(task, nameof(task));
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
var javaScriptEngine = engine as IJavaScriptEngine;
if ((javaScriptEngine is null) || (javaScriptEngine.BaseLanguageVersion < 6))
{
throw new NotSupportedException("The script engine does not support promises");
}
return javaScriptEngine.CreatePromiseForTask(task);
}
/// <summary>
/// Converts a <c><see cref="Task"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code currently running on the calling thread.
/// </summary>
/// <param name="task">The task to convert to a promise.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise(this Task task)
{
return task.ToPromise(ScriptEngine.Current);
}
/// <summary>
/// Converts a <c><see cref="Task"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code running in the specified script engine.
/// </summary>
/// <param name="task">The task to convert to a promise.</param>
/// <param name="engine">The script engine in which the promise will be used.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise(this Task task, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(task, nameof(task));
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
var javaScriptEngine = engine as IJavaScriptEngine;
if ((javaScriptEngine is null) || (javaScriptEngine.BaseLanguageVersion < 6))
{
throw new NotSupportedException("The script engine does not support promises");
}
return javaScriptEngine.CreatePromiseForTask(task);
}
/// <summary>
/// Converts a <c><see cref="ValueTask{T}"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code currently running on the calling thread.
/// </summary>
/// <typeparam name="T">The task's result type.</typeparam>
/// <param name="valueTask">The task to convert to a promise.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise<T>(this ValueTask<T> valueTask)
{
return valueTask.ToPromise(ScriptEngine.Current);
}
/// <summary>
/// Converts a <c><see cref="ValueTask{T}"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code running in the specified script engine.
/// </summary>
/// <typeparam name="T">The task's result type.</typeparam>
/// <param name="valueTask">The task to convert to a promise.</param>
/// <param name="engine">The script engine in which the promise will be used.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise<T>(this ValueTask<T> valueTask, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(valueTask, nameof(valueTask));
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
var javaScriptEngine = engine as IJavaScriptEngine;
if ((javaScriptEngine is null) || (javaScriptEngine.BaseLanguageVersion < 6))
{
throw new NotSupportedException("The script engine does not support promises");
}
return javaScriptEngine.CreatePromiseForValueTask(valueTask);
}
/// <summary>
/// Converts a <c><see cref="ValueTask"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code currently running on the calling thread.
/// </summary>
/// <param name="valueTask">The task to convert to a promise.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise(this ValueTask valueTask)
{
return valueTask.ToPromise(ScriptEngine.Current);
}
/// <summary>
/// Converts a <c><see cref="ValueTask"/></c> instance to a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// for use with script code running in the specified script engine.
/// </summary>
/// <param name="valueTask">The task to convert to a promise.</param>
/// <param name="engine">The script engine in which the promise will be used.</param>
/// <returns>A promise that represents the task's asynchronous operation.</returns>
public static object ToPromise(this ValueTask valueTask, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(valueTask, nameof(valueTask));
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
var javaScriptEngine = engine as IJavaScriptEngine;
if ((javaScriptEngine is null) || (javaScriptEngine.BaseLanguageVersion < 6))
{
throw new NotSupportedException("The script engine does not support promises");
}
return javaScriptEngine.CreatePromiseForValueTask(valueTask);
}
/// <summary>
/// Converts a
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promise</see>
/// to a <c><see cref="Task{Object}">Task<Object></see></c> instance.
/// </summary>
/// <param name="promise">The promise to convert to a task (see remarks).</param>
/// <returns>A task that represents the promise's asynchronous operation.</returns>
/// <remarks>
/// If the argument is a <c><see cref="Task{Object}">Task<Object></see></c> instance,
/// this method returns it as is.
/// </remarks>
public static Task<object> ToTask(this object promise)
{
MiscHelpers.VerifyNonNullArgument(promise, nameof(promise));
return promise as Task<object> ?? promise.ToTaskInternal();
}
/// <summary>
/// Supports managed iteration over an
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">iterable</see>
/// script object.
/// </summary>
/// <param name="iterable">An iterable script object (see remarks).</param>
/// <returns>An <c><see cref="IEnumerable{Object}">IEnumerable<Object></see></c> implementation that supports managed iteration over <paramref name="iterable"/>.</returns>
/// <remarks>
/// If the argument implements
/// <c><see cref="IEnumerable{Object}">IEnumerable<Object></see></c>, this method
/// returns it as is.
/// </remarks>
public static IEnumerable<object> ToEnumerable(this object iterable)
{
// WARNING: The IEnumerable<object> test below is a bit dicey, as most IEnumerable<T>
// implementations support IEnumerable<object> via covariance. The desired behavior
// here is for that test to fail for IDictionary<TKey, TValue>, as V8 script objects
// now support that interface. Luckily, that test does fail, but only because
// KeyValuePair<TKey, TValue> is a struct, so covariance doesn't apply.
MiscHelpers.VerifyNonNullArgument(iterable, nameof(iterable));
return iterable as IEnumerable<object> ?? iterable.ToEnumerableInternal();
}
/// <summary>
/// Supports managed asynchronous iteration over an
/// <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols">async-iterable</see>
/// script object.
/// </summary>
/// <param name="asyncIterable">An async-iterable script object (see remarks).</param>
/// <returns>An <c><see cref="IAsyncEnumerable{Object}">IAsyncEnumerator<Object></see></c> implementation that supports managed asynchronous iteration over <paramref name="asyncIterable"/>.</returns>
/// <remarks>
/// If the argument implements <c><see cref="IAsyncEnumerable{Object}">IAsyncEnumerator<Object></see></c>, this method returns it as is.
/// </remarks>
public static IAsyncEnumerable<object> ToAsyncEnumerable(this object asyncIterable)
{
MiscHelpers.VerifyNonNullArgument(asyncIterable, nameof(asyncIterable));
return asyncIterable as IAsyncEnumerable<object> ?? asyncIterable.ToAsyncEnumerableInternal();
}
private static Task<object> ToTaskInternal(this object promise)
{
var scriptObject = promise as ScriptObject;
if (scriptObject is null)
{
throw new ArgumentException("The object is not a promise", nameof(promise));
}
var javaScriptEngine = scriptObject.Engine as IJavaScriptEngine;
if ((javaScriptEngine is null) || (javaScriptEngine.BaseLanguageVersion < 6))
{
throw new NotSupportedException("The script engine does not support promises");
}
return javaScriptEngine.CreateTaskForPromise(scriptObject);
}
private static IEnumerable<object> ToEnumerableInternal(this object iterable)
{
if (iterable is ScriptObject scriptObject)
{
if (scriptObject.Engine is IJavaScriptEngine javaScriptEngine && (javaScriptEngine.BaseLanguageVersion >= 6))
{
if (scriptObject.Engine.EngineInternal.InvokeMethod("getIterator", scriptObject) is ScriptObject iterator)
{
while (iterator.InvokeMethod("next") is ScriptObject result && !Equals(result["done"], true))
{
yield return result["value"];
}
}
else
{
throw new ArgumentException("The object is not iterable", nameof(iterable));
}
}
else
{
throw new NotSupportedException("The script engine does not support iteration");
}
}
else if (iterable is IEnumerable enumerable)
{
foreach (var item in enumerable)
{
yield return item;
}
}
else
{
throw new ArgumentException("The object is not iterable", nameof(iterable));
}
}
private static async IAsyncEnumerable<object> ToAsyncEnumerableInternal(this object asyncIterable)
{
if (asyncIterable is IEnumerable<object> objectEnumerable)
{
foreach (var item in objectEnumerable)
{
yield return item;
}
}
else if (asyncIterable is ScriptObject scriptObject)
{
if (scriptObject.Engine is IJavaScriptEngine javaScriptEngine && (javaScriptEngine.BaseLanguageVersion >= 6))
{
var engineInternal = scriptObject.Engine.EngineInternal;
if (engineInternal.InvokeMethod("getAsyncIterator", scriptObject) is ScriptObject asyncIterator)
{
while (await asyncIterator.InvokeMethod("next").ToTask().ConfigureAwait(false) is ScriptObject result && !Equals(result["done"], true))
{
yield return result["value"];
}
}
else if (engineInternal.InvokeMethod("getIterator", scriptObject) is ScriptObject iterator)
{
while (iterator.InvokeMethod("next") is ScriptObject result && !Equals(result["done"], true))
{
yield return result["value"];
}
}
else
{
throw new ArgumentException("The object is not async-iterable", nameof(asyncIterable));
}
}
else
{
throw new NotSupportedException("The script engine does not support async iteration");
}
}
else if (asyncIterable is IEnumerable enumerable)
{
foreach (var item in enumerable)
{
yield return item;
}
}
else
{
throw new ArgumentException("The object is not async-iterable", nameof(asyncIterable));
}
}
}
}