rustia 0.2.0

Rustia is a pure-Rust interpreter for embedding standalone Luau in games and tools. Edit scripts without a rebuild and let players write mods. Rustia grew out of a game by Slowchop Studios that needed to script character decisions and replay them on desktop and in a browser.
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
use crate::function::IntoHostFunction;
use crate::{
    Error, GuestSurface, HostSchema, OpCapability, Profile, Result, Rustia, Settings, VmError,
    VmErrorKind,
};
use rustia_compiler::{compile_with_options, CompileOptions};
use rustia_vm::{
    ClockProvider, EntropyProvider, HostFunctionOp, ModuleResolver, Program, ResolvedModule,
    StdoutSink, VmSettings,
};
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use std::time::{Instant, SystemTime, UNIX_EPOCH};
#[cfg(all(
    target_arch = "wasm32",
    target_os = "unknown",
    feature = "platform-adapters"
))]
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};

#[cfg(all(
    target_arch = "wasm32",
    target_os = "unknown",
    feature = "platform-adapters"
))]
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console, js_name = log)]
    fn web_console_log(text: &str);
}

/// Chooses a script environment, resource limits, and host services.
///
/// Start with [`Rustia::builder`] for reproducible defaults, or use
/// [`live`](Self::live) for real clocks, entropy, and `loadstring`.
/// [`deterministic`](Self::deterministic) rejects nondeterministic input providers
/// and host capabilities. None of these presets sets a fuel or memory limit.
///
/// ```
/// use rustia::Rustia;
///
/// let mut lua = Rustia::builder()
///     .fuel(100_000)
///     .memory_limit_bytes(4 * 1024 * 1024)
///     .build()?;
/// assert_eq!(lua.load("return 6 * 7").eval::<i64>()?, 42);
/// # Ok::<(), rustia::Error>(())
/// ```
///
/// Use [`build_for_host`](Self::build_for_host) when callbacks need borrowed
/// per-invocation state. The more restricted
/// [`deterministic_leaf`](Self::deterministic_leaf) preset is for stateless
/// decisions and requires a typed host plus an explicit memory limit.
pub struct RustiaBuilder {
    profile: Profile,
    settings: VmSettings,
    extensions: Vec<RustiaExtension>,
    live_requested: bool,
}

/// Stable, typed description of the core profile and guest surface selected by
/// a builder.
///
/// The descriptor deliberately covers the core [`Profile`] and guest-visible
/// [`GuestSurface`] together. Its canonical bytes are one component of an
/// embedder's package or replay identity; settings, budgets, host ABI, and any
/// permitted services must be bound separately. Unlike a caller-maintained
/// label, the value can also be read back from the built [`Rustia`] VM.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RustiaExecutionProfile {
    profile: Profile,
    guest_surface: GuestSurface,
}

impl RustiaExecutionProfile {
    /// Ordinary sealed deterministic execution.
    pub const DETERMINISTIC_STANDARD: Self =
        Self::new(Profile::DeterministicSim, GuestSurface::Standard);

    /// Stateless authoritative-leaf execution.
    pub const DETERMINISTIC_HARDENED_LEAF: Self =
        Self::new(Profile::DeterministicSim, GuestSurface::HardenedLeaf);

    pub(crate) const fn new(profile: Profile, guest_surface: GuestSurface) -> Self {
        Self {
            profile,
            guest_surface,
        }
    }

    pub(crate) const fn from_vm(
        profile: rustia_vm::Profile,
        guest_surface: rustia_vm::GuestSurface,
    ) -> Self {
        Self::new(
            Profile::from_core(profile),
            GuestSurface::from_core(guest_surface),
        )
    }

    /// Constructs a builder whose core VM semantics exactly match this
    /// descriptor. Further builder methods may add budgets or services; the
    /// final descriptor remains readable through
    /// [`RustiaBuilder::execution_profile`].
    pub fn builder(self) -> RustiaBuilder {
        RustiaBuilder::from_execution_profile(self)
    }

    /// Stable compact code for fixed-width host protocol fields.
    ///
    /// Codes name the complete `(Profile, GuestSurface)` pair rather than
    /// relying on Rust enum discriminants.
    pub const fn compatibility_id(self) -> u32 {
        match (self.profile, self.guest_surface) {
            (Profile::DeterministicSim, GuestSurface::HardenedLeaf) => 1,
            (Profile::LuauCompat, GuestSurface::Standard) => 2,
            (Profile::ReproducibleStandard, GuestSurface::Standard) => 3,
            (Profile::DeterministicSim, GuestSurface::Standard) => 4,
            (Profile::LuauCompat, GuestSurface::HardenedLeaf) => 5,
            (Profile::ReproducibleStandard, GuestSurface::HardenedLeaf) => 6,
            (Profile::Puc51Compat, GuestSurface::Standard) => 7,
            (Profile::Puc51Compat, GuestSurface::HardenedLeaf) => 8,
        }
    }

    /// Canonical versioned bytes for package and replay identity components.
    ///
    /// Each field is an explicitly assigned little-endian `u32`: encoding
    /// version, core-profile tag, then guest-surface tag.
    pub const fn identity_bytes(self) -> [u8; 12] {
        let encoding = 1u32.to_le_bytes();
        let profile = match self.profile {
            Profile::LuauCompat => 1u32,
            Profile::ReproducibleStandard => 2,
            Profile::DeterministicSim => 3,
            Profile::Puc51Compat => 4,
        }
        .to_le_bytes();
        let surface = match self.guest_surface {
            GuestSurface::Standard => 1u32,
            GuestSurface::HardenedLeaf => 2,
        }
        .to_le_bytes();
        [
            encoding[0],
            encoding[1],
            encoding[2],
            encoding[3],
            profile[0],
            profile[1],
            profile[2],
            profile[3],
            surface[0],
            surface[1],
            surface[2],
            surface[3],
        ]
    }
}

