lunka 0.12.0

Pretty thin bindings to Lua 5.4
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
//! FFI definitions for `lauxlib.h`.

#![allow(non_snake_case)]

use super::*;

use core::{
	ffi::CStr,
	fmt,
	marker::PhantomData,
	mem::{
		MaybeUninit, size_of,
	},
	ptr::{
		null, null_mut,
	},
	slice::{
		from_raw_parts, from_raw_parts_mut,
	},
};

/// Global table name.
/// 
/// This cannot be changed for Lua that's already compiled.
pub const GLOBAL_TABLE: &CStr = c"_G";

/// Key, in the registry, for the table of loaded modules.
/// 
/// This cannot be changed for Lua that's already compiled.
pub const LOADED_TABLE: &CStr = c"_LOADED";

/// Key, in the registry, for the table of preloaded loaders.
/// 
/// This cannot be changed for Lua that's already compiled.
pub const PRELOAD_TABLE: &CStr = c"_PRELOAD";

/// Type for arrays of functions to be registered by [`luaL_setfuncs`].
/// Also known as `luaL_Reg`.
/// 
/// [`Reg::name`] is the function name and [`Reg::func`] is a pointer to the
/// function.
/// 
/// Any array of [`Reg`] must end with a sentinel entry in which both `name` and
/// `func` are null.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct Reg {
	pub name: *const c_char,
	pub func: Option<CFunction>
}

/// Packed combination [`Integer`] and [`Number`] type sizes for
/// [`luaL_checkversion_`] to check against.
pub const NUM_SIZES: usize = size_of::<Integer>() * 16 + size_of::<Number>();

/// Constant that is guaranteed to indicate no reference.
pub const NO_REF: c_int = -2;

/// Constant that indicates a reference to `nil`.
pub const REF_NIL: c_int = -1;

