Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/WebAssembly/Memory.cpp (C++)

Code before:
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/SharedArrayBufferConstructor.h>
#include <LibJS/Runtime/VM.h>
#include <LibWasm/Types.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MemoryPrototype.h>
#include <LibWeb/WebAssembly/Memory.h>
#include <LibWeb/WebAssembly/WebAssembly.h>

namespace Web::WebAssembly {

GC_DEFINE_ALLOCATOR(Memory);

WebIDL::ExceptionOr<GC::Ref<Memory>> Memory::construct_impl(JS::Realm& realm, MemoryDescriptor& descriptor)
{
    auto& vm = realm.vm();

    // https://webassembly.github.io/threads/js-api/index.html#dom-memory-memory
    // 3. If maximum is not empty and maximum < initial, throw a RangeError exception.
    if (descriptor.maximum.has_value()) {
        if (descriptor.maximum.value() < descriptor.initial) {
            return vm.throw_completion<JS::RangeError>("Initial is larger than maximum."sv);
        }
    }

    // 4. Let share be shared if descriptor["shared"] is true and unshared otherwise.
    // 5. If share is shared and maximum is empty, throw a TypeError exception.
    auto shared = descriptor.shared.value_or(false);
    if (shared && !descriptor.maximum.has_value())
        return vm.throw_completion<JS::TypeError>("Maximum has to be specified for shared memory."sv);

    Wasm::Limits limits { descriptor.initial, move(descriptor.maximum) };
    Wasm::MemoryType memory_type { move(limits) };

    auto& cache = Detail::get_cache(realm);
    auto address = cache.abstract_machine().store().allocate(memory_type);
    if (!address.has_value())
        return vm.throw_completion<JS::TypeError>("Wasm Memory allocation failed"sv);

    auto memory_object = realm.create<Memory>(realm, *address, shared ? Shared::Yes : Shared::No);

    return memory_object;
}

Memory::Memory(JS::Realm& realm, Wasm::MemoryAddress address, Shared shared)
    : Bindings::PlatformObject(realm)
    , m_address(address)

Review comment (suggestion):
You don't need to nest these conditions:

```cpp
if (descriptor.maximum.has_value() && descriptor.maximum.value() < descriptor.initial)
    return vm.throw_completion<JS::RangeError>("Initial is larger than maximum."sv);
```

Code after:
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/SharedArrayBufferConstructor.h>
#include <LibJS/Runtime/VM.h>
#include <LibWasm/Types.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MemoryPrototype.h>
#include <LibWeb/WebAssembly/Memory.h>
#include <LibWeb/WebAssembly/WebAssembly.h>

namespace Web::WebAssembly {

GC_DEFINE_ALLOCATOR(Memory);

WebIDL::ExceptionOr<GC::Ref<Memory>> Memory::construct_impl(JS::Realm& realm, MemoryDescriptor& descriptor)
{
    auto& vm = realm.vm();

    // https://webassembly.github.io/threads/js-api/index.html#dom-memory-memory
    // 3. If maximum is not empty and maximum < initial, throw a RangeError exception.
    if (descriptor.maximum.has_value() && descriptor.maximum.value() < descriptor.initial) {
        return vm.throw_completion<JS::RangeError>("Initial is larger than maximum."sv);
    }

    // 4. Let share be shared if descriptor["shared"] is true and unshared otherwise.
    // 5. If share is shared and maximum is empty, throw a TypeError exception.
    auto shared = descriptor.shared.value_or(false);
    if (shared && !descriptor.maximum.has_value())
        return vm.throw_completion<JS::TypeError>("Maximum has to be specified for shared memory."sv);

    Wasm::Limits limits { descriptor.initial, move(descriptor.maximum) };
    Wasm::MemoryType memory_type { move(limits) };

    auto& cache = Detail::get_cache(realm);
    auto address = cache.abstract_machine().store().allocate(memory_type);
    if (!address.has_value())
        return vm.throw_completion<JS::TypeError>("Wasm Memory allocation failed"sv);

    auto memory_object = realm.create<Memory>(realm, *address, shared ? Shared::Yes : Shared::No);

    return memory_object;
}

Memory::Memory(JS::Realm& realm, Wasm::MemoryAddress address, Shared shared)
    : Bindings::PlatformObject(realm)
    , m_address(address)
    , m_shared(shared)
{