Skip to main content

runmat_core/session/
run.rs

1use super::*;
2
3impl RunMatSession {
4    /// Execute MATLAB/Octave code
5    pub async fn execute(&mut self, input: &str) -> std::result::Result<ExecutionResult, RunError> {
6        self.run(input).await
7    }
8
9    /// Parse, lower, compile, and execute input.
10    pub async fn run(&mut self, input: &str) -> std::result::Result<ExecutionResult, RunError> {
11        let _active = ActiveExecutionGuard::new(self).map_err(|err| {
12            RunError::Runtime(
13                build_runtime_error(err.to_string())
14                    .with_identifier("RunMat:ExecutionAlreadyActive")
15                    .build(),
16            )
17        })?;
18        runmat_vm::set_call_stack_limit(self.callstack_limit);
19        runmat_vm::set_error_namespace(&self.error_namespace);
20        runmat_hir::set_error_namespace(&self.error_namespace);
21        let exec_span = info_span!(
22            "runtime.execute",
23            input_len = input.len(),
24            verbose = self.verbose
25        );
26        let _exec_guard = exec_span.enter();
27        runmat_runtime::console::reset_thread_buffer();
28        runmat_runtime::plotting_hooks::reset_recent_figures();
29        runmat_runtime::warning_store::reset();
30        runmat_builtins::set_display_format(self.format_mode);
31        reset_provider_telemetry();
32        self.interrupt_flag.store(false, Ordering::Relaxed);
33        let _interrupt_guard =
34            runmat_runtime::interrupt::replace_interrupt(Some(self.interrupt_flag.clone()));
35        let start_time = Instant::now();
36        self.stats.total_executions += 1;
37        let debug_trace = std::env::var("RUNMAT_DEBUG_REPL").is_ok();
38        let stdin_events: Arc<Mutex<Vec<StdinEvent>>> = Arc::new(Mutex::new(Vec::new()));
39        let host_async_handler = self.async_input_handler.clone();
40        let stdin_events_async = Arc::clone(&stdin_events);
41        let runtime_async_handler: Arc<runmat_runtime::interaction::AsyncInteractionHandler> =
42            Arc::new(
43                move |prompt: runmat_runtime::interaction::InteractionPromptOwned| {
44                    let request_kind = match prompt.kind {
45                        runmat_runtime::interaction::InteractionKind::Line { echo } => {
46                            InputRequestKind::Line { echo }
47                        }
48                        runmat_runtime::interaction::InteractionKind::KeyPress => {
49                            InputRequestKind::KeyPress
50                        }
51                    };
52                    let request = InputRequest {
53                        prompt: prompt.prompt,
54                        kind: request_kind,
55                    };
56                    let (event_kind, echo_flag) = match &request.kind {
57                        InputRequestKind::Line { echo } => (StdinEventKind::Line, *echo),
58                        InputRequestKind::KeyPress => (StdinEventKind::KeyPress, false),
59                    };
60                    let mut event = StdinEvent {
61                        prompt: request.prompt.clone(),
62                        kind: event_kind,
63                        echo: echo_flag,
64                        value: None,
65                        error: None,
66                    };
67
68                    let stdin_events_async = Arc::clone(&stdin_events_async);
69                    let host_async_handler = host_async_handler.clone();
70                    Box::pin(async move {
71                        let resp: Result<InputResponse, String> =
72                            if let Some(handler) = host_async_handler {
73                                handler(request).await
74                            } else {
75                                match &request.kind {
76                                    InputRequestKind::Line { echo } => {
77                                        runmat_runtime::interaction::default_read_line(
78                                            &request.prompt,
79                                            *echo,
80                                        )
81                                        .map(InputResponse::Line)
82                                    }
83                                    InputRequestKind::KeyPress => {
84                                        runmat_runtime::interaction::default_wait_for_key(
85                                            &request.prompt,
86                                        )
87                                        .map(|_| InputResponse::KeyPress)
88                                    }
89                                }
90                            };
91
92                        let resp = resp.inspect_err(|err| {
93                            event.error = Some(err.clone());
94                            if let Ok(mut guard) = stdin_events_async.lock() {
95                                guard.push(event.clone());
96                            }
97                        })?;
98
99                        let interaction_resp = match resp {
100                            InputResponse::Line(value) => {
101                                event.value = Some(value.clone());
102                                if let Ok(mut guard) = stdin_events_async.lock() {
103                                    guard.push(event);
104                                }
105                                runmat_runtime::interaction::InteractionResponse::Line(value)
106                            }
107                            InputResponse::KeyPress => {
108                                if let Ok(mut guard) = stdin_events_async.lock() {
109                                    guard.push(event);
110                                }
111                                runmat_runtime::interaction::InteractionResponse::KeyPress
112                            }
113                        };
114                        Ok(interaction_resp)
115                    })
116                },
117            );
118        let _async_input_guard =
119            runmat_runtime::interaction::replace_async_handler(Some(runtime_async_handler));
120
121        // Install a stateless expression evaluator for `input()` numeric parsing.
122        //
123        // The hook runs the full parse → lower → compile → interpret pipeline so
124        // that users can type arbitrary MATLAB expressions at an input() prompt:
125        // `sqrt(2)`, `pi/2`, `ones(3)`, `[1 2; 3 4]`, etc.
126        //
127        // Stack-overflow hazard: the hook calls runmat_vm::interpret() while
128        // the outer interpret() is already on the call stack. On WASM the JS event
129        // loop drives both as async state-machines and the WASM linear stack is
130        // large, so nesting is safe. On native the default thread stack is too
131        // small for two nested interpret() invocations, so we instead run the inner
132        // interpret() on a dedicated thread that has its own 16 MB stack and block
133        // the calling future synchronously on the result (safe because the native
134        // executor — futures::executor::block_on — is already synchronous).
135        let compat = self.compat_mode;
136        let _eval_hook_guard =
137            runmat_runtime::interaction::replace_eval_hook(Some(std::sync::Arc::new(
138                move |expr: String| -> runmat_runtime::interaction::EvalHookFuture {
139                    // Shared eval logic, used by both the WASM async path and the
140                    // native thread path below.
141                    async fn eval_expr(
142                        expr: String,
143                        compat: runmat_parser::CompatMode,
144                    ) -> Result<Value, RuntimeError> {
145                        let wrapped = format!("__runmat_input_result__ = ({expr});");
146                        let ast = parse_with_options(&wrapped, ParserOptions::new(compat))
147                            .map_err(|e| {
148                                build_runtime_error(format!("input: parse error: {e}"))
149                                    .with_identifier("RunMat:input:ParseError")
150                                    .build()
151                            })?;
152                        let lowering = runmat_hir::lower(
153                            &ast,
154                            &LoweringContext::new(&HashMap::new(), &HashMap::new()),
155                        )
156                        .map_err(|e| {
157                            build_runtime_error(format!("input: lowering error: {e}"))
158                                .with_identifier("RunMat:input:LowerError")
159                                .build()
160                        })?;
161                        let result_idx = lowering.variables.get("__runmat_input_result__").copied();
162                        let bc = runmat_vm::compile(&lowering.hir, &HashMap::new())
163                            .map_err(RuntimeError::from)?;
164                        let vars = runmat_vm::interpret(&bc).await?;
165                        result_idx
166                            .and_then(|idx| vars.get(idx).cloned())
167                            .ok_or_else(|| {
168                                build_runtime_error("input: expression produced no value")
169                                    .with_identifier("RunMat:input:NoValue")
170                                    .build()
171                            })
172                    }
173
174                    #[cfg(target_arch = "wasm32")]
175                    {
176                        // On WASM: await the inner interpret() directly. The JS async
177                        // runtime handles both futures as cooperative state-machines and
178                        // the WASM linear stack is large enough for the extra frames.
179                        Box::pin(eval_expr(expr, compat))
180                    }
181
182                    #[cfg(not(target_arch = "wasm32"))]
183                    {
184                        // On native: run interpret() on a dedicated thread so it gets
185                        // its own 16 MB stack, fully isolated from the outer interpret()
186                        // call stack. The result is sent back via a tokio oneshot channel
187                        // and awaited asynchronously so the tokio worker thread is never
188                        // blocked by a synchronous recv().
189                        let (tx, rx) = tokio::sync::oneshot::channel();
190                        let spawn_result = std::thread::Builder::new()
191                            .stack_size(16 * 1024 * 1024)
192                            .spawn(move || {
193                                let result = futures::executor::block_on(eval_expr(expr, compat));
194                                let _ = tx.send(result);
195                            });
196                        Box::pin(async move {
197                            spawn_result.map_err(|err| {
198                                build_runtime_error(format!(
199                                    "input: failed to spawn eval thread: {err}"
200                                ))
201                                .with_identifier("RunMat:input:EvalThreadSpawnFailed")
202                                .build()
203                            })?;
204                            rx.await.unwrap_or_else(|_| {
205                                Err(build_runtime_error("input: eval thread panicked")
206                                    .with_identifier("RunMat:input:EvalThreadPanic")
207                                    .build())
208                            })
209                        })
210                    }
211                },
212            )));
213
214        if self.verbose {
215            debug!("Executing: {}", input.trim());
216        }
217
218        let _source_guard = runmat_runtime::source_context::replace_current_source(Some(input));
219
220        let PreparedExecution {
221            ast,
222            lowering,
223            mut bytecode,
224        } = self.compile_input(input)?;
225        if self.verbose {
226            debug!("AST: {ast:?}");
227        }
228        let (hir, updated_vars, updated_functions, var_names_map) = (
229            lowering.hir,
230            lowering.variables,
231            lowering.functions,
232            lowering.var_names,
233        );
234        let max_var_id = updated_vars.values().copied().max().unwrap_or(0);
235        if debug_trace {
236            debug!(?updated_vars, "[repl] updated_vars");
237        }
238        if debug_trace {
239            debug!(workspace_values_before = ?self.workspace_values, "[repl] workspace snapshot before execution");
240        }
241        let id_to_name: HashMap<usize, String> = var_names_map
242            .iter()
243            .map(|(var_id, name)| (var_id.0, name.clone()))
244            .collect();
245        let mut assigned_this_execution: HashSet<String> = HashSet::new();
246        let assigned_snapshot: HashSet<String> = updated_vars
247            .keys()
248            .filter(|name| self.workspace_values.contains_key(name.as_str()))
249            .cloned()
250            .collect();
251        let prev_assigned_snapshot = assigned_snapshot.clone();
252        if debug_trace {
253            debug!(?assigned_snapshot, "[repl] assigned snapshot");
254        }
255        let _pending_workspace_guard =
256            runmat_vm::push_pending_workspace(updated_vars.clone(), assigned_snapshot.clone());
257        if self.verbose {
258            debug!("HIR generated successfully");
259        }
260
261        let (single_assign_var, single_stmt_non_assign) = if hir.body.len() == 1 {
262            match &hir.body[0] {
263                runmat_hir::HirStmt::Assign(var_id, _, _, _) => (Some(var_id.0), false),
264                _ => (None, true),
265            }
266        } else {
267            (None, false)
268        };
269
270        bytecode.var_names = id_to_name.clone();
271        if self.verbose {
272            debug!(
273                "Bytecode compiled: {} instructions",
274                bytecode.instructions.len()
275            );
276        }
277
278        #[cfg(not(target_arch = "wasm32"))]
279        let fusion_snapshot = if self.emit_fusion_plan {
280            build_fusion_snapshot(bytecode.accel_graph.as_ref(), &bytecode.fusion_groups)
281        } else {
282            None
283        };
284        #[cfg(target_arch = "wasm32")]
285        let fusion_snapshot: Option<FusionPlanSnapshot> = None;
286
287        // Prepare variable array with existing values before execution
288        self.prepare_variable_array_for_execution(&bytecode, &updated_vars, debug_trace);
289
290        if self.verbose {
291            debug!(
292                "Variable array after preparation: {:?}",
293                self.variable_array
294            );
295            debug!("Updated variable mapping: {updated_vars:?}");
296            debug!("Bytecode instructions: {:?}", bytecode.instructions);
297        }
298
299        #[cfg(feature = "jit")]
300        let mut used_jit = false;
301        #[cfg(not(feature = "jit"))]
302        let used_jit = false;
303        #[cfg(feature = "jit")]
304        let mut execution_completed = false;
305        #[cfg(not(feature = "jit"))]
306        let execution_completed = false;
307        let mut result_value: Option<Value> = None; // Always start fresh for each execution
308        let mut suppressed_value: Option<Value> = None; // Track value for type info when suppressed
309        let mut error = None;
310        let mut workspace_updates: Vec<WorkspaceEntry> = Vec::new();
311        let mut workspace_snapshot_force_full = false;
312        let mut ans_update: Option<(usize, Value)> = None;
313
314        // Check if this is an expression statement (ends with Pop)
315        let is_expression_stmt = bytecode
316            .instructions
317            .last()
318            .map(|instr| matches!(instr, runmat_vm::Instr::Pop))
319            .unwrap_or(false);
320
321        // Determine whether the final statement ended with a semicolon by inspecting the raw input.
322        let is_semicolon_suppressed = {
323            let toks = tokenize_detailed(input);
324            toks.into_iter()
325                .rev()
326                .map(|t| t.token)
327                .find(|token| {
328                    !matches!(
329                        token,
330                        LexToken::Newline
331                            | LexToken::LineComment
332                            | LexToken::BlockComment
333                            | LexToken::Section
334                    )
335                })
336                .map(|t| matches!(t, LexToken::Semicolon))
337                .unwrap_or(false)
338        };
339        let final_stmt_emit = last_displayable_statement_emit_disposition(&hir.body);
340
341        if self.verbose {
342            debug!("HIR body len: {}", hir.body.len());
343            if !hir.body.is_empty() {
344                debug!("HIR statement: {:?}", &hir.body[0]);
345            }
346            debug!("is_semicolon_suppressed: {is_semicolon_suppressed}");
347        }
348
349        // Use JIT for assignments, interpreter for expressions (to capture results properly)
350        #[cfg(feature = "jit")]
351        {
352            if let Some(ref mut jit_engine) = &mut self.jit_engine {
353                if !is_expression_stmt {
354                    // Ensure variable array is large enough
355                    if self.variable_array.len() < bytecode.var_count {
356                        self.variable_array
357                            .resize(bytecode.var_count, Value::Num(0.0));
358                    }
359
360                    if self.verbose {
361                        debug!(
362                            "JIT path for assignment: variable_array size: {}, bytecode.var_count: {}",
363                            self.variable_array.len(),
364                            bytecode.var_count
365                        );
366                    }
367
368                    // Use JIT for assignments
369                    match jit_engine.execute_or_compile(&bytecode, &mut self.variable_array) {
370                        Ok((_, actual_used_jit)) => {
371                            used_jit = actual_used_jit;
372                            execution_completed = true;
373                            if actual_used_jit {
374                                self.stats.jit_compiled += 1;
375                            } else {
376                                self.stats.interpreter_fallback += 1;
377                            }
378                            if let Some(runmat_hir::HirStmt::Assign(var_id, _, _, _)) =
379                                hir.body.first()
380                            {
381                                if let Some(name) = id_to_name.get(&var_id.0) {
382                                    assigned_this_execution.insert(name.clone());
383                                }
384                                if var_id.0 < self.variable_array.len() {
385                                    let assignment_value = self.variable_array[var_id.0].clone();
386                                    if !is_semicolon_suppressed {
387                                        result_value = Some(assignment_value);
388                                        if self.verbose {
389                                            debug!("JIT assignment result: {result_value:?}");
390                                        }
391                                    } else {
392                                        suppressed_value = Some(assignment_value);
393                                        if self.verbose {
394                                            debug!("JIT assignment suppressed due to semicolon, captured for type info");
395                                        }
396                                    }
397                                }
398                            }
399
400                            if self.verbose {
401                                debug!(
402                                    "{} assignment successful, variable_array: {:?}",
403                                    if actual_used_jit {
404                                        "JIT"
405                                    } else {
406                                        "Interpreter"
407                                    },
408                                    self.variable_array
409                                );
410                            }
411                        }
412                        Err(e) => {
413                            if self.verbose {
414                                debug!("JIT execution failed: {e}, using interpreter");
415                            }
416                            // Fall back to interpreter
417                        }
418                    }
419                }
420            }
421        }
422
423        // Use interpreter if JIT failed or is disabled
424        if !execution_completed {
425            if self.verbose {
426                debug!(
427                    "Interpreter path: variable_array size: {}, bytecode.var_count: {}",
428                    self.variable_array.len(),
429                    bytecode.var_count
430                );
431            }
432
433            // For expressions, modify bytecode to store result in a temp variable instead of using stack
434            let mut execution_bytecode = bytecode.clone();
435            if is_expression_stmt
436                && matches!(final_stmt_emit, FinalStmtEmitDisposition::Inline)
437                && !execution_bytecode.instructions.is_empty()
438            {
439                execution_bytecode.instructions.pop(); // Remove the Pop instruction
440
441                // Add StoreVar instruction to store the result in a temporary variable
442                let temp_var_id = std::cmp::max(execution_bytecode.var_count, max_var_id + 1);
443                execution_bytecode
444                    .instructions
445                    .push(runmat_vm::Instr::StoreVar(temp_var_id));
446                execution_bytecode.var_count = temp_var_id + 1; // Expand variable count for temp variable
447
448                // Ensure our variable array can hold the temporary variable
449                if self.variable_array.len() <= temp_var_id {
450                    self.variable_array.resize(temp_var_id + 1, Value::Num(0.0));
451                }
452
453                if self.verbose {
454                    debug!(
455                        "Modified expression bytecode, new instructions: {:?}",
456                        execution_bytecode.instructions
457                    );
458                }
459            }
460
461            match self.interpret_with_context(&execution_bytecode).await {
462                Ok(runmat_vm::InterpreterOutcome::Completed(results)) => {
463                    // Only increment interpreter_fallback if JIT wasn't attempted
464                    if !self.has_jit() || is_expression_stmt {
465                        self.stats.interpreter_fallback += 1;
466                    }
467                    if self.verbose {
468                        debug!("Interpreter results: {results:?}");
469                    }
470
471                    // Handle assignment statements (x = 42 should show the assigned value unless suppressed)
472                    if hir.body.len() == 1 {
473                        if let runmat_hir::HirStmt::Assign(var_id, _, _, _) = &hir.body[0] {
474                            if let Some(name) = id_to_name.get(&var_id.0) {
475                                assigned_this_execution.insert(name.clone());
476                            }
477                            // For assignments, capture the assigned value for both display and type info
478                            if var_id.0 < self.variable_array.len() {
479                                let assignment_value = self.variable_array[var_id.0].clone();
480                                if !is_semicolon_suppressed {
481                                    result_value = Some(assignment_value);
482                                    if self.verbose {
483                                        debug!("Interpreter assignment result: {result_value:?}");
484                                    }
485                                } else {
486                                    suppressed_value = Some(assignment_value);
487                                    if self.verbose {
488                                        debug!("Interpreter assignment suppressed due to semicolon, captured for type info");
489                                    }
490                                }
491                            }
492                        } else if !is_expression_stmt
493                            && !results.is_empty()
494                            && !is_semicolon_suppressed
495                            && matches!(final_stmt_emit, FinalStmtEmitDisposition::NeedsFallback)
496                        {
497                            result_value = Some(results[0].clone());
498                        }
499                    }
500
501                    // For expressions, get the result from the temporary variable (capture for both display and type info)
502                    if is_expression_stmt
503                        && matches!(final_stmt_emit, FinalStmtEmitDisposition::Inline)
504                        && !execution_bytecode.instructions.is_empty()
505                        && result_value.is_none()
506                        && suppressed_value.is_none()
507                    {
508                        let temp_var_id = execution_bytecode.var_count - 1; // The temp variable we added
509                        if temp_var_id < self.variable_array.len() {
510                            let expression_value = self.variable_array[temp_var_id].clone();
511                            if !is_semicolon_suppressed {
512                                // Capture for 'ans' update when output is not suppressed
513                                ans_update = Some((temp_var_id, expression_value.clone()));
514                                result_value = Some(expression_value);
515                                if self.verbose {
516                                    debug!("Expression result from temp var {temp_var_id}: {result_value:?}");
517                                }
518                            } else {
519                                suppressed_value = Some(expression_value);
520                                if self.verbose {
521                                    debug!("Expression suppressed, captured for type info from temp var {temp_var_id}: {suppressed_value:?}");
522                                }
523                            }
524                        }
525                    } else if !is_semicolon_suppressed
526                        && matches!(final_stmt_emit, FinalStmtEmitDisposition::NeedsFallback)
527                        && result_value.is_none()
528                    {
529                        result_value = results.into_iter().last();
530                        if self.verbose {
531                            debug!("Fallback result from interpreter: {result_value:?}");
532                        }
533                    }
534
535                    if self.verbose {
536                        debug!("Final result_value: {result_value:?}");
537                    }
538                    debug!("Interpreter execution successful");
539                }
540
541                Err(e) => {
542                    debug!("Interpreter execution failed: {e}");
543                    error = Some(e);
544                }
545            }
546        }
547
548        let last_assign_var = last_unsuppressed_assign_var(&hir.body);
549        let last_expr_emits = last_expr_emits_value(&hir.body);
550        if !is_semicolon_suppressed && result_value.is_none() {
551            if last_assign_var.is_some() || last_expr_emits {
552                if let Some(value) = runmat_runtime::console::take_last_value_output() {
553                    result_value = Some(value);
554                }
555            }
556            if result_value.is_none() {
557                if last_assign_var.is_some() {
558                    if let Some(var_id) = last_emit_var_index(&bytecode) {
559                        if var_id < self.variable_array.len() {
560                            result_value = Some(self.variable_array[var_id].clone());
561                        }
562                    }
563                }
564                if result_value.is_none() {
565                    if let Some(var_id) = last_assign_var {
566                        if var_id < self.variable_array.len() {
567                            result_value = Some(self.variable_array[var_id].clone());
568                        }
569                    }
570                }
571            }
572        }
573
574        let execution_time = start_time.elapsed();
575        let execution_time_ms = execution_time.as_millis() as u64;
576
577        self.stats.total_execution_time_ms += execution_time_ms;
578        self.stats.average_execution_time_ms =
579            self.stats.total_execution_time_ms as f64 / self.stats.total_executions as f64;
580
581        // Update variable names mapping and function definitions if execution was successful
582        if error.is_none() {
583            if let Some((mutated_names, assigned)) = runmat_vm::take_updated_workspace_state() {
584                if debug_trace {
585                    debug!(
586                        ?mutated_names,
587                        ?assigned,
588                        "[repl] mutated names and assigned return values"
589                    );
590                }
591                self.variable_names = mutated_names.clone();
592                let previous_workspace = self.workspace_values.clone();
593                let current_names: HashSet<String> = assigned
594                    .iter()
595                    .filter(|name| {
596                        mutated_names
597                            .get(*name)
598                            .map(|var_id| *var_id < self.variable_array.len())
599                            .unwrap_or(false)
600                    })
601                    .cloned()
602                    .collect();
603                let removed_names: HashSet<String> = previous_workspace
604                    .keys()
605                    .filter(|name| !current_names.contains(*name))
606                    .cloned()
607                    .collect();
608                let mut rebuilt_workspace = HashMap::new();
609                let mut changed_names: HashSet<String> = assigned
610                    .difference(&prev_assigned_snapshot)
611                    .cloned()
612                    .collect();
613                changed_names.extend(assigned_this_execution.iter().cloned());
614
615                for name in &current_names {
616                    let Some(var_id) = mutated_names.get(name).copied() else {
617                        continue;
618                    };
619                    if var_id >= self.variable_array.len() {
620                        continue;
621                    }
622                    let value_clone = self.variable_array[var_id].clone();
623                    if previous_workspace.get(name) != Some(&value_clone) {
624                        changed_names.insert(name.clone());
625                    }
626                    rebuilt_workspace.insert(name.clone(), value_clone);
627                }
628
629                if debug_trace {
630                    debug!(?changed_names, ?removed_names, "[repl] workspace changes");
631                }
632
633                self.workspace_values = rebuilt_workspace;
634                if !removed_names.is_empty() {
635                    workspace_snapshot_force_full = true;
636                } else {
637                    for name in changed_names {
638                        if let Some(value_clone) = self.workspace_values.get(&name).cloned() {
639                            workspace_updates.push(workspace_entry(&name, &value_clone));
640                            if debug_trace {
641                                debug!(name, ?value_clone, "[repl] workspace update");
642                            }
643                        }
644                    }
645                }
646            } else {
647                for name in &assigned_this_execution {
648                    if let Some(var_id) =
649                        id_to_name
650                            .iter()
651                            .find_map(|(vid, n)| if n == name { Some(*vid) } else { None })
652                    {
653                        if var_id < self.variable_array.len() {
654                            let value_clone = self.variable_array[var_id].clone();
655                            self.workspace_values
656                                .insert(name.clone(), value_clone.clone());
657                            workspace_updates.push(workspace_entry(name, &value_clone));
658                        }
659                    }
660                }
661            }
662            let mut repl_source_id: Option<SourceId> = None;
663            for (name, stmt) in &updated_functions {
664                if matches!(stmt, runmat_hir::HirStmt::Function { .. }) {
665                    let source_id = *repl_source_id
666                        .get_or_insert_with(|| self.source_pool.intern("<repl>", input));
667                    self.function_source_ids.insert(name.clone(), source_id);
668                }
669            }
670            self.function_definitions = updated_functions;
671            // Apply 'ans' update if applicable (persisting expression result)
672            if let Some((var_id, value)) = ans_update {
673                self.variable_names.insert("ans".to_string(), var_id);
674                self.workspace_values.insert("ans".to_string(), value);
675                if debug_trace {
676                    println!("Updated 'ans' to var_id {}", var_id);
677                }
678            }
679        }
680
681        if self.verbose {
682            debug!("Execution completed in {execution_time_ms}ms (JIT: {used_jit})");
683        }
684
685        if !is_expression_stmt
686            && !is_semicolon_suppressed
687            && matches!(final_stmt_emit, FinalStmtEmitDisposition::NeedsFallback)
688            && result_value.is_none()
689        {
690            if let Some(v) = self
691                .variable_array
692                .iter()
693                .rev()
694                .find(|v| !matches!(v, Value::Num(0.0)))
695                .cloned()
696            {
697                result_value = Some(v);
698            }
699        }
700
701        if !is_semicolon_suppressed
702            && matches!(final_stmt_emit, FinalStmtEmitDisposition::NeedsFallback)
703        {
704            if let Some(value) = result_value.as_ref() {
705                let label = determine_display_label_from_context(
706                    single_assign_var,
707                    &id_to_name,
708                    is_expression_stmt,
709                    single_stmt_non_assign,
710                );
711                runmat_runtime::console::record_value_output(label.as_deref(), value);
712            }
713        }
714
715        // Generate type info if we have a suppressed value
716        let type_info = suppressed_value.as_ref().map(format_type_info);
717
718        let streams = runmat_runtime::console::take_thread_buffer()
719            .into_iter()
720            .map(|entry| ExecutionStreamEntry {
721                stream: match entry.stream {
722                    runmat_runtime::console::ConsoleStream::Stdout => ExecutionStreamKind::Stdout,
723                    runmat_runtime::console::ConsoleStream::Stderr => ExecutionStreamKind::Stderr,
724                    runmat_runtime::console::ConsoleStream::ClearScreen => {
725                        ExecutionStreamKind::ClearScreen
726                    }
727                },
728                text: entry.text,
729                timestamp_ms: entry.timestamp_ms,
730            })
731            .collect();
732        let (workspace_entries, snapshot_full) = if workspace_snapshot_force_full {
733            let mut entries: Vec<WorkspaceEntry> = self
734                .workspace_values
735                .iter()
736                .map(|(name, value)| workspace_entry(name, value))
737                .collect();
738            entries.sort_by(|a, b| a.name.cmp(&b.name));
739            (entries, true)
740        } else if workspace_updates.is_empty() {
741            let source_map = if self.workspace_values.is_empty() {
742                &self.variables
743            } else {
744                &self.workspace_values
745            };
746            if source_map.is_empty() {
747                (workspace_updates, false)
748            } else {
749                let mut entries: Vec<WorkspaceEntry> = source_map
750                    .iter()
751                    .map(|(name, value)| workspace_entry(name, value))
752                    .collect();
753                entries.sort_by(|a, b| a.name.cmp(&b.name));
754                (entries, true)
755            }
756        } else {
757            (workspace_updates, false)
758        };
759        let workspace_snapshot = self.build_workspace_snapshot(workspace_entries, snapshot_full);
760        let figures_touched = runmat_runtime::plotting_hooks::take_recent_figures();
761        let stdin_events = stdin_events
762            .lock()
763            .map(|guard| guard.clone())
764            .unwrap_or_default();
765
766        let warnings = runmat_runtime::warning_store::take_all();
767
768        if let Some(runtime_error) = &mut error {
769            self.normalize_error_namespace(runtime_error);
770            self.populate_callstack(runtime_error);
771        }
772
773        let suppress_public_value =
774            is_expression_stmt && matches!(final_stmt_emit, FinalStmtEmitDisposition::Suppressed);
775        let public_value = if is_semicolon_suppressed || suppress_public_value {
776            None
777        } else {
778            result_value
779        };
780
781        self.format_mode = runmat_builtins::get_display_format();
782        Ok(ExecutionResult {
783            value: public_value,
784            execution_time_ms,
785            used_jit,
786            error,
787            type_info,
788            streams,
789            workspace: workspace_snapshot,
790            figures_touched,
791            warnings,
792            profiling: gather_profiling(execution_time_ms),
793            fusion_plan: fusion_snapshot,
794            stdin_events,
795        })
796    }
797
798    /// Interpret bytecode with persistent variable context
799    async fn interpret_with_context(
800        &mut self,
801        bytecode: &runmat_vm::Bytecode,
802    ) -> Result<runmat_vm::InterpreterOutcome, RuntimeError> {
803        let source_name = self.current_source_name().to_string();
804        runmat_vm::interpret_with_vars(
805            bytecode,
806            &mut self.variable_array,
807            Some(source_name.as_str()),
808        )
809        .await
810    }
811}