Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
LoadLocals
  • Loading branch information
youknowone committed Jan 29, 2026
commit abec14b858c821283d89fc299af6e0e5d70b22a3
8 changes: 7 additions & 1 deletion crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2048,8 +2048,14 @@ impl Compiler {

let op = match usage {
NameUsage::Load => {
// Special case for class scope
// ClassBlock (not inlined comp): LOAD_LOCALS first, then LOAD_FROM_DICT_OR_DEREF
if self.ctx.in_class && !self.ctx.in_func() {
emit!(self, Instruction::LoadLocals);
Instruction::LoadFromDictOrDeref
// can_see_class_scope: LOAD_DEREF(__classdict__) first
} else if can_see_class_scope {
let classdict_idx = self.get_free_var_index("__classdict__")?;
self.emit_arg(classdict_idx, Instruction::LoadDeref);
Instruction::LoadFromDictOrDeref
} else {
Instruction::LoadDeref
Expand Down
6 changes: 3 additions & 3 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum Instruction {
GetYieldFromIter = 19,
InterpreterExit = 20, // Placeholder
LoadBuildClass = 21,
LoadLocals = 22, // Placeholder
LoadLocals = 22,
MakeFunction = 23,
MatchKeys = 24,
MatchMapping = 25,
Expand Down Expand Up @@ -186,7 +186,7 @@ pub enum Instruction {
arg: Arg<u32>,
} = 89,
LoadFromDictOrDeref(Arg<NameIdx>) = 90,
LoadFromDictOrGlobals(Arg<NameIdx>) = 91, // Placeholder
LoadFromDictOrGlobals(Arg<NameIdx>) = 91,
LoadGlobal(Arg<NameIdx>) = 92,
LoadName(Arg<NameIdx>) = 93,
LoadSmallInt {
Expand Down Expand Up @@ -472,7 +472,7 @@ impl InstructionMetadata for Instruction {
Self::DeleteName(_) => 0,
Self::DeleteGlobal(_) => 0,
Self::DeleteDeref(_) => 0,
Self::LoadFromDictOrDeref(_) => 1,
Self::LoadFromDictOrDeref(_) => 0, // (dict -- value)
Self::StoreSubscr => -3,
Self::DeleteSubscr => -2,
Self::LoadAttr { idx } => {
Expand Down
5 changes: 4 additions & 1 deletion crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,13 +1176,16 @@ impl ExecutingFrame<'_> {
Ok(None)
}
Instruction::LoadFromDictOrDeref(i) => {
// Pop dict from stack (locals or classdict depending on context)
let class_dict = self.pop_value();
let i = i.get(arg) as usize;
let name = if i < self.code.cellvars.len() {
self.code.cellvars[i]
} else {
self.code.freevars[i - self.code.cellvars.len()]
};
let value = self.locals.mapping().subscript(name, vm).ok();
// First try to find in the dict
let value = class_dict.get_item(name, vm).ok();
self.push_value(match value {
Some(v) => v,
None => self.cells_frees[i]
Expand Down