Skip to content

Commit 960e8de

Browse files
committed
Deduplicate async/normal with compilation code
1 parent 30a2223 commit 960e8de

1 file changed

Lines changed: 32 additions & 50 deletions

File tree

compiler/src/compile.rs

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -385,73 +385,55 @@ impl<O: OutputStream> Compiler<O> {
385385
items,
386386
body,
387387
} => {
388-
if *is_async {
389-
let end_labels = items
390-
.iter()
391-
.map(|item| {
392-
let end_label = self.new_label();
393-
self.compile_expression(&item.context_expr)?;
388+
let is_async = *is_async;
389+
390+
let end_labels = items
391+
.iter()
392+
.map(|item| {
393+
let end_label = self.new_label();
394+
self.compile_expression(&item.context_expr)?;
395+
396+
if is_async {
394397
self.emit(Instruction::BeforeAsyncWith);
395398
self.emit(Instruction::GetAwaitable);
396399
self.emit(Instruction::LoadConst {
397400
value: bytecode::Constant::None,
398401
});
399402
self.emit(Instruction::YieldFrom);
400403
self.emit(Instruction::SetupAsyncWith { end: end_label });
401-
match &item.optional_vars {
402-
Some(var) => {
403-
self.compile_store(var)?;
404-
}
405-
None => {
406-
self.emit(Instruction::Pop);
407-
}
404+
} else {
405+
self.emit(Instruction::SetupWith { end: end_label });
406+
}
407+
408+
match &item.optional_vars {
409+
Some(var) => {
410+
self.compile_store(var)?;
408411
}
409-
Ok(end_label)
410-
})
411-
.collect::<Result<Vec<_>, CompileError>>()?;
412+
None => {
413+
self.emit(Instruction::Pop);
414+
}
415+
}
416+
Ok(end_label)
417+
})
418+
.collect::<Result<Vec<_>, CompileError>>()?;
412419

413-
self.compile_statements(body)?;
420+
self.compile_statements(body)?;
414421

415-
for end_label in end_labels {
416-
self.emit(Instruction::PopBlock);
417-
self.emit(Instruction::EnterFinally);
418-
self.set_label(end_label);
419-
self.emit(Instruction::WithCleanupStart);
422+
for end_label in end_labels {
423+
self.emit(Instruction::PopBlock);
424+
self.emit(Instruction::EnterFinally);
425+
self.set_label(end_label);
426+
self.emit(Instruction::WithCleanupStart);
427+
428+
if is_async {
420429
self.emit(Instruction::GetAwaitable);
421430
self.emit(Instruction::LoadConst {
422431
value: bytecode::Constant::None,
423432
});
424433
self.emit(Instruction::YieldFrom);
425-
self.emit(Instruction::WithCleanupFinish);
426434
}
427-
} else {
428-
let end_labels = items
429-
.iter()
430-
.map(|item| {
431-
let end_label = self.new_label();
432-
self.compile_expression(&item.context_expr)?;
433-
self.emit(Instruction::SetupWith { end: end_label });
434-
match &item.optional_vars {
435-
Some(var) => {
436-
self.compile_store(var)?;
437-
}
438-
None => {
439-
self.emit(Instruction::Pop);
440-
}
441-
}
442-
Ok(end_label)
443-
})
444-
.collect::<Result<Vec<_>, CompileError>>()?;
445-
446-
self.compile_statements(body)?;
447435

448-
for end_label in end_labels {
449-
self.emit(Instruction::PopBlock);
450-
self.emit(Instruction::EnterFinally);
451-
self.set_label(end_label);
452-
self.emit(Instruction::WithCleanupStart);
453-
self.emit(Instruction::WithCleanupFinish);
454-
}
436+
self.emit(Instruction::WithCleanupFinish);
455437
}
456438
}
457439
For {

0 commit comments

Comments
 (0)