Skip to content

Commit a84f645

Browse files
committed
Added tracebacks in tests
1 parent 6d620a1 commit a84f645

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

p8rs-piccolo/src/thread/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod vm;
55
use gc_arena::{Collect, DynamicRootSet, Mutation};
66
use thiserror::Error;
77
use alloc::vec::Vec;
8+
use core::fmt::{Display, Formatter};
89
use crate::compiler::LineNumber;
910
use crate::meta_ops::{MetaCallError, MetaOperatorError};
1011
use crate::StashedString;
@@ -61,6 +62,20 @@ pub type StashedTraceback = TracebackBase<StashedString>;
6162
pub type Traceback<'gc> = TracebackBase<crate::String<'gc>>;
6263
pub type ExternTraceback = TracebackBase<alloc::string::String>;
6364

65+
impl<S> Display for TracebackBase<S>
66+
where S: Display {
67+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
68+
for entry in self.entries.iter() {
69+
write!(f, "line {}", entry.line_number)?;
70+
if let Some(name) = &entry.name {
71+
write!(f, " in function '{}'", name)?;
72+
}
73+
write!(f, "\n")?;
74+
}
75+
Ok(())
76+
}
77+
}
78+
6479
impl<S> TracebackBase<S>
6580
{
6681
pub fn into_traceback<D>(self) -> TracebackBase<D>

p8rs-tests/src/runner/p8rs.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::Path;
33
use std::rc::Rc;
44
use std::thread;
55
use std::time::{Duration, Instant};
6-
use p8rs::vm;
6+
use p8rs::{vm, ExternError};
77
use p8rs_types::p8scii;
88

99
use crate::runner::{Log, RunResult, TIMEOUT_MS};
@@ -55,7 +55,15 @@ pub fn run(source: &[u8], log_file: impl AsRef<Path>) -> RunResult {
5555

5656
if let Some(err) = result.as_ref().err() {
5757
println!("-- p8rs error --");
58-
println!("{err}");
58+
match err {
59+
ExternError::Lua(err) => println!("{err}"),
60+
ExternError::Runtime(runtime_err) => {
61+
println!("{err}");
62+
if let Some(Ok(traceback)) = runtime_err.1.as_ref().map(|tb| vm.get_traceback(tb)) {
63+
println!("traceback:\n{traceback}");
64+
}
65+
}
66+
}
5967
}
6068

6169
if timeout {

p8rs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
extern crate alloc;
1010
extern crate core;
1111

12+
pub use p8rs_piccolo::error::ExternError;
13+
1214
pub mod rle;
1315
pub mod vm;
1416
pub mod colors;

p8rs/src/vm/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub use runtime::Runtime;
2121
pub use callbacks::Callbacks;
2222
use api::install_pico8_apis;
2323
use cart::CartLoadError;
24+
use p8rs_piccolo::stash::Fetchable;
25+
use p8rs_piccolo::thread::{ExternTraceback, StashedTraceback, Traceback};
2426
use traceback::write_traceback_entries;
2527

2628
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
@@ -187,6 +189,10 @@ impl<A: Allocator + 'static> P8rs<A> {
187189
pub fn runtime(&mut self) -> &mut Runtime<A> {
188190
&mut self.runtime
189191
}
192+
193+
pub fn get_traceback(&mut self, stashed: &StashedTraceback) -> Result<ExternTraceback, ExternError> {
194+
self.lua.try_enter(|ctx| Ok(ctx.fetch(stashed).into_traceback()))
195+
}
190196
}
191197

192198

0 commit comments

Comments
 (0)