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
//! Object methods use the same borrowed views, dispatcher, and journal as functions.

use crate::function::{adapter::Token, host_arg_from_raw, host_return_to_raw};
use crate::invocation::{
    ContextHostFunction, EffectView, ExternalView, InputView, MakeCapabilityView, SlotReadView,
    SlotWriteView,
};
use crate::object_method::checked_receiver;
use crate::{FromHostArgument, HostSchema, IntoHostReturn, Object, ObjectType};
use rustia_vm::OpCapability;
use std::any::type_name;

mod adapter {
    use super::*;

    pub(crate) trait Read<T: 'static, H: HostSchema, A, R, V>:
        Send + Sync + 'static
    {
        fn into_context_object_method(
            self,
            token: Token,
            name: String,
            fuel_cost: u32,
            capability: OpCapability,
        ) -> ContextHostFunction<H>;
    }

    pub(crate) trait Write<T: 'static, H: HostSchema, A, R, V>:
        Send + Sync + 'static
    {
        fn into_mut_context_object_method(
            self,
            token: Token,
            name: String,
            fuel_cost: u32,
            capability: OpCapability,
        ) -> ContextHostFunction<H>;
    }
}

/// A read-only object method that borrows the current invocation's input.
///
/// Closures receive `&T`, [`crate::Input`], then up to eight typed arguments.
/// This sealed marker is implemented automatically for matching closures and
/// function pointers.
///
/// ```compile_fail
/// struct CustomMethod;
/// impl rustia::IntoInputObjectMethod<(), (), (), ()> for CustomMethod {}
/// ```
#[allow(private_bounds)]
pub trait IntoInputObjectMethod<T: 'static, H: HostSchema, A, R>:
    adapter::Read<T, H, A, R, InputView>
{
}
impl<T: 'static, H: HostSchema, A, R, F> IntoInputObjectMethod<T, H, A, R> for F where
    F: adapter::Read<T, H, A, R, InputView>
{
}

/// A read-only object method that borrows the invocation's saved slot snapshot.
///
/// Closures receive `&T`, [`crate::Slots`], then up to eight typed arguments.
/// This sealed marker is implemented automatically for matching callbacks.
#[allow(private_bounds)]
pub trait IntoSlotReadObjectMethod<T: 'static, H: HostSchema, A, R>:
    adapter::Read<T, H, A, R, SlotReadView>
{
}
impl<T: 'static, H: HostSchema, A, R, F> IntoSlotReadObjectMethod<T, H, A, R> for F where
    F: adapter::Read<T, H, A, R, SlotReadView>
{
}

/// An object method that stages effects in the invocation's existing journal.
///
/// Closures receive `&T`, [`crate::Effects`], then up to eight typed arguments.
/// This sealed marker is implemented automatically for matching callbacks.
#[allow(private_bounds)]
pub trait IntoEffectObjectMethod<T: 'static, H: HostSchema, A, R>:
    adapter::Read<T, H, A, R, EffectView>
{
}
impl<T: 'static, H: HostSchema, A, R, F> IntoEffectObjectMethod<T, H, A, R> for F where
    F: adapter::Read<T, H, A, R, EffectView>
{
}

/// An object method that stages slot writes in the invocation's existing journal.
///
/// Closures receive `&T`, [`crate::SlotWrites`], then up to eight typed arguments.
/// This sealed marker is implemented automatically for matching callbacks.
#[allow(private_bounds)]
pub trait IntoSlotWriteObjectMethod<T: 'static, H: HostSchema, A, R>:
    adapter::Read<T, H, A, R, SlotWriteView>
{
}
impl<T: 'static, H: HostSchema, A, R, F> IntoSlotWriteObjectMethod<T, H, A, R> for F where
    F: adapter::Read<T, H, A, R, SlotWriteView>
{
}

/// An object method with immediate access to external application state.
///
/// Closures receive `&T`, [`crate::HostMut`], then up to eight typed arguments.
/// This sealed marker is implemented automatically for matching callbacks.
#[allow(private_bounds)]
pub trait IntoExternalObjectMethod<T: 'static, H: HostSchema, A, R>:
    adapter::Read<T, H, A, R, ExternalView>
{
}
impl<T: 'static, H: HostSchema, A, R, F> IntoExternalObjectMethod<T, H, A, R> for F where
    F: adapter::Read<T, H, A, R, ExternalView>
{
}

