Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
96 lines (85 loc) · 2.72 KB
/
Copy pathlib.rs
File metadata and controls
96 lines (85 loc) · 2.72 KB
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
use mlua::{Error, Lua};
#[cfg(target_os = "unknown")]
mod allocator {
use std::alloc::{GlobalAlloc, Layout};
use std::ffi::c_void;
// Rust and WASI libc must share an allocator
struct WasiAllocator;
unsafe extern "C" {
fn posix_memalign(ptr: *mut *mut c_void, align: usize, size: usize) -> i32;
fn free(ptr: *mut c_void);
}
unsafe impl GlobalAlloc for WasiAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut ptr = std::ptr::null_mut();
if unsafe { posix_memalign(&mut ptr, layout.align().max(size_of::<usize>()), layout.size()) } != 0
{
return std::ptr::null_mut();
}
ptr.cast()
}
unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {
unsafe { free(ptr.cast()) };
}
}
#[global_allocator]
static ALLOCATOR: WasiAllocator = WasiAllocator;
}
#[cfg(target_os = "unknown")]
#[unsafe(no_mangle)]
pub extern "C" fn run_tests() {
test_eval();
test_errors();
test_callbacks();
test_coroutines();
}
#[cfg_attr(target_os = "wasi", test)]
fn test_eval() {
let lua = Lua::new();
assert_eq!(lua.load("return 6 * 7").eval::<i32>().unwrap(), 42);
}
#[cfg_attr(target_os = "wasi", test)]
fn test_errors() {
let lua = Lua::new();
assert!(matches!(
lua.load("local =").exec(),
Err(Error::SyntaxError { .. })
));
assert!(matches!(
lua.load("error('test error')").exec(),
Err(Error::RuntimeError(_))
));
assert!(
!lua.load("return pcall(function() error('test error') end)")
.eval::<bool>()
.unwrap()
);
assert_eq!(lua.load("return 42").eval::<i32>().unwrap(), 42);
}
#[cfg_attr(target_os = "wasi", test)]
fn test_callbacks() {
let lua = Lua::new();
let double = lua.create_function(|_, n: i32| Ok(n * 2)).unwrap();
lua.globals().set("double", double).unwrap();
assert_eq!(lua.load("return double(21)").eval::<i32>().unwrap(), 42);
let fail = lua
.create_function(|_, ()| Err::<(), _>(Error::runtime("callback error")))
.unwrap();
lua.globals().set("fail", fail).unwrap();
assert!(matches!(
lua.load("fail()").exec(),
Err(Error::CallbackError { .. })
));
assert!(!lua.load("return pcall(fail)").eval::<bool>().unwrap());
}
#[cfg_attr(target_os = "wasi", test)]
fn test_coroutines() {
let lua = Lua::new();
let function = lua
.load("local n = ...; coroutine.yield(n); return n * 2")
.into_function()
.unwrap();
let thread = lua.create_thread(function).unwrap();
assert_eq!(thread.resume::<i32>(21).unwrap(), 21);
assert_eq!(thread.resume::<i32>(()).unwrap(), 42);
}