Categorygithub.com/httpwasm/http-wasm-host-go
module
0.0.0-20230614114731-767c95f002e6
Repository: https://github.com/httpwasm/http-wasm-host-go.git
Documentation: pkg.go.dev

# README

Build Go Report Card License

http-wasm Host Library for Go

http-wasm defines HTTP functions implemented in WebAssembly. This repository includes http_handler ABI middleware for various HTTP server libraries written in Go.

WARNING: This is an early draft

The current maturity phase is early draft. Once this is integrated with coraza and dapr, we can begin discussions about compatability.

个人理解

wasm/host处理流程

  • handler.Middleware作为包装器
type middleware struct {
	host            handler.Host // host实现
	runtime         wazero.Runtime // 运行时
	guestModule     wazero.CompiledModule //编译的模式
	moduleConfig    wazero.ModuleConfig //模块配置
	guestConfig     []byte // wasm二进制文件
	logger          api.Logger
	pool            sync.Pool
	features        handler.Features
	instanceCounter uint64
}
  • handler.Host接口增加函数
	// GetTemplate supports the WebAssembly function export FuncGetTemplate.
	GetTemplate(ctx context.Context) string

	// SetTemplate supports the WebAssembly function export FuncSetTemplate.
	SetTemplate(ctx context.Context, template string)
  • 默认空实现
func (UnimplementedHost) GetTemplate(context.Context) string { return "" }
func (UnimplementedHost) SetTemplate(context.Context, string) 
  • handler.middleware.go#692文件中,声明导入函数,这样在guest/wasm中就能调用了,都是通过指针地址进行数据交换的.
    //getTemplate
	NewFunctionBuilder().
	WithGoModuleFunction(wazeroapi.GoModuleFunc(m.getTemplate), []wazeroapi.ValueType{i32, i32}, []wazeroapi.ValueType{i32}).
	WithParameterNames("buf", "buf_limit").Export(handler.FuncGetTemplate).
	NewFunctionBuilder().
	//setTemplate
	WithGoModuleFunction(wazeroapi.GoModuleFunc(m.setTemplate), []wazeroapi.ValueType{i32, i32}, []wazeroapi.ValueType{}).
	WithParameterNames("template", "template_len").Export(handler.FuncSetTemplate).

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author