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,34 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Wasmtime.Imports
{
/// <summary>
/// Represents a function imported to a WebAssembly module.
/// </summary>
public class FunctionImport : Import
{
internal FunctionImport(IntPtr importType, IntPtr externType) : base(importType)
{
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 parameters of the imported function.
/// </summary>
public IReadOnlyList<ValueKind> Parameters { get; private set; }
/// <summary>
/// The results of the imported function.
/// </summary>
public IReadOnlyList<ValueKind> Results { get; private set; }
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Diagnostics;
namespace Wasmtime.Imports
{
/// <summary>
/// Represents a global variable imported to a WebAssembly module.
/// </summary>
public class GlobalImport : Import
{
internal GlobalImport(IntPtr importType, IntPtr externType) : base(importType)
{
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,39 @@
using System;
using System.Runtime.InteropServices;
namespace Wasmtime.Imports
{
/// <summary>
/// The base class for import types.
/// </summary>
public abstract class Import
{
internal Import(IntPtr importType)
{
unsafe
{
var moduleName = Interop.wasm_importtype_module(importType);
ModuleName = Marshal.PtrToStringUTF8((IntPtr)moduleName->data, (int)moduleName->size);
var name = Interop.wasm_importtype_name(importType);
Name = Marshal.PtrToStringUTF8((IntPtr)name->data, (int)name->size);
}
}
/// <summary>
/// The module name of the import.
/// </summary>
public string ModuleName { get; private set; }
/// <summary>
/// The name of the import.
/// </summary>
public string Name { get; private set; }
/// <inheritdoc/>
public override string ToString()
{
return $"{ModuleName}{(string.IsNullOrEmpty(ModuleName) ? "" : ".")}{Name}";
}
}
}

View File

@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
namespace Wasmtime.Imports
{
/// <summary>
/// Represents imported functions, globals, tables, and memories to a WebAssembly module.
/// </summary>
public class Imports
{
internal Imports(Module module)
{
Interop.wasm_importtype_vec_t imports;
Interop.wasm_module_imports(module.Handle, out imports);
try
{
var all = new List<Import>((int)imports.size);
var functions = new List<FunctionImport>();
var globals = new List<GlobalImport>();
var tables = new List<TableImport>();
var memories = new List<MemoryImport>();
for (int i = 0; i < (int)imports.size; ++i)
{
unsafe
{
var importType = imports.data[i];
var externType = Interop.wasm_importtype_type(importType);
switch (Interop.wasm_externtype_kind(externType))
{
case Interop.wasm_externkind_t.WASM_EXTERN_FUNC:
var function = new FunctionImport(importType, externType);
functions.Add(function);
all.Add(function);
break;
case Interop.wasm_externkind_t.WASM_EXTERN_GLOBAL:
var global = new GlobalImport(importType, externType);
globals.Add(global);
all.Add(global);
break;
case Interop.wasm_externkind_t.WASM_EXTERN_TABLE:
var table = new TableImport(importType, externType);
tables.Add(table);
all.Add(table);
break;
case Interop.wasm_externkind_t.WASM_EXTERN_MEMORY:
var memory = new MemoryImport(importType, externType);
memories.Add(memory);
all.Add(memory);
break;
default:
throw new NotSupportedException("Unsupported import extern type.");
}
}
}
Functions = functions;
Globals = globals;
Tables = tables;
Memories = memories;
All = all;
}
finally
{
Interop.wasm_importtype_vec_delete(ref imports);
}
}
/// <summary>
/// The imported functions required by a WebAssembly module.
/// </summary>
public IReadOnlyList<FunctionImport> Functions { get; private set; }
/// <summary>
/// The imported globals required by a WebAssembly module.
/// </summary>
public IReadOnlyList<GlobalImport> Globals { get; private set; }
/// <summary>
/// The imported tables required by a WebAssembly module.
/// </summary>
public IReadOnlyList<TableImport> Tables { get; private set; }
/// <summary>
/// The imported memories required by a WebAssembly module.
/// </summary>
public IReadOnlyList<MemoryImport> Memories { get; private set; }
internal IReadOnlyList<Import> All { get; private set; }
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Diagnostics;
namespace Wasmtime.Imports
{
/// <summary>
/// Represents a memory imported to a WebAssembly module.
/// </summary>
public class MemoryImport : Import
{
internal MemoryImport(IntPtr importType, IntPtr externType) : base(importType)
{
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.Imports
{
/// <summary>
/// Represents a table imported to a WebAssembly module.
/// </summary>
public class TableImport : Import
{
internal TableImport(IntPtr importType, IntPtr externType) : base(importType)
{
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; }
}
}