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
// Lua support (for different versions)

#[cfg(not(any(feature = "Lua5_3", feature = "Lua5_4")))]
compile_error!("no feature for supported Lua version");

use super::{cmach::cmach_lua_t, *};
use sandkiste::prelude::*;

use std::borrow::{Cow, Cow::Borrowed, Cow::Owned};
use std::error::Error;
use std::ffi::{c_int, c_void, CString};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::num::TryFromIntError;
use std::rc::Rc;
use std::slice;
use std::str;

/// Virtual machine that executes Lua code
///
/// # Basic usage
///
/// 1. A new virtual machine can be created with [`LuaMachine::new`].
/// 2. Use [`LuaMachine::load_stdlib_sealed`] to load a sealed version of the
///    Lua standard library; that is a modified version which does not allow
///    breaking out of the sandbox.
///    Alternatively, a full version of the standard library can be loaded with
///    [`LuaMachine::load_stdlib`], which, for example, can be used to load
///    further dependencies, and then be sealed afterwards with
///    [`LuaMachine::seal`]. Note, however, that this may introduce security
///    vulnerabilites depending on which libraries have been loaded or how the
///    state has been modified prior to calling `seal`.
/// 3. Set resource limits with [`LuaMachine::set_memory_limit`] and
///    [`LuaMachine::set_execution_limit`], if desired.
/// 4. Use the API provided by the [`sandkiste`] crate to compile and execute
///    Lua programs. For a concrete example using Lua, see the
///    [top-level module documentation of `sandkiste_lua`](crate).
///
/// # Lifetimes
///
/// Lifetime argument `'a` is a lower bound for closures passed to the Lua
/// machine. It can be inferred automatically.
#[derive(Debug)]
pub struct LuaMachine<'a> {
    c_machine: *mut cmach_lua_t,
    phantom: PhantomData<fn(&'a ()) -> &'a ()>,
}

/// A `LuaMachine` is only equal to itself
impl<'a> PartialEq for LuaMachine<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.c_machine == other.c_machine
    }
}

/// A `LuaMachine` is only equal to itself
impl<'a> Eq for LuaMachine<'a> {}

impl<'a> Hash for LuaMachine<'a> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.c_machine.hash(state);
    }
}

impl<'a> Machine<'a> for LuaMachine<'a> {
    type Datum<'b, 'c> = LuaDatum<'a, 'b, 'c>
    where
        Self: 'b;
    type Function<'b> = LuaFunction<'a, 'b>
    where
        Self: 'b;
}

/// Key used to reference values that are stored in the Lua registry
///
/// Lua objects that are referenced from Rust are stored with a numeric key in
/// the Lua registry. This type reflects the numeric key used in the Lua
/// registry and is an alias to [`c_int`]. It can be used to manually interact
/// with the Lua machine and is normally not needed for user code.
/// See [`LuaMachine::get_reference_key`].
pub type LuaReferenceKey = c_int;

#[derive(Debug)]
struct LuaInnerReference<'a, 'b> {
    machine: &'b LuaMachine<'a>,
    key: LuaReferenceKey,
    identity: *const c_void,
}

impl<'a, 'b> PartialEq for LuaInnerReference<'a, 'b> {
    fn eq(&self, other: &Self) -> bool {
        self.identity == other.identity
    }
}

impl<'a, 'b> Eq for LuaInnerReference<'a, 'b> {}

impl<'a, 'b> Hash for LuaInnerReference<'a, 'b> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.identity.hash(state);
    }
}

/// Reference to value inside Lua machine
///
/// This type reflects an opaque reference to a value inside the Lua machine.
/// A `LuaReference` may be obtained by calling [`MaybeOpaque::try_as_opaque`]
/// on a [`LuaDatum`]. A `LuaReference` may be cloned, compared, or hashed, but
/// provides no other means of accessing the underlying value.
///
/// # Lifetimes
///
/// Lifetime argument `'a` corresponds to the lifetime argument `'a` of
/// [`LuaMachine`]. It is a lower bound for closures passed to the Lua machine.
///
/// Lifetime argument `'b` corresponds to the lifetime of the shared reference
/// to the `LuaMachine` being worked with.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LuaReference<'a, 'b>(Rc<LuaInnerReference<'a, 'b>>);

impl<'a, 'b> LuaReference<'a, 'b> {
    fn new(inner_ref: LuaInnerReference<'a, 'b>) -> Self {
        LuaReference(Rc::new(inner_ref))
    }
}

impl<'a, 'b> AsRef<LuaInnerReference<'a, 'b>> for LuaReference<'a, 'b> {
    fn as_ref(&self) -> &LuaInnerReference<'a, 'b> {
        self.0.as_ref()
    }
}

/// Lua function
///
/// A function inside the Lua machine can be referenced from Rust through a
/// `LuaFunction`. See [`Function`] trait for information on how to use this
/// type.
///
/// # Lifetimes
///
/// Lifetime argument `'a` corresponds to the lifetime argument `'a` of
/// [`LuaMachine`]. It is a lower bound for closures passed to the Lua machine.
///
/// Lifetime argument `'b` corresponds to the lifetime of the shared reference
/// to the `LuaMachine` being worked with.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LuaFunction<'a, 'b> {
    lua_ref: LuaReference<'a, 'b>,
    chunk_name: Option<String>,
}

#[derive(Clone, Debug)]
enum LuaType<'a, 'b, 'c> {
    Nil,
    Boolean(bool),
    Function(LuaFunction<'a, 'b>),
    Float(cmach::lua_Number),
    Integer(cmach::lua_Integer),
    String(Cow<'c, str>),
    Binary(Cow<'c, [u8]>),
    Table(LuaReference<'a, 'b>),
    Userdata(LuaReference<'a, 'b>),
    Thread(LuaReference<'a, 'b>),
}

/// Lua value (of any supported type)
///
/// A `LuaDatum` is used to pass values from Rust to Lua and vice versa. It can
/// contain values of several types (dynamic typing). See module
/// [`sandkiste::types`] for more information.
///
/// # Lifetimes
///
/// Lifetime argument `'a` corresponds to the lifetime argument `'a` of
/// [`LuaMachine`]. It is a lower bound for closures passed to the Lua machine.
///
/// Lifetime argument `'b` corresponds to the lifetime of the shared reference
/// to the `LuaMachine` being worked with.
///
/// Lifetime argument `'c` is often `'static` but can be a shorter lifetime for
/// temporary `LuaDatum`s created from a `&'c str` reference, for example.
#[derive(Clone, Debug)]
pub struct LuaDatum<'a, 'b, 'c>(LuaType<'a, 'b, 'c>);

