1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibWasm: Implement memory.grow, memory.size and drop

These allow a very basic memory-using program to work.
This commit is contained in:
Ali Mohammad Pur 2021-05-03 22:45:47 +04:30 committed by Andreas Kling
parent 3402381d7a
commit 95b9821f26
3 changed files with 39 additions and 4 deletions

View file

@ -462,15 +462,35 @@ void Interpreter::interpret(Configuration& configuration, InstructionPointer& ip
global->set_value(move(value));
return;
}
case Instructions::memory_size.value():
case Instructions::memory_grow.value():
case Instructions::memory_size.value(): {
auto address = configuration.frame()->module().memories()[0];
auto instance = configuration.store().get(address);
auto pages = instance->size() / Constants::page_size;
dbgln_if(WASM_TRACE_DEBUG, "memory.size -> stack({})", pages);
configuration.stack().push(make<Value>((i32)pages));
return;
}
case Instructions::memory_grow.value(): {
auto address = configuration.frame()->module().memories()[0];
auto instance = configuration.store().get(address);
i32 old_pages = instance->size() / Constants::page_size;
auto new_pages = configuration.stack().pop().get<NonnullOwnPtr<Value>>()->to<i32>();
VERIFY(new_pages.has_value());
if (instance->grow(new_pages.value() * Constants::page_size))
configuration.stack().push(make<Value>((i32)old_pages));
else
configuration.stack().push(make<Value>((i32)-1));
return;
}
case Instructions::table_get.value():
case Instructions::table_set.value():
case Instructions::ref_null.value():
case Instructions::ref_func.value():
case Instructions::ref_is_null.value():
case Instructions::drop.value():
goto unimplemented;
case Instructions::drop.value():
configuration.stack().pop();
return;
case Instructions::select.value():
case Instructions::select_typed.value(): {
// Note: The type seems to only be used for validation.