diff options
author | Ben Smith <binji@chromium.org> | 2016-09-22 17:39:20 -0700 |
---|---|---|
committer | Ben Smith <binji@chromium.org> | 2016-09-29 11:37:27 -0700 |
commit | b4d2bc2f3fe925a78a7e1ea061106b1f01e8dba2 (patch) | |
tree | f6ae9ce3def03e9945cf2108221b0ab2138fa42a | |
parent | cca0d93b8ba2a6ea101f18927eddfbe5ec726dd0 (diff) | |
download | wabt-b4d2bc2f3fe925a78a7e1ea061106b1f01e8dba2.tar.gz wabt-b4d2bc2f3fe925a78a7e1ea061106b1f01e8dba2.tar.bz2 wabt-b4d2bc2f3fe925a78a7e1ea061106b1f01e8dba2.zip |
parse tests for {im,ex}porting tables, etc.
24 files changed, 174 insertions, 76 deletions
diff --git a/src/prebuilt/wasm-ast-parser-gen.c b/src/prebuilt/wasm-ast-parser-gen.c index 86d0ecc1..6ffda0c7 100644 --- a/src/prebuilt/wasm-ast-parser-gen.c +++ b/src/prebuilt/wasm-ast-parser-gen.c @@ -129,14 +129,14 @@ &dummy); \ } while (0) -#define INSERT_BINDING(module, kind, kinds, loc_, item) \ - do \ - if ((item).name.start) { \ - WasmBinding* binding = wasm_insert_binding( \ - parser->allocator, &(module)->kind##_bindings, &(item).name); \ - binding->loc = loc_; \ - binding->index = (module)->kinds.size - 1; \ - } \ +#define INSERT_BINDING(module, kind, kinds, loc_, name) \ + do \ + if ((name).start) { \ + WasmBinding* binding = wasm_insert_binding( \ + parser->allocator, &(module)->kind##_bindings, &(name)); \ + binding->loc = loc_; \ + binding->index = (module)->kinds.size - 1; \ + } \ while (0) #define APPEND_INLINE_EXPORT(module, KIND, loc_, value, index_) \ @@ -150,7 +150,7 @@ APPEND_ITEM_TO_VECTOR(module, Export, export, exports, \ &export_field->export_); \ INSERT_BINDING(module, export, exports, export_field->loc, \ - export_field->export_); \ + export_field->export_.name); \ } \ while (0) @@ -3294,8 +3294,8 @@ yyreduce: #line 949 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = new_import(parser->allocator); - (yyval.import)->name = (yyvsp[-2].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_FUNC; + (yyval.import)->func.name = (yyvsp[-2].text); (yyval.import)->func.decl.flags = WASM_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE; (yyval.import)->func.decl.type_var = (yyvsp[-1].var); } @@ -3306,8 +3306,8 @@ yyreduce: #line 956 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = new_import(parser->allocator); - (yyval.import)->name = (yyvsp[-2].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_FUNC; + (yyval.import)->func.name = (yyvsp[-2].text); (yyval.import)->func.decl.sig = (yyvsp[-1].func_sig); } #line 3314 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ @@ -3317,9 +3317,9 @@ yyreduce: #line 962 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = new_import(parser->allocator); - (yyval.import)->name = (yyvsp[-2].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_TABLE; (yyval.import)->table = (yyvsp[-1].table); + (yyval.import)->table.name = (yyvsp[-2].text); } #line 3325 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; @@ -3328,9 +3328,9 @@ yyreduce: #line 968 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = new_import(parser->allocator); - (yyval.import)->name = (yyvsp[-2].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_MEMORY; (yyval.import)->memory = (yyvsp[-1].memory); + (yyval.import)->memory.name = (yyvsp[-2].text); } #line 3336 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; @@ -3339,9 +3339,9 @@ yyreduce: #line 974 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = new_import(parser->allocator); - (yyval.import)->name = (yyvsp[-2].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_GLOBAL; (yyval.import)->global = (yyvsp[-1].global); + (yyval.import)->global.name = (yyvsp[-2].text); } #line 3347 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; @@ -3360,8 +3360,8 @@ yyreduce: #line 987 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = (yyvsp[-2].import); - (yyval.import)->name = (yyvsp[-3].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_FUNC; + (yyval.import)->func.name = (yyvsp[-3].text); (yyval.import)->func.decl.flags = WASM_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE; (yyval.import)->func.decl.type_var = (yyvsp[-1].var); } @@ -3372,8 +3372,8 @@ yyreduce: #line 994 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = (yyvsp[-2].import); - (yyval.import)->name = (yyvsp[-3].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_FUNC; + (yyval.import)->func.name = (yyvsp[-3].text); (yyval.import)->func.decl.sig = (yyvsp[-1].func_sig); } #line 3380 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ @@ -3383,9 +3383,9 @@ yyreduce: #line 1000 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = (yyvsp[-2].import); - (yyval.import)->name = (yyvsp[-3].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_TABLE; (yyval.import)->table = (yyvsp[-1].table); + (yyval.import)->table.name = (yyvsp[-3].text); } #line 3391 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; @@ -3394,9 +3394,9 @@ yyreduce: #line 1006 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = (yyvsp[-2].import); - (yyval.import)->name = (yyvsp[-3].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_MEMORY; (yyval.import)->memory = (yyvsp[-1].memory); + (yyval.import)->memory.name = (yyvsp[-3].text); } #line 3402 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; @@ -3405,9 +3405,9 @@ yyreduce: #line 1012 "src/wasm-ast-parser.y" /* yacc.c:1646 */ { (yyval.import) = (yyvsp[-2].import); - (yyval.import)->name = (yyvsp[-3].text); (yyval.import)->kind = WASM_EXTERNAL_KIND_GLOBAL; (yyval.import)->global = (yyvsp[-1].global); + (yyval.import)->global.name = (yyvsp[-3].text); } #line 3413 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; @@ -3530,7 +3530,7 @@ yyreduce: APPEND_FIELD_TO_LIST((yyval.module), field, FUNC_TYPE, func_type, (yylsp[0]), (yyvsp[0].func_type)); APPEND_ITEM_TO_VECTOR((yyval.module), FuncType, func_type, func_types, &field->func_type); - INSERT_BINDING((yyval.module), func_type, func_types, (yylsp[0]), (yyvsp[0].func_type)); + INSERT_BINDING((yyval.module), func_type, func_types, (yylsp[0]), (yyvsp[0].func_type).name); } #line 3536 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; @@ -3542,7 +3542,7 @@ yyreduce: WasmModuleField* field; APPEND_FIELD_TO_LIST((yyval.module), field, GLOBAL, global, (yylsp[0]), (yyvsp[0].exported_global).global); APPEND_ITEM_TO_VECTOR((yyval.module), Global, global, globals, &field->global); - INSERT_BINDING((yyval.module), global, globals, (yylsp[0]), (yyvsp[0].exported_global).global); + INSERT_BINDING((yyval.module), global, globals, (yylsp[0]), (yyvsp[0].exported_global).global.name); APPEND_INLINE_EXPORT((yyval.module), GLOBAL, (yylsp[0]), (yyvsp[0].exported_global), (yyval.module)->globals.size - 1); } #line 3549 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ @@ -3555,7 +3555,7 @@ yyreduce: WasmModuleField* field; APPEND_FIELD_TO_LIST((yyval.module), field, TABLE, table, (yylsp[0]), (yyvsp[0].exported_table).table); APPEND_ITEM_TO_VECTOR((yyval.module), Table, table, tables, &field->table); - INSERT_BINDING((yyval.module), table, tables, (yylsp[0]), (yyvsp[0].exported_table).table); + INSERT_BINDING((yyval.module), table, tables, (yylsp[0]), (yyvsp[0].exported_table).table.name); APPEND_INLINE_EXPORT((yyval.module), TABLE, (yylsp[0]), (yyvsp[0].exported_table), (yyval.module)->tables.size - 1); if ((yyvsp[0].exported_table).has_elem_segment) { @@ -3577,7 +3577,7 @@ yyreduce: WasmModuleField* field; APPEND_FIELD_TO_LIST((yyval.module), field, MEMORY, memory, (yylsp[0]), (yyvsp[0].exported_memory).memory); APPEND_ITEM_TO_VECTOR((yyval.module), Memory, memory, memories, &field->memory); - INSERT_BINDING((yyval.module), memory, memories, (yylsp[0]), (yyvsp[0].exported_memory).memory); + INSERT_BINDING((yyval.module), memory, memories, (yylsp[0]), (yyvsp[0].exported_memory).memory.name); APPEND_INLINE_EXPORT((yyval.module), MEMORY, (yylsp[0]), (yyvsp[0].exported_memory), (yyval.module)->memories.size - 1); if ((yyvsp[0].exported_memory).has_data_segment) { @@ -3601,7 +3601,7 @@ yyreduce: append_implicit_func_declaration(parser->allocator, &(yylsp[0]), (yyval.module), &field->func.decl); APPEND_ITEM_TO_VECTOR((yyval.module), Func, func, funcs, &field->func); - INSERT_BINDING((yyval.module), func, funcs, (yylsp[0]), *(yyvsp[0].exported_func).func); + INSERT_BINDING((yyval.module), func, funcs, (yylsp[0]), (yyvsp[0].exported_func).func->name); APPEND_INLINE_EXPORT((yyval.module), FUNC, (yylsp[0]), (yyvsp[0].exported_func), (yyval.module)->funcs.size - 1); } #line 3608 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ @@ -3653,27 +3653,27 @@ yyreduce: append_implicit_func_declaration(parser->allocator, &(yylsp[0]), (yyval.module), &(yyvsp[0].import)->func.decl); APPEND_ITEM_TO_VECTOR((yyval.module), Func, func, funcs, &field->import.func); - INSERT_BINDING((yyval.module), func, funcs, (yylsp[0]), *(yyvsp[0].import)); + INSERT_BINDING((yyval.module), func, funcs, (yylsp[0]), (yyvsp[0].import)->func.name); (yyval.module)->num_func_imports++; CHECK_IMPORT_ORDERING((yyval.module), func, funcs, (yylsp[0])); break; case WASM_EXTERNAL_KIND_TABLE: APPEND_ITEM_TO_VECTOR((yyval.module), Table, table, tables, &field->import.table); - INSERT_BINDING((yyval.module), table, tables, (yylsp[0]), *(yyvsp[0].import)); + INSERT_BINDING((yyval.module), table, tables, (yylsp[0]), (yyvsp[0].import)->table.name); (yyval.module)->num_table_imports++; CHECK_IMPORT_ORDERING((yyval.module), table, tables, (yylsp[0])); break; case WASM_EXTERNAL_KIND_MEMORY: APPEND_ITEM_TO_VECTOR((yyval.module), Memory, memory, memories, &field->import.memory); - INSERT_BINDING((yyval.module), memory, memories, (yylsp[0]), *(yyvsp[0].import)); + INSERT_BINDING((yyval.module), memory, memories, (yylsp[0]), (yyvsp[0].import)->memory.name); (yyval.module)->num_memory_imports++; CHECK_IMPORT_ORDERING((yyval.module), memory, memories, (yylsp[0])); break; case WASM_EXTERNAL_KIND_GLOBAL: APPEND_ITEM_TO_VECTOR((yyval.module), Global, global, globals, &field->import.global); - INSERT_BINDING((yyval.module), global, globals, (yylsp[0]), *(yyvsp[0].import)); + INSERT_BINDING((yyval.module), global, globals, (yylsp[0]), (yyvsp[0].import)->global.name); (yyval.module)->num_global_imports++; CHECK_IMPORT_ORDERING((yyval.module), global, globals, (yylsp[0])); break; @@ -3694,7 +3694,7 @@ yyreduce: WasmModuleField* field = wasm_append_module_field(parser->allocator, (yyval.module)); APPEND_FIELD_TO_LIST((yyval.module), field, EXPORT, export_, (yylsp[0]), (yyvsp[0].export_)); APPEND_ITEM_TO_VECTOR((yyval.module), Export, export, exports, &field->export_); - INSERT_BINDING((yyval.module), export, exports, (yylsp[0]), (yyvsp[0].export_)); + INSERT_BINDING((yyval.module), export, exports, (yylsp[0]), (yyvsp[0].export_).name); } #line 3700 "src/prebuilt/wasm-ast-parser-gen.c" /* yacc.c:1646 */ break; diff --git a/src/wasm-ast-checker.c b/src/wasm-ast-checker.c index fe3a159b..0595ca18 100644 --- a/src/wasm-ast-checker.c +++ b/src/wasm-ast-checker.c @@ -950,6 +950,8 @@ static void check_global(Context* ctx, static const char s_desc[] = "global initializer expression"; WasmType type = WASM_TYPE_VOID; WasmExpr* expr = global->init_expr; + if (!expr) + return; if (expr->next != NULL) { print_const_expr_error(ctx, loc, s_desc); diff --git a/src/wasm-ast-parser.y b/src/wasm-ast-parser.y index 32f67e58..d2bd24ab 100644 --- a/src/wasm-ast-parser.y +++ b/src/wasm-ast-parser.y @@ -72,14 +72,14 @@ &dummy); \ } while (0) -#define INSERT_BINDING(module, kind, kinds, loc_, item) \ - do \ - if ((item).name.start) { \ - WasmBinding* binding = wasm_insert_binding( \ - parser->allocator, &(module)->kind##_bindings, &(item).name); \ - binding->loc = loc_; \ - binding->index = (module)->kinds.size - 1; \ - } \ +#define INSERT_BINDING(module, kind, kinds, loc_, name) \ + do \ + if ((name).start) { \ + WasmBinding* binding = wasm_insert_binding( \ + parser->allocator, &(module)->kind##_bindings, &(name)); \ + binding->loc = loc_; \ + binding->index = (module)->kinds.size - 1; \ + } \ while (0) #define APPEND_INLINE_EXPORT(module, KIND, loc_, value, index_) \ @@ -93,7 +93,7 @@ APPEND_ITEM_TO_VECTOR(module, Export, export, exports, \ &export_field->export_); \ INSERT_BINDING(module, export, exports, export_field->loc, \ - export_field->export_); \ + export_field->export_.name); \ } \ while (0) @@ -948,34 +948,34 @@ global : import_kind : LPAR FUNC bind_var_opt type_use RPAR { $$ = new_import(parser->allocator); - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_FUNC; + $$->func.name = $3; $$->func.decl.flags = WASM_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE; $$->func.decl.type_var = $4; } | LPAR FUNC bind_var_opt func_sig RPAR { $$ = new_import(parser->allocator); - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_FUNC; + $$->func.name = $3; $$->func.decl.sig = $4; } | LPAR TABLE bind_var_opt table_sig RPAR { $$ = new_import(parser->allocator); - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_TABLE; $$->table = $4; + $$->table.name = $3; } | LPAR MEMORY bind_var_opt memory_sig RPAR { $$ = new_import(parser->allocator); - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_MEMORY; $$->memory = $4; + $$->memory.name = $3; } | LPAR GLOBAL bind_var_opt global_type RPAR { $$ = new_import(parser->allocator); - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_GLOBAL; $$->global = $4; + $$->global.name = $3; } ; import : @@ -986,34 +986,34 @@ import : } | LPAR FUNC bind_var_opt inline_import type_use RPAR { $$ = $4; - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_FUNC; + $$->func.name = $3; $$->func.decl.flags = WASM_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE; $$->func.decl.type_var = $5; } | LPAR FUNC bind_var_opt inline_import func_sig RPAR { $$ = $4; - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_FUNC; + $$->func.name = $3; $$->func.decl.sig = $5; } | LPAR TABLE bind_var_opt inline_import table_sig RPAR { $$ = $4; - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_TABLE; $$->table = $5; + $$->table.name = $3; } | LPAR MEMORY bind_var_opt inline_import memory_sig RPAR { $$ = $4; - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_MEMORY; $$->memory = $5; + $$->memory.name = $3; } | LPAR GLOBAL bind_var_opt inline_import global_type RPAR { $$ = $4; - $$->name = $3; $$->kind = WASM_EXTERNAL_KIND_GLOBAL; $$->global = $5; + $$->global.name = $3; } ; @@ -1097,14 +1097,14 @@ module_fields : APPEND_FIELD_TO_LIST($$, field, FUNC_TYPE, func_type, @2, $2); APPEND_ITEM_TO_VECTOR($$, FuncType, func_type, func_types, &field->func_type); - INSERT_BINDING($$, func_type, func_types, @2, $2); + INSERT_BINDING($$, func_type, func_types, @2, $2.name); } | module_fields global { $$ = $1; WasmModuleField* field; APPEND_FIELD_TO_LIST($$, field, GLOBAL, global, @2, $2.global); APPEND_ITEM_TO_VECTOR($$, Global, global, globals, &field->global); - INSERT_BINDING($$, global, globals, @2, $2.global); + INSERT_BINDING($$, global, globals, @2, $2.global.name); APPEND_INLINE_EXPORT($$, GLOBAL, @2, $2, $$->globals.size - 1); } | module_fields table { @@ -1112,7 +1112,7 @@ module_fields : WasmModuleField* field; APPEND_FIELD_TO_LIST($$, field, TABLE, table, @2, $2.table); APPEND_ITEM_TO_VECTOR($$, Table, table, tables, &field->table); - INSERT_BINDING($$, table, tables, @2, $2.table); + INSERT_BINDING($$, table, tables, @2, $2.table.name); APPEND_INLINE_EXPORT($$, TABLE, @2, $2, $$->tables.size - 1); if ($2.has_elem_segment) { @@ -1129,7 +1129,7 @@ module_fields : WasmModuleField* field; APPEND_FIELD_TO_LIST($$, field, MEMORY, memory, @2, $2.memory); APPEND_ITEM_TO_VECTOR($$, Memory, memory, memories, &field->memory); - INSERT_BINDING($$, memory, memories, @2, $2.memory); + INSERT_BINDING($$, memory, memories, @2, $2.memory.name); APPEND_INLINE_EXPORT($$, MEMORY, @2, $2, $$->memories.size - 1); if ($2.has_data_segment) { @@ -1148,7 +1148,7 @@ module_fields : append_implicit_func_declaration(parser->allocator, &@2, $$, &field->func.decl); APPEND_ITEM_TO_VECTOR($$, Func, func, funcs, &field->func); - INSERT_BINDING($$, func, funcs, @2, *$2.func); + INSERT_BINDING($$, func, funcs, @2, $2.func->name); APPEND_INLINE_EXPORT($$, FUNC, @2, $2, $$->funcs.size - 1); } | module_fields elem { @@ -1180,27 +1180,27 @@ module_fields : append_implicit_func_declaration(parser->allocator, &@2, $$, &$2->func.decl); APPEND_ITEM_TO_VECTOR($$, Func, func, funcs, &field->import.func); - INSERT_BINDING($$, func, funcs, @2, *$2); + INSERT_BINDING($$, func, funcs, @2, $2->func.name); $$->num_func_imports++; CHECK_IMPORT_ORDERING($$, func, funcs, @2); break; case WASM_EXTERNAL_KIND_TABLE: APPEND_ITEM_TO_VECTOR($$, Table, table, tables, &field->import.table); - INSERT_BINDING($$, table, tables, @2, *$2); + INSERT_BINDING($$, table, tables, @2, $2->table.name); $$->num_table_imports++; CHECK_IMPORT_ORDERING($$, table, tables, @2); break; case WASM_EXTERNAL_KIND_MEMORY: APPEND_ITEM_TO_VECTOR($$, Memory, memory, memories, &field->import.memory); - INSERT_BINDING($$, memory, memories, @2, *$2); + INSERT_BINDING($$, memory, memories, @2, $2->memory.name); $$->num_memory_imports++; CHECK_IMPORT_ORDERING($$, memory, memories, @2); break; case WASM_EXTERNAL_KIND_GLOBAL: APPEND_ITEM_TO_VECTOR($$, Global, global, globals, &field->import.global); - INSERT_BINDING($$, global, globals, @2, *$2); + INSERT_BINDING($$, global, globals, @2, $2->global.name); $$->num_global_imports++; CHECK_IMPORT_ORDERING($$, global, globals, @2); break; @@ -1216,7 +1216,7 @@ module_fields : WasmModuleField* field = wasm_append_module_field(parser->allocator, $$); APPEND_FIELD_TO_LIST($$, field, EXPORT, export_, @2, $2); APPEND_ITEM_TO_VECTOR($$, Export, export, exports, &field->export_); - INSERT_BINDING($$, export, exports, @2, $2); + INSERT_BINDING($$, export, exports, @2, $2.name); } ; diff --git a/src/wasm-ast-writer.c b/src/wasm-ast-writer.c index abbda8d0..01170ad7 100644 --- a/src/wasm-ast-writer.c +++ b/src/wasm-ast-writer.c @@ -513,11 +513,13 @@ static void write_expr_list(Context* ctx, const WasmExpr* first) { } static void write_init_expr(Context* ctx, const WasmExpr* expr) { - write_puts(ctx, "(", NEXT_CHAR_NONE); - write_expr(ctx, expr); - /* clear the next char, so we don't write a newline after the expr */ - ctx->next_char = NEXT_CHAR_NONE; - write_puts(ctx, ")", NEXT_CHAR_SPACE); + if (expr) { + write_puts(ctx, "(", NEXT_CHAR_NONE); + write_expr(ctx, expr); + /* clear the next char, so we don't write a newline after the expr */ + ctx->next_char = NEXT_CHAR_NONE; + write_puts(ctx, ")", NEXT_CHAR_SPACE); + } } static void write_type_bindings(Context* ctx, @@ -582,11 +584,15 @@ static void write_func(Context* ctx, static void write_begin_global(Context* ctx, const WasmGlobal* global) { write_open_space(ctx, "global"); - if (global->mutable_) - write_puts(ctx, "mut", NEXT_CHAR_SPACE); write_string_slice_or_index(ctx, &global->name, ctx->global_index++, NEXT_CHAR_SPACE); - write_type(ctx, global->type, NEXT_CHAR_SPACE); + if (global->mutable_) { + write_open_space(ctx, "mut"); + write_type(ctx, global->type, NEXT_CHAR_SPACE); + write_close_space(ctx); + } else { + write_type(ctx, global->type, NEXT_CHAR_SPACE); + } } static void write_global(Context* ctx, const WasmGlobal* global) { @@ -654,12 +660,13 @@ static void write_import(Context* ctx, const WasmImport* import) { assert(0); break; } - write_string_slice_or_index(ctx, &import->name, index, NEXT_CHAR_SPACE); write_quoted_string_slice(ctx, &import->module_name, NEXT_CHAR_SPACE); write_quoted_string_slice(ctx, &import->field_name, NEXT_CHAR_SPACE); switch (import->kind) { case WASM_EXTERNAL_KIND_FUNC: write_open_space(ctx, "func"); + write_string_slice_or_index(ctx, &import->func.name, index, + NEXT_CHAR_SPACE); if (wasm_decl_has_func_type(&import->func.decl)) { write_open_space(ctx, "type"); write_var(ctx, &import->func.decl.type_var, NEXT_CHAR_NONE); @@ -671,15 +678,11 @@ static void write_import(Context* ctx, const WasmImport* import) { break; case WASM_EXTERNAL_KIND_TABLE: - write_open_space(ctx, "table"); write_table(ctx, &import->table); - write_close_space(ctx); break; case WASM_EXTERNAL_KIND_MEMORY: - write_open_space(ctx, "memory"); write_memory(ctx, &import->memory); - write_close_space(ctx); break; case WASM_EXTERNAL_KIND_GLOBAL: diff --git a/src/wasm-ast.c b/src/wasm-ast.c index 12febdb0..bd3c90a5 100644 --- a/src/wasm-ast.c +++ b/src/wasm-ast.c @@ -513,7 +513,6 @@ void wasm_destroy_global(WasmAllocator* allocator, WasmGlobal* global) { } void wasm_destroy_import(WasmAllocator* allocator, WasmImport* import) { - wasm_destroy_string_slice(allocator, &import->name); wasm_destroy_string_slice(allocator, &import->module_name); wasm_destroy_string_slice(allocator, &import->field_name); switch (import->kind) { diff --git a/src/wasm-ast.h b/src/wasm-ast.h index 85dd8712..e5dbf611 100644 --- a/src/wasm-ast.h +++ b/src/wasm-ast.h @@ -207,7 +207,6 @@ typedef WasmDataSegment* WasmDataSegmentPtr; WASM_DEFINE_VECTOR(data_segment_ptr, WasmDataSegmentPtr); typedef struct WasmImport { - WasmStringSlice name; WasmStringSlice module_name; WasmStringSlice field_name; WasmExternalKind kind; diff --git a/src/wasm-binary-reader-ast.c b/src/wasm-binary-reader-ast.c index 0c117d13..032448c9 100644 --- a/src/wasm-binary-reader-ast.c +++ b/src/wasm-binary-reader-ast.c @@ -214,6 +214,7 @@ static WasmResult on_import_func(uint32_t index, assert(sig_index < ctx->module->func_types.size); WasmImport* import = ctx->module->imports.data[index]; + import->kind = WASM_EXTERNAL_KIND_FUNC; import->func.decl.flags = WASM_FUNC_DECLARATION_FLAG_HAS_FUNC_TYPE | WASM_FUNC_DECLARATION_FLAG_SHARED_SIGNATURE; import->func.decl.type_var.type = WASM_VAR_TYPE_INDEX; @@ -233,6 +234,7 @@ static WasmResult on_import_table(uint32_t index, Context* ctx = user_data; assert(index == ctx->module->imports.size - 1); WasmImport* import = ctx->module->imports.data[index]; + import->kind = WASM_EXTERNAL_KIND_TABLE; import->table.elem_limits = *elem_limits; WasmTablePtr table_ptr = &import->table; @@ -247,6 +249,7 @@ static WasmResult on_import_memory(uint32_t index, Context* ctx = user_data; assert(index == ctx->module->imports.size - 1); WasmImport* import = ctx->module->imports.data[index]; + import->kind = WASM_EXTERNAL_KIND_MEMORY; import->memory.page_limits = *page_limits; WasmMemoryPtr memory_ptr = &import->memory; @@ -263,6 +266,7 @@ static WasmResult on_import_global(uint32_t index, Context* ctx = user_data; assert(index == ctx->module->imports.size - 1); WasmImport* import = ctx->module->imports.data[index]; + import->kind = WASM_EXTERNAL_KIND_GLOBAL; import->global.type = type; import->global.mutable_ = mutable_; @@ -426,7 +430,7 @@ static WasmResult on_export(uint32_t index, assert(item_index < ctx->module->funcs.size); break; case WASM_EXTERNAL_KIND_TABLE: - assert(item_index < ctx->module->funcs.size); + assert(item_index < ctx->module->tables.size); break; case WASM_EXTERNAL_KIND_MEMORY: assert(item_index < ctx->module->memories.size); diff --git a/src/wasm-binary-reader.c b/src/wasm-binary-reader.c index 674a619f..3e250a5c 100644 --- a/src/wasm-binary-reader.c +++ b/src/wasm-binary-reader.c @@ -1053,6 +1053,9 @@ static void read_init_expr(Context* ctx, uint32_t index) { break; } + case WASM_OPCODE_END: + return; + default: RAISE_ERROR("unexpected opcode in initializer expression: %d (0x%x)", opcode, opcode); diff --git a/src/wasm-binary-writer.c b/src/wasm-binary-writer.c index 339a0655..6d3653e4 100644 --- a/src/wasm-binary-writer.c +++ b/src/wasm-binary-writer.c @@ -532,7 +532,8 @@ static void write_expr_list(Context* ctx, static void write_init_expr(Context* ctx, const WasmModule* module, const WasmExpr* expr) { - write_expr(ctx, module, NULL, expr); + if (expr) + write_expr(ctx, module, NULL, expr); write_opcode(&ctx->stream, WASM_OPCODE_END); } diff --git a/test/parse/module/bad-export-global-name-undefined.txt b/test/parse/module/bad-export-global-name-undefined.txt new file mode 100644 index 00000000..61c5af9b --- /dev/null +++ b/test/parse/module/bad-export-global-name-undefined.txt @@ -0,0 +1,7 @@ +;;; ERROR: 1 +(module (export "foo" (global $bar))) +(;; STDERR ;;; +parse/module/bad-export-global-name-undefined.txt:2:31: undefined global variable "$bar" +(module (export "foo" (global $bar))) + ^^^^ +;;; STDERR ;;) diff --git a/test/parse/module/bad-export-global-undefined.txt b/test/parse/module/bad-export-global-undefined.txt new file mode 100644 index 00000000..f2b5ad7c --- /dev/null +++ b/test/parse/module/bad-export-global-undefined.txt @@ -0,0 +1,7 @@ +;;; ERROR: 1 +(module (export "foo" (global 0))) +(;; STDERR ;;; +parse/module/bad-export-global-undefined.txt:2:31: global variable out of range (max 0) +(module (export "foo" (global 0))) + ^ +;;; STDERR ;;) diff --git a/test/parse/module/bad-export-memory-name-undefined.txt b/test/parse/module/bad-export-memory-name-undefined.txt new file mode 100644 index 00000000..feb4158c --- /dev/null +++ b/test/parse/module/bad-export-memory-name-undefined.txt @@ -0,0 +1,7 @@ +;;; ERROR: 1 +(module (export "foo" (memory $bar))) +(;; STDERR ;;; +parse/module/bad-export-memory-name-undefined.txt:2:31: undefined memory variable "$bar" +(module (export "foo" (memory $bar))) + ^^^^ +;;; STDERR ;;) diff --git a/test/parse/module/bad-export-memory-no-memory.txt b/test/parse/module/bad-export-memory-undefined.txt index eb884d07..d56971c1 100644 --- a/test/parse/module/bad-export-memory-no-memory.txt +++ b/test/parse/module/bad-export-memory-undefined.txt @@ -2,7 +2,7 @@ (module (export "mem" (memory 0))) (;; STDERR ;;; -parse/module/bad-export-memory-no-memory.txt:3:25: memory variable out of range (max 0) +parse/module/bad-export-memory-undefined.txt:3:25: memory variable out of range (max 0) (export "mem" (memory 0))) ^ ;;; STDERR ;;) diff --git a/test/parse/module/bad-export-table-name-undefined.txt b/test/parse/module/bad-export-table-name-undefined.txt new file mode 100644 index 00000000..ee8717aa --- /dev/null +++ b/test/parse/module/bad-export-table-name-undefined.txt @@ -0,0 +1,7 @@ +;;; ERROR: 1 +(module (export "foo" (table $bar))) +(;; STDERR ;;; +parse/module/bad-export-table-name-undefined.txt:2:30: undefined table variable "$bar" +(module (export "foo" (table $bar))) + ^^^^ +;;; STDERR ;;) diff --git a/test/parse/module/bad-export-table-undefined.txt b/test/parse/module/bad-export-table-undefined.txt new file mode 100644 index 00000000..86ec83bb --- /dev/null +++ b/test/parse/module/bad-export-table-undefined.txt @@ -0,0 +1,7 @@ +;;; ERROR: 1 +(module (export "foo" (table 0))) +(;; STDERR ;;; +parse/module/bad-export-table-undefined.txt:2:30: table variable out of range (max 0) +(module (export "foo" (table 0))) + ^ +;;; STDERR ;;) diff --git a/test/parse/module/bad-import-global-redefinition.txt b/test/parse/module/bad-import-global-redefinition.txt new file mode 100644 index 00000000..6877f70a --- /dev/null +++ b/test/parse/module/bad-import-global-redefinition.txt @@ -0,0 +1,9 @@ +;;; ERROR: 1 +(module + (import "foo" "bar" (global $baz i32)) + (import "oof" "rab" (global $baz i32))) +(;; STDERR ;;; +parse/module/bad-import-global-redefinition.txt:4:3: redefinition of global "$baz" + (import "oof" "rab" (global $baz i32))) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +;;; STDERR ;;) diff --git a/test/parse/module/bad-import-memory-redefinition.txt b/test/parse/module/bad-import-memory-redefinition.txt new file mode 100644 index 00000000..1610382a --- /dev/null +++ b/test/parse/module/bad-import-memory-redefinition.txt @@ -0,0 +1,9 @@ +;;; ERROR: 1 +(module + (import "foo" "bar" (memory $baz 0)) + (import "foo" "blah" (memory $baz 0))) +(;; STDERR ;;; +parse/module/bad-import-memory-redefinition.txt:4:3: redefinition of memory "$baz" + (import "foo" "blah" (memory $baz 0))) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +;;; STDERR ;;) diff --git a/test/parse/module/bad-import-table-redefinition.txt b/test/parse/module/bad-import-table-redefinition.txt new file mode 100644 index 00000000..1ca01851 --- /dev/null +++ b/test/parse/module/bad-import-table-redefinition.txt @@ -0,0 +1,9 @@ +;;; ERROR: 1 +(module + (import "foo" "bar" (table $baz 0 anyfunc)) + (import "foo" "blah" (table $baz 0 anyfunc))) +(;; STDERR ;;; +parse/module/bad-import-table-redefinition.txt:4:3: redefinition of table "$baz" + (import "foo" "blah" (table $baz 0 anyfunc))) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +;;; STDERR ;;) diff --git a/test/parse/module/export-global.txt b/test/parse/module/export-global.txt new file mode 100644 index 00000000..361efd32 --- /dev/null +++ b/test/parse/module/export-global.txt @@ -0,0 +1,5 @@ +(module + (global i32) + (global (mut f32)) + (export "global0" (global 0)) + (export "global1" (global 1))) diff --git a/test/parse/module/export-table.txt b/test/parse/module/export-table.txt new file mode 100644 index 00000000..12c926f0 --- /dev/null +++ b/test/parse/module/export-table.txt @@ -0,0 +1,3 @@ +(module + (table 0 anyfunc) + (export "my_table" (table 0))) diff --git a/test/parse/module/global.txt b/test/parse/module/global.txt index b473f0a6..13ad845f 100644 --- a/test/parse/module/global.txt +++ b/test/parse/module/global.txt @@ -1,4 +1,9 @@ (module + (global i32) + (global i64) + (global f32) + (global f64) + (global i32 (i32.const 1)) (global i64 (i64.const 2)) (global f32 (f32.const 3)) diff --git a/test/parse/module/import-global.txt b/test/parse/module/import-global.txt new file mode 100644 index 00000000..93b86cf8 --- /dev/null +++ b/test/parse/module/import-global.txt @@ -0,0 +1,6 @@ +(module + (import "foo" "1" (global i32)) + (import "foo" "2" (global i64)) + (import "foo" "3" (global f32)) + (import "foo" "4" (global f64)) + (import "foo" "5" (global (mut i32)))) diff --git a/test/parse/module/import-memory.txt b/test/parse/module/import-memory.txt new file mode 100644 index 00000000..3d30fe0f --- /dev/null +++ b/test/parse/module/import-memory.txt @@ -0,0 +1,3 @@ +(module + (import "foo" "1" (memory 0)) + (import "foo" "2" (memory 0 2))) diff --git a/test/parse/module/import-table.txt b/test/parse/module/import-table.txt new file mode 100644 index 00000000..5a8f078d --- /dev/null +++ b/test/parse/module/import-table.txt @@ -0,0 +1,3 @@ +(module + (import "foo" "1" (table 0 anyfunc)) + (import "foo" "2" (table 0 10 anyfunc))) |