diff --git a/src/Host.cs b/src/Host.cs index 619c4d70..b93d7de2 100644 --- a/src/Host.cs +++ b/src/Host.cs @@ -2,7 +2,6 @@ using System.IO; using System.Text; using System.Collections.Generic; -using System.Runtime.InteropServices; namespace Wasmtime { @@ -593,6 +592,29 @@ public Module LoadModule(string path) return LoadModule(Path.GetFileNameWithoutExtension(path), File.ReadAllBytes(path)); } + /// + /// Loads a given a stream. + /// + /// The name of the module. + /// The stream of the module data. + /// Returns a new . + public Module LoadModule(string name, Stream stream) + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException(nameof(name)); + } + + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + using var ms = new MemoryStream(); + stream.CopyTo(ms); + return LoadModule(name, ms.ToArray()); + } + /// /// Loads a based on a WebAssembly text format representation. /// @@ -646,6 +668,28 @@ public Module LoadModuleText(string path) return LoadModuleText(Path.GetFileNameWithoutExtension(path), File.ReadAllText(path)); } + /// + /// Loads a given stream as WebAssembly text format stream. + /// + /// The name of the module. + /// The stream of the module data. + /// Returns a new . + public Module LoadModuleText(string name, Stream stream) + { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException(nameof(name)); + } + + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + using var reader = new StreamReader(stream); + return LoadModuleText(name, reader.ReadToEnd()); + } + /// /// Instantiates a WebAssembly module. /// diff --git a/tests/ModuleLoadTests.cs b/tests/ModuleLoadTests.cs new file mode 100644 index 00000000..efebe5f5 --- /dev/null +++ b/tests/ModuleLoadTests.cs @@ -0,0 +1,31 @@ +using System; +using System.Reflection; +using FluentAssertions; +using Wasmtime; +using Xunit; + +namespace Wasmtime.Tests +{ + public class ModuleLoadTests + { + [Fact] + public void ItLoadsModuleFromEmbeddedResource() + { + using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("hello.wasm"); + stream.Should().NotBeNull(); + + using var host = new Host(); + host.LoadModule("hello.wasm", stream).Should().NotBeNull(); + } + + [Fact] + public void ItLoadsModuleTextFromEmbeddedResource() + { + using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("hello.wat"); + stream.Should().NotBeNull(); + + using var host = new Host(); + host.LoadModuleText("hello.wat", stream).Should().NotBeNull(); + } + } +} diff --git a/tests/Modules/hello.wasm b/tests/Modules/hello.wasm new file mode 100644 index 00000000..dbc6d58c Binary files /dev/null and b/tests/Modules/hello.wasm differ diff --git a/tests/Modules/hello.wat b/tests/Modules/hello.wat new file mode 100644 index 00000000..f7fb8df1 --- /dev/null +++ b/tests/Modules/hello.wat @@ -0,0 +1,8 @@ +(module + (type $t0 (func)) + (import "" "hello" (func $.hello (type $t0))) + (func $run + call $.hello + ) + (export "run" (func $run)) +) diff --git a/tests/Wasmtime.Tests.csproj b/tests/Wasmtime.Tests.csproj index ee1f462a..9d82fd3c 100644 --- a/tests/Wasmtime.Tests.csproj +++ b/tests/Wasmtime.Tests.csproj @@ -19,6 +19,8 @@ + +