Skip to content

Commit

Permalink
Merge pull request #642 from rben01/box-runtime-error
Browse files Browse the repository at this point in the history
Boxed some `RuntimeError`s
  • Loading branch information
sharkdp authored Nov 3, 2024
2 parents 547451f + 2b59d4f commit f4cc8b2
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
7 changes: 2 additions & 5 deletions numbat/examples/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,13 @@ fn inspect_functions_in_module(ctx: &Context, prelude_ctx: &Context, module: Str
if let Ok((statements, results)) =
example_ctx.interpret(&example_code, CodeSource::Internal)
{
let code = extra_import + &example_code;

//Format the example input
let example_input = format!("{}", code);
let example_input = extra_import + &example_code;

//Encode the example url
let example_url = format!(
"https://numbat.dev/?q={}",
percent_encoding::utf8_percent_encode(
&code,
&example_input,
percent_encoding::NON_ALPHANUMERIC
)
);
Expand Down
4 changes: 2 additions & 2 deletions numbat/src/ffi/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ fn calendar_add(
let n = quantity_arg!(args).unsafe_value().to_f64();

if n.fract() != 0.0 {
return Err(RuntimeError::UserError(format!(
return Err(Box::new(RuntimeError::UserError(format!(
"calendar_add: requires an integer number of {unit_name}s"
)));
))));
}

let n_i64 = n.to_i64().ok_or_else(|| {
Expand Down
4 changes: 2 additions & 2 deletions numbat/src/ffi/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ pub(crate) fn functions() -> &'static HashMap<String, ForeignFunction> {
}

fn error(mut args: Args) -> Result<Value> {
Err(RuntimeError::UserError(
Err(Box::new(RuntimeError::UserError(
arg!(args).unsafe_as_string().to_string(),
))
)))
}

fn value_of(mut args: Args) -> Result<Value> {
Expand Down
2 changes: 1 addition & 1 deletion numbat/src/ffi/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn head(mut args: Args) -> Result<Value> {
if let Some(first) = list.head() {
Ok(first)
} else {
Err(RuntimeError::EmptyList)
Err(Box::new(RuntimeError::EmptyList))
}
}

Expand Down
4 changes: 3 additions & 1 deletion numbat/src/ffi/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ pub fn _get_chemical_element_data_raw(mut args: Args) -> Result<Value> {
],
))
} else {
Err(RuntimeError::ChemicalElementNotFound(pattern.to_string()))
Err(Box::new(RuntimeError::ChemicalElementNotFound(
pattern.to_string(),
)))
}
}
2 changes: 1 addition & 1 deletion numbat/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ControlFlow = std::ops::ControlFlow<RuntimeError>;

pub(crate) type ArityRange = std::ops::RangeInclusive<usize>;

type Result<T> = std::result::Result<T, RuntimeError>;
type Result<T> = std::result::Result<T, Box<RuntimeError>>;

pub(crate) type Args = VecDeque<Value>;

Expand Down
8 changes: 4 additions & 4 deletions numbat/src/ffi/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ pub fn show(args: Args) -> Result<Value> {
// Dynamic dispatch hack since we don't have bounded polymorphism.
// And no real support for generics in the FFI.
let Value::StructInstance(info, _) = args.front().unwrap() else {
return Err(RuntimeError::UserError(
return Err(Box::new(RuntimeError::UserError(
"Unsupported argument to 'show'.".into(),
));
)));
};

let plot = if info.name == "LinePlot" {
line_plot(args)
} else if info.name == "BarChart" {
bar_chart(args)
} else {
return Err(RuntimeError::UserError(format!(
return Err(Box::new(RuntimeError::UserError(format!(
"Unsupported plot type: {}",
info.name
)));
))));
};

return_string!(owned = show_plot(plot))
Expand Down
4 changes: 2 additions & 2 deletions numbat/src/ffi/procedures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ fn assert_eq(_: &mut ExecutionContext, mut args: Args, arg_spans: Vec<Span>) ->
let rhs = arg!(args);

let error = ControlFlow::Break(RuntimeError::AssertEq2Failed(AssertEq2Error {
span_lhs: span_lhs,
span_lhs,
lhs: lhs.clone(),
span_rhs: span_rhs,
span_rhs,
rhs: rhs.clone(),
}));

Expand Down
2 changes: 1 addition & 1 deletion numbat/src/ffi/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn ord(mut args: Args) -> Result<Value> {
let input = string_arg!(args);

if input.is_empty() {
return Err(RuntimeError::EmptyList);
return Err(Box::new(RuntimeError::EmptyList));
}

let output = input.chars().next().unwrap() as u32;
Expand Down
1 change: 1 addition & 0 deletions numbat/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use thiserror::Error;
pub use crate::value::Value;

#[derive(Debug, Clone, Error, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum RuntimeError {
#[error("Division by zero")]
DivisionByZero,
Expand Down
6 changes: 3 additions & 3 deletions numbat/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl<T> NumbatList<T> {

/// Return the tail of the list without the first element.
/// Return an error if the list is empty.
pub fn tail(&mut self) -> Result<(), RuntimeError> {
pub fn tail(&mut self) -> Result<(), Box<RuntimeError>> {
if self.is_empty() {
return Err(RuntimeError::EmptyList);
return Err(Box::new(RuntimeError::EmptyList));
}
if let Some(view) = &mut self.view {
view.0 += 1;
Expand Down Expand Up @@ -239,7 +239,7 @@ mod test {
assert!(list.is_empty());
assert_eq!(alloc, Arc::as_ptr(&list.alloc));

assert_eq!(list.tail(), Err(RuntimeError::EmptyList));
assert_eq!(list.tail(), Err(Box::new(RuntimeError::EmptyList)));
}

#[test]
Expand Down

0 comments on commit f4cc8b2

Please sign in to comment.