summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/language-bindings/go/wamr/module.go
blob: 13480b221c2fc768423fea151a870c229a112f38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * 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.int(stdinfd), C.int(stdoutfd),
                                    C.int(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)
}