forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableHelpers.NetStandard.cs
More file actions
188 lines (143 loc) · 5.51 KB
/
Copy pathEnumerableHelpers.NetStandard.cs
File metadata and controls
188 lines (143 loc) · 5.51 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
// 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.JavaScript;
namespace Microsoft.ClearScript.Util
{
/// <exclude/>
[BypassCustomAttributeLoader]
[DefaultScriptUsage(ScriptAccess.Full)]
public interface IScriptableAsyncEnumerator<out T> : IAsyncEnumerator<T>
{
/// <exclude/>
T ScriptableCurrent { get; }
/// <exclude/>
object ScriptableMoveNextAsync();
/// <exclude/>
object ScriptableDisposeAsync();
}
internal static partial class EnumerableHelpers
{
public static IScriptableAsyncEnumerator<object> ToScriptableAsyncEnumerator(this IEnumerator enumerator, ScriptEngine engine)
{
return new ScriptableAsyncEnumeratorOnEnumerator(engine, enumerator);
}
public static IScriptableAsyncEnumerator<T> ToScriptableAsyncEnumerator<T>(this IEnumerator<T> enumerator, ScriptEngine engine)
{
return new ScriptableAsyncEnumeratorOnEnumerator<T>(engine, enumerator);
}
public static IScriptableAsyncEnumerator<T> ToScriptableAsyncEnumerator<T>(this IAsyncEnumerator<T> enumerator, ScriptEngine engine)
{
return new ScriptableAsyncEnumeratorOnAsyncEnumerator<T>(engine, enumerator);
}
}
internal static partial class ScriptableEnumerableHelpers
{
public static object GetScriptableAsyncEnumerator(IEnumerable source, ScriptEngine engine)
{
return HostItem.Wrap(engine, source.GetEnumerator().ToScriptableAsyncEnumerator(engine), typeof(IScriptableAsyncEnumerator<object>));
}
}
internal static partial class ScriptableEnumerableHelpers<T>
{
public static object GetScriptableAsyncEnumerator(IEnumerable<T> source, ScriptEngine engine)
{
return HostItem.Wrap(engine, source.GetEnumerator().ToScriptableAsyncEnumerator(engine), typeof(IScriptableAsyncEnumerator<T>));
}
public static object GetScriptableAsyncEnumerator(IAsyncEnumerable<T> source, ScriptEngine engine)
{
return HostItem.Wrap(engine, source.GetAsyncEnumerator().ToScriptableAsyncEnumerator(engine), typeof(IScriptableAsyncEnumerator<T>));
}
}
internal abstract class ScriptableAsyncEnumerator<T> : IScriptableAsyncEnumerator<T>
{
private readonly ScriptEngine engine;
protected ScriptableAsyncEnumerator(ScriptEngine engine)
{
this.engine = engine;
}
#region IScriptableAsyncEnumerator<T> implementation
public T ScriptableCurrent => Current;
public object ScriptableMoveNextAsync()
{
return MoveNextAsync().ToPromise(engine);
}
public object ScriptableDisposeAsync()
{
return DisposeAsync().ToPromise(engine);
}
#endregion
#region IAsyncEnumerable<T> implementation
public abstract T Current { get; }
public abstract ValueTask<bool> MoveNextAsync();
#endregion
#region IAsyncDisposable implementation
public abstract ValueTask DisposeAsync();
#endregion
}
internal sealed class ScriptableAsyncEnumeratorOnEnumerator : ScriptableAsyncEnumerator<object>
{
private readonly IEnumerator enumerator;
public ScriptableAsyncEnumeratorOnEnumerator(ScriptEngine engine, IEnumerator enumerator)
: base(engine)
{
this.enumerator = enumerator;
}
#region ScriptableAsyncEnumerator<object> overrides
public override object Current => enumerator.Current;
public override ValueTask<bool> MoveNextAsync()
{
return new ValueTask<bool>(enumerator.MoveNext());
}
public override ValueTask DisposeAsync()
{
(enumerator as IDisposable)?.Dispose();
return default;
}
#endregion
}
internal sealed class ScriptableAsyncEnumeratorOnEnumerator<T> : ScriptableAsyncEnumerator<T>
{
private readonly IEnumerator<T> enumerator;
public ScriptableAsyncEnumeratorOnEnumerator(ScriptEngine engine, IEnumerator<T> enumerator)
: base(engine)
{
this.enumerator = enumerator;
}
#region ScriptableAsyncEnumerator<T> overrides
public override T Current => enumerator.Current;
public override ValueTask<bool> MoveNextAsync()
{
return new ValueTask<bool>(enumerator.MoveNext());
}
public override ValueTask DisposeAsync()
{
enumerator.Dispose();
return default;
}
#endregion
}
internal sealed class ScriptableAsyncEnumeratorOnAsyncEnumerator<T> : ScriptableAsyncEnumerator<T>
{
private readonly IAsyncEnumerator<T> enumerator;
public ScriptableAsyncEnumeratorOnAsyncEnumerator(ScriptEngine engine, IAsyncEnumerator<T> enumerator)
: base(engine)
{
this.enumerator = enumerator;
}
#region ScriptableAsyncEnumerator<T> overrides
public override T Current => enumerator.Current;
public override ValueTask<bool> MoveNextAsync()
{
return enumerator.MoveNextAsync();
}
public override ValueTask DisposeAsync()
{
return enumerator.DisposeAsync();
}
#endregion
}
}