Spreadsheet: Convert JSIntegration to ThrowCompletionOr
This commit is contained in:
parent
7653be6062
commit
1939c72ecc
Notes:
sideshowbarker
2024-07-18 01:41:52 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/1939c72ecc4 Pull-request: https://github.com/SerenityOS/serenity/pull/10727 Reviewed-by: https://github.com/linusg ✅
2 changed files with 80 additions and 131 deletions
Userland/Applications/Spreadsheet
|
@ -151,12 +151,12 @@ void SheetGlobalObject::initialize_global_object()
|
|||
{
|
||||
Base::initialize_global_object();
|
||||
u8 attr = JS::Attribute::Configurable | JS::Attribute::Writable | JS::Attribute::Enumerable;
|
||||
define_old_native_function("get_real_cell_contents", get_real_cell_contents, 1, attr);
|
||||
define_old_native_function("set_real_cell_contents", set_real_cell_contents, 2, attr);
|
||||
define_old_native_function("parse_cell_name", parse_cell_name, 1, attr);
|
||||
define_old_native_function("current_cell_position", current_cell_position, 0, attr);
|
||||
define_old_native_function("column_arithmetic", column_arithmetic, 2, attr);
|
||||
define_old_native_function("column_index", column_index, 1, attr);
|
||||
define_native_function("get_real_cell_contents", get_real_cell_contents, 1, attr);
|
||||
define_native_function("set_real_cell_contents", set_real_cell_contents, 2, attr);
|
||||
define_native_function("parse_cell_name", parse_cell_name, 1, attr);
|
||||
define_native_function("current_cell_position", current_cell_position, 0, attr);
|
||||
define_native_function("column_arithmetic", column_arithmetic, 2, attr);
|
||||
define_native_function("column_index", column_index, 1, attr);
|
||||
}
|
||||
|
||||
void SheetGlobalObject::visit_edges(Visitor& visitor)
|
||||
|
@ -169,32 +169,24 @@ void SheetGlobalObject::visit_edges(Visitor& visitor)
|
|||
}
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents)
|
||||
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents)
|
||||
{
|
||||
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<SheetGlobalObject>(this_object)) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
return {};
|
||||
}
|
||||
if (!is<SheetGlobalObject>(this_object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
|
||||
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
|
||||
|
||||
if (vm.argument_count() != 1) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected exactly one argument to get_real_cell_contents()");
|
||||
return {};
|
||||
}
|
||||
if (vm.argument_count() != 1)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected exactly one argument to get_real_cell_contents()");
|
||||
|
||||
auto name_value = vm.argument(0);
|
||||
if (!name_value.is_string()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected a String argument to get_real_cell_contents()");
|
||||
return {};
|
||||
}
|
||||
if (!name_value.is_string())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected a String argument to get_real_cell_contents()");
|
||||
auto position = sheet_object->m_sheet.parse_cell_name(name_value.as_string().string());
|
||||
if (!position.has_value()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Invalid cell name");
|
||||
return {};
|
||||
}
|
||||
if (!position.has_value())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Invalid cell name");
|
||||
|
||||
const auto* cell = sheet_object->m_sheet.at(position.value());
|
||||
if (!cell)
|
||||
|
@ -206,38 +198,28 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents)
|
|||
return JS::js_string(vm, cell->data());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::set_real_cell_contents)
|
||||
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::set_real_cell_contents)
|
||||
{
|
||||
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<SheetGlobalObject>(this_object)) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
return {};
|
||||
}
|
||||
if (!is<SheetGlobalObject>(this_object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
|
||||
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
|
||||
|
||||
if (vm.argument_count() != 2) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected exactly two arguments to set_real_cell_contents()");
|
||||
return {};
|
||||
}
|
||||
if (vm.argument_count() != 2)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected exactly two arguments to set_real_cell_contents()");
|
||||
|
||||
auto name_value = vm.argument(0);
|
||||
if (!name_value.is_string()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected the first argument of set_real_cell_contents() to be a String");
|
||||
return {};
|
||||
}
|
||||
if (!name_value.is_string())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected the first argument of set_real_cell_contents() to be a String");
|
||||
auto position = sheet_object->m_sheet.parse_cell_name(name_value.as_string().string());
|
||||
if (!position.has_value()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Invalid cell name");
|
||||
return {};
|
||||
}
|
||||
if (!position.has_value())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Invalid cell name");
|
||||
|
||||
auto new_contents_value = vm.argument(1);
|
||||
if (!new_contents_value.is_string()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected the second argument of set_real_cell_contents() to be a String");
|
||||
return {};
|
||||
}
|
||||
if (!new_contents_value.is_string())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected the second argument of set_real_cell_contents() to be a String");
|
||||
|
||||
auto& cell = sheet_object->m_sheet.ensure(position.value());
|
||||
auto& new_contents = new_contents_value.as_string().string();
|
||||
|
@ -245,26 +227,20 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::set_real_cell_contents)
|
|||
return JS::js_null();
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
|
||||
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
|
||||
{
|
||||
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<SheetGlobalObject>(this_object)) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
return {};
|
||||
}
|
||||
if (!is<SheetGlobalObject>(this_object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
|
||||
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
|
||||
|
||||
if (vm.argument_count() != 1) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected exactly one argument to parse_cell_name()");
|
||||
return {};
|
||||
}
|
||||
if (vm.argument_count() != 1)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected exactly one argument to parse_cell_name()");
|
||||
auto name_value = vm.argument(0);
|
||||
if (!name_value.is_string()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected a String argument to parse_cell_name()");
|
||||
return {};
|
||||
}
|
||||
if (!name_value.is_string())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected a String argument to parse_cell_name()");
|
||||
auto position = sheet_object->m_sheet.parse_cell_name(name_value.as_string().string());
|
||||
if (!position.has_value())
|
||||
return JS::js_undefined();
|
||||
|
@ -276,19 +252,15 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
|
|||
return object;
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::current_cell_position)
|
||||
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::current_cell_position)
|
||||
{
|
||||
if (vm.argument_count() != 0) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected no arguments to current_cell_position()");
|
||||
return {};
|
||||
}
|
||||
if (vm.argument_count() != 0)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected no arguments to current_cell_position()");
|
||||
|
||||
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<SheetGlobalObject>(this_object)) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
return {};
|
||||
}
|
||||
if (!is<SheetGlobalObject>(this_object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
|
||||
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
|
||||
auto* current_cell = sheet_object->m_sheet.current_evaluated_cell();
|
||||
|
@ -304,72 +276,55 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::current_cell_position)
|
|||
return object;
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::column_index)
|
||||
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_index)
|
||||
{
|
||||
if (vm.argument_count() != 1) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected exactly one argument to column_index()");
|
||||
return {};
|
||||
}
|
||||
if (vm.argument_count() != 1)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected exactly one argument to column_index()");
|
||||
|
||||
auto column_name = vm.argument(0);
|
||||
if (!column_name.is_string()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "String");
|
||||
return {};
|
||||
}
|
||||
if (!column_name.is_string())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "String");
|
||||
|
||||
auto& column_name_str = column_name.as_string().string();
|
||||
|
||||
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<SheetGlobalObject>(this_object)) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
return {};
|
||||
}
|
||||
if (!is<SheetGlobalObject>(this_object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
|
||||
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
|
||||
auto& sheet = sheet_object->m_sheet;
|
||||
auto column_index = sheet.column_index(column_name_str);
|
||||
if (!column_index.has_value()) {
|
||||
vm.throw_exception(global_object, JS::TypeError::create(global_object, String::formatted("'{}' is not a valid column", column_name_str)));
|
||||
return {};
|
||||
}
|
||||
if (!column_index.has_value())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("'{}' is not a valid column", column_name_str));
|
||||
|
||||
return JS::Value((i32)column_index.value());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(SheetGlobalObject::column_arithmetic)
|
||||
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_arithmetic)
|
||||
{
|
||||
if (vm.argument_count() != 2) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected exactly two arguments to column_arithmetic()");
|
||||
return {};
|
||||
}
|
||||
if (vm.argument_count() != 2)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected exactly two arguments to column_arithmetic()");
|
||||
|
||||
auto column_name = vm.argument(0);
|
||||
if (!column_name.is_string()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "String");
|
||||
return {};
|
||||
}
|
||||
if (!column_name.is_string())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "String");
|
||||
|
||||
auto& column_name_str = column_name.as_string().string();
|
||||
|
||||
auto offset = TRY_OR_DISCARD(vm.argument(1).to_number(global_object));
|
||||
|
||||
auto offset = TRY(vm.argument(1).to_number(global_object));
|
||||
auto offset_number = offset.as_i32();
|
||||
|
||||
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<SheetGlobalObject>(this_object)) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
return {};
|
||||
}
|
||||
if (!is<SheetGlobalObject>(this_object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
|
||||
|
||||
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
|
||||
auto& sheet = sheet_object->m_sheet;
|
||||
auto new_column = sheet.column_arithmetic(column_name_str, offset_number);
|
||||
if (!new_column.has_value()) {
|
||||
vm.throw_exception(global_object, JS::TypeError::create(global_object, String::formatted("'{}' is not a valid column", column_name_str)));
|
||||
return {};
|
||||
}
|
||||
if (!new_column.has_value())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, String::formatted("'{}' is not a valid column", column_name_str));
|
||||
|
||||
return JS::js_string(vm, new_column.release_value());
|
||||
}
|
||||
|
@ -387,7 +342,7 @@ WorkbookObject::~WorkbookObject()
|
|||
void WorkbookObject::initialize(JS::GlobalObject& global_object)
|
||||
{
|
||||
Object::initialize(global_object);
|
||||
define_old_native_function("sheet", sheet, 1, JS::default_attributes);
|
||||
define_native_function("sheet", sheet, 1, JS::default_attributes);
|
||||
}
|
||||
|
||||
void WorkbookObject::visit_edges(Visitor& visitor)
|
||||
|
@ -397,24 +352,18 @@ void WorkbookObject::visit_edges(Visitor& visitor)
|
|||
visitor.visit(&sheet.global_object());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(WorkbookObject::sheet)
|
||||
JS_DEFINE_NATIVE_FUNCTION(WorkbookObject::sheet)
|
||||
{
|
||||
if (vm.argument_count() != 1) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected exactly one argument to sheet()");
|
||||
return {};
|
||||
}
|
||||
if (vm.argument_count() != 1)
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected exactly one argument to sheet()");
|
||||
auto name_value = vm.argument(0);
|
||||
if (!name_value.is_string() && !name_value.is_number()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, "Expected a String or Number argument to sheet()");
|
||||
return {};
|
||||
}
|
||||
if (!name_value.is_string() && !name_value.is_number())
|
||||
return vm.throw_completion<JS::TypeError>(global_object, "Expected a String or Number argument to sheet()");
|
||||
|
||||
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<WorkbookObject>(this_object)) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WorkbookObject");
|
||||
return {};
|
||||
}
|
||||
if (!is<WorkbookObject>(this_object))
|
||||
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WorkbookObject");
|
||||
|
||||
auto& workbook = static_cast<WorkbookObject*>(this_object)->m_workbook;
|
||||
|
||||
|
@ -425,7 +374,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WorkbookObject::sheet)
|
|||
return JS::Value(&sheet.global_object());
|
||||
}
|
||||
} else {
|
||||
auto index = TRY_OR_DISCARD(name_value.to_length(global_object));
|
||||
auto index = TRY(name_value.to_length(global_object));
|
||||
if (index < workbook.sheets().size())
|
||||
return JS::Value(&workbook.sheets()[index].global_object());
|
||||
}
|
||||
|
|
|
@ -32,12 +32,12 @@ public:
|
|||
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
|
||||
virtual void initialize_global_object() override;
|
||||
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(get_real_cell_contents);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(set_real_cell_contents);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(parse_cell_name);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(current_cell_position);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(column_index);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(column_arithmetic);
|
||||
JS_DECLARE_NATIVE_FUNCTION(get_real_cell_contents);
|
||||
JS_DECLARE_NATIVE_FUNCTION(set_real_cell_contents);
|
||||
JS_DECLARE_NATIVE_FUNCTION(parse_cell_name);
|
||||
JS_DECLARE_NATIVE_FUNCTION(current_cell_position);
|
||||
JS_DECLARE_NATIVE_FUNCTION(column_index);
|
||||
JS_DECLARE_NATIVE_FUNCTION(column_arithmetic);
|
||||
|
||||
private:
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
|
@ -54,7 +54,7 @@ public:
|
|||
|
||||
virtual void initialize(JS::GlobalObject&) override;
|
||||
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(sheet);
|
||||
JS_DECLARE_NATIVE_FUNCTION(sheet);
|
||||
|
||||
private:
|
||||
virtual void visit_edges(Visitor&) override;
|
||||
|
|
Loading…
Add table
Reference in a new issue