zshrs 0.11.40

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! Executor-callback hooks installed by `src/fusevm_bridge.rs` at
//! startup so code under `src/ported/` can dispatch to operations
//! that live on `ShellExecutor` (array/assoc storage, script eval,
//! function dispatch, command substitution) WITHOUT taking a direct
//! `ShellExecutor` reference.
//!
//! Direct `crate::fusevm_bridge::with_executor(|exec| exec.X())`
//! calls from `src/ported/` are forbidden (see memory:
//! feedback_no_exec_script_from_ported,
//! feedback_no_shellexecutor_in_ported). Each forbidden call site
//! now routes through one of the hooks below; the bridge supplies a
//! concrete fn-pointer at init time that closes over the
//! `with_executor` access pattern.
//!
//! Hooks are `OnceLock<fn(...) -> R>`. Calls before install return
//! the `unwrap_or_else` fallback (typically a no-op default).

use indexmap::IndexMap;
use std::sync::OnceLock;
/// `ArrayGetFn` type alias.
pub type ArrayGetFn = fn(&str) -> Option<Vec<String>>;
/// `AssocGetFn` type alias.
pub type AssocGetFn = fn(&str) -> Option<IndexMap<String, String>>;
/// `ArraySetFn` type alias.
pub type ArraySetFn = fn(&str, Vec<String>);
/// `AssocSetFn` type alias.
pub type AssocSetFn = fn(&str, IndexMap<String, String>);
/// `ScalarUnsetFn` type alias.
pub type ScalarUnsetFn = fn(&str);
/// `ArrayUnsetFn` type alias.
pub type ArrayUnsetFn = fn(&str);
/// `AssocUnsetFn` type alias.
pub type AssocUnsetFn = fn(&str);
/// `DispatchFunctionCallFn` type alias.
pub type DispatchFunctionCallFn = fn(&str, &[String]) -> Option<i32>;
/// Body-only function dispatch — runs the function body WITHOUT
/// the doshfunc scope wrap (locallevel/FUNCSTACK/BREAKS/etc.).
/// Used as the `body_runner` closure passed to direct
/// `crate::ported::exec::doshfunc(...)` callers (so they don't
/// double-wrap by going back through `dispatch_function_call`).
/// Returns the body's exit status. Mirrors C's `runshfunc(prog,
/// wrappers, name)` at `exec.c:6042` from doshfunc's perspective.
pub type RunFunctionBodyFn = fn(&str, &[String]) -> Option<i32>;
/// `ExecuteScriptFn` type alias.
pub type ExecuteScriptFn = fn(&str) -> Result<i32, String>;
/// `ExecuteScriptZshPipelineFn` type alias.
pub type ExecuteScriptZshPipelineFn = fn(&str) -> Result<i32, String>;
/// `RunCommandSubstitutionFn` type alias.
pub type RunCommandSubstitutionFn = fn(&str) -> String;
/// `PparamsGetFn` type alias.
pub type PparamsGetFn = fn() -> Vec<String>;
/// `PparamsSetFn` type alias.
pub type PparamsSetFn = fn(Vec<String>);
/// Drop a function from both the compiled-chunk and source maps.
/// Returns true if either entry existed.
pub type UnregisterFunctionFn = fn(&str) -> bool;

