Skip to content

Commit

Permalink
Merge pull request #36 from munik/fix-global-read
Browse files Browse the repository at this point in the history
Fix for "WebAssemblyValueType 7 not recognized" (#35)
  • Loading branch information
RyanLamansky authored May 23, 2021
2 parents 7338583 + 2ea4961 commit 70fff69
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
58 changes: 58 additions & 0 deletions WebAssembly.Tests/CompilerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using WebAssembly.Instructions;
Expand Down Expand Up @@ -639,5 +642,60 @@ public void Compiler_MemoryImportExport()
Assert.IsNotNull(roundMemory);
Assert.AreSame(memory, roundMemory);
}

/// <summary>
/// Verifies that during compilation we read the number of globals
/// exactly as defined in the global section, even when there is a
/// global import.
/// See https://github.com/RyanLamansky/dotnet-webassembly/issues/35
/// </summary>
[TestMethod]
public void Compiler_ReadGlobalSectionWhenGlobalImport()
{
// Set up a module with a global import and a global
var module = new Module();
module.Imports.Add(new WebAssembly.Import.Global("mod", "field"));
module.Globals.Add(new Global
{
ContentType = WebAssemblyValueType.Int32,
IsMutable = false,
InitializerExpression = new Instruction[] { new Int32Constant(1), new End() }.ToList()
});
module.Types.Add(new WebAssemblyType
{
Returns = new[]
{
WebAssemblyValueType.Int32,
},
});
module.Functions.Add(new Function { Type = 0 });
module.Codes.Add(new FunctionBody
{
Code = new Instruction[]
{
new GlobalGet(0),
new GlobalGet(1),
new Int32Add(),
new End(),
},
});
module.Exports.Add(new Export
{
Kind = ExternalKind.Function,
Index = 0,
Name = "fn",
});

using var memoryStream = new MemoryStream();
module.WriteToBinary(memoryStream);
memoryStream.Seek(0L, SeekOrigin.Begin);

var compilationResult = Compile.FromBinary<dynamic>(memoryStream);
var result = compilationResult.Invoke(new Dictionary<string, IDictionary<string, RuntimeImport>>
{
{ "mod", new Dictionary<string, RuntimeImport> { { "field", new GlobalImport(() => 2) } } },
}).Exports.fn();
Assert.AreEqual(3, result);
}
}
}
2 changes: 1 addition & 1 deletion WebAssembly/Runtime/Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ void CreateMemoryField()

var emptySignature = Signature.Empty;

for (var i = 0; i < globals.Length; i++)
for (var i = 0; i < count; i++)
{
var contentType = (WebAssemblyValueType)reader.ReadVarInt7();
var isMutable = reader.ReadVarUInt1() == 1;
Expand Down

0 comments on commit 70fff69

Please sign in to comment.