WebAssembly.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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::Cell::Visitor&);
  18. bool validate(JS::VM&, JS::Handle<JS::Object>& bytes);
  19. WebIDL::ExceptionOr<JS::Value> compile(JS::VM&, JS::Handle<JS::Object>& bytes);
  20. WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, JS::Handle<JS::Object>& bytes, Optional<JS::Handle<JS::Object>>& import_object);
  21. WebIDL::ExceptionOr<JS::Value> instantiate(JS::VM&, Module const& module_object, Optional<JS::Handle<JS::Object>>& import_object);
  22. namespace Detail {
  23. JS::ThrowCompletionOr<size_t> instantiate_module(JS::VM&, Wasm::Module const&);
  24. JS::ThrowCompletionOr<size_t> parse_module(JS::VM&, JS::Object* buffer);
  25. JS::NativeFunction* create_native_function(JS::VM&, Wasm::FunctionAddress address, DeprecatedString const& name);
  26. JS::ThrowCompletionOr<Wasm::Value> to_webassembly_value(JS::VM&, JS::Value value, Wasm::ValueType const& type);
  27. JS::Value to_js_value(JS::VM&, Wasm::Value& wasm_value);
  28. struct CompiledWebAssemblyModule {
  29. explicit CompiledWebAssemblyModule(Wasm::Module&& module)
  30. : module(move(module))
  31. {
  32. }
  33. Wasm::Module module;
  34. };
  35. // FIXME: These should just be members of the module (instance) object, but the module needs to stick
  36. // around while its instance is alive so ideally this would be a refcounted object, shared between
  37. // WebAssemblyModuleObject's and WebAssemblyInstantiatedModuleObject's.
  38. struct ModuleCache {
  39. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::FunctionObject>> function_instances;
  40. HashMap<Wasm::MemoryAddress, JS::GCPtr<WebAssembly::Memory>> memory_instances;
  41. HashMap<Wasm::TableAddress, JS::GCPtr<WebAssembly::Table>> table_instances;
  42. };
  43. struct GlobalModuleCache {
  44. HashMap<Wasm::FunctionAddress, JS::GCPtr<JS::NativeFunction>> function_instances;
  45. };
  46. extern Vector<NonnullOwnPtr<CompiledWebAssemblyModule>> s_compiled_modules;
  47. extern Vector<NonnullOwnPtr<Wasm::ModuleInstance>> s_instantiated_modules;
  48. extern Vector<ModuleCache> s_module_caches;
  49. extern GlobalModuleCache s_global_cache;
  50. extern Wasm::AbstractMachine s_abstract_machine;
  51. }
  52. }