-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathValue.cs
545 lines (456 loc) · 16.4 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
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,
/// <summary>
/// The value is an `anyref`.
/// </summary>
AnyRef,
}
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 AnyRefExtensions
{
public static bool IsNull(this in AnyRef anyref)
{
// This code originates from inline function "wasmtime_anyref_is_null" in `val.h`.
return anyref.store == 0;
}
}
internal static class ExternRefExtensions
{
public static bool IsNull(this in ExternRef externref)
{
// This code originates from inline function "wasmtime_externref_is_null" in `val.h`.
return externref.store == 0;
}
}
internal static class ExternFuncExtensions
{
public static bool IsNull(this in ExternFunc externfunc)
{
// This code originates from inline function "wasmtime_funcref_is_null" in `val.h`.
return externfunc.store == 0;
}
}
internal static class ValueType
{
public static IntPtr FromKind(TableKind kind)
{
return FromKind((ValueKind)kind);
}
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 ValueKind[] ToArray()
{
var arr = new ValueKind[(int)size];
for (int i = 0; i < (int)size; ++i)
{
arr[i] = ValueType.ToKind(data[i]);
}
return arr;
}
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);
}
}
/// <summary>
///
/// </summary>
/// <remarks>
/// <para>
/// When owning the value and you are finished with using it, you must release/unroot
/// it by calling the <see cref="Release(Store)"/> method. After that, the
/// <see cref="Value"/> must no longer be used.
/// </para>
/// <para>
/// Previously, this type implemented the <see cref="IDisposable"/> interface, but since
/// Wasmtime v20.0.0, unrooting the value requires passing a store context, which is why
/// the <see cref="Release(Store)"/> method needs to explicitly be called, passing a
/// <see cref="Store"/>.
/// </para>
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
internal struct Value
{
public void Release(Store store)
{
Native.wasmtime_val_unroot(store.Context.handle, this);
GC.KeepAlive(store);
}
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(Store store, ValueBox box)
{
var value = new Value();
value.kind = box.Kind;
value.of = box.Union;
if (value.kind == ValueKind.ExternRef)
{
value.of.externref = default;
if (box.ExternRefObject is not null)
{
var gcHandle = GCHandle.Alloc(box.ExternRefObject);
try
{
if (!Native.wasmtime_externref_new(
store.Context.handle,
GCHandle.ToIntPtr(gcHandle),
Finalizer,
ref value.of.externref
))
{
throw new WasmtimeException("The host wasn't able to create more GC values at this time.");
}
}
catch
{
gcHandle.Free();
throw;
}
GC.KeepAlive(store);
}
}
return value;
}
public ValueBox ToValueBox(Store store)
{
if (kind != ValueKind.ExternRef)
{
return new ValueBox(kind, of);
}
else
{
return new ValueBox(ResolveExternRef(store));
}
}
public static Value FromObject(Store store, object? o, TableKind kind)
{
return FromObject(store, o, (ValueKind)kind);
}
public static Value FromObject(Store store, 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}.");
value.of.v128 = (V128)o;
break;
case ValueKind.ExternRef:
value.of.externref = default;
if (o is not null)
{
var gcHandle = GCHandle.Alloc(o);
try
{
if (!Native.wasmtime_externref_new(
store.Context.handle,
GCHandle.ToIntPtr(gcHandle),
Value.Finalizer,
ref value.of.externref
))
{
throw new WasmtimeException("The host wasn't able to create more GC values at this time.");
}
}
catch
{
gcHandle.Free();
throw;
}
GC.KeepAlive(store);
}
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(Store 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:
return of.v128;
case ValueKind.ExternRef:
return ResolveExternRef(store);
case ValueKind.FuncRef:
return store.GetCachedExtern(of.funcref);
default:
throw new NotSupportedException("Unsupported value kind.");
}
}
private object? ResolveExternRef(Store store)
{
if (of.externref.IsNull())
{
return null;
}
var data = Native.wasmtime_externref_data(store.Context.handle, of.externref);
if (data == IntPtr.Zero)
{
return null;
}
return GCHandle.FromIntPtr(data).Target;
}
public static class Native
{
public delegate void Finalizer(IntPtr data);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_val_unroot(IntPtr context, in Value val);
[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool wasmtime_externref_new(IntPtr context, IntPtr data, Finalizer? finalizer, ref ExternRef @out);
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_externref_data(IntPtr context, in ExternRef externref);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_externref_unroot(IntPtr context, in ExternRef externref);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_externref_from_raw(IntPtr context, uint raw, out ExternRef @out);
[DllImport(Engine.LibraryName)]
public static extern uint wasmtime_externref_to_raw(IntPtr context, in ExternRef externref);
}
public 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 AnyRef anyref;
[FieldOffset(0)]
public ExternRef externref;
[FieldOffset(0)]
public ExternFunc funcref;
[FieldOffset(0)]
public V128 v128;
}
[StructLayout(LayoutKind.Sequential)]
internal struct AnyRef
{
public ulong store;
private uint __private1;
private uint __private2;
}
[StructLayout(LayoutKind.Sequential)]
internal struct ExternRef
{
public ulong store;
private uint __private1;
private uint __private2;
}
}