neovm-core 0.0.1

Core runtime structures for NeoVM
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
//! Portable dumper (pdump) for NeoVM.
//!
//! Serializes the post-bootstrap `Context` state to a binary file using
//! serde + bincode, then deserializes on startup to skip the 3-5s bootstrap.
//!
//! File format:
//! ```text
//! [8 bytes: magic "NEOPDUMP"]
//! [4 bytes: format version u32 LE]
//! [32 bytes: SHA-256 of bincode payload]
//! [4 bytes: payload length u32 LE]
//! [N bytes: bincode-serialized DumpContextState]
//! ```

pub mod convert;
pub mod runtime;
pub mod types;

use std::io::{Read, Write};
use std::path::Path;

use sha2::{Digest, Sha256};

use self::convert::*;
use self::runtime::*;
use self::types::DumpContextState;
use crate::emacs_core::charset::{
    CharsetRegistrySnapshot, restore_charset_registry, snapshot_charset_registry,
};
use crate::emacs_core::eval::Context;
use crate::emacs_core::fontset::{
    FontsetRegistrySnapshot, restore_fontset_registry, snapshot_fontset_registry,
};
use crate::emacs_core::intern;
use crate::emacs_core::value;

const MAGIC: &[u8; 8] = b"NEOPDUMP";
const FORMAT_VERSION: u32 = 10;

/// Errors from dump/load operations.
#[derive(Debug)]
pub enum DumpError {
    Io(std::io::Error),
    BadMagic,
    UnsupportedVersion(u32),
    ChecksumMismatch,
    SerializationError(String),
    DeserializationError(String),
}

impl std::fmt::Display for DumpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DumpError::Io(e) => write!(f, "I/O error: {e}"),
            DumpError::BadMagic => write!(f, "not a valid pdump file (bad magic)"),
            DumpError::UnsupportedVersion(v) => write!(f, "unsupported pdump version {v}"),
            DumpError::ChecksumMismatch => write!(f, "pdump checksum mismatch (corrupted file)"),
            DumpError::SerializationError(s) => write!(f, "serialization error: {s}"),
            DumpError::DeserializationError(s) => write!(f, "deserialization error: {s}"),
        }
    }
}

impl std::error::Error for DumpError {}

impl From<std::io::Error> for DumpError {
    fn from(e: std::io::Error) -> Self {
        DumpError::Io(e)
    }
}

/// Thread-local semantic runtime state that must be restored when switching
/// back from a cloned evaluator to the live evaluator on the same thread.
#[derive(Clone, Debug)]
pub struct ActiveRuntimeSnapshot {
    charset_registry: CharsetRegistrySnapshot,
    fontset_registry: FontsetRegistrySnapshot,
}

/// Serialize the evaluator state to a pdump file.
pub fn dump_to_file(eval: &Context, path: &Path) -> Result<(), DumpError> {
    let state = dump_evaluator(eval);

    let payload =
        bincode::serialize(&state).map_err(|e| DumpError::SerializationError(e.to_string()))?;

    let mut hasher = Sha256::new();
    hasher.update(&payload);
    let checksum = hasher.finalize();

    let parent = path.parent().unwrap_or_else(|| Path::new("."));
    let mut file = tempfile::NamedTempFile::new_in(parent)?;
    file.write_all(MAGIC)?;
    file.write_all(&FORMAT_VERSION.to_le_bytes())?;
    file.write_all(&checksum)?;
    file.write_all(&(payload.len() as u32).to_le_bytes())?;
    file.write_all(&payload)?;
    file.flush()?;
    file.as_file().sync_all()?;

    match file.persist(path) {
        Ok(_) => Ok(()),
        Err(err) => {
            if err.error.kind() == std::io::ErrorKind::AlreadyExists && path.exists() {
                Ok(())
            } else {
                Err(DumpError::Io(err.error))
            }
        }
    }
}

