Serenity Operating System
at master 53 lines 2.0 kB view raw
1/* 2 * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibJS/Runtime/GlobalObject.h> 8#include <LibWeb/Bindings/Intrinsics.h> 9#include <LibWeb/WebAssembly/WebAssemblyInstanceConstructor.h> 10#include <LibWeb/WebAssembly/WebAssemblyInstanceObject.h> 11#include <LibWeb/WebAssembly/WebAssemblyInstanceObjectPrototype.h> 12#include <LibWeb/WebAssembly/WebAssemblyModuleObject.h> 13#include <LibWeb/WebAssembly/WebAssemblyObject.h> 14 15namespace Web::Bindings { 16 17WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(JS::Realm& realm) 18 : NativeFunction(*realm.intrinsics().function_prototype()) 19{ 20} 21 22WebAssemblyInstanceConstructor::~WebAssemblyInstanceConstructor() = default; 23 24JS::ThrowCompletionOr<JS::Value> WebAssemblyInstanceConstructor::call() 25{ 26 return vm().throw_completion<JS::TypeError>(JS::ErrorType::ConstructorWithoutNew, "WebAssembly.Instance"); 27} 28 29JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Object>> WebAssemblyInstanceConstructor::construct(FunctionObject&) 30{ 31 auto& vm = this->vm(); 32 auto& realm = *vm.current_realm(); 33 34 auto* module_argument = TRY(vm.argument(0).to_object(vm)); 35 if (!is<WebAssemblyModuleObject>(module_argument)) 36 return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebAssembly.Module"); 37 auto& module_object = static_cast<WebAssemblyModuleObject&>(*module_argument); 38 auto result = TRY(WebAssemblyObject::instantiate_module(vm, module_object.module())); 39 return MUST_OR_THROW_OOM(heap().allocate<WebAssemblyInstanceObject>(realm, realm, result)); 40} 41 42JS::ThrowCompletionOr<void> WebAssemblyInstanceConstructor::initialize(JS::Realm& realm) 43{ 44 auto& vm = this->vm(); 45 46 MUST_OR_THROW_OOM(NativeFunction::initialize(realm)); 47 define_direct_property(vm.names.prototype, &Bindings::ensure_web_prototype<WebAssemblyInstancePrototype>(realm, "WebAssembly.Instance"), 0); 48 define_direct_property(vm.names.length, JS::Value(1), JS::Attribute::Configurable); 49 50 return {}; 51} 52 53}