Repository navigation
Expand file tree
/
Copy pathJSObject.cs
More file actions
275 lines (222 loc) · 9.56 KB
/
Copy pathJSObject.cs
File metadata and controls
275 lines (222 loc) · 9.56 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
// 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 JSObject : IJSValue<JSObject>, IDictionary<JSValue, JSValue>
{
private readonly JSValue _value;
/// <summary>
/// Implicitly converts a <see cref="JSObject" /> to a <see cref="JSValue" />.
/// </summary>
/// <param name="value">The <see cref="JSObject" /> to convert.</param>
public static implicit operator JSValue(JSObject obj) => obj._value;
/// <summary>
/// Explicitly converts a <see cref="JSValue" /> to a nullable <see cref="JSObject" />.
/// </summary>
/// <param name="value">The <see cref="JSValue" /> to convert.</param>
/// <returns>
/// The <see cref="JSObject" /> if it was successfully created or `null` if it was failed.
/// </returns>
public static explicit operator JSObject?(JSValue value) => value.As<JSObject>();
/// <summary>
/// Explicitly converts a <see cref="JSValue" /> to a <see cref="JSObject" />.
/// </summary>
/// <param name="value">The <see cref="JSValue" /> to convert.</param>
/// <returns><see cref="JSObject" /> 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 JSObject(JSValue value) => value.CastTo<JSObject>();
private JSObject(JSValue value)
{
_value = value;
}
public JSObject() : this(JSValue.CreateObject())
{
}
#region IJSValue<JSObject> 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="JSObject" /> 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="JSObject" /> can be created from
/// the specified <see cref="JSValue" />; otherwise, <c>false</c>.
/// </returns>
#if NET7_0_OR_GREATER
static bool IJSValue<JSObject>.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.IsObject();
/// <summary>
/// Creates a new instance of <see cref="JSObject" /> from
/// the specified <see cref="JSValue" />.
/// </summary>
/// <param name="value">
/// The <see cref="JSValue" /> to create a <see cref="JSObject" /> from.
/// </param>
/// <returns>
/// A new instance of <see cref="JSObject" /> created from
/// the specified <see cref="JSValue" />.
/// </returns>
#if NET7_0_OR_GREATER
static JSObject IJSValue<JSObject>.CreateUnchecked(JSValue value) => new(value);
#else
#pragma warning disable IDE0051 // It is used by the IJSValueShim<T> class through reflection.
private static JSObject CreateUnchecked(JSValue value) => new(value);
#pragma warning restore IDE0051
#endif
#endregion
public JSObject(IEnumerable<KeyValuePair<JSValue, JSValue>> properties) : this()
{
foreach (KeyValuePair<JSValue, JSValue> property in properties)
{
_value.SetProperty(property.Key, property.Value);
}
}
public JSObject(params KeyValuePair<JSValue, JSValue>[] properties)
: this((IEnumerable<KeyValuePair<JSValue, JSValue>>)properties)
{
}
int ICollection<KeyValuePair<JSValue, JSValue>>.Count
=> _value.GetPropertyNames().GetArrayLength();
bool ICollection<KeyValuePair<JSValue, JSValue>>.IsReadOnly => false;
public void DefineProperties(params JSPropertyDescriptor[] descriptors)
{
_value.DefineProperties(descriptors);
}
public void DefineProperties(IReadOnlyCollection<JSPropertyDescriptor> descriptors)
{
_value.DefineProperties(descriptors);
}
public JSObject Wrap(object target)
{
_value.Wrap(target);
return this;
}
public bool TryUnwrap<T>(out T? target) where T : class
{
if (_value.TryUnwrap() is object unwrapped)
{
target = unwrapped as T;
return true;
}
target = null;
return false;
}
public T Unwrap<T>() where T : class
{
return (T)_value.Unwrap(typeof(T).Name);
}
public JSValue this[JSValue name]
{
get => _value.GetProperty(name);
set => _value.SetProperty(name, value);
}
public JSValue this[string name]
{
get => _value.GetProperty(name);
set => _value.SetProperty(name, value);
}
public JSValue CallMethod(JSValue methodName)
=> _value.GetProperty(methodName).Call(_value);
public JSValue CallMethod(JSValue methodName, JSValue arg0)
=> _value.GetProperty(methodName).Call(_value, arg0);
public JSValue CallMethod(JSValue methodName, JSValue arg0, JSValue arg1)
=> _value.GetProperty(methodName).Call(_value, arg0, arg1);
public JSValue CallMethod(JSValue methodName, JSValue arg0, JSValue arg1, JSValue arg2)
=> _value.GetProperty(methodName).Call(_value, arg0, arg1, arg2);
public JSValue CallMethod(JSValue methodName, params JSValue[] args)
=> _value.GetProperty(methodName).Call(_value, args);
public JSValue CallMethod(JSValue methodName, ReadOnlySpan<JSValue> args)
=> _value.GetProperty(methodName).Call(_value, args);
public void Add(JSValue key, JSValue value) => _value.SetProperty(key, value);
public bool ContainsKey(JSValue key) => _value.HasProperty(key);
public bool Remove(JSValue key) => _value.DeleteProperty(key);
public bool TryGetValue(JSValue key, [MaybeNullWhen(false)] out JSValue value)
{
value = _value.GetProperty(key);
return !value.IsUndefined();
}
public void Add(KeyValuePair<JSValue, JSValue> item) => _value.SetProperty(item.Key, item.Value);
public bool Contains(KeyValuePair<JSValue, JSValue> item) => _value.HasProperty(item.Key);
public void CopyTo(KeyValuePair<JSValue, JSValue>[] array, int arrayIndex)
{
int i = arrayIndex;
foreach (KeyValuePair<JSValue, JSValue> item in this)
{
array[i++] = item;
}
}
public bool Remove(KeyValuePair<JSValue, JSValue> item) => _value.DeleteProperty(item.Key);
public Enumerator GetEnumerator() => new(_value);
public ICollection<JSValue> Keys => (JSArray)_value.GetPropertyNames();
ICollection<JSValue> IDictionary<JSValue, JSValue>.Values => throw new NotSupportedException();
void ICollection<KeyValuePair<JSValue, JSValue>>.Clear() => throw new NotSupportedException();
IEnumerator<KeyValuePair<JSValue, JSValue>> IEnumerable<KeyValuePair<JSValue, JSValue>>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Compares two JS values using JS "strict" equality.
/// </summary>
public static bool operator ==(JSObject a, JSObject b) => a._value.StrictEquals(b);
/// <summary>
/// Compares two JS values using JS "strict" equality.
/// </summary>
public static bool operator !=(JSObject a, JSObject b) => !a._value.StrictEquals(b);
/// <summary>
/// Compares two JS values using JS "strict" equality.
/// </summary>
public bool Equals(JSValue other) => _value.StrictEquals(other);
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is JSValue other && Equals(other);
}
public override int GetHashCode()
{
throw new NotSupportedException(
"Hashing JS values is not supported. Use JSSet or JSMap instead.");
}
}