#[cfg_attr(all(feature = "link-system", feature = "link-dynamic", target_os = "windows"), link(name = "lua54", kind = "raw-dylib"))]
#[cfg_attr(all(feature = "link-system", feature = "link-dynamic", not(target_os = "windows")), link(name = "lua5.4", kind = "dylib"))]
#[cfg_attr(all(feature = "link-system", not(feature = "link-dynamic"), target_os = "windows"), link(name = "lua54", kind = "static"))]
#[cfg_attr(all(feature = "link-system", not(feature = "link-dynamic"), not(target_os = "windows")), link(name = "lua5.4", kind = "static"))]
unsafe extern "C-unwind" {
	pub fn luaL_newstate() -> *mut State;

	pub fn luaL_prepbuffsize(buffer: *mut Buffer, size: usize) -> *mut c_char;
	pub fn luaL_addlstring(buffer: *mut Buffer, str: *const c_char, len: usize);
	pub fn luaL_addstring(buffer: *mut Buffer, str: *const c_char);
	pub fn luaL_addvalue(buffer: *mut Buffer);
	pub fn luaL_pushresult(buffer: *mut Buffer);
	pub fn luaL_pushresultsize(buffer: *mut Buffer, size: usize);

	lua_state_func! {
		pub fn luaL_checkversion_(self, ver: Number, sz: usize);
		pub fn luaL_getmetafield(
			self, obj: c_int, e: *const c_char
		) -> c_int;
		pub fn luaL_callmeta(self, obj: c_int, e: *const c_char) -> c_int;
		pub fn luaL_tolstring(
			self, idx: c_int, len: *mut usize
		) -> *const c_char;
		pub fn luaL_argerror(self, arg: c_int, extra_msg: *const c_char) -> !;
		pub fn luaL_typeerror(self, arg: c_int, type_name: *const c_char) -> !;
		pub fn luaL_checklstring(
			self, arg: c_int, len: *mut usize
		) -> *const c_char;
		pub fn luaL_optlstring(
			self, arg: c_int, default: *const c_char, len: *mut usize
		) -> *const c_char;
		pub fn luaL_checknumber(self, arg: c_int) -> Number;
		pub fn luaL_optnumber(self, arg: c_int, default: Number) -> Number;

		pub fn luaL_checkinteger(self, arg: c_int) -> Integer;
		pub fn luaL_optinteger(
			self, arg: c_int, default: Integer
		) -> Integer;

		pub fn luaL_checkstack(self, sz: c_int, msg: *const c_char);
		pub fn luaL_checktype(self, arg: c_int, type_tag: c_int);
		pub fn luaL_checkany(self, arg: c_int);

		pub fn luaL_newmetatable(self, type_name: *const c_char) -> c_int;
		pub fn luaL_setmetatable(self, type_name: *const c_char);
		pub fn luaL_testudata(
			self, ud: c_int, type_name: *const c_char
		) -> *mut c_void;
		pub fn luaL_checkudata(
			self, ud: c_int, type_name: *const c_char
		) -> *mut c_void;
		
		pub fn luaL_where(self, level: c_int);
		/// # Note
		/// The return type should be [`c_int`] judging from the C header,
		/// however the documentation states that this function *never* returns.
		/// 
		/// See the manual for more information:
		/// <https://www.lua.org/manual/5.4/manual.html#luaL_error>
		pub fn luaL_error(self, fmt: *const c_char, ...) -> !;

		pub fn luaL_checkoption(
			self, arg: c_int, default: *const c_char,
			list: *const *const c_char
		) -> c_int;

		pub fn luaL_fileresult(
			self, status: c_int, file_name: *const c_char
		) -> c_int;
		pub fn luaL_execresult(self, status: c_int) -> c_int;

		pub fn luaL_ref(self, table: c_int) -> c_int;
		pub fn luaL_unref(self, table: c_int, ref_idx: c_int);

		pub fn luaL_loadfilex(
			self, file_name: *const c_char, mode: *const c_char
		) -> c_int;

		pub fn luaL_loadbufferx(
			self,
			buffer: *const c_char, buffer_sz: usize,
			name: *const c_char,
			mode: *const c_char
		) -> c_int;
		pub fn luaL_loadstring(self, code: *const c_char) -> c_int;

		pub fn luaL_len(self, idx: c_int) -> Integer;

		pub fn luaL_gsub(
			self,
			haystack: *const c_char,
			needle: *const c_char, replacement: *const c_char
		) -> *const c_char;

		pub fn luaL_setfuncs(self, list: *const Reg, n_upvalues: c_int);
		pub fn luaL_getsubtable(
			self, idx: c_int, table_name: *const c_char
		) -> c_int;

		pub fn luaL_traceback(
			self, of: *mut State,
			message: *const c_char, level: c_int
		);

		pub fn luaL_requiref(
			self, module_name: *const c_char,
			open_fn: CFunction, into_global: c_int
		);

		pub fn luaL_buffinit(self, buffer: *mut Buffer);
		pub fn luaL_buffinitsize(self, buffer: *mut Buffer, size: usize);
	}
}

/// Equivalent to the `luaL_checkversion` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state.
pub unsafe fn luaL_checkversion(l: *mut State) {
	unsafe { luaL_checkversion_(l, VERSION_NUM, NUM_SIZES) }
}

/// Equivalent to the `luaL_loadfile` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state,
/// and `file_name` must be a valid C string.
pub unsafe fn luaL_loadfile(l: *mut State, file_name: *const c_char) -> c_int {
	unsafe { luaL_loadfilex(l, file_name, null()) }
}

/// Functionally equivalent to the `luaL_newlibtable` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state,
/// and `lib` has to be terminated with a sentinel pair - see [`Reg`].
pub unsafe fn luaL_newlibtable(l: *mut State, lib: &[Reg]) {
	unsafe { lua_createtable(l, 0, (lib.len() - 1) as _) }
}

/// Functionally equivalent to the `luaL_newlib` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state,
/// and `lib` has to be terminated with a sentinel pair - see [`Reg`].
pub unsafe fn luaL_newlib(l: *mut State, lib: &[Reg]) {
	unsafe {
		luaL_checkversion(l);
		luaL_newlibtable(l, lib);
		luaL_setfuncs(l, lib.as_ptr(), 0)
	}
}

// `luaL_argcheck` and `luaL_argexpected` omitted here because they're kind of
// useless. Maybe later.

/// Equivalent to the `luaL_checkstring` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state.
pub unsafe fn luaL_checkstring(l: *mut State, arg: c_int) -> *const c_char {
	unsafe { luaL_checklstring(l, arg, null_mut()) }
}

/// Equivalent to the `luaL_optstring` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state.
pub unsafe fn luaL_optstring(l: *mut State, arg: c_int, default: *const c_char) -> *const c_char {
	unsafe { luaL_optlstring(l, arg, default, null_mut()) }
}

