WebAssemblyInstanceConstructor.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <LibWeb/Bindings/WindowObject.h>
  9. #include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h>
  10. #include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
  11. #include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
  12. #include <LibWeb/WebAssembly/WebAssemblyModuleObject.h>
  13. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  14. namespace Web::Bindings {
  15. WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(JS::GlobalObject& global_object)
  16. : NativeFunction(*global_object.function_prototype())
  17. {
  18. }
  19. WebAssemblyInstanceConstructor::~WebAssemblyInstanceConstructor()
  20. {
  21. }
  22. JS::Value WebAssemblyInstanceConstructor::call()
  23. {
  24. vm().throw_exception<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssemblyInstance");
  25. return {};
  26. }
  27. JS::Value WebAssemblyInstanceConstructor::construct(FunctionObject&)
  28. {
  29. auto& vm = this->vm();
  30. auto& global_object = this->global_object();
  31. auto module_argument = vm.argument(0).to_object(global_object);
  32. if (vm.exception())
  33. return {};
  34. if (!is<WebAssemblyModuleObject>(module_argument)) {
  35. vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WebAssembly.Module");
  36. return {};
  37. }
  38. auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument);
  39. auto result = WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object);
  40. if (result.is_error()) {
  41. vm.throw_exception(global_object, result.release_error());
  42. return {};
  43. }
  44. return heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result.value());
  45. }
  46. void WebAssemblyInstanceConstructor::initialize(JS::GlobalObject& global_object)
  47. {
  48. auto& vm = this->vm();
  49. auto& window = static_cast<WindowObject&>(global_object);
  50. NativeFunction::initialize(global_object);
  51. define_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype"));
  52. define_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  53. }
  54. }