protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

commit ea7bfc7db638a22f789ba1a666673aea7bedf084
parent 58363be5add8a0cc92a69d6776a4c00543413281
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  5 Jul 2021 10:36:52 -0700

parse memory section

Diffstat:
Msrc/wasm.c | 46++++++++++++++++++++++++++++++++++++++++++++--
Msrc/wasm.h | 6++++++
2 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/src/wasm.c b/src/wasm.c @@ -338,6 +338,20 @@ static const char *reftype_name(enum reftype reftype) return "unknown_reftype"; } +static void print_memory_section(struct memsec *memory) +{ + int i; + struct limits *mem; + + debug("%d memory:\n", memory->num_mems); + for (i = 0; i < memory->num_mems; i++) { + mem = &memory->mems[i]; + debug(" "); + print_limits(mem); + debug("\n"); + } +} + static void print_table_section(struct tablesec *section) { int i; @@ -399,6 +413,7 @@ static void print_module(struct module *module) print_import_section(&module->import_section); print_export_section(&module->export_section); print_table_section(&module->table_section); + print_memory_section(&module->memory_section); //print_code_section(&module->code_section); } @@ -805,6 +820,30 @@ static int parse_table(struct wasm_parser *p, struct table *table) return 1; } +static int parse_memory_section(struct wasm_parser *p, + struct memsec *memory_section) +{ + struct limits *mems; + unsigned int elems, i; + + if (!parse_vector(p, sizeof(*mems), &elems, (void**)&mems)) { + note_error(p, "mems vector"); + return 0; + } + + for (i = 0; i < elems; i++) { + if (!parse_limits(p, &mems[i])) { + note_error(p, "memory #%d/%d", i+1, elems); + return 0; + } + } + + memory_section->num_mems = elems; + memory_section->mems = mems; + + return 1; +} + static int parse_table_section(struct wasm_parser *p, struct tablesec *table_section) { @@ -1040,8 +1079,11 @@ static int parse_section_by_tag(struct wasm_parser *p, enum section_tag tag, } return 1; case section_memory: - note_error(p, "section_memory parse not implemented"); - return 0; + if (!parse_memory_section(p, &p->module.memory_section)) { + note_error(p, "memory section"); + return 0; + } + return 1; case section_global: note_error(p, "section_global parse not implemented"); return 0; diff --git a/src/wasm.h b/src/wasm.h @@ -71,6 +71,11 @@ struct tablesec { int num_tables; }; +struct memsec { + struct limits *mems; /* memtype */ + int num_mems; +}; + struct funcsec { unsigned int *type_indices; int num_indices; @@ -304,6 +309,7 @@ struct module { struct exportsec export_section; struct codesec code_section; struct tablesec table_section; + struct memsec memory_section; }; struct wasm_interp {