diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-writer.c | 39 | ||||
-rw-r--r-- | src/binary-writer.h | 5 | ||||
-rw-r--r-- | src/tools/wast2wasm.c | 32 |
3 files changed, 26 insertions, 50 deletions
diff --git a/src/binary-writer.c b/src/binary-writer.c index 92976f67..b7cae6d1 100644 --- a/src/binary-writer.c +++ b/src/binary-writer.c @@ -557,7 +557,7 @@ static void write_global_header(Context* ctx, const WasmGlobal* global) { wasm_write_u8(&ctx->stream, global->mutable_, "global mutability"); } -static void write_module(Context* ctx, const WasmModule* module) { +static WasmResult write_module(Context* ctx, const WasmModule* module) { /* TODO(binji): better leb size guess. Some sections we know will only be 1 byte, but others we can be fairly certain will be larger. */ const size_t leb_size_guess = 1; @@ -850,26 +850,8 @@ static void write_module(Context* ctx, const WasmModule* module) { wasm_destroy_string_slice_vector(ctx->allocator, &index_to_name); } -} -static void write_commands(Context* ctx, const WasmScript* script) { - size_t i; - WasmBool wrote_module = WASM_FALSE; - for (i = 0; i < script->commands.size; ++i) { - const WasmCommand* command = &script->commands.data[i]; - if (command->type != WASM_COMMAND_TYPE_MODULE) - continue; - - write_module(ctx, &command->module); - wrote_module = WASM_TRUE; - break; - } - if (!wrote_module) { - /* just write an empty module */ - WasmModule module; - WASM_ZERO_MEMORY(module); - write_module(ctx, &module); - } + return ctx->stream.result; } WasmResult wasm_write_binary_module(WasmAllocator* allocator, @@ -882,20 +864,5 @@ WasmResult wasm_write_binary_module(WasmAllocator* allocator, ctx.options = options; ctx.log_stream = options->log_stream; wasm_init_stream(&ctx.stream, writer, ctx.log_stream); - write_module(&ctx, module); - return ctx.stream.result; -} - -WasmResult wasm_write_binary_script(WasmAllocator* allocator, - WasmWriter* writer, - const WasmScript* script, - const WasmWriteBinaryOptions* options) { - Context ctx; - WASM_ZERO_MEMORY(ctx); - ctx.allocator = allocator; - ctx.options = options; - ctx.log_stream = options->log_stream; - wasm_init_stream(&ctx.stream, writer, ctx.log_stream); - write_commands(&ctx, script); - return ctx.stream.result; + return write_module(&ctx, module); } diff --git a/src/binary-writer.h b/src/binary-writer.h index 16a838ff..b00bc9ef 100644 --- a/src/binary-writer.h +++ b/src/binary-writer.h @@ -42,11 +42,6 @@ WasmResult wasm_write_binary_module(struct WasmAllocator*, const struct WasmModule*, const WasmWriteBinaryOptions*); -WasmResult wasm_write_binary_script(struct WasmAllocator*, - struct WasmWriter*, - const struct WasmScript*, - const WasmWriteBinaryOptions*); - void wasm_write_u32_leb128(struct WasmStream* stream, uint32_t value, const char* desc); diff --git a/src/tools/wast2wasm.c b/src/tools/wast2wasm.c index a8638a05..14b7fcfb 100644 --- a/src/tools/wast2wasm.c +++ b/src/tools/wast2wasm.c @@ -51,8 +51,7 @@ static WasmBool s_validate_assert_invalid_and_malformed = WASM_TRUE; static WasmSourceErrorHandler s_error_handler = WASM_SOURCE_ERROR_HANDLER_DEFAULT; -static WasmFileWriter s_log_stream_writer; -static WasmStream s_log_stream; +static WasmFileStream s_log_stream; #define NOPE WASM_OPTION_NO_ARGUMENT #define YEP WASM_OPTION_HAS_ARGUMENT @@ -121,7 +120,7 @@ static void on_option(struct WasmOptionParser* parser, switch (option->id) { case FLAG_VERBOSE: s_verbose++; - s_write_binary_options.log_stream = &s_log_stream; + s_write_binary_options.log_stream = &s_log_stream.base; break; case FLAG_HELP: @@ -193,8 +192,8 @@ static void write_buffer_to_file(const char* filename, WasmOutputBuffer* buffer) { if (s_dump_module) { if (s_verbose) - wasm_writef(&s_log_stream, ";; dump\n"); - wasm_write_output_buffer_memory_dump(&s_log_stream, buffer); + wasm_writef(&s_log_stream.base, ";; dump\n"); + wasm_write_output_buffer_memory_dump(&s_log_stream.base, buffer); } if (filename) { @@ -220,8 +219,7 @@ int main(int argc, char** argv) { wasm_init_stdio(); - wasm_init_file_writer_existing(&s_log_stream_writer, stdout); - wasm_init_stream(&s_log_stream, &s_log_stream_writer.base, NULL); + wasm_init_file_stream_from_existing(&s_log_stream, stdout); parse_options(argc, argv); if (s_use_libc_allocator) { @@ -277,8 +275,24 @@ int main(int argc, char** argv) { if (WASM_FAILED(wasm_init_mem_writer(allocator, &writer))) WASM_FATAL("unable to open memory writer for writing\n"); - result = wasm_write_binary_script(allocator, &writer.base, &script, - &s_write_binary_options); + /* Write the first module, if any. */ + const WasmModule* module = NULL; + size_t i; + for (i = 0; i < script.commands.size; ++i) { + const WasmCommand* command = &script.commands.data[i]; + if (command->type != WASM_COMMAND_TYPE_MODULE) + continue; + module = &command->module; + break; + } + + if (module) { + result = wasm_write_binary_module(allocator, &writer.base, module, + &s_write_binary_options); + } else { + WASM_FATAL("no module found\n"); + } + if (WASM_SUCCEEDED(result)) write_buffer_to_file(s_outfile, &writer.buf); wasm_close_mem_writer(&writer); |