aboutsummaryrefslogtreecommitdiff
path: root/examples/minimal.c
blob: 8812ac541a7ba2ab4d517a5e8a439add3defe8d0 (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
/*
 * 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 <stddef.h>
#include <stdlib.h>

static int io_read(void *const buf, const size_t n, void *const user)
{
    return -1;
}

static enum nw_state io_seek(const long offset, void *const user)
{
    return -1;
}

static enum nw_state io_tell(long *const out, void *const user)
{
    return -1;
}

static int io_eof(void *const user)
{
    return -1;
}

static int push(const void *const src, const size_t n, void *const user)
{
    return -1;
}

static int pop(void *const dst, const size_t n, void *const user)
{
    return -1;
}

static size_t ptr(void *const user)
{
    return 0;
}

static int load(const nw_varuint32 offset, void *const dst, const size_t n,
    void *const user)
{
    return -1;
}

static int store(const nw_varuint32 offset, const void *const src,
    const size_t n, void *const user)
{
    return -1;
}

int main(int argc, char *argv[])
{
    const struct nw_io_cfg io =
    {
        .read = io_read,
        .seek = io_seek,
        .tell = io_tell,
        .eof = io_eof
    };

    const struct nw_mod_cfg cfg =
    {
        .io = io
    };

    struct nw_mod m;
    struct nw_mod_out mout;

    nw_init(&m, &cfg);

again:

    switch (nw_load(&m, &mout))
    {
        case NW_OK:
            break;

        case NW_AGAIN:
            goto again;

        case NW_FATAL:
            return EXIT_FAILURE;
    }

    const struct nw_inst_cfg icfg =
    {
        .interp_cfg =
        {
            .io = io,
            .m = &m,
            .stack =
            {
                .push = push,
                .pop = pop,
                .ptr = ptr
            },

            .linear =
            {
                .load = load,
                .store = store
            }
        }
    };

    struct nw_inst inst;

    if (nw_start(&inst, &icfg))
        return EXIT_FAILURE;

again2:

    switch (nw_run(&inst))
    {
        case NW_OK:
            break;

        case NW_AGAIN:
            goto again2;

        case NW_FATAL:
            return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}