github.com/moontrade/wavm-go@v0.3.2-0.20220316110326-d229dd66ad65/val.go (about)

     1  package wavm
     2  
     3  /*
     4  #include <stdlib.h>
     5  #include "wavm-c.h"
     6  
     7  int32_t wasm_val_get_i32(wasm_val_t* val) {
     8  	return val->i32;
     9  }
    10  
    11  int32_t wasm_val_get_i64(wasm_val_t* val) {
    12  	return val->i64;
    13  }
    14  
    15  int32_t wasm_val_get_f32(wasm_val_t* val) {
    16  	return val->f32;
    17  }
    18  
    19  int32_t wasm_val_get_f64(wasm_val_t* val) {
    20  	return val->f64;
    21  }
    22  */
    23  import "C"
    24  import "unsafe"
    25  
    26  type (
    27  	/*
    28  		typedef union wasm_val_t
    29  		{
    30  			int32_t i32;
    31  			int64_t i64;
    32  			wasm_float32_t f32;
    33  			wasm_float64_t f64;
    34  			wasm_v128_t v128;
    35  			wasm_ref_t* ref;
    36  		} wasm_val_t;
    37  	*/
    38  	Val C.wasm_val_t
    39  	/*
    40  		// NOTE: not 128-bit aligned
    41  		typedef struct wasm_v128_t
    42  		{
    43  			uint64_t u64x2[2];
    44  		} wasm_v128_t;
    45  	*/
    46  	V128T struct {
    47  		u64x2 [2]uint64
    48  	}
    49  )
    50  
    51  func ValI32(value int32) Val {
    52  	v := Val{}
    53  	v.SetI32(value)
    54  	return v
    55  }
    56  
    57  func ValI64(value int64) Val {
    58  	v := Val{}
    59  	v.SetI64(value)
    60  	return v
    61  }
    62  
    63  func ValF32(value float32) Val {
    64  	v := Val{}
    65  	v.SetF32(value)
    66  	return v
    67  }
    68  
    69  func ValF64(value float64) Val {
    70  	v := Val{}
    71  	v.SetF64(value)
    72  	return v
    73  }
    74  
    75  type ValKind uint8
    76  
    77  const (
    78  	I32     = ValKind(C.WASM_I32)
    79  	I64     = ValKind(C.WASM_I64)
    80  	F32     = ValKind(C.WASM_F32)
    81  	F64     = ValKind(C.WASM_F64)
    82  	V128    = ValKind(C.WASM_V128)
    83  	ANYREF  = ValKind(C.WASM_ANYREF)
    84  	FUNCREF = ValKind(C.WASM_FUNCREF)
    85  )
    86  
    87  func (k ValKind) IsNum() bool {
    88  	return k < ANYREF
    89  }
    90  func (k ValKind) IsRef() bool {
    91  	return k >= ANYREF
    92  }
    93  
    94  func (v *Val) I32() int32 {
    95  	return *(*int32)(unsafe.Pointer(v))
    96  }
    97  func ValGetI32(val *Val) int32 {
    98  	return int32(C.wasm_val_get_i32((*C.wasm_val_t)(val)))
    99  }
   100  func (v *Val) SetI32(value int32) {
   101  	*(*int32)(unsafe.Pointer(v)) = value
   102  }
   103  
   104  func (v *Val) I64() int64 {
   105  	return *(*int64)(unsafe.Pointer(v))
   106  }
   107  func ValGetI64(val *Val) int64 {
   108  	return int64(C.wasm_val_get_i64((*C.wasm_val_t)(val)))
   109  }
   110  func (v *Val) SetI64(value int64) {
   111  	*(*int64)(unsafe.Pointer(v)) = value
   112  }
   113  
   114  func (v *Val) F32() float32 {
   115  	return *(*float32)(unsafe.Pointer(v))
   116  }
   117  func ValGetF32(val *Val) float32 {
   118  	return float32(C.wasm_val_get_f32((*C.wasm_val_t)(val)))
   119  }
   120  func (v *Val) SetF32(value float32) {
   121  	*(*float32)(unsafe.Pointer(v)) = value
   122  }
   123  
   124  func (v *Val) F64() float64 {
   125  	return *(*float64)(unsafe.Pointer(v))
   126  }
   127  func ValGetF64(val *Val) float64 {
   128  	return float64(C.wasm_val_get_f64((*C.wasm_val_t)(val)))
   129  }
   130  func (v *Val) SetF64(value float64) {
   131  	*(*float64)(unsafe.Pointer(v)) = value
   132  }
   133  
   134  func (v *Val) V128() V128T {
   135  	return *(*V128T)(unsafe.Pointer(v))
   136  }
   137  func (v *Val) SetV128(value V128T) {
   138  	*(*V128T)(unsafe.Pointer(v)) = value
   139  }
   140  
   141  func (v *Val) Ref32() uint32 {
   142  	return *(*uint32)(unsafe.Pointer(v))
   143  }
   144  func (v *Val) SetRef32(value uint32) {
   145  	*(*uint32)(unsafe.Pointer(v)) = value
   146  }
   147  
   148  func (v *Val) Ref64() uint64 {
   149  	return *(*uint64)(unsafe.Pointer(v))
   150  }
   151  func (v *Val) SetRef64(value uint64) {
   152  	*(*uint64)(unsafe.Pointer(v)) = value
   153  }