move apply_defs to just before the VM evaluates code

This commit is contained in:
liquidex 2024-08-24 18:04:18 +02:00
parent 4761fd40ba
commit 2d1f49f78d
2 changed files with 7 additions and 4 deletions

View file

@ -174,6 +174,7 @@ unsafe extern "C" fn haku_instance_destroy(instance: *mut Instance) {
#[no_mangle] #[no_mangle]
unsafe extern "C" fn haku_reset(instance: *mut Instance) { unsafe extern "C" fn haku_reset(instance: *mut Instance) {
debug!("resetting instance: {instance:?}");
let instance = &mut *instance; let instance = &mut *instance;
instance.system.restore_image(&instance.system_image); instance.system.restore_image(&instance.system_image);
instance.defs.restore_image(&instance.defs_image); instance.defs.restore_image(&instance.defs_image);
@ -181,6 +182,7 @@ unsafe extern "C" fn haku_reset(instance: *mut Instance) {
#[no_mangle] #[no_mangle]
unsafe extern "C" fn haku_reset_vm(instance: *mut Instance) { unsafe extern "C" fn haku_reset_vm(instance: *mut Instance) {
debug!("resetting instance VM: {instance:?}");
let instance = &mut *instance; let instance = &mut *instance;
instance.vm.restore_image(&instance.vm_image); instance.vm.restore_image(&instance.vm_image);
} }
@ -348,8 +350,6 @@ unsafe extern "C" fn haku_compile_brush(
}; };
brush.state = BrushState::Ready(chunk_id); brush.state = BrushState::Ready(chunk_id);
instance.vm.apply_defs(&instance.defs);
info!("brush compiled into {chunk_id:?}"); info!("brush compiled into {chunk_id:?}");
StatusCode::Ok StatusCode::Ok
@ -395,6 +395,9 @@ unsafe extern "C" fn haku_eval_brush(instance: *mut Instance, brush: *const Brus
panic!("brush is not compiled and ready to be used"); panic!("brush is not compiled and ready to be used");
}; };
debug!("applying defs");
instance.vm.apply_defs(&instance.defs);
let Ok(closure_id) = instance.vm.create_ref(Ref::Closure(Closure { let Ok(closure_id) = instance.vm.create_ref(Ref::Closure(Closure {
start: BytecodeLoc { start: BytecodeLoc {
chunk_id, chunk_id,

View file

@ -114,8 +114,6 @@ impl Haku {
let chunk_id = self.system.add_chunk(chunk).context("too many chunks")?; let chunk_id = self.system.add_chunk(chunk).context("too many chunks")?;
self.brush = Some(chunk_id); self.brush = Some(chunk_id);
self.vm.apply_defs(&self.defs);
Ok(()) Ok(())
} }
@ -124,6 +122,8 @@ impl Haku {
.brush .brush
.ok_or_eyre("brush is not compiled and ready to be used")?; .ok_or_eyre("brush is not compiled and ready to be used")?;
self.vm.apply_defs(&self.defs);
let closure_id = self let closure_id = self
.vm .vm
.create_ref(Ref::Closure(Closure { .create_ref(Ref::Closure(Closure {