/// A method that may change its payload and external application state immediately.
///
/// Closures receive `&mut T`, [`crate::HostMut`], then up to eight typed arguments.
/// This sealed marker is implemented automatically for matching callbacks.
#[allow(private_bounds)]
pub trait IntoExternalMutObjectMethod<T: 'static, H: HostSchema, A, R>:
    adapter::Write<T, H, A, R, ExternalView>
{
}
impl<T: 'static, H: HostSchema, A, R, F> IntoExternalMutObjectMethod<T, H, A, R> for F where
    F: adapter::Write<T, H, A, R, ExternalView>
{
}

impl<T: 'static, H: HostSchema> ObjectType<T, H> {
    /// Adds a read-only method over this invocation's deterministic input.
    pub fn input<F, A, R>(mut self, name: impl Into<String>, fuel_cost: u32, method: F) -> Self
    where
        F: IntoInputObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::ReadsDeterministicInput,
                )
                .into_op(),
        );
        self
    }

    /// Adds a read-only method over this invocation's saved slot snapshot.
    pub fn slot_read<F, A, R>(mut self, name: impl Into<String>, fuel_cost: u32, method: F) -> Self
    where
        F: IntoSlotReadObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::ReadsSlotState,
                )
                .into_op(),
        );
        self
    }

    /// Adds a method that stages effects with `&T` and [`crate::Effects`].
    ///
    /// Effects share the invocation's ordering and output limits. A failing
    /// method discards its own staged suffix; a failure escaping the outer
    /// invocation discards all its records.
    pub fn effect<F, A, R>(mut self, name: impl Into<String>, fuel_cost: u32, method: F) -> Self
    where
        F: IntoEffectObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::EmitsDeterministicEffect,
                )
                .into_op(),
        );
        self
    }

    /// Adds a method that stages saved-state changes with [`crate::SlotWrites`].
    ///
    /// Slot writes use the same journal, ordering, and limits as effects.
    pub fn slot_write<F, A, R>(mut self, name: impl Into<String>, fuel_cost: u32, method: F) -> Self
    where
        F: IntoSlotWriteObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::WritesSlotEffect,
                )
                .into_op(),
        );
        self
    }

    /// Adds a read-only payload method with immediate external-state access.
    ///
    /// The callback receives `&T`, then [`crate::HostMut`]. This declares
    /// external mutable state and is rejected by deterministic construction.
    /// Application changes are outside invocation and installation rollback.
    pub fn external<F, A, R>(mut self, name: impl Into<String>, fuel_cost: u32, method: F) -> Self
    where
        F: IntoExternalObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::ReadsExternalMutableState,
                )
                .into_op(),
        );
        self
    }

    /// Adds a read-only payload method that performs nondeterministic external work.
    ///
    /// The callback receives `&T`, then [`crate::HostMut`]. Deterministic
    /// construction rejects the definition. External effects are immediate.
    pub fn nondeterministic<F, A, R>(
        mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        method: F,
    ) -> Self
    where
        F: IntoExternalObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::NondeterministicEffect,
                )
                .into_op(),
        );
        self
    }

    /// Adds a method with `&mut T` and immediate external-state access.
    ///
    /// This declares external mutable state. Payload and application changes
    /// survive later script errors and failed installations. Reset drops the
    /// payload; it does not undo changes to the external application.
    pub fn external_mut<F, A, R>(
        mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        method: F,
    ) -> Self
    where
        F: IntoExternalMutObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_mut_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::ReadsExternalMutableState,
                )
                .into_op(),
        );
        self
    }

    /// Adds a mutable payload method that performs nondeterministic external work.
    ///
    /// Receives `&mut T`, then [`crate::HostMut`]. Both changes are immediate,
    /// and deterministic construction rejects the complete definition.
    pub fn nondeterministic_mut<F, A, R>(
        mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        method: F,
    ) -> Self
    where
        F: IntoExternalMutObjectMethod<T, H, A, R>,
    {
        self.methods.push(
            method
                .into_mut_context_object_method(
                    Token,
                    name.into(),
                    fuel_cost,
                    OpCapability::NondeterministicEffect,
                )
                .into_op(),
        );
        self
    }
}

