WebAssembly.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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::NonnullGCPtr<WebIDL::Promise>> compile(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes);
  21. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> compile_streaming(JS::VM&, JS::Handle<WebIDL::Promise> source);
  22. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM&, JS::Handle<WebIDL::BufferSource>& bytes, Optional<JS::Handle<JS::Object>>& import_object);
  23. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate(JS::VM&, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object);
  24. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> instantiate_streaming(JS::VM&, JS::Handle<WebIDL::Promise> source, Optional<JS::Handle<JS::Object>>& import_object);
  25. namespace Detail {
  26. struct CompiledWebAssemblyModule : public RefCounted<CompiledWebAssemblyModule> {
  27. explicit CompiledWebAssemblyModule(NonnullRefPtr<Wasm::Module> module)
  28. : module(move(module))
  29. {
  30. }
  31. NonnullRefPtr<Wasm::Module> module;
  32. };
  33. class WebAssemblyCache {
  34. public:
  35. void add_compiled_module(NonnullRefPtr<CompiledWebAssemblyModule> module) { m_compiled_modules.append(module); }
  36. void add_function_instance(Wasm::FunctionAddress address, JS::GCPtr<JS::NativeFunction> function) { m_function_instances.set(address, function); }
  37. void add_imported_object(JS::GCPtr<JS::Object> object) { m_imported_objects.set(object); }
  38. void add_extern_value(Wasm::ExternAddress address, JS::Value value) { m_extern_values.set(address, value); }
  39. Optional<JS::GCPtr<JS::NativeFunction>> get_function_instance(Wasm::FunctionAddress address) { return m_function_instances.get(address); }
  40. Optional<JS::Value> get_extern_value(Wasm::ExternAddress address) { return m_extern_values.get(address); }
  41. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances() const { return m_function_instances; }
  42. HashMap<Wasm::ExternAddress, JS::Value> extern_values() const { return m_extern_values; }
  43. HashTable<JS::GCPtr<JS::Object>> imported_objects() const { return m_imported_objects; }
  44. Wasm::AbstractMachine& abstract_machine() { return m_abstract_machine; }
  45. private:
  46. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> m_function_instances;
  47. HashMap<Wasm::ExternAddress, JS::Value> m_extern_values;
  48. Vector<NonnullRefPtr<CompiledWebAssemblyModule>> m_compiled_modules;
  49. HashTable<JS::GCPtr<JS::Object>> m_imported_objects;
  50. Wasm::AbstractMachine m_abstract_machine;
  51. };
  52. WebAssemblyCache& get_cache(JS::Realm&);
  53. JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS::VM&, Wasm::Module const&, JS::GCPtr<JS::Object> import_object);
  54. JS::ThrowCompletionOr<NonnullRefPtr<CompiledWebAssemblyModule>> compile_a_webassembly_module(JS::VM&, ByteBuffer);
  55. JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, ByteString const& name, Instance* instance = nullptr);
  56. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
  57. Wasm::Value default_webassembly_value(JS::VM&, Wasm::ValueType type);
  58. JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value, Wasm::ValueType type);
  59. extern HashMap<JS::GCPtr<JS::Object>, WebAssemblyCache> s_caches;
  60. }
  61. }