LibWeb: Convert WebAssemblyMemoryPrototype funcs to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-31 16:57:30 +02:00
parent c7c914800c
commit f19512bf55
Notes: sideshowbarker 2024-07-18 01:41:03 +09:00
2 changed files with 15 additions and 21 deletions

View file

@ -13,18 +13,16 @@ namespace Web::Bindings {
void WebAssemblyMemoryPrototype::initialize(JS::GlobalObject& global_object)
{
Object::initialize(global_object);
define_old_native_accessor("buffer", buffer_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_old_native_function("grow", grow, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_accessor("buffer", buffer_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_function("grow", grow, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
}
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
{
auto page_count = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
return {};
}
auto page_count = TRY(vm.argument(0).to_u32(global_object));
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
auto* memory_object = static_cast<WebAssemblyMemoryObject*>(this_object);
auto address = memory_object->address();
auto* memory = WebAssemblyObject::s_abstract_machine.store().get(address);
@ -32,21 +30,17 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
return JS::js_undefined();
auto previous_size = memory->size() / Wasm::Constants::page_size;
if (!memory->grow(page_count * Wasm::Constants::page_size)) {
vm.throw_exception<JS::TypeError>(global_object, "Memory.grow() grows past the stated limit of the memory instance");
return {};
}
if (!memory->grow(page_count * Wasm::Constants::page_size))
return vm.throw_completion<JS::TypeError>(global_object, "Memory.grow() grows past the stated limit of the memory instance");
return JS::Value(static_cast<u32>(previous_size));
}
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::buffer_getter)
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::buffer_getter)
{
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
return {};
}
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyMemoryObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
auto* memory_object = static_cast<WebAssemblyMemoryObject*>(this_object);
auto address = memory_object->address();
auto* memory = WebAssemblyObject::s_abstract_machine.store().get(address);

View file

@ -27,8 +27,8 @@ public:
virtual void initialize(JS::GlobalObject&) override;
private:
JS_DECLARE_OLD_NATIVE_FUNCTION(grow);
JS_DECLARE_OLD_NATIVE_FUNCTION(buffer_getter);
JS_DECLARE_NATIVE_FUNCTION(grow);
JS_DECLARE_NATIVE_FUNCTION(buffer_getter);
};
}