-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathValue.cs
432 lines (361 loc) · 12.6 KB
/
Value.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Wasmtime
{
/// <summary>
/// Represents the possible kinds of WebAssembly values.
/// </summary>
public enum ValueKind : byte
{
/// <summary>
/// The value is a 32-bit integer.
/// </summary>
Int32,
/// <summary>
/// The value is a 64-bit integer.
/// </summary>
Int64,
/// <summary>
/// The value is a 32-bit floating point number.
/// </summary>
Float32,
/// <summary>
/// The value is a 64-bit floating point number.
/// </summary>
Float64,
/// <summary>
/// The value is a 128-bit value representing the WebAssembly `v128` type.
/// </summary>
V128,
/// <summary>
/// The value is a function reference.
/// </summary>
FuncRef,
/// <summary>
/// The value is an external reference.
/// </summary>
ExternRef,
}
internal static class ValueKindExtensions
{
public static bool IsAssignableFrom(this ValueKind kind, Type type)
{
return (kind) switch
{
ValueKind.Int32 => type == typeof(int),
ValueKind.Int64 => type == typeof(long),
ValueKind.Float32 => type == typeof(float),
ValueKind.Float64 => type == typeof(double),
ValueKind.V128 => type == typeof(V128),
ValueKind.FuncRef => type == typeof(Function),
ValueKind.ExternRef => type.IsClass,
_ => throw new ArgumentOutOfRangeException(nameof(kind), kind, null)
};
}
}
internal static class ValueType
{
public static IntPtr FromKind(ValueKind kind)
{
switch (kind)
{
case ValueKind.Int32:
case ValueKind.Int64:
case ValueKind.Float32:
case ValueKind.Float64:
case ValueKind.V128:
return Native.wasm_valtype_new((byte)kind);
case ValueKind.ExternRef:
return Native.wasm_valtype_new(128);
case ValueKind.FuncRef:
return Native.wasm_valtype_new(129);
default:
throw new ArgumentException("unsupported value kind");
}
}
public static ValueKind ToKind(IntPtr type)
{
var kind = (ValueKind)Native.wasm_valtype_kind(type);
switch (kind)
{
case ValueKind.Int32:
case ValueKind.Int64:
case ValueKind.Float32:
case ValueKind.Float64:
case ValueKind.V128:
return kind;
case (ValueKind)128:
return ValueKind.ExternRef;
case (ValueKind)129:
return ValueKind.FuncRef;
default:
throw new ArgumentException("unsupported value kind");
}
}
private static class Native
{
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_valtype_new(byte kind);
[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern byte wasm_valtype_kind(IntPtr valueType);
}
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct ValueTypeArray : IDisposable
{
public ValueTypeArray(IReadOnlyList<ValueKind> kinds)
{
Native.wasm_valtype_vec_new_uninitialized(out var vec, (UIntPtr)kinds.Count);
this.size = vec.size;
this.data = vec.data;
for (int i = 0; i < kinds.Count; ++i)
{
this.data[i] = ValueType.FromKind(kinds[i]);
}
}
public List<ValueKind> ToList()
{
var list = new List<ValueKind>((int)size);
for (int i = 0; i < (int)size; ++i)
{
list.Add(ValueType.ToKind(data[i]));
}
return list;
}
public readonly UIntPtr size;
public readonly IntPtr* data;
public void Dispose()
{
Native.wasm_valtype_vec_delete(this);
}
private static class Native
{
[DllImport(Engine.LibraryName)]
public static extern void wasm_valtype_vec_delete(in ValueTypeArray vec);
[DllImport(Engine.LibraryName)]
public static extern void wasm_valtype_vec_new_uninitialized(out ValueTypeArray vec, UIntPtr len);
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct Value : IDisposable
{
/// <inheritdoc/>
public void Dispose()
{
Native.wasmtime_val_delete(this);
}
public static bool TryGetKind(Type type, out ValueKind kind)
{
if (type == typeof(int))
{
kind = ValueKind.Int32;
return true;
}
if (type == typeof(long))
{
kind = ValueKind.Int64;
return true;
}
if (type == typeof(float))
{
kind = ValueKind.Float32;
return true;
}
if (type == typeof(double))
{
kind = ValueKind.Float64;
return true;
}
if (type == typeof(V128))
{
kind = ValueKind.V128;
return true;
}
if (type == typeof(Function))
{
kind = ValueKind.FuncRef;
return true;
}
if (!type.IsValueType)
{
kind = ValueKind.ExternRef;
return true;
}
kind = default;
return false;
}
public static Value FromValueBox(ValueBox box)
{
var value = new Value();
value.kind = box.Kind;
value.of = box.Union;
if (value.kind == ValueKind.ExternRef)
{
value.of.externref = IntPtr.Zero;
if (box.ExternRefObject is not null)
{
value.of.externref = Native.wasmtime_externref_new(
GCHandle.ToIntPtr(GCHandle.Alloc(box.ExternRefObject)),
Finalizer
);
}
}
return value;
}
public ValueBox ToValueBox()
{
if (kind != ValueKind.ExternRef)
{
return new ValueBox(kind, of);
}
else
{
return new ValueBox(ResolveExternRef());
}
}
public static Value FromObject(object? o, ValueKind kind)
{
var value = new Value();
value.kind = kind;
try
{
switch (kind)
{
case ValueKind.Int32:
if (o is null)
throw new WasmtimeException($"The value `null` is not valid for WebAssembly type {kind}.");
value.of.i32 = (int)Convert.ChangeType(o, TypeCode.Int32);
break;
case ValueKind.Int64:
if (o is null)
throw new WasmtimeException($"The value `null` is not valid for WebAssembly type {kind}.");
value.of.i64 = (long)Convert.ChangeType(o, TypeCode.Int64);
break;
case ValueKind.Float32:
if (o is null)
throw new WasmtimeException($"The value `null` is not valid for WebAssembly type {kind}.");
value.of.f32 = (float)Convert.ChangeType(o, TypeCode.Single);
break;
case ValueKind.Float64:
if (o is null)
throw new WasmtimeException($"The value `null` is not valid for WebAssembly type {kind}.");
value.of.f64 = (double)Convert.ChangeType(o, TypeCode.Double);
break;
case ValueKind.V128:
if (o is null)
throw new WasmtimeException($"The value `null` is not valid for WebAssembly type {kind}.");
var bytes = (V128)o;
unsafe
{
bytes.CopyTo(value.of.v128);
}
break;
case ValueKind.ExternRef:
value.of.externref = IntPtr.Zero;
if (!(o is null))
{
value.of.externref = Native.wasmtime_externref_new(
GCHandle.ToIntPtr(GCHandle.Alloc(o)),
Value.Finalizer
);
}
break;
case ValueKind.FuncRef:
switch (o)
{
case null:
value.of.funcref = Function.Null.func;
break;
case Function f:
value.of.funcref = f.func;
break;
default:
throw new ArgumentException("expected a function value", nameof(o));
}
break;
default:
throw new NotSupportedException("Unsupported value type.");
}
}
catch (InvalidCastException ex)
{
throw new WasmtimeException($"The value `{o ?? "null"}` is not valid for WebAssembly type {kind}.", ex);
}
return value;
}
public object? ToObject(IStore store)
{
switch (kind)
{
case ValueKind.Int32:
return of.i32;
case ValueKind.Int64:
return of.i64;
case ValueKind.Float32:
return of.f32;
case ValueKind.Float64:
return of.f64;
case ValueKind.V128:
unsafe
{
fixed (byte* v128 = of.v128)
{
return new V128(v128);
}
}
case ValueKind.ExternRef:
return ResolveExternRef();
case ValueKind.FuncRef:
return new Function(store, of.funcref);
default:
throw new NotSupportedException("Unsupported value kind.");
}
}
private object? ResolveExternRef()
{
if (of.externref == IntPtr.Zero)
{
return null;
}
var data = Native.wasmtime_externref_data(of.externref);
if (data == IntPtr.Zero)
{
return null;
}
return GCHandle.FromIntPtr(data).Target;
}
private static class Native
{
public delegate void Finalizer(IntPtr data);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_val_delete(in Value val);
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_externref_new(IntPtr data, Finalizer? finalizer);
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_externref_data(IntPtr externref);
}
private static readonly Native.Finalizer Finalizer = (p) => GCHandle.FromIntPtr(p).Free();
private ValueKind kind;
private ValueUnion of;
}
[StructLayout(LayoutKind.Explicit)]
internal unsafe struct ValueUnion
{
[FieldOffset(0)]
public int i32;
[FieldOffset(0)]
public long i64;
[FieldOffset(0)]
public float f32;
[FieldOffset(0)]
public double f64;
[FieldOffset(0)]
public ExternFunc funcref;
[FieldOffset(0)]
public IntPtr externref;
[FieldOffset(0)]
public fixed byte v128[16];
}
}