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
use rustia::{
    Effects, Error, FromRustia, HostContext, HostSchema, Input, IntoRustiaMulti, InvocationLimits,
    NewObject, Object, ObjectType, OwnedValue, Result, Rustia, RustiaBuilder, SlotWrites, Slots,
};
#[cfg(not(feature = "send"))]
use std::cell::Cell;
#[cfg(not(feature = "send"))]
use std::rc::Rc;
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc,
};

struct Player {
    score: i64,
}

struct WordList {
    words: Vec<String>,
    fail_after: Option<i64>,
}

struct CountTo(i64);

struct WordIterator {
    words: Vec<String>,
    index: usize,
    fail_after: Option<i64>,
}

struct CountIterator {
    next: i64,
    end: i64,
}

fn word_list_type() -> ObjectType<WordList> {
    ObjectType::new("WordList")
        .length(2, |list: &WordList| list.words.len() as i64)
        .iterator(
            3,
            |list: &WordList| {
                if list.fail_after == Some(0) {
                    return Err(Error::embed("iterator setup stopped"));
                }
                Ok(WordIterator {
                    words: list.words.clone(),
                    index: 0,
                    fail_after: list.fail_after,
                })
            },
            |state: &mut WordIterator, _: Option<i64>| -> Result<Option<(i64, String)>> {
                state.index += 1;
                let next = state.index as i64;
                if state.fail_after == Some(next) {
                    return Err(Error::embed("iterator stopped"));
                }
                Ok(state
                    .words
                    .get((next - 1) as usize)
                    .cloned()
                    .map(|word| (next, word)))
            },
        )
        .method_mut("push", 2, |list: &mut WordList, word: String| {
            list.words.push(word)
        })
}

