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
Notes: sideshowbarker 2024-07-18 01:40:59 +09:00
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)
{
Object::initialize(global_object);
define_old_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_old_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_accessor("length", length_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_function("grow", grow, 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_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));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->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 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())
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>();
if (!table->grow(delta, reference)) {
vm.throw_exception<JS::RangeError>(global_object, "Failed to grow table");
return {};
}
if (!table->grow(delta, reference))
return vm.throw_completion<JS::RangeError>(global_object, "Failed to grow table");
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));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
if (!table)
return JS::js_undefined();
if (table->elements().size() <= index) {
vm.throw_exception<JS::RangeError>(global_object, "Table element index out of range");
return {};
}
if (table->elements().size() <= index)
return vm.throw_completion<JS::RangeError>(global_object, "Table element index out of range");
auto& ref = table->elements()[index];
if (!ref.has_value())
@ -81,28 +73,24 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
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));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->address();
auto* table = WebAssemblyObject::s_abstract_machine.store().get(address);
if (!table)
return JS::js_undefined();
if (table->elements().size() <= index) {
vm.throw_exception<JS::RangeError>(global_object, "Table element index out of range");
return {};
}
if (table->elements().size() <= index)
return vm.throw_completion<JS::RangeError>(global_object, "Table element index out of range");
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())
return Wasm::Value(table->type().element_type(), 0ull);
@ -115,13 +103,11 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
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));
if (!is<WebAssemblyTableObject>(this_object)) {
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
return {};
}
auto* this_object = TRY(vm.this_value(global_object).to_object(global_object));
if (!is<WebAssemblyTableObject>(this_object))
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
auto* table_object = static_cast<WebAssemblyTableObject*>(this_object);
auto address = table_object->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;
private:
JS_DECLARE_OLD_NATIVE_FUNCTION(grow);
JS_DECLARE_OLD_NATIVE_FUNCTION(get);
JS_DECLARE_OLD_NATIVE_FUNCTION(set);
JS_DECLARE_OLD_NATIVE_FUNCTION(length_getter);
JS_DECLARE_NATIVE_FUNCTION(grow);
JS_DECLARE_NATIVE_FUNCTION(get);
JS_DECLARE_NATIVE_FUNCTION(set);
JS_DECLARE_NATIVE_FUNCTION(length_getter);
};
}