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);
        }
        public void Compile_Empty()
        {
            var module = new Module();

            using (var memory = new MemoryStream())
            {
                module.WriteToBinary(memory);
                memory.Position = 0;

                var result = Compile.FromBinary <object>(memory)();
                Assert.IsNotNull(result);
            }
        }
        public void Execute_Sample_Issue7()
        {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WebAssembly.Samples.Issue7.wasm"))
            {
                var compiled = Compile.FromBinary <dynamic>(stream,
                                                            new RuntimeImport[] {
                    new FunctionImport("env", "sayc", typeof(SampleTests).GetTypeInfo().GetMethod(nameof(Issue7Receive)))
                })();
                Assert.AreEqual <int>(0, compiled.Exports.main());
            }

            Assert.AreEqual("Hello World (from WASM)\n", issue7Received.ToString());
        }
Beispiel #4
0
        public static TExport CreateInstance <TExport>(string name, ValueType? @return, IList <ValueType> parameters, params Instruction[] code)
            where TExport : class
        {
            Assert.IsNotNull(name);
            Assert.IsNotNull(parameters);
            Assert.IsNotNull(code);

            var module = new Module();

            module.Types.Add(new Type
            {
                Returns = @return.HasValue == false
                                ? new ValueType[0]
                                : new[]
                {
                    @return.GetValueOrDefault()
                },
                Parameters = parameters,
            });
            module.Functions.Add(new Function
            {
            });
            module.Exports.Add(new Export
            {
                Name = name
            });
            module.Codes.Add(new FunctionBody
            {
                Code = code
            });

            Instance <TExport> compiled;

            using (var memory = new MemoryStream())
            {
                module.WriteToBinary(memory);
                Assert.AreNotEqual(0, memory.Length);
                memory.Position = 0;

                var maker = Compile.FromBinary <TExport>(memory);
                Assert.IsNotNull(maker);
                compiled = maker();
            }

            Assert.IsNotNull(compiled);
            Assert.IsNotNull(compiled.Exports);

            return(compiled.Exports);
        }
Beispiel #5
0
        public void Execute_Sample_Issue7()
        {
            Assert.AreEqual(0, issue7Received.Length);

            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WebAssembly.Samples.Issue7.wasm"))
            {
                var imports = new ImportDictionary
                {
                    { "env", "sayc", new FunctionImport(new Action <int>(Issue7Receive)) },
                };
                var compiled = Compile.FromBinary <dynamic>(stream)(imports);
                Assert.AreEqual <int>(0, compiled.Exports.main());
            }

            Assert.AreEqual("Hello World (from WASM)\n", issue7Received.ToString());
        }
        public void Compile_HelloWorld_Static()
        {
            var module = new Module();

            module.Types.Add(new Type
            {
                Returns = new[]
                {
                    ValueType.Int32,
                }
            });
            module.Functions.Add(new Function
            {
            });
            module.Exports.Add(new Export
            {
                Name = nameof(HelloWorldExports.Start)
            });
            module.Codes.Add(new FunctionBody
            {
                Code = new Instruction[]
                {
                    new Int32Constant {
                        Value = 3
                    },
                    new End(),
                },
            });

            Instance <HelloWorldExports> compiled;

            using (var memory = new MemoryStream())
            {
                module.WriteToBinary(memory);
                memory.Position = 0;

                compiled = Compile.FromBinary <HelloWorldExports>(memory)();
            }

            var exports = compiled.Exports;

            Assert.AreEqual(3, exports.Start());
        }