From d672ee30c9424670fe950ca6595d7c5438a52f20 Mon Sep 17 00:00:00 2001 From: titzer Date: Tue, 12 Jan 2016 05:33:56 -0800 Subject: [PATCH] [wasm] Fix empty asm.js function in ASM->WASM. R=ahaas@chromium.org BUG= Review URL: https://codereview.chromium.org/1574263002 Cr-Commit-Position: refs/heads/master@{#33238} --- src/wasm/encoder.cc | 48 +++++++++++++++++++---------------- test/mjsunit/wasm/asm-wasm.js | 13 ++++++++++ 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/wasm/encoder.cc b/src/wasm/encoder.cc index a1dd24ac16..cba3e38fdd 100644 --- a/src/wasm/encoder.cc +++ b/src/wasm/encoder.cc @@ -157,27 +157,30 @@ WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, WasmModuleBuilder* mb) const { WasmFunctionEncoder* e = new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_); - auto var_index = new uint16_t[locals_.size()]; + uint16_t* var_index = zone->NewArray(locals_.size()); IndexVars(e, var_index); - const byte* start = &body_[0]; - const byte* end = start + body_.size(); - size_t local_index = 0; - for (size_t i = 0; i < body_.size();) { - if (local_index < local_indices_.size() && - i == local_indices_[local_index]) { - int length = 0; - uint32_t index; - ReadUnsignedLEB128Operand(start + i, end, &length, &index); - uint16_t new_index = var_index[index]; - const std::vector& index_vec = UnsignedLEB128From(new_index); - for (size_t j = 0; j < index_vec.size(); j++) { - e->body_.push_back(index_vec.at(j)); + if (body_.size() > 0) { + // TODO(titzer): iterate over local indexes, not the bytes. + const byte* start = &body_[0]; + const byte* end = start + body_.size(); + size_t local_index = 0; + for (size_t i = 0; i < body_.size();) { + if (local_index < local_indices_.size() && + i == local_indices_[local_index]) { + int length = 0; + uint32_t index; + ReadUnsignedLEB128Operand(start + i, end, &length, &index); + uint16_t new_index = var_index[index]; + const std::vector& index_vec = UnsignedLEB128From(new_index); + for (size_t j = 0; j < index_vec.size(); j++) { + e->body_.push_back(index_vec.at(j)); + } + i += length; + local_index++; + } else { + e->body_.push_back(*(start + i)); + i++; } - i += length; - local_index++; - } else { - e->body_.push_back(*(start + i)); - i++; } } FunctionSig::Builder sig(zone, return_type_ == kAstStmt ? 0 : 1, @@ -189,7 +192,6 @@ WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, sig.AddParam(static_cast(e->params_[i])); } e->signature_index_ = mb->AddSignature(sig.Build()); - delete[] var_index; e->name_.insert(e->name_.begin(), name_.begin(), name_.end()); return e; } @@ -295,8 +297,10 @@ void WasmFunctionEncoder::Serialize(byte* buffer, byte** header, if (!external_) { EmitUint16(header, static_cast(body_.size())); - std::memcpy(*header, &body_[0], body_.size()); - (*header) += body_.size(); + if (body_.size() > 0) { + std::memcpy(*header, &body_[0], body_.size()); + (*header) += body_.size(); + } } } diff --git a/test/mjsunit/wasm/asm-wasm.js b/test/mjsunit/wasm/asm-wasm.js index 7f187ada27..7c1da6ef03 100644 --- a/test/mjsunit/wasm/asm-wasm.js +++ b/test/mjsunit/wasm/asm-wasm.js @@ -4,6 +4,19 @@ // Flags: --expose-wasm +function EmptyTest() { + "use asm"; + function caller() { + empty(); + return 11; + } + function empty() { + } + return {caller: caller}; +} + +assertEquals(11, WASM.asmCompileRun(EmptyTest.toString())); + function IntTest() { "use asm"; function sum(a, b) {