-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathConfig.cs
474 lines (412 loc) · 18.3 KB
/
Config.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
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
namespace Wasmtime
{
/// <summary>
/// Represents the Wasmtime compiler strategy.
/// </summary>
public enum CompilerStrategy : byte
{
/// <summary>
/// Automatically pick the compiler strategy.
/// </summary>
Auto,
/// <summary>
/// Use the Cranelift compiler.
/// </summary>
Cranelift
}
/// <summary>
/// Represents the Wasmtime optimization level.
/// </summary>
public enum OptimizationLevel : byte
{
/// <summary>
/// Disable optimizations.
/// </summary>
None,
/// <summary>
/// Optimize for speed.
/// </summary>
Speed,
/// <summary>
/// Optimize for speed and size.
/// </summary>
SpeedAndSize
}
/// <summary>
/// Represents the Wasmtime code profiling strategy.
/// </summary>
public enum ProfilingStrategy : byte
{
/// <summary>
/// Disable code profiling.
/// </summary>
None,
/// <summary>
/// Linux "jitdump" profiling.
/// </summary>
JitDump,
/// <summary>
/// VTune code profiling.
/// </summary>
VTune,
/// <summary>
/// Linux "perfmap" profiling.
/// </summary>
PerfMap
}
/// <summary>
/// Represents a configuration used to create <see cref="Engine"/> instances.
/// </summary>
public class Config : IDisposable
{
/// <summary>
/// Creates a new configuration.
/// </summary>
public Config()
{
handle = new Handle(Native.wasm_config_new());
}
/// <summary>
/// Sets whether or not to enable debug information.
/// </summary>
/// <param name="enable">True to enable debug information or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithDebugInfo(bool enable)
{
Native.wasmtime_config_debug_info_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not to enable epoch-based interruption of WebAssembly code.
/// </summary>
/// <param name="enable">True to enable epoch-based interruption or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithEpochInterruption(bool enable)
{
Native.wasmtime_config_epoch_interruption_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not to enable fuel consumption for WebAssembly code.
/// </summary>
/// <param name="enable">True to enable fuel consumption or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithFuelConsumption(bool enable)
{
Native.wasmtime_config_consume_fuel_set(handle, enable);
return this;
}
/// <summary>
/// Sets the maximum WebAssembly stack size.
/// </summary>
/// <param name="size">The maximum WebAssembly stack size, in bytes.</param>
/// <returns>Returns the current config.</returns>
public Config WithMaximumStackSize(int size)
{
if (size < 0)
{
throw new ArgumentException("Stack size cannot be negative.", nameof(size));
}
Native.wasmtime_config_max_wasm_stack_set(handle, (UIntPtr)size);
return this;
}
/// <summary>
/// Sets whether or not enable WebAssembly threads support.
/// </summary>
/// <param name="enable">True to enable WebAssembly threads support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithWasmThreads(bool enable)
{
Native.wasmtime_config_wasm_threads_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not enable WebAssembly reference types support.
/// </summary>
/// <param name="enable">True to enable WebAssembly reference types support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithReferenceTypes(bool enable)
{
Native.wasmtime_config_wasm_reference_types_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not enable WebAssembly SIMD support.
/// </summary>
/// <param name="enable">True to enable WebAssembly SIMD support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithSIMD(bool enable)
{
Native.wasmtime_config_wasm_simd_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not to enable WebAssembly Relaxed SIMD support. New SIMD instructions that may be non-deterministic across different hosts unless deterministic mode is enabled.
/// </summary>
/// <param name="enable">True to enable WebAssembly Relaxed SIMD support or false to disable.</param>
/// <param name="deterministic">True to enable deterministic mode for WebAssembly Relaxed SIMD or false to allow non-deterministic execution.</param>
/// <returns>Returns the current config.</returns>
public Config WithRelaxedSIMD(bool enable, bool deterministic)
{
Native.wasmtime_config_wasm_relaxed_simd_set(handle, enable);
Native.wasmtime_config_wasm_relaxed_simd_deterministic_set(handle, deterministic);
return this;
}
/// <summary>
/// Sets whether or not enable WebAssembly bulk memory support.
/// </summary>
/// <param name="enable">True to enable WebAssembly bulk memory support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithBulkMemory(bool enable)
{
Native.wasmtime_config_wasm_bulk_memory_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not enable WebAssembly multi-value support.
/// </summary>
/// <param name="enable">True to enable WebAssembly multi-value support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithMultiValue(bool enable)
{
Native.wasmtime_config_wasm_multi_value_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not enable WebAssembly multi-memory support.
/// </summary>
/// <param name="enable">True to enable WebAssembly multi-memory support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithMultiMemory(bool enable)
{
Native.wasmtime_config_wasm_multi_memory_set(handle, enable);
return this;
}
/// <summary>
/// Sets whether or not enable WebAssembly memory64 support.
/// </summary>
/// <param name="enable">True to enable WebAssembly memory64 support or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithMemory64(bool enable)
{
Native.wasmtime_config_wasm_memory64_set(handle, enable);
return this;
}
/// <summary>
/// Sets the compiler strategy to use.
/// </summary>
/// <param name="strategy">The compiler strategy to use.</param>
/// <returns>Returns the current config.</returns>
public Config WithCompilerStrategy(CompilerStrategy strategy)
{
if (!Enum.IsDefined(typeof(CompilerStrategy), (byte)strategy))
{
throw new ArgumentOutOfRangeException(nameof(strategy));
}
Native.wasmtime_config_strategy_set(handle, (byte)strategy);
return this;
}
/// <summary>
/// Sets whether or not enable the Cranelift debug verifier.
/// </summary>
/// <param name="enable">True to enable the Cranelift debug verifier or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithCraneliftDebugVerifier(bool enable)
{
Native.wasmtime_config_cranelift_debug_verifier_set(handle, enable);
return this;
}
/// <summary>
/// Configures whether Cranelift should perform a NaN-canonicalization pass.
///
/// When Cranelift is used as a code generation backend this will configure
/// it to replace NaNs with a single canonical value.This is useful for users
/// requiring entirely deterministic WebAssembly computation.
///
/// This is not required by the WebAssembly spec, so it is not enabled by default.
///
/// The default value for this is `false`
/// </summary>
/// <param name="enable">True to enable the Cranelift nan canonicalization or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithCraneliftNaNCanonicalization(bool enable)
{
Native.wasmtime_config_cranelift_nan_canonicalization_set(handle, enable);
return this;
}
/// <summary>
/// Sets the optimization level to use.
/// </summary>
/// <param name="level">The optimization level to use.</param>
/// <returns>Returns the current config.</returns>
public Config WithOptimizationLevel(OptimizationLevel level)
{
if (!Enum.IsDefined(typeof(OptimizationLevel), (byte)level))
{
throw new ArgumentOutOfRangeException(nameof(level));
}
Native.wasmtime_config_cranelift_opt_level_set(handle, (byte)level);
return this;
}
/// <summary>
/// Sets the profiling strategy to use.
/// </summary>
/// <param name="strategy">The profiling strategy to use.</param>
/// <returns>Returns the current config.</returns>
public Config WithProfilingStrategy(ProfilingStrategy strategy)
{
if (!Enum.IsDefined(typeof(ProfilingStrategy), (byte)strategy))
{
throw new ArgumentOutOfRangeException(nameof(strategy));
}
Native.wasmtime_config_profiler_set(handle, (byte)strategy);
return this;
}
/// <summary>
/// Sets the maximum size of static WebAssembly linear memories.
/// </summary>
/// <param name="size">The maximum size of static WebAssembly linear memories, in bytes.</param>
/// <returns>Returns the current config.</returns>
public Config WithStaticMemoryMaximumSize(ulong size)
{
Native.wasmtime_config_static_memory_maximum_size_set(handle, size);
return this;
}
/// <summary>
/// Sets the maximum size of the guard region for static WebAssembly linear memories.
/// </summary>
/// <param name="size">The maximum guard region size for static WebAssembly linear memories, in bytes.</param>
/// <returns>Returns the current config.</returns>
public Config WithStaticMemoryGuardSize(ulong size)
{
Native.wasmtime_config_static_memory_guard_size_set(handle, size);
return this;
}
/// <summary>
/// Sets the maximum size of the guard region for dynamic WebAssembly linear memories.
/// </summary>
/// <param name="size">The maximum guard region size for dynamic WebAssembly linear memories, in bytes.</param>
/// <returns>Returns the current config.</returns>
public Config WithDynamicMemoryGuardSize(ulong size)
{
Native.wasmtime_config_dynamic_memory_guard_size_set(handle, size);
return this;
}
/// <summary>
/// Sets the path to the Wasmtime cache configuration to use.
/// </summary>
/// <remarks>
/// If the path is null, the default Wasmtime cache configuration will be used.
/// </remarks>
/// <param name="path">The path to the cache configuration file to use or null to load the default configuration.</param>
/// <returns>Returns the current config.</returns>
public Config WithCacheConfig(string? path)
{
var error = Native.wasmtime_config_cache_config_load(handle, path);
if (error != IntPtr.Zero)
{
throw WasmtimeException.FromOwnedError(error);
}
return this;
}
/// <summary>
/// Configures whether, when on macOS, Mach ports are used for exception handling
/// instead of traditional Unix-based signal handling.
///
/// This option defaults to true, using Mach ports by default.
/// </summary>
/// <param name="enable">True to enable Mach ports or false to disable.</param>
/// <returns>Returns the current config.</returns>
public Config WithMacosMachPorts(bool enable)
{
Native.wasmtime_config_macos_use_mach_ports(handle, enable);
return this;
}
/// <inheritdoc/>
public void Dispose()
{
handle.Dispose();
}
internal Handle NativeHandle
{
get
{
if (handle.IsInvalid || handle.IsClosed)
{
throw new ObjectDisposedException(typeof(Config).FullName);
}
return handle;
}
}
internal class Handle : SafeHandleZeroOrMinusOneIsInvalid
{
public Handle(IntPtr handle)
: base(true)
{
SetHandle(handle);
}
protected override bool ReleaseHandle()
{
Native.wasm_config_delete(handle);
return true;
}
}
private static class Native
{
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasm_config_new();
[DllImport(Engine.LibraryName)]
public static extern void wasm_config_delete(IntPtr config);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_debug_info_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_epoch_interruption_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_consume_fuel_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_max_wasm_stack_set(Handle config, nuint size);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_threads_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_reference_types_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_simd_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_relaxed_simd_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_relaxed_simd_deterministic_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_bulk_memory_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_multi_value_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_multi_memory_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_wasm_memory64_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_strategy_set(Handle config, byte strategy);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_cranelift_debug_verifier_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_cranelift_nan_canonicalization_set(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_cranelift_opt_level_set(Handle config, byte level);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_profiler_set(Handle config, byte strategy);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_static_memory_maximum_size_set(Handle config, ulong size);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_static_memory_guard_size_set(Handle config, ulong size);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_dynamic_memory_guard_size_set(Handle config, ulong size);
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_config_cache_config_load(Handle config, [MarshalAs(Extensions.LPUTF8Str)] string? path);
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_config_macos_use_mach_ports(Handle config, [MarshalAs(UnmanagedType.I1)] bool enable);
}
private readonly Handle handle;
}
}