Add parsing of export table

This commit is contained in:
CSDUMMI 2025-05-10 19:47:03 +02:00
parent 2b3c39d18c
commit 64f9409f9c

41
wai.c
View file

@ -28,6 +28,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <inttypes.h>
enum section { enum section {
Section_Custom, Section_Custom,
@ -51,10 +52,24 @@ struct stack {
size_t count; size_t count;
}; };
#define MAX_FUNCTIONS 128
enum export_desc {
Export_Func,
Export_Table,
Export_Mem,
Export_Global,
};
struct export_t {
u_char name[128];
size_t name_length;
uint32_t index;
enum export_desc description;
};
struct module { struct module {
struct type_t *types; struct type_t *types;
u_char *funcs[128]; u_char *funcs[MAX_FUNCTIONS];
struct table_t *tables; struct table_t *tables;
struct mem_t *mems; struct mem_t *mems;
struct global_t *globals; struct global_t *globals;
@ -62,7 +77,7 @@ struct module {
struct data_t *datas; struct data_t *datas;
struct start_t *start; struct start_t *start;
struct import_t *imports; struct import_t *imports;
struct export_t *exports; struct export_t exports[MAX_FUNCTIONS];
u_char *binary; u_char *binary;
struct stack stack; struct stack stack;
int scope; int scope;
@ -271,19 +286,27 @@ int parse_section(struct module *module, u_char *binary, int len) {
case Section_Export: case Section_Export:
printf("section: exports\n"); printf("section: exports\n");
int num_exports = binary[i]; int num_exports = binary[i];
if(num_exports > MAX_FUNCTIONS) {
printf("Number of exports exceeds maximum number of functions in a module (%d)", MAX_FUNCTIONS);
return -1;
}
incr(i, len); incr(i, len);
for (int exports_i = 0; exports_i < num_exports; exports_i++) { for (int exports_i = 0; exports_i < num_exports; exports_i++) {
int string_len = binary[i]; struct export_t *export = &module->exports[i];
export->name_length = binary[i];
incr(i, len); incr(i, len);
printf("export name: ");
for (int si = 0; si < string_len; si++) { for (int si = 0; si < export->name_length; si++) {
putchar(binary[i]); export->name[si] = binary[i];
incr(i, len); incr(i, len);
} }
putchar('\n'); export->description = (int) binary[i];
// export kind
incr(i, len); incr(i, len);
// export func index export->index = (uint32_t) binary[i];
printf("export name: %s of type %d\n", export->name, export->description);
incr(i, len); incr(i, len);
} }
break; break;