Skip to content

Commit

Permalink
Added additional Global constructors for easier usage; minor syntax i…
Browse files Browse the repository at this point in the history
…mprovements.
  • Loading branch information
RyanLamansky committed May 24, 2021
1 parent 70fff69 commit d9766ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 129 deletions.
152 changes: 24 additions & 128 deletions WebAssembly.Tests/Instructions/GlobalGetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,148 +45,52 @@ public void GetGlobal_Immutable_Compiled()
var module = new Module();
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
Returns = new[]
{
WebAssemblyValueType.Int32,
}
});
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
Returns = new[]
{
WebAssemblyValueType.Int64,
}
});
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
Returns = new[]
{
WebAssemblyValueType.Float32,
}
});
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
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<TestBase>();

Expand All @@ -206,39 +110,31 @@ public void GetGlobal_Mutable_Compiled()
var module = new Module();
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
Returns = new[]
{
WebAssemblyValueType.Int32,
}
});
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
Returns = new[]
{
WebAssemblyValueType.Int64,
}
});
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
Returns = new[]
{
WebAssemblyValueType.Float32,
}
});
module.Types.Add(new WebAssemblyType
{
Parameters = new WebAssemblyValueType[]
{
},
Parameters = Array.Empty<WebAssemblyValueType>(),
Returns = new[]
{
WebAssemblyValueType.Float64,
Expand Down
2 changes: 1 addition & 1 deletion WebAssembly.Tests/OpCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
21 changes: 21 additions & 0 deletions WebAssembly/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ public Global()
{
}


/// <summary>
/// Creates a new <see cref="Global"/> instance with the provided content type.
/// </summary>
/// <param name="contentType">The <see cref="ContentType"/> value.</param>
public Global(WebAssemblyValueType contentType)
{
this.ContentType = contentType;
}

/// <summary>
/// Creates a new <see cref="Global"/> instance with the provided content type and initializer expression.
/// </summary>
/// <param name="contentType">The <see cref="ContentType"/> value.</param>
/// <param name="initializerExpression">The <see cref="InitializerExpression"/> value.</param>
public Global(WebAssemblyValueType contentType, params Instruction[] initializerExpression)
{
this.ContentType = contentType;
this.initializerExpression = initializerExpression;
}

internal Global(Reader reader)
{
this.ContentType = (WebAssemblyValueType)reader.ReadVarInt7();
Expand Down

0 comments on commit d9766ae

Please sign in to comment.