mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-11 17:00:37 +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:
parent
3402381d7a
commit
95b9821f26
Notes:
sideshowbarker
2024-07-18 17:54:19 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/95b9821f269 Pull-request: https://github.com/SerenityOS/serenity/pull/7097 Reviewed-by: https://github.com/Dexesttp
3 changed files with 39 additions and 4 deletions
|
@ -297,7 +297,20 @@ public:
|
|||
auto& data() const { return m_data; }
|
||||
auto& data() { return m_data; }
|
||||
|
||||
void grow(size_t new_size) { m_data.grow(new_size); }
|
||||
bool grow(size_t size_to_grow)
|
||||
{
|
||||
if (size_to_grow == 0)
|
||||
return true;
|
||||
auto new_size = m_data.size() + size_to_grow;
|
||||
if (m_type.limits().max().value_or(new_size) < new_size)
|
||||
return false;
|
||||
auto previous_size = m_size;
|
||||
m_data.grow(new_size);
|
||||
m_size = new_size;
|
||||
// The spec requires that we zero out everything on grow
|
||||
__builtin_memset(m_data.offset_pointer(previous_size), 0, size_to_grow);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
const MemoryType& m_type;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -34,4 +34,6 @@ static constexpr auto extern_table_tag = 0x01;
|
|||
static constexpr auto extern_memory_tag = 0x02;
|
||||
static constexpr auto extern_global_tag = 0x03;
|
||||
|
||||
static constexpr auto page_size = 64 * KiB;
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue