LibWasm: Improve table support

Implements `table.get`, `table.set`, `elem.drop`, `table.size`,
and `table.grow`. Also fixes a few issues when generating ref-related
spectests. Also changes the `TableInstance` type to use
`Vector<Reference>` instead of `Vector<Optional<Reference>>`, because
the ability to be null is already encoded in the `Reference` type.
This commit is contained in:
Diego 2024-05-31 17:14:49 -07:00 committed by Ali Mohammad Pur
commit d906255cbb
Notes: sideshowbarker 2024-07-18 03:35:30 +09:00
6 changed files with 82 additions and 29 deletions

View file

@ -61,6 +61,10 @@ public:
: m_ref(move(ref))
{
}
explicit Reference()
: m_ref(Reference::Null { ValueType(ValueType::Kind::FunctionReference) })
{
}
auto& ref() const { return m_ref; }
@ -370,7 +374,7 @@ using FunctionInstance = Variant<WasmFunction, HostFunction>;
class TableInstance {
public:
explicit TableInstance(TableType const& type, Vector<Optional<Reference>> elements)
explicit TableInstance(TableType const& type, Vector<Reference> elements)
: m_elements(move(elements))
, m_type(type)
{
@ -380,15 +384,18 @@ public:
auto& elements() { return m_elements; }
auto& type() const { return m_type; }
bool grow(size_t size_to_grow, Reference const& fill_value)
bool grow(u32 size_to_grow, Reference const& fill_value)
{
if (size_to_grow == 0)
return true;
auto new_size = m_elements.size() + size_to_grow;
size_t new_size = m_elements.size() + size_to_grow;
if (auto max = m_type.limits().max(); max.has_value()) {
if (max.value() < new_size)
return false;
}
if (new_size >= NumericLimits<u32>::max()) {
return false;
}
auto previous_size = m_elements.size();
if (m_elements.try_resize(new_size).is_error())
return false;
@ -398,7 +405,7 @@ public:
}
private:
Vector<Optional<Reference>> m_elements;
Vector<Reference> m_elements;
TableType m_type;
};