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
use std::{
ffi::{CStr, CString},
mem::size_of,
};
pub type StatePtr = *mut dmsdk_ffi::lua_State;
pub type Function = extern "C" fn(l: StatePtr) -> i32;
pub type Reg = &'static [(&'static str, Function)];
#[derive(Clone, Copy)]
pub struct State {
ptr: StatePtr,
}
impl State {
pub unsafe fn new(ptr: StatePtr) -> Self {
Self { ptr }
}
pub fn ptr(&self) -> StatePtr {
self.ptr
}
}
#[macro_export]
macro_rules! declare_functions {
($ident:ident, $($func:ident),*) => {
paste! {
const $ident: lua::Reg = &[$((stringify!($func), [<_wrapped_ $func>]), )*];
$(
#[no_mangle]
extern "C" fn [<_wrapped_ $func>](l: lua::StatePtr) -> i32 {
unsafe {
$func(lua::State::new(l))
}
}
)*
}
};
}
pub fn push_userdata<T>(l: State, userdata: T) {
unsafe {
let ptr = dmsdk_ffi::lua_newuserdata(l.ptr, size_of::<T>()) as *mut T;
ptr.write(userdata);
}
}
pub unsafe fn to_userdata<T>(l: State, i: i32) -> T {
let ptr = dmsdk_ffi::lua_touserdata(l.ptr, i) as *mut T;
ptr.read()
}
pub fn push_integer(l: State, n: isize) {
unsafe {
dmsdk_ffi::lua_pushinteger(l.ptr, n);
}
}
pub fn push_string(l: State, s: &str) {
let s = CString::new(s).unwrap();
unsafe {
dmsdk_ffi::lua_pushstring(l.ptr, s.as_ptr());
}
}
pub fn check_string(l: State, i: i32) -> String {
unsafe {
let ptr = dmsdk_ffi::luaL_checklstring(l.ptr, i, std::ptr::null_mut());
let cstr = CStr::from_ptr(ptr);
String::from_utf8_lossy(cstr.to_bytes()).into_owned()
}
}
pub fn check_bytes(l: State, i: i32) -> Vec<u8> {
let mut length = 0;
let ptr = unsafe { dmsdk_ffi::luaL_checklstring(l.ptr, i, &mut length) };
let mut vec = Vec::with_capacity(length as usize);
for i in 0..length {
let byte = unsafe { *ptr.add(i as usize) as u8 };
vec.push(byte);
}
vec
}
pub fn check_int(l: State, i: i32) -> isize {
unsafe { dmsdk_ffi::luaL_checkinteger(l.ptr, i) }
}
pub fn check_float(l: State, i: i32) -> f64 {
unsafe { dmsdk_ffi::luaL_checknumber(l.ptr, i) }
}
pub fn to_bool(l: State, i: i32) -> bool {
unsafe { dmsdk_ffi::lua_toboolean(l.ptr, i) > 0 }
}
pub fn get_top(l: State) -> i32 {
unsafe { dmsdk_ffi::lua_gettop(l.ptr) }
}
pub fn new_table(l: State) {
unsafe {
dmsdk_ffi::lua_createtable(l.ptr, 0, 0);
}
}
pub fn raw_set_i(l: State, i: i32, n: i32) {
unsafe {
dmsdk_ffi::lua_rawseti(l.ptr, i, n);
}
}
pub fn pop(l: State, n: i32) {
unsafe {
dmsdk_ffi::lua_settop(l.ptr, -n - 1);
}
}
pub fn register(l: State, lib_name: &str, functions: &[(&str, Function)]) {
let lib_name = CString::new(lib_name).unwrap();
let cstringed_fns: Vec<(CString, Function)> = functions
.iter()
.map(|&(name, func)| (CString::new(name).unwrap(), func))
.collect();
let mut lua_fns: Vec<dmsdk_ffi::luaL_Reg> = Vec::with_capacity(cstringed_fns.len() + 1);
for &(ref name, func) in &cstringed_fns {
lua_fns.push(dmsdk_ffi::luaL_Reg {
name: name.as_ptr(),
func: Some(func),
});
}
lua_fns.push(dmsdk_ffi::luaL_Reg {
name: std::ptr::null(),
func: None,
});
unsafe {
dmsdk_ffi::luaL_register(l.ptr, lib_name.as_ptr(), lua_fns.as_ptr());
}
}
#[doc(hidden)]
pub fn __error(l: State, str: &str) -> ! {
let s = CString::new(str).unwrap();
unsafe {
dmsdk_ffi::luaL_error(l.ptr, s.as_ptr());
}
panic!("luaL_error failed to return!")
}
#[doc(hidden)]
pub fn __push_fstring(l: State, str: &str) {
let s = CString::new(str).unwrap();
unsafe {
dmsdk_ffi::lua_pushfstring(l.ptr, s.as_ptr());
}
}
#[macro_export]
macro_rules! __internal_lua_error {
($l:ident, $($arg:tt)*) => {
lua::__error($l, &format!($($arg)*));
};
}
#[macro_export]
macro_rules! __internal_push_fstring {
($l:ident, $($arg:tt)*) => {
lua::__push_fstring($l, &format!($($arg)*));
};
}
#[doc(inline)]
pub use crate::{
__internal_lua_error as error, __internal_push_fstring as push_fstring, declare_functions,
};