WebAssemblyModuleConstructor.cpp 1.8 KB

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