MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / handle_container

Method handle_container

compiler/src/modules/vm/handlers/data.rs:47–103  ·  view source on GitHub ↗

Indexed access/store, unpacking, and `{value!s:spec}` formatting. `GetItem`/`StoreItem`/`DelItem` are dispatched directly from the hot loop; the arms below cover legacy callers that may route through here. */

(&mut self, op: OpCode, operand: u16, chunk: &SSAChunk, slots: &mut [Val])

Source from the content-addressed store, hash-verified

45
46 /* Indexed access/store, unpacking, and `{value!s:spec}` formatting. `GetItem`/`StoreItem`/`DelItem` are dispatched directly from the hot loop; the arms below cover legacy callers that may route through here. */
47 pub(crate) fn handle_container(&mut self, op: OpCode, operand: u16, chunk: &SSAChunk, slots: &mut [Val]) -> Result<(), VmErr> {
48 match op {
49 OpCode::StoreItem => {
50 self.mark_impure();
51 self.store_item(chunk, slots)?;
52 }
53 OpCode::DelItem => {
54 self.mark_impure();
55 self.del_item(chunk, slots)?;
56 }
57 OpCode::UnpackSequence => self.exec_unpack_seq(operand as usize)?,
58 OpCode::UnpackEx => self.unpack_ex(operand)?,
59 OpCode::FormatValue => {
60 /* Operand layout: bit 0 has_spec, bits 1..=2 conversion (0 none, 1 !r, 2 !s, 3 !a). See parser/literals.rs. */
61 let has_spec = (operand & 1) != 0;
62 let conv = (operand >> 1) & 0b11;
63 let spec_val = if has_spec { Some(self.pop()?) } else { None };
64 let v = self.pop()?;
65
66 // Conversion flags consult the dunder-aware helpers so `f"{x!s}"` honours `__str__`.
67 // Charge each result's length; a big Str is one heap object the object quota misses.
68 let converted = match conv {
69 1 => { let s = self.repr_op(v, chunk, slots)?; self.charge_steps(s.len())?; self.heap.alloc(HeapObj::Str(s))? }
70 2 => { let s = self.display_op(v, chunk, slots)?; self.charge_steps(s.len())?; self.heap.alloc(HeapObj::Str(s))? }
71 3 => {
72 let raw = super::format::display_inline(v, &self.heap);
73 self.charge_steps(raw.len())?;
74 self.heap.alloc(HeapObj::Str(raw.escape_default().collect::<String>()))?
75 }
76 _ => v,
77 };
78
79 let result = match spec_val {
80 Some(sv) => {
81 // `try_get`: a non-heap spec value is a TypeError, not a bad-index heap access.
82 let spec = match self.heap.try_get(sv) {
83 Some(HeapObj::Str(s)) => s.clone(),
84 _ => return Err(cold_type("format spec must be a string")),
85 };
86 // Instance `__format__(spec)` runs through `format_op`; built-ins fall through to the spec engine.
87 self.format_op(converted, &spec, chunk, slots)?
88 }
89 None => {
90 if conv != 0 && let HeapObj::Str(s) = self.heap.get(converted) {
91 s.clone()
92 } else {
93 self.display_op(converted, chunk, slots)?
94 }
95 }
96 };
97 let val = self.heap.alloc(HeapObj::Str(result))?;
98 self.push(val);
99 }
100 _ => return Err(cold_runtime("non-container opcode in handle_container")),
101 }
102 Ok(())
103 }
104

Callers 1

dispatch_genericMethod · 0.80

Calls 15

display_inlineFunction · 0.85
cold_typeFunction · 0.85
cold_runtimeFunction · 0.85
mark_impureMethod · 0.80
store_itemMethod · 0.80
del_itemMethod · 0.80
exec_unpack_seqMethod · 0.80
unpack_exMethod · 0.80
repr_opMethod · 0.80
charge_stepsMethod · 0.80
display_opMethod · 0.80
try_getMethod · 0.80

Tested by

no test coverage detected