1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:47:34 +00:00

LibJS: Replace GlobalObject with VM in Value AOs [Part 4/19]

This is where the fun begins. :^)
This commit is contained in:
Linus Groh 2022-08-21 14:00:56 +01:00
parent f6c4a0f5d0
commit a022e548b8
129 changed files with 1230 additions and 1325 deletions

View file

@ -20,8 +20,9 @@ DateCell::DateCell()
JS::ThrowCompletionOr<String> DateCell::display(Cell& cell, CellTypeMetadata const& metadata) const
{
return propagate_failure(cell, [&]() -> JS::ThrowCompletionOr<String> {
auto& vm = cell.sheet().global_object().vm();
auto timestamp = TRY(js_value(cell, metadata));
auto string = Core::DateTime::from_timestamp(TRY(timestamp.to_i32(cell.sheet().global_object()))).to_string(metadata.format.is_empty() ? "%Y-%m-%d %H:%M:%S"sv : metadata.format.view());
auto string = Core::DateTime::from_timestamp(TRY(timestamp.to_i32(vm))).to_string(metadata.format.is_empty() ? "%Y-%m-%d %H:%M:%S"sv : metadata.format.view());
if (metadata.length >= 0)
return string.substring(0, metadata.length);
@ -32,8 +33,9 @@ JS::ThrowCompletionOr<String> DateCell::display(Cell& cell, CellTypeMetadata con
JS::ThrowCompletionOr<JS::Value> DateCell::js_value(Cell& cell, CellTypeMetadata const&) const
{
auto& vm = cell.sheet().global_object().vm();
auto js_data = cell.js_data();
auto value = TRY(js_data.to_double(cell.sheet().global_object()));
auto value = TRY(js_data.to_double(vm));
return JS::Value(value / 1000); // Turn it to seconds
}

View file

@ -17,11 +17,12 @@ IdentityCell::IdentityCell()
JS::ThrowCompletionOr<String> IdentityCell::display(Cell& cell, CellTypeMetadata const& metadata) const
{
auto& vm = cell.sheet().global_object().vm();
auto data = cell.js_data();
if (!metadata.format.is_empty())
data = TRY(cell.sheet().evaluate(metadata.format, &cell));
return data.to_string(cell.sheet().global_object());
return data.to_string(vm);
}
JS::ThrowCompletionOr<JS::Value> IdentityCell::js_value(Cell& cell, CellTypeMetadata const&) const

View file

@ -20,12 +20,13 @@ NumericCell::NumericCell()
JS::ThrowCompletionOr<String> NumericCell::display(Cell& cell, CellTypeMetadata const& metadata) const
{
return propagate_failure(cell, [&]() -> JS::ThrowCompletionOr<String> {
auto& vm = cell.sheet().global_object().vm();
auto value = TRY(js_value(cell, metadata));
String string;
if (metadata.format.is_empty())
string = TRY(value.to_string(cell.sheet().global_object()));
string = TRY(value.to_string(vm));
else
string = format_double(metadata.format.characters(), TRY(value.to_double(cell.sheet().global_object())));
string = format_double(metadata.format.characters(), TRY(value.to_double(vm)));
if (metadata.length >= 0)
return string.substring(0, min(string.length(), metadata.length));
@ -37,7 +38,8 @@ JS::ThrowCompletionOr<String> NumericCell::display(Cell& cell, CellTypeMetadata
JS::ThrowCompletionOr<JS::Value> NumericCell::js_value(Cell& cell, CellTypeMetadata const&) const
{
return propagate_failure(cell, [&]() {
return cell.js_data().to_number(cell.sheet().global_object());
auto& vm = cell.sheet().global_object().vm();
return cell.js_data().to_number(vm);
});
}

View file

@ -17,7 +17,8 @@ StringCell::StringCell()
JS::ThrowCompletionOr<String> StringCell::display(Cell& cell, CellTypeMetadata const& metadata) const
{
auto string = TRY(cell.js_data().to_string(cell.sheet().global_object()));
auto& vm = cell.sheet().global_object().vm();
auto string = TRY(cell.js_data().to_string(vm));
if (metadata.length >= 0)
return string.substring(0, metadata.length);

View file

@ -171,7 +171,7 @@ void SheetGlobalObject::visit_edges(Visitor& visitor)
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_name)
{
auto* this_object = TRY(vm.this_value().to_object(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -182,7 +182,7 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_name)
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents)
{
auto* this_object = TRY(vm.this_value().to_object(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -211,7 +211,7 @@ 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(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -242,7 +242,7 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
{
auto& realm = *global_object.associated_realm();
auto* this_object = TRY(vm.this_value().to_object(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -272,7 +272,7 @@ 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()");
auto* this_object = TRY(vm.this_value().to_object(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -302,7 +302,7 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_index)
auto& column_name_str = column_name.as_string().string();
auto* this_object = TRY(vm.this_value().to_object(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -327,10 +327,10 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_arithmetic)
auto& column_name_str = column_name.as_string().string();
auto offset = TRY(vm.argument(1).to_number(global_object));
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(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -354,7 +354,7 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_column_bound)
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "String");
auto& column_name_str = column_name.as_string().string();
auto* this_object = TRY(vm.this_value().to_object(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<SheetGlobalObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject");
@ -396,7 +396,7 @@ 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()");
auto* this_object = TRY(vm.this_value().to_object(global_object));
auto* this_object = TRY(vm.this_value().to_object(vm));
if (!is<WorkbookObject>(this_object))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WorkbookObject");
@ -410,7 +410,7 @@ JS_DEFINE_NATIVE_FUNCTION(WorkbookObject::sheet)
return JS::Value(&sheet.global_object());
}
} else {
auto index = TRY(name_value.to_length(global_object));
auto index = TRY(name_value.to_length(vm));
if (index < workbook.sheets().size())
return JS::Value(&workbook.sheets()[index].global_object());
}

View file

@ -24,13 +24,14 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
return String::empty();
Function<String(JS::Value)> to_string_as_exception = [&](JS::Value value) {
auto& vm = cell->sheet().global_object().vm();
StringBuilder builder;
builder.append("Error: "sv);
if (value.is_object()) {
auto& object = value.as_object();
if (is<JS::Error>(object)) {
auto message = object.get_without_side_effects("message");
auto error = message.to_string(cell->sheet().global_object());
auto error = message.to_string(vm);
if (error.is_throw_completion())
builder.append(message.to_string_without_side_effects());
else
@ -38,7 +39,7 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
return builder.to_string();
}
}
auto error_message = value.to_string(cell->sheet().global_object());
auto error_message = value.to_string(vm);
if (error_message.is_throw_completion())
return to_string_as_exception(*error_message.release_error().value());