fn player_type() -> ObjectType<Player> {
    ObjectType::new("Player").method("score", 1, |p: &Player| p.score)
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn typed_length_and_iteration_preserve_luau_control_and_error_semantics() -> Result<()> {
    let mut lua = RustiaBuilder::standard().fuel(20_000).build()?;
    lua.register_object_type(word_list_type())?;
    lua.register_object_type(ObjectType::new("CountTo").iterator(
        1,
        |range: &CountTo| {
            Ok(CountIterator {
                next: 1,
                end: range.0,
            })
        },
        |state: &mut CountIterator, _: Option<i64>| {
            let next = state.next;
            state.next += 1;
            (next <= state.end).then_some(next)
        },
    ))?;
    let list = lua.create_object(WordList {
        words: vec!["a".into(), "b".into(), "c".into()],
        fail_after: None,
    })?;
    lua.globals()?
        .set_live(&mut lua, "words", &list.as_value())?;
    assert_eq!(
        lua.eval::<String>(
            "local out = {}; for i, word in words do out[i] = word end; return #words .. ':' .. table.concat(out, ',')"
        )?,
        "3:a,b,c"
    );
    let range = lua.create_object(CountTo(3))?;
    lua.globals()?
        .set_live(&mut lua, "count_to", &range.as_value())?;
    assert_eq!(
        lua.eval::<String>(
            "local out = ''; for value in count_to do out ..= tostring(value) end; return out"
        )?,
        "123"
    );
    assert_eq!(
        lua.eval::<String>(
            "local out = ''; for _, a in words do for _, b in words do out ..= a .. b end end; return out"
        )?,
        "aaabacbabbbccacbcc"
    );
    assert_eq!(
        lua.eval::<String>(
            r#"
            local function run()
                local out = ''
                for _, word in words do
                    out ..= word
                    coroutine.yield(out)
                end
                return out
            end
            local a, b = coroutine.create(run), coroutine.create(run)
            local out = ''
            for _ = 1, 4 do
                local _, av = coroutine.resume(a)
                local _, bv = coroutine.resume(b)
                out ..= av .. ':' .. bv .. ';'
            end
            return out
        "#
        )?,
        "a:a;ab:ab;abc:abc;abc:abc;"
    );
    assert_eq!(
        lua.eval::<String>(
            "local out = ''; for _, word in words do out ..= word; if word == 'a' then words:push('z') end end; return out .. ':' .. #words"
        )?,
        "abc:4"
    );
    lua.exec("words:push('d')")?;
    assert_eq!(
        lua.eval::<String>("local out = ''; for _, word in words do out ..= word end; return out")?,
        "abczd"
    );

    let broken = lua.create_object(WordList {
        words: vec!["x".into(), "y".into()],
        fail_after: Some(2),
    })?;
    lua.globals()?
        .set_live(&mut lua, "broken", &broken.as_value())?;
    assert!(lua.eval::<bool>(
        "local seen = ''; local ok, message = pcall(function() for _, word in broken do seen ..= word end end); return not ok and seen == 'x' and string.find(message, 'iterator stopped', 1, true) ~= nil"
    )?);
    let broken_factory = lua.create_object(WordList {
        words: vec!["unused".into()],
        fail_after: Some(0),
    })?;
    lua.globals()?
        .set_live(&mut lua, "broken_factory", &broken_factory.as_value())?;
    assert!(lua.eval::<bool>(
        "local ok, message = pcall(function() for _ in broken_factory do end end); return not ok and string.find(message, 'iterator setup stopped', 1, true) ~= nil"
    )?);

    lua.reset_decision()?;
    let reset = lua.create_object(WordList {
        words: vec!["reset".into()],
        fail_after: None,
    })?;
    lua.globals()?
        .set_live(&mut lua, "words", &reset.as_value())?;
    assert_eq!(
        lua.eval::<String>("for _, word in words do return word end")?,
        "reset"
    );
    Ok(())
}

#[test]
fn expanded_call_continuations_root_typed_objects() -> Result<()> {
    let mut lua = RustiaBuilder::standard()
        .memory_limit_bytes(2 * 1024 * 1024)
        .fuel(100_000)
        .build()?;
    lua.register_object_type(player_type())?;
    lua.register_function("make_player", || NewObject::new(Player { score: 21 }))?;
    let source = r#"
        local function pass(...) return ... end
        local function pressure()
            for i=1,48 do local garbage=table.create(16000,i) end
        end
        local function argument(...)
            coroutine.yield('argument')
            string.len=function(object,x,y)
                coroutine.yield('callee')
                return object:score()+x+y
            end
            return ...
        end
        local co=coroutine.create(function()
            return string.len(make_player(),pass(pass(pass(pass(argument(4,5))))))
        end)
        local a,b=coroutine.resume(co)
        pressure()
        local c,d=coroutine.resume(co)
        pressure()
        local e,f=coroutine.resume(co)
        return tostring(a)..':'..b..':'..tostring(c)..':'..d..':'..tostring(e)..':'..f
    "#;
    assert_eq!(
        lua.eval::<String>(source)?,
        "true:argument:true:callee:true:30"
    );
    lua.reset_decision()?;
    assert_eq!(
        lua.eval::<String>(source)?,
        "true:argument:true:callee:true:30"
    );
    Ok(())
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn method_metadata_is_available_before_registration() {
    let definition =
        player_type().method_mut("set", 5, |p: &mut Player, score: i64| -> Result<()> {
            p.score = score;
            Ok(())
        });
    assert_eq!(definition.name(), "Player");
    assert_eq!(definition.fuel_policy(), rustia::RUSTIA_HOST_FUEL_POLICY);
    let methods: Vec<_> = definition.methods().collect();
    assert_eq!(methods.len(), 2);
    assert_eq!(methods[0].name, "score");
    assert_eq!(
        methods[0].fuel_abi_version,
        rustia::RUSTIA_HOST_FUEL_ABI_VERSION
    );
    assert_eq!(methods[0].capability, rustia::OpCapability::Pure);
    assert_eq!(methods[0].parameter_count, 1);
    assert_eq!(
        methods[0].parameter_types,
        &[std::any::type_name::<Object<Player>>()]
    );
    assert_eq!(methods[0].return_types, &[std::any::type_name::<i64>()]);
    assert!(!methods[0].fallible);
    assert_eq!(methods[1].name, "set");
    assert_eq!(methods[1].fuel_cost, 5);
    assert_eq!(
        methods[1].capability,
        rustia::OpCapability::ReadsExternalMutableState
    );
    assert_eq!(methods[1].parameter_count, 2);
    assert!(methods[1].return_types.is_empty());
    assert!(methods[1].fallible);
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn typed_objects_preserve_identity_and_old_methods_across_replacement() -> Result<()> {
    let mut lua = Rustia::builder().build()?;
    lua.register_object_type(player_type())?;
    let old = lua.create_object(Player { score: 21 })?;
    lua.globals()?.set_live(&mut lua, "old", &old.as_value())?;
    lua.exec("alias = old; detached = old.score")?;
    lua.register_object_type(
        ObjectType::new("Player").method("score", 1, |p: &Player| p.score * 2),
    )?;
    let new = lua.create_object(Player { score: 21 })?;
    lua.globals()?.set_live(&mut lua, "new", &new.as_value())?;
    assert!(lua.eval::<bool>("return old == alias and old ~= new and old:score() == 21 and new:score() == 42 and detached(new) == 21")?);
    assert_eq!(old.call_method::<_, i64>(&mut lua, "score", ())?, 21);
    assert_eq!(new.call_method::<_, i64>(&mut lua, "score", ())?, 42);
    assert!(old.as_value().as_object::<Player>(&mut lua)?.is_some());
    assert!(old.as_value().as_object::<String>(&mut lua)?.is_none());
    assert!(lua
        .load("return newproxy()")
        .eval_live()?
        .as_object::<Player>(&mut lua)?
        .is_none());
    lua.reset_decision()?;
    assert!(old.call_method::<_, i64>(&mut lua, "score", ()).is_err());
    let after_reset = lua.create_object(Player { score: 5 })?;
    assert_eq!(
        after_reset.call_method::<_, i64>(&mut lua, "score", ())?,
        10
    );
    Ok(())
}

struct ConversionProbe;
impl FromRustia for ConversionProbe {
    fn from_rustia(_: OwnedValue) -> Result<Self> {
        Err(Error::embed("argument conversion reached"))
    }
}

struct HostConversionProbe;
impl IntoRustiaMulti for HostConversionProbe {
    fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
        Err(Error::embed("host conversion reached"))
    }
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn fresh_results_are_validated_before_owned_result_allocation() -> Result<()> {
    struct Unregistered;
    let mut lua = RustiaBuilder::standard()
        .memory_limit_bytes(2 * 1024 * 1024)
        .build()?;
    lua.register_function("bad_result", || {
        (vec![1_i64; 131_072], NewObject::new(Unregistered))
    })?;
    let message: String =
        lua.eval("local ok, err = pcall(bad_result); assert(not ok); return tostring(err)")?;
    assert!(message.contains("not registered"), "{message}");
    assert_eq!(lua.eval::<i64>("return 42")?, 42);
    let mut leaf = RustiaBuilder::deterministic_leaf()
        .memory_limit_bytes(2 * 1024 * 1024)
        .build_for_host::<Game>()?;
    let before = leaf.memory_stats();
    assert!(leaf.create_object(Player { score: 1 }).is_err());
    assert_eq!(leaf.memory_stats().live_bytes(), before.live_bytes());
    Ok(())
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[test]
fn a_panicking_method_releases_the_borrow_and_reset_drops_payload_once() -> Result<()> {
    struct Dropped(Arc<AtomicUsize>);
    impl Drop for Dropped {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
    }
    let drops = Arc::new(AtomicUsize::new(0));
    let mut lua = Rustia::builder().build()?;
    lua.register_object_type(ObjectType::new("Dropped").method_mut(
        "panic",
        1,
        |_: &mut Dropped| -> () {
            panic!("test host panic");
        },
    ))?;
    let object = lua.create_object(Dropped(drops.clone()))?;
    lua.globals()?
        .set_live(&mut lua, "object", &object.as_value())?;
    assert!(lua.exec("pcall(function() object:panic() end)").is_err());
    assert!(
        lua.exec("return 1").is_err(),
        "host panic must poison further guest execution"
    );
    lua.reset_decision()?;
    assert_eq!(
        drops.load(Ordering::Relaxed),
        1,
        "the stale Rust handle must not retain the payload"
    );
    drop(object);
    assert_eq!(drops.load(Ordering::Relaxed), 1);
    assert_eq!(lua.eval::<i64>("return 42")?, 42);
    Ok(())
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn receivers_are_checked_before_other_argument_conversions() -> Result<()> {
    let mut first = Rustia::builder().build()?;
    first.register_object_type(player_type().method(
        "probe",
        1,
        |_: &Player, _: ConversionProbe| (),
    ))?;
    let player = first.create_object(Player { score: 7 })?;
    first
        .globals()?
        .set_live(&mut first, "player", &player.as_value())?;
    let message: String = first
        .eval("local ok, err = pcall(player.probe, {}, 1); assert(not ok); return tostring(err)")?;
    assert!(
        !message.contains("argument conversion reached"),
        "{message}"
    );
    let error = player
        .call_method::<_, ()>(
            &mut Rustia::builder().build()?,
            "probe",
            HostConversionProbe,
        )
        .unwrap_err();
    assert!(!error.to_string().contains("host conversion reached"));
    first.reset_decision()?;
    let error = player
        .call_method::<_, ()>(&mut first, "probe", HostConversionProbe)
        .unwrap_err();
    assert!(!error.to_string().contains("host conversion reached"));
    Ok(())
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn callback_arguments_and_mixed_fresh_results_keep_objects_live() -> Result<()> {
    let mut lua = Rustia::builder().build()?;
    lua.register_object_type(
        player_type()
            .method("copy", 1, |p: &Player| {
                NewObject::new(Player { score: p.score })
            })
            .method("same", 1, |_: &Player, other: Object<Player>| other),
    )?;
    lua.register_function("spawn", |score: i64| {
        ("ready", NewObject::new(Player { score }), 9_i64)
    })?;
    lua.register_function("identity", |object: Object<Player>| object)?;
    assert!(lua.eval::<bool>("local tag, p, n = spawn(42); local copy = p:copy(); return tag == 'ready' and n == 9 and copy ~= p and copy:score() == 42 and identity(p) == p and copy:same(p) == p")?);
    let object = lua
        .load("local _, p = spawn(12); return p")
        .eval_live()?
        .as_object::<Player>(&mut lua)?
        .unwrap();
    let copies = object.call_method_values(&mut lua, "copy", &[])?;
    let copy = copies[0].as_object::<Player>(&mut lua)?.unwrap();
    assert_eq!(copy.call_method::<_, i64>(&mut lua, "score", ())?, 12);
    assert!(
        object.call_method::<_, ()>(&mut lua, "copy", ()).is_err(),
        "owned returns must reject objects even when discarded"
    );
    Ok(())
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn owned_arguments_keep_nested_tables_alive_during_later_allocations() -> Result<()> {
    let mut lua = RustiaBuilder::standard()
        .memory_limit_bytes(2 * 1024 * 1024)
        .build()?;
    lua.register_object_type(player_type().method(
        "check",
        1,
        |_: &Player, first: Vec<Vec<i64>>, second: Vec<i64>| {
            assert_eq!(first.len(), 64);
            assert!(first
                .iter()
                .all(|row| row.len() == 512 && row.iter().all(|n| *n == 7)));
            assert_eq!(second.len(), 32_768);
            assert!(second.iter().all(|n| *n == 9));
            42_i64
        },
    ))?;
    let object = lua.create_object(Player { score: 0 })?;
    let garbage = lua.load("return table.create(70000, 1)").eval_live()?;
    drop(garbage);
    assert_eq!(
        object.call_method::<_, i64>(
            &mut lua,
            "check",
            (vec![vec![7_i64; 512]; 64], vec![9_i64; 32_768])
        )?,
        42
    );
    let exports = lua
        .load("return { check = function(a, b) return a[64][512] + b[32768] end }")
        .install()?;
    let function = exports.function(&mut lua, "check")?;
    let garbage = lua.load("return table.create(70000, 1)").eval_live()?;
    drop(garbage);
    assert_eq!(
        function.call::<_, i64>(&mut lua, (vec![vec![7_i64; 512]; 64], vec![9_i64; 32_768]))?,
        16
    );
    Ok(())
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg(not(feature = "send"))]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn forbidden_definitions_are_atomic_and_payloads_need_no_clone_or_send() -> Result<()> {
    struct Local(Rc<Cell<i64>>);
    let mut lua = RustiaBuilder::deterministic().build()?;
    lua.register_object_type(ObjectType::new("Local").method("value", 1, |v: &Local| v.0.get()))?;
    assert!(lua
        .register_object_type(
            ObjectType::new("Local")
                .method("value", 1, |_: &Local| 999_i64)
                .method_mut("change", 1, |v: &mut Local| v.0.set(10))
        )
        .is_err());
    let value = Rc::new(Cell::new(7));
    let object = lua.create_object(Local(value.clone()))?;
    assert_eq!(object.call_method::<_, i64>(&mut lua, "value", ())?, 7);
    assert_eq!(Rc::strong_count(&value), 2);
    let clone = object.clone();
    lua.reset_decision()?;
    assert_eq!(
        Rc::strong_count(&value),
        1,
        "stale root clones cannot retain payload ownership"
    );
    assert!(clone.call_method::<_, i64>(&mut lua, "value", ()).is_err());
    Ok(())
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn immediate_mutation_survives_errors_and_failed_installation() -> Result<()> {
    for builder in [
        RustiaBuilder::standard(),
        #[cfg(any(
            not(all(target_arch = "wasm32", target_os = "unknown")),
            feature = "platform-adapters"
        ))]
        RustiaBuilder::live(),
    ] {
        let mut lua = builder.build()?;
        lua.register_object_type(
            player_type().method_mut("add", 1, |p: &mut Player, n: i64| {
                p.score += n;
                p.score
            }),
        )?;
        let player = lua.create_object(Player { score: 1 })?;
        lua.globals()?
            .set_live(&mut lua, "player", &player.as_value())?;
        assert!(lua.exec("player:add(2); error('failed')").is_err());
        assert_eq!(player.call_method::<_, i64>(&mut lua, "score", ())?, 3);
        assert!(lua
            .load("player:add(4); error('candidate failed')")
            .install()
            .is_err());
        assert_eq!(player.call_method::<_, i64>(&mut lua, "score", ())?, 7);
        assert!(lua.eval::<bool>("local ok = pcall(function() player:add(8); error('caught') end); return not ok and player:score() == 15")?);
    }
    Ok(())
}

struct Game;
impl HostSchema for Game {
    type Input = i64;
    type Slots = i64;
    type External = i64;
    type Effect = i64;
    type SlotWrite = i64;
    type Record = (u32, &'static str, i64);
}
struct Turn {
    input: i64,
    slots: i64,
    external: i64,
}
impl HostContext<Game> for Turn {
    fn input(&self) -> &i64 {
        &self.input
    }
    fn slots(&self) -> &i64 {
        &self.slots
    }
    fn external_mut(&mut self) -> &mut i64 {
        &mut self.external
    }
    fn record_effect(&self, sequence: u32, value: i64) -> (u32, &'static str, i64) {
        (sequence, "effect", value)
    }
    fn record_slot_write(&self, sequence: u32, value: i64) -> (u32, &'static str, i64) {
        (sequence, "write", value)
    }
    fn record_bytes(&self, _: &(u32, &'static str, i64)) -> u64 {
        24
    }
}

#[cfg_attr(
    all(target_arch = "wasm32", target_os = "unknown"),
    wasm_bindgen_test::wasm_bindgen_test
)]
#[cfg_attr(not(all(target_arch = "wasm32", target_os = "unknown")), test)]
fn deterministic_methods_use_the_existing_ordered_journal_and_fresh_context() -> Result<()> {
    let mut lua = RustiaBuilder::deterministic().build_for_host::<Game>()?;
    lua.register_object_type(
        ObjectType::<Player, Game>::new("Player")
            .input("input", 2, |p: &Player, input: Input<'_, Game>| {
                p.score + input.get()
            })
            .slot_read("saved", 2, |p: &Player, slots: Slots<'_, Game>| {
                p.score + slots.get()
            })
            .effect(
                "request",
                2,
                |p: &Player, mut effects: Effects<'_, Game>, n: i64| effects.emit(p.score + n),
            )
            .effect(
                "failed_request",
                2,
                |_: &Player, mut effects: Effects<'_, Game>| -> Result<()> {
                    effects.emit(999)?;
                    Err(Error::embed("request rejected"))
                },
            )
            .slot_write(
                "save",
                2,
                |p: &Player, mut writes: SlotWrites<'_, Game>, n: i64| writes.write(p.score + n),
            ),
    )?;
    let player = lua.create_object(Player { score: 10 })?;
    lua.globals()?
        .set_live(&mut lua, "player", &player.as_value())?;
    for input in [1, 2] {
        let mut turn = Turn {
            input,
            slots: 3,
            external: 0,
        };
        let limits = InvocationLimits::new(1000)
            .with_max_records(4)
            .with_max_record_bytes(96);
        let outcome = lua.invoke(&mut turn, limits, |call| {
            assert_eq!(player.call_method_in::<_, _, i64>(call, "input", ())?, 10 + input);
            call.load("player:request(4); assert(not pcall(function() player:failed_request() end)); player:save(5); return player:saved()").eval::<i64>()
        })?;
        assert_eq!(*outcome.value(), 13);
        assert_eq!(outcome.records(), &[(0, "effect", 14), (1, "write", 15)]);
        assert_eq!(
            turn.slots, 3,
            "staged changes require the application's explicit commit"
        );
        assert!(lua
            .invoke(&mut turn, limits, |call| {
                call.load("player:request(8); error('cancel')").exec()
            })
            .is_err());
        assert_eq!(turn.slots, 3);
    }
    Ok(())
}