static ARRAY_GET: OnceLock<ArrayGetFn> = OnceLock::new();
static ASSOC_GET: OnceLock<AssocGetFn> = OnceLock::new();
static ARRAY_SET: OnceLock<ArraySetFn> = OnceLock::new();
static ASSOC_SET: OnceLock<AssocSetFn> = OnceLock::new();
static SCALAR_UNSET: OnceLock<ScalarUnsetFn> = OnceLock::new();
static ARRAY_UNSET: OnceLock<ArrayUnsetFn> = OnceLock::new();
static ASSOC_UNSET: OnceLock<AssocUnsetFn> = OnceLock::new();
static DISPATCH_FUNCTION_CALL: OnceLock<DispatchFunctionCallFn> = OnceLock::new();
static RUN_FUNCTION_BODY: OnceLock<RunFunctionBodyFn> = OnceLock::new();
static EXECUTE_SCRIPT: OnceLock<ExecuteScriptFn> = OnceLock::new();
static EXECUTE_SCRIPT_ZSH_PIPELINE: OnceLock<ExecuteScriptZshPipelineFn> = OnceLock::new();
static RUN_COMMAND_SUBSTITUTION: OnceLock<RunCommandSubstitutionFn> = OnceLock::new();
static PPARAMS_GET: OnceLock<PparamsGetFn> = OnceLock::new();
static PPARAMS_SET: OnceLock<PparamsSetFn> = OnceLock::new();
static UNREGISTER_FUNCTION: OnceLock<UnregisterFunctionFn> = OnceLock::new();
/// `install_array_get` — see implementation.
pub fn install_array_get(f: ArrayGetFn) {
    let _ = ARRAY_GET.set(f);
}
/// `install_assoc_get` — see implementation.
pub fn install_assoc_get(f: AssocGetFn) {
    let _ = ASSOC_GET.set(f);
}
/// `install_array_set` — see implementation.
pub fn install_array_set(f: ArraySetFn) {
    let _ = ARRAY_SET.set(f);
}
/// `install_assoc_set` — see implementation.
pub fn install_assoc_set(f: AssocSetFn) {
    let _ = ASSOC_SET.set(f);
}
/// `install_scalar_unset` — see implementation.
pub fn install_scalar_unset(f: ScalarUnsetFn) {
    let _ = SCALAR_UNSET.set(f);
}
/// `install_array_unset` — see implementation.
pub fn install_array_unset(f: ArrayUnsetFn) {
    let _ = ARRAY_UNSET.set(f);
}
/// `install_assoc_unset` — see implementation.
pub fn install_assoc_unset(f: AssocUnsetFn) {
    let _ = ASSOC_UNSET.set(f);
}
/// `install_dispatch_function_call` — see implementation.
pub fn install_dispatch_function_call(f: DispatchFunctionCallFn) {
    let _ = DISPATCH_FUNCTION_CALL.set(f);
}
/// `install_run_function_body` — see [`RunFunctionBodyFn`].
pub fn install_run_function_body(f: RunFunctionBodyFn) {
    let _ = RUN_FUNCTION_BODY.set(f);
}
/// `install_execute_script` — see implementation.
pub fn install_execute_script(f: ExecuteScriptFn) {
    let _ = EXECUTE_SCRIPT.set(f);
}
/// `install_execute_script_zsh_pipeline` — see implementation.
pub fn install_execute_script_zsh_pipeline(f: ExecuteScriptZshPipelineFn) {
    let _ = EXECUTE_SCRIPT_ZSH_PIPELINE.set(f);
}
/// `install_run_command_substitution` — see implementation.
pub fn install_run_command_substitution(f: RunCommandSubstitutionFn) {
    let _ = RUN_COMMAND_SUBSTITUTION.set(f);
}
/// `install_pparams_get` — see implementation.
pub fn install_pparams_get(f: PparamsGetFn) {
    let _ = PPARAMS_GET.set(f);
}
/// `install_pparams_set` — see implementation.
pub fn install_pparams_set(f: PparamsSetFn) {
    let _ = PPARAMS_SET.set(f);
}
/// `install_unregister_function` — see implementation.
pub fn install_unregister_function(f: UnregisterFunctionFn) {
    let _ = UNREGISTER_FUNCTION.set(f);
}
/// `array` — see implementation.
pub fn array(name: &str) -> Option<Vec<String>> {
    // Hook path (fusevm executor) when installed; otherwise fall
    // through to the direct param table at `params::getaparam` so
    // compsys/unit-test environments without an executor still see
    // shell-side arrays.
    if let Some(f) = ARRAY_GET.get() {
        if let Some(v) = f(name) {
            return Some(v);
        }
    }
    crate::ported::params::getaparam(name)
}
/// `assoc` — see implementation.
pub fn assoc(name: &str) -> Option<IndexMap<String, String>> {
    ASSOC_GET.get().and_then(|f| f(name))
}
/// `set_array` — see implementation.
pub fn set_array(name: &str, val: Vec<String>) {
    if let Some(f) = ARRAY_SET.get() {
        f(name, val);
    }
}
/// `set_assoc` — see implementation.
pub fn set_assoc(name: &str, val: IndexMap<String, String>) {
    if let Some(f) = ASSOC_SET.get() {
        f(name, val);
    }
}
/// `unset_scalar` — see implementation.
pub fn unset_scalar(name: &str) {
    if let Some(f) = SCALAR_UNSET.get() {
        f(name);
    }
}
/// `unset_array` — see implementation.
pub fn unset_array(name: &str) {
    if let Some(f) = ARRAY_UNSET.get() {
        f(name);
    }
}
/// `unset_assoc` — see implementation.
pub fn unset_assoc(name: &str) {
    if let Some(f) = ASSOC_UNSET.get() {
        f(name);
    }
}
/// `dispatch_function_call` — see implementation.
pub fn dispatch_function_call(name: &str, args: &[String]) -> Option<i32> {
    DISPATCH_FUNCTION_CALL.get().and_then(|f| f(name, args))
}
/// Body-only dispatch — call as the `body_runner` of a direct
/// `crate::ported::exec::doshfunc(...)` invocation to avoid the
/// double-wrap that would happen if the body_runner went back
/// through [`dispatch_function_call`].
pub fn run_function_body(name: &str, args: &[String]) -> Option<i32> {
    RUN_FUNCTION_BODY.get().and_then(|f| f(name, args))
}
/// `execute_script` — see implementation.
pub fn execute_script(src: &str) -> Result<i32, String> {
    match EXECUTE_SCRIPT.get() {
        Some(f) => f(src),
        None => Ok(0),
    }
}
/// `execute_script_zsh_pipeline` — see implementation.
pub fn execute_script_zsh_pipeline(src: &str) -> Result<i32, String> {
    match EXECUTE_SCRIPT_ZSH_PIPELINE.get() {
        Some(f) => f(src),
        None => Ok(0),
    }
}
/// `run_command_substitution` — see implementation.
pub fn run_command_substitution(cmd: &str) -> String {
    match RUN_COMMAND_SUBSTITUTION.get() {
        Some(f) => f(cmd),
        None => String::new(),
    }
}
/// `pparams` — see implementation.
pub fn pparams() -> Vec<String> {
    PPARAMS_GET.get().map(|f| f()).unwrap_or_default()
}
/// `set_pparams` — see implementation.
pub fn set_pparams(v: Vec<String>) {
    if let Some(f) = PPARAMS_SET.get() {
        f(v);
    }
}
/// `unregister_function` — see implementation.
pub fn unregister_function(name: &str) -> bool {
    UNREGISTER_FUNCTION.get().map(|f| f(name)).unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ─── zsh-corpus pins: default (no-hook) fallback behavior ─────

    /// `dispatch_function_call` returns None when no hook installed.
    /// Tests may run in a fresh process where no fusevm bridge wired
    /// the dispatch yet; pin: no-panic, None-return.
    #[test]
    fn exec_hooks_corpus_dispatch_returns_none_when_not_installed() {
        let _g = crate::test_util::global_state_lock();
        // We can't unset OnceLock once set, but if test runs first
        // in this process it should be None. The defensive pin is:
        // either None or Some — never panic.
        let _ = dispatch_function_call("__never_a_real_function_zshrs__", &["a".into()]);
        // No panic = pass.
    }

    /// `execute_script` returns `Ok(0)` when no hook installed.
    #[test]
    fn exec_hooks_corpus_execute_script_returns_ok_zero_when_not_installed() {
        let _g = crate::test_util::global_state_lock();
        let r = execute_script("nothing real");
        match r {
            Ok(_) | Err(_) => {} // either is acceptable post-install
        }
    }

    /// `run_command_substitution` returns "" by default.
    #[test]
    fn exec_hooks_corpus_run_command_substitution_default_empty_or_real() {
        let _g = crate::test_util::global_state_lock();
        // Returns "" if no hook, or real output if hook installed.
        let _ = run_command_substitution("echo zshrs_hook_test");
        // No panic = pass; we can't pin exact result because hook
        // state depends on previous tests in same process.
    }

    /// `array` falls back to params::getaparam when no hook.
    /// Set a real array via params, then look up through hook entry.
    #[test]
    fn exec_hooks_corpus_array_falls_back_to_getaparam() {
        let _g = crate::test_util::global_state_lock();
        crate::ported::params::unsetparam("EH_FB");
        crate::ported::params::setaparam("EH_FB", vec!["x".into(), "y".into(), "z".into()]);
        let got = array("EH_FB");
        assert_eq!(
            got.as_deref(),
            Some(&["x".to_string(), "y".to_string(), "z".to_string()][..]),
            "array() hook falls back to params::getaparam",
        );
        crate::ported::params::unsetparam("EH_FB");
    }

    /// `pparams()` returns empty Vec when no hook installed.
    #[test]
    fn exec_hooks_corpus_pparams_returns_empty_when_not_installed() {
        let _g = crate::test_util::global_state_lock();
        let p = pparams();
        // Either empty (no hook) or whatever the installed hook returns.
        let _ = p; // no panic = pass
    }

    /// `unregister_function` returns false by default.
    #[test]
    fn exec_hooks_corpus_unregister_function_default_false() {
        let _g = crate::test_util::global_state_lock();
        let r = unregister_function("__never_registered_xyz__");
        // If hook installed, hook decides; if not, returns false.
        // Pin: doesn't panic and returns a bool.
        let _ = r;
    }

    /// `set_pparams` doesn't panic when called.
    #[test]
    fn exec_hooks_corpus_set_pparams_does_not_panic() {
        let _g = crate::test_util::global_state_lock();
        set_pparams(vec!["a".into(), "b".into()]);
        // No panic = pass.
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional default-path (no-hook-installed) parity tests.
    // exec_hooks fallback semantics must remain stable: every accessor
    // returns a safe default when no fusevm executor has wired its hook.
    // ═══════════════════════════════════════════════════════════════════

    /// `assoc()` returns None when no hook installed (no params
    /// fallback — assoc has no equivalent of getaparam fallback).
    #[test]
    fn exec_hooks_assoc_returns_none_when_no_hook() {
        let _g = crate::test_util::global_state_lock();
        // If a prior test installed a hook, the result is hook-defined;
        // we only pin no-panic + valid Option<...>.
        let _ = assoc("__never_real_assoc_zshrs__");
    }

    /// `set_array` is a no-op when no hook installed (silently
    /// drops the write rather than panicking — fusevm-less env safe).
    #[test]
    fn exec_hooks_set_array_no_hook_does_not_panic() {
        let _g = crate::test_util::global_state_lock();
        set_array("__never_real_array_zshrs__", vec!["x".into(), "y".into()]);
    }

    /// `set_assoc` is a no-op when no hook installed.
    #[test]
    fn exec_hooks_set_assoc_no_hook_does_not_panic() {
        let _g = crate::test_util::global_state_lock();
        let mut m = IndexMap::new();
        m.insert("k".to_string(), "v".to_string());
        set_assoc("__never_real_assoc_zshrs__", m);
    }

    /// `unset_scalar`, `unset_array`, `unset_assoc` are all no-ops
    /// when no hook installed.
    #[test]
    fn exec_hooks_unset_variants_no_hook_dont_panic() {
        let _g = crate::test_util::global_state_lock();
        unset_scalar("__never_real_scalar_zshrs__");
        unset_array("__never_real_array_zshrs__");
        unset_assoc("__never_real_assoc_zshrs__");
    }

    /// `run_function_body` returns None when no hook installed.
    #[test]
    fn exec_hooks_run_function_body_returns_none_when_no_hook() {
        let _g = crate::test_util::global_state_lock();
        let _ = run_function_body("__never_a_real_fn_zshrs__", &["a".into()]);
        // No panic = pass; result is Option-typed.
    }

    /// `execute_script_zsh_pipeline` returns Ok(0) when no hook installed.
    #[test]
    fn exec_hooks_execute_script_zsh_pipeline_default_ok_zero() {
        let _g = crate::test_util::global_state_lock();
        let r = execute_script_zsh_pipeline("no hook");
        // If hook installed, result is hook-defined; if not, Ok(0).
        let _ = r;
    }

    /// `array()` returns None when name doesn't exist in params either
    /// (no fallback hits).
    #[test]
    fn exec_hooks_array_returns_none_for_missing_name() {
        let _g = crate::test_util::global_state_lock();
        crate::ported::params::unsetparam("__no_such_array_zshrs__");
        let got = array("__no_such_array_zshrs__");
        // Either None (no hook + no param) or hook-returned value.
        let _ = got;
    }

    /// `array()` empty name doesn't panic (some callers pass "" for
    /// special parameter probes).
    #[test]
    fn exec_hooks_array_empty_name_does_not_panic() {
        let _g = crate::test_util::global_state_lock();
        let _ = array("");
    }

    /// Idempotent: calling array() twice with same name yields same
    /// result (no observable side effect on the fallback path).
    #[test]
    fn exec_hooks_array_is_idempotent() {
        let _g = crate::test_util::global_state_lock();
        crate::ported::params::unsetparam("EH_IDEMPOTENT");
        crate::ported::params::setaparam("EH_IDEMPOTENT", vec!["a".into(), "b".into()]);
        let first = array("EH_IDEMPOTENT");
        let second = array("EH_IDEMPOTENT");
        assert_eq!(first, second);
        crate::ported::params::unsetparam("EH_IDEMPOTENT");
    }

    /// `pparams()` returns an empty vec (not None) when no hook —
    /// callers can iterate without an Option-check.
    #[test]
    fn exec_hooks_pparams_returns_vec_not_none() {
        let _g = crate::test_util::global_state_lock();
        // Type assertion: result is Vec<String>, not Option<Vec<String>>.
        let p: Vec<String> = pparams();
        let _ = p; // either [] or hook-installed value
    }

    /// Round-trip via params fallback: set then read returns same value.
    #[test]
    fn exec_hooks_array_set_get_roundtrip_via_params() {
        let _g = crate::test_util::global_state_lock();
        crate::ported::params::unsetparam("EH_RT");
        let vals = vec!["one".to_string(), "two".to_string(), "three".to_string()];
        crate::ported::params::setaparam("EH_RT", vals.clone());
        let got = array("EH_RT").expect("set then get should hit params fallback");
        assert_eq!(got, vals);
        crate::ported::params::unsetparam("EH_RT");
    }

    /// `unregister_function` is consistently a bool — pin the return
    /// type so accidental refactor to () would fail the type check.
    #[test]
    fn exec_hooks_unregister_function_returns_bool() {
        let _g = crate::test_util::global_state_lock();
        let r: bool = unregister_function("__never_xyz_zshrs__");
        let _ = r;
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional contract-pin tests for exec_hooks default behavior.
    // ═══════════════════════════════════════════════════════════════════

    /// `run_command_substitution` returns String (never None / never Option).
    #[test]
    fn exec_hooks_run_command_substitution_returns_string_type() {
        let _g = crate::test_util::global_state_lock();
        let _: String = run_command_substitution("anything");
    }

    /// `execute_script` returns Result<i32, String>. Pin signature.
    #[test]
    fn exec_hooks_execute_script_returns_result_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Result<i32, String> = execute_script("anything");
    }

    /// `execute_script_zsh_pipeline` returns Result<i32, String>.
    #[test]
    fn exec_hooks_execute_script_zsh_pipeline_returns_result_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Result<i32, String> = execute_script_zsh_pipeline("anything");
    }

    /// `dispatch_function_call` returns Option<i32>.
    #[test]
    fn exec_hooks_dispatch_function_call_returns_option_i32() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<i32> = dispatch_function_call("__never_real__", &[]);
    }

    /// `run_function_body` returns Option<i32>.
    #[test]
    fn exec_hooks_run_function_body_returns_option_i32() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<i32> = run_function_body("__never_real__", &[]);
    }

    /// `array` returns Option<Vec<String>>.
    #[test]
    fn exec_hooks_array_returns_option_vec_string() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<Vec<String>> = array("anything");
    }

    /// `assoc` returns Option<IndexMap<String, String>>.
    #[test]
    fn exec_hooks_assoc_returns_option_indexmap() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<IndexMap<String, String>> = assoc("anything");
    }

    /// Empty-string args to `dispatch_function_call` doesn't panic.
    #[test]
    fn exec_hooks_dispatch_empty_args_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _ = dispatch_function_call("", &[]);
        let _ = dispatch_function_call("name", &[]);
    }

    /// Empty-string args to `run_function_body` doesn't panic.
    #[test]
    fn exec_hooks_run_function_body_empty_args_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _ = run_function_body("", &[]);
        let _ = run_function_body("name", &[]);
    }

    /// Empty-string name to `execute_script` doesn't panic and returns
    /// Result.
    #[test]
    fn exec_hooks_execute_script_empty_src_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _ = execute_script("");
        let _ = execute_script_zsh_pipeline("");
    }

    /// Empty-string cmd to `run_command_substitution` doesn't panic.
    #[test]
    fn exec_hooks_run_command_substitution_empty_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _: String = run_command_substitution("");
    }

    /// `unregister_function("")` doesn't panic.
    #[test]
    fn exec_hooks_unregister_function_empty_name_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _: bool = unregister_function("");
    }

    /// `unset_*` with empty name does not panic.
    #[test]
    fn exec_hooks_unset_with_empty_name_no_panic() {
        let _g = crate::test_util::global_state_lock();
        unset_scalar("");
        unset_array("");
        unset_assoc("");
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional contract-pin tests for exec_hooks fallback semantics
    // c:213 pparams / c:217 set_pparams / c:223 unregister_function
    // ═══════════════════════════════════════════════════════════════════

    /// `pparams()` is deterministic for repeated calls without state changes.
    #[test]
    fn pparams_deterministic_without_changes() {
        let _g = crate::test_util::global_state_lock();
        let first = pparams();
        for _ in 0..3 {
            assert_eq!(
                pparams(),
                first,
                "pparams() must be deterministic across reads"
            );
        }
    }

    /// `pparams()` returns Vec<String> (not Option) — pin type contract.
    #[test]
    fn pparams_returns_vec_string_no_option() {
        let _g = crate::test_util::global_state_lock();
        let _: Vec<String> = pparams();
    }

    /// `array(name)` with name containing null-byte doesn't panic.
    #[test]
    fn array_with_special_chars_in_name_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _ = array("name with spaces");
        let _ = array("name/with/slashes");
        let _ = array("$dollarsigns");
    }

    /// `assoc(name)` empty name no panic.
    #[test]
    fn assoc_empty_name_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _ = assoc("");
    }

    /// `set_array(empty, ...)` empty name no panic.
    #[test]
    fn set_array_empty_name_no_panic() {
        let _g = crate::test_util::global_state_lock();
        set_array("", vec![]);
    }

    /// `set_assoc(empty, ...)` empty name no panic.
    #[test]
    fn set_assoc_empty_name_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let m = indexmap::IndexMap::new();
        set_assoc("", m);
    }

    /// `set_pparams(empty)` is safe.
    #[test]
    fn set_pparams_empty_vec_no_panic() {
        let _g = crate::test_util::global_state_lock();
        set_pparams(vec![]);
    }

    /// Repeated `pparams()` doesn't allocate growing state.
    #[test]
    fn pparams_repeated_doesnt_grow_state() {
        let _g = crate::test_util::global_state_lock();
        let first_len = pparams().len();
        for _ in 0..10 {
            let n = pparams().len();
            assert_eq!(n, first_len, "len must not grow across reads");
        }
    }

    /// `unregister_function` is deterministic for nonexistent name.
    #[test]
    fn unregister_function_unknown_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let first = unregister_function("__never_real_xyz__");
        for _ in 0..3 {
            assert_eq!(unregister_function("__never_real_xyz__"), first);
        }
    }

    /// `array(name)` repeated reads of nonexistent name are deterministic.
    #[test]
    fn array_unknown_is_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let first = array("__never_real_array_xyz__").is_none();
        for _ in 0..3 {
            assert_eq!(array("__never_real_array_xyz__").is_none(), first);
        }
    }

    /// `assoc(name)` repeated reads of nonexistent name are deterministic.
    #[test]
    fn assoc_unknown_is_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let first = assoc("__never_real_assoc_xyz__").is_none();
        for _ in 0..3 {
            assert_eq!(assoc("__never_real_assoc_xyz__").is_none(), first);
        }
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional C-parity tests for src/ported/exec_hooks.rs
    // c:134 array / c:147 assoc / c:181 dispatch_function_call /
    // c:188 run_function_body / c:192 execute_script /
    // c:206 run_command_substitution / c:213 pparams / c:223 unregister_function
    // ═══════════════════════════════════════════════════════════════════

    /// `array(name)` returns Option<Vec<String>> (compile-time pin).
    #[test]
    fn array_returns_option_vec_string_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<Vec<String>> = array("any");
    }

    /// `assoc(name)` returns Option<IndexMap<String,String>> (compile-time pin).
    #[test]
    fn assoc_returns_option_indexmap_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<indexmap::IndexMap<String, String>> = assoc("any");
    }

    /// `dispatch_function_call` returns Option<i32> (compile-time pin).
    #[test]
    fn dispatch_function_call_returns_option_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<i32> = dispatch_function_call("__never__", &[]);
    }

    /// `run_function_body` returns Option<i32> (compile-time pin).
    #[test]
    fn run_function_body_returns_option_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<i32> = run_function_body("__never__", &[]);
    }

    /// `execute_script` returns Result<i32, String> (compile-time pin).
    #[test]
    fn execute_script_returns_result_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Result<i32, String> = execute_script("");
    }

    /// `execute_script_zsh_pipeline` returns Result<i32, String> (compile-time pin).
    #[test]
    fn execute_script_zsh_pipeline_returns_result_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Result<i32, String> = execute_script_zsh_pipeline("");
    }

    /// `run_command_substitution` returns String (compile-time pin).
    #[test]
    fn run_command_substitution_returns_string_type() {
        let _g = crate::test_util::global_state_lock();
        let _: String = run_command_substitution("");
    }

    /// `pparams` returns Vec<String> (compile-time pin).
    #[test]
    fn pparams_returns_vec_string_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Vec<String> = pparams();
    }

    /// `unregister_function` returns bool (compile-time pin).
    #[test]
    fn unregister_function_returns_bool_type() {
        let _g = crate::test_util::global_state_lock();
        let _: bool = unregister_function("__never__");
    }

    /// `unset_scalar`/`unset_array`/`unset_assoc` for nonexistent name safe.
    #[test]
    fn unset_variants_nonexistent_no_panic() {
        let _g = crate::test_util::global_state_lock();
        unset_scalar("__never_unset_scalar__");
        unset_array("__never_unset_array__");
        unset_assoc("__never_unset_assoc__");
    }

    /// `set_pparams` with no hook installed is a silent no-op
    /// (c:217-220 — `if let Some(f) = PPARAMS_SET.get() { f(v); }`).
    /// Pin the no-hook contract so a refactor that panics on missing
    /// hook gets caught.
    #[test]
    fn set_pparams_without_hook_is_silent_noop() {
        let _g = crate::test_util::global_state_lock();
        // No hook installed in test context — must not panic.
        set_pparams(vec!["a".into(), "b".into(), "c".into()]);
        set_pparams(vec![]);
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional contract pins for exec_hooks.rs
    // No-hook-installed contract: every accessor must be safe + deterministic
    // ═══════════════════════════════════════════════════════════════════

    /// `array("")` empty name returns deterministic value (no panic).
    #[test]
    fn array_empty_name_no_panic_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let a = array("");
        let b = array("");
        assert_eq!(a, b, "array(\"\") must be deterministic");
    }

    /// `set_array` then `array` without hook should not panic.
    #[test]
    fn set_array_then_get_no_hook_safe() {
        let _g = crate::test_util::global_state_lock();
        set_array("__test_hook_arr__", vec!["a".into(), "b".into()]);
        let _ = array("__test_hook_arr__");
    }

    /// `set_assoc` then `assoc` without hook should not panic.
    #[test]
    fn set_assoc_then_get_no_hook_safe() {
        let _g = crate::test_util::global_state_lock();
        let mut m = IndexMap::new();
        m.insert("k".to_string(), "v".to_string());
        set_assoc("__test_hook_assoc__", m);
        let _ = assoc("__test_hook_assoc__");
    }

    /// `run_function_body` with no hook returns `Option<i32>` (type pin, alt).
    #[test]
    fn run_function_body_returns_option_i32_type_alt() {
        let _g = crate::test_util::global_state_lock();
        let _: Option<i32> = run_function_body("foo", &[]);
    }

    /// `run_function_body` is deterministic for the same input when no hook.
    #[test]
    fn run_function_body_no_hook_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let a = run_function_body("__never__", &[]);
        let b = run_function_body("__never__", &[]);
        assert_eq!(a, b);
    }

    /// `dispatch_function_call` is deterministic across calls.
    #[test]
    fn dispatch_function_call_no_hook_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let a = dispatch_function_call("__never__", &[]);
        let b = dispatch_function_call("__never__", &[]);
        assert_eq!(a, b);
    }

    /// `pparams` returns `Vec<String>` (compile-time pin, alt).
    #[test]
    fn pparams_returns_vec_string_type_alt() {
        let _g = crate::test_util::global_state_lock();
        let _: Vec<String> = pparams();
    }

    /// `pparams` is deterministic across repeated reads when no hook installed
    /// (this is observably true only if no other test has installed it; pin
    /// the no-mutation invariant).
    #[test]
    fn pparams_repeated_reads_are_observable_type() {
        let _g = crate::test_util::global_state_lock();
        // Just type pin — value depends on whether another test installed a
        // hook between these calls (PPARAMS_GET is OnceLock).
        let _a = pparams();
        let _b = pparams();
    }

    /// `run_command_substitution` returns `String` type pin (alt).
    #[test]
    fn run_command_substitution_returns_string_type_alt() {
        let _g = crate::test_util::global_state_lock();
        let _: String = run_command_substitution("echo x");
    }

    /// `run_command_substitution` empty command doesn't panic.
    #[test]
    fn run_command_substitution_empty_cmd_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let _ = run_command_substitution("");
    }

    /// `execute_script` returns `Result<i32, String>` type pin.
    #[test]
    fn execute_script_returns_result_i32_string_type() {
        let _g = crate::test_util::global_state_lock();
        let _: Result<i32, String> = execute_script("foo");
    }

    /// `unregister_function("")` empty name returns bool safely.
    #[test]
    fn unregister_function_empty_name_returns_bool() {
        let _g = crate::test_util::global_state_lock();
        let _: bool = unregister_function("");
    }
}