WebAssemblyObject.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Object.h>
  8. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::Bindings {
  11. class WebAssemblyMemoryObject;
  12. class WebAssemblyTableObject;
  13. JS::ThrowCompletionOr<size_t> parse_module(JS::VM&, JS::Object* buffer);
  14. JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, DeprecatedString const& name);
  15. JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value);
  16. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
  17. class WebAssemblyObject final : public JS::Object {
  18. JS_OBJECT(WebAssemblyObject, JS::Object);
  19. public:
  20. explicit WebAssemblyObject(JS::Realm&);
  21. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  22. virtual ~WebAssemblyObject() override = default;
  23. virtual void visit_edges(Cell::Visitor&) override;
  24. static JS::ThrowCompletionOr<size_t> instantiate_module(JS::VM&, Wasm::Module const&);
  25. struct CompiledWebAssemblyModule {
  26. explicit CompiledWebAssemblyModule(Wasm::Module&& module)
  27. : module(move(module))
  28. {
  29. }
  30. Wasm::Module module;
  31. };
  32. // FIXME: These should just be members of the module (instance) object,
  33. // but the module needs to stick around while its instance is alive
  34. // so ideally this would be a refcounted object, shared between
  35. // WebAssemblyModuleObject's and WebAssemblyInstantiatedModuleObject's.
  36. struct ModuleCache {
  37. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::FunctionObject>> function_instances;
  38. HashMap<Wasm::MemoryAddress, JS::GCPtr<WebAssemblyMemoryObject>> memory_instances;
  39. HashMap<Wasm::TableAddress, JS::GCPtr<WebAssemblyTableObject>> table_instances;
  40. };
  41. struct GlobalModuleCache {
  42. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances;
  43. };
  44. static Vector<NonnullOwnPtr<CompiledWebAssemblyModule>> s_compiled_modules;
  45. static Vector<NonnullOwnPtr<Wasm::ModuleInstance>> s_instantiated_modules;
  46. static Vector<ModuleCache> s_module_caches;
  47. static GlobalModuleCache s_global_cache;
  48. static Wasm::AbstractMachine s_abstract_machine;
  49. private:
  50. JS_DECLARE_NATIVE_FUNCTION(validate);
  51. JS_DECLARE_NATIVE_FUNCTION(compile);
  52. JS_DECLARE_NATIVE_FUNCTION(instantiate);
  53. };
  54. class WebAssemblyMemoryObject final : public JS::Object {
  55. JS_OBJECT(WebAssemblyMemoryObject, JS::Object);
  56. public:
  57. WebAssemblyMemoryObject(JS::Realm&, Wasm::MemoryAddress);
  58. virtual ~WebAssemblyMemoryObject() override = default;
  59. auto address() const { return m_address; }
  60. private:
  61. Wasm::MemoryAddress m_address;
  62. };
  63. }