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

     1  package wavm
     2  
     3  /*
     4  #include <stdlib.h>
     5  #include "wavm-c.h"
     6  
     7  char* wasm_module_compile(wasm_engine_t* engine,
     8  						  const char* wasmBytes,
     9  						  uintptr_t numWASMBytes,
    10  						  uintptr_t* outBytes);
    11  
    12  char* wasm_module_compile_object(wasm_engine_t* engine,
    13  								const char* wasmBytes,
    14  								uintptr_t numWASMBytes,
    15  								uintptr_t* outBytes);
    16  */
    17  import "C"
    18  import (
    19  	"reflect"
    20  	"unsafe"
    21  )
    22  
    23  type Engine C.wasm_engine_t
    24  
    25  func NewEngine() *Engine {
    26  	return (*Engine)(C.wasm_engine_new())
    27  }
    28  
    29  func (e *Engine) Close() error {
    30  	e.Delete()
    31  	return nil
    32  }
    33  
    34  func (e *Engine) Delete() {
    35  	C.wasm_engine_delete((*C.wasm_engine_t)(e))
    36  }
    37  
    38  func NewEngineWithConfig(config *Config) *Engine {
    39  	return (*Engine)(C.wasm_engine_new_with_config((*C.wasm_config_t)(config)))
    40  }
    41  
    42  func (e *Engine) NewCompartment(debugName string) *Compartment {
    43  	return NewCompartment(e, debugName)
    44  }
    45  
    46  func (e *Engine) NewModule(binary []byte) *Module {
    47  	return NewModule(e, binary)
    48  }
    49  
    50  func (e *Engine) NewPrecompiledModule(binary []byte) *Module {
    51  	return NewModulePrecompiled(e, binary)
    52  }
    53  
    54  func (e *Engine) Compile(wasm []byte, out []byte) []byte {
    55  	var size C.uintptr_t
    56  	bytes := C.wasm_module_compile(
    57  		(*C.wasm_engine_t)(e),
    58  		(*C.char)(unsafe.Pointer(&wasm[0])),
    59  		(C.uintptr_t)(len(wasm)),
    60  		&size,
    61  	)
    62  	if bytes == nil {
    63  		return nil
    64  	}
    65  	if len(out) < int(size) {
    66  		out = make([]byte, int(size))
    67  	}
    68  	copy(out, *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    69  		Data: uintptr(unsafe.Pointer(bytes)),
    70  		Len:  int(size),
    71  		Cap:  int(size),
    72  	})))
    73  	// Free
    74  	C.free(unsafe.Pointer(bytes))
    75  	return out[:int(size)]
    76  }
    77  
    78  func (e *Engine) CompileObject(wasm []byte, out []byte) []byte {
    79  	var size C.uintptr_t
    80  	bytes := C.wasm_module_compile_object(
    81  		(*C.wasm_engine_t)(e),
    82  		(*C.char)(unsafe.Pointer(&wasm[0])),
    83  		(C.uintptr_t)(len(wasm)),
    84  		&size,
    85  	)
    86  	if bytes == nil {
    87  		return nil
    88  	}
    89  	if len(out) < int(size) {
    90  		out = make([]byte, int(size))
    91  	}
    92  	copy(out, *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    93  		Data: uintptr(unsafe.Pointer(bytes)),
    94  		Len:  int(size),
    95  		Cap:  int(size),
    96  	})))
    97  	// Free
    98  	C.free(unsafe.Pointer(bytes))
    99  	return out[:int(size)]
   100  }