WebAssembly.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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(NonnullRefPtr<Wasm::Module> module)
  26. : module(move(module))
  27. {
  28. }
  29. NonnullRefPtr<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. void add_extern_value(Wasm::ExternAddress address, JS::Value value) { m_extern_values.set(address, value); }
  37. Optional<JS::GCPtr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); }
  38. Optional<JS::Value> get_extern_value(Wasm::ExternAddress address) { return m_extern_values.get(address); }
  39. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances() const { return m_function_instances; }
  40. HashMap<Wasm::ExternAddress, JS::Value> extern_values() const { return m_extern_values; }
  41. HashTable<JS::GCPtr<JS::Object>> imported_objects() const { return m_imported_objects; }
  42. Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; }
  43. private:
  44. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> m_function_instances;
  45. HashMap<Wasm::ExternAddress, JS::Value> m_extern_values;
  46. Vector<NonnullRefPtr<CompiledWebAssemblyModule>> m_compiled_modules;
  47. HashTable<JS::GCPtr<JS::Object>> m_imported_objects;
  48. Wasm::AbstractMachine m_abstract_machine;
  49. };
  50. WebAssemblyCache& get_cache(JS::Realm&);
  51. JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM&, Wasm::Module const&);
  52. JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> parse_module(JS::VM&, JS::Object* buffer);
  53. JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name, Instance* instance = nullptr);
  54. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
  55. Wasm::Value default_webassembly_value(JS::VM&, Wasm::ValueType type);
  56. JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type);
  57. extern HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
  58. }
  59. }