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

Spreadsheet: No longer use vm.exception() to signal exception state

Instead, use the completions which are returned directly. This means we
no longer have to worry about the global VM state when running code.
This commit is contained in:
davidot 2022-02-07 15:00:40 +01:00 committed by Linus Groh
parent e160f508a8
commit 4ef1e8f226
11 changed files with 98 additions and 97 deletions

View file

@ -96,15 +96,18 @@ void Cell::update_data(Badge<Sheet>)
if (!m_dirty) if (!m_dirty)
return; return;
m_js_exception = {};
if (m_dirty) { if (m_dirty) {
m_dirty = false; m_dirty = false;
if (m_kind == Formula) { if (m_kind == Formula) {
if (!m_evaluated_externally) { if (!m_evaluated_externally) {
auto [value, exception] = m_sheet->evaluate(m_data, this); auto value_or_error = m_sheet->evaluate(m_data, this);
m_evaluated_data = value; if (value_or_error.is_error()) {
m_js_exception = move(exception); m_evaluated_data = JS::js_undefined();
m_thrown_value = *value_or_error.release_error().release_value();
} else {
m_evaluated_data = value_or_error.release_value();
m_thrown_value = {};
}
} }
} }
@ -118,14 +121,14 @@ void Cell::update_data(Badge<Sheet>)
m_evaluated_formats.background_color.clear(); m_evaluated_formats.background_color.clear();
m_evaluated_formats.foreground_color.clear(); m_evaluated_formats.foreground_color.clear();
if (!m_js_exception) { if (m_thrown_value.is_empty()) {
for (auto& fmt : m_conditional_formats) { for (auto& fmt : m_conditional_formats) {
if (!fmt.condition.is_empty()) { if (!fmt.condition.is_empty()) {
auto [value, exception] = m_sheet->evaluate(fmt.condition, this); auto value_or_error = m_sheet->evaluate(fmt.condition, this);
if (exception) { if (value_or_error.is_error()) {
m_js_exception = move(exception); m_thrown_value = *value_or_error.release_error().release_value();
} else { } else {
if (value.to_boolean()) { if (value_or_error.release_value().to_boolean()) {
if (fmt.background_color.has_value()) if (fmt.background_color.has_value())
m_evaluated_formats.background_color = fmt.background_color; m_evaluated_formats.background_color = fmt.background_color;
if (fmt.foreground_color.has_value()) if (fmt.foreground_color.has_value())
@ -185,8 +188,7 @@ void Cell::copy_from(const Cell& other)
m_type_metadata = other.m_type_metadata; m_type_metadata = other.m_type_metadata;
m_conditional_formats = other.m_conditional_formats; m_conditional_formats = other.m_conditional_formats;
m_evaluated_formats = other.m_evaluated_formats; m_evaluated_formats = other.m_evaluated_formats;
if (!other.m_js_exception) m_thrown_value = other.m_thrown_value;
m_js_exception = other.m_js_exception;
} }
} }

View file

@ -49,8 +49,13 @@ struct Cell : public Weakable<Cell> {
bool dirty() const { return m_dirty; } bool dirty() const { return m_dirty; }
void clear_dirty() { m_dirty = false; } void clear_dirty() { m_dirty = false; }
void set_exception(JS::Exception* exc) { m_js_exception = exc; } void set_thrown_value(JS::Value value) { m_thrown_value = value; }
JS::Exception* exception() const { return m_js_exception; } Optional<JS::Value> thrown_value() const
{
if (m_thrown_value.is_empty())
return {};
return m_thrown_value;
}
const String& data() const { return m_data; } const String& data() const { return m_data; }
const JS::Value& evaluated_data() const { return m_evaluated_data; } const JS::Value& evaluated_data() const { return m_evaluated_data; }
@ -103,7 +108,7 @@ private:
bool m_evaluated_externally { false }; bool m_evaluated_externally { false };
String m_data; String m_data;
JS::Value m_evaluated_data; JS::Value m_evaluated_data;
JS::Exception* m_js_exception { nullptr }; JS::Value m_thrown_value;
Kind m_kind { LiteralString }; Kind m_kind { LiteralString };
WeakPtr<Sheet> m_sheet; WeakPtr<Sheet> m_sheet;
Vector<WeakPtr<Cell>> m_referencing_cells; Vector<WeakPtr<Cell>> m_referencing_cells;

View file

@ -34,21 +34,24 @@ void CellSyntaxHighlighter::rehighlight(const Palette& palette)
(u64)-1, (u64)-1,
false); false);
if (m_cell && m_cell->exception()) { if (m_cell && m_cell->thrown_value().has_value()) {
auto& traceback = m_cell->exception()->traceback(); if (auto value = m_cell->thrown_value().value(); value.is_object() && is<JS::Error>(value.as_object())) {
auto& range = traceback.first().source_range; auto& error = static_cast<JS::Error const&>(value.as_object());
GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column } }; auto& traceback = error.traceback();
m_client->spans().prepend( auto& range = traceback.first().source_range;
GUI::TextDocumentSpan { GUI::TextRange text_range { { range.start.line - 1, range.start.column }, { range.end.line - 1, range.end.column } };
text_range, m_client->spans().prepend(
Gfx::TextAttributes { GUI::TextDocumentSpan {
Color::Black, text_range,
Color::Red, Gfx::TextAttributes {
false, Color::Black,
false, Color::Red,
}, false,
(u64)-1, false,
false }); },
(u64)-1,
false });
}
} }
m_client->do_update(); m_client->do_update();
} }

View file

@ -23,19 +23,15 @@ DateCell::~DateCell()
JS::ThrowCompletionOr<String> DateCell::display(Cell& cell, const CellTypeMetadata& metadata) const JS::ThrowCompletionOr<String> DateCell::display(Cell& cell, const CellTypeMetadata& metadata) const
{ {
ScopeGuard propagate_exception { [&cell] { return propagate_failure(cell, [&]() -> JS::ThrowCompletionOr<String> {
if (auto exc = cell.sheet().interpreter().exception()) { auto timestamp = TRY(js_value(cell, metadata));
cell.sheet().interpreter().vm().clear_exception(); 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" : metadata.format.characters());
cell.set_exception(exc);
}
} };
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" : metadata.format.characters());
if (metadata.length >= 0) if (metadata.length >= 0)
return string.substring(0, metadata.length); return string.substring(0, metadata.length);
return string; return string;
});
} }
JS::ThrowCompletionOr<JS::Value> DateCell::js_value(Cell& cell, const CellTypeMetadata&) const JS::ThrowCompletionOr<JS::Value> DateCell::js_value(Cell& cell, const CellTypeMetadata&) const

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include "Numeric.h"
#include "Type.h" #include "Type.h"
namespace Spreadsheet { namespace Spreadsheet {

View file

@ -23,34 +23,26 @@ NumericCell::~NumericCell()
JS::ThrowCompletionOr<String> NumericCell::display(Cell& cell, const CellTypeMetadata& metadata) const JS::ThrowCompletionOr<String> NumericCell::display(Cell& cell, const CellTypeMetadata& metadata) const
{ {
ScopeGuard propagate_exception { [&cell] { return propagate_failure(cell, [&]() -> JS::ThrowCompletionOr<String> {
if (auto exc = cell.sheet().interpreter().exception()) { auto value = TRY(js_value(cell, metadata));
cell.sheet().interpreter().vm().clear_exception(); String string;
cell.set_exception(exc); if (metadata.format.is_empty())
} string = TRY(value.to_string(cell.sheet().global_object()));
} }; else
auto value = TRY(js_value(cell, metadata)); string = format_double(metadata.format.characters(), TRY(value.to_double(cell.sheet().global_object())));
String string;
if (metadata.format.is_empty())
string = TRY(value.to_string(cell.sheet().global_object()));
else
string = format_double(metadata.format.characters(), TRY(value.to_double(cell.sheet().global_object())));
if (metadata.length >= 0) if (metadata.length >= 0)
return string.substring(0, metadata.length); return string.substring(0, metadata.length);
return string; return string;
});
} }
JS::ThrowCompletionOr<JS::Value> NumericCell::js_value(Cell& cell, const CellTypeMetadata&) const JS::ThrowCompletionOr<JS::Value> NumericCell::js_value(Cell& cell, const CellTypeMetadata&) const
{ {
ScopeGuard propagate_exception { [&cell] { return propagate_failure(cell, [&]() {
if (auto exc = cell.sheet().interpreter().exception()) { return cell.js_data().to_number(cell.sheet().global_object());
cell.sheet().interpreter().vm().clear_exception(); });
cell.set_exception(exc);
}
} };
return cell.js_data().to_number(cell.sheet().global_object());
} }
} }

View file

@ -6,10 +6,21 @@
#pragma once #pragma once
#include "../Cell.h"
#include "Type.h" #include "Type.h"
namespace Spreadsheet { namespace Spreadsheet {
template<typename Callable>
static auto propagate_failure(Cell& cell, Callable&& steps)
{
auto result_or_error = steps();
if (result_or_error.is_error())
cell.set_thrown_value(*result_or_error.throw_completion().value());
return result_or_error;
}
class NumericCell : public CellType { class NumericCell : public CellType {
public: public:

View file

@ -164,8 +164,9 @@ void SheetGlobalObject::visit_edges(Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);
for (auto& it : m_sheet.cells()) { for (auto& it : m_sheet.cells()) {
if (it.value->exception()) if (auto opt_thrown_value = it.value->thrown_value(); opt_thrown_value.has_value())
visitor.visit(it.value->exception()); visitor.visit(*opt_thrown_value);
visitor.visit(it.value->evaluated_data()); visitor.visit(it.value->evaluated_data());
} }
} }

View file

@ -60,15 +60,22 @@ Sheet::Sheet(Workbook& workbook)
warnln("SyntaxError: {}", error.to_string()); warnln("SyntaxError: {}", error.to_string());
} }
} else { } else {
(void)interpreter().run(script_or_error.value()); auto result = interpreter().run(script_or_error.value());
if (auto* exception = interpreter().exception()) { if (result.is_error()) {
warnln("Spreadsheet: Failed to run runtime code:"); warnln("Spreadsheet: Failed to run runtime code:");
for (auto& traceback_frame : exception->traceback()) { auto thrown_value = *result.throw_completion().value();
auto& function_name = traceback_frame.function_name; warn("Threw: {}", thrown_value.to_string_without_side_effects());
auto& source_range = traceback_frame.source_range; if (thrown_value.is_object() && is<JS::Error>(thrown_value.as_object())) {
dbgln(" {} at {}:{}:{}", function_name, source_range.filename, source_range.start.line, source_range.start.column); auto& error = static_cast<JS::Error const&>(thrown_value.as_object());
warnln(" with message '{}'", error.get_without_side_effects(interpreter().vm().names.message));
for (auto& traceback_frame : error.traceback()) {
auto& function_name = traceback_frame.function_name;
auto& source_range = traceback_frame.source_range;
dbgln(" {} at {}:{}:{}", function_name, source_range.filename, source_range.start.line, source_range.start.column);
}
} else {
warnln();
} }
interpreter().vm().clear_exception();
} }
} }
} }
@ -159,22 +166,15 @@ void Sheet::update(Cell& cell)
} }
} }
Sheet::ValueAndException Sheet::evaluate(StringView source, Cell* on_behalf_of) JS::ThrowCompletionOr<JS::Value> Sheet::evaluate(StringView source, Cell* on_behalf_of)
{ {
TemporaryChange cell_change { m_current_cell_being_evaluated, on_behalf_of }; TemporaryChange cell_change { m_current_cell_being_evaluated, on_behalf_of };
ScopeGuard clear_exception { [&] { interpreter().vm().clear_exception(); } };
auto script_or_error = JS::Script::parse(source, interpreter().realm()); auto script_or_error = JS::Script::parse(source, interpreter().realm());
// FIXME: Convert parser errors to exceptions to show them to the user. if (script_or_error.is_error())
if (script_or_error.is_error() || interpreter().exception()) return interpreter().vm().throw_completion<JS::SyntaxError>(interpreter().global_object(), script_or_error.error().first().to_string());
return { JS::js_undefined(), interpreter().exception() };
auto result = interpreter().run(script_or_error.value()); return interpreter().run(script_or_error.value());
if (result.is_error()) {
auto* exc = interpreter().exception();
return { JS::js_undefined(), exc };
}
return { result.value(), {} };
} }
Cell* Sheet::at(StringView name) Cell* Sheet::at(StringView name)

