aboutsummaryrefslogtreecommitdiff
path: root/src/routines/section/import.c
blob: 26ef49a4ac033fde221199e540bc71923ab5f2f6 (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * nanowasm, a tiny WebAssembly/Wasm interpreter
 * Copyright (C) 2023-2025  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/.
 */

#include <nanowasm/nw.h>
#include <nw/io.h>
#include <nw/log.h>
#include <nw/routines.h>
#include <string.h>

static enum nw_state entry_loop(struct nw_mod *);
static enum nw_state find_import(struct nw_mod *);

static enum nw_state get_function_type(struct nw_mod *const m)
{
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    struct nw_sm_leb128 *const l = &imp->leb128;
    struct nw_import_index *const ii = &m->cfg.imp_indexes[imp->imp_i];
    nw_varuint32 type;
    const enum nw_state n = nwp_varuint32(cfg, l, &type, cfg->user);

    if (n)
        return n;
    else if (imp->entry_i >= m->cfg.n_imports)
    {
#ifdef NW_LOG
        nwp_log("too many import entries\n");
#endif
        return NW_FATAL;
    }

    ii->index = imp->entry_i++;
    m->next = entry_loop;
    return NW_AGAIN;
}

static enum nw_state skip_import(struct nw_mod *const m)
{
    struct nw_sm_imp *const imp = &m->sm.import;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    const enum nw_state n = cfg->seek(imp->mod_off, cfg->user);

    if (n)
        return n;

    imp->len_i = 0;
    imp->imp_i++;
    m->next = find_import;
    return NW_AGAIN;
}

static enum nw_state get_kind(struct nw_mod *const m)
{
    unsigned char kind;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
    struct nw_sm_io io = {0};
    enum nw_state n;

    io.buf = &kind;
    io.n = sizeof kind;

    if ((n = nwp_io_read(cfg, &io, cfg->user)))
        return n;
    else if (kind != i->kind)
        m->next = skip_import;
    /* TODO: process every import type. */
    else if (kind != NW_KIND_FUNCTION)
        m->next = nwp_section_exit;
    else
        m->next = get_function_type;

    return NW_AGAIN;
}

static enum nw_state compare_field(struct nw_mod *const m)
{
    unsigned char byte;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
    struct nw_sm_io io = {0};
    enum nw_state n;

    io.buf = &byte;
    io.n = sizeof byte;

    if ((n = nwp_io_read(cfg, &io, cfg->user)))
        return n;
    else if (byte != i->field[imp->len_i])
        m->next = skip_import;
    else if (++imp->len_i >= imp->field_len)
    {
        m->next = get_kind;
        imp->len_i = 0;
    }

    return NW_AGAIN;
}

static enum nw_state get_field_offset(struct nw_mod *const m)
{
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    const enum nw_state n = cfg->tell(&imp->field_off, cfg->user);
    const struct nw_import *const i = &m->cfg.imports[imp->imp_i];

    if (n)
        return n;

    m->next = imp->field_len != strlen(i->field) ? skip_import : compare_field;
    return NW_AGAIN;
}

static enum nw_state get_field_len(struct nw_mod *const m)
{
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    struct nw_sm_leb128 *const l = &imp->leb128;
    const enum nw_state n = nwp_varuint32(cfg, l, &imp->field_len, cfg->user);

    if (n)
        return n;

    m->next = get_field_offset;
    return NW_AGAIN;
}

static enum nw_state compare_name(struct nw_mod *const m)
{
    unsigned char byte;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
    struct nw_sm_io io = {0};
    enum nw_state n;

    io.buf = &byte;
    io.n = sizeof byte;

    if ((n = nwp_io_read(cfg, &io, cfg->user)))
        return n;
    else if (byte != i->module[imp->len_i])
        m->next = skip_import;
    else if (++imp->len_i >= imp->mod_len)
    {
        m->next = get_field_len;
        imp->len_i = 0;
    }

    return NW_AGAIN;
}

static enum nw_state dump_import_field_name(struct nw_mod *const m)
{
    unsigned char byte;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_io io = {0};
    struct nw_sm_imp *const imp = &m->sm.import;
    enum nw_state n;

    io.buf = &byte;
    io.n = sizeof byte;

    if ((n = nwp_io_read(cfg, &io, cfg->user)))
        return n;

#ifdef NW_LOG
    nwp_log("%c", (char)byte);
#endif

    if (++imp->len_i >= imp->field_len)
    {
#ifdef NW_LOG
        nwp_log("\n");
#endif
        return NW_FATAL;
    }

    return NW_AGAIN;
}

static enum nw_state rewind_to_field_name(struct nw_mod *const m)
{
    struct nw_sm_imp *const imp = &m->sm.import;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    const enum nw_state n = cfg->seek(imp->field_off, cfg->user);

    if (n)
        return n;

    m->next = dump_import_field_name;
    return NW_AGAIN;
}

static enum nw_state dump_import_mod_name(struct nw_mod *const m)
{
    unsigned char byte;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    struct nw_sm_io io = {0};
    enum nw_state n;

    io.buf = &byte;
    io.n = sizeof byte;

    if ((n = nwp_io_read(cfg, &io, cfg->user)))
        return n;

#ifdef NW_LOG
    nwp_log("%c", (char)byte);
#endif

    if (++imp->len_i >= imp->mod_len)
    {
        imp->len_i = 0;

        if (imp->field_off)
        {
#ifdef NW_LOG
            nwp_log("::");
#endif
            m->next = rewind_to_field_name;
        }
        else
        {
#ifdef NW_LOG
            nwp_log("\n");
#endif
            return NW_FATAL;
        }
    }

    return NW_AGAIN;
}

static enum nw_state rewind_to_mod_name(struct nw_mod *const m)
{
    struct nw_sm_imp *const imp = &m->sm.import;
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    const enum nw_state n = cfg->seek(imp->mod_off, cfg->user);

    if (n)
        return n;

    m->next = dump_import_mod_name;
    return NW_AGAIN;
}

static enum nw_state find_import(struct nw_mod *const m)
{
    struct nw_sm_imp *const imp = &m->sm.import;
    const struct nw_import *i;

    if (imp->imp_i >= m->cfg.n_imports)
    {
#ifdef NW_LOG
        nwp_log("required import: ");
#endif
        m->next = rewind_to_mod_name;
        return NW_AGAIN;
    }

    i = &m->cfg.imports[imp->imp_i];

    if (imp->mod_len == strlen(i->module))
        m->next = compare_name;
    else
        imp->imp_i++;

    return NW_AGAIN;
}

static enum nw_state get_module_offset(struct nw_mod *const m)
{
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    const enum nw_state n = cfg->tell(&imp->mod_off, cfg->user);

    if (n)
        return n;

    m->next = find_import;
    return NW_AGAIN;
}

static enum nw_state get_module_len(struct nw_mod *const m)
{
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    struct nw_sm_leb128 *const l = &imp->leb128;
    const enum nw_state n = nwp_varuint32(cfg, l, &imp->mod_len, cfg->user);

    if (n)
        return n;

    m->next = get_module_offset;
    return NW_AGAIN;
}

static enum nw_state entry_loop(struct nw_mod *const m)
{
    struct nw_sm_imp *const imp = &m->sm.import;

    imp->len_i = 0;
    imp->imp_i = 0;
    m->next = imp->entry_i < m->import_count ?
        get_module_len : nwp_section_exit;
    return NW_AGAIN;
}

static enum nw_state get_count(struct nw_mod *const m)
{
    const struct nw_io_cfg *const cfg = &m->cfg.io;
    struct nw_sm_imp *const imp = &m->sm.import;
    struct nw_sm_leb128 *const l = &imp->leb128;
    const enum nw_state n = nwp_varuint32(cfg, l, &m->import_count, cfg->user);

    if (n)
        return n;

    m->next = entry_loop;
    return NW_AGAIN;
}

void nwp_section_import(struct nw_mod *const m)
{
    const struct nw_sm_imp imp = {0};

    m->next = get_count;
    m->sm.import = imp;
}