/// Equivalent to the `luaL_typename` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state.
pub unsafe fn luaL_typename(l: *mut State, idx: c_int) -> *const c_char {
	unsafe { lua_typename(l, lua_type(l, idx)) }
}

/// Equivalent to the `luaL_dofile` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state,
/// and `file_name` must be a valid C string.
pub unsafe fn luaL_dofile(l: *mut State, file_name: *const c_char) -> bool {
	unsafe { luaL_loadfile(l, file_name) != 0 || lua_pcall(l, 0, MULT_RET, 0) != 0 }
}

/// Equivalent to the `luaL_dofile` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state,
/// and `code` must be a valid C string.
pub unsafe fn luaL_dostring(l: *mut State, code: *const c_char) -> bool {
	unsafe { luaL_loadstring(l, code) != 0 || lua_pcall(l, 0, MULT_RET, 0) != 0 }
}

/// Equivalent to the `luaL_getmetatable` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state,
/// and `name` must be a valid C string.
pub unsafe fn luaL_getmetatable(l: *mut State, name: *const c_char) -> c_int {
	unsafe { lua_getfield(l, REGISTRY_INDEX, name) }
}

// `luaL_opt` omitted here because it can be written out easily.

/// Equivalent to the `luaL_loadbuffer` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state.
/// `buffer` must be the size of `buffer_sz`,
/// and `name` must be a valid C string.
pub unsafe fn luaL_loadbuffer(
	l: *mut State,
	buffer: *const c_char, buffer_sz: usize,
	name: *const c_char
) -> c_int {
	unsafe { luaL_loadbufferx(l, buffer, buffer_sz, name, null()) }
}

// `luaL_intop` omitted here because it can be written out easily.

/// Equivalent to the `luaL_pushfail` C macro.
/// 
/// # Safety
/// `l` must be a valid pointer to a Lua state.
pub unsafe fn luaL_pushfail(l: *mut State) {
	unsafe { lua_pushnil(l) }
}

/// Initial buffer size used by the buffer system, for [`Buffer`].
/// Also known as `LUAL_BUFFERSIZE`.
/// 
/// This cannot be changed for Lua that's already compiled.
// TODO: Does it make sense to ever change this?
pub const BUFFER_SIZE: usize =
	16 * size_of::<*mut c_void>() * size_of::<Number>();

/// String buffer that allows code to build Lua strings piecemeal.
/// 
/// This structure has a lifetime `'l` because it internally stores a pointer to
/// the Lua [`State`] that was used to initialize this buffer.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(C)]
pub struct Buffer<'l> {
	buffer_ptr: *mut c_char,
	capacity: usize,
	len: usize,
	l: *mut State,
	// `buffer` replaces `init` because that structure is not needed here.
	// Also, I am not entirely sure how this is used.
	pub buffer: [c_char; BUFFER_SIZE],
	_life: PhantomData<&'l *mut State>
}

