Skip to content

Commit 42102b2

Browse files
author
Peter Huene
authored
Merge pull request #22 from yvt/patch-1
Leave the supplied stream open in `LoadModuleText`
2 parents 85b2981 + 350366c commit 42102b2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Host.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,11 @@ public Module LoadModuleText(string name, Stream stream)
686686
throw new ArgumentNullException(nameof(stream));
687687
}
688688

689-
using var reader = new StreamReader(stream);
689+
// Create a `StreamReader` to read a text from the supplied stream. The minimum buffer
690+
// size and other parameters are hard-coded based on the default values used by the
691+
// `StreamReader(Stream)` constructor. Make sure to leave `stream` open by specifying
692+
// `leaveOpen`.
693+
using var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, leaveOpen: true);
690694
return LoadModuleText(name, reader.ReadToEnd());
691695
}
692696

tests/ModuleLoadTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public void ItLoadsModuleFromEmbeddedResource()
1616

1717
using var host = new Host();
1818
host.LoadModule("hello.wasm", stream).Should().NotBeNull();
19+
20+
// `LoadModule` is not supposed to close the supplied stream,
21+
// so the following statement should complete without throwing
22+
// `ObjectDisposedException`
23+
stream.Read(new byte[0], 0, 0);
1924
}
2025

2126
[Fact]
@@ -26,6 +31,11 @@ public void ItLoadsModuleTextFromEmbeddedResource()
2631

2732
using var host = new Host();
2833
host.LoadModuleText("hello.wat", stream).Should().NotBeNull();
34+
35+
// `LoadModuleText` is not supposed to close the supplied stream,
36+
// so the following statement should complete without throwing
37+
// `ObjectDisposedException`
38+
stream.Read(new byte[0], 0, 0);
2939
}
3040
}
3141
}

0 commit comments

Comments
 (0)