Skip to content

Commit 85f1cda

Browse files
committed
Minor quality improvements.
1 parent c2beb13 commit 85f1cda

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

WebAssembly/Reader.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44

55
namespace WebAssembly;
66

7-
internal sealed class Reader : IDisposable
7+
internal sealed class Reader(Stream input) : IDisposable
88
{
9-
private readonly UTF8Encoding utf8 = new(false, false);
10-
private BinaryReader? reader;
11-
12-
public Reader(Stream input)
13-
{
14-
//The UTF8 encoding parameter is not actually used; it's just there so that the leaveOpen parameter can be reached.
15-
this.reader = new BinaryReader(input, utf8, true);
16-
}
9+
private BinaryReader? reader = new(input, Encoding.UTF8, true);
1710

1811
public long Offset { get; private set; }
1912

@@ -127,7 +120,7 @@ public double ReadFloat64()
127120
public string ReadString(uint length)
128121
{
129122
var bytes = this.ReadBytes(length);
130-
return utf8.GetString(bytes, 0, bytes.Length);
123+
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
131124
}
132125

133126
public byte[] ReadBytes(uint length)

WebAssembly/Runtime/Compile.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ public static InstanceCreator<TExports> FromBinary<TExports>(Stream input, Compi
104104
{
105105
CheckPreamble(reader);
106106

107-
constructor = FromBinary(
108-
AssemblyBuilder.DefineDynamicAssembly(
107+
var assembly = AssemblyBuilder.DefineDynamicAssembly(
109108
new AssemblyName("CompiledWebAssembly"),
110-
AssemblyBuilderAccess.RunAndCollect
111-
),
112-
reader,
109+
AssemblyBuilderAccess.RunAndCollect);
110+
111+
constructor = FromBinary(
112+
assembly,
113+
reader,
113114
configuration,
114115
typeof(Instance<TExports>),
115-
typeof(TExports)
116+
typeof(TExports),
117+
assembly.DefineDynamicModule("CompiledWebAssembly")
116118
);
117119
}
118120
catch (OverflowException x)
@@ -203,11 +205,19 @@ private static void CheckPreamble(Reader reader)
203205

204206
#if NET9_0_OR_GREATER
205207
/// <summary>
206-
/// Creates an <see cref="AssemblyBuilder"/> from a binary WASM source.
208+
/// Creates a <see cref="PersistedAssemblyBuilder"/> from a binary WASM source.
209+
/// </summary>
210+
/// <param name="input">The source of data. The stream is left open after reading is complete.</param>
211+
/// <returns>An assembly builder that can be used to further modify or save the assembly.</returns>
212+
public static PersistedAssemblyBuilder CreatePersistedAssembly(Stream input)
213+
=> CreatePersistedAssembly(input, new());
214+
215+
/// <summary>
216+
/// Creates a <see cref="PersistedAssemblyBuilder"/> from a binary WASM source.
207217
/// </summary>
208218
/// <param name="input">The source of data. The stream is left open after reading is complete.</param>
209219
/// <param name="configuration">Configures the compiler.</param>
210-
/// <returns>The created assembly.</returns>
220+
/// <returns>An assembly builder that can be used to further modify or save the assembly.</returns>
211221
public static PersistedAssemblyBuilder CreatePersistedAssembly(
212222
Stream input,
213223
CompilerConfiguration configuration
@@ -272,7 +282,7 @@ private static ConstructorInfo FromBinary(
272282
CompilerConfiguration configuration,
273283
Type instanceContainer,
274284
Type exportContainer,
275-
ModuleBuilder? module = null,
285+
ModuleBuilder module,
276286
TypeBuilder? importBuilder = null
277287
)
278288
{
@@ -299,8 +309,6 @@ private static ConstructorInfo FromBinary(
299309
KeyValuePair<string, uint>[]? exportedFunctions = null;
300310
var previousSection = Section.None;
301311

302-
module ??= assembly.DefineDynamicModule("CompiledWebAssembly");
303-
304312
var context = new CompilationContext(configuration);
305313
var exportsBuilder = context.CheckedExportsBuilder = module.DefineType("CompiledExports", ClassAttributes, exportContainer);
306314
MethodBuilder? importedMemoryProvider = null;

WebAssembly/WebAssembly.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<AssemblyOriginatorKeyFile>Properties\WebAssembly.snk</AssemblyOriginatorKeyFile>
2323
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
2424
<NoWarn>CA1716;CA1720</NoWarn>
25-
<DocumentationFile>bin\WebAssembly.xml</DocumentationFile>
25+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
2626
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2727
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2828
<EmbedUntrackedSources>true</EmbedUntrackedSources>

0 commit comments

Comments
 (0)