impl Buffer<'_> {
	/// Construct an instance of [`Buffer`] that's zeroed, and put it inside of
	/// [`MaybeUninit`] for future usage.
	/// 
	/// This actually puts the buffer into an invalid state - it must be used
	/// with [`luaL_buffinit`] to properly initialize it.
	/// Only then can it be assumed to be initialized.
	pub const fn zeroed() -> MaybeUninit<Self> {
		MaybeUninit::new(Self {
			buffer_ptr: null_mut(),
			capacity: 0,
			len: 0,
			l: null_mut(),
			buffer: [0; BUFFER_SIZE],
			_life: PhantomData
		})
	}

	/// Construct a properly initialized instance of [`Buffer`] with the
	/// capacity [`BUFFER_SIZE`] in the raw Lua state pointed to by `l`.
	/// 
	/// # Safety
	/// `l` must point to a valid Lua [`State`].
	pub unsafe fn new_in_raw(l: *mut State) -> Self {
		let mut buffer = Self::zeroed();
		unsafe {
			luaL_buffinit(l, buffer.as_mut_ptr());
			buffer.assume_init()
		}
	}

	/// Allocate enough space in the buffer for a given number of C characters,
	/// and use a Rust function to fill the space with characters.
	/// 
	/// # Safety
	/// The underlying Lua state may raise a memory [error](crate::errors).
	pub unsafe fn prep_with(
		&mut self,
		size: usize, func: impl FnOnce(&mut [c_char])
	) {
		let prep_space = unsafe { luaL_prepbuffsize(self as *mut _, size) };
		func(unsafe { from_raw_parts_mut(prep_space, size) });
		self.len += size
	}

	/// Allocate enough space in the buffer [`BUFFER_SIZE`] C characters, and
	/// use a Rust function to fill the space with characters.
	/// 
	/// See also [`Buffer::prep_with`].
	/// 
	/// # Safety
	/// The underlying Lua state may raise a memory [error](crate::errors).
	pub unsafe fn prep_default_with(&mut self, func: impl FnOnce(&mut [c_char])) {
		unsafe { self.prep_with(BUFFER_SIZE, func) }
	}

	/// Get the current length of the buffer.
	/// 
	/// Equivalent to the C macro `luaL_bufflen`.
	pub const fn len(&self) -> usize {
		self.len
	}

	/// Return `true` if the buffer is empty.
	pub const fn is_empty(&self) -> bool {
		self.len == 0
	}

	/// Get the current capacity of the buffer.
	pub const fn capacity(&self) -> usize {
		self.capacity
	}

	/// Return the current contents of the buffer as a Rust mutable slice.
	/// 
	/// Functionally equivalent to the C macro `luaL_buffaddr`.
	pub fn contents(&mut self) -> &mut [c_char] {
		unsafe { from_raw_parts_mut(self.buffer_ptr, self.capacity) }
	}

	/// Add 1 C character to the buffer.
	/// 
	/// Equivalent to the C macro `luaL_addchar`.
	/// 
	/// # Safety
	/// The underlying Lua state may raise a memory [error](crate::errors).
	pub unsafe fn add_char(&mut self, ch: c_char) {
		if self.len >= self.capacity {
			unsafe { luaL_prepbuffsize(self as *mut _, 1) };
		}
		self.buffer[self.len] = ch;
		self.len += 1;
	}

	/// Remove a given number of C characters from the buffer.
	/// Equivalent to the C macro `luaL_buffsub`.
	pub fn remove(&mut self, delta: usize) {
		self.len -= delta
	}

	/// Add an array of C characters to the buffer.
	/// 
	/// Functionally equivalent to the function [`luaL_addlstring`].
	/// 
	/// # Safety
	/// The underlying Lua state may raise a memory [error](crate::errors).
	pub unsafe fn add_chars(&mut self, data: &[c_char]) {
		unsafe { luaL_addlstring(self as *mut _, data.as_ptr(), data.len()) }
	}

	/// Add a C string to the buffer.
	/// Equivalent to the function [`luaL_addstring`].
	/// 
	/// # Safety
	/// The underlying Lua state may raise a memory [error](crate::errors).
	pub unsafe fn add_string(&mut self, data: &CStr) {
		unsafe { luaL_addstring(self as *mut _, data.as_ptr()) }
	}

	/// Convert a value on top of the associated Lua stack to a string, and pop
	/// the result into the buffer.
	/// 
	/// Equivalent to the function [`luaL_addvalue`].
	pub fn add_value(&mut self) {
		unsafe { luaL_addvalue(self as *mut _) }
	}

	/// Equivalent to the function [`luaL_pushresult`].
	pub fn finish(mut self) {
		debug_assert!(
			self.len <= self.capacity,
			"buffer length is bigger than its capacity"
		);
		unsafe { luaL_pushresult(&mut self as *mut _) }
	}
}

const fn bytes_to_c_chars(b: &[u8]) -> &[c_char] {
	unsafe { from_raw_parts(
		b.as_ptr() as *const c_char,
		b.len() * size_of::<u8>() / size_of::<c_char>(),
	) }
}

impl fmt::Write for Buffer<'_> {
	fn write_str(&mut self, s: &str) -> fmt::Result {
		unsafe { self.add_chars(bytes_to_c_chars(s.as_bytes())) };
		Ok(())
	}

	fn write_char(&mut self, c: char) -> fmt::Result {
		let mut char_data = [0u8; 4];
		c.encode_utf8(&mut char_data);
		unsafe { self.add_chars(bytes_to_c_chars(&char_data as &[u8])) };
		Ok(())
	}
}