Skip to content

Commit 85b2981

Browse files
author
Peter Huene
authored
Merge pull request #13 from redradist/feature/extend-host-api
Added two methods for loading embedded .wasm and .wat
2 parents a033cca + cea0d9f commit 85b2981

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed

src/Host.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.IO;
33
using System.Text;
44
using System.Collections.Generic;
5-
using System.Runtime.InteropServices;
65

76
namespace Wasmtime
87
{
@@ -593,6 +592,29 @@ public Module LoadModule(string path)
593592
return LoadModule(Path.GetFileNameWithoutExtension(path), File.ReadAllBytes(path));
594593
}
595594

595+
/// <summary>
596+
/// Loads a <see cref="Module"/> given a stream.
597+
/// </summary>
598+
/// <param name="name">The name of the module.</param>
599+
/// <param name="stream">The stream of the module data.</param>
600+
/// <returns>Returns a new <see cref="Module"/>.</returns>
601+
public Module LoadModule(string name, Stream stream)
602+
{
603+
if (string.IsNullOrEmpty(name))
604+
{
605+
throw new ArgumentNullException(nameof(name));
606+
}
607+
608+
if (stream is null)
609+
{
610+
throw new ArgumentNullException(nameof(stream));
611+
}
612+
613+
using var ms = new MemoryStream();
614+
stream.CopyTo(ms);
615+
return LoadModule(name, ms.ToArray());
616+
}
617+
596618
/// <summary>
597619
/// Loads a <see cref="Module"/> based on a WebAssembly text format representation.
598620
/// </summary>
@@ -646,6 +668,28 @@ public Module LoadModuleText(string path)
646668
return LoadModuleText(Path.GetFileNameWithoutExtension(path), File.ReadAllText(path));
647669
}
648670

671+
/// <summary>
672+
/// Loads a <see cref="Module"/> given stream as WebAssembly text format stream.
673+
/// </summary>
674+
/// <param name="name">The name of the module.</param>
675+
/// <param name="stream">The stream of the module data.</param>
676+
/// <returns>Returns a new <see cref="Module"/>.</returns>
677+
public Module LoadModuleText(string name, Stream stream)
678+
{
679+
if (string.IsNullOrEmpty(name))
680+
{
681+
throw new ArgumentNullException(nameof(name));
682+
}
683+
684+
if (stream is null)
685+
{
686+
throw new ArgumentNullException(nameof(stream));
687+
}
688+
689+
using var reader = new StreamReader(stream);
690+
return LoadModuleText(name, reader.ReadToEnd());
691+
}
692+
649693
/// <summary>
650694
/// Instantiates a WebAssembly module.
651695
/// </summary>

tests/ModuleLoadTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Reflection;
3+
using FluentAssertions;
4+
using Wasmtime;
5+
using Xunit;
6+
7+
namespace Wasmtime.Tests
8+
{
9+
public class ModuleLoadTests
10+
{
11+
[Fact]
12+
public void ItLoadsModuleFromEmbeddedResource()
13+
{
14+
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("hello.wasm");
15+
stream.Should().NotBeNull();
16+
17+
using var host = new Host();
18+
host.LoadModule("hello.wasm", stream).Should().NotBeNull();
19+
}
20+
21+
[Fact]
22+
public void ItLoadsModuleTextFromEmbeddedResource()
23+
{
24+
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("hello.wat");
25+
stream.Should().NotBeNull();
26+
27+
using var host = new Host();
28+
host.LoadModuleText("hello.wat", stream).Should().NotBeNull();
29+
}
30+
}
31+
}

tests/Modules/hello.wasm

47 Bytes
Binary file not shown.

tests/Modules/hello.wat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(type $t0 (func))
3+
(import "" "hello" (func $.hello (type $t0)))
4+
(func $run
5+
call $.hello
6+
)
7+
(export "run" (func $run))
8+
)

tests/Wasmtime.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
<ItemGroup>
2121
<None Update="Modules/*.wat" CopyToOutputDirectory="PreserveNewest" />
22+
<EmbeddedResource Include="Modules/hello.wat" LogicalName="hello.wat" />
23+
<EmbeddedResource Include="Modules/hello.wasm" LogicalName="hello.wasm" />
2224
</ItemGroup>
2325

2426
</Project>

0 commit comments

Comments
 (0)