Skip to content

Commit f3501f4

Browse files
committed
Basic async for comprehension support
1 parent 61f37b1 commit f3501f4

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

‎compiler/codegen/src/compile.rs‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,24 +2629,30 @@ impl Compiler {
26292629
compile_element: &dyn Fn(&mut Self) -> CompileResult<()>,
26302630
) -> CompileResult<()> {
26312631
let prev_ctx = self.ctx;
2632+
let is_async = generators.iter().any(|g| g.is_async);
26322633

26332634
self.ctx = CompileContext {
26342635
loop_data: None,
26352636
in_class: prev_ctx.in_class,
2636-
func: FunctionContext::Function,
2637+
func: if is_async {
2638+
FunctionContext::AsyncFunction
2639+
} else {
2640+
FunctionContext::Function
2641+
},
26372642
};
26382643

26392644
// We must have at least one generator:
26402645
assert!(!generators.is_empty());
26412646

2647+
let flags = bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED;
2648+
let flags = if is_async {
2649+
flags | bytecode::CodeFlags::IS_COROUTINE
2650+
} else {
2651+
flags
2652+
};
2653+
26422654
// Create magnificent function <listcomp>:
2643-
self.push_output(
2644-
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
2645-
1,
2646-
1,
2647-
0,
2648-
name.to_owned(),
2649-
);
2655+
self.push_output(flags, 1, 1, 0, name.to_owned());
26502656
let arg0 = self.varname(".0")?;
26512657

26522658
let return_none = init_collection.is_none();
@@ -2656,11 +2662,12 @@ impl Compiler {
26562662
}
26572663

26582664
let mut loop_labels = vec![];
2659-
let mut is_async = false;
26602665
for generator in generators {
26612666
let loop_block = self.new_block();
26622667
let after_block = self.new_block();
26632668

2669+
// emit!(self, Instruction::SetupLoop);
2670+
26642671
if loop_labels.is_empty() {
26652672
// Load iterator onto stack (passed as first argument):
26662673
emit!(self, Instruction::LoadFast(arg0));
@@ -2679,13 +2686,16 @@ impl Compiler {
26792686
loop_labels.push((loop_block, after_block));
26802687
self.switch_to_block(loop_block);
26812688
if generator.is_async {
2682-
is_async = true;
2683-
emit!(self, Instruction::SetupExcept {
2684-
handler: after_block,
2685-
});
2689+
emit!(
2690+
self,
2691+
Instruction::SetupExcept {
2692+
handler: after_block,
2693+
}
2694+
);
26862695
emit!(self, Instruction::GetANext);
26872696
self.emit_constant(ConstantData::None);
26882697
emit!(self, Instruction::YieldFrom);
2698+
self.compile_store(&generator.target)?;
26892699
emit!(self, Instruction::PopBlock);
26902700
} else {
26912701
emit!(
@@ -2694,10 +2704,9 @@ impl Compiler {
26942704
target: after_block,
26952705
}
26962706
);
2707+
self.compile_store(&generator.target)?;
26972708
}
26982709

2699-
self.compile_store(&generator.target)?;
2700-
27012710
// Now evaluate the ifs:
27022711
for if_condition in &generator.ifs {
27032712
self.compile_jump_if(if_condition, false, loop_block)?

‎vm/src/frame.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,7 @@ impl ExecutingFrame<'_> {
19441944
#[track_caller]
19451945
fn pop_block(&mut self) -> Block {
19461946
let block = self.state.blocks.pop().expect("No more blocks to pop!");
1947+
// eprintln!("popped block: {:?} stack: {} truncate to {}", block.typ, self.state.stack.len(), block.level);
19471948
#[cfg(debug_assertions)]
19481949
if self.state.stack.len() < block.level {
19491950
dbg!(&self);
@@ -2002,6 +2003,7 @@ impl ExecutingFrame<'_> {
20022003
}
20032004

20042005
#[inline]
2006+
#[track_caller]
20052007
fn nth_value(&self, depth: u32) -> &PyObject {
20062008
let stack = &self.state.stack;
20072009
&stack[stack.len() - depth as usize - 1]

0 commit comments

Comments
 (0)