/* * 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 #include #include #include #include static enum nw_state entry_loop(struct nw_mod *); static enum nw_state get_return_type(struct nw_mod *const m) { struct nw_sm_st *const st = &m->sm.type; struct nw_sm_leb128 *const l = &st->leb128; const struct nw_io_cfg *const cfg = &m->cfg.io; nw_varint7 return_type; const enum nw_state n = nwp_varint7(cfg, l, &return_type, cfg->user); if (n) return n; m->next = entry_loop; st->entry_i++; return NW_AGAIN; } static enum nw_state get_return_count(struct nw_mod *const m) { struct nw_sm_st *const st = &m->sm.type; struct nw_sm_leb128 *const l = &st->leb128; const struct nw_io_cfg *const cfg = &m->cfg.io; nw_varuint1 return_count; const enum nw_state n = nwp_varuint1(cfg, l, &return_count, cfg->user); if (n) return n; else if (return_count) m->next = get_return_type; else { m->next = entry_loop; st->entry_i++; } return NW_AGAIN; } static enum nw_state param_loop(struct nw_mod *const m) { struct nw_sm_st *const st = &m->sm.type; const struct nw_io_cfg *const cfg = &m->cfg.io; struct nw_sm_leb128 *const l = &st->leb128; nw_varint7 type; enum nw_state n; if (st->p_i >= st->param_count) { m->next = get_return_count; return NW_AGAIN; } else if ((n = nwp_varint7(cfg, l, &type, cfg->user))) return n; st->p_i++; return NW_AGAIN; } static enum nw_state get_param_count(struct nw_mod *const m) { struct nw_sm_st *const st = &m->sm.type; struct nw_sm_leb128 *const l = &st->leb128; const struct nw_io_cfg *const cfg = &m->cfg.io; const enum nw_state n = nwp_varuint32(cfg, l, &st->param_count, cfg->user); if (n) return n; st->p_i = 0; m->next = param_loop; return NW_AGAIN; } static int check_form(const nw_varint7 form) { static const nw_varint7 v[] = { /* TODO: move values to enums? */ 0x7f, /* i32 */ 0x7e, /* i64 */ 0x7d, /* f32 */ 0x7c, /* f64 */ 0x70, /* anyfunc */ 0x60, /* func */ 0x40 /* empty block_type */ }; size_t i; for (i = 0; i < sizeof v / sizeof *v; i++) if (form == v[i]) return 0; return -1; } static enum nw_state get_form(struct nw_mod *const m) { struct nw_sm_st *const st = &m->sm.type; struct nw_sm_leb128 *const l = &st->leb128; const struct nw_io_cfg *const cfg = &m->cfg.io; nw_varint7 form; const enum nw_state n = nwp_varint7(cfg, l, &form, cfg->user); if (n) return n; else if (check_form(form)) { #ifdef NW_LOG nwp_log("invalid form %#x\n", (unsigned)form); #endif return NW_FATAL; } m->next = get_param_count; return NW_AGAIN; } static enum nw_state entry_loop(struct nw_mod *const m) { struct nw_sm_st *const st = &m->sm.type; m->next = st->entry_i < m->type_count ? get_form : 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_st *const st = &m->sm.type; struct nw_sm_leb128 *const l = &st->leb128; const enum nw_state n = nwp_varuint32(cfg, l, &m->type_count, cfg->user); if (n) return n; m->next = entry_loop; return NW_AGAIN; } void nwp_section_type(struct nw_mod *const m) { const struct nw_sm_st st = {0}; m->next = get_count; m->sm.type = st; }