impl<'a, 'b, 'c, T> From<Option<T>> for LuaDatum<'a, 'b, 'c>
where
    T: Into<LuaDatum<'a, 'b, 'c>>,
{
    fn from(value: Option<T>) -> Self {
        match value {
            None => LuaDatum::null(),
            Some(v) => v.into(),
        }
    }
}

impl<'a, 'b, 'c> Nullable for LuaDatum<'a, 'b, 'c> {
    fn null() -> Self {
        LuaDatum(LuaType::Nil)
    }
    fn is_null(&self) -> bool {
        match self.0 {
            LuaType::Nil => true,
            _ => false,
        }
    }
}

impl<'a, 'b, 'c> From<bool> for LuaDatum<'a, 'b, 'c> {
    fn from(value: bool) -> Self {
        LuaDatum(LuaType::Boolean(value))
    }
}

impl<'a, 'b, 'c> MaybeBoolean for LuaDatum<'a, 'b, 'c> {
    fn try_as_bool(&self) -> Result<bool, DatumViewError> {
        match self.0 {
            LuaType::Boolean(false) => Ok(false),
            LuaType::Boolean(true) => Ok(true),
            _ => Err(DatumViewError::new("boolean expected")),
        }
    }
}

impl<'a, 'b, 'c> From<LuaFunction<'a, 'b>> for LuaDatum<'a, 'b, 'c> {
    fn from(value: LuaFunction<'a, 'b>) -> Self {
        LuaDatum(LuaType::Function(value))
    }
}

impl<'a, 'b, 'c> MaybeFunction for LuaDatum<'a, 'b, 'c> {
    type Function = LuaFunction<'a, 'b>;
    type FunctionRef<'d> = &'d LuaFunction<'a, 'b>
    where
        Self: 'd;
    fn try_as_function(&self) -> Result<&LuaFunction<'a, 'b>, DatumViewError> {
        match self.0 {
            LuaType::Function(ref f) => Ok(f),
            _ => Err(DatumViewError::new("function expected")),
        }
    }
}

impl<'a, 'b, 'c> TryFrom<LuaDatum<'a, 'b, 'c>> for LuaReference<'a, 'b> {
    type Error = DatumConversionError<LuaDatum<'a, 'b, 'c>>;
    fn try_from(value: LuaDatum<'a, 'b, 'c>) -> Result<Self, Self::Error> {
        match value.0 {
            LuaType::Function(f) => Ok(f.lua_ref),
            LuaType::Table(r) => Ok(r),
            LuaType::Userdata(r) => Ok(r),
            LuaType::Thread(r) => Ok(r),
            _ => Err(DatumConversionError::new(value, "reference type expected")),
        }
    }
}

impl<'a, 'b, 'c> MaybeOpaque for LuaDatum<'a, 'b, 'c> {
    type Opaque = LuaReference<'a, 'b>;
    type OpaqueRef<'d> = &'d LuaReference<'a, 'b>
    where
        Self: 'd;
    fn is_opaque(&self) -> bool {
        match self.0 {
            LuaType::Function(_) => true,
            LuaType::Table(_) => true,
            LuaType::Userdata(_) => true,
            LuaType::Thread(_) => true,
            _ => false,
        }
    }
    fn try_as_opaque(&self) -> Result<&LuaReference<'a, 'b>, DatumViewError> {
        match self.0 {
            LuaType::Function(ref f) => Ok(&f.lua_ref),
            LuaType::Table(ref r) => Ok(r),
            LuaType::Userdata(ref r) => Ok(r),
            LuaType::Thread(ref r) => Ok(r),
            _ => Err(DatumViewError::new("reference type expected")),
        }
    }
}

impl<'a, 'b, 'c> From<String> for LuaDatum<'a, 'b, 'c> {
    fn from(value: String) -> LuaDatum<'a, 'b, 'c> {
        LuaDatum(LuaType::String(Owned(value)))
    }
}

impl<'a, 'b, 'c> From<&'c str> for LuaDatum<'a, 'b, 'c> {
    fn from(value: &'c str) -> LuaDatum<'a, 'b, 'c> {
        LuaDatum(LuaType::String(Borrowed(value)))
    }
}

impl<'a, 'b, 'c> TryFrom<LuaDatum<'a, 'b, 'c>> for String {
    type Error = DatumConversionError<LuaDatum<'a, 'b, 'c>>;
    fn try_from(value: LuaDatum<'a, 'b, 'c>) -> Result<Self, Self::Error> {
        match value.0 {
            LuaType::String(s) => Ok(s.into_owned()),
            LuaType::Binary(_) => Err(DatumConversionError::new(
                value,
                "Lua string contains invalid UTF-8 sequence",
            )),
            _ => Err(DatumConversionError::new(value, "string expected")),
        }
    }
}

impl<'a, 'b, 'c> MaybeString<'c> for LuaDatum<'a, 'b, 'c> {
    fn try_as_str(&self) -> Result<&str, DatumViewError> {
        match self.0 {
            LuaType::String(ref s) => Ok(s),
            LuaType::Binary(_) => Err(DatumViewError::new(
                "Lua string contains invalid UTF-8 sequence",
            )),
            _ => Err(DatumViewError::new("string expected")),
        }
    }
}

