LibWeb: Implement the WebAssembly "Memory object cache" + other changes

This cache is referenced by a few parts of the JS API spec, including
the threads spec (such as in toFixedLengthBuffer), as well as the
"refresh the Memory buffer" algorithm, which was implemented as a method
of Memory before this change.

Now, this algorithm can be implemented in a spec-like fashion (though it
mostly seems to add extra complexity). This change also fixes a bug
where memories that were re-exported from an imported WebAssembly.Memory
were given a distinct WebAssembly.Memory object, since the caching that
existed in Instance.cpp was instance-local, not global to the realm.

We also make Memory::m_buffer non-lazy, since we have to implement
"initialize a memory object" correctly anyway.
This commit is contained in:
CountBleck 2025-08-18 21:13:58 -07:00 committed by Ali Mohammad Pur
parent a2dc6c4bbb
commit f2a170bcfb
Notes: github-actions[bot] 2025-08-23 06:27:54 +00:00
6 changed files with 55 additions and 33 deletions

View file

@ -71,10 +71,11 @@ void Instance::initialize(JS::Realm& realm)
m_exports->define_direct_property(name, *object, JS::default_attributes);
},
[&](Wasm::MemoryAddress const& address) {
Optional<GC::Ptr<Memory>> object = m_memory_instances.get(address);
Optional<GC::Ptr<Memory>> object = cache.get_memory_instance(address);
if (!object.has_value()) {
// FIXME: Once LibWasm implements the threads/atomics proposal, the shared-ness should be
// obtained from the Wasm::MemoryInstance's type.
object = realm.create<Memory>(realm, address, Memory::Shared::No);
m_memory_instances.set(address, *object);
}
m_exports->define_direct_property(name, *object, JS::default_attributes);
@ -98,7 +99,6 @@ void Instance::visit_edges(Visitor& visitor)
Base::visit_edges(visitor);
visitor.visit(m_exports);
visitor.visit(m_function_instances);
visitor.visit(m_memory_instances);
visitor.visit(m_table_instances);
}