1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:07:35 +00:00

LibWeb: Convert WebAssemblyTablePrototype funcs to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-31 16:59:06 +02:00
parent f19512bf55
commit aa61110bdd
2 changed files with 35 additions and 49 deletions

View file

@ -13,21 +13,19 @@ namespace Web::Bindings {
void WebAssemblyTablePrototype::initialize(JS::GlobalObject& global_object) void WebAssemblyTablePrototype::initialize(JS::GlobalObject& global_object)
{ {
Object::initialize(global_object); Object::initialize(global_object);
define_old_native_accessor("length", length_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable); define_native_accessor("length", length_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_function("grow", grow, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_old_native_function("get", get, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable); define_native_function("get", get, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_old_native_function("set", set, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable); define_native_function("set", set, 1, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
} }
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
{ {
auto delta = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object)); auto delta = TRY(vm.argument(0).to_u32(global_object));
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) { if (!is<WebAssemblyTableObject>(this_object))
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table"); return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object); auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address(); auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address); auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
@ -36,7 +34,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
auto initial_size = table->elements().size(); auto initial_size = table->elements().size();
auto value_value = vm.argument(1); auto value_value = vm.argument(1);
auto reference_value = TRY_OR_DISCARD([&]() -> JS::ThrowCompletionOr<Wasm::Value> { auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
if (value_value.is_undefined()) if (value_value.is_undefined())
return Wasm::Value(table->type().element_type(), 0ull); return Wasm::Value(table->type().element_type(), 0ull);
@ -45,33 +43,27 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
auto& reference = reference_value.value().get<Wasm::Reference>(); auto& reference = reference_value.value().get<Wasm::Reference>();
if (!table->grow(delta, reference)) { if (!table->grow(delta, reference))
vm.throw_exception<JS::RangeError>(global_object, "Failed to grow table"); return vm.throw_completion<JS::RangeError>(global_object, "Failed to grow table");
return {};
}
return JS::Value(static_cast<u32>(initial_size)); return JS::Value(static_cast<u32>(initial_size));
} }
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::get) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
{ {
auto index = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object)); auto index = TRY(vm.argument(0).to_u32(global_object));
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) { if (!is<WebAssemblyTableObject>(this_object))
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table"); return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object); auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address(); auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address); auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
if (!table) if (!table)
return JS::js_undefined(); return JS::js_undefined();
if (table->elements().size() <= index) { if (table->elements().size() <= index)
vm.throw_exception<JS::RangeError>(global_object, "Table element index out of range"); return vm.throw_completion<JS::RangeError>(global_object, "Table element index out of range");
return {};
}
auto& ref = table->elements()[index]; auto& ref = table->elements()[index];
if (!ref.has_value()) if (!ref.has_value())
@ -81,28 +73,24 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
return to_js_value(global_object, wasm_value); return to_js_value(global_object, wasm_value);
} }
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::set) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
{ {
auto index = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object)); auto index = TRY(vm.argument(0).to_u32(global_object));
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) { if (!is<WebAssemblyTableObject>(this_object))
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table"); return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object); auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address(); auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address); auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
if (!table) if (!table)
return JS::js_undefined(); return JS::js_undefined();
if (table->elements().size() <= index) { if (table->elements().size() <= index)
vm.throw_exception<JS::RangeError>(global_object, "Table element index out of range"); return vm.throw_completion<JS::RangeError>(global_object, "Table element index out of range");
return {};
}
auto value_value = vm.argument(1); auto value_value = vm.argument(1);
auto reference_value = TRY_OR_DISCARD([&]() -> JS::ThrowCompletionOr<Wasm::Value> { auto reference_value = TRY([&]() -> JS::ThrowCompletionOr<Wasm::Value> {
if (value_value.is_undefined()) if (value_value.is_undefined())
return Wasm::Value(table->type().element_type(), 0ull); return Wasm::Value(table->type().element_type(), 0ull);
@ -115,13 +103,11 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
return JS::js_undefined(); return JS::js_undefined();
} }
JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::length_getter) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::length_getter)
{ {
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object)) { if (!is<WebAssemblyTableObject>(this_object))
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table"); return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object); auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address(); auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address); auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);

View file

@ -27,10 +27,10 @@ public:
virtual void initialize(JS::GlobalObject& global_object) override; virtual void initialize(JS::GlobalObject& global_object) override;
private: private:
JS_DECLARE_OLD_NATIVE_FUNCTION(grow); JS_DECLARE_NATIVE_FUNCTION(grow);
JS_DECLARE_OLD_NATIVE_FUNCTION(get); JS_DECLARE_NATIVE_FUNCTION(get);
JS_DECLARE_OLD_NATIVE_FUNCTION(set); JS_DECLARE_NATIVE_FUNCTION(set);
JS_DECLARE_OLD_NATIVE_FUNCTION(length_getter); JS_DECLARE_NATIVE_FUNCTION(length_getter);
}; };
} }