-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinstance.h
192 lines (173 loc) · 6.93 KB
/
instance.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdbool.h>
#include <stdint.h>
#include "platform.h"
struct module;
struct instance;
struct exec_context;
struct funcinst;
struct functype;
struct resulttype;
struct import;
struct import_object;
struct import_object_entry;
struct mem_context;
struct report;
struct name;
struct val;
__BEGIN_EXTERN_C
/*
* This API is inspired from js-api.
* https://webassembly.github.io/spec/js-api/index.html#instances
*/
int instance_create(struct mem_context *mctx, const struct module *m,
struct instance **instp,
const struct import_object *imports,
struct report *report);
/*
* Instead of instance_create, you can use instance_create_no_init and
* then instance_execute_init. That way you might be able to handle
* some kind of errors more flexibly. These functions are introduced
* to deal with some of corner cases you can see in opam-2.0.0 linking.wast.
* cf. https://github.com/WebAssembly/spec/issues/1530
*
* note than instance_execute_init can return a restartable error.
* see also: instance_execute_continue.
*/
int instance_create_no_init(struct mem_context *mctx, const struct module *m,
struct instance **instp,
const struct import_object *imports,
struct report *report);
int instance_execute_init(struct exec_context *ctx);
/*
* Note: If you have multiple instances linked together
* with import/export, usually the only safe way to destroy those
* instances is to destroy them together at once after ensuring none of
* them are running. Note that import/export is not necessarily a one-way
* dependency. Because funcref values, which are implemented as bare host
* pointers in this engine, can be freely passed among instances, linked
* instances can have references each other.
* cf. https://github.com/WebAssembly/spec/issues/1513
*/
void instance_destroy(struct instance *inst);
/*
* instance_execute_func:
*
* execute the function.
*
* in addition to usual <errno.h> error numbers, this function can
* return a few toywasm-specific errors.
* see the comment on ETOYWASMxxx macros in exec_context.h.
*/
int instance_execute_func(struct exec_context *ctx, uint32_t funcidx,
const struct resulttype *paramtype,
const struct resulttype *resulttype);
/*
* instance_execute_func_nocheck is meant to be used where the caller
* already knows the function type for sure.
*/
int instance_execute_func_nocheck(struct exec_context *ctx, uint32_t funcidx);
/*
* instance_execute_continue:
*
* when one of instance_execute_xxx functions including the following ones
* returned a restartable error, the app can resume the execution by calling
* this function.
*
* - instance_execute_init
* - instance_execute_func
* - instance_execute_func_nocheck
* - instance_execute_continue
*
* see also: instance_execute_handle_restart
*/
int instance_execute_continue(struct exec_context *ctx);
/*
* instance_execute_handle_restart:
*
* an equivalent of keep calling instance_execute_handle_restart_once
* until it returns a success or a non-restartable error.
*/
int instance_execute_handle_restart(struct exec_context *ctx, int exec_ret);
/*
* instance_execute_handle_restart_once:
*
* after performing the default handling of a restartable error,
* continue the execution as instance_execute_continue does.
*
* Note: this function can return a restartable error.
*
* Note: if the given exec_ret is not a restartable error, this function
* just returns it as it is.
*/
int instance_execute_handle_restart_once(struct exec_context *ctx,
int exec_ret);
/*
* see the comment in type.h about the concept of import_object.
*/
int import_object_create_for_exports(struct mem_context *mctx,
struct instance *inst,
const struct name *module_name,
struct import_object **resultp);
void import_object_destroy(struct mem_context *mctx, struct import_object *im);
int import_object_alloc(struct mem_context *mctx, uint32_t nentries,
struct import_object **resultp);
int import_object_find_entry(
const struct import_object *impobj, const struct import *im,
int (*check)(const struct import_object_entry *e, const void *arg),
const void *checkarg, const struct import_object_entry **resultp,
struct report *report);
struct meminst;
struct memtype;
int memory_instance_create(struct mem_context *mctx, struct meminst **mip,
const struct memtype *mt);
void memory_instance_destroy(struct mem_context *mctx, struct meminst *mi);
uint32_t memory_grow(struct meminst *mi, uint32_t sz);
int memory_instance_getptr2(struct meminst *meminst, uint32_t ptr,
uint32_t offset, uint32_t size, void **pp,
bool *movedp);
struct globalinst;
struct globaltype;
int global_instance_create(struct mem_context *mctx, struct globalinst **gip,
const struct globaltype *gt);
void global_instance_destroy(struct mem_context *mctx, struct globalinst *gi);
void global_set(struct globalinst *ginst, const struct val *val);
void global_get(struct globalinst *ginst, struct val *val);
struct tableinst;
struct tabletype;
int table_instance_create(struct mem_context *mctx, struct tableinst **tip,
const struct tabletype *tt);
void table_instance_destroy(struct mem_context *mctx, struct tableinst *ti);
void table_set(struct tableinst *tinst, uint32_t elemidx,
const struct val *val);
void table_get(struct tableinst *tinst, uint32_t elemidx, struct val *val);
int table_grow(struct tableinst *tinst, const struct val *val, uint32_t n);
int table_get_func(struct exec_context *ectx, const struct tableinst *t,
uint32_t i, const struct functype *ft,
const struct funcinst **fip);
/*
* create_satisfying_shared_memories:
*
* create shared memories to satisfy all shared memory imports
* in the module.
*
* mainly for wasi-threads, where it's host's responsibility to
* provide the linear memory.
*/
int create_satisfying_shared_memories(struct mem_context *mctx,
struct mem_context *instance_mctx,
const struct module *module,
struct import_object **imop);
/*
* create_satisfying_functions:
*
* create dummy host functions to satisfy all function imports
* in the module.
*
* if the created host functions are actually called, they just trap.
* (TRAP_UNRESOLVED_IMPORTED_FUNC)
*/
int create_satisfying_functions(struct mem_context *mctx,
const struct module *module,
struct import_object **imop);
void instance_print_stats(const struct instance *inst);
__END_EXTERN_C