impl RustiaBuilder {
    pub(crate) fn new() -> Self {
        Self {
            profile: Profile::ReproducibleStandard,
            settings: VmSettings::default(),
            extensions: Vec::new(),
            live_requested: false,
        }
    }

    fn from_execution_profile(execution_profile: RustiaExecutionProfile) -> Self {
        let mut builder = Self::new();
        builder.profile = execution_profile.profile;
        builder.settings.guest_surface = execution_profile.guest_surface.to_core();
        builder
    }

    /// Returns the typed core execution semantics currently selected by this
    /// builder.
    pub const fn execution_profile(&self) -> RustiaExecutionProfile {
        RustiaExecutionProfile::new(
            self.profile,
            GuestSurface::from_core(self.settings.guest_surface),
        )
    }

    /// Returns the effective limits and service declarations selected so far.
    ///
    /// Use [`Settings::identity_bytes`] when configuration is part of a replay
    /// or package identity. Provider data and application code remain separate
    /// identity inputs.
    pub fn settings(&self) -> Settings {
        Settings::from_core(self.profile, &self.settings)
    }

    #[cfg(test)]
    pub(crate) const fn vm_settings(&self) -> &VmSettings {
        &self.settings
    }

    /// Starts with reproducible inputs and the standard script environment.
    ///
    /// This uses [`Profile::ReproducibleStandard`]: clocks use deterministic
    /// defaults, the random seed is zero, and `loadstring` is disabled. Script
    /// `print` writes to stdout on native/WASI targets and is silent on browser
    /// Wasm until a sink is supplied. Real clocks and other nondeterministic
    /// providers remain available when explicitly configured.
    /// [`Rustia::builder`] starts with this preset.
    pub fn standard() -> Self {
        let builder = Self::new();
        #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
        let builder = builder.stdout(|text| print!("{text}"));
        builder
    }

    /// Starts with [`Profile::DeterministicSim`] and no host services.
    ///
    /// Nondeterministic input providers and host capabilities are rejected.
    /// Replay-stable providers and a `print` sink can still be added explicitly.
    pub fn deterministic() -> Self {
        RustiaExecutionProfile::DETERMINISTIC_STANDARD.builder()
    }

    /// Starts a restricted environment for independent, stateless decisions.
    ///
    /// Uses [`GuestSurface::HardenedLeaf`] with [`Profile::DeterministicSim`].
    /// Set an explicit [`memory_limit_bytes`](Self::memory_limit_bytes) and use
    /// [`build_for_host`](Self::build_for_host). Execute host-owned decision
    /// bodies with [`Rustia::compile_entry`] and [`Rustia::invoke_entry_into`].
    pub fn deterministic_leaf() -> Self {
        RustiaExecutionProfile::DETERMINISTIC_HARDENED_LEAF.builder()
    }

