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])
| 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 |
no test coverage detected