macro_rules! read_method {
    ($count:expr; $($arg:ident),*) => {
        impl<T, H, F, R, V, $($arg),*> adapter::Read<T, H, ($($arg,)*), R, V> for F
        where
            T: 'static, H: HostSchema, V: MakeCapabilityView<H> + 'static,
            F: for<'call> Fn(&T, V::View<'call>, $($arg),*) -> R + Send + Sync + 'static,
            R: IntoHostReturn, $($arg: FromHostArgument,)*
        {
            #[allow(non_snake_case, unused_mut, unused_variables)]
            fn into_context_object_method(self, _: Token, name: String, fuel_cost: u32, capability: OpCapability) -> ContextHostFunction<H> {
                let diagnostic_name = name.clone();
                ContextHostFunction {
                    name, fuel_cost, capability,
                    parameter_count: 1 + $count,
                    parameter_types: vec![type_name::<Object<T>>(), $(type_name::<$arg>()),*],
                    return_types: R::rustia_type_names(Token),
                    fallible: R::is_fallible(Token),
                    handler: Box::new(move |vm, context, journal, args| {
                        let object = checked_receiver::<T>(vm, args)?;
                        let mut index = 1;
                        $(let $arg = host_arg_from_raw(vm, args, index, &diagnostic_name)?; index += 1;)*
                        let _ = index;
                        let result = {
                            let receiver = object.try_borrow::<T>()?;
                            let view = V::make(context, journal);
                            self(&receiver, view, $($arg),*)
                        };
                        host_return_to_raw(vm, &diagnostic_name, result)
                    }),
                }
            }
        }
    };
}

macro_rules! write_method {
    ($count:expr; $($arg:ident),*) => {
        impl<T, H, F, R, V, $($arg),*> adapter::Write<T, H, ($($arg,)*), R, V> for F
        where
            T: 'static, H: HostSchema, V: MakeCapabilityView<H> + 'static,
            F: for<'call> Fn(&mut T, V::View<'call>, $($arg),*) -> R + Send + Sync + 'static,
            R: IntoHostReturn, $($arg: FromHostArgument,)*
        {
            #[allow(non_snake_case, unused_mut, unused_variables)]
            fn into_mut_context_object_method(self, _: Token, name: String, fuel_cost: u32, capability: OpCapability) -> ContextHostFunction<H> {
                let diagnostic_name = name.clone();
                ContextHostFunction {
                    name, fuel_cost, capability,
                    parameter_count: 1 + $count,
                    parameter_types: vec![type_name::<Object<T>>(), $(type_name::<$arg>()),*],
                    return_types: R::rustia_type_names(Token),
                    fallible: R::is_fallible(Token),
                    handler: Box::new(move |vm, context, journal, args| {
                        let object = checked_receiver::<T>(vm, args)?;
                        let mut index = 1;
                        $(let $arg = host_arg_from_raw(vm, args, index, &diagnostic_name)?; index += 1;)*
                        let _ = index;
                        let result = {
                            let mut receiver = object.try_borrow_mut::<T>()?;
                            let view = V::make(context, journal);
                            self(&mut receiver, view, $($arg),*)
                        };
                        host_return_to_raw(vm, &diagnostic_name, result)
                    }),
                }
            }
        }
    };
}