    /// Starts with real clocks, a fresh random seed, output, and `loadstring`.
    ///
    /// Uses [`Profile::ReproducibleStandard`] with nondeterministic services;
    /// results are not guaranteed to repeat. `loadstring` lets scripts compile
    /// additional code at runtime, so choose this preset deliberately for
    /// untrusted scripts.
    ///
    /// Native and WASI targets use their system services. On `wasm32-unknown-unknown`,
    /// the default `platform-adapters` feature connects to JavaScript’s `console`,
    /// `Date`, `performance`, and `crypto`. To run without JavaScript imports, disable
    /// default features and give [`build`](Self::build) a [`wall_clock`](Self::wall_clock),
    /// a [`monotonic_clock`](Self::monotonic_clock), and an [`entropy`](Self::entropy) provider.
    pub fn live() -> Self {
        let builder = Self::standard();
        #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
        let builder = builder
            .wall_clock(ServiceClock::system())
            .monotonic_clock(ServiceClock::monotonic())
            .entropy(ServiceEntropy::system());
        #[cfg(all(
            target_arch = "wasm32",
            target_os = "unknown",
            feature = "platform-adapters"
        ))]
        let builder = builder
            .stdout(web_console_log)
            .wall_clock(ServiceClock::system())
            .monotonic_clock(ServiceClock::monotonic())
            .entropy(ServiceEntropy::system());
        let mut builder = builder;
        builder.live_requested = true;
        builder.settings.loadstring_compiler = Some(stock_loadstring_compiler);
        builder
    }

    /// Starts a Lua 5.1 compatibility environment with `loadstring` enabled.
    ///
    /// The profile alone reproduces PUC 5.1.5 table layout, number
    /// formatting, and `table.sort`; this preset adds `loadstring` so PUC-era
    /// code that compiles chunks at runtime works out of the box. Runtime
    /// compilation is an extra capability for untrusted scripts; enable it
    /// where Lua 5.1 source compatibility requires it. Host services otherwise
    /// use the defaults from [`standard`](Self::standard).
    pub fn puc51_compat() -> Self {
        let mut builder = Self::standard().profile(Profile::Puc51Compat);
        builder.settings.loadstring_compiler = Some(stock_loadstring_compiler);
        builder
    }

    /// Selects the core compatibility and capability policy.
    /// Existing settings and services are retained and checked when building.
    pub fn profile(mut self, profile: Profile) -> Self {
        self.profile = profile;
        self
    }

    #[cfg(test)]
    pub(crate) fn raw_settings(mut self, settings: VmSettings) -> Self {
        self.settings = settings;
        self
    }

    /// Configures host-side reports independently of guest heap and fuel limits.
    pub fn diagnostic_limits(mut self, limits: crate::DiagnosticLimits) -> Self {
        self.settings.diagnostic_limits = limits.to_core();
        self
    }

    /// Limits the instruction work allowed during execution.
    ///
    /// Direct `Rustia<()>` entry points use this as their top-level budget. A
    /// typed invocation uses the lower of this ceiling and its explicit
    /// [`crate::InvocationLimits`] fuel, across every guest call in that
    /// invocation. Fuel measures deterministic work rather than elapsed time;
    /// script `pcall` cannot catch fuel exhaustion. The default is unlimited.
    pub fn fuel(mut self, fuel: u64) -> Self {
        self.settings.initial_fuel = Some(fuel);
        self
    }

    /// Limits nested script calls. The default and maximum are 512.
    ///
    /// Larger values are clamped to the engine maximum. Exhaustion returns a
    /// distinct error that script `pcall` cannot catch.
    pub fn recursion_limit(mut self, limit: usize) -> Self {
        self.settings.recursion_limit = limit.min(rustia_vm::MAX_RECURSION_LIMIT);
        self
    }

    /// Limits the VM's accounted live memory. The default is unlimited.
    ///
    /// Some charges depend on pointer width, so a script near its limit can run
    /// out of memory at different points on native and Wasm targets. The limit
    /// covers VM-accounted memory and excludes allocator capacity, total process
    /// memory and memory captured by Rust callbacks.
    pub fn memory_limit_bytes(mut self, bytes: usize) -> Self {
        self.settings.memory_limit_bytes = Some(bytes);
        self
    }

    /// Enables tracking of live and reserved memory peaks at instruction boundaries.
    pub fn memory_tracking(mut self, enabled: bool) -> Self {
        self.settings.enable_memory_tracking = enabled;
        self
    }

    /// Adds a group of Rust callbacks to install when the VM is built.
    /// For a typed host, use [`crate::HostModule`] after building instead.
    pub fn extension(mut self, extension: RustiaExtension) -> Self {
        self.extensions.push(extension);
        self
    }

    /// Chooses where script `print` output goes.
    ///
    /// Each call sends one string, with arguments joined by tabs and a trailing
    /// newline. The closure cannot return an error. Output sinks are allowed in
    /// every ordinary profile, including [`Profile::DeterministicSim`].
    #[cfg(not(feature = "send"))]
    pub fn stdout(mut self, sink: impl FnMut(&str) + 'static) -> Self {
        self.settings.services.stdout = Some(StdoutSink::new(sink));
        self
    }

    /// Chooses a transferable sink for script `print` output.
    #[cfg(feature = "send")]
    pub fn stdout(mut self, sink: impl FnMut(&str) + Send + 'static) -> Self {
        self.settings.services.stdout = Some(StdoutSink::new(sink));
        self
    }

    /// Discards script `print` output by removing the configured sink.
    pub fn without_stdout(mut self) -> Self {
        self.settings.services.stdout = None;
        self
    }

    /// Installs the host-owned resolver behind script-visible `require`.
    ///
    /// The resolver chooses a canonical module identity and source for every
    /// request. Successful results are cached by identity within the calling
    /// function's module cohort: the cache shared by a module and its dependencies.
    /// Each installation starts a fresh cohort; retained functions keep using
    /// their original cohort. A module can therefore execute again in a new
    /// installation before [`Rustia::reset_decision`] clears all guest state.
    /// See [`ServiceModuleResolver`] for loading, failure, and suspension rules.
    pub fn module_resolver(mut self, resolver: ServiceModuleResolver) -> Self {
        self.settings.module_resolver = Some(resolver.into_provider());
        self
    }

    /// Supplies the clock for no-argument `os.time()` and the default `os.date`
    /// timestamp. A nondeterministic provider is rejected at [`build`](Self::build) on
    /// `DeterministicSim`.
    pub fn wall_clock(mut self, clock: ServiceClock) -> Self {
        self.settings.services.wall_clock = Some(clock.into_provider());
        self
    }

    /// Supplies the clock for `os.clock()`, overriding the VM's built-in
    /// logical clock. Same `DeterministicSim` validation as [`wall_clock`](Self::wall_clock).
    pub fn monotonic_clock(mut self, clock: ServiceClock) -> Self {
        self.settings.services.monotonic_clock = Some(clock.into_provider());
        self
    }

    /// Supplies the seed read once at VM construction for the initial `math.random`
    /// sequence. Calling `math.randomseed` later still replaces that state. A nondeterministic
    /// provider is rejected by [`deterministic`](Self::deterministic).
    pub fn entropy(mut self, entropy: ServiceEntropy) -> Self {
        self.settings.services.entropy = Some(entropy.into_provider());
        self
    }

    /// Validates the configuration and builds a VM without a typed host context.
    ///
    /// Returns an error for incompatible services, invalid extensions, or a
    /// hardened-leaf preset, which requires [`build_for_host`](Self::build_for_host).
    pub fn build(self) -> Result<Rustia> {
        self.validate()?;
        if self.settings.guest_surface.is_hardened_leaf() {
            return Err(Error::from(VmError::new(
                VmErrorKind::HardenedLeafViolation {
                    feature: "context-free facade",
                },
            )));
        }
        let mut rustia = Rustia::from_settings(self.profile, self.settings);
        for extension in self.extensions {
            rustia.install_extension(extension)?;
        }
        Ok(rustia)
    }

    /// Builds a typed VM while preserving this builder's profile, settings,
    /// and host services.
    ///
    /// Legacy [`RustiaExtension`] values deliberately do not cross into the
    /// typed capability surface; install a [`crate::HostModule<H>`] after
    /// building instead.
    pub fn build_for_host<H>(self) -> Result<Rustia<H>>
    where
        H: HostSchema,
    {
        self.validate()?;
        if !self.extensions.is_empty() {
            return Err(Error::embed(
                "legacy RustiaExtension values cannot be installed into a typed VM; \
                 use HostModule instead"
                    .to_owned(),
            ));
        }
        Ok(Rustia::<H>::from_settings(self.profile, self.settings))
    }

    fn validate(&self) -> Result<()> {
        if self.live_requested
            && (self.settings.services.wall_clock.is_none()
                || self.settings.services.monotonic_clock.is_none()
                || self.settings.services.entropy.is_none())
        {
            return Err(Error::from(VmError::new(VmErrorKind::Unsupported {
                feature: "live() without a complete platform adapter (plug \
                          .wall_clock/.monotonic_clock/.entropy providers, or enable the \
                          default platform-adapters feature for browser/JavaScript wasm hosts)",
            })));
        }
        if self.profile.is_deterministic_sim() {
            for (feature, clock) in [
                (
                    "non-deterministic wall clock provider",
                    &self.settings.services.wall_clock,
                ),
                (
                    "non-deterministic monotonic clock provider",
                    &self.settings.services.monotonic_clock,
                ),
            ] {
                if clock.as_ref().is_some_and(|clock| !clock.deterministic()) {
                    return Err(Error::from(VmError::new(VmErrorKind::CapabilityRemoved {
                        feature,
                        operation: None,
                        profile: self.profile,
                    })));
                }
            }
            if self
                .settings
                .services
                .entropy
                .as_ref()
                .is_some_and(|entropy| !entropy.deterministic())
            {
                return Err(Error::from(VmError::new(VmErrorKind::CapabilityRemoved {
                    feature: "non-deterministic entropy provider",
                    operation: None,
                    profile: self.profile,
                })));
            }
            if self
                .settings
                .module_resolver
                .as_ref()
                .is_some_and(|resolver| !resolver.deterministic())
            {
                return Err(Error::from(VmError::new(VmErrorKind::CapabilityRemoved {
                    feature: "non-deterministic module resolver",
                    operation: Some("require".to_owned()),
                    profile: self.profile,
                })));
            }
        }
        if self.settings.guest_surface.is_hardened_leaf() {
            if !self.profile.is_deterministic_sim() {
                return Err(Error::from(VmError::new(
                    VmErrorKind::HardenedLeafViolation {
                        feature: "non-DeterministicSim profile",
                    },
                )));
            }
            if self.settings.memory_limit_bytes.is_none() {
                return Err(Error::from(VmError::new(
                    VmErrorKind::HardenedLeafViolation {
                        feature: "missing logical-live memory limit",
                    },
                )));
            }
            if self.settings.services.stdout.is_some()
                || self.settings.services.wall_clock.is_some()
                || self.settings.services.monotonic_clock.is_some()
                || self.settings.services.entropy.is_some()
            {
                return Err(Error::from(VmError::new(
                    VmErrorKind::HardenedLeafViolation {
                        feature: "host output, clock, or entropy services",
                    },
                )));
            }
            if self.settings.loadstring_compiler.is_some()
                || self.settings.module_resolver.is_some()
                || self.settings.enable_conformance_coverage
                || self.settings.enable_opcode_census
                || self.settings.enable_conformance_top_level_yield
                || self.settings.enable_conformance_int64_userdata
                || self.settings.enable_conformance_native_userdata
                || self.settings.enable_conformance_native_type_checks
                || self.settings.enable_conformance_pcall_helpers
            {
                return Err(Error::from(VmError::new(
                    VmErrorKind::HardenedLeafViolation {
                        feature: "dynamic-code or conformance-only facilities",
                    },
                )));
            }
        }
        Ok(())
    }
}

