stack traces in the brush editor

after 35 thousand years it's finally here
good erro message
This commit is contained in:
りき萌 2025-06-25 20:51:34 +02:00
parent c1612b2a94
commit e49885c83a
11 changed files with 710 additions and 150 deletions

View file

@ -1,10 +1,9 @@
use core::{
fmt::{self, Display},
mem::transmute,
};
use core::fmt::{self, Display};
use alloc::{borrow::ToOwned, string::String, vec::Vec};
use crate::source::Span;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Opcode {
@ -56,6 +55,14 @@ pub const CAPTURE_CAPTURE: u8 = 1;
#[derive(Debug, Clone)]
pub struct Chunk {
pub bytecode: Vec<u8>,
pub span_info: Vec<SpanRun>,
pub current_span: Span,
}
#[derive(Debug, Clone)]
pub struct SpanRun {
pub span: Span,
pub len: u16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -66,6 +73,8 @@ impl Chunk {
if capacity <= (1 << 16) {
Ok(Chunk {
bytecode: Vec::with_capacity(capacity),
span_info: Vec::new(),
current_span: Span::new(0, 0),
})
} else {
Err(ChunkSizeError)
@ -76,6 +85,16 @@ impl Chunk {
Offset(self.bytecode.len() as u16)
}
fn push_span_info(&mut self, span: Span, len: u16) {
if let Some(info) = self.span_info.last_mut() {
if info.span == span {
info.len += len;
return;
}
}
self.span_info.push(SpanRun { span, len });
}
pub fn emit_bytes(&mut self, bytes: &[u8]) -> Result<Offset, EmitError> {
if self.bytecode.len() + bytes.len() > self.bytecode.capacity() {
return Err(EmitError);
@ -83,6 +102,7 @@ impl Chunk {
let offset = Offset(self.bytecode.len() as u16);
self.bytecode.extend_from_slice(bytes);
self.push_span_info(self.current_span, bytes.len() as u16);
Ok(offset)
}
@ -122,40 +142,15 @@ impl Chunk {
self.patch_u16(offset, x.0);
}
// NOTE: I'm aware these aren't the fastest implementations since they validate quite a lot
// during runtime, but this is just an MVP. It doesn't have to be blazingly fast.
pub fn read_u8(&self, pc: &mut usize) -> Result<u8, ReadError> {
let x = self.bytecode.get(*pc).copied();
*pc += 1;
x.ok_or(ReadError)
}
pub fn read_u16(&self, pc: &mut usize) -> Result<u16, ReadError> {
let xs = &self.bytecode[*pc..*pc + 2];
*pc += 2;
Ok(u16::from_le_bytes(xs.try_into().map_err(|_| ReadError)?))
}
pub fn read_u32(&self, pc: &mut usize) -> Result<u32, ReadError> {
let xs = &self.bytecode[*pc..*pc + 4];
*pc += 4;
Ok(u32::from_le_bytes(xs.try_into().map_err(|_| ReadError)?))
}
pub fn read_f32(&self, pc: &mut usize) -> Result<f32, ReadError> {
let xs = &self.bytecode[*pc..*pc + 4];
*pc += 4;
Ok(f32::from_le_bytes(xs.try_into().map_err(|_| ReadError)?))
}
pub fn read_opcode(&self, pc: &mut usize) -> Result<Opcode, ReadError> {
let x = self.read_u8(pc)?;
if x <= Opcode::Return as u8 {
Ok(unsafe { transmute::<u8, Opcode>(x) })
} else {
Err(ReadError)
pub fn find_span(&self, pc: u16) -> Option<&Span> {
let mut cur = 0;
for info in &self.span_info {
if pc >= cur && pc < cur + info.len {
return Some(&info.span);
}
cur += info.len;
}
None
}
}
@ -183,15 +178,6 @@ impl Display for EmitError {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReadError;
impl Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid bytecode: out of bounds read or invalid opcode")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct DefId(u16);