Skip to content

Commit d9766ae

Browse files
committed
Added additional Global constructors for easier usage; minor syntax improvements.
1 parent 70fff69 commit d9766ae

File tree

3 files changed

+46
-129
lines changed

3 files changed

+46
-129
lines changed

WebAssembly.Tests/Instructions/GlobalGetTests.cs

Lines changed: 24 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -45,148 +45,52 @@ public void GetGlobal_Immutable_Compiled()
4545
var module = new Module();
4646
module.Types.Add(new WebAssemblyType
4747
{
48-
Parameters = new WebAssemblyValueType[]
49-
{
50-
},
48+
Parameters = Array.Empty<WebAssemblyValueType>(),
5149
Returns = new[]
5250
{
5351
WebAssemblyValueType.Int32,
5452
}
5553
});
5654
module.Types.Add(new WebAssemblyType
5755
{
58-
Parameters = new WebAssemblyValueType[]
59-
{
60-
},
56+
Parameters = Array.Empty<WebAssemblyValueType>(),
6157
Returns = new[]
6258
{
6359
WebAssemblyValueType.Int64,
6460
}
6561
});
6662
module.Types.Add(new WebAssemblyType
6763
{
68-
Parameters = new WebAssemblyValueType[]
69-
{
70-
},
64+
Parameters = Array.Empty<WebAssemblyValueType>(),
7165
Returns = new[]
7266
{
7367
WebAssemblyValueType.Float32,
7468
}
7569
});
7670
module.Types.Add(new WebAssemblyType
7771
{
78-
Parameters = new WebAssemblyValueType[]
79-
{
80-
},
72+
Parameters = Array.Empty<WebAssemblyValueType>(),
8173
Returns = new[]
8274
{
8375
WebAssemblyValueType.Float64,
8476
}
8577
});
86-
module.Functions.Add(new Function
87-
{
88-
Type = 0,
89-
});
90-
module.Functions.Add(new Function
91-
{
92-
Type = 1,
93-
});
94-
module.Functions.Add(new Function
95-
{
96-
Type = 2,
97-
});
98-
module.Functions.Add(new Function
99-
{
100-
Type = 3,
101-
});
102-
module.Globals.Add(new Global
103-
{
104-
ContentType = WebAssemblyValueType.Int32,
105-
InitializerExpression = new Instruction[]
106-
{
107-
new Int32Constant(4),
108-
new End(),
109-
},
110-
});
111-
module.Globals.Add(new Global
112-
{
113-
ContentType = WebAssemblyValueType.Int64,
114-
InitializerExpression = new Instruction[]
115-
{
116-
new Int64Constant(5),
117-
new End(),
118-
},
119-
});
120-
module.Globals.Add(new Global
121-
{
122-
ContentType = WebAssemblyValueType.Float32,
123-
InitializerExpression = new Instruction[]
124-
{
125-
new Float32Constant(6),
126-
new End(),
127-
},
128-
});
129-
module.Globals.Add(new Global
130-
{
131-
ContentType = WebAssemblyValueType.Float64,
132-
InitializerExpression = new Instruction[]
133-
{
134-
new Float64Constant(7),
135-
new End(),
136-
},
137-
});
138-
module.Exports.Add(new Export
139-
{
140-
Index = 0,
141-
Name = nameof(TestBase.TestInt32)
142-
});
143-
module.Exports.Add(new Export
144-
{
145-
Index = 1,
146-
Name = nameof(TestBase.TestInt64)
147-
});
148-
module.Exports.Add(new Export
149-
{
150-
Index = 2,
151-
Name = nameof(TestBase.TestFloat32)
152-
});
153-
module.Exports.Add(new Export
154-
{
155-
Index = 3,
156-
Name = nameof(TestBase.TestFloat64)
157-
});
158-
module.Codes.Add(new FunctionBody
159-
{
160-
Code = new Instruction[]
161-
{
162-
new GlobalGet(0),
163-
new End(),
164-
},
165-
});
166-
module.Codes.Add(new FunctionBody
167-
{
168-
Code = new Instruction[]
169-
{
170-
new GlobalGet(1),
171-
new End(),
172-
},
173-
});
174-
module.Codes.Add(new FunctionBody
175-
{
176-
Code = new Instruction[]
177-
{
178-
new GlobalGet(2),
179-
new End(),
180-
},
181-
});
182-
module.Codes.Add(new FunctionBody
183-
{
184-
Code = new Instruction[]
185-
{
186-
new GlobalGet(3),
187-
new End(),
188-
},
189-
});
78+
module.Functions.Add(new Function(0));
79+
module.Functions.Add(new Function(1));
80+
module.Functions.Add(new Function(2));
81+
module.Functions.Add(new Function(3));
82+
module.Globals.Add(new Global(WebAssemblyValueType.Int32, new Int32Constant(4), new End()));
83+
module.Globals.Add(new Global(WebAssemblyValueType.Int64, new Int64Constant(5), new End()));
84+
module.Globals.Add(new Global(WebAssemblyValueType.Float32, new Float32Constant(6), new End()));
85+
module.Globals.Add(new Global(WebAssemblyValueType.Float64, new Float64Constant(7), new End()));
86+
module.Exports.Add(new Export(nameof(TestBase.TestInt32), 0));
87+
module.Exports.Add(new Export(nameof(TestBase.TestInt64), 1));
88+
module.Exports.Add(new Export(nameof(TestBase.TestFloat32), 2));
89+
module.Exports.Add(new Export(nameof(TestBase.TestFloat64), 3));
90+
module.Codes.Add(new FunctionBody(new GlobalGet(0), new End()));
91+
module.Codes.Add(new FunctionBody(new GlobalGet(1), new End()));
92+
module.Codes.Add(new FunctionBody(new GlobalGet(2), new End()));
93+
module.Codes.Add(new FunctionBody(new GlobalGet(3), new End()));
19094

