feat: suspend managed calls on generic awaitable operands
This commit is contained in:
+53
-3
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user