Move Wasmtime for .NET to the Wasmtime repo.

This moves the Wasmtime for .NET implementation to the Wasmtime repo.

Wasmtime for .NET is a binding of the Wasmtime API for use in .NET.
This commit is contained in:
Peter Huene
2019-11-22 17:11:00 -08:00
parent bbe2a797ba
commit 9fdf5bce8e
100 changed files with 6391 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
using System;
using System.Runtime.InteropServices;
namespace Wasmtime.Exports
{
/// <summary>
/// Represents an export of a WebAssembly module.
/// </summary>
public abstract class Export
{
internal Export(IntPtr exportType)
{
unsafe
{
var name = Interop.wasm_exporttype_name(exportType);
Name = Marshal.PtrToStringUTF8((IntPtr)name->data, (int)name->size);
}
}
/// <summary>
/// The name of the export.
/// </summary>
public string Name { get; private set; }
/// <inheritdoc/>
public override string ToString()
{
return Name;
}
}
}

View File

@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
namespace Wasmtime.Exports
{
/// <summary>
/// Represents the exports of a WebAssembly module.
/// </summary>
public class Exports
{
internal Exports(Module module)
{
Interop.wasm_exporttype_vec_t exports;
Interop.wasm_module_exports(module.Handle, out exports);
try
{
var all = new List<Export>((int)exports.size);
var functions = new List<FunctionExport>();
var globals = new List<GlobalExport>();
var tables = new List<TableExport>();
var memories = new List<MemoryExport>();
for (int i = 0; i < (int)exports.size; ++i)
{
unsafe
{
var exportType = exports.data[i];
var externType = Interop.wasm_exporttype_type(exportType);
switch (Interop.wasm_externtype_kind(externType))
{
case Interop.wasm_externkind_t.WASM_EXTERN_FUNC:
var function = new FunctionExport(exportType, externType);
functions.Add(function);
all.Add(function);
break;
case Interop.wasm_externkind_t.WASM_EXTERN_GLOBAL:
var global = new GlobalExport(exportType, externType);
globals.Add(global);
all.Add(global);
break;
case Interop.wasm_externkind_t.WASM_EXTERN_TABLE:
var table = new TableExport(exportType, externType);
tables.Add(table);
all.Add(table);
break;
case Interop.wasm_externkind_t.WASM_EXTERN_MEMORY:
var memory = new MemoryExport(exportType, externType);
memories.Add(memory);
all.Add(memory);
break;
default:
throw new NotSupportedException("Unsupported export extern type.");
}
}
}
Functions = functions;
Globals = globals;
Tables = tables;
Memories = memories;
All = all;
}
finally
{
Interop.wasm_exporttype_vec_delete(ref exports);
}
}
/// <summary>
/// The exported functions of a WebAssembly module.
/// </summary>
public IReadOnlyList<FunctionExport> Functions { get; private set; }
/// <summary>
/// The exported globals of a WebAssembly module.
/// </summary>
public IReadOnlyList<GlobalExport> Globals { get; private set; }
/// <summary>
/// The exported tables of a WebAssembly module.
/// </summary>
public IReadOnlyList<TableExport> Tables { get; private set; }
/// <summary>
/// The exported memories of a WebAssembly module.
/// </summary>
public IReadOnlyList<MemoryExport> Memories { get; private set; }
internal List<Export> All { get; private set; }
}
}

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Wasmtime.Exports
{
/// <summary>
/// Represents a function exported from a WebAssembly module.
/// </summary>
public class FunctionExport : Export
{
internal FunctionExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
Debug.Assert(Interop.wasm_externtype_kind(externType) == Interop.wasm_externkind_t.WASM_EXTERN_FUNC);
unsafe
{
var funcType = Interop.wasm_externtype_as_functype_const(externType);
Parameters = Interop.ToValueKindList(Interop.wasm_functype_params(funcType));
Results = Interop.ToValueKindList(Interop.wasm_functype_results(funcType));
}
}
/// <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; }
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Diagnostics;
namespace Wasmtime.Exports
{
/// <summary>
/// Represents a global variable exported from a WebAssembly module.
/// </summary>
public class GlobalExport : Export
{
internal GlobalExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
Debug.Assert(Interop.wasm_externtype_kind(externType) == Interop.wasm_externkind_t.WASM_EXTERN_GLOBAL);
var globalType = Interop.wasm_externtype_as_globaltype_const(externType);
Kind = Interop.wasm_valtype_kind(Interop.wasm_globaltype_content(globalType));
IsMutable = Interop.wasm_globaltype_mutability(globalType) == Interop.wasm_mutability_t.WASM_VAR;
}
/// <summary>
/// The kind of value for the global variable.
/// </summary>
public ValueKind Kind { get; private set; }
/// <summary>
/// Determines whether or not the global variable is mutable.
/// </summary>
public bool IsMutable { get; private set; }
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Diagnostics;
namespace Wasmtime.Exports
{
/// <summary>
/// Represents a memory exported from a WebAssembly module.
/// </summary>
public class MemoryExport : Export
{
internal MemoryExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
Debug.Assert(Interop.wasm_externtype_kind(externType) == Interop.wasm_externkind_t.WASM_EXTERN_MEMORY);
var memoryType = Interop.wasm_externtype_as_memorytype_const(externType);
unsafe
{
var limits = Interop.wasm_memorytype_limits(memoryType);
Minimum = limits->min;
Maximum = limits->max;
}
}
/// <summary>
/// The minimum memory size (in WebAssembly page units).
/// </summary>
public uint Minimum { get; private set; }
/// <summary>
/// The maximum memory size (in WebAssembly page units).
/// </summary>
public uint Maximum { get; private set; }
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Diagnostics;
namespace Wasmtime.Exports
{
/// <summary>
/// Represents a table exported from a WebAssembly module.
/// </summary>
public class TableExport : Export
{
internal TableExport(IntPtr exportType, IntPtr externType) : base(exportType)
{
Debug.Assert(Interop.wasm_externtype_kind(externType) == Interop.wasm_externkind_t.WASM_EXTERN_TABLE);
var tableType = Interop.wasm_externtype_as_tabletype_const(externType);
Kind = Interop.wasm_valtype_kind(Interop.wasm_tabletype_element(tableType));
unsafe
{
var limits = Interop.wasm_tabletype_limits(tableType);
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; }
}
}