Files
wazero/internal/wasm/global.go
Thomas Pelletier 0bfb4b52eb Give Listener a read-only view of globals (#1404)
* experimental: give listener r/o view of globals

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* experimental: add global support to interpreter

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* exp: replace globals proxy with module interface

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* exp: trim down experimental.InternalModule

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* Replace globals view slice with constantGlobal

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* Fix tests after merge

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* Address PR feedback

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* Rename methods of experimental.Module

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* Run make check

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

* Add WazeroOnlyType to constantGlobal

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>

---------

Signed-off-by: Thomas Pelletier <thomas@pelletier.codes>
2023-04-29 00:07:28 -07:00

57 lines
1.2 KiB
Go

package wasm
import (
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/internalapi"
)
// constantGlobal wraps GlobalInstance to implement api.Global.
type constantGlobal struct {
internalapi.WazeroOnlyType
g *GlobalInstance
}
// Type implements api.Global.
func (g constantGlobal) Type() api.ValueType {
return g.g.Type.ValType
}
// Get implements api.Global.
func (g constantGlobal) Get() uint64 {
return g.g.Val
}
// String implements api.Global.
func (g constantGlobal) String() string {
return g.g.String()
}
// mutableGlobal extends constantGlobal to allow updates.
type mutableGlobal struct {
internalapi.WazeroOnlyType
g *GlobalInstance
}
// Type implements api.Global.
func (g mutableGlobal) Type() api.ValueType {
return g.g.Type.ValType
}
// Get implements api.Global.
func (g mutableGlobal) Get() uint64 {
return g.g.Val
}
// String implements api.Global.
func (g mutableGlobal) String() string {
return g.g.String()
}
// Set implements the same method as documented on api.MutableGlobal.
func (g mutableGlobal) Set(v uint64) {
g.g.Val = v
}
// compile-time check to ensure mutableGlobal is a api.Global.
var _ api.Global = mutableGlobal{}