refactor vm

This commit is contained in:
2026-04-19 17:23:51 +08:00
parent ca7f7a5ec8
commit e527d31450
14 changed files with 1504 additions and 906 deletions
+86
View File
@@ -0,0 +1,86 @@
use fix_error::Error;
use fix_common::Symbol;
use crate::{BytecodeReader, CallFrame, StepResult, WithEnv};
use gc_arena::Gc;
impl<'gc> crate::Vm<'gc> {
#[inline(always)]
pub(crate) fn op_push_with(
&mut self,
ctx: &mut impl crate::VmContext,
reader: &mut BytecodeReader<'_>,
mc: &gc_arena::Mutation<'gc>,
) -> StepResult<'gc> {
let env = reader.read_operand_data(ctx).resolve(mc, self);
let scope = Gc::new(
mc,
WithEnv {
env,
prev: self.with_env,
},
);
self.with_env = Some(scope);
StepResult::Continue
}
#[inline(always)]
pub(crate) fn op_pop_with(&mut self) -> StepResult<'gc> {
let Some(scope) = self.with_env else {
unreachable!("no with_scope to pop");
};
self.with_env = scope.prev;
StepResult::Continue
}
#[inline(always)]
pub(crate) fn op_prepare_with(&mut self) -> StepResult<'gc> {
self.call_stack.push(CallFrame {
pc: usize::MAX,
stack_depth: 0,
thunk: None,
env: self.env,
with_env: self.with_env,
});
StepResult::Continue
}
#[inline(always)]
pub(crate) fn op_lookup_with(
&mut self,
ctx: &mut impl crate::VmContext,
reader: &mut BytecodeReader<'_>,
mc: &gc_arena::Mutation<'gc>,
) -> StepResult<'gc> {
let name = reader.read_string_id();
let Some(&WithEnv { env, prev }) = self.with_env.as_deref() else {
let Some(CallFrame { with_env, .. }) = self.call_stack.pop() else {
unreachable!()
};
self.with_env = with_env;
return StepResult::Done(Err(Error::eval_error(format!(
"undefined variable '{}'",
Symbol::from(ctx.resolve_string(name))
))));
};
self.push_stack(env);
if let Some(step) = self.try_force_resolved(0, reader.inst_start_pc(), mc) {
return step;
}
let env = self.pop_stack().as_gc::<crate::AttrSet>().unwrap();
let Some(val) = env.lookup(name) else {
reader.set_pc(reader.inst_start_pc());
self.with_env = prev;
return StepResult::Continue;
};
self.push_stack(val);
let Some(CallFrame { with_env, .. }) = self.call_stack.pop() else {
unreachable!()
};
self.with_env = with_env;
StepResult::Continue
}
}