diff options
author | Ben Smith <binji@chromium.org> | 2016-11-08 12:25:58 -0800 |
---|---|---|
committer | Ben Smith <binji@chromium.org> | 2016-12-05 15:41:36 -0800 |
commit | 98355e73ef121b8c3e2a0433e31fc8bac7057c70 (patch) | |
tree | 55536508a775e31ea652bdf1f780c4a5b7643ba2 /src | |
parent | 538f5e61d10b19a0d7283e765589432bbeedcfa8 (diff) | |
download | wabt-98355e73ef121b8c3e2a0433e31fc8bac7057c70.tar.gz wabt-98355e73ef121b8c3e2a0433e31fc8bac7057c70.tar.bz2 wabt-98355e73ef121b8c3e2a0433e31fc8bac7057c70.zip |
Fix gen-spec-js.py
This now (mostly) matches the output from the spec interpreter.
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-writer-spec.c | 41 | ||||
-rw-r--r-- | src/tools/wasm-interp.c | 46 |
2 files changed, 87 insertions, 0 deletions
diff --git a/src/binary-writer-spec.c b/src/binary-writer-spec.c index 35d52ab1..3a48ccf7 100644 --- a/src/binary-writer-spec.c +++ b/src/binary-writer-spec.c @@ -162,6 +162,13 @@ static void write_var(Context* ctx, const WasmVar* var) { write_escaped_string_slice(ctx, var->name); } +static void write_type_object(Context* ctx, WasmType type) { + wasm_writef(&ctx->json_stream, "{"); + write_key(ctx, "type"); + write_string(ctx, wasm_get_type_name(type)); + wasm_writef(&ctx->json_stream, "}"); +} + static void write_const(Context* ctx, const WasmConst* const_) { wasm_writef(&ctx->json_stream, "{"); write_key(ctx, "type"); @@ -249,6 +256,36 @@ static void write_action(Context* ctx, const WasmAction* action) { wasm_writef(&ctx->json_stream, "}"); } +static void write_action_result_type(Context* ctx, + WasmScript* script, + const WasmAction* action) { + const WasmModule* module = + wasm_get_module_by_var(script, &action->module_var); + const WasmExport* export; + wasm_writef(&ctx->json_stream, "["); + switch (action->type) { + case WASM_ACTION_TYPE_INVOKE: { + export = wasm_get_export_by_name(module, &action->invoke.name); + assert(export->kind == WASM_EXTERNAL_KIND_FUNC); + WasmFunc* func = wasm_get_func_by_var(module, &export->var); + size_t num_results = wasm_get_num_results(func); + size_t i; + for (i = 0; i < num_results; ++i) + write_type_object(ctx, wasm_get_result_type(func, i)); + break; + } + + case WASM_ACTION_TYPE_GET: { + export = wasm_get_export_by_name(module, &action->get.name); + assert(export->kind == WASM_EXTERNAL_KIND_GLOBAL); + WasmGlobal* global = wasm_get_global_by_var(module, &export->var); + write_type_object(ctx, global->type); + break; + } + } + wasm_writef(&ctx->json_stream, "]"); +} + static void write_module(Context* ctx, char* filename, const WasmModule* module) { @@ -398,6 +435,10 @@ static void write_commands(Context* ctx, WasmScript* script) { write_location(ctx, &command->assert_return_nan.action.loc); write_separator(ctx); write_action(ctx, &command->assert_return_nan.action); + write_separator(ctx); + write_key(ctx, "expected"); + write_action_result_type(ctx, script, + &command->assert_return_nan.action); break; case WASM_COMMAND_TYPE_ASSERT_TRAP: diff --git a/src/tools/wasm-interp.c b/src/tools/wasm-interp.c index 9956c614..2180762e 100644 --- a/src/tools/wasm-interp.c +++ b/src/tools/wasm-interp.c @@ -909,6 +909,46 @@ static WasmResult parse_line(Context* ctx) { return WASM_OK; } +static WasmResult parse_type_object(Context* ctx, WasmType* out_type) { + WasmStringSlice type_str; + EXPECT("{"); + PARSE_KEY_STRING_VALUE("type", &type_str); + EXPECT("}"); + + if (string_slice_equals_str(&type_str, "i32")) { + *out_type = WASM_TYPE_I32; + return WASM_OK; + } else if (string_slice_equals_str(&type_str, "f32")) { + *out_type = WASM_TYPE_F32; + return WASM_OK; + } else if (string_slice_equals_str(&type_str, "i64")) { + *out_type = WASM_TYPE_I64; + return WASM_OK; + } else if (string_slice_equals_str(&type_str, "f64")) { + *out_type = WASM_TYPE_F64; + return WASM_OK; + } else { + print_parse_error(ctx, "unknown type: \"" PRIstringslice "\"", + WASM_PRINTF_STRING_SLICE_ARG(type_str)); + return WASM_ERROR; + } +} + +static WasmResult parse_type_vector(Context* ctx, WasmTypeVector* out_types) { + WASM_ZERO_MEMORY(*out_types); + EXPECT("["); + WasmBool first = WASM_TRUE; + while (!match(ctx, "]")) { + if (!first) + EXPECT(","); + WasmType type; + CHECK_RESULT(parse_type_object(ctx, &type)); + first = WASM_FALSE; + wasm_append_type_value(ctx->allocator, out_types, &type); + } + return WASM_OK; +} + static WasmResult parse_const(Context* ctx, WasmInterpreterTypedValue* out_value) { WasmStringSlice type_str; @@ -1457,13 +1497,19 @@ static WasmResult parse_command(Context* ctx) { destroy_action(ctx->allocator, &action); } else if (match(ctx, "\"assert_return_nan\"")) { Action action; + WasmTypeVector expected; WASM_ZERO_MEMORY(action); EXPECT(","); CHECK_RESULT(parse_line(ctx)); EXPECT(","); CHECK_RESULT(parse_action(ctx, &action)); + EXPECT(","); + /* Not needed for wasm-interp, but useful for other parsers. */ + EXPECT_KEY("expected"); + CHECK_RESULT(parse_type_vector(ctx, &expected)); on_assert_return_nan_command(ctx, &action); + wasm_destroy_type_vector(ctx->allocator, &expected); destroy_action(ctx->allocator, &action); } else if (match(ctx, "\"assert_trap\"")) { Action action; |