WebAssemblyModuleConstructor.cpp 1.6 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/TypedArray.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/WebAssembly/WebAssemblyModuleConstructor.h>
  9. #include <LibWeb/WebAssembly/WebAssemblyModuleObject.h>
  10. #include <LibWeb/WebAssembly/WebAssemblyModulePrototype.h>
  11. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  12. namespace Web::Bindings {
  13. WebAssemblyModuleConstructor::WebAssemblyModuleConstructor(JS::Realm& realm)
  14. : NativeFunction(*realm.intrinsics().function_prototype())
  15. {
  16. }
  17. WebAssemblyModuleConstructor::~WebAssemblyModuleConstructor() = default;
  18. JS::ThrowCompletionOr<JS::Value> WebAssemblyModuleConstructor::call()
  19. {
  20. return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Module");
  21. }
  22. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WebAssemblyModuleConstructor::construct(FunctionObject&)
  23. {
  24. auto& vm = this->vm();
  25. auto& realm = *vm.current_realm();
  26. auto* buffer_object = TRY(vm.argument(0).to_object(vm));
  27. auto result = TRY(parse_module(vm, buffer_object));
  28. return MUST_OR_THROW_OOM(heap().allocate<WebAssemblyModuleObject>(realm, realm, result));
  29. }
  30. JS::ThrowCompletionOr<void> WebAssemblyModuleConstructor::initialize(JS::Realm& realm)
  31. {
  32. auto& vm = this->vm();
  33. MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
  34. define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyModulePrototype>(realm, "WebAssembly.Module"), 0);
  35. define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  36. return {};
  37. }
  38. }