Index: src/wasm/encoder.cc |
diff --git a/src/wasm/encoder.cc b/src/wasm/encoder.cc |
index a1dd24ac16114a636873d643c4405497a06be4ed..cba3e38fddd5ce8499360b46eb9eaa2ca88e6581 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<uint16_t>(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<uint8_t>& 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<uint8_t>& 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<LocalType>(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<uint16_t>(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(); |
+ } |
} |
} |