impl<'a, 'b, 'c> From<Vec<u8>> for LuaDatum<'a, 'b, 'c> {
    fn from(value: Vec<u8>) -> LuaDatum<'a, 'b, 'c> {
        match String::from_utf8(value) {
            Ok(s) => LuaDatum(LuaType::String(Owned(s))),
            Err(err) => LuaDatum(LuaType::Binary(Owned(err.into_bytes()))),
        }
    }
}

impl<'a, 'b, 'c> From<&'c [u8]> for LuaDatum<'a, 'b, 'c> {
    fn from(value: &'c [u8]) -> LuaDatum<'a, 'b, 'c> {
        match str::from_utf8(value) {
            Ok(s) => LuaDatum(LuaType::String(Borrowed(s))),
            Err(_) => LuaDatum(LuaType::Binary(Borrowed(value))),
        }
    }
}

impl<'a, 'b, 'c> TryFrom<LuaDatum<'a, 'b, 'c>> for Vec<u8> {
    type Error = DatumConversionError<LuaDatum<'a, 'b, 'c>>;
    fn try_from(value: LuaDatum<'a, 'b, 'c>) -> Result<Self, Self::Error> {
        match value.0 {
            LuaType::String(s) => Ok(s.into_owned().into()),
            LuaType::Binary(b) => Ok(b.into_owned()),
            _ => Err(DatumConversionError::new(value, "binary string expected")),
        }
    }
}

impl<'a, 'b, 'c> MaybeBinary<'c> for LuaDatum<'a, 'b, 'c> {
    fn try_as_bin(&self) -> Result<&[u8], DatumViewError> {
        match self.0 {
            LuaType::String(ref s) => Ok(s.as_bytes()),
            LuaType::Binary(ref b) => Ok(b),
            _ => Err(DatumViewError::new("binary string expected")),
        }
    }
}

impl<'a, 'b, 'c> From<f64> for LuaDatum<'a, 'b, 'c> {
    fn from(value: f64) -> Self {
        LuaDatum(LuaType::Float(value))
    }
}

impl<'a, 'b, 'c> From<f32> for LuaDatum<'a, 'b, 'c> {
    fn from(value: f32) -> Self {
        LuaDatum(LuaType::Float(value as f64))
    }
}

impl<'a, 'b, 'c> MaybeFloat for LuaDatum<'a, 'b, 'c> {
    fn try_as_f64(&self) -> Result<f64, DatumViewError> {
        match self.0 {
            LuaType::Float(value) => Ok(value as f64),
            LuaType::Integer(value) => Ok(value as f64),
            _ => Err(DatumViewError::new("float (or integer) expected")),
        }
    }
}

impl<'a, 'b, 'c> TryFrom<usize> for LuaDatum<'a, 'b, 'c> {
    type Error = TryFromIntError;
    fn try_from(value: usize) -> Result<Self, Self::Error> {
        Ok(LuaDatum(LuaType::Integer(value.try_into()?)))
    }
}

impl<'a, 'b, 'c> TryFrom<isize> for LuaDatum<'a, 'b, 'c> {
    type Error = TryFromIntError;
    fn try_from(value: isize) -> Result<Self, Self::Error> {
        Ok(LuaDatum(LuaType::Integer(value.try_into()?)))
    }
}

impl<'a, 'b, 'c> TryFrom<u64> for LuaDatum<'a, 'b, 'c> {
    type Error = TryFromIntError;
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        Ok(LuaDatum(LuaType::Integer(value.try_into()?)))
    }
}

impl<'a, 'b, 'c> TryFrom<i64> for LuaDatum<'a, 'b, 'c> {
    type Error = TryFromIntError;
    fn try_from(value: i64) -> Result<Self, Self::Error> {
        Ok(LuaDatum(LuaType::Integer(value.try_into()?)))
    }
}

impl<'a, 'b, 'c> TryFrom<u32> for LuaDatum<'a, 'b, 'c> {
    type Error = TryFromIntError;
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        Ok(LuaDatum(LuaType::Integer(value.try_into()?)))
    }
}

impl<'a, 'b, 'c> From<i32> for LuaDatum<'a, 'b, 'c> {
    fn from(value: i32) -> Self {
        LuaDatum(LuaType::Integer(value.into()))
    }
}

impl<'a, 'b, 'c> From<u16> for LuaDatum<'a, 'b, 'c> {
    fn from(value: u16) -> Self {
        LuaDatum(LuaType::Integer(value.into()))
    }
}

impl<'a, 'b, 'c> From<i16> for LuaDatum<'a, 'b, 'c> {
    fn from(value: i16) -> Self {
        LuaDatum(LuaType::Integer(value.into()))
    }
}

impl<'a, 'b, 'c> From<u8> for LuaDatum<'a, 'b, 'c> {
    fn from(value: u8) -> Self {
        LuaDatum(LuaType::Integer(value.into()))
    }
}

impl<'a, 'b, 'c> From<i8> for LuaDatum<'a, 'b, 'c> {
    fn from(value: i8) -> Self {
        LuaDatum(LuaType::Integer(value.into()))
    }
}

impl<'a, 'b, 'c> MaybeInteger for LuaDatum<'a, 'b, 'c> {
    fn try_as_i64(&self) -> Result<i64, DatumViewError> {
        match self.0 {
            LuaType::Integer(value) => value
                .try_into()
                .map_err(|_| DatumViewError::new("Lua integer does not fit into i64")),
            LuaType::Float(_) => Err(DatumViewError::new("integer expected, but got float")),
            _ => Err(DatumViewError::new("integer expected")),
        }
    }
}

