Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Wasmtime
{
Expand Down Expand Up @@ -593,6 +592,29 @@ public Module LoadModule(string path)
return LoadModule(Path.GetFileNameWithoutExtension(path), File.ReadAllBytes(path));
}

/// <summary>
/// Loads a <see cref="Module"/> given a stream.
/// </summary>
/// <param name="name">The name of the module.</param>
/// <param name="stream">The stream of the module data.</param>
/// <returns>Returns a new <see cref="Module"/>.</returns>
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());
}

/// <summary>
/// Loads a <see cref="Module"/> based on a WebAssembly text format representation.
/// </summary>
Expand Down Expand Up @@ -646,6 +668,28 @@ public Module LoadModuleText(string path)
return LoadModuleText(Path.GetFileNameWithoutExtension(path), File.ReadAllText(path));
}

/// <summary>
/// Loads a <see cref="Module"/> given stream as WebAssembly text format stream.
/// </summary>
/// <param name="name">The name of the module.</param>
/// <param name="stream">The stream of the module data.</param>
/// <returns>Returns a new <see cref="Module"/>.</returns>
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());
}

/// <summary>
/// Instantiates a WebAssembly module.
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions tests/ModuleLoadTests.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Binary file added tests/Modules/hello.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/Modules/hello.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(module
(type $t0 (func))
(import "" "hello" (func $.hello (type $t0)))
(func $run
call $.hello
)
(export "run" (func $run))
)
2 changes: 2 additions & 0 deletions tests/Wasmtime.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

<ItemGroup>
<None Update="Modules/*.wat" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="Modules/hello.wat" LogicalName="hello.wat" />
<EmbeddedResource Include="Modules/hello.wasm" LogicalName="hello.wasm" />
</ItemGroup>

</Project>