-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathJSArray.cs
More file actions
282 lines (238 loc) · 9.49 KB
/
JSArray.cs
File metadata and controls
282 lines (238 loc) · 9.49 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.JavaScript.NodeApi;
public readonly partial struct JSArray : IJSValue<JSArray>, IList<JSValue>
{
private readonly JSValue _value;
/// <summary>
/// Implicitly converts a <see cref="JSArray" /> to a <see cref="JSValue" />.
/// </summary>
/// <param name="value">The <see cref="JSArray" /> to convert.</param>
public static implicit operator JSValue(JSArray arr) => arr._value;
/// <summary>
/// Explicitly converts a <see cref="JSValue" /> to a nullable <see cref="JSArray" />.
/// </summary>
/// <param name="value">The <see cref="JSValue" /> to convert.</param>
/// <returns>
/// The <see cref="JSArray" /> if it was successfully created or `null` if it was failed.
/// </returns>
public static explicit operator JSArray?(JSValue value) => value.As<JSArray>();
/// <summary>
/// Explicitly converts a <see cref="JSValue" /> to a <see cref="JSArray" />.
/// </summary>
/// <param name="value">The <see cref="JSValue" /> to convert.</param>
/// <returns><see cref="JSArray" /> struct created based on this `JSValue`.</returns>
/// <exception cref="InvalidCastException">
/// Thrown when the T struct cannot be created based on this `JSValue`.
/// </exception>
public static explicit operator JSArray(JSValue value) => value.CastTo<JSArray>();
public static explicit operator JSArray(JSObject obj) => (JSArray)(JSValue)obj;
public static implicit operator JSObject(JSArray arr) => (JSObject)arr._value;
public static explicit operator JSArray(JSIterable obj) => (JSArray)(JSValue)obj;
public static implicit operator JSIterable(JSArray arr) => (JSIterable)arr._value;
private JSArray(JSValue value)
{
_value = value;
}
public JSArray() : this(JSValue.CreateArray())
{
}
public JSArray(int length) : this(JSValue.CreateArray((uint)length))
{
}
public JSArray(JSValue[] array)
{
// Do not specify the length because it will create a sparse array
// that prevents some V8 optimizations.
_value = JSValue.CreateArray();
for (int i = 0; i < array.Length; i++)
{
_value.SetElement(i, array[i]);
}
}
#region IJSValue<JSArray> implementation
/// <summary>
/// Checks if the T struct can be created from this instance`.
/// </summary>
/// <typeparam name="T">A struct that implements IJSValue interface.</typeparam>
/// <returns>
/// `true` if the T struct can be created from this instance. Otherwise it returns `false`.
/// </returns>
public bool Is<T>() where T : struct, IJSValue<T> => _value.Is<T>();
/// <summary>
/// Tries to create a T struct from this instance.
/// It returns `null` if the T struct cannot be created.
/// </summary>
/// <typeparam name="T">A struct that implements IJSValue interface.</typeparam>
/// <returns>
/// Nullable value that contains T struct if it was successfully created
/// or `null` if it was failed.
/// </returns>
public T? As<T>() where T : struct, IJSValue<T> => _value.As<T>();
/// <summary>
/// Creates a T struct from this instance without checking the enclosed handle type.
/// It must be used only when the handle type is known to be correct.
/// </summary>
/// <typeparam name="T">A struct that implements IJSValue interface.</typeparam>
/// <returns>T struct created based on this instance.</returns>
public T AsUnchecked<T>() where T : struct, IJSValue<T> => _value.AsUnchecked<T>();
/// <summary>
/// Creates a T struct from this instance.
/// It throws `InvalidCastException` in case of failure.
/// </summary>
/// <typeparam name="T">A struct that implements IJSValue interface.</typeparam>
/// <returns>T struct created based on this instance.</returns>
/// <exception cref="InvalidCastException">
/// Thrown when the T struct cannot be crated based on this instance.
/// </exception>
public T CastTo<T>() where T : struct, IJSValue<T> => _value.CastTo<T>();
/// <summary>
/// Determines whether a <see cref="JSArray" /> can be created from
/// the specified <see cref="JSValue" />.
/// </summary>
/// <param name="value">The <see cref="JSValue" /> to check.</param>
/// <returns>
/// <c>true</c> if a <see cref="JSArray" /> can be created from
/// the specified <see cref="JSValue" />; otherwise, <c>false</c>.
/// </returns>
#if NET7_0_OR_GREATER
static bool IJSValue<JSArray>.CanCreateFrom(JSValue value)
#else
#pragma warning disable IDE0051 // It is used by the IJSValueShim<T> class through reflection.
private static bool CanCreateFrom(JSValue value)
#pragma warning restore IDE0051
#endif
=> value.IsArray();
/// <summary>
/// Creates a new instance of <see cref="JSArray" /> from
/// the specified <see cref="JSValue" />.
/// </summary>
/// <param name="value">
/// The <see cref="JSValue" /> to create a <see cref="JSArray" /> from.
/// </param>
/// <returns>
/// A new instance of <see cref="JSArray" /> created from
/// the specified <see cref="JSValue" />.
/// </returns>
#if NET7_0_OR_GREATER
static JSArray IJSValue<JSArray>.CreateUnchecked(JSValue value) => new(value);
#else
#pragma warning disable IDE0051 // It is used by the IJSValueShim<T> class through reflection.
private static JSArray CreateUnchecked(JSValue value) => new(value);
#pragma warning restore IDE0051
#endif
#endregion
/// <inheritdoc/>
public int Length => _value.GetArrayLength();
int ICollection<JSValue>.Count => _value.GetArrayLength();
bool ICollection<JSValue>.IsReadOnly => false;
public JSValue this[int index]
{
get => _value.GetElement(index);
set => _value.SetElement(index, value);
}
/// <inheritdoc/>
public void Add(JSValue item) => _value["push"].Call(_value, item);
public void AddRange(IEnumerable<JSValue> items)
{
foreach (JSValue item in items)
{
Add(item);
}
}
public void AddRange<T>(IEnumerable<T> items, JSValue.From<T> toJS)
{
foreach (T item in items)
{
Add(toJS(item));
}
}
/// <inheritdoc/>
public void CopyTo(JSValue[] array, int arrayIndex)
{
int i = arrayIndex;
foreach (JSValue item in this)
{
array[i++] = item;
}
}
/// <summary>
/// Copies array elements to a destination array, converting each element from JS
/// values using a conversion delegate.
/// </summary>
/// <param name="array">Destination array.</param>
/// <param name="arrayIndex">Starting index in the destination array.</param>
/// <param name="fromJS">Delegate that converts from JS value to array element type.</param>
public void CopyTo<T>(T[] array, int arrayIndex, JSValue.To<T> fromJS)
{
int i = arrayIndex;
foreach (JSValue item in this)
{
array[i++] = fromJS(item);
}
}
/// <summary>
/// Copies array elements from a source array, converting each element to JS values
/// using a conversion delegate.
/// </summary>
/// <param name="array">Source array.</param>
/// <param name="arrayIndex">Starting index in the destination array.</param>
/// <param name="toJS">Delegate that converts from array element type to JS value.</param>
public void CopyFrom<T>(T[] array, int arrayIndex, JSValue.From<T> toJS)
{
int i = arrayIndex;
foreach (T item in array)
{
this[i++] = toJS(item);
}
}
/// <inheritdoc/>
public Enumerator GetEnumerator() => new(_value);
IEnumerator<JSValue> IEnumerable<JSValue>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <inheritdoc/>
public int IndexOf(JSValue item) => (int)_value.CallMethod("indexOf", item);
/// <inheritdoc/>
public void Insert(int index, JSValue item) => _value.CallMethod("splice", index, 0, item);
/// <inheritdoc/>
public void RemoveAt(int index) => _value.CallMethod("splice", index, 1);
/// <inheritdoc/>
public void Clear() => _value.CallMethod("splice", 0, Length);
/// <inheritdoc/>
public bool Contains(JSValue item) => IndexOf(item) >= 0;
/// <inheritdoc/>
public bool Remove(JSValue item)
{
int index = IndexOf(item);
if (index < 0) return false;
RemoveAt(index);
return true;
}
/// <summary>
/// Compares two JS values using JS "strict" equality.
/// </summary>
public static bool operator ==(JSArray a, JSArray b) => a._value.StrictEquals(b);
/// <summary>
/// Compares two JS values using JS "strict" equality.
/// </summary>
public static bool operator !=(JSArray a, JSArray b) => !a._value.StrictEquals(b);
/// <summary>
/// Compares two JS values using JS "strict" equality.
/// </summary>
public bool Equals(JSValue other) => _value.StrictEquals(other);
/// <inheritdoc/>
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is JSValue other && Equals(other);
}
/// <inheritdoc/>
public override int GetHashCode()
{
throw new NotSupportedException(
"Hashing JS values is not supported. Use JSSet or JSMap instead.");
}
}