/// Load evaluator state from a pdump file.
///
/// This reconstructs a full `Context` from the serialized state,
/// setting up thread-local pointers and resetting caches.
pub fn load_from_dump(path: &Path) -> Result<Context, DumpError> {
    let load_start = std::time::Instant::now();
    let mut file = std::fs::File::open(path)?;

    // Read and validate header
    let mut magic = [0u8; 8];
    file.read_exact(&mut magic)?;
    if &magic != MAGIC {
        return Err(DumpError::BadMagic);
    }

    let mut version_bytes = [0u8; 4];
    file.read_exact(&mut version_bytes)?;
    let version = u32::from_le_bytes(version_bytes);
    if version != FORMAT_VERSION {
        return Err(DumpError::UnsupportedVersion(version));
    }

    let mut expected_checksum = [0u8; 32];
    file.read_exact(&mut expected_checksum)?;

    let mut len_bytes = [0u8; 4];
    file.read_exact(&mut len_bytes)?;
    let payload_len = u32::from_le_bytes(len_bytes) as usize;

    let mut payload = vec![0u8; payload_len];
    file.read_exact(&mut payload)?;

    // Validate checksum
    let mut hasher = Sha256::new();
    hasher.update(&payload);
    let actual_checksum = hasher.finalize();
    if actual_checksum.as_slice() != &expected_checksum {
        return Err(DumpError::ChecksumMismatch);
    }

    // Deserialize
    let state: types::DumpContextState = bincode::deserialize(&payload)
        .map_err(|e| DumpError::DeserializationError(e.to_string()))?;

    // Reconstruct evaluator
    let mut eval = reconstruct_evaluator(&state)?;
    record_loaded_dump(path, load_start.elapsed());
    run_after_pdump_load_hook(&mut eval);
    Ok(eval)
}

/// Clone a live evaluator through the pdump conversion pipeline.
///
/// This gives bootstrap/load code an isolated working evaluator with the same
/// logical runtime state, without sharing heap objects that can be mutated
/// during eager macroexpansion.
pub fn snapshot_evaluator(eval: &Context) -> DumpContextState {
    dump_evaluator(eval)
}

/// Snapshot an evaluator after activating its thread-local runtime bindings.
///
/// Use this entry point when multiple `Context`s may share the current thread.
/// The pdump conversion pipeline relies on thread-local tagged-heap state, so
/// the source evaluator must be active before we walk its heap-backed values.
pub fn snapshot_active_evaluator(eval: &mut Context) -> DumpContextState {
    eval.setup_thread_locals();
    dump_evaluator(eval)
}

/// Snapshot thread-local semantic runtime registries for the active evaluator.
///
/// Cloning an evaluator through pdump reconstructs these registries for the
/// cloned heap. Callers that later switch the current thread back to the live
/// evaluator must restore this snapshot as part of runtime reactivation.
pub fn snapshot_active_runtime(eval: &mut Context) -> ActiveRuntimeSnapshot {
    eval.setup_thread_locals();
    ActiveRuntimeSnapshot {
        charset_registry: snapshot_charset_registry(),
        fontset_registry: snapshot_fontset_registry(),
    }
}

/// Reactivate a live evaluator after using a cloned evaluator on the same
/// thread, restoring thread-local semantic registries alongside heap state.
pub fn restore_active_runtime(eval: &mut Context, snapshot: &ActiveRuntimeSnapshot) {
    eval.setup_thread_locals();
    restore_charset_registry(snapshot.charset_registry.clone());
    restore_fontset_registry(snapshot.fontset_registry.clone());
    eval.sync_thread_runtime_bindings();
    eval.sync_current_thread_buffer_state();
}

/// Reconstruct an evaluator from a previously captured in-memory pdump snapshot.
pub fn restore_snapshot(state: &DumpContextState) -> Result<Context, DumpError> {
    reconstruct_evaluator(state)
}

/// Clone a live evaluator through the pdump conversion pipeline.
///
/// Prefer `snapshot_evaluator` + `restore_snapshot` when cloning the same
/// template repeatedly; that avoids rebuilding the intermediate dump state.
pub fn clone_evaluator(eval: &Context) -> Result<Context, DumpError> {
    restore_snapshot(&snapshot_evaluator(eval))
}

/// Clone an evaluator after activating its thread-local runtime bindings.
///
/// Use this when cloning from a live runtime that shares the current thread
/// with other `Context`s.
pub fn clone_active_evaluator(eval: &mut Context) -> Result<Context, DumpError> {
    restore_snapshot(&snapshot_active_evaluator(eval))
}

