feat: prepare layout and paint occurrences lazily within VM quanta

This commit is contained in:
2026-10-23 11:23:08 +08:00
parent 65f5092f0f
commit 618bffdada
3 changed files with 262 additions and 147 deletions
+76
View File
@@ -34,6 +34,82 @@ pub fn build(
Ok(driver)
}
/// A fixed-size driver. Each callback returns whether another occurrence is
/// needed; delegated source calls and all iterations share this VM budget.
pub fn repeat_call(
cx: &mut NativeContext<'_>,
callback: ObjectRef,
name: &'static str,
desktop: ObjectRef,
) -> Result<ObjectRef, VmError> {
let mut instructions = vec![
Instruction::new(Opcode::LoadConst, 0),
Instruction::simple(Opcode::PushNull),
Instruction::new(Opcode::Call, 0),
];
repeat_while(&mut instructions)?;
instructions.extend([
Instruction::new(Opcode::LoadConst, 1),
Instruction::simple(Opcode::ReturnValue),
]);
driver(
cx,
instructions,
vec![Value::Object(callback), Value::Nil],
name,
desktop,
)
}
/// Prepare only the current occurrence, collect its result, then continue the
/// outer driver. The helper frame and its managed captures die before the next.
pub fn collect_and_continue(
cx: &mut NativeContext<'_>,
collector: ObjectRef,
function: ObjectRef,
arguments: Vec<Value>,
name: &'static str,
desktop: ObjectRef,
) -> Result<ObjectRef, VmError> {
let count = arguments.len();
let mut constants = vec![Value::Object(collector), Value::Object(function)];
constants.extend(arguments);
constants.push(Value::Bool(true));
let mut instructions = vec![
Instruction::new(Opcode::LoadConst, 0),
Instruction::simple(Opcode::PushNull),
Instruction::new(Opcode::LoadConst, 1),
Instruction::simple(Opcode::PushNull),
];
for index in 0..count {
instructions.push(Instruction::new(Opcode::LoadConst, index as u32 + 2));
}
instructions.extend([
Instruction::new(Opcode::Call, count as u32),
Instruction::new(Opcode::Call, 1),
Instruction::simple(Opcode::PopTop),
Instruction::new(Opcode::LoadConst, count as u32 + 2),
Instruction::simple(Opcode::ReturnValue),
]);
driver(cx, instructions, constants, name, desktop)
}
fn driver(
cx: &mut NativeContext<'_>,
instructions: Vec<Instruction>,
constants: Vec<Value>,
name: &'static str,
desktop: ObjectRef,
) -> Result<ObjectRef, VmError> {
let code = EndorCode::from_instructions(&instructions, constants, CodeLayout::default())?
.with_location(name, "<tessera>", 1);
let code = cx.allocate(EndorCodeObject::new(code))?;
let globals = cx.new_dict()?;
let function = cx.new_function(name, code, globals)?;
crate::ui_bridge::attach_desktop_type(cx, function, "Function", desktop)?;
Ok(function)
}
/// Consume the condition on the stack: true repeats this same frame from zero,
/// false falls through. Operand widths include EXTENDED_ARG code units.
pub fn repeat_while(instructions: &mut Vec<Instruction>) -> Result<(), VmError> {