Skip to content

Commit cea0d9f

Browse files
author
Peter Huene
committed
Minor code cleanup and add test to load module from embedded resource.
A minor cleanup to the new overloads of `LoadModule` and `LoadModuleText`. Added test cases to cover loading a module from an embedded resource stream.
1 parent 4d9678f commit cea0d9f

File tree

6 files changed

+75
-31
lines changed

6 files changed

+75
-31
lines changed

src/Host.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
using System.IO;
33
using System.Text;
44
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Reflection;
7-
using System.Runtime.InteropServices;
85

96
namespace Wasmtime
107
{
@@ -596,16 +593,26 @@ public Module LoadModule(string path)
596593
}
597594

598595
/// <summary>
599-
/// Loads a <see cref="Module"/> given stream as WebAssembly.
596+
/// Loads a <see cref="Module"/> given a stream.
600597
/// </summary>
601-
/// <param name="moduleName">Name of the module</param>
602-
/// <param name="stream">Stream with module data</param>
598+
/// <param name="name">The name of the module.</param>
599+
/// <param name="stream">The stream of the module data.</param>
603600
/// <returns>Returns a new <see cref="Module"/>.</returns>
604-
public Module LoadModule(string moduleName, Stream moduleStream)
601+
public Module LoadModule(string name, Stream stream)
605602
{
606-
byte[] byteArray = new byte[moduleStream.Length];
607-
moduleStream.Read(byteArray);
608-
return LoadModule(moduleName, byteArray);
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());
609616
}
610617

611618
/// <summary>
@@ -664,14 +671,23 @@ public Module LoadModuleText(string path)
664671
/// <summary>
665672
/// Loads a <see cref="Module"/> given stream as WebAssembly text format stream.
666673
/// </summary>
667-
/// <param name="moduleName">Name of the module</param>
668-
/// <param name="stream">WebAssembly text format stream with module data</param>
674+
/// <param name="name">The name of the module.</param>
675+
/// <param name="stream">The stream of the module data.</param>
669676
/// <returns>Returns a new <see cref="Module"/>.</returns>
670-
public Module LoadModuleText(string moduleName, Stream moduleStream)
677+
public Module LoadModuleText(string name, Stream stream)
671678
{
672-
byte[] byteArray = new byte[moduleStream.Length];
673-
moduleStream.Read(byteArray);
674-
return LoadModuleText(moduleName, Encoding.UTF8.GetString(byteArray, 0, byteArray.Length));
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());
675691
}
676692

677693
/// <summary>

tests/Fixtures/ModuleFixture.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ public ModuleFixture()
1313
.WithReferenceTypes(true)
1414
.Build();
1515

16-
var modulePath = Path.Combine("Modules", ModuleFileName);
17-
Module = Host.LoadModuleText(modulePath);
18-
using (FileStream fs = File.OpenRead(modulePath))
19-
{
20-
StreamModule = Host.LoadModuleText(modulePath, fs);
21-
}
16+
Module = Host.LoadModuleText(Path.Combine("Modules", ModuleFileName));
2217
}
2318

2419
public void Dispose()
@@ -29,12 +24,6 @@ public void Dispose()
2924
Module = null;
3025
}
3126

32-
if (!(StreamModule is null))
33-
{
34-
StreamModule.Dispose();
35-
StreamModule = null;
36-
}
37-
3827
if (!(Host is null))
3928
{
4029
Host.Dispose();
@@ -44,7 +33,6 @@ public void Dispose()
4433

4534
public Host Host { get; set; }
4635
public Module Module { get; set; }
47-
public Module StreamModule { get; set; }
4836

4937
protected abstract string ModuleFileName { get; }
5038
}

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
<ItemGroup>
2121
<None Update="Modules/*.wat" CopyToOutputDirectory="PreserveNewest" />
22-
<!-- <EmbeddedResource Update="Modules/*.wat" />-->
22+
<EmbeddedResource Include="Modules/hello.wat" LogicalName="hello.wat" />
23+
<EmbeddedResource Include="Modules/hello.wasm" LogicalName="hello.wasm" />
2324
</ItemGroup>
24-
25+
2526
</Project>

0 commit comments

Comments
 (0)