/// Host-supplied source returned by a [`ServiceModuleResolver`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ModuleSource {
    identity: String,
    source_name: String,
    source: Vec<u8>,
}

impl ModuleSource {
    /// Creates a module whose diagnostic and resolver-referrer source name
    /// matches its canonical cache identity.
    pub fn new(identity: impl Into<String>, source: impl AsRef<[u8]>) -> Self {
        let identity = identity.into();
        Self {
            source_name: identity.clone(),
            identity,
            source: source.as_ref().to_vec(),
        }
    }

    /// Creates a module with a separate canonical cache identity and source
    /// name. The source name is used for diagnostics and nested `require`
    /// resolver referrers.
    pub fn with_source_name(
        identity: impl Into<String>,
        source_name: impl Into<String>,
        source: impl AsRef<[u8]>,
    ) -> Self {
        Self {
            identity: identity.into(),
            source_name: source_name.into(),
            source: source.as_ref().to_vec(),
        }
    }

    pub fn identity(&self) -> &str {
        &self.identity
    }

    pub fn source_name(&self) -> &str {
        &self.source_name
    }

    pub fn source(&self) -> &[u8] {
        &self.source
    }
}

/// Host-owned resolver for script-visible `require`.
///
/// Resolution is performed on demand with the requested path and the current
/// chunk's source name. The host must return a stable canonical identity when
/// it labels the resolver deterministic. Because the canonical identity is a
/// resolver result, the resolver is called before every cache lookup; a host
/// with expensive I/O or compilation should keep its own source cache. A cache
/// hit skips module execution and discards the newly resolved program.
///
/// A module may return zero or one value. Successful results are cached within
/// the calling function's module cohort; failures can be retried. Module code
/// running in a guest coroutine may yield and resume before its result is
/// cached. Recursive loading is an error, as is requesting an identity that a
/// different coroutine is still loading.
///
/// Ordinary `require` keeps completed writes when a module fails. An enclosing
/// [`Chunk::install`](crate::Chunk::install) still rolls back guest state when
/// the installation fails; external Rust effects remain outside that rollback.
///
/// ```
/// use rustia::{ModuleSource, RustiaBuilder, ServiceModuleResolver};
///
/// # fn main() -> rustia::Result<()> {
/// let resolver = ServiceModuleResolver::from_fn(|name, _referrer| {
///     match name {
///         "answer" => Ok(ModuleSource::new("answer", "return 42")),
///         _ => Err(rustia::Error::embed(format!("unknown module {name}"))),
///     }
/// });
/// let mut lua = RustiaBuilder::deterministic().module_resolver(resolver).build()?;
/// assert_eq!(lua.load("return require('answer')").eval::<i64>()?, 42);
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct ServiceModuleResolver {
    provider: ModuleResolver,
}

