[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}
This commit is contained in:
titzer 2016-01-12 05:33:56 -08:00 committed by Commit bot
parent d00c4666a4
commit d672ee30c9
2 changed files with 39 additions and 22 deletions

View File

@ -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();
}
}
}

View File

@ -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) {