public void Compile_TableImport_UndersizedTable() { var module = new Module(); module.Types.Add(new WebAssemblyType { Returns = new[] { WebAssemblyValueType.Int32 }, Parameters = new[] { WebAssemblyValueType.Int32 } }); module.Imports.Add(new Import.Table("Test", "Test", 1)); module.Functions.Add(new Function { }); module.Exports.Add(new Export { Name = "Test", }); module.Elements.Add(new Element(0, 0)); module.Codes.Add(new FunctionBody { Code = new Instruction[] { new LocalGet(0), new End() }, }); var table = new FunctionTable(0, 1); Assert.AreEqual(0u, table.Length); var compiled = module.ToInstance <CompilerTestBase <int> >( new ImportDictionary { { "Test", "Test", table }, }); Assert.AreEqual(1u, table.Length); var rawDelegate = table[0]; Assert.IsNotNull(rawDelegate); Assert.IsInstanceOfType(rawDelegate, typeof(Func <int, int>)); var nativeDelegate = (Func <int, int>)rawDelegate !; Assert.AreEqual(0, nativeDelegate(0)); Assert.AreEqual(5, nativeDelegate(5)); }
public void Compile_TableImport_ExistingFunction() { var module = new Module(); module.Types.Add(new WebAssemblyType { Returns = new[] { WebAssemblyValueType.Int32 }, Parameters = new[] { WebAssemblyValueType.Int32 } }); module.Imports.Add(new Import.Table("Test", "Test", 1)); module.Functions.Add(new Function { }); module.Exports.Add(new Export { Name = "Test", }); module.Codes.Add(new FunctionBody { Code = new Instruction[] { new LocalGet(0), new Int32Constant(0), new CallIndirect(0), new End() }, }); var table = new FunctionTable(1); var calls = 0; table[0] = new Func <int, int>(value => { calls++; return(value + 1); }); var compiled = module.ToInstance <CompilerTestBase <int> >( new ImportDictionary { { "Test", "Test", table }, }); Assert.AreEqual(0, calls); Assert.AreEqual(3, compiled.Exports.Test(2)); Assert.AreEqual(1, calls); }
public void Compile_TableImport_ExportedImport() { var module = new Module(); module.Imports.Add(new Import.Table("Test", "Test", 1)); module.Exports.Add(new Export { Name = nameof(ExportedTable.Table), Kind = ExternalKind.Table, }); var table = new FunctionTable(0); var exportedTable = module.ToInstance <ExportedTable>( new ImportDictionary { { "Test", "Test", table }, }) .Exports .Table; Assert.AreSame(table, exportedTable); }
public void Compile_TableImport_UndersizedTable() { var module = new Module(); module.Types.Add(new Type { Returns = new[] { ValueType.Int32 }, Parameters = new[] { ValueType.Int32 } }); module.Imports.Add(new Import.Table { Module = "Test", Field = "Test", Definition = new Table { ElementType = ElementType.AnyFunction, ResizableLimits = new ResizableLimits(1) } }); module.Functions.Add(new Function { }); module.Exports.Add(new Export { Name = "Test", }); module.Elements.Add(new Element { Elements = new uint[] { 0 }, InitializerExpression = new Instruction[] { new Int32Constant(0), new End(), }, }); module.Codes.Add(new FunctionBody { Code = new Instruction[] { new GetLocal(0), new End() }, }); var table = new FunctionTable(0, 1); Assert.AreEqual(0u, table.Length); var compiled = module.ToInstance <CompilerTestBase <int> >( new ImportDictionary { { "Test", "Test", table }, }); Assert.AreEqual(1u, table.Length); var rawDelegate = table[0]; Assert.IsNotNull(rawDelegate); Assert.IsInstanceOfType(rawDelegate, typeof(Func <int, int>)); var nativeDelegate = (Func <int, int>)rawDelegate; Assert.AreEqual(0, nativeDelegate(0)); Assert.AreEqual(5, nativeDelegate(5)); }