WebAssemblyObject.h 2.8 KB

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