`format(value [, spec])`.
(&mut self, op: u16, chunk: &crate::modules::parser::SSAChunk, slots: &mut [Val])
| 99 | |
| 100 | // `format(value [, spec])`. |
| 101 | pub fn call_format(&mut self, op: u16, chunk: &crate::modules::parser::SSAChunk, slots: &mut [Val]) -> Result<(), VmErr> { |
| 102 | if op != 1 && op != 2 { |
| 103 | return Err(cold_type("format() takes 1 or 2 arguments")); |
| 104 | } |
| 105 | let spec_val = if op == 2 { Some(self.pop()?) } else { None }; |
| 106 | let val = self.pop()?; |
| 107 | let result = match spec_val { |
| 108 | Some(sv) => { |
| 109 | // `sv` may be a non-heap value (int/float); guard before indexing the heap. |
| 110 | let spec = match sv.is_heap().then(|| self.heap.get(sv)) { |
| 111 | Some(HeapObj::Str(s)) => s.clone(), |
| 112 | _ => return Err(cold_type("format() spec must be a string")), |
| 113 | }; |
| 114 | self.format_op(val, &spec, chunk, slots)? |
| 115 | } |
| 116 | None => self.display_op(val, chunk, slots)?, |
| 117 | }; |
| 118 | self.alloc_and_push_str(result) |
| 119 | } |
| 120 | } |
no test coverage detected