-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathValueBox.cs
367 lines (305 loc) · 10.6 KB
/
ValueBox.cs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using System;
namespace Wasmtime
{
/// <summary>
/// Allocation free container for a single value
/// </summary>
public readonly struct ValueBox
{
internal readonly ValueKind Kind;
internal readonly ValueUnion Union;
internal readonly object? ExternRefObject;
internal ValueBox(ValueKind kind, ValueUnion of)
{
if (kind == ValueKind.ExternRef)
{
throw new InvalidOperationException("Must pass in `object?` for an externref ValueBox`");
}
Kind = kind;
Union = of;
ExternRefObject = null;
}
internal ValueBox(object? externref)
{
Kind = ValueKind.ExternRef;
Union = default;
ExternRefObject = externref;
}
internal Value ToValue(ValueKind convertTo)
{
if (convertTo != Kind)
return Value.FromValueBox(ConvertTo(convertTo));
return Value.FromValueBox(this);
}
internal ValueBox ConvertTo(ValueKind convertTo)
{
return (Kind, convertTo) switch
{
(ValueKind.Int32, ValueKind.Int32) => this,
(ValueKind.Int32, ValueKind.Int64) => Convert.ToInt64(Union.i32),
(ValueKind.Int32, ValueKind.Float32) => Convert.ToSingle(Union.i32),
(ValueKind.Int32, ValueKind.Float64) => Convert.ToDouble(Union.i32),
(ValueKind.Int64, ValueKind.Int32) => Convert.ToInt32(Union.i64),
(ValueKind.Int64, ValueKind.Int64) => Convert.ToInt64(Union.i64),
(ValueKind.Int64, ValueKind.Float32) => Convert.ToSingle(Union.i64),
(ValueKind.Int64, ValueKind.Float64) => Convert.ToDouble(Union.i64),
(ValueKind.Float32, ValueKind.Int32) => Convert.ToInt32(Union.f32),
(ValueKind.Float32, ValueKind.Int64) => Convert.ToInt64(Union.f32),
(ValueKind.Float32, ValueKind.Float32) => Convert.ToSingle(Union.f32),
(ValueKind.Float32, ValueKind.Float64) => Convert.ToDouble(Union.f32),
(ValueKind.Float64, ValueKind.Int32) => Convert.ToInt32(Union.f64),
(ValueKind.Float64, ValueKind.Int64) => Convert.ToInt64(Union.f64),
(ValueKind.Float64, ValueKind.Float32) => Convert.ToSingle(Union.f64),
(ValueKind.Float64, ValueKind.Float64) => Convert.ToDouble(Union.f64),
_ => throw new InvalidCastException($"Cannot convert from `{Kind}` to `{convertTo}`")
};
}
/// <summary>
/// "Box" an int without any heap allocations
/// </summary>
/// <param name="value"></param>
public static implicit operator ValueBox(int value)
{
return new ValueBox(ValueKind.Int32, new ValueUnion { i32 = value });
}
/// <summary>
/// "Box" a long without any heap allocations
/// </summary>
/// <param name="value"></param>
public static implicit operator ValueBox(long value)
{
return new ValueBox(ValueKind.Int64, new ValueUnion { i64 = value });
}
/// <summary>
/// "Box" a float without any heap allocations
/// </summary>
/// <param name="value"></param>
public static implicit operator ValueBox(float value)
{
return new ValueBox(ValueKind.Float32, new ValueUnion { f32 = value });
}
/// <summary>
/// "Box" a double without any heap allocations
/// </summary>
/// <param name="value"></param>
public static implicit operator ValueBox(double value)
{
return new ValueBox(ValueKind.Float64, new ValueUnion { f64 = value });
}
/// <summary>
/// "Box" a 16 element vector of bytes without any heap allocations
/// </summary>
/// <param name="value"></param>
public static implicit operator ValueBox(V128 value)
{
var union = new ValueUnion();
unsafe
{
value.CopyTo(union.v128);
}
return new ValueBox(ValueKind.V128, union);
}
/// <summary>
/// "Box" a 16 element byte array without any heap allocations
/// </summary>
/// <param name="value"></param>
public static explicit operator ValueBox(byte[] value)
{
return (ValueBox)(ReadOnlySpan<byte>)value;
}
/// <summary>
/// "Box" a 16 element byte span without any heap allocations
/// </summary>
/// <param name="value"></param>
public static explicit operator ValueBox(ReadOnlySpan<byte> value)
{
if (value.Length != 16)
throw new ArgumentException("expected a 16 byte array for a v128 value", nameof(value));
var union = new ValueUnion();
unsafe
{
value.CopyTo(new Span<byte>(union.v128, 16));
}
return new ValueBox(ValueKind.V128, union);
}
/// <summary>
/// "Box" a function without any heap allocations
/// </summary>
/// <param name="value"></param>
public static implicit operator ValueBox(Function? value)
{
var func = value?.func ?? Function.Null.func;
return new ValueBox(ValueKind.FuncRef, new ValueUnion { funcref = func });
}
/// <summary>
/// "Box" a string without any heap allocations
/// </summary>
/// <param name="value"></param>
public static implicit operator ValueBox(string value)
{
return new ValueBox(value);
}
/// <summary>
/// "Box" an arbitrary reference type without any heap allocations
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static ValueBox AsBox<T>(T? value)
where T : class
{
return new ValueBox(value);
}
internal static IValueBoxConverter<T> Converter<T>()
{
if (typeof(T) == typeof(int))
{
return (IValueBoxConverter<T>)Int32ValueBoxConverter.Instance;
}
if (typeof(T) == typeof(long))
{
return (IValueBoxConverter<T>)Int64ValueBoxConverter.Instance;
}
if (typeof(T) == typeof(float))
{
return (IValueBoxConverter<T>)Float32ValueBoxConverter.Instance;
}
if (typeof(T) == typeof(double))
{
return (IValueBoxConverter<T>)Float64ValueBoxConverter.Instance;
}
if (typeof(T) == typeof(Function))
{
return (IValueBoxConverter<T>)FuncRefValueBoxConverter.Instance;
}
if (typeof(T) == typeof(V128))
{
return (IValueBoxConverter<T>)V128ValueBoxConverter.Instance;
}
if (typeof(T).IsClass)
{
return (IValueBoxConverter<T>)GenericValueBoxConverter<T>.Instance;
}
throw new InvalidOperationException($"Cannot convert type '{typeof(T).Name}' into a WASM parameter type");
}
}
internal interface IValueBoxConverter<T>
{
public ValueBox Box(T value);
public T Unbox(IStore store, ValueBox value);
}
internal class Int32ValueBoxConverter
: IValueBoxConverter<int>
{
public static readonly Int32ValueBoxConverter Instance = new Int32ValueBoxConverter();
private Int32ValueBoxConverter()
{
}
public ValueBox Box(int value)
{
return value;
}
public int Unbox(IStore store, ValueBox value)
{
return value.Union.i32;
}
}
internal class Int64ValueBoxConverter
: IValueBoxConverter<long>
{
public static readonly Int64ValueBoxConverter Instance = new Int64ValueBoxConverter();
private Int64ValueBoxConverter()
{
}
public ValueBox Box(long value)
{
return value;
}
public long Unbox(IStore store, ValueBox value)
{
return value.Union.i64;
}
}
internal class Float32ValueBoxConverter
: IValueBoxConverter<float>
{
public static readonly Float32ValueBoxConverter Instance = new Float32ValueBoxConverter();
private Float32ValueBoxConverter()
{
}
public ValueBox Box(float value)
{
return value;
}
public float Unbox(IStore store, ValueBox value)
{
return value.Union.f32;
}
}
internal class Float64ValueBoxConverter
: IValueBoxConverter<double>
{
public static readonly Float64ValueBoxConverter Instance = new Float64ValueBoxConverter();
private Float64ValueBoxConverter()
{
}
public ValueBox Box(double value)
{
return value;
}
public double Unbox(IStore store, ValueBox value)
{
return value.Union.f64;
}
}
internal class FuncRefValueBoxConverter
: IValueBoxConverter<Function>
{
public static readonly FuncRefValueBoxConverter Instance = new FuncRefValueBoxConverter();
private FuncRefValueBoxConverter()
{
}
public ValueBox Box(Function value)
{
return value;
}
public Function Unbox(IStore store, ValueBox value)
{
return new Function(store, value.Union.funcref);
}
}
internal class V128ValueBoxConverter
: IValueBoxConverter<V128>
{
public static readonly V128ValueBoxConverter Instance = new V128ValueBoxConverter();
private V128ValueBoxConverter()
{
}
public ValueBox Box(V128 value)
{
return value;
}
public V128 Unbox(IStore store, ValueBox value)
{
unsafe
{
return new V128(value.Union.v128);
}
}
}
internal class GenericValueBoxConverter<T>
: IValueBoxConverter<T?>
{
public static readonly GenericValueBoxConverter<T> Instance = new GenericValueBoxConverter<T>();
private GenericValueBoxConverter()
{
}
public ValueBox Box(T? value)
{
return ValueBox.AsBox((object?)value);
}
public T? Unbox(IStore store, ValueBox value)
{
return (T?)value.ExternRefObject;
}
}
}