implement brush cost gauges

they're a little ugly at the moment, and can be a little useless for most simple brushes, but whatever we'll make them better later
This commit is contained in:
りき萌 2024-10-25 21:38:18 +02:00
parent 43e6951f7d
commit 913d65b0a8
19 changed files with 378 additions and 65 deletions

View file

@ -279,11 +279,24 @@ extern "C" fn haku_status_string(code: StatusCode) -> *const i8 {
.as_ptr()
}
#[derive(Debug)]
struct CompileStats {
ast_size: usize,
}
#[derive(Debug)]
struct RunnableBrush {
chunk_id: ChunkId,
closure_spec: ClosureSpec,
compile_stats: CompileStats,
}
#[derive(Debug, Default)]
enum BrushState {
#[default]
Default,
Ready(ChunkId, ClosureSpec),
Ready(RunnableBrush),
}
#[derive(Debug, Default)]
@ -431,7 +444,13 @@ unsafe extern "C" fn haku_compile_brush(
Ok(chunk_id) => chunk_id,
Err(_) => return StatusCode::TooManyChunks,
};
brush.state = BrushState::Ready(chunk_id, closure_spec);
brush.state = BrushState::Ready(RunnableBrush {
chunk_id,
closure_spec,
compile_stats: CompileStats {
ast_size: ast.len(),
},
});
info!("brush compiled into {chunk_id:?}");
@ -470,7 +489,7 @@ unsafe extern "C" fn haku_begin_brush(instance: *mut Instance, brush: *const Bru
let instance = &mut *instance;
let brush = &*brush;
let BrushState::Ready(chunk_id, closure_spec) = brush.state else {
let BrushState::Ready(runnable) = &brush.state else {
panic!("brush is not compiled and ready to be used");
};
@ -479,10 +498,10 @@ unsafe extern "C" fn haku_begin_brush(instance: *mut Instance, brush: *const Bru
instance.reset_exception();
instance.trampoline = None;
let Ok(closure_id) = instance
.vm
.create_ref(Ref::Closure(Closure::chunk(chunk_id, closure_spec)))
else {
let Ok(closure_id) = instance.vm.create_ref(Ref::Closure(Closure::chunk(
runnable.chunk_id,
runnable.closure_spec,
))) else {
return StatusCode::OutOfRefSlots;
};
@ -574,3 +593,26 @@ unsafe extern "C" fn haku_cont_dotter(
)
})
}
#[unsafe(no_mangle)]
unsafe extern "C" fn haku_stat_ast_size(brush: *const Brush) -> usize {
match &(*brush).state {
BrushState::Default => 0,
BrushState::Ready(runnable) => runnable.compile_stats.ast_size,
}
}
#[unsafe(no_mangle)]
unsafe extern "C" fn haku_stat_num_refs(instance: *const Instance) -> usize {
(*instance).vm.num_refs()
}
#[unsafe(no_mangle)]
unsafe extern "C" fn haku_stat_remaining_fuel(instance: *const Instance) -> usize {
(*instance).vm.remaining_fuel()
}
#[unsafe(no_mangle)]
unsafe extern "C" fn haku_stat_remaining_memory(instance: *const Instance) -> usize {
(*instance).vm.remaining_memory()
}

View file

@ -73,10 +73,18 @@ impl Vm {
}
}
pub fn num_refs(&self) -> usize {
self.refs.len()
}
pub fn remaining_fuel(&self) -> usize {
self.fuel
}
pub fn remaining_memory(&self) -> usize {
self.memory
}
pub fn set_fuel(&mut self, fuel: usize) {
self.fuel = fuel;
}