/// Reconstruct an `Context` from deserialized dump state.
fn reconstruct_evaluator(state: &DumpContextState) -> Result<Context, DumpError> {
    // 1. Reconstruct the global append-only symbol table before any values that
    // refer to SymIds are loaded.
    load_interner(&state.interner);

    // 2. Reconstruct the tagged heap before any heap-backed value/object loads
    // so tagged dump references can resolve directly to live tagged objects.
    let mut tagged_heap = Box::new(crate::tagged::gc::TaggedHeap::new());
    crate::tagged::gc::set_tagged_heap(&mut tagged_heap);
    preload_tagged_heap(&state.tagged_heap)?;

    // 3. Reset thread-local runtime caches before replaying semantic state.
    reset_runtime_for_new_heap(HeapResetMode::PdumpRestore);

    // 4b. Restore thread-local registries whose contents are semantic runtime
    // state, not disposable caches.
    load_charset_registry(&state.charset_registry);
    load_fontset_registry(&state.fontset_registry);

    // 5. Reconstruct all subsystems
    let obarray = load_obarray(&state.obarray);
    let lexenv = load_value(&state.lexenv);
    let features: Vec<_> = state.features.iter().map(|id| intern::SymId(*id)).collect();
    let require_stack: Vec<_> = state
        .require_stack
        .iter()
        .map(|id| intern::SymId(*id))
        .collect();
    let loads_in_progress: Vec<_> = state
        .loads_in_progress
        .iter()
        .map(std::path::PathBuf::from)
        .collect();

    let eval = Context::from_dump(
        tagged_heap,
        obarray,
        lexenv,
        features,
        require_stack,
        loads_in_progress,
        load_buffer_manager(&state.buffers),
        load_autoload_manager(&state.autoloads),
        load_custom_manager(&state.custom),
        load_mode_registry(&state.modes),
        load_coding_system_manager(&state.coding_systems),
        load_face_table(&state.face_table),
        load_abbrev_manager(&state.abbrevs),
        load_interactive_registry(&state.interactive),
        load_rectangle(&state.rectangle),
        load_value(&state.standard_syntax_table),
        load_value(&state.standard_category_table),
        load_value(&state.current_local_map),
        load_kmacro(&state.kmacro),
        load_register_manager(&state.registers),
        load_bookmark_manager(&state.bookmarks),
        load_watcher_list(&state.watchers),
    );

    finish_preload_tagged_heap();

    Ok(eval)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::emacs_core::intern::intern;
    use crate::emacs_core::pdump::types::{
        DumpByteCodeFunction, DumpHeapObject, DumpLambdaParams, DumpOp,
    };
    use crate::emacs_core::value::Value;

    #[test]
    fn test_pdump_round_trip_basic() {
        crate::test_utils::init_test_tracing();
        // Create a minimal evaluator
        let mut eval = Context::new();

        // Set a symbol value to verify round-trip
        eval.obarray
            .set_symbol_value("test-pdump-var", Value::fixnum(42));

        // Dump to temp file
        let dir = tempfile::tempdir().unwrap();
        let dump_path = dir.path().join("test.pdump");
        dump_to_file(&eval, &dump_path).expect("dump should succeed");

        // Load from dump
        let loaded = load_from_dump(&dump_path).expect("load should succeed");

        // Verify the symbol value survived
        assert_eq!(
            loaded.obarray.symbol_value("test-pdump-var"),
            Some(&Value::fixnum(42))
        );
    }

    #[test]
    fn test_clone_active_evaluator_preserves_in_progress_require_and_load_state() {
        crate::test_utils::init_test_tracing();
        let mut eval = Context::new();
        eval.require_stack.push(intern("cl-macs"));
        eval.loads_in_progress.push(std::path::PathBuf::from(
            "/tmp/neomacs-pdump-clone-in-progress.el",
        ));

        let cloned = clone_active_evaluator(&mut eval).expect("clone should succeed");

        assert_eq!(cloned.require_stack, vec![intern("cl-macs")]);
        assert_eq!(
            cloned.loads_in_progress,
            vec![std::path::PathBuf::from(
                "/tmp/neomacs-pdump-clone-in-progress.el"
            )]
        );
    }

    #[test]
    fn test_restore_active_runtime_after_clone_reinstalls_live_charset_registry() {
        crate::test_utils::init_test_tracing();
        crate::emacs_core::charset::reset_charset_registry();

        let mut eval = Context::new();
        let mut args = vec![value::Value::NIL; 17];
        args[0] = value::Value::symbol("charset-pdump-clone-restore-test");
        args[1] = value::Value::fixnum(1);
        args[2] = value::Value::vector(vec![value::Value::fixnum(0), value::Value::fixnum(127)]);
        args[16] = value::Value::list(vec![
            value::Value::symbol("doc"),
            value::Value::string("live charset registry should survive clone handoff"),
        ]);
        crate::emacs_core::charset::builtin_define_charset_internal(args).unwrap();

        let live_runtime = snapshot_active_runtime(&mut eval);
        let cloned = clone_active_evaluator(&mut eval).expect("first clone should succeed");
        restore_active_runtime(&mut eval, &live_runtime);
        drop(cloned);

        let cloned_again = clone_active_evaluator(&mut eval).expect("second clone should succeed");
        restore_active_runtime(&mut eval, &live_runtime);
        drop(cloned_again);

        let registry = crate::emacs_core::charset::snapshot_charset_registry();
        let entry = registry
            .charsets
            .iter()
            .find(|info| info.name == "charset-pdump-clone-restore-test")
            .expect("restored charset entry");
        assert_eq!(
            entry.plist,
            vec![(
                "doc".to_string(),
                value::Value::string("live charset registry should survive clone handoff"),
            )]
        );
    }

    #[test]
    fn test_file_load_records_pdumper_stats_and_runs_after_pdump_load_hook() {
        crate::test_utils::init_test_tracing();
        let mut eval = Context::new();
        let setup = crate::emacs_core::value_reader::read_all(
            "(progn
               (setq compat-pdump-hook-fired nil)
               (setq after-pdump-load-hook
                     (list (lambda () (setq compat-pdump-hook-fired t)))))",
        )
        .unwrap();
        eval.eval_sub(setup[0])
            .expect("setup hook should evaluate");

        let dir = tempfile::tempdir().unwrap();
        let dump_path = dir.path().join("stats-and-hook.pdump");
        dump_to_file(&eval, &dump_path).expect("dump should succeed");
        drop(eval);

        let mut loaded = load_from_dump(&dump_path).expect("load should succeed");
        assert_eq!(
            loaded.obarray.symbol_value("compat-pdump-hook-fired"),
            Some(&Value::T)
        );

        let forms = crate::emacs_core::value_reader::read_all("(pdumper-stats)").unwrap();
        let stats = loaded
            .eval_sub(forms[0])
            .expect("pdumper-stats should evaluate");
        assert!(stats.is_cons(), "pdumper-stats should return an alist");

        let dumped_with = stats.cons_car();
        assert_eq!(dumped_with.cons_car(), Value::symbol("dumped-with-pdumper"));
        assert_eq!(dumped_with.cons_cdr(), Value::T);

        let load_time = stats.cons_cdr().cons_car();
        assert_eq!(load_time.cons_car(), Value::symbol("load-time"));
        assert!(load_time.cons_cdr().is_float());

        let dump_file = stats.cons_cdr().cons_cdr().cons_car();
        assert_eq!(dump_file.cons_car(), Value::symbol("dump-file-name"));
        let expected = dump_path
            .canonicalize()
            .unwrap()
            .to_string_lossy()
            .into_owned();
        assert_eq!(
            dump_file.cons_cdr().as_str_owned().as_deref(),
            Some(expected.as_str())
        );
    }

    #[test]
    fn test_pdump_bad_magic() {
        crate::test_utils::init_test_tracing();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.pdump");
        std::fs::write(&path, b"BADMAGIC").unwrap();
        assert!(matches!(load_from_dump(&path), Err(DumpError::BadMagic)));
    }

    #[test]
    fn test_pdump_round_trip_bootstrap() {
        crate::test_utils::init_test_tracing();
        // Bootstrap, dump, load, and verify eval works on loaded state
        let eval = crate::emacs_core::load::create_bootstrap_evaluator()
            .expect("bootstrap should succeed");

        let dir = tempfile::tempdir().unwrap();
        let dump_path = dir.path().join("bootstrap.pdump");

        let dump_start = std::time::Instant::now();
        dump_to_file(&eval, &dump_path).expect("dump should succeed");
        let dump_time = dump_start.elapsed();
        let file_size = std::fs::metadata(&dump_path).unwrap().len();
        eprintln!(
            "pdump: dump took {dump_time:.2?}, file size: {file_size} bytes ({:.1} MB)",
            file_size as f64 / 1048576.0
        );

        // Drop original evaluator before loading to test standalone load
        drop(eval);

        let load_start = std::time::Instant::now();
        let mut loaded = load_from_dump(&dump_path).expect("load should succeed");
        let load_time = load_start.elapsed();
        eprintln!("pdump: load took {load_time:.2?}");

        // Verify the loaded evaluator can evaluate Elisp
        let forms = crate::emacs_core::value_reader::read_all("(+ 1 2)").unwrap();
        let result = loaded.eval_sub(forms[0]).expect("eval should succeed");
        assert_eq!(result, Value::fixnum(3));

        // Verify features survived (bootstrap sets many features)
        // Note: subr.el does NOT call (provide 'subr); use 'backquote instead
        let forms = crate::emacs_core::value_reader::read_all("(featurep 'backquote)").unwrap();
        let result = loaded
            .eval_sub(forms[0])
            .expect("featurep should succeed");
        assert_eq!(result, Value::T, "featurep 'backquote should be t");

        // Verify a bootstrapped function works
        let forms = crate::emacs_core::value_reader::read_all("(length '(a b c))").unwrap();
        let result = loaded.eval_sub(forms[0]).expect("eval should succeed");
        assert_eq!(result, Value::fixnum(3));

        // Verify string operations (tests heap String objects)
        let forms =
            crate::emacs_core::value_reader::read_all("(concat \"hello\" \" \" \"world\")").unwrap();
        let result = loaded.eval_sub(forms[0]).expect("eval should succeed");
        assert_eq!(crate::emacs_core::print_value(&result), "\"hello world\"");

        // Verify hash table access (tests hash table round-trip)
        let forms = crate::emacs_core::value_reader::read_all(
            "(let ((h (make-hash-table :test 'equal))) (puthash \"key\" 42 h) (gethash \"key\" h))",
        )
        .unwrap();
        let result = loaded.eval_sub(forms[0]).expect("eval should succeed");
        assert_eq!(result, Value::fixnum(42));

        // Verify defun works (tests lambda/macro round-trip)
        let forms = crate::emacs_core::value_reader::read_all(
            "(progn (defun pdump-test-fn (x) (* x x)) (pdump-test-fn 7))",
        )
        .unwrap();
        let result = loaded.eval_sub(forms[0]).expect("eval should succeed");
        assert_eq!(result, Value::fixnum(49));
    }

    #[test]
    fn test_pdump_round_trip_preserves_runtime_derived_mode_syntax() {
        crate::test_utils::init_test_tracing();
        let mut eval = crate::emacs_core::load::create_bootstrap_evaluator()
            .expect("bootstrap should succeed");
        crate::emacs_core::load::apply_runtime_startup_state(&mut eval)
            .expect("runtime startup should succeed");

        let probe_src = r#"(list
                 (boundp 'lisp-data-mode-syntax-table)
                 (boundp 'emacs-lisp-mode-syntax-table)
                 (boundp 'lisp-interaction-mode-syntax-table)
                 (functionp (symbol-function 'lisp-interaction-mode))
                 (eq (char-table-parent emacs-lisp-mode-syntax-table)
                     lisp-data-mode-syntax-table)
                 (eq (char-table-parent lisp-interaction-mode-syntax-table)
                     emacs-lisp-mode-syntax-table)
                 (char-syntax ?\n)
                 (char-syntax ?\;)
                 (char-syntax ?{)
                 (char-syntax ?'))"#;
        let probe = crate::emacs_core::value_reader::read_all(probe_src).unwrap();
        let full_result = eval
            .eval_sub(probe[0])
            .expect("full bootstrap probe should run");
        assert_eq!(
            crate::emacs_core::print_value_with_buffers(&full_result, &eval.buffers),
            "(t t t t t t 62 60 95 39)"
        );

        let dir = tempfile::tempdir().unwrap();
        let dump_path = dir.path().join("derived-mode-syntax.pdump");
        dump_to_file(&eval, &dump_path).expect("dump should succeed");
        drop(eval);

        let mut loaded = load_from_dump(&dump_path).expect("load should succeed");
        crate::emacs_core::load::apply_runtime_startup_state(&mut loaded)
            .expect("runtime startup after load should succeed");

        let probe = crate::emacs_core::value_reader::read_all(probe_src).unwrap();
        let loaded_result = loaded
            .eval_sub(probe[0])
            .expect("loaded bootstrap probe should run");
        assert_eq!(
            crate::emacs_core::print_value_with_buffers(&loaded_result, &loaded.buffers),
            "(t t t t t t 62 60 95 39)"
        );
    }

    #[test]
    fn test_pdump_round_trip_preserves_pre_runtime_standard_syntax_identity() {
        crate::test_utils::init_test_tracing();
        let eval = crate::emacs_core::load::create_bootstrap_evaluator()
            .expect("bootstrap should succeed");

        let dir = tempfile::tempdir().unwrap();
        let dump_path = dir.path().join("bootstrap-pre-runtime-syntax.pdump");
        dump_to_file(&eval, &dump_path).expect("dump should succeed");
        drop(eval);

        let mut loaded = load_from_dump(&dump_path).expect("load should succeed");
        crate::emacs_core::load::apply_runtime_startup_state(&mut loaded)
            .expect("runtime startup after load should succeed");

        let probe = crate::emacs_core::value_reader::read_all(
            r#"(list
                 (eq (char-table-parent emacs-lisp-mode-syntax-table)
                     lisp-data-mode-syntax-table)
                 (eq (char-table-parent lisp-interaction-mode-syntax-table)
                     emacs-lisp-mode-syntax-table)
                 (char-syntax ?\n)
                 (char-syntax ?\;)
                 (char-syntax ?{)
                 (char-syntax ?'))"#,
        )
        .unwrap();
        let result = loaded
            .eval_sub(probe[0])
            .expect("loaded pre-runtime probe should run");
        assert_eq!(
            crate::emacs_core::print_value_with_buffers(&result, &loaded.buffers),
            "(t t 62 60 95 39)"
        );
    }

    #[test]
    fn test_pdump_round_trip_preserves_default_fontset_han_order() {
        crate::test_utils::init_test_tracing();
        let mut eval =
            crate::emacs_core::load::create_bootstrap_evaluator_with_features(&["neomacs"])
                .expect("bootstrap should succeed");
        let setup = crate::emacs_core::value_reader::read_all(
            r#"(new-fontset
                "fontset-default"
                '((han
                   (nil . "GB2312.1980-0")
                   (nil . "JISX0208*")
                   (nil . "gb18030"))))"#,
        )
        .unwrap();
        eval.eval_sub(setup[0])
            .expect("han-only fontset should install before dump");

        let dir = tempfile::tempdir().unwrap();
        let dump_path = dir.path().join("bootstrap-charsets.pdump");
        dump_to_file(&eval, &dump_path).expect("dump should succeed");
        drop(eval);

        let mut loaded = load_from_dump(&dump_path).expect("load should succeed");
        let probe = crate::emacs_core::value_reader::read_all(
            r#"(list
                (fontset-font t ?好 t)
                (fontset-font t (string-to-char "好") t))"#,
        )
        .unwrap();
        let result = loaded
            .eval_sub(probe[0])
            .expect("pdump fontset probe should run");
        let rendered = crate::emacs_core::print_value_with_buffers(&result, &loaded.buffers);

        assert!(
            rendered.starts_with(
                "(((nil . \"gb2312.1980-0\") \
                  (nil . \"jisx0208*\") \
                  (nil . \"gb18030\")) \
                 ((nil . \"gb2312.1980-0\") \
                  (nil . \"jisx0208*\") \
                  (nil . \"gb18030\")))"
            ),
            "unexpected pdump fontset order: {rendered}"
        );
    }

    #[test]
    fn test_restore_snapshot_isolated_between_clones() {
        crate::test_utils::init_test_tracing();
        let template = crate::emacs_core::load::create_bootstrap_evaluator_cached()
            .expect("bootstrap template should succeed");
        let snapshot = snapshot_evaluator(&template);

        let mut first = restore_snapshot(&snapshot).expect("first clone should succeed");
        let setup = crate::emacs_core::value_reader::read_all(
            "(progn
               (setq compat-pdump-clone-smoke 'first)
               compat-pdump-clone-smoke)",
        )
        .unwrap();
        let first_result = first
            .eval_sub(setup[0])
            .expect("first clone evaluation should succeed");
        assert_eq!(
            crate::emacs_core::print_value_with_buffers(&first_result, &first.buffers),
            "first"
        );

        let mut second = restore_snapshot(&snapshot).expect("second clone should succeed");
        let probe =
            crate::emacs_core::value_reader::read_all("(boundp 'compat-pdump-clone-smoke)").unwrap();
        let second_result = second
            .eval_sub(probe[0])
            .expect("second clone evaluation should succeed");
        assert_eq!(
            crate::emacs_core::print_value_with_buffers(&second_result, &second.buffers),
            "nil"
        );
    }

    #[test]
    fn test_restore_snapshot_preserves_core_subr_callable_surface() {
        crate::test_utils::init_test_tracing();
        let template = Context::new();
        let snapshot = snapshot_evaluator(&template);

        let mut restored = restore_snapshot(&snapshot).expect("restored snapshot should succeed");
        let forms = crate::emacs_core::value_reader::read_all(
            r#"(list (funcall 'cons 1 2)
                     (funcall 'list 1 2 3)
                     (funcall 'intern "compat-pdump-subr-probe")
                     (funcall 'format "%s-%s" "pdump" "ok"))"#,
        )
        .expect("parse");
        let result = restored
            .eval_sub(forms[0])
            .expect("restored runtime subrs should be callable");
        assert_eq!(
            crate::emacs_core::print_value_with_buffers(&result, &restored.buffers),
            "((1 . 2) (1 2 3) compat-pdump-subr-probe \"pdump-ok\")"
        );
    }

    #[test]
    fn test_restore_snapshot_does_not_report_file_based_pdump_session() {
        crate::test_utils::init_test_tracing();
        let mut template = Context::new();
        let setup = crate::emacs_core::value_reader::read_all(
            "(progn
               (setq compat-pdump-snapshot-hook-fired nil)
               (setq after-pdump-load-hook
                     (list (lambda () (setq compat-pdump-snapshot-hook-fired t)))))",
        )
        .unwrap();
        template
            .eval_sub(setup[0])
            .expect("setup hook should evaluate");
        let snapshot = snapshot_evaluator(&template);

        let mut restored = restore_snapshot(&snapshot).expect("restored snapshot should succeed");
        assert_eq!(
            restored
                .obarray
                .symbol_value("compat-pdump-snapshot-hook-fired"),
            Some(&Value::NIL)
        );

        let forms = crate::emacs_core::value_reader::read_all("(pdumper-stats)").unwrap();
        let stats = restored
            .eval_sub(forms[0])
            .expect("pdumper-stats should evaluate");
        assert!(stats.is_nil());
    }

    #[test]
    fn test_pdump_checksum_mismatch() {
        crate::test_utils::init_test_tracing();
        let dir = tempfile::tempdir().unwrap();
        let dump_path = dir.path().join("test.pdump");

        let eval = Context::new();
        dump_to_file(&eval, &dump_path).expect("dump should succeed");

        // Corrupt a byte in the payload
        let mut data = std::fs::read(&dump_path).unwrap();
        if let Some(last) = data.last_mut() {
            *last ^= 0xFF;
        }
        std::fs::write(&dump_path, &data).unwrap();

        let result = load_from_dump(&dump_path);
        // Should fail with checksum mismatch or deserialization error
        assert!(result.is_err());
    }

    #[test]
    fn test_restore_snapshot_rejects_legacy_unwind_protect_dump_opcode() {
        crate::test_utils::init_test_tracing();
        let mut snapshot = snapshot_evaluator(&Context::new());
        snapshot
            .tagged_heap
            .objects
            .push(DumpHeapObject::ByteCode(DumpByteCodeFunction {
                ops: vec![DumpOp::UnwindProtect(7), DumpOp::Nil, DumpOp::Return],
                constants: vec![],
                max_stack: 1,
                params: DumpLambdaParams {
                    required: vec![],
                    optional: vec![],
                    rest: None,
                },
                lexical: false,
                env: None,
                gnu_byte_offset_map: None,
                docstring: None,
                doc_form: None,
                interactive: None,
            }));
        let result = restore_snapshot(&snapshot);
        match result {
            Err(DumpError::DeserializationError(message)) => {
                assert!(
                    message.contains(
                        "legacy neomacs unwind-protect opcode is unsupported in pdump snapshots"
                    ),
                    "unexpected error: {message}"
                );
            }
            Ok(_) => panic!("expected deserialization error, got successful restore"),
            Err(err) => panic!("expected deserialization error, got {err}"),
        }
    }
}