19195
var compiled = module.ToInstance<TestBase>();
19296

@@ -206,39 +110,31 @@ public void GetGlobal_Mutable_Compiled()
206110
var module = new Module();
207111
module.Types.Add(new WebAssemblyType
208112
{
209-
Parameters = new WebAssemblyValueType[]
210-
{
211-
},
113+
Parameters = Array.Empty<WebAssemblyValueType>(),
212114
Returns = new[]
213115
{
214116
WebAssemblyValueType.Int32,
215117
}
216118
});
217119
module.Types.Add(new WebAssemblyType
218120
{
219-
Parameters = new WebAssemblyValueType[]
220-
{
221-
},
121+
Parameters = Array.Empty<WebAssemblyValueType>(),
222122
Returns = new[]
223123
{
224124
WebAssemblyValueType.Int64,
225125
}
226126
});
227127
module.Types.Add(new WebAssemblyType
228128
{
229-
Parameters = new WebAssemblyValueType[]
230-
{
231-
},
129+
Parameters = Array.Empty<WebAssemblyValueType>(),
232130
Returns = new[]
233131
{
234132
WebAssemblyValueType.Float32,
235133
}
236134
});
237135
module.Types.Add(new WebAssemblyType
238136
{
239-
Parameters = new WebAssemblyValueType[]
240-
{
241-
},
137+
Parameters = Array.Empty<WebAssemblyValueType>(),
242138
Returns = new[]
243139
{
244140
WebAssemblyValueType.Float64,

WebAssembly.Tests/OpCodeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void OpCode_NameMatchesCharacteristics()
9090
continue;
9191
}
9292

93-
expectedName.Append(char.ToUpper(part[0])).Append(part.Substring(1));
93+
expectedName.Append(char.ToUpper(part[0])).Append(part[1..]);
9494
}
9595

9696
Assert.AreEqual(expectedName.ToString(), opCode.ToString());

WebAssembly/Global.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ public Global()
4040
{
4141
}
4242

43+
44+
/// <summary>
45+
/// Creates a new <see cref="Global"/> instance with the provided content type.
46+
/// </summary>
47+
/// <param name="contentType">The <see cref="ContentType"/> value.</param>
48+
public Global(WebAssemblyValueType contentType)
49+
{
50+
this.ContentType = contentType;
51+
}
52+
53+
/// <summary>
54+
/// Creates a new <see cref="Global"/> instance with the provided content type and initializer expression.
55+
/// </summary>
56+
/// <param name="contentType">The <see cref="ContentType"/> value.</param>
57+
/// <param name="initializerExpression">The <see cref="InitializerExpression"/> value.</param>
58+
public Global(WebAssemblyValueType contentType, params Instruction[] initializerExpression)
59+
{
60+
this.ContentType = contentType;
61+
this.initializerExpression = initializerExpression;
62+
}
63+
4364
internal Global(Reader reader)
4465
{
4566
this.ContentType = (WebAssemblyValueType)reader.ReadVarInt7();

0 commit comments

Comments
 (0)