WebAssembly.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Optional.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Heap/Handle.h>
  11. #include <LibJS/Runtime/Completion.h>
  12. #include <LibJS/Runtime/Value.h>
  13. #include <LibWasm/AbstractMachine/AbstractMachine.h>
  14. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  15. #include <LibWeb/Forward.h>
  16. namespace Web::WebAssembly {
  17. void visit_edges(JS::Object&, JS::Cell::Visitor&);
  18. void finalize(JS::Object&);
  19. bool validate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
  20. WebIDL::ExceptionOr<JS::Value> compile(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
  21. WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object);
  22. WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object);
  23. namespace Detail {
  24. struct CompiledWebAssemblyModule : public RefCounted<CompiledWebAssemblyModule> {
  25. explicit CompiledWebAssemblyModule(Wasm::Module&& module)
  26. : module(move(module))
  27. {
  28. }
  29. Wasm::Module module;
  30. };
  31. class WebAssemblyCache {
  32. public:
  33. void add_compiled_module(NonnullRefPtr<CompiledWebAssemblyModule> module) { m_compiled_modules.append(module); }
  34. void add_function_instance(Wasm::FunctionAddress address, JS::GCPtr<JS::NativeFunction> function) { m_function_instances.set(address, function); }
  35. void add_imported_object(JS::GCPtr<JS::Object> object) { m_imported_objects.set(object); }
  36. Optional<JS::GCPtr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); }
  37. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances() const { return m_function_instances; }
  38. HashTable<JS::GCPtr<JS::Object>> imported_objects() const { return m_imported_objects; }
  39. Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; }
  40. private:
  41. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> m_function_instances;
  42. Vector<NonnullRefPtr<CompiledWebAssemblyModule>> m_compiled_modules;
  43. HashTable<JS::GCPtr<JS::Object>> m_imported_objects;
  44. Wasm::AbstractMachine m_abstract_machine;
  45. };
  46. WebAssemblyCache& get_cache(JS::Realm&);
  47. JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM&, Wasm::Module const&);
  48. JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> parse_module(JS::VM&, JS::Object* buffer);
  49. JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name, Instance* instance = nullptr);
  50. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
  51. JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type);
  52. extern HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
  53. }
  54. }