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

     1  package wavm
     2  
     3  // #include <stdlib.h>
     4  // #include "wavm-c.h"
     5  import "C"
     6  import (
     7  	"reflect"
     8  	"unsafe"
     9  )
    10  
    11  type (
    12  	/*
    13  		typedef struct wasm_import_t
    14  		{
    15  			const char* module;
    16  			size_t num_module_bytes;
    17  			const char* name;
    18  			size_t num_name_bytes;
    19  			wasm_externtype_t* type;
    20  		} wasm_import_t;
    21  	*/
    22  	Import struct {
    23  		module           *C.char
    24  		num_module_bytes C.size_t
    25  		name             *C.char
    26  		num_name_bytes   C.size_t
    27  		_type            *C.wasm_externtype_t
    28  	}
    29  )
    30  
    31  func (w *Import) Module() string {
    32  	return string(*(*string)(unsafe.Pointer(&reflect.StringHeader{
    33  		Data: uintptr(unsafe.Pointer(w.module)),
    34  		Len:  int(w.num_module_bytes),
    35  	})))
    36  }
    37  
    38  func (w *Import) ModuleUnsafe() string {
    39  	return *(*string)(unsafe.Pointer(&reflect.StringHeader{
    40  		Data: uintptr(unsafe.Pointer(w.module)),
    41  		Len:  int(w.num_module_bytes),
    42  	}))
    43  }
    44  
    45  func (w *Import) Name() string {
    46  	return string(w.NameUnsafe())
    47  }
    48  
    49  func (w *Import) NameUnsafe() string {
    50  	return *(*string)(unsafe.Pointer(&reflect.StringHeader{
    51  		Data: uintptr(unsafe.Pointer(w.name)),
    52  		Len:  int(w.num_name_bytes),
    53  	}))
    54  }