-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathExport.cs
274 lines (231 loc) · 8.61 KB
/
Export.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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Wasmtime
{
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct ExportTypeArray : IDisposable
{
public UIntPtr size;
public IntPtr* data;
public void Dispose()
{
Native.wasm_exporttype_vec_delete(this);
}
public Export[] ToExportArray()
{
if (size == UIntPtr.Zero)
{
return Array.Empty<Export>();
}
var exports = new Export[(int)this.size];
for (int i = 0; i < (int)this.size; ++i)
{
var exportType = this.data[i];
var externType = Native.wasm_exporttype_type(exportType);
switch ((WasmExternKind)Native.wasm_externtype_kind(externType))
{
case WasmExternKind.Func:
exports[i] = new FunctionExport(exportType, externType);
break;
case WasmExternKind.Global:
exports[i] = new GlobalExport(exportType, externType);
break;
case WasmExternKind.Table:
exports[i] = new TableExport(exportType, externType);
break;
case WasmExternKind.Memory:
exports[i] = new MemoryExport(exportType, externType);
break;
default:
throw new NotSupportedException("Unsupported export extern type.");
}
}
return exports;
}
internal static class Native
{
[DllImport(Engine.LibraryName)]
public static extern void wasm_exporttype_vec_delete(in ExportTypeArray vec);
[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern byte wasm_externtype_kind(IntPtr valueType);
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_exporttype_type(IntPtr exportType);
}
}
/// <summary>
/// Represents an export of a WebAssembly module or instance.
/// </summary>
public abstract class Export
{
internal Export(IntPtr exportType)
{
unsafe
{
var name = Native.wasm_exporttype_name(exportType);
if (name->size == 0)
{
Name = String.Empty;
}
else
{
Name = Extensions.PtrToStringUTF8((IntPtr)name->data, checked((int)name->size));
}
}
}
/// <summary>
/// The name of the export.
/// </summary>
public string Name { get; private set; }
/// <inheritdoc/>
public override string ToString()
{
return Name;
}
private static class Native
{
[DllImport(Engine.LibraryName)]
public static unsafe extern ByteArray* wasm_exporttype_name(IntPtr type);
}
}
/// <summary>
/// Represents a function exported from a WebAssembly module or instance.
/// </summary>
public class FunctionExport : Export
{
internal FunctionExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
unsafe
{
var type = Native.wasm_externtype_as_functype_const(externType);
if (type == IntPtr.Zero)
{
throw new InvalidOperationException();
}
Parameters = (*Function.Native.wasm_functype_params(type)).ToArray();
Results = (*Function.Native.wasm_functype_results(type)).ToArray();
}
}
/// <summary>
/// The parameter of the exported WebAssembly function.
/// </summary>
public IReadOnlyList<ValueKind> Parameters { get; private set; }
/// <summary>
/// The results of the exported WebAssembly function.
/// </summary>
public IReadOnlyList<ValueKind> Results { get; private set; }
internal static class Native
{
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_externtype_as_functype_const(IntPtr type);
}
}
/// <summary>
/// Represents a global variable exported from a WebAssembly module or instance.
/// </summary>
public class GlobalExport : Export
{
internal GlobalExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
var type = Native.wasm_externtype_as_globaltype_const(externType);
if (type == IntPtr.Zero)
{
throw new InvalidOperationException();
}
Kind = ValueType.ToKind(Global.Native.wasm_globaltype_content(type));
Mutability = new Mutability(Global.Native.wasm_globaltype_mutability(type));
}
/// <summary>
/// The kind of value for the global variable.
/// </summary>
public ValueKind Kind { get; private set; }
/// <summary>
/// Gets the mutability of the global.
/// </summary>
public Mutability Mutability { get; private set; }
internal static class Native
{
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_externtype_as_globaltype_const(IntPtr type);
}
}
/// <summary>
/// Represents a memory exported from a WebAssembly module or instance.
/// </summary>
public class MemoryExport : Export
{
internal MemoryExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
var type = Native.wasm_externtype_as_memorytype_const(externType);
if (type == IntPtr.Zero)
{
throw new InvalidOperationException();
}
Minimum = (long)Memory.Native.wasmtime_memorytype_minimum(type);
if (Memory.Native.wasmtime_memorytype_maximum(type, out ulong max))
{
Maximum = (long)max;
}
Is64Bit = Memory.Native.wasmtime_memorytype_is64(type);
}
/// <summary>
/// Gets the minimum memory size (in WebAssembly page units).
/// </summary>
/// <value>The minimum memory size (in WebAssembly page units).</value>
public long Minimum { get; }
/// <summary>
/// Gets the maximum memory size (in WebAssembly page units).
/// </summary>
/// <value>The maximum memory size (in WebAssembly page units), or <c>null</c> if no maximum is specified.</value>
public long? Maximum { get; }
/// <summary>
/// Gets a value that indicates whether this type of memory represents a 64-bit memory.
/// </summary>
/// <value><c>true</c> if this type of memory represents a 64-bit memory, <c>false</c> if it represents a 32-bit memory.</value>
public bool Is64Bit { get; }
private static class Native
{
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_externtype_as_memorytype_const(IntPtr type);
}
}
/// <summary>
/// Represents a table exported from a WebAssembly module or instance.
/// </summary>
public class TableExport : Export
{
internal TableExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
var type = Native.wasm_externtype_as_tabletype_const(externType);
if (type == IntPtr.Zero)
{
throw new InvalidOperationException();
}
Kind = ValueType.ToKind(Table.Native.wasm_tabletype_element(type));
unsafe
{
var limits = Table.Native.wasm_tabletype_limits(type);
Minimum = limits->min;
Maximum = limits->max;
}
}
/// <summary>
/// The value kind of the table.
/// </summary>
public ValueKind Kind { get; private set; }
/// <summary>
/// The minimum number of elements in the table.
/// </summary>
public uint Minimum { get; private set; }
/// <summary>
/// The maximum number of elements in the table.
/// </summary>
public uint Maximum { get; private set; }
internal static class Native
{
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_externtype_as_tabletype_const(IntPtr type);
}
}
}