View file

@ -107,11 +107,7 @@ public:
} }
} }
struct ValueAndException { JS::ThrowCompletionOr<JS::Value> evaluate(StringView, Cell* = nullptr);
JS::Value value;
JS::Exception* exception { nullptr };
};
ValueAndException evaluate(StringView, Cell* = nullptr);
JS::Interpreter& interpreter() const; JS::Interpreter& interpreter() const;
SheetGlobalObject& global_object() const { return *m_global_object; } SheetGlobalObject& global_object() const { return *m_global_object; }

View file

@ -28,12 +28,6 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
return String::empty(); return String::empty();
Function<String(JS::Value)> to_string_as_exception = [&](JS::Value value) { Function<String(JS::Value)> to_string_as_exception = [&](JS::Value value) {
ScopeGuard clear_exception {
[&] {
cell->sheet().interpreter().vm().clear_exception();
}
};
StringBuilder builder; StringBuilder builder;
builder.append("Error: "sv); builder.append("Error: "sv);
if (value.is_object()) { if (value.is_object()) {
@ -58,8 +52,8 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
}; };
if (cell->kind() == Spreadsheet::Cell::Formula) { if (cell->kind() == Spreadsheet::Cell::Formula) {
if (auto exception = cell->exception()) if (auto opt_throw_value = cell->thrown_value(); opt_throw_value.has_value())
return to_string_as_exception(exception->value()); return to_string_as_exception(*opt_throw_value);
} }
auto display = cell->typed_display(); auto display = cell->typed_display();
@ -86,7 +80,7 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
return {}; return {};
if (cell->kind() == Spreadsheet::Cell::Formula) { if (cell->kind() == Spreadsheet::Cell::Formula) {
if (cell->exception()) if (cell->thrown_value().has_value())
return Color(Color::Red); return Color(Color::Red);
} }