WebAssemblyInstanceConstructor.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/Intrinsics.h>
  8. #include <LibWeb/WebAssembly/Module.h>
  9. #include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h>
  10. #include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h>
  11. #include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h>
  12. #include <LibWeb/WebAssembly/WebAssemblyObject.h>
  13. namespace Web::Bindings {
  14. WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(JS::Realm& realm)
  15. : NativeFunction(*realm.intrinsics().function_prototype())
  16. {
  17. }
  18. WebAssemblyInstanceConstructor::~WebAssemblyInstanceConstructor() = default;
  19. JS::ThrowCompletionOr<JS::Value> WebAssemblyInstanceConstructor::call()
  20. {
  21. return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance");
  22. }
  23. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WebAssemblyInstanceConstructor::construct(FunctionObject&)
  24. {
  25. auto& vm = this->vm();
  26. auto& realm = *vm.current_realm();
  27. auto* module_argument = TRY(vm.argument(0).to_object(vm));
  28. if (!is<WebAssembly::Module>(module_argument))
  29. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module");
  30. auto& module_object = static_cast<WebAssembly::Module&>(*module_argument);
  31. auto result = TRY(WebAssemblyObject::instantiate_module(vm, module_object.module()));
  32. return MUST_OR_THROW_OOM(heap().allocate<WebAssemblyInstanceObject>(realm, realm, result));
  33. }
  34. JS::ThrowCompletionOr<void> WebAssemblyInstanceConstructor::initialize(JS::Realm& realm)
  35. {
  36. auto& vm = this->vm();
  37. MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
  38. define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"), 0);
  39. define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable);
  40. return {};
  41. }
  42. }