feat: suspend managed calls on generic awaitable operands

This commit is contained in:
2026-10-23 20:14:15 +08:00
parent b33d322690
commit 16816c38c7
9 changed files with 90 additions and 10 deletions
+53 -3
View File
@@ -88,7 +88,7 @@ impl Vm {
}
let mut frames = vec![frame];
let result = self
.eval_frames(&mut frames, limits, &mut executed, usize::MAX)
.eval_frames(&mut frames, limits, &mut executed, usize::MAX, false)
.map(|value| value.expect("unbounded slice completes or fails"));
if result.is_err() {
self.last_traceback = frames
@@ -189,7 +189,13 @@ impl Vm {
.suspended_call
.take()
.ok_or(VmError::InvalidArgument("no suspended call"))?;
let result = self.eval_frames(&mut call.frames, call.limits, &mut call.executed, slice_end);
let result = self.eval_frames(
&mut call.frames,
call.limits,
&mut call.executed,
slice_end,
false,
);
if let Ok(None) = result {
let instructions = call.executed;
self.suspended_call = Some(call);
@@ -376,8 +382,14 @@ impl Vm {
_ => unreachable!(),
};
let slice_end = executed.saturating_add(slice);
let result = self.eval_frames(&mut frames, limits, &mut executed, slice_end);
let result = self.eval_frames(&mut frames, limits, &mut executed, slice_end, true);
if let Ok(None) = result {
let waiting = frames.last().filter(|frame| frame.waiting).map(|frame| {
match Self::top_value(frame).expect("await operand remains on stack") {
Value::Object(reference) => reference,
_ => unreachable!(),
}
});
let frames = std::sync::Arc::new(frames);
let mut roots = Vec::new();
for frame in frames.iter() {
@@ -395,6 +407,12 @@ impl Vm {
self.release(root)?;
}
published?;
if let Some(awaitable) = waiting {
return Ok(CoroutineProgress::Waiting {
awaitable,
instructions: executed,
});
}
return Ok(CoroutineProgress::Pending {
instructions: executed,
});
@@ -596,6 +614,7 @@ impl Vm {
name: data.name().into(),
instruction_offset: 0,
current_offset: 0,
waiting: false,
localsplus: vec![None; executable.slot_count()],
stack: Vec::with_capacity(executable.stack_size()),
executable,
@@ -981,6 +1000,7 @@ impl Vm {
limits: ExecutionLimits,
executed: &mut usize,
slice_end: usize,
allow_wait: bool,
) -> Result<Option<Value>, VmError> {
loop {
if *executed >= limits.max_instructions {
@@ -997,6 +1017,36 @@ impl Vm {
let opcode = decoded.instruction.opcode;
let argument = decoded.instruction.argument as usize;
match opcode {
Opcode::Await => {
if self.objects.is_read_only() {
return Err(VmError::UnsupportedOperation(
"await during read-only execution",
));
}
let Value::Object(awaitable) = Self::top_value(frame)? else {
return Err(VmError::ObjectKindMismatch("awaitable object"));
};
match self.object(awaitable)?.payload().poll_await()? {
crate::AwaitPoll::Pending => {
if !allow_wait {
return Err(VmError::UnsupportedOperation(
"pending await outside a coroutine",
));
}
// Keep the evaluated operand, never replay its call
// or the statements before this await instruction.
frame.waiting = true;
frame.instruction_offset = decoded.offset;
return Ok(None);
}
crate::AwaitPoll::Ready(value) => {
self.retain_value(&value)?;
self.discard(frame, 1);
frame.stack.push(StackEntry::Value(value));
frame.waiting = false;
}
}
}
Opcode::Nop => {}
Opcode::LoadConst => {
let value = frame.executable.constants()[argument].clone();
+12 -2
View File
@@ -38,6 +38,7 @@ pub struct EndorFrame {
pub(crate) name: String,
pub(crate) instruction_offset: usize,
pub(crate) current_offset: usize,
pub(crate) waiting: bool,
pub(crate) localsplus: Vec<Option<Value>>,
pub(crate) stack: Vec<StackEntry>,
}
@@ -118,8 +119,17 @@ pub(crate) struct SuspendedCall {
/// not keep a VM-wide heap scope active between quanta.
#[derive(Debug)]
pub enum CoroutineProgress {
Pending { instructions: usize },
Complete { value: Value, instructions: usize },
Waiting {
awaitable: ObjectRef,
instructions: usize,
},
Pending {
instructions: usize,
},
Complete {
value: Value,
instructions: usize,
},
}
#[derive(Debug)]
pub(crate) enum CoroutineState {
+2 -2
View File
@@ -48,8 +48,8 @@ pub use frame::{
// Preserve existing embedding imports; objects owns these definitions.
pub use objects::ObjectStore;
pub use objects::{
AnnotationType, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes,
TypeConstructor, TypedMethodRequirement,
AnnotationType, AwaitPoll, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement,
RootTypes, TypeConstructor, TypedMethodRequirement,
};
pub use objects::{
CallArity, CallEffect, CallSignature, CallType, ChangeBatch, ChangeKey, CodeLayout, CompareOp,