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