aboutsummaryrefslogtreecommitdiff
path: root/src/routines/section/import.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-09-07 00:04:38 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-11-06 14:38:40 +0100
commit6d9d80362f9932bbc87e162b8ef7df06c73e27e1 (patch)
treee3e228c63fe26f07503f226de7fb5086b3dc2286 /src/routines/section/import.c
downloadnanowasm-6d9d80362f9932bbc87e162b8ef7df06c73e27e1.tar.gz
First commit
Diffstat (limited to 'src/routines/section/import.c')
-rw-r--r--src/routines/section/import.c339
1 files changed, 339 insertions, 0 deletions
diff --git a/src/routines/section/import.c b/src/routines/section/import.c
new file mode 100644
index 0000000..26ef49a
--- /dev/null
+++ b/src/routines/section/import.c
@@ -0,0 +1,339 @@
+/*
+ * 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 <nw/io.h>
+#include <nw/log.h>
+#include <nw/routines.h>
+#include <string.h>
+
+static enum nw_state entry_loop(struct nw_mod *);
+static enum nw_state find_import(struct nw_mod *);
+
+static enum nw_state get_function_type(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ struct nw_import_index *const ii = &m->cfg.imp_indexes[imp->imp_i];
+ nw_varuint32 type;
+ const enum nw_state n = nwp_varuint32(cfg, l, &type, cfg->user);
+
+ if (n)
+ return n;
+ else if (imp->entry_i >= m->cfg.n_imports)
+ {
+#ifdef NW_LOG
+ nwp_log("too many import entries\n");
+#endif
+ return NW_FATAL;
+ }
+
+ ii->index = imp->entry_i++;
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+static enum nw_state skip_import(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(imp->mod_off, cfg->user);
+
+ if (n)
+ return n;
+
+ imp->len_i = 0;
+ imp->imp_i++;
+ m->next = find_import;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_kind(struct nw_mod *const m)
+{
+ unsigned char kind;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+ struct nw_sm_io io = {0};
+ enum nw_state n;
+
+ io.buf = &kind;
+ io.n = sizeof kind;
+
+ if ((n = nwp_io_read(cfg, &io, cfg->user)))
+ return n;
+ else if (kind != i->kind)
+ m->next = skip_import;
+ /* TODO: process every import type. */
+ else if (kind != NW_KIND_FUNCTION)
+ m->next = nwp_section_exit;
+ else
+ m->next = get_function_type;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state compare_field(struct nw_mod *const m)
+{
+ unsigned char byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+ struct nw_sm_io io = {0};
+ enum nw_state n;
+
+ io.buf = &byte;
+ io.n = sizeof byte;
+
+ if ((n = nwp_io_read(cfg, &io, cfg->user)))
+ return n;
+ else if (byte != i->field[imp->len_i])
+ m->next = skip_import;
+ else if (++imp->len_i >= imp->field_len)
+ {
+ m->next = get_kind;
+ imp->len_i = 0;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_field_offset(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const enum nw_state n = cfg->tell(&imp->field_off, cfg->user);
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+
+ if (n)
+ return n;
+
+ m->next = imp->field_len != strlen(i->field) ? skip_import : compare_field;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_field_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->field_len, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = get_field_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state compare_name(struct nw_mod *const m)
+{
+ unsigned char byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *const i = &m->cfg.imports[imp->imp_i];
+ struct nw_sm_io io = {0};
+ enum nw_state n;
+
+ io.buf = &byte;
+ io.n = sizeof byte;
+
+ if ((n = nwp_io_read(cfg, &io, cfg->user)))
+ return n;
+ else if (byte != i->module[imp->len_i])
+ m->next = skip_import;
+ else if (++imp->len_i >= imp->mod_len)
+ {
+ m->next = get_field_len;
+ imp->len_i = 0;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state dump_import_field_name(struct nw_mod *const m)
+{
+ unsigned char byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_io io = {0};
+ struct nw_sm_imp *const imp = &m->sm.import;
+ enum nw_state n;
+
+ io.buf = &byte;
+ io.n = sizeof byte;
+
+ if ((n = nwp_io_read(cfg, &io, cfg->user)))
+ return n;
+
+#ifdef NW_LOG
+ nwp_log("%c", (char)byte);
+#endif
+
+ if (++imp->len_i >= imp->field_len)
+ {
+#ifdef NW_LOG
+ nwp_log("\n");
+#endif
+ return NW_FATAL;
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state rewind_to_field_name(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(imp->field_off, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = dump_import_field_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state dump_import_mod_name(struct nw_mod *const m)
+{
+ unsigned char byte;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_io io = {0};
+ enum nw_state n;
+
+ io.buf = &byte;
+ io.n = sizeof byte;
+
+ if ((n = nwp_io_read(cfg, &io, cfg->user)))
+ return n;
+
+#ifdef NW_LOG
+ nwp_log("%c", (char)byte);
+#endif
+
+ if (++imp->len_i >= imp->mod_len)
+ {
+ imp->len_i = 0;
+
+ if (imp->field_off)
+ {
+#ifdef NW_LOG
+ nwp_log("::");
+#endif
+ m->next = rewind_to_field_name;
+ }
+ else
+ {
+#ifdef NW_LOG
+ nwp_log("\n");
+#endif
+ return NW_FATAL;
+ }
+ }
+
+ return NW_AGAIN;
+}
+
+static enum nw_state rewind_to_mod_name(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ const enum nw_state n = cfg->seek(imp->mod_off, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = dump_import_mod_name;
+ return NW_AGAIN;
+}
+
+static enum nw_state find_import(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const struct nw_import *i;
+
+ if (imp->imp_i >= m->cfg.n_imports)
+ {
+#ifdef NW_LOG
+ nwp_log("required import: ");
+#endif
+ m->next = rewind_to_mod_name;
+ return NW_AGAIN;
+ }
+
+ i = &m->cfg.imports[imp->imp_i];
+
+ if (imp->mod_len == strlen(i->module))
+ m->next = compare_name;
+ else
+ imp->imp_i++;
+
+ return NW_AGAIN;
+}
+
+static enum nw_state get_module_offset(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ const enum nw_state n = cfg->tell(&imp->mod_off, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = find_import;
+ return NW_AGAIN;
+}
+
+static enum nw_state get_module_len(struct nw_mod *const m)
+{
+ const struct nw_io_cfg *const cfg = &m->cfg.io;
+ struct nw_sm_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &imp->mod_len, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = get_module_offset;
+ return NW_AGAIN;
+}
+
+static enum nw_state entry_loop(struct nw_mod *const m)
+{
+ struct nw_sm_imp *const imp = &m->sm.import;
+
+ imp->len_i = 0;
+ imp->imp_i = 0;
+ m->next = imp->entry_i < m->import_count ?
+ get_module_len : 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_imp *const imp = &m->sm.import;
+ struct nw_sm_leb128 *const l = &imp->leb128;
+ const enum nw_state n = nwp_varuint32(cfg, l, &m->import_count, cfg->user);
+
+ if (n)
+ return n;
+
+ m->next = entry_loop;
+ return NW_AGAIN;
+}
+
+void nwp_section_import(struct nw_mod *const m)
+{
+ const struct nw_sm_imp imp = {0};
+
+ m->next = get_count;
+ m->sm.import = imp;
+}