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

     1  package wavm
     2  
     3  /*
     4  #include <stdlib.h>
     5  #include "wavm-c.h"
     6  
     7  wasm_module_t* wasm_module_precompiled_new(wasm_engine_t*,
     8  										  const char* binary,
     9  										  size_t num_binary_bytes);
    10  */
    11  import "C"
    12  import (
    13  	"reflect"
    14  	"unsafe"
    15  )
    16  
    17  type (
    18  	Module C.wasm_module_t
    19  )
    20  
    21  func NewModule(engine *Engine, binary []byte) *Module {
    22  	slice := (*reflect.StringHeader)(unsafe.Pointer(&binary))
    23  	return (*Module)(C.wasm_module_new((*C.wasm_engine_t)(engine), (*C.char)(unsafe.Pointer(slice.Data)), (C.size_t)(slice.Len)))
    24  }
    25  
    26  func NewModulePrecompiled(engine *Engine, binary []byte) *Module {
    27  	slice := (*reflect.StringHeader)(unsafe.Pointer(&binary))
    28  	return (*Module)(C.wasm_module_precompiled_new((*C.wasm_engine_t)(engine), (*C.char)(unsafe.Pointer(slice.Data)), (C.size_t)(slice.Len)))
    29  }
    30  
    31  //func wasm_module_new_text(engine *C.wasm_engine_t, wast string) *C.wasm_module_t {
    32  //	return wasm_module_new_wast(engine, []byte(wast))
    33  //}
    34  
    35  func NewModuleText(engine *Engine, wast string) *Module {
    36  	if len(wast) == 0 {
    37  		return nil
    38  	}
    39  	ptr := C.CString(wast)
    40  	defer C.free(unsafe.Pointer(ptr))
    41  
    42  	return (*Module)(C.wasm_module_new_text((*C.wasm_engine_t)(engine), ptr, C.size_t(len(wast))))
    43  }
    44  
    45  func (m *Module) Close() error {
    46  	m.Delete()
    47  	return nil
    48  }
    49  func (m *Module) Delete() {
    50  	C.wasm_module_delete((*C.wasm_module_t)(m))
    51  }
    52  
    53  func (m *Module) Print() string {
    54  	return string(m.PrintTo(nil))
    55  }
    56  
    57  func (m *Module) PrintTo(b []byte) []byte {
    58  	var out C.size_t
    59  	ptr := C.wasm_module_print((*C.wasm_module_t)(m), &out)
    60  	b = append(b, *(*string)(unsafe.Pointer(&reflect.StringHeader{
    61  		Data: uintptr(unsafe.Pointer(ptr)),
    62  		Len:  int(out),
    63  	}))...)
    64  	if ptr == nil {
    65  		return nil
    66  	}
    67  	C.free(unsafe.Pointer(ptr))
    68  	return b
    69  }
    70  
    71  func ModuleValidate(binary []byte) bool {
    72  	if len(binary) == 0 {
    73  		return false
    74  	}
    75  	return bool(C.wasm_module_validate((*C.char)(unsafe.Pointer(&binary[0])), (C.size_t)(len(binary))))
    76  }
    77  
    78  func (m *Module) NumImports() int {
    79  	return int(C.wasm_module_num_imports((*C.wasm_module_t)(m)))
    80  }
    81  
    82  func (m *Module) Import(index int) Import {
    83  	var out C.wasm_import_t
    84  	C.wasm_module_import((*C.wasm_module_t)(m), (C.size_t)(index), &out)
    85  	return *(*Import)(unsafe.Pointer(&out))
    86  }
    87  
    88  func (m *Module) Imports(imports []Import) []Import {
    89  	count := m.NumImports()
    90  	if len(imports) > 0 {
    91  		imports = imports[:0]
    92  	}
    93  	for i := 0; i < count; i++ {
    94  		imports = append(imports, m.Import(i))
    95  	}
    96  	return imports
    97  }
    98  
    99  func (m *Module) NumExports() int {
   100  	return int(C.wasm_module_num_exports((*C.wasm_module_t)(m)))
   101  }
   102  
   103  func (m *Module) Export(index int) Export {
   104  	var out C.wasm_export_t
   105  	C.wasm_module_export((*C.wasm_module_t)(m), (C.size_t)(index), &out)
   106  	return *(*Export)(unsafe.Pointer(&out))
   107  }
   108  
   109  func (m *Module) Exports(exports []Export) []Export {
   110  	count := m.NumExports()
   111  	if len(exports) > 0 {
   112  		exports = exports[:0]
   113  	}
   114  	for i := 0; i < count; i++ {
   115  		exports = append(exports, m.Export(i))
   116  	}
   117  	return exports
   118  }