Skip to content

Commit

Permalink
Print error message
Browse files Browse the repository at this point in the history
- fix 2 typos (uint16_t to Walrus::ByteCodeStackOffset)

- fix 1 typo (because of it 3 tests failed, altough it was
  unnoticable hence the error wasn't caught)

- refactor nested if...else to switch...case

- print error message before exit

- make string() operator for Value class
  • Loading branch information
GorogPeter committed Jul 24, 2023
1 parent 326fc4e commit d537e06
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/parser/WASMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
ASSERT(peekVMStackSize() == Walrus::valueSizeInStack(toValueKind(Type::I32)));
auto stackPos = popVMStack();
size_t pos = m_currentFunction->currentByteCodeSize();
pushByteCode(Walrus::JumpIfFalse(stackPos, sizeof(Walrus::JumpIfFalse) + sizeof(Walrus::End) + sizeof(uint16_t) * m_currentFunctionType->result().size()), WASMOpcode::BrIfOpcode);
pushByteCode(Walrus::JumpIfFalse(stackPos, sizeof(Walrus::JumpIfFalse) + sizeof(Walrus::End) + sizeof(Walrus::ByteCodeStackOffset) * m_currentFunctionType->result().size()), WASMOpcode::BrIfOpcode);
for (size_t i = 0; i < m_currentFunctionType->result().size(); i++) {
ASSERT((m_vmStack.rbegin() + i)->m_size == Walrus::valueSizeInStack(m_currentFunctionType->result()[m_currentFunctionType->result().size() - i - 1]));
}
Expand Down Expand Up @@ -1458,7 +1458,7 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
if (tagIndex != std::numeric_limits<Index>::max()) {
auto functionType = m_result.m_functionTypes[m_result.m_tagTypes[tagIndex]->sigIndex()];
auto& param = functionType->param();
m_currentFunction->expandByteCode(sizeof(uint16_t) * param.size());
m_currentFunction->expandByteCode(sizeof(Walrus::ByteCodeStackOffset) * param.size());
Walrus::Throw* code = m_currentFunction->peekByteCode<Walrus::Throw>(pos);
for (size_t i = 0; i < param.size(); i++) {
code->dataOffsets()[param.size() - i - 1] = (m_vmStack.rbegin() + i)->m_position;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
&data);
}

if (UNLIKELY(elem->tableIndex() >= numberOfTableTypes() || index >= instance->m_tables[elem->tableIndex()]->size() || index + elem->functionIndex().size() > instance->m_tables[elem->tableIndex()]->size())) {
if (UNLIKELY(elem->tableIndex() >= numberOfTableTypes() || index > instance->m_tables[elem->tableIndex()]->size() || index + elem->functionIndex().size() > instance->m_tables[elem->tableIndex()]->size())) {
Trap::throwException(state, "out of bounds table access");
}

Expand Down
20 changes: 20 additions & 0 deletions src/runtime/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ class Value {
return false;
}

operator std::string() const
{
switch (m_type) {
case I32:
return std::to_string(asI32());
case I64:
return std::to_string(asI64());
case F32:
return std::to_string(asF32());
case F64:
return std::to_string(asF64());
case FuncRef:
case ExternRef:
return std::to_string((size_t)(asExternal()));
default:
ASSERT_NOT_REACHED();
return "";
}
}

private:
union {
int32_t m_i32;
Expand Down
Loading

0 comments on commit d537e06

Please sign in to comment.