impl ServiceModuleResolver {
    /// Host-supplied replay-stable resolver. The determinism assertion is
    /// trusted, not verified.
    #[cfg(not(feature = "send"))]
    pub fn from_fn(
        resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static,
    ) -> Self {
        Self::from_fn_with_determinism(resolver, true)
    }

    /// Host-supplied replay-stable transferable resolver.
    #[cfg(feature = "send")]
    pub fn from_fn(
        resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + Send + 'static,
    ) -> Self {
        Self::from_fn_with_determinism(resolver, true)
    }

    /// Host-supplied live resolver explicitly labeled nondeterministic.
    #[cfg(not(feature = "send"))]
    pub fn from_nondeterministic_fn(
        resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static,
    ) -> Self {
        Self::from_fn_with_determinism(resolver, false)
    }

    /// Host-supplied live transferable resolver.
    #[cfg(feature = "send")]
    pub fn from_nondeterministic_fn(
        resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + Send + 'static,
    ) -> Self {
        Self::from_fn_with_determinism(resolver, false)
    }

    #[cfg(not(feature = "send"))]
    fn from_fn_with_determinism(
        mut resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static,
        deterministic: bool,
    ) -> Self {
        let provider = ModuleResolver::new_with_profile(
            move |requested, referrer, profile| {
                let module =
                    resolver(requested, referrer).map_err(crate::value::embed_to_vm_error)?;
                compile_module_source(module, profile)
            },
            deterministic,
        );
        Self { provider }
    }

    #[cfg(feature = "send")]
    fn from_fn_with_determinism(
        mut resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + Send + 'static,
        deterministic: bool,
    ) -> Self {
        let provider = ModuleResolver::new_with_profile(
            move |requested, referrer, profile| {
                let module =
                    resolver(requested, referrer).map_err(crate::value::embed_to_vm_error)?;
                compile_module_source(module, profile)
            },
            deterministic,
        );
        Self { provider }
    }

    fn into_provider(self) -> ModuleResolver {
        self.provider
    }
}

fn compile_module_source(
    module: ModuleSource,
    profile: rustia_vm::Profile,
) -> rustia_vm::Result<ResolvedModule> {
    if module.identity.is_empty() {
        return Err(crate::value::embed_to_vm_error(Error::embed(
            "module identity must not be empty",
        )));
    }
    let chunk = rustia_parser::parse_source_bytes(&module.source).map_err(|error| {
        module_compile_error(
            Error::from_engine(error),
            &module.source_name,
            &module.source,
        )
    })?;
    let options = CompileOptions {
        puc51_constructor_order: profile == rustia_vm::Profile::Puc51Compat,
        ..CompileOptions::default()
    }
    .with_source_bytes(module.source_name.clone(), &module.source);
    let mut program = compile_with_options(&chunk, options).map_err(|error| {
        module_compile_error(
            Error::from_engine(error),
            &module.source_name,
            &module.source,
        )
    })?;
    program.diagnostic_source = rustia_vm::diagnostic::SourceSnapshot::copy_with_limit(
        &module.source,
        crate::DiagnosticLimits::default().source_bytes,
    );
    Ok(ResolvedModule::new(module.identity, program))
}

fn module_compile_error(error: Error, name: &str, source: &[u8]) -> rustia_vm::Error {
    let message = format!("{name}:1: {error}");
    let mut error = error.with_source(name, source, crate::DiagnosticLimits::default());
    // Keep a single snapshot lease at the outer diagnostic, not an unbudgeted
    // copy hidden in the typed cause chain.
    let mut diagnostic = error.diagnostic.take();
    let mut error = crate::value::embed_to_vm_error(error);
    if let Some(diagnostic) = &mut diagnostic {
        std::sync::Arc::make_mut(diagnostic).host_cause = error
            .diagnostic
            .as_ref()
            .and_then(|diagnostic| diagnostic.host_cause.clone());
    }
    error.diagnostic = diagnostic.map(|diagnostic| std::sync::Arc::new(diagnostic.to_core()));
    if let rustia_vm::ErrorKind::RuntimeError { message: raw } = &mut error.kind {
        *raw = message;
    }
    error
}

/// A clock supplied to [`RustiaBuilder::wall_clock`] or
/// [`RustiaBuilder::monotonic_clock`].
///
/// Every constructor declares whether readings repeat during replay.
/// [`Profile::DeterministicSim`] rejects nondeterministic providers when built;
/// other profiles accept either kind.
/// Cloning shares the underlying provider.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct ServiceClock {
    provider: ClockProvider,
}

impl ServiceClock {
    /// Real system time in seconds since the Unix epoch, anchored to a monotonic instant at
    /// construction so readings never move backwards. Labeled nondeterministic: usable on
    /// `ReproducibleStandard`/`LuauCompat`, rejected on `DeterministicSim`.
    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
    pub fn system() -> Self {
        let epoch = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_or(0.0, |duration| duration.as_secs_f64());
        let start = Instant::now();
        Self {
            provider: ClockProvider::new(move || epoch + start.elapsed().as_secs_f64(), false),
        }
    }

    /// Monotonic elapsed time in seconds. Labeled nondeterministic.
    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
    pub fn monotonic() -> Self {
        let start = Instant::now();
        Self {
            provider: ClockProvider::new(move || start.elapsed().as_secs_f64(), false),
        }
    }

    /// JavaScript host wall time in seconds since the Unix epoch. Labeled nondeterministic.
    #[cfg(all(
        target_arch = "wasm32",
        target_os = "unknown",
        feature = "platform-adapters"
    ))]
    pub fn system() -> Self {
        Self {
            provider: ClockProvider::new(|| js_sys::Date::now() / 1_000.0, false),
        }
    }

    /// JavaScript host monotonic time in seconds. Uses `performance.now()` when available and
    /// falls back to `Date.now()`. Labeled nondeterministic.
    #[cfg(all(
        target_arch = "wasm32",
        target_os = "unknown",
        feature = "platform-adapters"
    ))]
    pub fn monotonic() -> Self {
        Self {
            provider: ClockProvider::new(web_monotonic_seconds, false),
        }
    }

    /// Constant clock that always reads `t` seconds. Labeled deterministic.
    pub fn fixed(t: f64) -> Self {
        Self {
            provider: ClockProvider::new(move || t, true),
        }
    }

    /// Supplies a clock whose readings the host promises to reproduce during replay.
    /// Rustia trusts this declaration and does not verify it.
    #[cfg(not(feature = "send"))]
    pub fn from_fn(clock: impl FnMut() -> f64 + 'static) -> Self {
        Self {
            provider: ClockProvider::new(clock, true),
        }
    }

    /// Supplies a transferable clock whose readings the host promises to reproduce.
    #[cfg(feature = "send")]
    pub fn from_fn(clock: impl FnMut() -> f64 + Send + 'static) -> Self {
        Self {
            provider: ClockProvider::new(clock, true),
        }
    }

    /// Host-supplied live clock. The provider is explicitly labeled nondeterministic.
    #[cfg(not(feature = "send"))]
    pub fn from_nondeterministic_fn(clock: impl FnMut() -> f64 + 'static) -> Self {
        Self {
            provider: ClockProvider::new(clock, false),
        }
    }

    /// Host-supplied transferable live clock.
    #[cfg(feature = "send")]
    pub fn from_nondeterministic_fn(clock: impl FnMut() -> f64 + Send + 'static) -> Self {
        Self {
            provider: ClockProvider::new(clock, false),
        }
    }

    pub(crate) fn into_provider(self) -> ClockProvider {
        self.provider
    }
}

/// Host entropy used to select the VM's initial `math.random` sequence.
///
/// Like [`ServiceClock`], every constructor carries a determinism label. The provider is read
/// once while the VM is built; `math.randomseed` remains available afterward.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct ServiceEntropy {
    provider: EntropyProvider,
}

impl ServiceEntropy {
    /// System entropy where Rustia has a default platform adapter. If the platform random
    /// source is temporarily unavailable, a time-varying fallback still preserves `live()`'s
    /// no-replay-guarantee contract. Labeled nondeterministic.
    #[cfg(any(
        not(all(target_arch = "wasm32", target_os = "unknown")),
        all(
            target_arch = "wasm32",
            target_os = "unknown",
            feature = "platform-adapters"
        )
    ))]
    pub fn system() -> Self {
        let fallback_counter = std::sync::atomic::AtomicU64::new(0);
        Self {
            provider: EntropyProvider::new(
                move || {
                    system_entropy().unwrap_or_else(|| {
                        let counter = fallback_counter
                            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
                            .wrapping_add(1);
                        fallback_entropy(counter)
                    })
                },
                false,
            ),
        }
    }

    /// Constant replay-stable seed.
    pub fn fixed(seed: u64) -> Self {
        Self {
            provider: EntropyProvider::new(move || seed, true),
        }
    }

    /// Host-supplied replay-stable seed provider.
    #[cfg(not(feature = "send"))]
    pub fn from_fn(entropy: impl FnMut() -> u64 + 'static) -> Self {
        Self {
            provider: EntropyProvider::new(entropy, true),
        }
    }

    /// Host-supplied transferable replay-stable seed provider.
    #[cfg(feature = "send")]
    pub fn from_fn(entropy: impl Fn() -> u64 + Send + Sync + 'static) -> Self {
        Self {
            provider: EntropyProvider::new(entropy, true),
        }
    }

    /// Host-supplied live seed provider, explicitly labeled nondeterministic.
    #[cfg(not(feature = "send"))]
    pub fn from_nondeterministic_fn(entropy: impl FnMut() -> u64 + 'static) -> Self {
        Self {
            provider: EntropyProvider::new(entropy, false),
        }
    }

    /// Host-supplied transferable live seed provider.
    #[cfg(feature = "send")]
    pub fn from_nondeterministic_fn(entropy: impl Fn() -> u64 + Send + Sync + 'static) -> Self {
        Self {
            provider: EntropyProvider::new(entropy, false),
        }
    }

    pub(crate) fn into_provider(self) -> EntropyProvider {
        self.provider
    }
}

#[cfg(feature = "platform-adapters")]
fn system_entropy() -> Option<u64> {
    getrandom::u64().ok()
}

#[cfg(all(
    not(feature = "platform-adapters"),
    not(all(target_arch = "wasm32", target_os = "unknown"))
))]
fn system_entropy() -> Option<u64> {
    None
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
fn fallback_entropy(counter: u64) -> u64 {
    let wall = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0_u128, |duration| duration.as_nanos());
    (wall as u64) ^ ((wall >> 64) as u64) ^ counter.rotate_left(29)
}

#[cfg(all(
    target_arch = "wasm32",
    target_os = "unknown",
    feature = "platform-adapters"
))]
fn fallback_entropy(counter: u64) -> u64 {
    js_sys::Date::now().to_bits() ^ web_monotonic_seconds().to_bits() ^ counter.rotate_left(29)
}

#[cfg(all(
    target_arch = "wasm32",
    target_os = "unknown",
    feature = "platform-adapters"
))]
fn web_monotonic_seconds() -> f64 {
    let global = js_sys::global();
    let performance = js_sys::Reflect::get(&global, &JsValue::from_str("performance"));
    let Ok(performance) = performance else {
        return js_sys::Date::now() / 1_000.0;
    };
    let now = js_sys::Reflect::get(&performance, &JsValue::from_str("now"))
        .ok()
        .and_then(|value| value.dyn_into::<js_sys::Function>().ok())
        .and_then(|now| now.call0(&performance).ok())
        .and_then(|value| value.as_f64());
    now.unwrap_or_else(js_sys::Date::now) / 1_000.0
}

/// Stock `loadstring` compiler installed by [`RustiaBuilder::live`]: the same parse + compile
/// pipeline the facade uses for ahead-of-time source (public builtin environment), with
/// Luau's `[string "..."]` chunk naming when the script passes no chunk name.
///
/// The VM supplies its active [`Profile`] so profile-dependent codegen stays
/// consistent between ahead-of-time and runtime-compiled chunks (currently the
/// PUC 5.1 table-constructor write order under `Profile::Puc51Compat`).
fn stock_loadstring_compiler(
    source: &[u8],
    source_name: Option<&[u8]>,
    profile: rustia_vm::Profile,
) -> std::result::Result<Program, String> {
    let source_name = source_name.map_or_else(
        || loadstring_chunk_name(source),
        |name| String::from_utf8_lossy(name).into_owned(),
    );
    let chunk = rustia_parser::parse_source_bytes(source)
        .map_err(|error| format!("{source_name}:1: {error}"))?;
    let options = CompileOptions {
        puc51_constructor_order: profile == rustia_vm::Profile::Puc51Compat,
        ..CompileOptions::default()
    }
    .with_source_bytes(source_name.clone(), source);
    let mut program = compile_with_options(&chunk, options)
        .map_err(|error| format!("{source_name}:1: {error}"))?;
    program.diagnostic_source = rustia_vm::diagnostic::SourceSnapshot::copy_with_limit(
        source,
        crate::DiagnosticLimits::default().source_bytes,
    );
    Ok(program)
}

fn loadstring_chunk_name(source: &[u8]) -> String {
    let mut preview = String::new();
    for byte in source.iter().copied().take(60) {
        match byte {
            b'\\' => preview.push_str("\\\\"),
            b'"' => preview.push_str("\\\""),
            b'\n' => break,
            0x20..=0x7e => preview.push(byte as char),
            _ => preview.push('?'),
        }
    }
    if source.len() > 60 {
        preview.push_str("...");
    }
    format!("[string \"{preview}\"]")
}

/// A group of Rust callbacks installed together, optionally in a named table.
///
/// Add it with [`RustiaBuilder::extension`] or [`Rustia::install_extension`].
/// For callbacks that borrow per-invocation host state, use [`crate::HostModule`]
/// instead. The capability labels here describe captured host state; they do
/// not provide a borrowed host context or make captured mutations transactional.
#[derive(Clone, Debug)]
pub struct RustiaExtension {
    namespace: Option<String>,
    functions: Vec<HostFunctionOp>,
}

/// Version for the public host-function fuel-cost schedule.
///
/// Bump this when a rustia release changes the meaning of declared host fuel costs.
pub const RUSTIA_HOST_FUEL_ABI_VERSION: u32 = 1;

/// Default fuel cost assigned by capability-specific host-function helpers.
pub const RUSTIA_DEFAULT_HOST_FUNCTION_FUEL: u32 = 1;

/// Minimum accepted host-function fuel cost.
pub const RUSTIA_MIN_HOST_FUNCTION_FUEL: u32 = 1;

/// Public host-function fuel policy for extension metadata.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RustiaHostFuelPolicy {
    /// ABI version for interpreting host-function fuel costs.
    pub abi_version: u32,
    /// Fuel cost used when the helper API does not take an explicit cost.
    pub default_function_fuel: u32,
    /// Smallest fuel cost accepted by VM registration.
    pub minimum_function_fuel: u32,
}

/// The fuel policy used by extension registration helpers.
pub const RUSTIA_HOST_FUEL_POLICY: RustiaHostFuelPolicy = RustiaHostFuelPolicy {
    abi_version: RUSTIA_HOST_FUEL_ABI_VERSION,
    default_function_fuel: RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
    minimum_function_fuel: RUSTIA_MIN_HOST_FUNCTION_FUEL,
};

/// Public pre-install metadata for a registered host function.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RustiaHostFunctionInfo<'a> {
    /// The binding name inside the extension's namespace, or a global name.
    pub name: &'a str,
    /// ABI version used to interpret `fuel_cost`.
    pub fuel_abi_version: u32,
    /// Fuel charged per call.
    pub fuel_cost: u32,
    /// The callback's declared access to host state and effects.
    pub capability: OpCapability,
    /// The number of typed Rust parameters.
    pub parameter_count: u8,
    /// Rust type names for the callback parameters.
    pub parameter_types: &'a [&'static str],
    /// Rust type names for the callback results.
    pub return_types: &'a [&'static str],
    /// Whether the callback returns a Rustia `Result`.
    pub fallible: bool,
}

impl RustiaExtension {
    /// Creates callbacks to install under a named table. An empty name selects globals.
    pub fn new(namespace: impl Into<String>) -> Self {
        let namespace = namespace.into();
        Self {
            namespace: (!namespace.is_empty()).then_some(namespace),
            functions: Vec::new(),
        }
    }

    /// Creates callbacks to install directly as globals.
    pub fn global() -> Self {
        Self {
            namespace: None,
            functions: Vec::new(),
        }
    }

    /// Returns the table name, or `None` for global callbacks.
    pub fn namespace(&self) -> Option<&str> {
        self.namespace.as_deref()
    }

    /// Returns the fuel policy used by these registration helpers.
    pub fn fuel_policy(&self) -> RustiaHostFuelPolicy {
        RUSTIA_HOST_FUEL_POLICY
    }

    /// Describes the callbacks before installation.
    pub fn functions(&self) -> impl Iterator<Item = RustiaHostFunctionInfo<'_>> + '_ {
        self.functions
            .iter()
            .map(|function| RustiaHostFunctionInfo {
                name: &function.name,
                fuel_abi_version: RUSTIA_HOST_FUEL_ABI_VERSION,
                fuel_cost: function.fuel_cost,
                capability: OpCapability::from_core(function.capability),
                parameter_count: function.parameter_count,
                parameter_types: &function.parameter_types,
                return_types: &function.return_types,
                fallible: function.fallible,
            })
    }

    /// Adds a callback declared free of external mutable state, at the default fuel cost.
    pub fn fn_pure<Func, Args, Return>(self, name: impl Into<String>, function: Func) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.fn_with(
            name,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
            OpCapability::Pure,
            function,
        )
    }

    /// Adds a callback declared to read deterministic input, at the default fuel cost.
    pub fn fn_input<Func, Args, Return>(self, name: impl Into<String>, function: Func) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.fn_with(
            name,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
            OpCapability::ReadsDeterministicInput,
            function,
        )
    }

    /// Adds a callback declared to read slot state, at the default fuel cost.
    pub fn fn_slot_read<Func, Args, Return>(self, name: impl Into<String>, function: Func) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.fn_with(
            name,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
            OpCapability::ReadsSlotState,
            function,
        )
    }

    /// Adds a callback declared to write slot effects, at the default fuel cost.
    pub fn fn_slot_write<Func, Args, Return>(self, name: impl Into<String>, function: Func) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.fn_with(
            name,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
            OpCapability::WritesSlotEffect,
            function,
        )
    }

    /// Adds a callback declared to emit deterministic effects, at the default fuel cost.
    pub fn fn_effect<Func, Args, Return>(self, name: impl Into<String>, function: Func) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.fn_with(
            name,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
            OpCapability::EmitsDeterministicEffect,
            function,
        )
    }

    /// Adds a callback that reads external mutable state. Deterministic profiles reject it.
    pub fn fn_external_state<Func, Args, Return>(
        self,
        name: impl Into<String>,
        function: Func,
    ) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.fn_with(
            name,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
            OpCapability::ReadsExternalMutableState,
            function,
        )
    }

    /// Adds a nondeterministic callback. Deterministic profiles reject it.
    pub fn fn_nondeterministic<Func, Args, Return>(
        self,
        name: impl Into<String>,
        function: Func,
    ) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.fn_with(
            name,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL,
            OpCapability::NondeterministicEffect,
            function,
        )
    }

    /// Adds a callback with an explicit capability and nonzero fuel cost.
    /// Invalid costs and disallowed capabilities are rejected during installation.
    pub fn fn_with<Func, Args, Return>(
        mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        capability: OpCapability,
        function: Func,
    ) -> Self
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.functions.push(
            crate::HostFunction::<()>::with_capability(capability, function)
                .fuel_cost(fuel_cost)
                .into_op(name.into()),
        );
        self
    }

    pub(crate) fn install(self, rustia: &mut Rustia) -> Result<()> {
        let Some(namespace) = self.namespace else {
            return rustia
                .vm_mut()
                .install_host_functions(self.functions)
                .map_err(Error::from_engine);
        };

        rustia
            .vm_mut()
            .install_host_module(namespace, self.functions)
            .map_err(Error::from_engine)
    }
}