impl<'a, 'b, 'c> MaybeArray for LuaDatum<'a, 'b, 'c> {
    type Element<'d> = LuaDatum<'a, 'b, 'd>;
    fn try_array(&self) -> Result<(), DatumViewError> {
        match self.0 {
            LuaType::Table(_) => Ok(()),
            _ => Err(DatumViewError::new("array (table) expected")),
        }
    }
    fn array_len(&self) -> Result<usize, MachineError> {
        match self.0 {
            LuaType::Table(ref lua_ref) => {
                let machine = lua_ref.as_ref().machine;
                let l = machine.lua_state();
                unsafe {
                    machine.push_reference(lua_ref);
                    machine.pop_error_with_backtrace(cmach::cmach_lua_len(l))?;
                    let result = machine.pop_datum()?;
                    Ok(result.try_as_usize()?)
                }
            }
            _ => Err(self.try_array().unwrap_err())?,
        }
    }
    fn array_get(&self, index: usize) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        match self.0 {
            LuaType::Table(ref lua_ref) => {
                let machine = lua_ref.as_ref().machine;
                let l = machine.lua_state();
                unsafe {
                    let index = cmach::lua_Integer::try_from(index)
                        .ok()
                        .and_then(|x| x.checked_add(1))
                        .ok_or_else(|| MachineError {
                            message: Some("lua_Integer too small to store array index".to_string()),
                            ..Default::default()
                        })?;
                    machine.push_reference(lua_ref);
                    cmach::lua_pushinteger(l, index);
                    machine.pop_error_with_backtrace(cmach::cmach_lua_gettable(l))?;
                    Ok(machine.pop_datum()?)
                }
            }
            _ => Err(self.try_array().unwrap_err())?,
        }
    }
    fn array_set<'d>(
        &self,
        index: usize,
        element: LuaDatum<'a, 'b, 'd>,
    ) -> Result<(), MachineError> {
        match self.0 {
            LuaType::Table(ref lua_ref) => {
                let machine = lua_ref.as_ref().machine;
                let l = machine.lua_state();
                unsafe {
                    let index = cmach::lua_Integer::try_from(index)
                        .ok()
                        .and_then(|x| x.checked_add(1))
                        .ok_or_else(|| MachineError {
                            message: Some("lua_Integer too small to store array index".to_string()),
                            ..Default::default()
                        })?;
                    machine.push_reference(lua_ref);
                    cmach::lua_pushinteger(l, index);
                    match machine.push_datum(&element) {
                        Ok(_) => (),
                        Err(err) => {
                            cmach::lua_pop(l, 2);
                            Err(err)?
                        }
                    }
                    machine.pop_error_with_backtrace(cmach::cmach_lua_settable(l))?;
                    Ok(())
                }
            }
            _ => Err(self.try_array().unwrap_err())?,
        }
    }
    fn array_push<'d>(&self, element: LuaDatum<'a, 'b, 'd>) -> Result<(), MachineError> {
        self.array_set(self.array_len()?, element)
    }
    fn array_truncate(&self, len: usize) -> Result<(), MachineError> {
        for index in (len..self.array_len()?).rev() {
            self.array_set(index, LuaDatum::null())?;
        }
        Ok(())
    }
}

impl<'a, 'b, 'c> MaybeStringMap for LuaDatum<'a, 'b, 'c> {
    type Value<'d> = LuaDatum<'a, 'b, 'd>;
    fn try_string_map(&self) -> Result<(), DatumViewError> {
        match self.0 {
            LuaType::Table(_) => Ok(()),
            _ => Err(DatumViewError::new("string map (table) expected")),
        }
    }
    fn string_map_get(&self, key: &str) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        match self.0 {
            LuaType::Table(ref lua_ref) => {
                let machine = lua_ref.as_ref().machine;
                let l = machine.lua_state();
                unsafe {
                    machine.push_reference(lua_ref);
                    match machine.push_str(key) {
                        Ok(_) => (),
                        Err(err) => {
                            cmach::lua_pop(l, 1);
                            Err(err)?
                        }
                    }
                    machine.pop_error_with_backtrace(cmach::cmach_lua_gettable(l))?;
                    Ok(machine.pop_datum()?)
                }
            }
            _ => Err(self.try_string_map().unwrap_err())?,
        }
    }
    fn string_map_set<'d, 'e>(
        &self,
        key: &'d str,
        value: LuaDatum<'a, 'b, 'e>,
    ) -> Result<(), MachineError> {
        match self.0 {
            LuaType::Table(ref lua_ref) => {
                let machine = lua_ref.as_ref().machine;
                let l = machine.lua_state();
                unsafe {
                    machine.push_reference(lua_ref);
                    match machine.push_str(key) {
                        Ok(_) => (),
                        Err(err) => {
                            cmach::lua_pop(l, 1);
                            Err(err)?
                        }
                    }
                    match machine.push_datum(&value) {
                        Ok(_) => (),
                        Err(err) => {
                            cmach::lua_pop(l, 2);
                            Err(err)?
                        }
                    }
                    machine.pop_error_with_backtrace(cmach::cmach_lua_settable(l))?;
                    Ok(())
                }
            }
            _ => Err(self.try_string_map().unwrap_err())?,
        }
    }
}

impl<'a> HasArray<'a> for LuaMachine<'a> {
    fn new_empty_array<'b>(&'b self) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        self.new_table()
    }
}

impl<'a> HasStringMap<'a> for LuaMachine<'a> {
    fn new_empty_string_map<'b>(&'b self) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        self.new_table()
    }
}

/// (Sub-)module in a [`LuaMachine`] (which is a Lua table)
///
/// The type `LuaModule` is used to implement the abstract [`Module`] trait
/// which allows getting and setting variables (through [`Module::get`] and
/// [`Module::set`], respectively) a module (in this case: in a Lua table).
/// A `LuaModule` may be obtained by calling [`HasModules::module`] on the
/// [`LuaMachine`] (to obtain a top-level module/table) or [`Module::module`]
/// on a `LuaModule` (to obtain a sub module/table, respectively).
///
/// To set global variables, use [`Globals::get`] and [`Globals::set`] directly
/// on the [`LuaMachine`] instead.
///
/// # Lifetimes
///
/// Lifetime argument `'a` corresponds to the lifetime argument `'a` of
/// [`LuaMachine`]. It is a lower bound for closures passed to the Lua machine.
///
/// Lifetime argument `'b` corresponds to the lifetime of the shared reference
/// to the `LuaMachine` being worked with.
pub struct LuaModule<'a, 'b> {
    machine: &'b LuaMachine<'a>,
    table: LuaDatum<'a, 'b, 'static>,
}

