better type assertion ergonomic

This commit is contained in:
2026-04-20 17:57:54 +08:00
parent 581c333070
commit 11b0b8a78e
13 changed files with 399 additions and 258 deletions
+12 -12
View File
@@ -1,7 +1,7 @@
use fix_error::Error;
use gc_arena::{Gc, Mutation, RefLock};
use crate::{BytecodeReader, CallFrame, Closure, Env, StepResult, ThunkState, VmContextExt};
use crate::{BytecodeReader, CallFrame, Closure, Env, Step, ThunkState, VmContextExt};
impl<'gc> crate::Vm<'gc> {
#[inline(always)]
@@ -10,7 +10,7 @@ impl<'gc> crate::Vm<'gc> {
ctx: &mut impl crate::VmContext,
reader: &mut BytecodeReader<'_>,
mc: &Mutation<'gc>,
) -> StepResult {
) -> Step {
if let Some(step) = self.try_force(0, reader, mc) {
return step;
}
@@ -18,7 +18,7 @@ impl<'gc> crate::Vm<'gc> {
return self.finish_err(Error::eval_error("stack overflow; max-call-depth exceeded"));
}
self.call_depth += 1;
let func = self.pop_stack();
let func = self.pop();
let arg = reader.read_operand_data(ctx).resolve(mc, self);
if let Some(closure) = func.as_gc::<Closure>() {
let ip = closure.ip;
@@ -41,7 +41,7 @@ impl<'gc> crate::Vm<'gc> {
} else {
todo!("call other types: {func:?}")
}
StepResult::Continue
Step::Continue
}
#[inline(always)]
@@ -50,7 +50,7 @@ impl<'gc> crate::Vm<'gc> {
ctx: &mut impl crate::VmContext,
reader: &mut BytecodeReader<'_>,
mc: &Mutation<'gc>,
) -> StepResult {
) -> Step {
self.handle_return(reader, ctx, mc)
}
@@ -59,7 +59,7 @@ impl<'gc> crate::Vm<'gc> {
reader: &mut BytecodeReader<'_>,
ctx: &C,
mc: &Mutation<'gc>,
) -> StepResult {
) -> Step {
let ret_inst_pc = reader.pc() - 1;
let Some(CallFrame {
pc: ret_pc,
@@ -69,18 +69,18 @@ impl<'gc> crate::Vm<'gc> {
with_env,
}) = self.call_stack.pop()
else {
let val = self.pop_stack();
let val = self.pop();
return self.finish_ok(ctx.convert_value(val));
};
reader.set_pc(ret_pc);
if let Some(outer_thunk) = thunk {
let val = self.pop_stack();
let val = self.pop();
match val.restrict() {
Ok(val) => {
*outer_thunk.borrow_mut(mc) = ThunkState::Evaluated(val);
if reader.bytecode().get(ret_pc).copied() == Some(fix_codegen::Op::Return as u8)
{
self.push_stack(val.relax());
self.push(val.relax());
}
}
Err(inner_thunk) => {
@@ -109,14 +109,14 @@ impl<'gc> crate::Vm<'gc> {
reader.set_pc(inner_ip);
self.env = inner_env;
self.with_env = inner_with_env;
return StepResult::Continue;
return Step::Continue;
}
ThunkState::Evaluated(val) => {
*outer_thunk.borrow_mut(mc) = ThunkState::Evaluated(val);
if reader.bytecode().get(ret_pc).copied()
== Some(fix_codegen::Op::Return as u8)
{
self.push_stack(val.relax());
self.push(val.relax());
}
}
ThunkState::Apply { func: _, arg: _ } => todo!("force Apply thunk"),
@@ -132,6 +132,6 @@ impl<'gc> crate::Vm<'gc> {
}
self.env = env;
self.with_env = with_env;
StepResult::Continue
Step::Continue
}
}