Files
wazero/internal/wasm/global.go
Crypt Keeper 1e4834612a Removes host-specific types (#347)
This converges host-defined modules with Wasm defined modules by
introducing a custom section for host-defined functions. The net result
are far less types and consistent initialization.

* HostModule is removed for Module
* HostFunction is removed for Function
* ModuleContext is removed for Module

Note: One impact of this is that the low-level API no longer accepts a
go context (context.Context), rather a `wasm.Module` which the function
is called in context of. This meant exposing `wasm.Module.WithContext`
to override the default.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-08 16:43:13 +08:00

124 lines
2.7 KiB
Go

package internalwasm
import (
"fmt"
publicwasm "github.com/tetratelabs/wazero/wasm"
)
type mutableGlobal struct {
g *GlobalInstance
}
// compile-time check to ensure mutableGlobal is a wasm.Global
var _ publicwasm.Global = &mutableGlobal{}
// Type implements wasm.Global Type
func (g *mutableGlobal) Type() publicwasm.ValueType {
return g.g.Type.ValType
}
// Get implements wasm.Global Get
func (g *mutableGlobal) Get() uint64 {
return g.g.Val
}
// Set implements wasm.MutableGlobal Set
func (g *mutableGlobal) Set(v uint64) {
g.g.Val = v
}
// String implements fmt.Stringer
func (g *mutableGlobal) String() string {
switch g.Type() {
case ValueTypeI32, ValueTypeI64:
return fmt.Sprintf("global(%d)", g.Get())
case ValueTypeF32:
return fmt.Sprintf("global(%f)", publicwasm.DecodeF32(g.Get()))
case ValueTypeF64:
return fmt.Sprintf("global(%f)", publicwasm.DecodeF64(g.Get()))
default:
panic(fmt.Errorf("BUG: unknown value type %X", g.Type()))
}
}
type globalI32 uint64
// compile-time check to ensure globalI32 is a wasm.Global
var _ publicwasm.Global = globalI32(0)
// Type implements wasm.Global Type
func (g globalI32) Type() publicwasm.ValueType {
return ValueTypeI32
}
// Get implements wasm.Global Get
func (g globalI32) Get() uint64 {
return uint64(g)
}
// String implements fmt.Stringer
func (g globalI32) String() string {
return fmt.Sprintf("global(%d)", g)
}
type globalI64 uint64
// compile-time check to ensure globalI64 is a publicwasm.Global
var _ publicwasm.Global = globalI64(0)
// Type implements wasm.Global Type
func (g globalI64) Type() publicwasm.ValueType {
return ValueTypeI64
}
// Get implements wasm.Global Get
func (g globalI64) Get() uint64 {
return uint64(g)
}
// String implements fmt.Stringer
func (g globalI64) String() string {
return fmt.Sprintf("global(%d)", g)
}
type globalF32 uint64
// compile-time check to ensure globalF32 is a publicwasm.Global
var _ publicwasm.Global = globalF32(0)
// Type implements wasm.Global Type
func (g globalF32) Type() publicwasm.ValueType {
return ValueTypeF32
}
// Get implements wasm.Global Get
func (g globalF32) Get() uint64 {
return uint64(g)
}
// String implements fmt.Stringer
func (g globalF32) String() string {
return fmt.Sprintf("global(%f)", publicwasm.DecodeF32(g.Get()))
}
type globalF64 uint64
// compile-time check to ensure globalF64 is a publicwasm.Global
var _ publicwasm.Global = globalF64(0)
// Type implements wasm.Global Type
func (g globalF64) Type() publicwasm.ValueType {
return ValueTypeF64
}
// Get implements wasm.Global Get
func (g globalF64) Get() uint64 {
return uint64(g)
}
// String implements fmt.Stringer
func (g globalF64) String() string {
return fmt.Sprintf("global(%f)", publicwasm.DecodeF64(g.Get()))
}