Skip to content
Merged
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
14 changes: 8 additions & 6 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4423,11 +4423,12 @@ impl Compiler {
}

// Build kwargs if needed
let has_kwargs = arguments.is_some_and(|args| !args.keywords.is_empty());
if has_kwargs {
if arguments.is_some_and(|args| !args.keywords.is_empty()) {
self.compile_keywords(&arguments.unwrap().keywords)?;
} else {
emit!(self, Instruction::PushNull);
}
emit!(self, Instruction::CallFunctionEx { has_kwargs });
emit!(self, Instruction::CallFunctionEx);
} else {
// Simple case: no starred bases, no **kwargs
// Compile bases normally
Expand Down Expand Up @@ -6961,11 +6962,12 @@ impl Compiler {
}

// Create an optional map with kw-args:
let has_kwargs = !arguments.keywords.is_empty();
if has_kwargs {
if !arguments.keywords.is_empty() {
self.compile_keywords(&arguments.keywords)?;
} else {
emit!(self, Instruction::PushNull);
}
emit!(self, Instruction::CallFunctionEx { has_kwargs });
emit!(self, Instruction::CallFunctionEx);
} else if !arguments.keywords.is_empty() {
// No **kwargs in this branch (has_double_star is false),
// so all keywords have arg.is_some()
Expand Down
10 changes: 4 additions & 6 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ pub enum Instruction {
Call {
nargs: Arg<u32>,
} = 53,
CallFunctionEx {
has_kwargs: Arg<bool>,
} = 54,
CallFunctionEx = 54,
CallIntrinsic1 {
func: Arg<IntrinsicFunction1>,
} = 55,
Expand Down Expand Up @@ -575,8 +573,8 @@ impl InstructionMetadata for Instruction {
Self::Call { nargs } => -(nargs.get(arg) as i32) - 2 + 1,
// CallKw: pops kw_names_tuple + nargs + self_or_null + callable, pushes result
Self::CallKw { nargs } => -1 - (nargs.get(arg) as i32) - 2 + 1,
// CallFunctionEx: pops kwargs(if any) + args_tuple + self_or_null + callable, pushes result
Self::CallFunctionEx { has_kwargs } => -1 - (has_kwargs.get(arg) as i32) - 2 + 1,
// CallFunctionEx: always pops kwargs_or_null + args_tuple + self_or_null + callable, pushes result
Self::CallFunctionEx => -4 + 1,
Self::CheckEgMatch => 0, // pops 2 (exc, type), pushes 2 (rest, match)
Self::ConvertValue { .. } => 0,
Self::FormatSimple => 0,
Expand Down Expand Up @@ -880,7 +878,7 @@ impl InstructionMetadata for Instruction {
Self::BuildTupleFromIter => w!(BUILD_TUPLE_FROM_ITER),
Self::BuildTupleFromTuples { size } => w!(BUILD_TUPLE_FROM_TUPLES, size),
Self::Call { nargs } => w!(CALL, nargs),
Self::CallFunctionEx { has_kwargs } => w!(CALL_FUNCTION_EX, has_kwargs),
Self::CallFunctionEx => w!(CALL_FUNCTION_EX),
Self::CallKw { nargs } => w!(CALL_KW, nargs),
Self::CallIntrinsic1 { func } => w!(CALL_INTRINSIC_1, ?func),
Self::CallIntrinsic2 { func } => w!(CALL_INTRINSIC_2, ?func),
Expand Down
12 changes: 6 additions & 6 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,9 @@ impl ExecutingFrame<'_> {
let args = self.collect_keyword_args(nargs.get(arg));
self.execute_call(args, vm)
}
Instruction::CallFunctionEx { has_kwargs } => {
// Stack: [callable, self_or_null, args_tuple, (kwargs_dict)?]
let args = self.collect_ex_args(vm, has_kwargs.get(arg))?;
Instruction::CallFunctionEx => {
// Stack: [callable, self_or_null, args_tuple, kwargs_or_null]
let args = self.collect_ex_args(vm)?;
self.execute_call(args, vm)
}
Instruction::CallIntrinsic1 { func } => {
Expand Down Expand Up @@ -2128,9 +2128,9 @@ impl ExecutingFrame<'_> {
FuncArgs::with_kwargs_names(args, kwarg_names)
}

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

// Use keys() method for all mapping objects to preserve order
Expand Down
Loading