WebAssemblyModuleConstructor.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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::ThrowCompletionOr<JS::Value> WebAssemblyModuleConstructor::call()
  22. {
  23. return vm().throw_completion<JS::TypeError>(global_object(), JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Module");
  24. }
  25. JS::ThrowCompletionOr<JS::Object*> WebAssemblyModuleConstructor::construct(FunctionObject&)
  26. {
  27. auto& vm = this->vm();
  28. auto& global_object = this->global_object();
  29. auto* buffer_object = TRY(vm.argument(0).to_object(global_object));
  30. auto result = TRY(parse_module(global_object, buffer_object));
  31. return heap().allocate<WebAssemblyModuleObject>(global_object, global_object, result);
  32. }
  33. void WebAssemblyModuleConstructor::initialize(JS::GlobalObject& global_object)
  34. {
  35. auto& vm = this->vm();
  36. auto& window = static_cast<WindowObject&>(global_object);
  37. NativeFunction::initialize(global_object);
  38. define_direct_property(vm.names.prototype, &window.ensure_web_prototype<WebAssemblyModulePrototype>("WebAssemblyModulePrototype"), 0);
  39. define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  40. }
  41. }