impl<'a> HasModules<'a> for LuaMachine<'a> {
    type Module<'b> = LuaModule<'a, 'b>
    where
        'a: 'b;
    fn module<'b>(&'b self, name: &str) -> Result<LuaModule<'a, 'b>, MachineError> {
        let mut table = self.get(name)?;
        if table.is_null() {
            table = self.new_empty_string_map()?;
            self.set(name, table.clone())?;
        }
        Ok(LuaModule {
            machine: self,
            table,
        })
    }
}

impl<'a, 'b> Module<'a, 'b> for LuaModule<'a, 'b> {
    type Machine = LuaMachine<'a>;
    /// Open a sub-module (create if non-existent)
    fn module(&self, name: &str) -> Result<Self, MachineError> {
        let mut table = self.table.string_map_get(name)?;
        if table.is_null() {
            table = self.machine.new_empty_string_map()?;
            self.table.string_map_set(name, table.clone())?;
        }
        Ok(LuaModule {
            machine: self.machine,
            table,
        })
    }
    /// Get value from module
    fn get(
        &self,
        name: &str,
    ) -> Result<<Self::Machine as Machine<'a>>::Datum<'b, 'static>, MachineError> {
        Ok(self.table.string_map_get(name)?)
    }
    /// Set value in module
    fn set<'c>(
        &self,
        name: &str,
        datum: <Self::Machine as Machine<'a>>::Datum<'b, 'c>,
    ) -> Result<(), MachineError> {
        self.table.string_map_set(name, datum)?;
        Ok(())
    }
}

impl<'a, 'b> Drop for LuaInnerReference<'a, 'b> {
    fn drop(&mut self) {
        unsafe {
            cmach::luaL_unref(
                self.machine.lua_state(),
                cmach::LUA_REGISTRYINDEX.try_into().unwrap(),
                self.key,
            )
        };
    }
}

impl<'a> Drop for LuaMachine<'a> {
    fn drop(&mut self) {
        unsafe {
            cmach::cmach_lua_free(self.c_machine);
        };
    }
}

impl<'a> Globals<'a> for LuaMachine<'a> {
    fn get<'b>(&'b self, name: &str) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        self.assert_empty_stack();
        unsafe {
            let l = self.lua_state();
            self.push_str(name)?;
            self.pop_error_with_backtrace(cmach::cmach_lua_getglobal(l))?;
            self.pop_datum()
        }
    }
    fn set<'b, 'c>(&'b self, name: &str, value: LuaDatum<'a, 'b, 'c>) -> Result<(), MachineError> {
        self.assert_empty_stack();
        unsafe {
            let l = self.lua_state();
            self.push_str(name)?;
            match self.push_datum(&value) {
                Ok(()) => (),
                Err(err) => {
                    cmach::lua_pop(l, 1);
                    Err(err)?;
                }
            }
            self.pop_error_with_backtrace(cmach::cmach_lua_setglobal(l))?;
            Ok(())
        }
    }
}

