{"id":19100039,"url":"https://github.com/RyanLamansky/dotnet-webassembly","last_synced_at":"2025-04-18T17:32:36.644Z","repository":{"id":40436728,"uuid":"85406800","full_name":"RyanLamansky/dotnet-webassembly","owner":"RyanLamansky","description":"Create, read, modify, write and execute WebAssembly (WASM) files from .NET-based applications.","archived":false,"fork":false,"pushed_at":"2024-07-14T22:25:50.000Z","size":1688,"stargazers_count":791,"open_issues_count":17,"forks_count":74,"subscribers_count":35,"default_branch":"main","last_synced_at":"2024-11-04T22:08:58.570Z","etag":null,"topics":["csharp","dotnet","dotnet-core","dotnet-standard","wasm","wasm-files","web-assembly","webassembly"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RyanLamansky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-18T14:20:38.000Z","updated_at":"2024-10-12T17:29:27.000Z","dependencies_parsed_at":"2024-07-06T17:00:00.130Z","dependency_job_id":null,"html_url":"https://github.com/RyanLamansky/dotnet-webassembly","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLamansky%2Fdotnet-webassembly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLamansky%2Fdotnet-webassembly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLamansky%2Fdotnet-webassembly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLamansky%2Fdotnet-webassembly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RyanLamansky","download_url":"https://codeload.github.com/RyanLamansky/dotnet-webassembly/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223783091,"owners_count":17201903,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","dotnet-core","dotnet-standard","wasm","wasm-files","web-assembly","webassembly"],"created_at":"2024-11-09T03:52:26.733Z","updated_at":"2024-11-09T03:53:00.689Z","avatar_url":"https://github.com/RyanLamansky.png","language":"C#","funding_links":[],"categories":["dotnet","csharp"],"sub_categories":[],"readme":"﻿# WebAssembly for .NET\n[![NuGet](https://img.shields.io/nuget/v/WebAssembly.svg)](https://www.nuget.org/packages/WebAssembly)\n\n\u003e [!WARNING]\n\u003e Only WebAssembly 1.0 is supported!\n\u003e Most WASM files target a higher version and will encounter errors if you try to load them with WebAssembly for .NET.\n\nA library able to create, read, modify, write and execute WebAssembly (WASM) files from .NET-based applications.\n*Execution does not use an interpreter or a 3rd party library:*\nWASM instructions are mapped to their .NET equivalents and converted to native machine language by the .NET JIT compiler.\n\nAvailable on NuGet at https://www.nuget.org/packages/WebAssembly .\n\n## Getting Started\n\n- Use the `WebAssembly.Module` class to create, read, modify, and write WebAssembly (WASM) binary files.\n  - `Module.ReadFromBinary` reads a stream into an instance, which can then be inspected and modified through its properties.\n    - Most WASM files use post-1.0 features and will experience errors when you try to load them.\n  - `WriteToBinary` on a module instance writes binary WASM to the provided stream.\n- Use the `WebAssembly.Runtime.Compile` class to execute WebAssembly (WASM) binary files using the .NET JIT compiler.\n  - Most WASM files have many imports and exports--you'll need to cover these yourself.\n  - This should work for most WASM 1.0 files, but spec compliance is not perfect.\n  - This will not work for any newer-than-1.0 files\n\nYou're welcome to report a bug if you can share a WASM file that has a problem, but no one is actively working on this project so a fix may not come.\n\n## Sample: Create and execute a WebAssembly file in memory\n\n``` C#\nusing System;\nusing WebAssembly; // Acquire from https://www.nuget.org/packages/WebAssembly\nusing WebAssembly.Instructions;\nusing WebAssembly.Runtime;\n\n// Module can be used to create, read, modify, and write WebAssembly files.\nvar module = new Module(); // In this case, we're creating a new one.\n\n// Types are function signatures: the list of parameters and returns.\nmodule.Types.Add(new WebAssemblyType // The first added type gets index 0.\n{\n    Parameters =\n    [\n        WebAssemblyValueType.Int32, // This sample takes a single Int32 as input.\n        // Complex types can be passed by sending them in pieces.\n    ],\n    Returns =\n    [\n        // Multiple returns are supported by the binary format.\n        // Standard currently allows a count of 0 or 1, though.\n        WebAssemblyValueType.Int32,\n    ],\n});\n// Types can be re-used for multiple functions to reduce WASM size.\n\n// The function list associates a function index to a type index.\nmodule.Functions.Add(new Function // The first added function gets index 0.\n{\n    Type = 0, // The index for the \"type\" value added above.\n});\n\n// Code must be passed in the exact same order as the Functions above.\nmodule.Codes.Add(new FunctionBody\n{\n    Code =\n    [\n        new LocalGet(0), // The parameters are the first locals, in order.\n        // We defined the first parameter as Int32, so now an Int32 is at the top of the stack.\n        new Int32CountOneBits(), // Returns the count of binary bits set to 1.\n        // It takes the Int32 from the top of the stack, and pushes the return value.\n        // So, in the end, there is still a single Int32 on the stack.\n        new End(), // All functions must end with \"End\".\n        // The final \"End\" also delivers the returned value.\n    ],\n});\n\n// Exports enable features to be accessed by external code.\n// Typically this means JavaScript, but this library adds .NET execution capability, too.\nmodule.Exports.Add(new Export\n{\n    Kind = ExternalKind.Function,\n    Index = 0, // This should match the function index from above.\n    Name = \"Demo\", // Anything legal in Unicode is legal in an export name.\n});\n\n// We now have enough for a usable WASM file, which we could save with module.WriteToBinary().\n// Below, we show how the Compile feature can be used for .NET-based execution.\n// For stream-based compilation, WebAssembly.Compile should be used.\nvar instanceCreator = module.Compile\u003cSample\u003e(); // Sample is defined later.\n\n// Instances should be wrapped in a \"using\" block for automatic disposal.\n// This sample doesn't import anything, so we pass an empty import dictionary.\nusing (var instance = instanceCreator(new ImportDictionary()))\n{\n    // FYI, instanceCreator can be used multiple times to create independent instances.\n    Console.WriteLine(instance.Exports.Demo(0)); // Binary 0, result 0\n    Console.WriteLine(instance.Exports.Demo(1)); // Binary 1, result 1,\n    Console.WriteLine(instance.Exports.Demo(42));  // Binary 101010, result 3\n} // Automatically release the WebAssembly instance here.\n\npublic abstract class Sample\n{\n    // Sometimes you can use C# dynamic instead of building an abstract class like this.\n    public abstract int Demo(int value);\n}\n\n```\n\n## Other Information\n\n* [Breaking Change Log](docs/BreakingChanges.md)\n* [Examples](Examples)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRyanLamansky%2Fdotnet-webassembly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRyanLamansky%2Fdotnet-webassembly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRyanLamansky%2Fdotnet-webassembly/lists"}