read_method!(0;);
read_method!(1; A);
read_method!(2; A, B);
read_method!(3; A, B, C);
read_method!(4; A, B, C, D);
read_method!(5; A, B, C, D, E);
read_method!(6; A, B, C, D, E, G);
read_method!(7; A, B, C, D, E, G, I);
read_method!(8; A, B, C, D, E, G, I, J);
write_method!(0;);
write_method!(1; A);
write_method!(2; A, B);
write_method!(3; A, B, C);
write_method!(4; A, B, C, D);
write_method!(5; A, B, C, D, E);
write_method!(6; A, B, C, D, E, G);
write_method!(7; A, B, C, D, E, G, I);
write_method!(8; A, B, C, D, E, G, I, J);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        Effects, HostContext, HostMut, Input, InvocationLimits, RustiaBuilder, SlotWrites, Slots,
    };

    struct Schema;
    impl HostSchema for Schema {
        type Input = i64;
        type Slots = i64;
        type External = i64;
        type Effect = i64;
        type SlotWrite = i64;
        type Record = (u32, &'static str, i64);
    }

    struct Context<'a> {
        input: &'a i64,
        slots: &'a i64,
        external: &'a mut i64,
    }

    impl HostContext<Schema> for Context<'_> {
        fn input(&self) -> &i64 {
            self.input
        }
        fn slots(&self) -> &i64 {
            self.slots
        }
        fn external_mut(&mut self) -> &mut i64 {
            self.external
        }
        fn record_effect(&self, sequence: u32, effect: i64) -> (u32, &'static str, i64) {
            (sequence, "effect", effect)
        }
        fn record_slot_write(&self, sequence: u32, write: i64) -> (u32, &'static str, i64) {
            (sequence, "slot", write)
        }
        fn record_bytes(&self, _: &(u32, &'static str, i64)) -> u64 {
            16
        }
    }

    struct Actor(i64);

    fn input_function(actor: &Actor, input: Input<'_, Schema>) -> i64 {
        actor.0 + input.get()
    }

    fn add_input<F, A, R>(
        definition: ObjectType<Actor, Schema>,
        method: F,
    ) -> ObjectType<Actor, Schema>
    where
        F: IntoInputObjectMethod<Actor, Schema, A, R>,
    {
        definition.input("input", 1, method)
    }

    #[test]
    fn views_use_each_calls_borrowed_context_and_the_existing_ordered_journal() {
        let definition = add_input(ObjectType::<Actor, Schema>::new("Actor"), input_function)
            .slot_read("slot", 1, |actor: &Actor, slots: Slots<'_, Schema>| {
                actor.0 + slots.get()
            })
            .effect(
                "emit",
                1,
                |actor: &Actor, mut effects: Effects<'_, Schema>, amount: i64| {
                    effects.emit(actor.0 + amount)
                },
            )
            .slot_write(
                "write",
                1,
                |actor: &Actor, mut writes: SlotWrites<'_, Schema>, amount: i64| {
                    writes.write(actor.0 + amount)
                },
            )
            .external(
                "external",
                1,
                |actor: &Actor, mut host: HostMut<'_, Schema>| actor.0 + *host.get_mut(),
            )
            .nondeterministic(
                "external_add",
                1,
                |actor: &Actor, mut host: HostMut<'_, Schema>, amount: i64| {
                    *host.get_mut() += actor.0 + amount;
                },
            )
            .external_mut(
                "set",
                1,
                |actor: &mut Actor, mut host: HostMut<'_, Schema>, amount: i64| {
                    actor.0 = amount;
                    *host.get_mut() += amount;
                },
            )
            .nondeterministic_mut(
                "add",
                1,
                |actor: &mut Actor, mut host: HostMut<'_, Schema>, amount: i64| {
                    actor.0 += amount;
                    *host.get_mut() += amount;
                },
            );
        let mut lua = RustiaBuilder::standard()
            .without_stdout()
            .build_for_host::<Schema>()
            .unwrap();
        lua.register_object_type(definition).unwrap();
        let actor = lua.create_object(Actor(2)).unwrap();
        lua.globals()
            .unwrap()
            .set_live(&mut lua, "actor", &actor.as_value())
            .unwrap();
        let mut external = 10;
        for input in [3, 7] {
            let slots = 5;
            let mut context = Context {
                input: &input,
                slots: &slots,
                external: &mut external,
            };
            let outcome = lua.invoke(&mut context, InvocationLimits::new(10_000).with_max_records(2).with_max_record_bytes(64), |call| {
                call.load("actor:emit(8); actor:write(9); return {actor:input(), actor:slot(), actor:external()}")
                    .eval::<Vec<i64>>()
            }).unwrap();
            assert_eq!(outcome.value, vec![input + 2, 7, 12]);
            assert_eq!(outcome.records, [(0, "effect", 10), (1, "slot", 11)]);
        }
        let mut context = Context {
            input: &0,
            slots: &0,
            external: &mut external,
        };
        lua.invoke(
            &mut context,
            InvocationLimits::new(10_000)
                .with_max_records(2)
                .with_max_record_bytes(64),
            |call| {
                call.load("actor:external_add(1); actor:set(4); actor:add(3); error('later')")
                    .exec()
            },
        )
        .unwrap_err();
        assert_eq!(external, 20);
        let mut context = Context {
            input: &0,
            slots: &0,
            external: &mut external,
        };
        let outcome = lua
            .invoke(
                &mut context,
                InvocationLimits::new(10_000)
                    .with_max_records(2)
                    .with_max_record_bytes(64),
                |call| call.load("return actor:input()").eval::<i64>(),
            )
            .unwrap();
        assert_eq!(outcome.value, 7);
        assert!(outcome.records.is_empty());
    }

    #[test]
    fn contextual_adapters_support_eight_explicit_arguments() {
        let definition = ObjectType::<Actor, Schema>::new("Actor")
            .input(
                "sum",
                1,
                |actor: &Actor,
                 input: Input<'_, Schema>,
                 a: i64,
                 b: i64,
                 c: i64,
                 d: i64,
                 e: i64,
                 f: i64,
                 g: i64,
                 h: i64| { actor.0 + input.get() + a + b + c + d + e + f + g + h },
            )
            .external_mut(
                "sum_mut",
                1,
                |actor: &mut Actor,
                 mut host: HostMut<'_, Schema>,
                 a: i64,
                 b: i64,
                 c: i64,
                 d: i64,
                 e: i64,
                 f: i64,
                 g: i64,
                 h: i64| {
                    actor.0 += a + b + c + d + e + f + g + h;
                    *host.get_mut() += actor.0;
                    actor.0
                },
            );
        let mut lua = RustiaBuilder::standard()
            .without_stdout()
            .build_for_host::<Schema>()
            .unwrap();
        lua.register_object_type(definition).unwrap();
        let actor = lua.create_object(Actor(2)).unwrap();
        lua.globals()
            .unwrap()
            .set_live(&mut lua, "actor", &actor.as_value())
            .unwrap();
        let mut external = 0;
        let mut context = Context {
            input: &3,
            slots: &0,
            external: &mut external,
        };
        let outcome = lua
            .invoke(
                &mut context,
                InvocationLimits::new(10_000)
                    .with_max_records(2)
                    .with_max_record_bytes(64),
                |call| {
                    call.load("return {actor:sum(1,2,3,4,5,6,7,8), actor:sum_mut(1,2,3,4,5,6,7,8)}")
                        .eval::<Vec<i64>>()
                },
            )
            .unwrap();
        assert_eq!(outcome.value, vec![41, 38]);
        assert_eq!(external, 38);
    }

    #[test]
    fn wrong_receivers_do_not_project_the_host_context() {
        struct Observe {
            input_reads: std::cell::Cell<u32>,
            external_reads: u32,
            value: i64,
        }
        impl HostContext<Schema> for Observe {
            fn input(&self) -> &i64 {
                self.input_reads.set(self.input_reads.get() + 1);
                &self.value
            }
            fn slots(&self) -> &i64 {
                &self.value
            }
            fn external_mut(&mut self) -> &mut i64 {
                self.external_reads += 1;
                &mut self.value
            }
            fn record_effect(&self, sequence: u32, effect: i64) -> (u32, &'static str, i64) {
                (sequence, "effect", effect)
            }
            fn record_slot_write(&self, sequence: u32, write: i64) -> (u32, &'static str, i64) {
                (sequence, "slot", write)
            }
            fn record_bytes(&self, _: &(u32, &'static str, i64)) -> u64 {
                16
            }
        }
        let mut lua = RustiaBuilder::standard()
            .without_stdout()
            .build_for_host::<Schema>()
            .unwrap();
        lua.register_object_type(
            ObjectType::<Actor, Schema>::new("Actor")
                .input("read", 1, |_: &Actor, input: Input<'_, Schema>| {
                    *input.get()
                })
                .external_mut(
                    "change",
                    1,
                    |_: &mut Actor, mut host: HostMut<'_, Schema>| *host.get_mut() += 1,
                ),
        )
        .unwrap();
        let actor = lua.create_object(Actor(2)).unwrap();
        lua.globals()
            .unwrap()
            .set_live(&mut lua, "actor", &actor.as_value())
            .unwrap();
        let mut context = Observe {
            input_reads: std::cell::Cell::new(0),
            external_reads: 0,
            value: 9,
        };
        let outcome = lua.invoke(&mut context, InvocationLimits::new(10_000), |call| {
            call.load("assert(not pcall(actor.read, {})); assert(not pcall(actor.change, {})); return actor:read()")
                .eval::<i64>()
        }).unwrap();
        assert_eq!(outcome.value, 9);
        assert_eq!(context.input_reads.get(), 1);
        assert_eq!(context.external_reads, 0);
    }
}