wa-lang.org/wazero@v1.0.2/internal/wasm/global.go (about)

     1  package wasm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"wa-lang.org/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(context.Context) uint64 {
    24  	return g.g.Val
    25  }
    26  
    27  // Set implements the same method as documented on api.MutableGlobal.
    28  func (g *mutableGlobal) Set(_ context.Context, v uint64) {
    29  	g.g.Val = v
    30  }
    31  
    32  // String implements fmt.Stringer
    33  func (g *mutableGlobal) String() string {
    34  	switch g.Type() {
    35  	case ValueTypeI32, ValueTypeI64:
    36  		return fmt.Sprintf("global(%d)", g.Get(context.Background()))
    37  	case ValueTypeF32:
    38  		return fmt.Sprintf("global(%f)", api.DecodeF32(g.Get(context.Background())))
    39  	case ValueTypeF64:
    40  		return fmt.Sprintf("global(%f)", api.DecodeF64(g.Get(context.Background())))
    41  	default:
    42  		panic(fmt.Errorf("BUG: unknown value type %X", g.Type()))
    43  	}
    44  }
    45  
    46  type globalI32 uint64
    47  
    48  // compile-time check to ensure globalI32 is a api.Global
    49  var _ api.Global = globalI32(0)
    50  
    51  // Type implements the same method as documented on api.Global.
    52  func (g globalI32) Type() api.ValueType {
    53  	return ValueTypeI32
    54  }
    55  
    56  // Get implements the same method as documented on api.Global.
    57  func (g globalI32) Get(context.Context) uint64 {
    58  	return uint64(g)
    59  }
    60  
    61  // String implements fmt.Stringer
    62  func (g globalI32) String() string {
    63  	return fmt.Sprintf("global(%d)", g)
    64  }
    65  
    66  type globalI64 uint64
    67  
    68  // compile-time check to ensure globalI64 is a api.Global
    69  var _ api.Global = globalI64(0)
    70  
    71  // Type implements the same method as documented on api.Global.
    72  func (g globalI64) Type() api.ValueType {
    73  	return ValueTypeI64
    74  }
    75  
    76  // Get implements the same method as documented on api.Global.
    77  func (g globalI64) Get(context.Context) uint64 {
    78  	return uint64(g)
    79  }
    80  
    81  // String implements fmt.Stringer
    82  func (g globalI64) String() string {
    83  	return fmt.Sprintf("global(%d)", g)
    84  }
    85  
    86  type globalF32 uint64
    87  
    88  // compile-time check to ensure globalF32 is a api.Global
    89  var _ api.Global = globalF32(0)
    90  
    91  // Type implements the same method as documented on api.Global.
    92  func (g globalF32) Type() api.ValueType {
    93  	return ValueTypeF32
    94  }
    95  
    96  // Get implements the same method as documented on api.Global.
    97  func (g globalF32) Get(context.Context) uint64 {
    98  	return uint64(g)
    99  }
   100  
   101  // String implements fmt.Stringer
   102  func (g globalF32) String() string {
   103  	return fmt.Sprintf("global(%f)", api.DecodeF32(g.Get(context.Background())))
   104  }
   105  
   106  type globalF64 uint64
   107  
   108  // compile-time check to ensure globalF64 is a api.Global
   109  var _ api.Global = globalF64(0)
   110  
   111  // Type implements the same method as documented on api.Global.
   112  func (g globalF64) Type() api.ValueType {
   113  	return ValueTypeF64
   114  }
   115  
   116  // Get implements the same method as documented on api.Global.
   117  func (g globalF64) Get(context.Context) uint64 {
   118  	return uint64(g)
   119  }
   120  
   121  // String implements fmt.Stringer
   122  func (g globalF64) String() string {
   123  	return fmt.Sprintf("global(%f)", api.DecodeF64(g.Get(context.Background())))
   124  }