/* * 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 seek_pc(struct nw_interp *const i) { struct nw_find_param *const fp = i->state; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = cfg->seek(fp->pc, cfg->user); if (n) return n; fp->out.addr = i->fr.fr_start - fp->sz; i->next = fp->next; return NW_AGAIN; } static enum nw_state get_param_type(struct nw_interp *const i) { nw_varint7 type; struct nw_find_param *const fp = i->state; struct nw_sm_leb128 *const l = &fp->leb128; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = nwp_varint7(cfg, l, &type, cfg->user); enum nw_type param_type; size_t sz; if (n) return n; else if (nwp_get_type(type, ¶m_type) || nwp_type_sz(param_type, &sz)) { static const char *const exc = "invalid param type"; i->exception = exc; #ifdef NW_LOG nwp_log("%s: %#x\n", exc, (unsigned)type); #endif return NW_FATAL; } if (fp->param_i == fp->index) fp->out.type = param_type; if (fp->param_i >= fp->index) fp->sz += sz; if (++fp->param_i >= i->fr.fn.param_count) i->next = seek_pc; return NW_AGAIN; } static enum nw_state seek_param_types(struct nw_interp *const i) { const struct nw_io_cfg *const cfg = &i->cfg.io; const long offset = i->fr.fn.param_types; const enum nw_state n = cfg->seek(offset, cfg->user); if (n) return n; i->next = get_param_type; return NW_AGAIN; } static enum nw_state tell(struct nw_interp *const i) { struct nw_find_param *const fp = i->state; const struct nw_io_cfg *const cfg = &i->cfg.io; const enum nw_state n = cfg->tell(&fp->pc, cfg->user); if (n) return n; i->next = seek_param_types; return NW_AGAIN; } void nwp_find_param(struct nw_interp *const i, struct nw_find_param *const f, const nw_varuint32 index, enum nw_state (*const next)(struct nw_interp *), void *const args) { const struct nw_find_param fp = {0}; *f = fp; f->index = index; f->next = next; i->state = f; i->next = tell; i->args = args; }