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
|
/*
* nanowasm, a tiny WebAssembly/Wasm interpreter
* Copyright (C) 2023-2024 Xavier Del Campo Romero
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifndef NANOWASM_TYPES_H
#define NANOWASM_TYPES_H
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif
union nw_value
{
long i32;
long long i64;
float f32;
double f64;
/* TODO: define {de}serialization between Wasm and host. */
void *p;
};
enum nw_kind
{
NW_KIND_FUNCTION,
NW_KIND_TABLE,
NW_KIND_MEMORY,
NW_KIND_GLOBAL
};
struct nw_args
{
const union nw_value *args;
size_t n;
};
struct nw_buf
{
void *buf;
size_t n;
};
enum nw_state
{
NW_STATE_AGAIN,
NW_STATE_RETURNED,
NW_STATE_EXCEPTION,
NW_STATE_FATAL
};
struct nw_mod_cfg
{
const struct nw_import
{
enum nw_kind kind;
const char *module, *field;
union
{
struct
{
/* This must follow the format below, whitespace ignored:
* [return type]([param type][, ...])
* For convenience, the interpreter can do automatic checks on
* pointer arguments and/or return values if using '*' as type.
* Examples:
* "()"
* "i32(f32)"
* "*(f64, i32, *, i64)"
* As per WebAssembly MVP spec, multiple return types are not
* supported yet. */
const char *signature;
int (*fn)(const struct nw_args *params,
union nw_value *ret, void *user);
} function;
} u;
} *imports;
size_t n_imports;
const char *path;
void *user;
};
struct nw_interp_cfg
{
struct nw_buf stack, heap, global;
const struct nw_mod *m;
};
struct nw_inst_cfg
{
struct nw_interp_cfg interp;
};
struct nw_mod
{
struct nw_sections
{
long type, import, function, table, memory, global, export,
start, element, code, data;
} sections;
size_t data_len, heap_len;
struct nw_mod_cfg cfg;
};
struct nw_inst
{
struct nw_interp
{
const struct nw_mod *m;
FILE *f;
const char *exception;
bool exit;
int retval;
struct nw_interp_cfg cfg;
size_t stack_i, global_i;
struct nw_frame *fp;
struct nw_gframe *gfp;
} interp;
union nw_inst_state
{
long retval;
const char *exception;
} state;
};
#define NW_BUF(sz) {.buf = (char [sz]){0}, .n = sz}
#ifdef __cplusplus
}
#endif
#endif
|