diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/emscripten-exported.json | 33 | ||||
-rw-r--r-- | src/emscripten-helpers.c | 3 | ||||
-rw-r--r-- | src/resolve-names.c | 12 | ||||
-rw-r--r-- | src/tools/wast2wasm.c | 12 | ||||
-rw-r--r-- | src/wabt.js | 131 |
5 files changed, 65 insertions, 126 deletions
diff --git a/src/emscripten-exported.json b/src/emscripten-exported.json index 5319e237..05f1f694 100644 --- a/src/emscripten-exported.json +++ b/src/emscripten-exported.json @@ -10,13 +10,8 @@ "_wabt_ast_parser_error", "_wabt_ast_parser_parse", "_wabt_call_host", -"_wabt_check_assert_invalid_and_malformed", -"_wabt_check_ast", -"_wabt_check_names", "_wabt_close_file_writer", "_wabt_close_mem_writer", -"_wabt_default_assert_invalid_source_error_callback", -"_wabt_default_assert_malformed_source_error_callback", "_wabt_default_binary_error_callback", "_wabt_default_source_error_callback", "_wabt_destroy_action", @@ -47,6 +42,7 @@ "_wabt_destroy_optional_export", "_wabt_destroy_output_buffer", "_wabt_destroy_raw_module", +"_wabt_destroy_reloc_section", "_wabt_destroy_script", "_wabt_destroy_stack_allocator", "_wabt_destroy_string_slice", @@ -60,6 +56,8 @@ "_wabt_ensure_capacity", "_wabt_extend_elements", "_wabt_find_binding_index_by_name", +"_wabt_find_duplicate_bindings", +"_wabt_func_signatures_are_equal", "_wabt_generate_names", "_wabt_get_export_by_name", "_wabt_get_first_module", @@ -101,6 +99,7 @@ "_wabt_is_nan_f64", "_wabt_is_naturally_aligned", "_wabt_make_type_binding_reverse_mapping", +"_wabt_mark_interpreter_environment", "_wabt_move_data", "_wabt_new_ast_buffer_lexer", "_wabt_new_ast_file_lexer", @@ -181,12 +180,19 @@ "_wabt_read_binary_ast", "_wabt_read_binary_interpreter", "_wabt_read_file", +"_wabt_read_i32_leb128", +"_wabt_read_u32_leb128", +"_wabt_remove_binding", +"_wabt_reset_interpreter_environment_to_mark", "_wabt_resize_vector", +"_wabt_resolve_names_module", +"_wabt_resolve_names_script", "_wabt_run_interpreter", "_wabt_sizeof_allocator", "_wabt_sizeof_binary_error_handler", "_wabt_sizeof_location", "_wabt_sizeof_memory_writer", +"_wabt_sizeof_module", "_wabt_sizeof_output_buffer", "_wabt_sizeof_read_binary_options", "_wabt_sizeof_script", @@ -197,21 +203,36 @@ "_wabt_sizeof_write_binary_options", "_wabt_sizeof_writer", "_wabt_steal_mem_writer_output_buffer", +"_wabt_string_slice_eq_cstr", "_wabt_string_slice_from_cstr", +"_wabt_string_slice_is_empty", +"_wabt_string_slice_startswith", "_wabt_string_slices_are_equal", "_wabt_trace_pc", +"_wabt_u32_leb128_length", +"_wabt_validate_script", +"_wabt_visit_expr_list", "_wabt_visit_func", "_wabt_write_ast", "_wabt_write_binary_module", -"_wabt_write_binary_script", "_wabt_write_binary_spec_script", "_wabt_write_data", "_wabt_write_data_at", "_wabt_write_double_hex", +"_wabt_write_fixed_u32_leb128", +"_wabt_write_fixed_u32_leb128_at", +"_wabt_write_fixed_u32_leb128_raw", "_wabt_write_float_hex", +"_wabt_write_i32_leb128", +"_wabt_write_limits", "_wabt_write_memory_dump", +"_wabt_write_opcode", "_wabt_write_output_buffer_to_file", +"_wabt_write_str", +"_wabt_write_type", "_wabt_write_u32", +"_wabt_write_u32_leb128", +"_wabt_write_u32_leb128_at", "_wabt_write_u64", "_wabt_write_u8", "_wabt_writef"]
\ No newline at end of file diff --git a/src/emscripten-helpers.c b/src/emscripten-helpers.c index e4795352..a4a159c4 100644 --- a/src/emscripten-helpers.c +++ b/src/emscripten-helpers.c @@ -81,6 +81,9 @@ DEFINE_STRUCT( WabtMemoryWriter, memory_writer, base, buf) +DEFINE_STRUCT0( + WabtModule, module) + DEFINE_STRUCT( WabtOutputBuffer, output_buffer, allocator, start, size, capacity) diff --git a/src/resolve-names.c b/src/resolve-names.c index 7277831d..1442575d 100644 --- a/src/resolve-names.c +++ b/src/resolve-names.c @@ -341,12 +341,12 @@ static void visit_raw_module(Context* ctx, WabtRawModule* raw_module) { visit_module(ctx, raw_module->text); } -void dummy_source_error_callback(const WabtLocation* loc, - const char* error, - const char* source_line, - size_t source_line_length, - size_t source_line_column_offset, - void* user_data) {} +static void dummy_source_error_callback(const WabtLocation* loc, + const char* error, + const char* source_line, + size_t source_line_length, + size_t source_line_column_offset, + void* user_data) {} static void visit_command(Context* ctx, WabtCommand* command) { switch (command->type) { diff --git a/src/tools/wast2wasm.c b/src/tools/wast2wasm.c index 2fc85d37..8af8c71c 100644 --- a/src/tools/wast2wasm.c +++ b/src/tools/wast2wasm.c @@ -243,17 +243,7 @@ int main(int argc, char** argv) { if (WABT_FAILED(wabt_init_mem_writer(allocator, &writer))) WABT_FATAL("unable to open memory writer for writing\n"); - /* Write the first module, if any. */ - const WabtModule* module = NULL; - size_t i; - for (i = 0; i < script.commands.size; ++i) { - const WabtCommand* command = &script.commands.data[i]; - if (command->type != WABT_COMMAND_TYPE_MODULE) - continue; - module = &command->module; - break; - } - + WabtModule* module = wabt_get_first_module(&script); if (module) { result = wabt_write_binary_module(allocator, &writer.base, module, &s_write_binary_options); diff --git a/src/wabt.js b/src/wabt.js index 5412713b..fff5505c 100644 --- a/src/wabt.js +++ b/src/wabt.js @@ -546,6 +546,7 @@ var AstLexer = Struct('AstLexer'); var BinaryErrorHandler = Struct('BinaryErrorHandler'); var Location = Struct('Location'); var MemoryWriter = Struct('MemoryWriter'); +var Module = Struct('Module'); var OutputBuffer = Struct('OutputBuffer'); var ReadBinaryOptions = Struct('ReadBinaryOptions'); var Script = Struct('Script'); @@ -587,6 +588,8 @@ MemoryWriter.define('memory_writer', { buf: OutputBuffer, }); +Module.define('module'); + OutputBuffer.define('output_buffer', { allocator: Allocator, start: BufPtr, @@ -635,7 +638,6 @@ Writer.define('writer', { // Wabt low-level functions //////////////////////////////////////////////////// -var checkAst = Fn(I32, [Ptr(Allocator), Ptr(AstLexer), Ptr(Script), Ptr(SourceErrorHandler)]).define('_wabt_check_ast'); var closeMemWriter = Fn(Void, [Ptr(MemoryWriter)]).define('_wabt_close_mem_writer'); var defaultBinaryErrorCallback = BinaryErrorHandlerCallback.define('_wabt_default_binary_error_callback'); var defaultSourceErrorCallback = SourceErrorHandlerCallback.define('_wabt_default_source_error_callback'); @@ -643,13 +645,16 @@ var destroyAstLexer = Fn(Void, [Ptr(AstLexer)]).define('_wabt_destroy_ast_lexer' var destroyOutputBuffer = Fn(Void, [Ptr(OutputBuffer)]).define('_wabt_destroy_output_buffer'); var destroyScript = Fn(Void, [Ptr(Script)]).define('_wabt_destroy_script'); var destroyStackAllocator = Fn(Void, [Ptr(StackAllocator)]).define('_wabt_destroy_stack_allocator'); +var getFirstModule = Fn(Ptr(Module), [Ptr(Script)]).define('_wabt_get_first_module'); var getLibcAllocator = Fn(Ptr(Allocator), []).define('_wabt_get_libc_allocator'); var initMemWriter = Fn(I32, [Ptr(Allocator), Ptr(MemoryWriter)]).define('_wabt_init_mem_writer'); var initStackAllocator = Fn(Void, [Ptr(StackAllocator), Ptr(Allocator)]).define('_wabt_init_stack_allocator'); var initStream = Fn(Void, [Ptr(Stream), Ptr(Writer), Ptr(Stream)]).define('_wabt_init_stream'); var newAstBufferLexer = Fn(Ptr(AstLexer), [Ptr(Allocator), Str, BufPtr, BufLen]).define('_wabt_new_ast_buffer_lexer'); var parseAst = Fn(I32, [Ptr(AstLexer), Ptr(Script), Ptr(SourceErrorHandler)]).define('_wabt_parse_ast'); -var writeBinaryScript = Fn(I32, [Ptr(Allocator), Ptr(Writer), Ptr(Script), Ptr(WriteBinaryOptions)]).define('_wabt_write_binary_script'); +var resolveNamesScript = Fn(I32, [Ptr(Allocator), Ptr(AstLexer), Ptr(Script), Ptr(SourceErrorHandler)]).define('_wabt_resolve_names_script'); +var validateScript = Fn(I32, [Ptr(Allocator), Ptr(AstLexer), Ptr(Script), Ptr(SourceErrorHandler)]).define('_wabt_validate_script'); +var writeBinaryModule = Fn(I32, [Ptr(Allocator), Ptr(Writer), Ptr(Module), Ptr(WriteBinaryOptions)]).define('_wabt_write_binary_module'); return { // Types @@ -658,6 +663,7 @@ return { BinaryErrorHandler: BinaryErrorHandler, Location: Location, MemoryWriter: MemoryWriter, + Module: Module, OutputBuffer: OutputBuffer, ReadBinaryOptions: ReadBinaryOptions, Script: Script, @@ -669,7 +675,6 @@ return { Writer: Writer, // Functions - checkAst: checkAst, closeMemWriter: closeMemWriter, defaultBinaryErrorCallback: defaultBinaryErrorCallback, defaultSourceErrorCallback: defaultSourceErrorCallback, @@ -677,13 +682,16 @@ return { destroyOutputBuffer: destroyOutputBuffer, destroyScript: destroyScript, destroyStackAllocator: destroyStackAllocator, + getFirstModule: getFirstModule, getLibcAllocator: getLibcAllocator, initMemWriter: initMemWriter, initStackAllocator: initStackAllocator, initStream: initStream, newAstBufferLexer: newAstBufferLexer, parseAst: parseAst, - writeBinaryScript: writeBinaryScript, + resolveNamesScript: resolveNamesScript, + validateScript: validateScript, + writeBinaryModule: writeBinaryModule, }; })(); @@ -946,21 +954,32 @@ function Script() { this.$errorHandler = null; } Script.prototype = Object.create(Object.prototype); -Script.prototype.check = function() { - var result = I.checkAst( +Script.prototype.resolveNames = function() { + var result = I.resolveNamesScript( + this.$allocator.$, this.$astLexer.$, this.$, this.$errorHandler.$); + if (result != OK) { + throw new Error('resolveNames failed:\n' + this.$errorHandler.errorMessage); + } +}; +Script.prototype.validate = function() { + var result = I.validateScript( this.$allocator.$, this.$astLexer.$, this.$, this.$errorHandler.$); if (result != OK) { - throw new Error('check failed:\n' + this.$errorHandler.errorMessage); + throw new Error('validate failed:\n' + this.$errorHandler.errorMessage); } }; Script.prototype.toBinary = function(options) { var mw = new MemoryWriter(this.$allocator); options = new WriteBinaryOptions(this.$allocator, options || {}); try { + var module = I.getFirstModule(this.$); + if (module.$addr === 0) { + throw new Error('Script has no module.'); + } var result = - I.writeBinaryScript(this.$allocator.$, mw.writer.$, this.$, options.$); + I.writeBinaryModule(this.$allocator.$, mw.writer.$, module, options.$); if (result != OK) { - throw new Error('writeBinaryScript failed'); + throw new Error('writeBinaryModule failed'); } return {buffer: mw.buf.buf, log: options.log} } finally { @@ -1049,97 +1068,3 @@ wabt = { resolve(); }; - -/* -wabt.ready.then(function() { - if (false) { - try { - var sa = new wabt.StackAllocator(wabt.LibcAllocator); - var a = sa.allocator; - var data = '(module\n (func (result i32)\n (i32.const 1)))'; - var s = wabt.parseAst(a, 'foo.wast', data); - s.check(); - var output = s.toBinary({log: true}); - print('log output:\n' + output.log); - print('output:\n' + output.buffer); - } catch(e) { - print('ERROR' + (e.stack ? e.stack : e)); - } - if (s) s.$destroy(); - if (sa) sa.$destroy(); - } - - if (false) { - try { - var a = new wabt.StackAllocator(wabt.LibcAllocator); - var ma = wabt.LibcAllocator; - var buf = new Uint8Array([ - 0, 97, 115, 109, 11, 0, 0, 0, 4, 116, 121, 112, 101, 8, 2, 64, - 1, 1, 0, 64, 0, 0, 6, 105, 109, 112, 111, 114, 116, 12, 1, 0, - 3, 115, 116, 100, 5, 112, 114, 105, 110, 116, 8, 102, 117, 110, 99, 116, - 105, 111, 110, 2, 1, 1, 6, 101, 120, 112, 111, 114, 116, 4, 1, 0, - 1, 102, 4, 99, 111, 100, 101, 8, 1, 6, 0, 16, 42, 24, 1, 0, - ]); - var im = wabt.readInterpreterModule(a.allocator, ma, buf, {}); - var it = new wabt.InterpreterThread(a.allocator, im); - im.run(it, 1000, 0); - } catch(e) { - print('ERROR:' + (e.stack ? e.stack : e)); - } - if (it) it.$destroy(); - if (im) im.$destroy(); - if (a) a.$destroy(); - } - - if (true) { - try { - var sa = new wabt.StackAllocator(wabt.LibcAllocator); - var a = sa.allocator; - var source = ` - (module - (func $test (result i32) - (call $fib (i32.const 4))) - (func $fib (param $p i32) (result i32) - (local $a i32) - (local $b i32) - (local $t i32) - (set_local $a (i32.const 1)) - (set_local $b (i32.const 1)) - (loop $exit $cont - (set_local $p (i32.sub (get_local $p) (i32.const 1))) - (br_if $exit (i32.le_s (get_local $p) (i32.const 0))) - (set_local $t (get_local $b)) - (set_local $b (i32.add (get_local $a) (get_local $b))) - (set_local $a (get_local $t)) - (br $cont)) - (return (get_local $b))) - (export "test" $test)) - `; - var s = wabt.parseAst(a, 'foo.wast', source); - s.check(); - var output = s.toBinary(); - var im = wabt.readInterpreterModule(a, wabt.LibcAllocator, output.buffer); - if (true) { - print('disassemble:\n' + im.disassemble(0, 1000)); - } - if (true) { - var it = new wabt.InterpreterThread(a, im); - print('running'); - do { - print(im.tracePC(it).trimRight()); - } while (im.run(it, 1, 0)); - print('done'); - } - } catch(e) { - print('ERROR:' + (e.stack ? e.stack : e)); - } - if (it) it.$destroy(); - if (im) im.$destroy(); - if (s) s.$destroy(); - if (sa) sa.$destroy(); - } - -}).catch(function(error) { - print('ERROR:' + (error.stack ? error.stack : error)); -}); -*/ |