mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
LibWasm: Implement memory.init and passive mode data
This commit is contained in:
parent
aff2c59f37
commit
b5ca290605
5 changed files with 88 additions and 11 deletions
|
@ -54,6 +54,13 @@ Optional<GlobalAddress> Store::allocate(GlobalType const& type, Value value)
|
|||
return address;
|
||||
}
|
||||
|
||||
Optional<DataAddress> Store::allocate_data(Vector<u8> initializer)
|
||||
{
|
||||
DataAddress address { m_datas.size() };
|
||||
m_datas.append(DataInstance { move(initializer) });
|
||||
return address;
|
||||
}
|
||||
|
||||
Optional<ElementAddress> Store::allocate(ValueType const& type, Vector<Reference> references)
|
||||
{
|
||||
ElementAddress address { m_elements.size() };
|
||||
|
@ -101,6 +108,14 @@ ElementInstance* Store::get(ElementAddress address)
|
|||
return &m_elements[value];
|
||||
}
|
||||
|
||||
DataInstance* Store::get(DataAddress address)
|
||||
{
|
||||
auto value = address.value();
|
||||
if (m_datas.size() <= value)
|
||||
return nullptr;
|
||||
return &m_datas[value];
|
||||
}
|
||||
|
||||
ErrorOr<void, ValidationError> AbstractMachine::validate(Module& module)
|
||||
{
|
||||
if (module.validation_status() != Module::ValidationStatus::Unchecked) {
|
||||
|
@ -304,7 +319,7 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<Ex
|
|||
size_t offset = 0;
|
||||
result.values().first().value().visit(
|
||||
[&](auto const& value) { offset = value; },
|
||||
[&](Reference const&) { instantiation_result = InstantiationError { "Data segment offset returned a reference" }; });
|
||||
[&](Reference const&) { instantiation_result = InstantiationError { "Data segment offset returned a reference"sv }; });
|
||||
if (instantiation_result.has_value() && instantiation_result->is_error())
|
||||
return;
|
||||
if (main_module_instance.memories().size() <= data.index.value()) {
|
||||
|
@ -314,6 +329,13 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<Ex
|
|||
};
|
||||
return;
|
||||
}
|
||||
auto maybe_data_address = m_store.allocate_data(data.init);
|
||||
if (!maybe_data_address.has_value()) {
|
||||
instantiation_result = InstantiationError { "Failed to allocate a data instance for an active data segment"sv };
|
||||
return;
|
||||
}
|
||||
main_module_instance.datas().append(*maybe_data_address);
|
||||
|
||||
if (data.init.is_empty())
|
||||
return;
|
||||
auto address = main_module_instance.memories()[data.index.value()];
|
||||
|
@ -332,8 +354,13 @@ InstantiationResult AbstractMachine::instantiate(Module const& module, Vector<Ex
|
|||
instance->data().overwrite(offset, data.init.data(), data.init.size());
|
||||
}
|
||||
},
|
||||
[&](DataSection::Data::Passive const&) {
|
||||
// FIXME: What do we do here?
|
||||
[&](DataSection::Data::Passive const& passive) {
|
||||
auto maybe_data_address = m_store.allocate_data(passive.init);
|
||||
if (!maybe_data_address.has_value()) {
|
||||
instantiation_result = InstantiationError { "Failed to allocate a data instance for a passive data segment"sv };
|
||||
return;
|
||||
}
|
||||
main_module_instance.datas().append(*maybe_data_address);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue