==> include/wasm/module.h <==1/* @ajax/wasm-module: a decoded module (what @ajax/wasm-decode produces, @ajax/wasm-validate checks
2 * and @ajax/wasm-interp runs). Everything points into the binary it came from, which must outlive
3 * the module; only the arrays are owned. Index spaces put imports first, as the spec does. */
4#ifndef WASM_MODULE_H
5#define WASM_MODULE_H
6
7#include "wasm/types.h"
8
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdint.h>
12
13typedef struct {
14 uint32_t nparams, nresults;
15 const wasm_valtype *params; /* into the binary */
16 const wasm_valtype *results; /* into the binary */
17} wasm_functype;
18
19typedef struct {
20 uint64_t min, max; /* max only when flags has WASM_LIMITS_HAS_MAX */
21 uint8_t flags;
22} wasm_limits;
23
24typedef struct {
25 wasm_valtype elem;
26 wasm_limits limits;
27} wasm_tabletype;
28
29typedef struct {
30 wasm_valtype type;
31 bool mut;
32} wasm_globaltype;
33
34/* A constant expression or a function body: bytes up to and including its final `end`. */
35typedef struct {
36 const uint8_t *start;
37 size_t len;
38 size_t offset; /* of start in the binary */
39} wasm_expr;
40
41typedef struct {
42 const uint8_t *module, *name; /* UTF-8, not NUL-terminated */
43 uint32_t module_len, name_len;
44 uint8_t kind; /* WASM_EXTERN_* */
45 uint32_t func_type;
46 wasm_tabletype table;
47 wasm_limits mem;
48 wasm_globaltype global;
49} wasm_import;
50
51typedef struct {
52 const uint8_t *name;
53 uint32_t name_len;
54 uint8_t kind;
55 uint32_t index;
56} wasm_export;
57
58enum { WASM_SEG_ACTIVE, WASM_SEG_PASSIVE, WASM_SEG_DECLARATIVE };
59
60typedef struct {
61 uint8_t mode;
62 uint32_t table;
63 wasm_expr offset; /* active only */
64 wasm_valtype type; /* WASM_FUNCREF or WASM_EXTERNREF */
65 uint32_t count;
66 uint32_t *funcs; /* function indices (flags 0-3), or NULL */
67 wasm_expr *exprs; /* element expressions (flags 4-7), or NULL */
68 size_t offset_in_binary;
69} wasm_elem;
70
71typedef struct {
72 uint8_t mode; /* active or passive */
73 uint32_t mem;
74 wasm_expr offset;
75 const uint8_t *bytes;
76 uint32_t len;
77 size_t offset_in_binary;
78} wasm_data;
79
80typedef struct {
81 uint32_t count;
82 wasm_valtype type;
83} wasm_localgroup;
84
85typedef struct {
86 wasm_localgroup *locals;
87 uint32_t nlocal_groups;
88 uint64_t nlocals; /* declared locals, parameters not included */
89 wasm_expr body; /* the instructions after the local declarations */
90 size_t offset; /* of the entry (its size field) in the binary */
91} wasm_code;
92
93typedef struct wasm_module {
94 const uint8_t *bytes;
95 size_t len;
96 wasm_functype *types;
97 uint32_t ntypes;
98 wasm_import *imports;
99 uint32_t nimports;
100 uint32_t *funcs; /* type index of every function, imports first */
101 uint32_t nfuncs, nfunc_imports;
102 wasm_tabletype *tables;
103 uint32_t ntables, ntable_imports;
104 wasm_limits *mems;
105 uint32_t nmems, nmem_imports;
106 wasm_globaltype *globals;
107 uint32_t nglobals, nglobal_imports;
108 wasm_expr *global_inits; /* defined globals only: global i's at [i - nglobal_imports] */
109 wasm_export *exports;
110 uint32_t nexports;
111 bool has_start;
112 uint32_t start;
113 wasm_elem *elems;
114 uint32_t nelems;
115 bool has_datacount;
116 uint32_t datacount;
117 wasm_code *codes; /* defined functions: function i's at [i - nfunc_imports] */
118 uint32_t ncodes;
119 wasm_data *datas;
120 uint32_t ndatas;
121 /* Offsets of the code section's start, for messages. */
122 size_t code_section_offset;
123} wasm_module;
124
125/* Frees the arrays (not the binary). Safe on a zeroed or partly decoded module. */
126void wasm_module_free(wasm_module *m);
127
128/* The type of function f (imports first). */
129static inline const wasm_functype *wasm_func_type(const wasm_module *m, uint32_t f) { return &m->types[m->funcs[f]]; }
130
131/* The export named name of kind, or NULL. */
132const wasm_export *wasm_module_export(const wasm_module *m, const char *name, uint8_t kind);
133
134#endif