Skip to content

erandis-vol/binaryen-net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 

Repository files navigation

binaryen.NET

This is a .NET binding of binaryen.

Usage

using Binaryen;

namespace Example
{
    static class Program
    {
        static void Main(string[] args)
        {
            // Create a module
            using (var module = new Module())
            {
                // Create the "add" function
                module.AddFunction(
                    "add",
                    module.AddFunctionType("iii", ValueType.Int32, new[] { ValueType.Int32, ValueType.Int32 }),
                    module.Block(null, new[] {
                        module.SetLocal(2,
                            module.Binary(
                                BinaryOperator.AddInt32,
                                module.GetLocal(0, ValueType.Int32),
                                module.GetLocal(1, ValueType.Int32)
                            )
                        ),
                        module.Return(
                            module.GetLocal(2, ValueType.Int32)
                        )
                    })
                );
                
                // Create an export for our function
                module.AddFunctionExport("add", "add");
                
                // Optimize the module using default passes and levls
                module.Optimize();
                
                // Validate the module
                if (!module.Validate())
                {
                    // Validation error
                }
                
                // Generate binary format
                var binary = module.Emit();
                
                // Save it to a file
                System.IO.File.WriteAllBytes("add.wasm", binary.Bytes);
            }
        }
    }
}

Releases

No releases published

Packages

No packages published

Languages