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

LibWeb: Cache the WebAssembly objects that we hand out to JS

The spec requires this behaviour, and it's generally faster to do this
instead of re-resolving and re-creating the instances anyway.
This commit is contained in:
Ali Mohammad Pur 2021-06-21 04:12:15 +04:30 committed by Ali Mohammad Pur
parent a256997064
commit e523e530fc
2 changed files with 52 additions and 7 deletions

View file

@ -13,6 +13,8 @@
namespace Web::Bindings {
class WebAssemblyMemoryObject;
class WebAssemblyObject final : public JS::Object {
JS_OBJECT(WebAssemblyObject, JS::Object);
@ -21,6 +23,8 @@ public:
virtual void initialize(JS::GlobalObject&) override;
virtual ~WebAssemblyObject() override = default;
virtual void visit_edges(Cell::Visitor&) override;
struct CompiledWebAssemblyModule {
explicit CompiledWebAssemblyModule(Wasm::Module&& module)
: module(move(module))
@ -34,8 +38,18 @@ public:
// but the module needs to stick around while its instance is alive
// so ideally this would be a refcounted object, shared between
// WebAssemblyModuleObject's and WebAssemblyInstantiatedModuleObject's.
struct ModuleCache {
HashMap<Wasm::FunctionAddress, JS::Function*> function_instances;
HashMap<Wasm::MemoryAddress, WebAssemblyMemoryObject*> memory_instances;
};
struct GlobalModuleCache {
HashMap<Wasm::FunctionAddress, JS::NativeFunction*> function_instances;
};
static NonnullOwnPtrVector<CompiledWebAssemblyModule> s_compiled_modules;
static NonnullOwnPtrVector<Wasm::ModuleInstance> s_instantiated_modules;
static Vector<ModuleCache> s_module_caches;
static GlobalModuleCache s_global_cache;
static Wasm::AbstractMachine s_abstract_machine;
@ -69,6 +83,7 @@ public:
size_t index() const { return m_index; }
Wasm::ModuleInstance& instance() const { return WebAssemblyObject::s_instantiated_modules.at(m_index); }
auto& cache() { return WebAssemblyObject::s_module_caches.at(m_index); }
void visit_edges(Cell::Visitor&) override;