forked from bytecodealliance/wasm-micro-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
153 lines (126 loc) · 3.44 KB
/
runtime.go
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
147
148
149
150
151
152
153
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
package wamr
/*
#include <stdlib.h>
#include <string.h>
#include <wasm_export.h>
void
bh_log_set_verbose_level(uint32_t level);
bool
init_wamr_runtime(bool alloc_with_pool, uint8_t *heap_buf,
uint32_t heap_size, uint32_t max_thread_num)
{
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
if (alloc_with_pool) {
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = heap_buf;
init_args.mem_alloc_option.pool.heap_size = heap_size;
}
else {
init_args.mem_alloc_type = Alloc_With_System_Allocator;
}
return wasm_runtime_full_init(&init_args);
}
*/
import "C"
import (
"fmt"
"unsafe"
)
type LogLevel uint32
const (
LOG_LEVEL_FATAL LogLevel = 0
LOG_LEVEL_ERROR LogLevel = 1
LOG_LEVEL_WARNING LogLevel = 2
LOG_LEVEL_DEBUG LogLevel = 3
LOG_LEVEL_VERBOSE LogLevel = 4
)
/*
type NativeSymbol struct {
symbol string
func_ptr *uint8
signature string
}
*/
type _Runtime struct {
initialized bool
}
var _runtime_singleton *_Runtime
/* Return the runtime singleton */
func Runtime() *_Runtime {
if (_runtime_singleton == nil) {
self := &_Runtime{}
_runtime_singleton = self
}
return _runtime_singleton;
}
/* Initialize the WASM runtime environment */
func (self *_Runtime) FullInit(alloc_with_pool bool, heap_buf []byte,
max_thread_num uint) error {
var heap_buf_C *C.uchar
if (self.initialized) {
return nil
}
if (alloc_with_pool) {
if (heap_buf == nil) {
return fmt.Errorf("Failed to init WAMR runtime")
}
heap_buf_C = (*C.uchar)(unsafe.Pointer(&heap_buf[0]))
}
if (!C.init_wamr_runtime((C.bool)(alloc_with_pool), heap_buf_C,
(C.uint)(len(heap_buf)),
(C.uint)(max_thread_num))) {
return fmt.Errorf("Failed to init WAMR runtime")
}
self.initialized = true
return nil
}
/* Initialize the WASM runtime environment */
func (self *_Runtime) Init() error {
return self.FullInit(false, nil, 1)
}
/* Destroy the WASM runtime environment */
func (self *_Runtime) Destroy() {
if (self.initialized) {
C.wasm_runtime_destroy()
self.initialized = false
}
}
/* Set log verbose level (0 to 5, default is 2),
larger level with more log */
func (self *_Runtime) SetLogLevel(level LogLevel) {
C.bh_log_set_verbose_level(C.uint32_t(level))
}
/*
func (self *_Runtime) RegisterNatives(moduleName string,
nativeSymbols []NativeSymbol) {
}
*/ /* TODO */
func (self *_Runtime) InitThreadEnv() bool {
if (!C.wasm_runtime_init_thread_env()) {
return false
}
return true
}
func (self *_Runtime) DestroyThreadEnv() {
C.wasm_runtime_destroy_thread_env();
}
func (self *_Runtime) ThreadEnvInited() bool {
if (!C.wasm_runtime_thread_env_inited()) {
return false
}
return true
}
/* Allocate memory from runtime memory environment */
func (self *_Runtime) Malloc(size uint32) *uint8 {
ptr := C.wasm_runtime_malloc((C.uint32_t)(size))
return (*uint8)(ptr)
}
/* Free memory to runtime memory environment */
func (self *_Runtime) Free(ptr *uint8) {
C.wasm_runtime_free((unsafe.Pointer)(ptr))
}