From d9766aed26a2a99f858ed8b60df0e02aa5a63b4c Mon Sep 17 00:00:00 2001 From: Ryan Lamansky <13633345+RyanLamansky@users.noreply.github.com> Date: Sun, 23 May 2021 19:45:13 -0500 Subject: [PATCH] Added additional Global constructors for easier usage; minor syntax improvements. --- .../Instructions/GlobalGetTests.cs | 152 +++--------------- WebAssembly.Tests/OpCodeTests.cs | 2 +- WebAssembly/Global.cs | 21 +++ 3 files changed, 46 insertions(+), 129 deletions(-) diff --git a/WebAssembly.Tests/Instructions/GlobalGetTests.cs b/WebAssembly.Tests/Instructions/GlobalGetTests.cs index ae2a014a..bed38cbf 100644 --- a/WebAssembly.Tests/Instructions/GlobalGetTests.cs +++ b/WebAssembly.Tests/Instructions/GlobalGetTests.cs @@ -45,9 +45,7 @@ public void GetGlobal_Immutable_Compiled() var module = new Module(); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Int32, @@ -55,9 +53,7 @@ public void GetGlobal_Immutable_Compiled() }); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Int64, @@ -65,9 +61,7 @@ public void GetGlobal_Immutable_Compiled() }); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Float32, @@ -75,118 +69,28 @@ public void GetGlobal_Immutable_Compiled() }); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Float64, } }); - module.Functions.Add(new Function - { - Type = 0, - }); - module.Functions.Add(new Function - { - Type = 1, - }); - module.Functions.Add(new Function - { - Type = 2, - }); - module.Functions.Add(new Function - { - Type = 3, - }); - module.Globals.Add(new Global - { - ContentType = WebAssemblyValueType.Int32, - InitializerExpression = new Instruction[] - { - new Int32Constant(4), - new End(), - }, - }); - module.Globals.Add(new Global - { - ContentType = WebAssemblyValueType.Int64, - InitializerExpression = new Instruction[] - { - new Int64Constant(5), - new End(), - }, - }); - module.Globals.Add(new Global - { - ContentType = WebAssemblyValueType.Float32, - InitializerExpression = new Instruction[] - { - new Float32Constant(6), - new End(), - }, - }); - module.Globals.Add(new Global - { - ContentType = WebAssemblyValueType.Float64, - InitializerExpression = new Instruction[] - { - new Float64Constant(7), - new End(), - }, - }); - module.Exports.Add(new Export - { - Index = 0, - Name = nameof(TestBase.TestInt32) - }); - module.Exports.Add(new Export - { - Index = 1, - Name = nameof(TestBase.TestInt64) - }); - module.Exports.Add(new Export - { - Index = 2, - Name = nameof(TestBase.TestFloat32) - }); - module.Exports.Add(new Export - { - Index = 3, - Name = nameof(TestBase.TestFloat64) - }); - module.Codes.Add(new FunctionBody - { - Code = new Instruction[] - { - new GlobalGet(0), - new End(), - }, - }); - module.Codes.Add(new FunctionBody - { - Code = new Instruction[] - { - new GlobalGet(1), - new End(), - }, - }); - module.Codes.Add(new FunctionBody - { - Code = new Instruction[] - { - new GlobalGet(2), - new End(), - }, - }); - module.Codes.Add(new FunctionBody - { - Code = new Instruction[] - { - new GlobalGet(3), - new End(), - }, - }); + module.Functions.Add(new Function(0)); + module.Functions.Add(new Function(1)); + module.Functions.Add(new Function(2)); + module.Functions.Add(new Function(3)); + module.Globals.Add(new Global(WebAssemblyValueType.Int32, new Int32Constant(4), new End())); + module.Globals.Add(new Global(WebAssemblyValueType.Int64, new Int64Constant(5), new End())); + module.Globals.Add(new Global(WebAssemblyValueType.Float32, new Float32Constant(6), new End())); + module.Globals.Add(new Global(WebAssemblyValueType.Float64, new Float64Constant(7), new End())); + module.Exports.Add(new Export(nameof(TestBase.TestInt32), 0)); + module.Exports.Add(new Export(nameof(TestBase.TestInt64), 1)); + module.Exports.Add(new Export(nameof(TestBase.TestFloat32), 2)); + module.Exports.Add(new Export(nameof(TestBase.TestFloat64), 3)); + module.Codes.Add(new FunctionBody(new GlobalGet(0), new End())); + module.Codes.Add(new FunctionBody(new GlobalGet(1), new End())); + module.Codes.Add(new FunctionBody(new GlobalGet(2), new End())); + module.Codes.Add(new FunctionBody(new GlobalGet(3), new End())); var compiled = module.ToInstance(); @@ -206,9 +110,7 @@ public void GetGlobal_Mutable_Compiled() var module = new Module(); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Int32, @@ -216,9 +118,7 @@ public void GetGlobal_Mutable_Compiled() }); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Int64, @@ -226,9 +126,7 @@ public void GetGlobal_Mutable_Compiled() }); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Float32, @@ -236,9 +134,7 @@ public void GetGlobal_Mutable_Compiled() }); module.Types.Add(new WebAssemblyType { - Parameters = new WebAssemblyValueType[] - { - }, + Parameters = Array.Empty(), Returns = new[] { WebAssemblyValueType.Float64, diff --git a/WebAssembly.Tests/OpCodeTests.cs b/WebAssembly.Tests/OpCodeTests.cs index b7fc43ec..6a46b636 100644 --- a/WebAssembly.Tests/OpCodeTests.cs +++ b/WebAssembly.Tests/OpCodeTests.cs @@ -90,7 +90,7 @@ public void OpCode_NameMatchesCharacteristics() continue; } - expectedName.Append(char.ToUpper(part[0])).Append(part.Substring(1)); + expectedName.Append(char.ToUpper(part[0])).Append(part[1..]); } Assert.AreEqual(expectedName.ToString(), opCode.ToString()); diff --git a/WebAssembly/Global.cs b/WebAssembly/Global.cs index fa481fe0..f3ed7e66 100644 --- a/WebAssembly/Global.cs +++ b/WebAssembly/Global.cs @@ -40,6 +40,27 @@ public Global() { } + + /// + /// Creates a new instance with the provided content type. + /// + /// The value. + public Global(WebAssemblyValueType contentType) + { + this.ContentType = contentType; + } + + /// + /// Creates a new instance with the provided content type and initializer expression. + /// + /// The value. + /// The value. + public Global(WebAssemblyValueType contentType, params Instruction[] initializerExpression) + { + this.ContentType = contentType; + this.initializerExpression = initializerExpression; + } + internal Global(Reader reader) { this.ContentType = (WebAssemblyValueType)reader.ReadVarInt7();