Skip to content

Commit 0717b53

Browse files
authored
Align CallFunctionEx to 3.14 (#6786)
1 parent ed785e3 commit 0717b53

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

crates/codegen/src/compile.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4423,11 +4423,12 @@ impl Compiler {
44234423
}
44244424

44254425
// Build kwargs if needed
4426-
let has_kwargs = arguments.is_some_and(|args| !args.keywords.is_empty());
4427-
if has_kwargs {
4426+
if arguments.is_some_and(|args| !args.keywords.is_empty()) {
44284427
self.compile_keywords(&arguments.unwrap().keywords)?;
4428+
} else {
4429+
emit!(self, Instruction::PushNull);
44294430
}
4430-
emit!(self, Instruction::CallFunctionEx { has_kwargs });
4431+
emit!(self, Instruction::CallFunctionEx);
44314432
} else {
44324433
// Simple case: no starred bases, no **kwargs
44334434
// Compile bases normally
@@ -6961,11 +6962,12 @@ impl Compiler {
69616962
}
69626963

69636964
// Create an optional map with kw-args:
6964-
let has_kwargs = !arguments.keywords.is_empty();
6965-
if has_kwargs {
6965+
if !arguments.keywords.is_empty() {
69666966
self.compile_keywords(&arguments.keywords)?;
6967+
} else {
6968+
emit!(self, Instruction::PushNull);
69676969
}
6968-
emit!(self, Instruction::CallFunctionEx { has_kwargs });
6970+
emit!(self, Instruction::CallFunctionEx);
69696971
} else if !arguments.keywords.is_empty() {
69706972
// No **kwargs in this branch (has_double_star is false),
69716973
// so all keywords have arg.is_some()

crates/compiler-core/src/bytecode/instruction.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ pub enum Instruction {
9595
Call {
9696
nargs: Arg<u32>,
9797
} = 53,
98-
CallFunctionEx {
99-
has_kwargs: Arg<bool>,
100-
} = 54,
98+
CallFunctionEx = 54,
10199
CallIntrinsic1 {
102100
func: Arg<IntrinsicFunction1>,
103101
} = 55,
@@ -575,8 +573,8 @@ impl InstructionMetadata for Instruction {
575573
Self::Call { nargs } => -(nargs.get(arg) as i32) - 2 + 1,
576574
// CallKw: pops kw_names_tuple + nargs + self_or_null + callable, pushes result
577575
Self::CallKw { nargs } => -1 - (nargs.get(arg) as i32) - 2 + 1,
578-
// CallFunctionEx: pops kwargs(if any) + args_tuple + self_or_null + callable, pushes result
579-
Self::CallFunctionEx { has_kwargs } => -1 - (has_kwargs.get(arg) as i32) - 2 + 1,
576+
// CallFunctionEx: always pops kwargs_or_null + args_tuple + self_or_null + callable, pushes result
577+
Self::CallFunctionEx => -4 + 1,
580578
Self::CheckEgMatch => 0, // pops 2 (exc, type), pushes 2 (rest, match)
581579
Self::ConvertValue { .. } => 0,
582580
Self::FormatSimple => 0,
@@ -880,7 +878,7 @@ impl InstructionMetadata for Instruction {
880878
Self::BuildTupleFromIter => w!(BUILD_TUPLE_FROM_ITER),
881879
Self::BuildTupleFromTuples { size } => w!(BUILD_TUPLE_FROM_TUPLES, size),
882880
Self::Call { nargs } => w!(CALL, nargs),
883-
Self::CallFunctionEx { has_kwargs } => w!(CALL_FUNCTION_EX, has_kwargs),
881+
Self::CallFunctionEx => w!(CALL_FUNCTION_EX),
884882
Self::CallKw { nargs } => w!(CALL_KW, nargs),
885883
Self::CallIntrinsic1 { func } => w!(CALL_INTRINSIC_1, ?func),
886884
Self::CallIntrinsic2 { func } => w!(CALL_INTRINSIC_2, ?func),

crates/vm/src/frame.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,9 @@ impl ExecutingFrame<'_> {
785785
let args = self.collect_keyword_args(nargs.get(arg));
786786
self.execute_call(args, vm)
787787
}
788-
Instruction::CallFunctionEx { has_kwargs } => {
789-
// Stack: [callable, self_or_null, args_tuple, (kwargs_dict)?]
790-
let args = self.collect_ex_args(vm, has_kwargs.get(arg))?;
788+
Instruction::CallFunctionEx => {
789+
// Stack: [callable, self_or_null, args_tuple, kwargs_or_null]
790+
let args = self.collect_ex_args(vm)?;
791791
self.execute_call(args, vm)
792792
}
793793
Instruction::CallIntrinsic1 { func } => {
@@ -2150,9 +2150,9 @@ impl ExecutingFrame<'_> {
21502150
FuncArgs::with_kwargs_names(args, kwarg_names)
21512151
}
21522152

2153-
fn collect_ex_args(&mut self, vm: &VirtualMachine, has_kwargs: bool) -> PyResult<FuncArgs> {
2154-
let kwargs = if has_kwargs {
2155-
let kw_obj = self.pop_value();
2153+
fn collect_ex_args(&mut self, vm: &VirtualMachine) -> PyResult<FuncArgs> {
2154+
let kwargs_or_null = self.pop_value_opt();
2155+
let kwargs = if let Some(kw_obj) = kwargs_or_null {
21562156
let mut kwargs = IndexMap::new();
21572157

21582158
// Use keys() method for all mapping objects to preserve order

0 commit comments

Comments
 (0)