Epsilon: 用Go编写的WASM虚拟机
Epsilon: A WASM virtual machine written in Go

原始链接: https://github.com/ziggy42/epsilon

Epsilon 是一个轻量级、无依赖的 Go 运行时,用于 WebAssembly (Wasm) 2.0。它能够在 Go 应用程序中直接执行 Wasm,无需 CGo。它支持多种架构(amd64、arm64 等),并允许从字节切片或文件加载和运行 Wasm 模块。 主要特性包括:将 Wasm 模块嵌入到 Go 中,通过 `ImportBuilder` 使用自定义 Go 函数扩展模块,以及一个交互式 REPL,用于测试和调试,包含 `LOAD`、`INVOKE`、`GET` 和 `MEM` 等命令。 Epsilon 提供了单元测试和规范测试工具,以及基准测试工具。它是一个社区驱动的项目,不是官方 Google 产品,并采用 Apache 2.0 许可。你可以使用 `go get github.com/ziggy42/epsilon` 安装它。

## Epsilon: 基于Go的WASM虚拟机 Ziggy42创建了Epsilon,一个用Go实现的WebAssembly (WASM) 虚拟机,并在Hacker News上分享了它。该项目最初是个人的挑战,旨在构建一些有趣的东西,而无需依赖现有实现,并逐渐发展成为一个潜在的有用工具。值得注意的是,Epsilon没有依赖项,仅使用Go标准库。 讨论主要集中在与现有WASM运行时(如wazero)的比较、解释器与JIT编译器(尤其是在iOS等平台上的优势)以及支持WebAssembly组件模型以提高鲁棒性的可能性。人们提出了关于沙箱和防止无限循环的担忧,并建议使用上下文取消和指令限制。 许多评论者强调了WASM开发的挑战,尤其是在内存管理和文档方面。其他人则对该项目的潜力表示兴奋,特别是对于插件开发和在Web浏览器中运行WASM等用例。创建者确认计划探索基于上下文的超时机制以提高安全性,并承认受到了wazero和go-sqlite3等项目的启发。
相关文章

原文

Go Reference Go Report Card License

Epsilon is a pure Go WebAssembly runtime with zero dependencies.

  • Fully supports WebAssembly 2.0 Specification
  • Runs on any architecture supported by Go (amd64, arm64, etc.) without requiring CGo
  • Allows embedding WebAssembly modules in Go applications
  • Includes an interactive REPL for testing and debugging
go get github.com/ziggy42/epsilon

Load and run a WebAssembly module directly from a byte slice:

package main

import (
	"fmt"
	"os"

	"github.com/ziggy42/epsilon/epsilon"
)

func main() {
	// 1. Read the WASM file
	wasmBytes, _ := os.ReadFile("add.wasm")

	// 2. Instantiate the module
	instance, _ := epsilon.NewRuntime().InstantiateModuleFromBytes(wasmBytes)

	// 3. Invoke an exported function
	result, _ := instance.Invoke("add", int32(5), int32(37))

	fmt.Println(result[0]) // Output: 42
}

Extend your WebAssembly modules with custom Go functions and more using ImportBuilder:

// Create imports before instantiation
imports := epsilon.NewImportBuilder().
	AddHostFunc("env", "log", func(args ...any) []any {
		fmt.Printf("[WASM Log]: %v\n", args[0])
		return nil
	}).
	Build()

// Instantiate with imports
instance, _ := epsilon.NewRuntime().
	InstantiateModuleWithImports(wasmFile, imports)

Epsilon includes a REPL for interactively testing and debugging modules.

# Run the REPL
go run ./cmd/epsilon
Category Command Description
Loading LOAD <path|url> Load a module from a file or URL
Running INVOKE <func> [args...] Call an exported function
State GET <global> Read a global variable
Debug MEM <offset> <len> Inspect linear memory
System LIST List loaded modules and their exports

Example Session:

$ go run ./cmd/epsilon
>> LOAD https://github.com/mdn/webassembly-examples/raw/refs/heads/main/understanding-text-format/add.wasm
'default' instantiated.
>> INVOKE add 10 32
42
# Run unit tests
go test ./epsilon/...

# Run spec tests (requires git submodule)
go test ./internal/spec_tests/...

# Run benchmarks
go test -bench . ./internal/benchmarks

See CONTRIBUTING.md for details.

Apache 2.0; see LICENSE for details.

This is not an officially supported Google product. This project is not eligible for the Google Open Source Software Vulnerability Rewards Program.

联系我们 contact @ memedata.com