wasm-micro-runtime/language-bindings/go/wamr/module.go
Wenyong Huang 9e07aa6f9f
Cumulative fixes for release/1.3.x (#3268)
Apply patches which were fixed branch main to branch `release/1.3.x`:
- zephyr: Use zephyr sys_cache instead of CMSIS (#3162)
- Fix llvm jit push funcref/externref result type issue (#3169)
- Fix inconsistent coding convention (#3171)
- VSCode IDE enhancement and readme update (#3172)
- zephyr: include math only with minimal libc (#3177)
- Fix wasm loader handling opcode br_table (#3176)
- Specify language in the wasi socket ext project (#3183)
- lldb_function_to_function_dbi: Fix a null dereference (#3189)
- Fix LLVM assertion failure and update CONTRIBUTING.md (#3197)
- lldb_function_to_function_dbi: A hack to avoid crashing on C++ methods (#3190)
- Fix compilation errors on MinGW (#3217)
- Fix compilation errors on esp-idf platform (#3224)
- Fix aot relocation symbols not found on windows 32-bit (#3231)
- Fix nightly run tsan ASLR issue (#3233)
- Go binding: Change C.long to C.int64_t when call wasm_runtime_set_wasi_args_ex (#3235)
- posix_file.c: Correct the dirfd argument that passes to fstatat (#3244)
- Fix compilation errors on zephyr platform (#3255)
- Fix dynamic offset not updated in op_br for block with ret type (#3269)
- Fix CI error when install packages for macos-14 (#3270)
2024-04-03 12:28:20 +08:00

147 lines
4.1 KiB
Go

/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
package wamr
// #include <wasm_export.h>
import "C"
import (
"unsafe"
"runtime"
"fmt"
)
type Module struct {
module C.wasm_module_t
}
/* Create WASM/AOT module from the memory buffer */
func NewModule(wasmBytes []byte) (*Module, error) {
if (wasmBytes == nil || len(wasmBytes) == 0) {
return nil, fmt.Errorf("NewModule error: invalid input")
}
wasmPtr := (*C.uint8_t)(unsafe.Pointer(&wasmBytes[0]))
wasmLen := C.uint(len(wasmBytes))
errorBytes := make([]byte, 128)
errorPtr := (*C.char)(unsafe.Pointer(&errorBytes[0]))
errorLen := C.uint(len(errorBytes))
m := C.wasm_runtime_load(wasmPtr, wasmLen, errorPtr, errorLen)
if (m == nil) {
return nil, fmt.Errorf("NewModule error: %s", string(errorBytes))
}
self := &Module{
module: m,
}
runtime.SetFinalizer(self, func(self *Module) {
self.Destroy()
})
return self, nil
}
/* Destroy the module */
func (self *Module) Destroy() {
runtime.SetFinalizer(self, nil)
if (self.module != nil) {
C.wasm_runtime_unload(self.module)
}
}
/* Set module's wasi arguments */
func (self *Module) SetWasiArgs(dirList [][]byte, mapDirList [][]byte,
env [][]byte, argv[][]byte) {
var dirPtr, mapDirPtr, envPtr, argvPtr **C.char
var dirCount, mapDirCount, envCount C.uint
var argc C.int
if (dirList != nil) {
dirPtr = (**C.char)(unsafe.Pointer(&dirList[0]))
dirCount = C.uint(len(dirList))
}
if (mapDirList != nil) {
mapDirPtr = (**C.char)(unsafe.Pointer(&mapDirList[0]))
mapDirCount = C.uint(len(mapDirList))
}
if (env != nil) {
envPtr = (**C.char)(unsafe.Pointer(&env[0]))
envCount = C.uint(len(env))
}
if (argv != nil) {
argvPtr = (**C.char)(unsafe.Pointer(&argv[0]))
argc = C.int(len(argv))
}
C.wasm_runtime_set_wasi_args(self.module, dirPtr, dirCount,
mapDirPtr, mapDirCount,
envPtr, envCount, argvPtr, argc)
}
/* Set module's wasi arguments */
func (self *Module) SetWasiArgsEx(dirList [][]byte, mapDirList [][]byte,
env [][]byte, argv[][]byte,
stdinfd int, stdoutfd int, stderrfd int) {
var dirPtr, mapDirPtr, envPtr, argvPtr **C.char
var dirCount, mapDirCount, envCount C.uint
var argc C.int
if (dirList != nil) {
dirPtr = (**C.char)(unsafe.Pointer(&dirList[0]))
dirCount = C.uint(len(dirList))
}
if (mapDirList != nil) {
mapDirPtr = (**C.char)(unsafe.Pointer(&mapDirList[0]))
mapDirCount = C.uint(len(mapDirList))
}
if (env != nil) {
envPtr = (**C.char)(unsafe.Pointer(&env[0]))
envCount = C.uint(len(env))
}
if (argv != nil) {
argvPtr = (**C.char)(unsafe.Pointer(&argv[0]))
argc = C.int(len(argv))
}
C.wasm_runtime_set_wasi_args_ex(self.module, dirPtr, dirCount,
mapDirPtr, mapDirCount,
envPtr, envCount, argvPtr, argc,
C.int64_t(stdinfd), C.int64_t(stdoutfd),
C.int64_t(stderrfd))
}
/* Set module's wasi network address pool */
func (self *Module) SetWasiAddrPool(addrPool [][]byte) {
var addrPoolPtr **C.char
var addrPoolSize C.uint
if (addrPool != nil) {
addrPoolPtr = (**C.char)(unsafe.Pointer(&addrPool[0]))
addrPoolSize = C.uint(len(addrPool))
}
C.wasm_runtime_set_wasi_addr_pool(self.module, addrPoolPtr, addrPoolSize)
}
/* Set module's wasi domain lookup pool */
func(self *Module) SetWasiNsLookupPool(nsLookupPool [][]byte) {
var nsLookupPoolPtr **C.char
var nsLookupPoolSize C.uint
if (nsLookupPool != nil) {
nsLookupPoolPtr = (**C.char)(unsafe.Pointer(&nsLookupPool[0]))
nsLookupPoolSize = C.uint(len(nsLookupPool))
}
C.wasm_runtime_set_wasi_ns_lookup_pool(self.module, nsLookupPoolPtr, nsLookupPoolSize)
}