WebAssemblyObject.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. JS::ThrowCompletionOr<size_t> parse_module(JS::GlobalObject& global_object, JS::Object* buffer);
  14. JS::NativeFunction* create_native_function(JS::GlobalObject& global_object, Wasm::FunctionAddress address, String const& name);
  15. JS::Value to_js_value(JS::GlobalObject& global_object, Wasm::Value& wasm_value);
  16. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::GlobalObject& global_object, JS::Value value, const Wasm::ValueType& type);
  17. class WebAssemblyObject final : public JS::Object {
  18. JS_OBJECT(WebAssemblyObject, JS::Object);
  19. public:
  20. explicit WebAssemblyObject(JS::GlobalObject&);
  21. virtual void initialize(JS::GlobalObject&) override;
  22. virtual ~WebAssemblyObject() override = default;
  23. virtual void visit_edges(Cell::Visitor&) override;
  24. static JS::ThrowCompletionOr<size_t> instantiate_module(Wasm::Module const&, JS::VM&, JS::GlobalObject&);
  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::FunctionObject*> function_instances;
  38. HashMap<Wasm::MemoryAddress, WebAssemblyMemoryObject*> memory_instances;
  39. };
  40. struct GlobalModuleCache {
  41. HashMap<Wasm::FunctionAddress, JS::NativeFunction*> function_instances;
  42. };
  43. static NonnullOwnPtrVector<CompiledWebAssemblyModule> s_compiled_modules;
  44. static NonnullOwnPtrVector<Wasm::ModuleInstance> s_instantiated_modules;
  45. static Vector<ModuleCache> s_module_caches;
  46. static GlobalModuleCache s_global_cache;
  47. static Wasm::AbstractMachine s_abstract_machine;
  48. private:
  49. JS_DECLARE_NATIVE_FUNCTION(validate);
  50. JS_DECLARE_NATIVE_FUNCTION(compile);
  51. JS_DECLARE_NATIVE_FUNCTION(instantiate);
  52. };
  53. class WebAssemblyMemoryObject final : public JS::Object {
  54. JS_OBJECT(WebAssemblyMemoryObject, JS::Object);
  55. public:
  56. explicit WebAssemblyMemoryObject(JS::GlobalObject&, Wasm::MemoryAddress);
  57. virtual ~WebAssemblyMemoryObject() override = default;
  58. auto address() const { return m_address; }
  59. private:
  60. Wasm::MemoryAddress m_address;
  61. };
  62. }