impl<'a> LuaMachine<'a> {
    /// Create a bare Lua machine
    pub fn new() -> Self {
        unsafe {
            let c_machine: *mut cmach_lua_t = cmach::cmach_lua_new();
            if c_machine.is_null() {
                panic!("memory allocation error in cmach_lua_new");
            }
            // `LuaMachine::drop` ensures cleanup
            LuaMachine {
                c_machine,
                phantom: PhantomData,
            }
        }
    }
    /// Retrieve pointer to `lua_State`
    pub fn lua_state(&self) -> *mut cmach::lua_State {
        unsafe { (*self.c_machine).L }
    }
    /// Assert that Lua stack is empty
    pub fn assert_empty_stack(&self) {
        unsafe {
            let l = self.lua_state();
            assert_eq!(cmach::lua_gettop(l), 0, "unexpected values on Lua stack");
        }
    }
    /// Set limit for executed instructions
    pub fn set_execution_limit(&self, instr: u64) {
        unsafe {
            let instr = c_int::try_from(instr).unwrap_or(c_int::MAX);
            cmach::cmach_lua_execlimit(self.c_machine, instr);
        }
    }
    /// Set memory limit
    pub fn set_memory_limit(&self, bytes: usize) {
        unsafe {
            (*self.c_machine).mem_limit = bytes;
        }
    }
    /// Load standard library
    pub fn load_stdlib(&self) -> Result<(), MachineError> {
        self.assert_empty_stack();
        unsafe {
            let l = self.lua_state();
            self.pop_error_with_backtrace(cmach::cmach_lua_openlibs(l))?;
            Ok(())
        }
    }
    /// Load sealed version of standard library
    pub fn load_stdlib_sealed(&self) -> Result<(), MachineError> {
        self.assert_empty_stack();
        unsafe {
            let l = self.lua_state();
            self.pop_error_with_backtrace(cmach::cmach_lua_openlibs_sealed(l))?;
            Ok(())
        }
    }
    /// Remove certain parts of standard library to seal machine as sandbox
    pub fn seal(&self) -> Result<(), MachineError> {
        self.assert_empty_stack();
        unsafe {
            let l = self.lua_state();
            self.pop_error_with_backtrace(cmach::cmach_lua_seal(l))?;
            Ok(())
        }
    }
    /// Create empty table
    pub fn new_table<'b>(&'b self) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        self.assert_empty_stack();
        unsafe {
            let l = self.lua_state();
            self.pop_error_with_backtrace(cmach::cmach_lua_newtable(l))?;
            let result = self.extract_datum(-1);
            cmach::lua_pop(l, 1);
            result
        }
    }
    /// Get [`LuaReferenceKey`] for [`LuaReference`]
    pub fn get_reference_key<'b>(&'b self, lua_ref: &LuaReference<'a, 'b>) -> LuaReferenceKey {
        let inner_ref = lua_ref.as_ref();
        assert_eq!(
            self, inner_ref.machine,
            "reference used on wrong Lua machine"
        );
        inner_ref.key
    }
    /// Pop error value from Lua stack and convert into [`String`]
    pub unsafe fn pop_error_message(&self) -> String {
        let l = self.lua_state();
        let message = if cmach::cmach_lua_touserstr(l) == cmach::LUA_OK.try_into().unwrap() {
            let udata = cmach::lua_touserdata(l, -1);
            let len = cmach::lua_rawlen(l, -1);
            String::from_utf8_lossy(slice::from_raw_parts(udata as *const u8, len as usize))
                .into_owned()
        } else {
            "(unknown)".to_string()
        };
        cmach::lua_pop(l, 1);
        message
    }
    /// If `status != LUA_OK`, pop error value from Lua stack and convert into [`MachineError`]
    pub unsafe fn pop_error(&self, status: c_int) -> Result<(), MachineError> {
        if status == cmach::LUA_OK.try_into().unwrap() {
            Ok(())
        } else {
            let is_execlimit = cmach::cmach_lua_is_execlimit(self.c_machine) != 0;
            let message = Some(self.pop_error_message());
            Err(MachineError {
                kind: if status == cmach::LUA_ERRMEM.try_into().unwrap() {
                    MachineErrorKind::Memory
                } else if is_execlimit {
                    MachineErrorKind::ExecLimit
                } else if status == cmach::LUA_ERRSYNTAX.try_into().unwrap() {
                    MachineErrorKind::Syntax
                } else if status == cmach::LUA_ERRRUN.try_into().unwrap() {
                    MachineErrorKind::Runtime
                } else {
                    MachineErrorKind::Unknown
                },
                message,
                message_incl_chunk_name: true,
                message_incl_pos: true,
                ..Default::default()
            })
        }
    }
    /// Same as [`LuaMachine::pop_error`] but also extract traceback
    /// (requires `cmach_lua_errmsgh` to be used during `lua_pcall`)
    pub unsafe fn pop_error_with_backtrace(&self, status: c_int) -> Result<(), MachineError> {
        if status == cmach::LUA_ERRRUN.try_into().unwrap() {
            let l = self.lua_state();
            cmach::lua_rawgeti(l, -1, 1);
            let is_execlimit = cmach::cmach_lua_is_execlimit(self.c_machine) != 0;
            let message = Some(self.pop_error_message());
            cmach::lua_rawgeti(l, -1, 2);
            let machine_backtrace = Some(self.pop_error_message());
            cmach::lua_rawgeti(l, -1, 3);
            let chunk_name = Some(self.pop_error_message());
            cmach::lua_rawgeti(l, -1, 4);
            let line = usize::try_from(cmach::lua_tointeger(l, -1)).ok();
            cmach::lua_pop(l, 2); // pop line number and error table
            Err(MachineError {
                kind: if is_execlimit {
                    MachineErrorKind::ExecLimit
                } else {
                    MachineErrorKind::Runtime
                },
                message,
                machine_backtrace,
                message_incl_chunk_name: true,
                chunk_name,
                message_incl_pos: true,
                line,
                ..Default::default()
            })
        } else {
            self.pop_error(status)
        }
    }
    /// Convert value on Lua stack into `LuaDatum`
    ///
    /// NOTE: requires some free space on Lua stack for operation
    pub unsafe fn extract_datum<'b>(
        &'b self,
        index: c_int,
    ) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        let l = self.lua_state();
        let type_id = cmach::lua_type(l, index);
        let lua_type = if type_id == cmach::LUA_TNIL.try_into().unwrap() {
            LuaType::Nil
        } else if type_id == cmach::LUA_TBOOLEAN.try_into().unwrap() {
            LuaType::Boolean(cmach::lua_toboolean(l, index) != 0)
        } else if type_id == cmach::LUA_TNUMBER.try_into().unwrap() {
            if cmach::lua_isinteger(l, index) != 0 {
                LuaType::Integer(cmach::lua_tointeger(l, index))
            } else {
                LuaType::Float(cmach::lua_tonumber(l, index))
            }
        } else if type_id == cmach::LUA_TSTRING.try_into().unwrap() {
            cmach::lua_pushvalue(l, index);
            self.pop_error_with_backtrace(cmach::cmach_lua_touserstr(l))?;
            let udata = cmach::lua_touserdata(l, -1);
            let len = cmach::lua_rawlen(l, -1);
            let vec = slice::from_raw_parts(udata as *const u8, len as usize).to_vec();
            cmach::lua_pop(l, 1);
            match String::from_utf8(vec) {
                Ok(s) => LuaType::String(Owned(s)),
                Err(err) => LuaType::Binary(Owned(err.into_bytes())),
            }
        } else {
            let identity = cmach::lua_topointer(l, index);
            cmach::lua_pushvalue(l, index);
            self.pop_error_with_backtrace(cmach::cmach_lua_ref(l))?;
            let key = cmach::lua_tointeger(l, -1) as LuaReferenceKey;
            cmach::lua_pop(l, 1);
            // `LuaInnerReference::drop` ensures cleanup
            let inner_ref = LuaInnerReference {
                machine: &self,
                key,
                identity,
            };
            let lua_ref = LuaReference::new(inner_ref);
            if type_id == cmach::LUA_TFUNCTION.try_into().unwrap() {
                LuaType::Function(LuaFunction {
                    lua_ref,
                    chunk_name: None,
                })
            } else if type_id == cmach::LUA_TTABLE.try_into().unwrap() {
                LuaType::Table(lua_ref)
            } else if type_id == cmach::LUA_TUSERDATA.try_into().unwrap() {
                LuaType::Userdata(lua_ref)
            } else if type_id == cmach::LUA_TTHREAD.try_into().unwrap() {
                LuaType::Thread(lua_ref)
            } else {
                panic!("unexpected type {} on Lua stack", type_id)
            }
        };
        Ok(LuaDatum(lua_type))
    }
    /// Pop value from Lua stack and convert into `LuaDatum`
    ///
    /// NOTE: requires some free space on Lua stack for operation
    pub unsafe fn pop_datum<'b>(&'b self) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> {
        let result = self.extract_datum(-1);
        cmach::lua_pop(self.lua_state(), 1);
        result
    }
    /// Push `LuaReference` on top of Lua stack
    pub unsafe fn push_reference<'b>(&'b self, lua_ref: &LuaReference<'a, 'b>) {
        cmach::lua_rawgeti(
            self.lua_state(),
            cmach::LUA_REGISTRYINDEX.try_into().unwrap(),
            self.get_reference_key(lua_ref) as cmach::lua_Integer,
        );
    }
    /// Push binary data on top of Lua stack
    ///
    /// NOTE: requires some free space on Lua stack for operation
    pub unsafe fn push_bin(&self, binary: &[u8]) -> Result<(), MachineError> {
        self.pop_error_with_backtrace(cmach::cmach_lua_pushlstring(
            self.lua_state(),
            binary.as_ptr() as *const _,
            cmach::lua_Integer::try_from(binary.len()).map_err(|_| MachineError {
                message: Some("lua_Integer too small to store length".to_string()),
                ..Default::default()
            })?,
        ))
    }
    /// Push string on top of Lua stack
    ///
    /// NOTE: requires some free space on Lua stack for operation
    pub unsafe fn push_str(&self, string: &str) -> Result<(), MachineError> {
        self.push_bin(string.as_ref())
    }
    /// Push `LuaDatum` on top of Lua stack
    ///
    /// NOTE: requires some free space on Lua stack for operation
    pub unsafe fn push_datum<'b, 'c>(
        &'b self,
        datum: &LuaDatum<'a, 'b, 'c>,
    ) -> Result<(), MachineError> {
        let l = self.lua_state();
        match datum.0 {
            LuaType::Nil => cmach::lua_pushnil(l),
            LuaType::Boolean(b) => cmach::lua_pushboolean(l, if b { 1 } else { 0 }),
            LuaType::Function(ref f) => self.push_reference(&f.lua_ref),
            LuaType::Float(x) => cmach::lua_pushnumber(l, x),
            LuaType::Integer(i) => cmach::lua_pushinteger(l, i),
            LuaType::String(ref s) => self.push_str(s)?,
            LuaType::Binary(ref b) => self.push_bin(b)?,
            LuaType::Table(ref lua_ref) => self.push_reference(lua_ref),
            LuaType::Userdata(ref lua_ref) => self.push_reference(lua_ref),
            LuaType::Thread(ref lua_ref) => self.push_reference(lua_ref),
        };
        Ok(())
    }
}

