# WebAssembly for .NET
[![NuGet](https://img.shields.io/nuget/v/WebAssembly.svg)](https://www.nuget.org/packages/WebAssembly)
A library able to create, read, modify, write and (incomplete) execute WebAssembly (WASM) files from .NET-based applications.
*Execution does not use an interpreter.*
WASM instructions are mapped to their .NET equivalents and converted to native machine language by the .NET JIT compiler.
Available on NuGet at https://www.nuget.org/packages/WebAssembly .
## Getting Started
- Use the `WebAssembly.Module` class to create, read, modify, and write WebAssembly (WASM) binary files.
- There are no known issues with this functionality and the API is stable.
- Use the `WebAssembly.Runtime.Compile` class to execute WebAssembly (WASM) binary files using the .NET JIT compiler.
- The only significant missing feature is Table Imports; WASM files that do not require this feature are likely to work.
Please file an issue if you encounter an assembly that works in browsers but not with this library.
## Sample: Create and execute a WebAssembly file in memory
``` C#
using System;
using WebAssembly; // Acquire from https://www.nuget.org/packages/WebAssembly
using WebAssembly.Instructions;
using WebAssembly.Runtime;
// We need this later to call the code we're generating.
public abstract class Sample
{
// Sometimes you can use C# dynamic instead of building an abstract class like this.
public abstract int Demo(int value);
}
static class Program
{
static void Main()
{
// Module can be used to create, read, modify, and write WebAssembly files.
var module = new Module(); // In this case, we're creating a new one.
// Types are function signatures: the list of parameters and returns.
module.Types.Add(new WebAssemblyType // The first added type gets index 0.
{
Parameters = new[]
{
WebAssemblyValueType.Int32, // This sample takes a single Int32 as input.
// Complex types can be passed by sending them in pieces.
},
Returns = new[]
{
// Multiple returns are supported by the binary format.
// Standard currently allows a count of 0 or 1, though.
WebAssemblyValueType.Int32,
},
});
// Types can be re-used for multiple functions to reduce WASM size.
// The function list associates a function index to a type index.
module.Functions.Add(new Function // The first added function gets index 0.
{
Type = 0, // The index for the "type" value added above.
});
// Code must be passed in the exact same order as the Functions above.
module.Codes.Add(new FunctionBody
{
Code = new Instruction[]
{
new LocalGet(0), // The parameters are the first locals, in order.
// We defined the first parameter as Int32, so now an Int32 is at the top of the stack.
new Int32CountOneBits(), // Returns the count of binary bits set to 1.
// It takes the Int32 from the top of the stack, and pushes the return value.
// So, in the end, there is still a single Int32 on the stack.
new End(), // All functions must end with "End".
// The final "End" also delivers the returned value.
},
});
// Exports enable features to be accessed by external code.
// Typically this means JavaScript, but this library adds .NET execution capability, too.
module.Exports.Add(new Export
{
Kind = ExternalKind.Function,
Index = 0, // This should match the function index from above.
Name = "Demo", // Anything legal in Unicode is legal in an export name.
});
// We now have enough for a usable WASM file, which we could save with module.WriteToBinary().
// Below, we show how the Compile feature can be used for .NET-based execution.
// For stream-based compilation, WebAssembly.Compile should be used.
var instanceCreator = module.Compile<Sample>();
// Instances should be wrapped in a "using" block for automatic disposal.
// This sample doesn't import anything, so we pass an empty import dictionary.
using (var instance = instanceCreator(new ImportDictionary()))
{
// FYI, instanceCreator can be used multiple times to create independent instances.
Console.WriteLine(instance.Exports.Demo(0)); // Binary 0, result 0
Console.WriteLine(instance.Exports.Demo(1)); // Binary 1, result 1,
Console.WriteLine(instance.Exports.Demo(42)); // Binary 101010, result 3
} // Automatically release the WebAssembly instance here.
}
}
```
## Development Status
### Required for 1.0
- Implement Table Imports.
### After 1.0
- Provide a mechanism to replace the `System.Reflection.Emit.AssemblyBuilder`-affiliated methods with replacements so that something like Mono.Cecil can be used to produce a DLL.
- If https://github.com/dotnet/corefx/issues/4491 is fixed, use it to enable saving DLLs.
- Remove the compiler's Data section segment size limit of 4128768 bytes.
## Other Information
* [Breaking Change Log](docs/BreakingChanges.md)
没有合适的资源?快使用搜索试试~ 我知道了~
dotnet-webassembly:通过基于.NET的应用程序创建,读取,修改,编写和执行WebAssembly(WASM)文...
共2000个文件
wasm:2513个
cs:478个
wat:253个
5星 · 超过95%的资源 需积分: 11 6 下载量 138 浏览量
2021-02-05
03:00:03
上传
评论
收藏 1.49MB ZIP 举报
温馨提示
.NET的WebAssembly 一个能够从基于.NET的应用程序中创建,读取,修改,写入和(不完整)执行WebAssembly(WASM)文件的库。 执行不使用解释器。 WASM指令映射到它们的.NET等效项,并由.NET JIT编译器转换为本机语言。 在NuGet上提供,为 。 入门 使用WebAssembly.Module类创建,读取,修改和写入WebAssembly(WASM)二进制文件。 此功能没有已知问题,API稳定。 使用WebAssembly.Runtime.Compile类使用.NET JIT编译器执行WebAssembly(WASM)二进制文件。 唯一缺少的重要功能是
资源详情
资源评论
资源推荐
收起资源包目录
dotnet-webassembly:通过基于.NET的应用程序创建,读取,修改,编写和执行WebAssembly(WASM)文件 (2000个子文件)
Compile.cs 74KB
SpecTests.cs 39KB
OpCode.cs 32KB
SpecTestRunner.cs 31KB
Module.cs 28KB
Instruction.cs 23KB
CompilerTests.cs 19KB
GlobalSetTests.cs 16KB
GlobalGetTests.cs 14KB
TableImportTests.cs 14KB
CompilationContext.cs 13KB
ModuleTests.cs 11KB
Import.cs 9KB
EndTests.cs 9KB
Int64StoreTests.cs 7KB
Float64StoreTests.cs 7KB
Int64Load32UnsignedTests.cs 7KB
Int64Load32SignedTests.cs 7KB
FunctionImportTests.cs 7KB
Float32LoadTests.cs 7KB
GlobalImport.cs 7KB
MemoryImmediateInstruction.cs 7KB
Float64LoadTests.cs 6KB
FunctionTable.cs 6KB
WebAssemblyType.cs 6KB
Int64Load16UnsignedTests.cs 6KB
Int64Load16SignedTests.cs 6KB
Int64Load8UnsignedTests.cs 6KB
UnmanagedMemory.cs 6KB
CallIndirectTests.cs 6KB
Int64LoadTests.cs 6KB
BranchTable.cs 6KB
Int32LoadTests.cs 5KB
Int64Store32Tests.cs 5KB
Int64Load8SignedTests.cs 5KB
CallIndirect.cs 5KB
CompilerConfiguration.cs 5KB
FunctionBody.cs 5KB
Float32StoreTests.cs 5KB
Int32StoreTests.cs 5KB
Select.cs 5KB
RuntimeImport.cs 5KB
Reader.cs 5KB
Int32Load16UnsignedTests.cs 5KB
CallTests.cs 5KB
Int32Load16SignedTests.cs 5KB
Int32Constant.cs 4KB
SelectTests.cs 4KB
Int64Store16Tests.cs 4KB
InstructionTests.cs 4KB
Int32Store16Tests.cs 4KB
Int32Load8UnsignedTests.cs 4KB
Int32Load8SignedTests.cs 4KB
TruncateSaturateInstruction.cs 4KB
Signature.cs 4KB
TestUtility.cs 4KB
OpCodeTests.cs 4KB
ModuleExtensions.cs 4KB
LocalGetTests.cs 4KB
Int32Store8Tests.cs 4KB
Int64Store8Tests.cs 4KB
Writer.cs 4KB
Call.cs 3KB
Int64CountLeadingZeroes.cs 3KB
Int32CountLeadingZeroes.cs 3KB
ReturnTests.cs 3KB
NameCleaner.cs 3KB
SampleTests.cs 3KB
GlobalImportTests.cs 3KB
ImportDictionary.cs 3KB
MissingDelegateTypesException.cs 3KB
ApiQualityTests.cs 3KB
LocalGet.cs 3KB
Int32TruncateSaturateFloat64UnsignedTests.cs 3KB
Element.cs 3KB
AssemblyBuilder.cs 3KB
MiscellaneousOpCode.cs 3KB
MemoryGrow.cs 3KB
ResizeableLimits.cs 3KB
Int64TruncateSaturateFloat64UnsignedTests.cs 3KB
Int64TruncateSaturateFloat64SignedTests.cs 3KB
Data.cs 3KB
Int64TruncateSaturateFloat32SignedTests.cs 3KB
Int64CountOneBits.cs 3KB
Int32CountOneBits.cs 3KB
Int64Constant.cs 3KB
LocalTee.cs 3KB
Int32TruncateSaturateFloat64SignedTests.cs 3KB
BranchIf.cs 3KB
Branch.cs 3KB
Int32TruncateSaturateFloat32SignedTests.cs 3KB
Int32TruncateSaturateFloat32UnsignedTests.cs 3KB
End.cs 3KB
Int64TruncateSaturateFloat32UnsignedTests.cs 3KB
Samples.cs 3KB
LocalSet.cs 3KB
BranchTableTests.cs 2KB
Float64Constant.cs 2KB
Float32Constant.cs 2KB
Memory.cs 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
可爱的小树懒
- 粉丝: 16
- 资源: 4577
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论2