Thanks to visit codestin.com
Credit goes to github.com

Skip to content

(WIP) implement async for function #2999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2654,11 +2654,8 @@ impl Compiler {
}

let mut loop_labels = vec![];
let mut is_async = false;
for generator in generators {
if generator.is_async {
unimplemented!("async for comprehensions");
}

let loop_block = self.new_block();
let after_block = self.new_block();

Expand All @@ -2670,18 +2667,32 @@ impl Compiler {
self.compile_expression(&generator.iter)?;

// Get iterator / turn item into an iterator
emit!(self, Instruction::GetIter);
if generator.is_async {
emit!(self, Instruction::GetAIter);
} else {
emit!(self, Instruction::GetIter);
}
}

loop_labels.push((loop_block, after_block));

self.switch_to_block(loop_block);
emit!(
self,
Instruction::ForIter {
target: after_block,
}
);
if generator.is_async {
is_async = true;
emit!(self, Instruction::SetupFinally {
handler: after_block,
});
emit!(self, Instruction::GetANext);
self.emit_constant(ConstantData::None);
emit!(self, Instruction::YieldFrom);
emit!(self, Instruction::PopBlock);
} else {
emit!(
self,
Instruction::ForIter {
target: after_block,
}
);
}

self.compile_store(&generator.target)?;

Expand All @@ -2699,6 +2710,9 @@ impl Compiler {

// End of for loop:
self.switch_to_block(after_block);
if is_async {
emit!(self, Instruction::EndAsyncFor);
}
}

if return_none {
Expand Down Expand Up @@ -2735,10 +2749,19 @@ impl Compiler {
self.compile_expression(&generators[0].iter)?;

// Get iterator / turn item into an iterator
emit!(self, Instruction::GetIter);
if is_async {
emit!(self, Instruction::GetAIter);
} else {
emit!(self, Instruction::GetIter);
};

// Call just created <listcomp> function:
emit!(self, Instruction::CallFunctionPositional { nargs: 1 });
if is_async {
emit!(self, Instruction::GetAwaitable);
self.emit_constant(ConstantData::None);
emit!(self, Instruction::YieldFrom);
}
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ impl Instruction {
}
GetAIter => 0,
GetANext => 1,
EndAsyncFor => -2,
EndAsyncFor => -1,
ExtendedArg => 0,
}
}
Expand Down
1 change: 0 additions & 1 deletion vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,6 @@ impl ExecutingFrame<'_> {
}
bytecode::Instruction::EndAsyncFor => {
let exc = self.pop_value();
self.pop_value(); // async iterator we were calling __anext__ on
if exc.fast_isinstance(vm.ctx.exceptions.stop_async_iteration) {
vm.take_exception().expect("Should have exception in stack");
Ok(None)
Expand Down