WebAssemblyInstanceConstructor.cpp 2.2 KB

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