WebAssemblyModuleConstructor.cpp 1.7 KB

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