impl<'a, 'b> Function for LuaFunction<'a, 'b> {
    type Datum<'c> = LuaDatum<'a, 'b, 'c>;
    fn chunk_name(&self) -> Option<&str> {
        self.chunk_name.as_deref()
    }
    fn call<'c, A>(&self, args: A) -> Result<Vec<LuaDatum<'a, 'b, 'static>>, MachineError>
    where
        A: IntoIterator<Item = LuaDatum<'a, 'b, 'c>>,
        <A as IntoIterator>::IntoIter: ExactSizeIterator,
    {
        let args = args.into_iter();
        unsafe {
            let machine = self.lua_ref.as_ref().machine;
            machine.assert_empty_stack();
            let l = machine.lua_state();
            let old_top = cmach::lua_gettop(l);
            // TODO: use `try` expression here if/when `try_blocks` feature
            // becomes stable:
            let res = (|| {
                cmach::lua_pushcfunction(l, Some(cmach::cmach_lua_errmsgh));
                let base_top = old_top + 1;
                machine.push_reference(&self.lua_ref);
                let argc = c_int::try_from(args.len()).map_err(|_| MachineError {
                    message: Some("C integer too small to store argument count".to_string()),
                    ..Default::default()
                })?;
                let mut remaining_argc = argc;
                if cmach::lua_checkstack(l, argc) == 0 {
                    Err(MachineError {
                        message: Some(
                            "Lua stack exhausted while preparing function arguments".to_string(),
                        ),
                        chunk_name: self.chunk_name.clone(),
                        ..Default::default()
                    })?;
                }
                for arg in args {
                    if remaining_argc == 0 {
                        panic!("ExactSizeIterator for arguments provided more items than reported");
                    }
                    remaining_argc -= 1;
                    machine.push_datum(&arg)?;
                }
                machine.pop_error_with_backtrace(cmach::lua_pcall(
                    l,
                    argc,
                    cmach::LUA_MULTRET as c_int,
                    old_top + 1,
                ))?;
                // ensure there is still some space (10 elements) on Lua stack
                // (some extra space is required by `extract_datum`)
                if cmach::lua_checkstack(l, 10) == 0 {
                    Err(MachineError {
                        message: Some("Lua stack exhausted after calling function".to_string()),
                        chunk_name: self.chunk_name.clone(),
                        ..Default::default()
                    })?;
                }
                let new_top = cmach::lua_gettop(l);
                let mut retvals = Vec::with_capacity((new_top - base_top) as usize);
                for index in base_top..new_top {
                    retvals.push(machine.extract_datum(index + 1)?);
                }
                Ok(retvals)
            })();
            cmach::lua_settop(l, old_top);
            res
        }
    }
}

impl<'a> Compile<'a, String> for LuaMachine<'a> {
    fn compile(
        &self,
        chunk_name: Option<String>,
        code: String,
    ) -> Result<LuaFunction<'a, '_>, MachineError> {
        self.assert_empty_stack();
        let codelen = code.len();
        let code = CString::new(code).map_err(|err| MachineError {
            kind: MachineErrorKind::Syntax,
            message: Some(err.to_string()),
            ..Default::default()
        })?;
        let lua_name: Option<CString> = chunk_name.as_ref().map(|name| {
            let mut lua_name = String::with_capacity(name.len() + 2);
            lua_name.push('=');
            lua_name.push_str(name);
            CString::new(lua_name)
                .unwrap_or_else(|_| CString::new("(invalid name)".to_string()).unwrap())
        });
        let inner_ref = unsafe {
            let l = self.lua_state();
            self.pop_error(cmach::luaL_loadbufferx(
                self.lua_state(),
                code.as_ptr(),
                codelen
                    .try_into()
                    .expect("could not convert integer of code length"),
                // NOTE: taking reference (`&`) in following match is important for `.as_ptr()`!
                match &lua_name {
                    None => code.as_ptr(),
                    Some(n) => n.as_ptr(),
                },
                b"t\0".as_ptr() as *const _,
            ))?;
            let identity = cmach::lua_topointer(l, -1);
            self.pop_error(cmach::cmach_lua_ref(l))?;
            let key = cmach::lua_tointeger(l, -1) as LuaReferenceKey;
            cmach::lua_pop(l, 1);
            // `LuaInnerReference::drop` ensures cleanup
            LuaInnerReference {
                machine: &self,
                key,
                identity,
            }
        };
        Ok(LuaFunction {
            lua_ref: LuaReference::new(inner_ref),
            chunk_name,
        })
    }
}

impl<'a, 'b> Compile<'a, &'b str> for LuaMachine<'a> {
    fn compile(
        &self,
        chunk_name: Option<String>,
        code: &'b str,
    ) -> Result<LuaFunction<'a, '_>, MachineError> {
        self.compile(chunk_name, code.to_string())
    }
}

unsafe extern "C" fn callback_trampoline<F>(context: *mut c_void, nargs: c_int) -> c_int
where
    F: FnMut(c_int) -> c_int,
{
    let closure = &mut *(context as *mut F);
    closure(nargs)
}

fn get_callback_trampoline<F>(_closure: &F) -> cmach::cmach_callback_fptr
where
    F: FnMut(c_int) -> c_int,
{
    Some(callback_trampoline::<F>)
}

unsafe extern "C" fn release_trampoline<F>(context: *mut c_void)
where
    F: FnMut(c_int) -> c_int,
{
    drop(Box::from_raw(context as *mut F));
}

fn get_release_trampoline<F>(_closure: &F) -> cmach::cmach_callback_release_fptr
where
    F: FnMut(c_int) -> c_int,
{
    Some(release_trampoline::<F>)
}

impl<'a> Callback<'a> for LuaMachine<'a> {
    fn callback<'b, 'c, F, R>(&'b self, func: F) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
    where
        R: IntoIterator<Item = Self::Datum<'b, 'c>>,
        <R as IntoIterator>::IntoIter: ExactSizeIterator,
        F: 'a + Fn(Vec<LuaDatum<'a, 'b, 'static>>) -> Result<R, Box<dyn Error>>,
    {
        self.assert_empty_stack();
        let l = self.lua_state();
        let inner_closure = move |nargs| -> Result<c_int, MachineError> {
            unsafe {
                // ensure there is still some space (10 elements) on Lua stack
                // (some extra space is required by `extract_datum`)
                if cmach::lua_checkstack(l, 10) == 0 {
                    Err(MachineError {
                        message: Some("Lua stack exhausted".to_string()),
                        ..Default::default()
                    })?;
                }
                let mut args = Vec::with_capacity(nargs as usize);
                for i in 0..nargs {
                    args.push(self.extract_datum(i - nargs)?);
                }
                cmach::lua_pop(l, nargs);
                match func(args) {
                    Ok(retvals) => {
                        let retvals = retvals.into_iter();
                        // ensure there is still some space (number of
                        // return values + 10 elements) on Lua stack
                        // (some extra space is required by `push_datum`)
                        let retvalc = c_int::try_from(retvals.len()).map_err(|_| MachineError {
                            message: Some(
                                "C integer too small to store return value count".to_string(),
                            ),
                            ..Default::default()
                        })?;
                        let mut remaining_retvalc = retvalc;
                        let space = retvalc.checked_add(10).ok_or_else(|| MachineError {
                            message: Some("return value count too high".to_string()),
                            ..Default::default()
                        })?;
                        if cmach::lua_checkstack(l, space) == 0 {
                            Err(MachineError {
                                message: Some("Lua stack exhausted".to_string()),
                                ..Default::default()
                            })?;
                        }
                        for retval in retvals {
                            if remaining_retvalc == 0 {
                                panic!("ExactSizeIterator for arguments provided more items than reported");
                            }
                            remaining_retvalc -= 1;
                            self.push_datum(&retval)?;
                        }
                        Ok(retvalc)
                    }
                    Err(err) => {
                        self.push_str(&format!("callback returned error: {}", err))?;
                        Ok(-1)
                    }
                }
            }
        };
        let closure = move |nargs| match inner_closure(nargs) {
            Ok(retvals) => retvals,
            Err(err) => {
                let errstr = err.to_string();
                unsafe {
                    let (errstrptr, errlen) = match cmach::lua_Integer::try_from(errstr.len()) {
                        Ok(errlen) => (errstr.as_ptr(), errlen),
                        Err(_) => {
                            const TOOLONG: &'static str = "error message too long";
                            (TOOLONG.as_ptr(), TOOLONG.len() as cmach::lua_Integer)
                        }
                    };
                    cmach::cmach_lua_pushlstring(l, errstrptr as *const _, errlen);
                    // leave error on stack if `cmach_lua_pushlstring` threw error
                }
                -1
            }
        };
        let callback_trampoline = get_callback_trampoline(&closure);
        let release_trampoline = get_release_trampoline(&closure);
        let closure_ptr = Box::into_raw(Box::new(closure));
        unsafe {
            self.pop_error_with_backtrace(cmach::cmach_lua_pushclosure(
                l,
                closure_ptr as *mut c_void,
                callback_trampoline,
                release_trampoline,
            ))?;
            Ok(self.pop_datum()?)
        }
    }
}