WebAssemblyModuleConstructor.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/TypedArray.h>
  9. #include <LibWeb/Bindings/WindowObject.h>
  10. #include <LibWeb/WebAssembly/WebAssemblyModuleConstructor.h>
  11. #include <LibWeb/WebAssembly/WebAssemblyModuleObject.h>
  12. #include <LibWeb/WebAssembly/WebAssemblyModulePrototype.h>
  13. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  14. namespace Web::Bindings {
  15. WebAssemblyModuleConstructor::WebAssemblyModuleConstructor(JS::GlobalObject& global_object)
  16. : NativeFunction(*global_object.function_prototype())
  17. {
  18. }
  19. WebAssemblyModuleConstructor::~WebAssemblyModuleConstructor()
  20. {
  21. }
  22. JS::Value WebAssemblyModuleConstructor::call()
  23. {
  24. vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssemblyModule");
  25. return {};
  26. }
  27. JS::Value WebAssemblyModuleConstructor::construct(FunctionObject&)
  28. {
  29. auto& vm = this->vm();
  30. auto& global_object = this->global_object();
  31. auto buffer_object = vm.argument(0).to_object(global_object);
  32. if (vm.exception())
  33. return {};
  34. auto result = parse_module(global_object, buffer_object);
  35. if (result.is_error()) {
  36. vm.throw_exception(global_object, result.error());
  37. return {};
  38. }
  39. return heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result.release_value());
  40. }
  41. void WebAssemblyModuleConstructor::initialize(JS::GlobalObject& global_object)
  42. {
  43. auto& vm = this->vm();
  44. auto& window = static_cast<WindowObject&>(global_object);
  45. NativeFunction::initialize(global_object);
  46. define_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyModulePrototype>("WebAssemblyModulePrototype"));
  47. define_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  48. }
  49. }