mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
LibWeb: Implement a very basic WebAssembly JS API
This impl is *extremely* simple, and is missing a lot of things, it's also not particularly spec-compliant in some places, but it's definitely a start :^)
This commit is contained in:
parent
bdd7741ae1
commit
a2af04837e
5 changed files with 450 additions and 1 deletions
96
Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h
Normal file
96
Userland/Libraries/LibWeb/WebAssembly/WebAssemblyObject.h
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibWasm/AbstractMachine/AbstractMachine.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::Bindings {
|
||||
|
||||
class WebAssemblyObject final : public JS::Object {
|
||||
JS_OBJECT(WebAssemblyObject, JS::Object);
|
||||
|
||||
public:
|
||||
explicit WebAssemblyObject(JS::GlobalObject&);
|
||||
virtual void initialize(JS::GlobalObject&) override;
|
||||
virtual ~WebAssemblyObject() override = default;
|
||||
|
||||
struct CompiledWebAssemblyModule {
|
||||
explicit CompiledWebAssemblyModule(Wasm::Module&& module)
|
||||
: module(move(module))
|
||||
{
|
||||
}
|
||||
|
||||
Wasm::Module module;
|
||||
};
|
||||
|
||||
// FIXME: These should just be members of the module (instance) object,
|
||||
// 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.
|
||||
static NonnullOwnPtrVector<CompiledWebAssemblyModule> s_compiled_modules;
|
||||
static NonnullOwnPtrVector<Wasm::ModuleInstance> s_instantiated_modules;
|
||||
|
||||
static Wasm::AbstractMachine s_abstract_machine;
|
||||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(validate);
|
||||
JS_DECLARE_NATIVE_FUNCTION(compile);
|
||||
JS_DECLARE_NATIVE_FUNCTION(instantiate);
|
||||
};
|
||||
|
||||
class WebAssemblyModuleObject final : public JS::Object {
|
||||
JS_OBJECT(WebAssemblyModuleObject, JS::Object);
|
||||
|
||||
public:
|
||||
explicit WebAssemblyModuleObject(JS::GlobalObject&, size_t index);
|
||||
virtual ~WebAssemblyModuleObject() override = default;
|
||||
|
||||
size_t index() const { return m_index; }
|
||||
const Wasm::Module& module() const { return WebAssemblyObject::s_compiled_modules.at(m_index).module; }
|
||||
|
||||
private:
|
||||
size_t m_index { 0 };
|
||||
};
|
||||
|
||||
class WebAssemblyInstancePrototype final : public JS::Object {
|
||||
JS_OBJECT(WebAssemblyInstancePrototype, JS::Object);
|
||||
|
||||
public:
|
||||
explicit WebAssemblyInstancePrototype(JS::GlobalObject& global_object)
|
||||
: JS::Object(global_object)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void initialize(JS::GlobalObject&) override;
|
||||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_GETTER(exports_getter);
|
||||
};
|
||||
|
||||
class WebAssemblyInstanceObject final : public JS::Object {
|
||||
JS_OBJECT(WebAssemblyInstanceObject, JS::Object);
|
||||
|
||||
public:
|
||||
explicit WebAssemblyInstanceObject(JS::GlobalObject&, size_t index);
|
||||
virtual void initialize(JS::GlobalObject&) override;
|
||||
virtual ~WebAssemblyInstanceObject() override = default;
|
||||
|
||||
size_t index() const { return m_index; }
|
||||
Wasm::ModuleInstance& instance() const { return WebAssemblyObject::s_instantiated_modules.at(m_index); }
|
||||
|
||||
void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
friend class WebAssemblyInstancePrototype;
|
||||
|
||||
private:
|
||||
size_t m_index { 0 };
|
||||
JS::Object* m_exports_object { nullptr };
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue