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
use rustia::{
    DiagnosticLimits, ModuleSource, OwnedTable, RustiaBuilder, ServiceModuleResolver, VmErrorKind,
    RUSTIA_MODULE_RESOLUTION_ABI_VERSION,
};
use std::sync::{
    atomic::{AtomicU32, Ordering},
    Arc, Mutex,
};

#[test]
fn dependency_syntax_error_retains_its_source_and_loading_callers() {
    let resolver = ServiceModuleResolver::from_fn(|requested, _| {
        Ok(match requested {
            "outer" => {
                ModuleSource::with_source_name("outer", "outer.luau", "return require('broken')")
            }
            _ => {
                ModuleSource::with_source_name("broken", "broken.luau", "-- dependency\nlocal = 1")
            }
        })
    });
    let mut vm = RustiaBuilder::standard()
        .module_resolver(resolver)
        .build()
        .unwrap();
    let error = vm
        .load("return require('outer')")
        .source_name("entry.luau")
        .exec()
        .unwrap_err();
    let report = error.report().to_string();
    assert!(report.contains("broken.luau:2"), "{report}");
    assert!(report.contains("2 | local = 1"), "{report}");
    assert!(report.contains("outer.luau:1"), "{report}");
    assert!(report.contains("entry.luau:1"), "{report}");
    vm.reset_decision().unwrap();
    assert!(vm.diagnostic_source_bytes() > 0);
    drop(error);
    assert_eq!(vm.diagnostic_source_bytes(), 0);
}

#[test]
fn dependency_syntax_error_obeys_zero_snapshot_budget() {
    let resolver =
        ServiceModuleResolver::from_fn(|_, _| Ok(ModuleSource::new("broken", "local = 1")));
    let limits = DiagnosticLimits::default().with_retained_source_bytes(0);
    let mut vm = RustiaBuilder::standard()
        .diagnostic_limits(limits)
        .module_resolver(resolver)
        .build()
        .unwrap();
    let error = vm.eval::<()>("require('broken')").unwrap_err();
    assert!(error.diagnostic().unwrap().snapshot.is_none());
    assert_eq!(vm.diagnostic_source_bytes(), 0);
}

#[test]
fn require_resolves_by_host_identity_and_executes_once() {
    assert_eq!(RUSTIA_MODULE_RESOLUTION_ABI_VERSION, 3);
    let resolutions = Arc::new(AtomicU32::new(0));
    let resolutions_for_host = Arc::clone(&resolutions);
    let resolver = ServiceModuleResolver::from_fn(move |requested, referrer| {
        resolutions_for_host.fetch_add(1, Ordering::Relaxed);
        assert_eq!(referrer, Some("entry.luau"));
        assert!(matches!(requested, "first" | "second"));
        Ok(ModuleSource::with_source_name(
            "shared-module",
            "modules/shared.luau",
            br#"
                _G.module_runs = (_G.module_runs or 0) + 1
                return { answer = 42 }
            "#,
        ))
    });
    let mut vm = RustiaBuilder::deterministic()
        .module_resolver(resolver)
        .build()
        .expect("deterministic resolver");

    let result: OwnedTable = vm
        .load(
            r#"
                local first = require("first")
                local second = require("second")
                return { same = first == second, runs = module_runs }
            "#,
        )
        .source_name("entry.luau")
        .eval()
        .expect("module result");

    assert_eq!(result.get_string_as::<bool>("same").unwrap(), Some(true));
    assert_eq!(result.get_string_as::<f64>("runs").unwrap(), Some(1.0));
    assert_eq!(resolutions.load(Ordering::Relaxed), 2);
}

#[test]
fn require_compiles_modules_with_the_requesting_profile() {
    let resolver = ServiceModuleResolver::from_fn(|requested, _| {
        assert_eq!(requested, "puc-constructor");
        Ok(ModuleSource::new(
            "puc-constructor",
            "local t = {1, [1] = 9} return t[1]",
        ))
    });
    let mut vm = RustiaBuilder::puc51_compat()
        .module_resolver(resolver)
        .build()
        .expect("PUC resolver");

    let result: f64 = vm
        .eval(r#"return require("puc-constructor")"#)
        .expect("module result");
    assert_eq!(result, 1.0);
}

#[test]
fn require_passes_nested_referrers_and_caches_empty_results() {
    let calls = Arc::new(Mutex::new(Vec::<(String, Option<String>)>::new()));
    let calls_for_host = Arc::clone(&calls);
    let resolver = ServiceModuleResolver::from_fn(move |requested, referrer| {
        calls_for_host
            .lock()
            .unwrap()
            .push((requested.to_owned(), referrer.map(str::to_owned)));
        match requested {
            "outer" => Ok(ModuleSource::with_source_name(
                "outer-id",
                "modules/outer.luau",
                r#"
                    require("empty")
                    require("empty")
                    return 42
                "#,
            )),
            "empty" => Ok(ModuleSource::with_source_name(
                "empty-id",
                "modules/empty.luau",
                "_G.empty_runs = (_G.empty_runs or 0) + 1",
            )),
            _ => Err(rustia::Error::embed(format!("unknown module {requested}"))),
        }
    });
    let mut vm = RustiaBuilder::deterministic()
        .module_resolver(resolver)
        .build()
        .expect("deterministic resolver");

    let result: OwnedTable = vm
        .load(r#"return { value = require("outer"), runs = empty_runs }"#)
        .source_name("entry.luau")
        .eval()
        .expect("nested modules");
    assert_eq!(result.get_string_as::<f64>("value").unwrap(), Some(42.0));
    assert_eq!(result.get_string_as::<f64>("runs").unwrap(), Some(1.0));
    assert_eq!(
        calls.lock().unwrap().as_slice(),
        [
            ("outer".to_owned(), Some("entry.luau".to_owned())),
            ("empty".to_owned(), Some("modules/outer.luau".to_owned())),
            ("empty".to_owned(), Some("modules/outer.luau".to_owned())),
        ]
    );
}

#[test]
fn require_reports_cycles_and_does_not_cache_failures() {
    let resolver = ServiceModuleResolver::from_fn(|requested, _| match requested {
        "a" => Ok(ModuleSource::new(
            "a",
            r#"
                _G.a_runs = (_G.a_runs or 0) + 1
                require("b")
                return "a"
            "#,
        )),
        "b" => Ok(ModuleSource::new("b", r#"return require("a")"#)),
        _ => Err(rustia::Error::embed(format!("unknown module {requested}"))),
    });
    let mut vm = RustiaBuilder::deterministic()
        .module_resolver(resolver)
        .build()
        .expect("deterministic resolver");

    let result: OwnedTable = vm
        .eval(
            r#"
                local first_ok, first_error = pcall(require, "a")
                local second_ok = pcall(require, "a")
                return {
                    first = first_ok,
                    second = second_ok,
                    runs = a_runs,
                    cycle = string.find(first_error, "a %-> b %-> a") ~= nil,
                }
            "#,
        )
        .expect("cycle is catchable");
    assert_eq!(result.get_string_as::<bool>("first").unwrap(), Some(false));
    assert_eq!(result.get_string_as::<bool>("second").unwrap(), Some(false));
    assert_eq!(result.get_string_as::<f64>("runs").unwrap(), Some(2.0));
    assert_eq!(result.get_string_as::<bool>("cycle").unwrap(), Some(true));
}

#[test]
fn require_reset_clears_the_execute_once_cache() {
    let output = Arc::new(Mutex::new(String::new()));
    let output_for_host = Arc::clone(&output);
    let resolver = ServiceModuleResolver::from_fn(|requested, _| {
        assert_eq!(requested, "probe");
        Ok(ModuleSource::new("probe", "print('loaded'); return 7"))
    });
    let mut vm = RustiaBuilder::deterministic()
        .stdout(move |text| output_for_host.lock().unwrap().push_str(text))
        .module_resolver(resolver)
        .build()
        .expect("deterministic resolver");

    let first: OwnedTable = vm
        .eval(r#"return { require("probe"), require("probe") }"#)
        .expect("first epoch");
    assert_eq!(first.get_array_as::<f64>(0).unwrap(), Some(7.0));
    assert_eq!(first.get_array_as::<f64>(1).unwrap(), Some(7.0));
    vm.reset_decision().expect("decision reset");
    let second: f64 = vm.eval(r#"return require("probe")"#).expect("second epoch");
    assert_eq!(second, 7.0);
    assert_eq!(output.lock().unwrap().as_str(), "loaded\nloaded\n");
}

#[test]
fn required_values_remain_rooted_and_modules_share_caller_fuel() {
    let resolver = ServiceModuleResolver::from_fn(|requested, _| match requested {
        "rooted" => Ok(ModuleSource::new("rooted", "return { answer = 42 }")),
        "expensive" => Ok(ModuleSource::new(
            "expensive",
            "local n = 0; for i = 1, 1000 do n += i end; return n",
        )),
        _ => Err(rustia::Error::embed(format!("unknown module {requested}"))),
    });
    let mut vm = RustiaBuilder::standard()
        .module_resolver(resolver.clone())
        .build()
        .expect("standard resolver");
    let rooted: bool = vm
        .eval(
            r#"
                local first = require("rooted")
                collectgarbage("collect")
                local second = require("rooted")
                return first == second and second.answer == 42
            "#,
        )
        .expect("cached value survives collection");
    assert!(rooted);

    let mut fuelled = RustiaBuilder::standard()
        .fuel(20)
        .module_resolver(resolver)
        .build()
        .expect("fuelled resolver");
    let error = fuelled
        .eval::<f64>(r#"return require("expensive")"#)
        .unwrap_err();
    assert!(matches!(
        error.kind(),
        rustia::ErrorKind::Vm(error)
            if matches!(error.kind(), VmErrorKind::FuelExhausted { limit: 20 })
    ));
}

#[test]
fn multi_result_modules_fail_without_entering_the_cache() {
    let resolver = ServiceModuleResolver::from_fn(|_, _| {
        Ok(ModuleSource::new(
            "multi",
            "_G.multi_runs = (_G.multi_runs or 0) + 1; return 1, 2",
        ))
    });
    let mut vm = RustiaBuilder::deterministic()
        .module_resolver(resolver)
        .build()
        .expect("deterministic resolver");
    let result: OwnedTable = vm
        .eval(
            r#"
                local first_ok, first_error = pcall(require, "multi")
                local second_ok = pcall(require, "multi")
                return {
                    first = first_ok,
                    second = second_ok,
                    runs = multi_runs,
                    message = string.find(first_error, "module must return a single value", 1, true) ~= nil,
                }
            "#,
        )
        .expect("module errors are catchable");
    assert_eq!(result.get_string_as::<bool>("first").unwrap(), Some(false));
    assert_eq!(result.get_string_as::<bool>("second").unwrap(), Some(false));
    assert_eq!(result.get_string_as::<f64>("runs").unwrap(), Some(2.0));
    assert_eq!(result.get_string_as::<bool>("message").unwrap(), Some(true));
}

#[test]
fn module_identity_quota_rejection_is_atomic_and_catchable() {
    let mut baseline_vm = RustiaBuilder::standard().build().expect("baseline VM");
    let baseline = baseline_vm.memory_stats().live_bytes();
    let oversized_identity = "m".repeat(256 * 1024);
    let resolver = ServiceModuleResolver::from_fn(move |_, _| {
        Ok(ModuleSource::new(
            oversized_identity.clone(),
            "_G.module_started = true; return 1",
        ))
    });
    let mut vm = RustiaBuilder::standard()
        .memory_limit_bytes(baseline + 128 * 1024)
        .module_resolver(resolver)
        .build()
        .expect("quota VM");

    let stayed_atomic: bool = vm
        .eval(
            r#"
                local ok, message = pcall(require, "oversized")
                return not ok
                    and string.find(message, "not enough memory", 1, true) ~= nil
                    and module_started == nil
            "#,
        )
        .expect("legacy pcall catches quota error");
    assert!(stayed_atomic);
    assert_eq!(
        vm.eval::<f64>("return 42").expect("VM remains usable"),
        42.0
    );
}

#[test]
fn deterministic_and_hardened_profiles_reject_invalid_resolvers() {
    let nondeterministic =
        ServiceModuleResolver::from_nondeterministic_fn(|_, _| Err(rustia::Error::embed("unused")));
    let error = RustiaBuilder::deterministic()
        .module_resolver(nondeterministic)
        .build()
        .err()
        .expect("deterministic profile rejection");
    assert!(matches!(
        error.kind(),
        rustia::ErrorKind::Vm(error)
            if matches!(
                error.kind(),
                VmErrorKind::CapabilityRemoved {
                    feature: "non-deterministic module resolver",
                    ..
                }
            )
    ));

    let deterministic = ServiceModuleResolver::from_fn(|_, _| Err(rustia::Error::embed("unused")));
    let error = RustiaBuilder::deterministic_leaf()
        .memory_limit_bytes(256 * 1024)
        .module_resolver(deterministic)
        .build()
        .err()
        .expect("hardened leaf rejection");
    assert!(matches!(
        error.kind(),
        rustia::ErrorKind::Vm(error)
            if matches!(
                error.kind(),
                VmErrorKind::HardenedLeafViolation {
                    feature: "dynamic-code or conformance-only facilities"
                }
            )
    ));
}

#[test]
fn resolver_panics_poison_until_decision_reset() {
    let calls = Arc::new(AtomicU32::new(0));
    let calls_for_host = Arc::clone(&calls);
    let resolver = ServiceModuleResolver::from_fn(move |_, _| {
        if calls_for_host.fetch_add(1, Ordering::Relaxed) == 0 {
            panic!("resolver panic");
        }
        Ok(ModuleSource::new("recovered", "return 42"))
    });
    let mut vm = RustiaBuilder::deterministic()
        .module_resolver(resolver)
        .build()
        .expect("deterministic resolver");

    let first = vm.eval::<f64>(r#"return require("probe")"#).unwrap_err();
    assert!(matches!(
        first.kind(),
        rustia::ErrorKind::Vm(error)
            if matches!(error.kind(), VmErrorKind::HostPanicked { .. })
    ));
    assert!(matches!(
        (vm.eval::<f64>("return 1").unwrap_err()).kind(),
        rustia::ErrorKind::Vm(error) if matches!(error.kind(), VmErrorKind::VmPoisoned)
    ));

    vm.reset_decision().expect("reset poisoned VM");
    assert_eq!(
        vm.eval::<f64>(r#"return require("probe")"#)
            .expect("resolver recovered"),
        42.0
    );
}