/* * 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 #include #include #include #include static enum nw_state get_content_type(struct nw_inst *const i); static enum nw_state store(struct nw_inst *const inst) { struct nw_inst_sm_gl *const g = &inst->sm.global; struct nw_interp *const i = &inst->interp; const enum nw_state n = nwp_global_store(i, &g->io, g->entry_i); if (n) return n; inst->next = ++g->entry_i >= g->count ? g->next : get_content_type; return NW_AGAIN; } static enum nw_state pop(struct nw_inst *const inst) { struct nw_inst_sm_gl *const g = &inst->sm.global; struct nw_interp *const i = &inst->interp; const enum nw_state n = nwp_stack_pop(i, &g->io); if (n) return n; else { struct nw_sm_io io = {0}; io.buf = &g->out; io.n = sizeof g->out; g->io = io; inst->next = store; } return NW_AGAIN; } static enum nw_state loop(struct nw_inst *const i) { struct nw_inst_sm_gl *const g = &i->sm.global; const enum nw_state n = nwp_interp_run(&i->interp); size_t sz; if (n) return n; else if (nwp_type_sz(g->out.type, &sz)) { #ifdef NW_LOG static const char *const exc = "invalid type"; nwp_log("%s: %#x\n", exc, (unsigned)g->out.type); #endif return NW_FATAL; } else { struct nw_sm_io io = {0}; io.buf = &g->out.value; io.n = sz; g->io = io; i->next = pop; } return NW_AGAIN; } static enum nw_state get_mutability(struct nw_inst *const inst) { struct nw_inst_sm_gl *const g = &inst->sm.global; struct nw_interp *const i = &inst->interp; const struct nw_io_cfg *const cfg = &i->cfg.io; struct nw_sm_leb128 *const l = &g->leb128; const enum nw_state n = nwp_varuint1(cfg, l, &g->out.mutability, cfg->user); if (n) return n; nwp_interp_resume(i); inst->next = loop; return NW_AGAIN; } static enum nw_state get_content_type(struct nw_inst *const i) { nw_varint7 content_type; struct nw_inst_sm_gl *const g = &i->sm.global; const struct nw_io_cfg *const cfg = &i->interp.cfg.io; struct nw_sm_leb128 *const l = &g->leb128; const enum nw_state n = nwp_varint7(cfg, l, &content_type, cfg->user); if (n) return n; else if (nwp_get_type(content_type, &g->out.type)) { #ifdef NW_LOG static const char *const exc = "invalid type"; nwp_log("%s: %#x\n", exc, (unsigned)content_type); #endif return NW_FATAL; } i->next = get_mutability; return NW_AGAIN; } static enum nw_state get_count(struct nw_inst *const i) { struct nw_inst_sm_gl *const g = &i->sm.global; struct nw_sm_leb128 *const l = &g->leb128; const struct nw_interp_cfg *const icfg = &i->interp.cfg; const struct nw_io_cfg *const cfg = &icfg->io; const nw_varuint32 expected = icfg->m->global_count; const enum nw_state n = nwp_varuint32(cfg, l, &g->count, cfg->user); if (n) return n; else if (g->count != expected) { #ifdef NW_LOG static const char *const exc = "global count mismatch"; nwp_log("%s, expected %lu, got %lu\n", exc, (unsigned long)expected, (unsigned long)g->count); #endif return NW_FATAL; } i->next = g->count ? get_content_type : g->next; return NW_AGAIN; } static enum nw_state seek_global(struct nw_inst *const i) { struct nw_inst_sm_gl *const g = &i->sm.global; const struct nw_interp_cfg *const icfg = &i->interp.cfg; const struct nw_io_cfg *const cfg = &icfg->io; const long offset = icfg->m->sections[NW_SECTION_GLOBAL]; enum nw_state n; if (!offset) { i->next = g->next; return NW_AGAIN; } else if ((n = cfg->seek(offset, cfg->user))) return n; i->next = get_count; return NW_AGAIN; } void nwp_init_globals(struct nw_inst *const i, enum nw_state (*const next)(struct nw_inst *)) { const struct nw_inst_sm_gl gl = {0}; struct nw_inst_sm_gl *const p = &i->sm.global; *p = gl; p->next = next; i->next = seek_global; }