github.com/taubyte/vm-wasm-utils@v1.0.2/global.go (about) 1 package wasm 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/tetratelabs/wazero/api" 8 ) 9 10 type mutableGlobal struct { 11 g *GlobalInstance 12 } 13 14 // compile-time check to ensure mutableGlobal is a api.Global. 15 var _ api.Global = &mutableGlobal{} 16 17 // Type implements the same method as documented on api.Global. 18 func (g *mutableGlobal) Type() api.ValueType { 19 return g.g.Type.ValType 20 } 21 22 // Get implements the same method as documented on api.Global. 23 func (g *mutableGlobal) Get() uint64 { 24 // Note: If you use the context.Context param, don't forget to coerce nil to context.Background()! 25 26 return g.g.Val 27 } 28 29 // Set implements the same method as documented on api.MutableGlobal. 30 func (g *mutableGlobal) Set(_ context.Context, v uint64) { 31 // Note: If you use the context.Context param, don't forget to coerce nil to context.Background()! 32 33 g.g.Val = v 34 } 35 36 // String implements fmt.Stringer 37 func (g *mutableGlobal) String() string { 38 switch g.Type() { 39 case ValueTypeI32, ValueTypeI64: 40 return fmt.Sprintf("global(%d)", g.Get()) 41 case ValueTypeF32: 42 return fmt.Sprintf("global(%f)", api.DecodeF32(g.Get())) 43 case ValueTypeF64: 44 return fmt.Sprintf("global(%f)", api.DecodeF64(g.Get())) 45 default: 46 panic(fmt.Errorf("BUG: unknown value type %X", g.Type())) 47 } 48 } 49 50 type globalI32 uint64 51 52 // compile-time check to ensure globalI32 is a api.Global 53 var _ api.Global = globalI32(0) 54 55 // Type implements the same method as documented on api.Global. 56 func (g globalI32) Type() api.ValueType { 57 return ValueTypeI32 58 } 59 60 // Get implements the same method as documented on api.Global. 61 func (g globalI32) Get() uint64 { 62 // Note: If you use the context.Context param, don't forget to coerce nil to context.Background()! 63 64 return uint64(g) 65 } 66 67 // String implements fmt.Stringer 68 func (g globalI32) String() string { 69 return fmt.Sprintf("global(%d)", g) 70 } 71 72 type globalI64 uint64 73 74 // compile-time check to ensure globalI64 is a api.Global 75 var _ api.Global = globalI64(0) 76 77 // Type implements the same method as documented on api.Global. 78 func (g globalI64) Type() api.ValueType { 79 return ValueTypeI64 80 } 81 82 // Get implements the same method as documented on api.Global. 83 func (g globalI64) Get() uint64 { 84 // Note: If you use the context.Context param, don't forget to coerce nil to context.Background()! 85 86 return uint64(g) 87 } 88 89 // String implements fmt.Stringer 90 func (g globalI64) String() string { 91 return fmt.Sprintf("global(%d)", g) 92 } 93 94 type globalF32 uint64 95 96 // compile-time check to ensure globalF32 is a api.Global 97 var _ api.Global = globalF32(0) 98 99 // Type implements the same method as documented on api.Global. 100 func (g globalF32) Type() api.ValueType { 101 return ValueTypeF32 102 } 103 104 // Get implements the same method as documented on api.Global. 105 func (g globalF32) Get() uint64 { 106 // Note: If you use the context.Context param, don't forget to coerce nil to context.Background()! 107 108 return uint64(g) 109 } 110 111 // String implements fmt.Stringer 112 func (g globalF32) String() string { 113 return fmt.Sprintf("global(%f)", api.DecodeF32(g.Get())) 114 } 115 116 type globalF64 uint64 117 118 // compile-time check to ensure globalF64 is a api.Global 119 var _ api.Global = globalF64(0) 120 121 // Type implements the same method as documented on api.Global. 122 func (g globalF64) Type() api.ValueType { 123 return ValueTypeF64 124 } 125 126 // Get implements the same method as documented on api.Global. 127 func (g globalF64) Get() uint64 { 128 // Note: If you use the context.Context param, don't forget to coerce nil to context.Background()! 129 130 return uint64(g) 131 } 132 133 // String implements fmt.Stringer 134 func (g globalF64) String() string { 135 return fmt.Sprintf("global(%f)", api.DecodeF64(g.Get())) 136 }