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
|
/*
* 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 <nanowasm/types.h>
#include <nw/interp.h>
#include <nw/io.h>
#include <nw/log.h>
#include <nw/routines.h>
static enum nw_state seek_code(struct nw_interp *const i)
{
const struct nw_i_sm_ffn *const f = &i->sm.ffn;
const struct nw_io_cfg *const cfg = &i->cfg.io;
const unsigned long fbo = nwp_leuint32(&f->fbo);
const enum nw_state n = cfg->seek(fbo, cfg->user);
if (n)
return n;
f->next(i);
return NW_AGAIN;
}
static enum nw_state get_fbo(struct nw_interp *const i)
{
struct nw_i_sm_ffn *const f = &i->sm.ffn;
const struct nw_io_cfg *const cfg = &i->cfg.io;
const enum nw_state n = nwp_io_read(cfg, &f->io, cfg->user);
if (n)
return n;
i->next = seek_code;
return NW_AGAIN;
}
static enum nw_state seek_fbo(struct nw_interp *const i)
{
struct nw_i_sm_ffn *const f = &i->sm.ffn;
const struct nw_mod *const m = i->cfg.m;
long offset = m->c_sections[NW_CUSTOM_FBO];
const struct nw_io_cfg *const cfg = &i->cfg.io;
enum nw_state n;
if (!offset)
{
static const char *const exc = "nw_fbo section not found";
#ifdef NW_LOG
nwp_log("%s\n", exc);
#endif
i->exception = exc;
return NW_FATAL;
}
else if (f->fn.index < m->import_count)
{
static const char *const exc = "invalid function index";
i->exception = exc;
#ifdef NW_LOG
nwp_log("%s: %lu\n", exc, (unsigned long)f->fn.index);
#endif
return NW_FATAL;
}
offset += sizeof f->fbo * (f->fn.index - m->import_count);
if ((n = cfg->seek(offset, cfg->user)))
return n;
else
{
struct nw_sm_io io = {0};
io.buf = &f->fbo;
io.n = sizeof f->fbo;
f->io = io;
i->next = get_fbo;
}
return NW_AGAIN;
}
void nwp_find_function(struct nw_interp *const i, const struct nw_fn *const fn,
void (*const next)(struct nw_interp *))
{
const struct nw_i_sm_ffn f = {0};
struct nw_i_sm_ffn *const pf = &i->sm.ffn;
*pf = f;
pf->fn = *fn;
pf->next = next;
i->next = seek_fbo;
}
|