WebAssemblyInstanceConstructor.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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() = default;
  19. JS::ThrowCompletionOr<JS::Value> WebAssemblyInstanceConstructor::call()
  20. {
  21. return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance");
  22. }
  23. JS::ThrowCompletionOr<JS::Object*> WebAssemblyInstanceConstructor::construct(FunctionObject&)
  24. {
  25. auto& vm = this->vm();
  26. auto& global_object = this->global_object();
  27. auto* module_argument = TRY(vm.argument(0).to_object(global_object));
  28. if (!is<WebAssemblyModuleObject>(module_argument))
  29. return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
  30. auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument);
  31. auto result = TRY(WebAssemblyObject::instantiate_module(module_object.module(), vm, global_object));
  32. return heap().allocate<WebAssemblyInstanceObject>(global_object, global_object, result);
  33. }
  34. void WebAssemblyInstanceConstructor::initialize(JS::GlobalObject& global_object)
  35. {
  36. auto& vm = this->vm();
  37. auto& window = static_cast<WindowObject&>(global_object);
  38. NativeFunction::initialize(global_object);
  39. define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyInstancePrototype>("WebAssemblyInstancePrototype"), 0);
  40. define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  41. }
  42. }