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

LibJS: Port Value::to_object() to NonnullGCPtr

This commit is contained in:
Linus Groh 2023-04-13 15:26:41 +02:00 committed by Andreas Kling
parent e79f5b6e85
commit f345f72b55
29 changed files with 264 additions and 263 deletions

View file

@ -174,23 +174,23 @@ void SheetGlobalObject::visit_edges(Visitor& visitor)
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_name)
{
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
return JS::PrimitiveString::create(vm, sheet_object->m_sheet.name());
auto& sheet_object = static_cast<SheetGlobalObject&>(*this_object);
return JS::PrimitiveString::create(vm, sheet_object.m_sheet.name());
}
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents)
{
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
auto& sheet_object = static_cast<SheetGlobalObject&>(*this_object);
if (vm.argument_count() != 1)
return vm.throw_completion<JS::TypeError>("Expected exactly one argument to get_real_cell_contents()"sv);
@ -198,11 +198,11 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents)
auto name_value = vm.argument(0);
if (!name_value.is_string())
return vm.throw_completion<JS::TypeError>("Expected a String argument to get_real_cell_contents()"sv);
auto position = sheet_object->m_sheet.parse_cell_name(TRY(name_value.as_string().deprecated_string()));
auto position = sheet_object.m_sheet.parse_cell_name(TRY(name_value.as_string().deprecated_string()));
if (!position.has_value())
return vm.throw_completion<JS::TypeError>("Invalid cell name"sv);
auto const* cell = sheet_object->m_sheet.at(position.value());
auto const* cell = sheet_object.m_sheet.at(position.value());
if (!cell)
return JS::js_undefined();
@ -214,12 +214,12 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents)
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::set_real_cell_contents)
{
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
auto& sheet_object = static_cast<SheetGlobalObject&>(*this_object);
if (vm.argument_count() != 2)
return vm.throw_completion<JS::TypeError>("Expected exactly two arguments to set_real_cell_contents()"sv);
@ -227,7 +227,7 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::set_real_cell_contents)
auto name_value = vm.argument(0);
if (!name_value.is_string())
return vm.throw_completion<JS::TypeError>("Expected the first argument of set_real_cell_contents() to be a String"sv);
auto position = sheet_object->m_sheet.parse_cell_name(TRY(name_value.as_string().deprecated_string()));
auto position = sheet_object.m_sheet.parse_cell_name(TRY(name_value.as_string().deprecated_string()));
if (!position.has_value())
return vm.throw_completion<JS::TypeError>("Invalid cell name"sv);
@ -235,7 +235,7 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::set_real_cell_contents)
if (!new_contents_value.is_string())
return vm.throw_completion<JS::TypeError>("Expected the second argument of set_real_cell_contents() to be a String"sv);
auto& cell = sheet_object->m_sheet.ensure(position.value());
auto& cell = sheet_object.m_sheet.ensure(position.value());
auto new_contents = TRY(new_contents_value.as_string().deprecated_string());
cell.set_data(new_contents);
return JS::js_null();
@ -245,24 +245,24 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
{
auto& realm = *vm.current_realm();
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
auto& sheet_object = static_cast<SheetGlobalObject&>(*this_object);
if (vm.argument_count() != 1)
return vm.throw_completion<JS::TypeError>("Expected exactly one argument to parse_cell_name()"sv);
auto name_value = vm.argument(0);
if (!name_value.is_string())
return vm.throw_completion<JS::TypeError>("Expected a String argument to parse_cell_name()"sv);
auto position = sheet_object->m_sheet.parse_cell_name(TRY(name_value.as_string().deprecated_string()));
auto position = sheet_object.m_sheet.parse_cell_name(TRY(name_value.as_string().deprecated_string()));
if (!position.has_value())
return JS::js_undefined();
auto object = JS::Object::create(realm, realm.intrinsics().object_prototype());
object->define_direct_property("column", JS::PrimitiveString::create(vm, sheet_object->m_sheet.column(position.value().column)), JS::default_attributes);
object->define_direct_property("column", JS::PrimitiveString::create(vm, sheet_object.m_sheet.column(position.value().column)), JS::default_attributes);
object->define_direct_property("row", JS::Value((unsigned)position.value().row), JS::default_attributes);
return object;
@ -275,20 +275,20 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::current_cell_position)
if (vm.argument_count() != 0)
return vm.throw_completion<JS::TypeError>("Expected no arguments to current_cell_position()"sv);
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
auto* current_cell = sheet_object->m_sheet.current_evaluated_cell();
auto& sheet_object = static_cast<SheetGlobalObject&>(*this_object);
auto* current_cell = sheet_object.m_sheet.current_evaluated_cell();
if (!current_cell)
return JS::js_null();
auto position = current_cell->position();
auto object = JS::Object::create(realm, realm.intrinsics().object_prototype());
object->define_direct_property("column", JS::PrimitiveString::create(vm, sheet_object->m_sheet.column(position.column)), JS::default_attributes);
object->define_direct_property("column", JS::PrimitiveString::create(vm, sheet_object.m_sheet.column(position.column)), JS::default_attributes);
object->define_direct_property("row", JS::Value((unsigned)position.row), JS::default_attributes);
return object;
@ -305,13 +305,13 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_index)
auto column_name_str = TRY(column_name.as_string().deprecated_string());
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
auto& sheet = sheet_object->m_sheet;
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())
return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' is not a valid column", column_name_str)));
@ -333,13 +333,13 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_arithmetic)
auto offset = TRY(vm.argument(1).to_number(vm));
auto offset_number = static_cast<i32>(offset.as_double());
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
auto& sheet = sheet_object->m_sheet;
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())
return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' is not a valid column", column_name_str)));
@ -357,13 +357,13 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_column_bound)
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "String");
auto column_name_str = TRY(column_name.as_string().deprecated_string());
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
if (!is<SheetGlobalObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
auto sheet_object = static_cast<SheetGlobalObject*>(this_object);
auto& sheet = sheet_object->m_sheet;
auto& sheet_object = static_cast<SheetGlobalObject&>(*this_object);
auto& sheet = sheet_object.m_sheet;
auto maybe_column_index = sheet.column_index(column_name_str);
if (!maybe_column_index.has_value())
return vm.throw_completion<JS::TypeError>(TRY_OR_THROW_OOM(vm, String::formatted("'{}' is not a valid column", column_name_str)));
@ -401,12 +401,13 @@ JS_DEFINE_NATIVE_FUNCTION(WorkbookObject::sheet)
if (!name_value.is_string() && !name_value.is_number())
return vm.throw_completion<JS::TypeError>("Expected a String or Number argument to sheet()"sv);
auto* this_object = TRY(vm.this_value().to_object(vm));
auto this_object = TRY(vm.this_value().to_object(vm));
if (!is<WorkbookObject>(this_object))
if (!is<WorkbookObject>(*this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WorkbookObject");
auto& workbook = static_cast<WorkbookObject*>(this_object)->m_workbook;
auto& workbook_object = static_cast<WorkbookObject&>(*this_object);
auto& workbook = workbook_object.m_workbook;
if (name_value.is_string()) {
auto name = TRY(name_value.as_string().deprecated_string());