Skip to content

Commit e61b471

Browse files
committed
chore(lua_ffi): add some doc comments
1 parent 3851530 commit e61b471

File tree

1 file changed

+236
-39
lines changed

1 file changed

+236
-39
lines changed

hlua/ffi/src/lib.rs

Lines changed: 236 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ pub struct luaL_Reg {
4848
pub type lua_Number = libc::c_double;
4949
pub type lua_Integer = libc::ptrdiff_t;
5050

51+
/// Type for C functions.
52+
///
53+
/// In order to communicate properly with Lua, a C function must use the
54+
/// following protocol, which defines the way parameters and results are passed:
55+
/// a C function receives its arguments from Lua in its stack in direct order
56+
/// (the first argument is pushed first). So, when the function starts,
57+
/// [`lua_gettop`]`(L)` returns the number of arguments received by the function.
58+
/// The first argument (if any) is at index 1 and its last argument is at index
59+
/// [`lua_gettop`](L). To return values to Lua, a C function just pushes them
60+
/// onto the stack, in direct order (the first result is pushed first), and
61+
/// returns the number of results. Any other value in the stack below the
62+
/// results will be properly discarded by Lua. Like a Lua function, a C function
63+
/// called by Lua can also return many results.
64+
///
65+
/// As an example, the following function receives a variable number of
66+
/// numerical arguments and returns their average and sum:
67+
///
68+
/// ```
69+
/// unsafe extern "C" fn foo(l: *mut lua_State) {
70+
/// let n = lua_gettop(l); /* number of arguments */
71+
/// let mut sum: lua_Number = 0;
72+
/// let i: i32;
73+
/// for i in 1..=n {
74+
/// if !lua_isnumber(l, i) {
75+
/// lua_pushstring(l, CString::new("incorrect argument").into_raw());
76+
/// lua_error(l);
77+
/// }
78+
/// sum += lua_tonumber(l, i);
79+
/// }
80+
/// lua_pushnumber(l, sum / n); /* first result */
81+
/// lua_pushnumber(l, sum); /* second result */
82+
/// return 2; /* number of results */
83+
/// }
84+
/// ```
5185
pub type lua_CFunction = unsafe extern "C" fn(l: *mut lua_State) -> c_int;
5286

5387
pub type lua_Alloc = extern "C" fn(
@@ -80,56 +114,188 @@ extern "C" {
80114

81115
pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
82116

117+
/// Returns the index of the top element in the stack. Because indices start
118+
/// at 1, this result is equal to the number of elements in the stack (and
119+
/// so 0 means an empty stack).
120+
/// *[-0, +0, -]*
83121
pub fn lua_gettop(l: *mut lua_State) -> c_int;
84-
pub fn lua_settop(l: *mut lua_State, idx: c_int);
122+
pub fn lua_settop(l: *mut lua_State, index: c_int);
85123
pub fn lua_pushboolean(l: *mut lua_State, n: c_int);
86124
pub fn lua_pushlstring(l: *mut lua_State, s: *const libc::c_char, l: libc::size_t);
125+
126+
/// Pushes the zero-terminated string pointed to by `s` onto the stack. Lua
127+
/// makes (or reuses) an internal copy of the given string, so the memory at
128+
/// s can be freed or reused immediately after the function returns. The
129+
/// string cannot contain embedded zeros; it is assumed to end at the first
130+
/// zero.
131+
/// *[-0, +1, m]*
87132
pub fn lua_pushstring(l: *mut lua_State, s: *const c_schar) -> *const c_schar;
88133
pub fn lua_pushinteger(l: *mut lua_State, n: isize);
89134
pub fn lua_pushnumber(l: *mut lua_State, n: c_double);
135+
136+
/// Pushes a new C closure onto the stack.
137+
/// *[-n, +1, m]*
138+
///
139+
/// When a C function is created, it is possible to associate some values
140+
/// with it, thus creating a C closure; these values are then accessible to
141+
/// the function whenever it is called. To associate values with a C
142+
/// function, first these values should be pushed onto the stack (when there
143+
/// are multiple values, the first value is pushed first). Then
144+
/// lua_pushcclosure is called to create and push the C function onto the
145+
/// stack, with the argument `n` telling how many values should be
146+
/// associated with the function. lua_pushcclosure also pops these values
147+
/// from the stack.
148+
///
149+
/// The maximum value for `n` is 255.
90150
pub fn lua_pushcclosure(l: *mut lua_State, fun: lua_CFunction, n: c_int);
91151
pub fn lua_pushnil(l: *mut lua_State);
92-
/// [-0, +1, -]
93-
///
152+
94153
/// Pushes a copy of the element at the given valid `index` onto the stack.
154+
/// *[-0, +1, -]*
95155
pub fn lua_pushvalue(l: *mut lua_State, index: c_int);
96-
pub fn lua_tointeger(l: *mut lua_State, idx: c_int) -> isize;
97-
pub fn lua_toboolean(l: *mut lua_State, idx: c_int) -> c_int;
98-
pub fn lua_tolstring(l: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_schar;
99-
pub fn lua_touserdata(l: *mut lua_State, idx: c_int) -> *mut libc::c_void;
100-
pub fn lua_setfield(l: *mut lua_State, idx: c_int, s: *const c_schar);
101-
pub fn lua_getfield(l: *mut lua_State, idx: c_int, s: *const c_schar);
156+
pub fn lua_tointeger(l: *mut lua_State, index: c_int) -> isize;
157+
pub fn lua_toboolean(l: *mut lua_State, index: c_int) -> c_int;
158+
159+
/// Converts the Lua value at the given acceptable `index` to a C string. If
160+
/// `len` is not NULL, it also sets `*len` with the string length. The Lua
161+
/// value must be a string or a number; otherwise, the function returns
162+
/// NULL. If the value is a number, then `lua_tolstring` also changes the
163+
/// actual value in the stack to a string. (This change confuses
164+
/// [`lua_next`] when `lua_tolstring` is applied to keys during a table
165+
/// traversal.)
166+
/// *[-0, +0, m]*
167+
///
168+
/// `lua_tolstring` returns a fully aligned pointer to a string inside the
169+
/// Lua state. This string always has a zero ('\0') after its last character
170+
/// (as in C), but can contain other zeros in its body. Because Lua has
171+
/// garbage collection, there is no guarantee that the pointer returned by
172+
/// `lua_tolstring` will be valid after the corresponding value is removed
173+
/// from the stack.
174+
pub fn lua_tolstring(l: *mut lua_State, index: c_int, len: *mut usize) -> *const c_schar;
175+
176+
/// If the value at the given acceptable `index` is a full userdata, returns
177+
/// its block address. If the value is a light userdata, returns its
178+
/// pointer. Otherwise, returns `NULL`.
179+
/// *[-0, +0, -]*
180+
pub fn lua_touserdata(l: *mut lua_State, index: c_int) -> *mut libc::c_void;
181+
182+
/// Does the equivalent to `t[k] = v`, where `t` is the value at the given
183+
/// valid index and `v` is the value at the top of the stack.
184+
/// *[-1, +0, e]*
185+
///
186+
/// This function pops the value from the stack. As in Lua, this function
187+
/// may trigger a metamethod for the "newindex" event
188+
pub fn lua_setfield(l: *mut lua_State, index: c_int, k: *const c_schar);
189+
190+
/// Pushes onto the stack the value `t[k]`, where `t` is the value at the
191+
/// given valid `index`. As in Lua, this function may trigger a metamethod
192+
/// for the "index" event
193+
/// *[-0, +1, e]*
194+
pub fn lua_getfield(l: *mut lua_State, index: c_int, k: *const c_schar);
195+
102196
pub fn lua_createtable(l: *mut lua_State, narr: c_int, nrec: c_int);
103-
pub fn lua_newuserdata(l: *mut lua_State, sz: libc::size_t) -> *mut libc::c_void;
104-
/// [-1, +1, e]
197+
198+
/// This function allocates a new block of memory with the given size,
199+
/// pushes onto the stack a new full userdata with the block address, and
200+
/// returns this address.
201+
/// *[-0, +1, m]*
202+
///
203+
/// Userdata represent C values in Lua. A full userdata represents a block
204+
/// of memory. It is an object (like a table): you must create it, it can
205+
/// have its own metatable, and you can detect when it is being collected. A
206+
/// full userdata is only equal to itself (under raw equality).
105207
///
208+
/// When Lua collects a full userdata with a gc metamethod, Lua calls the
209+
/// metamethod and marks the userdata as finalized. When this userdata is
210+
/// collected again then Lua frees its corresponding memory.
211+
pub fn lua_newuserdata(l: *mut lua_State, sz: libc::size_t) -> *mut libc::c_void;
212+
106213
/// Pushes onto the stack the value `t[k]`, where `t` is the value at the
107214
/// given valid `index` and `k` is the value at the top of the stack.
215+
/// *[-1, +1, e]*
108216
///
109217
/// This function pops the key from the stack (putting the resulting value
110218
/// in its place). As in Lua, this function may trigger a metamethod for the
111219
/// "index" event
112220
pub fn lua_gettable(l: *mut lua_State, index: c_int);
113-
pub fn lua_settable(l: *mut lua_State, idx: c_int);
221+
222+
/// Does the equivalent to `t[k] = v`, where `t` is the value at the given
223+
/// valid `index`, `v` is the value at the top of the stack, and `k` is the
224+
/// value just below the top.
225+
/// *[-2, +0, e]*
226+
///
227+
/// This function pops both the key and the value from the stack. As in Lua,
228+
/// this function may trigger a metamethod for the "newindex" event.
229+
pub fn lua_settable(l: *mut lua_State, index: c_int);
230+
231+
/// Returns the type of the value in the given acceptable `index`, or
232+
/// [`LUA_TNONE`] for a non-valid index (that is, an index to an "empty"
233+
/// stack position). The types returned by lua_type are coded by the
234+
/// following constants: [`LUA_TNIL`], [`LUA_TNUMBER`], [`LUA_TBOOLEAN`],
235+
/// [`LUA_TSTRING`], [`LUA_TTABLE`], [`LUA_TFUNCTION`], [`LUA_TUSERDATA`],
236+
/// [`LUA_TTHREAD`], and [`LUA_TLIGHTUSERDATA`].
237+
/// *[-0, +0, -]*
114238
pub fn lua_type(state: *mut lua_State, index: c_int) -> c_int;
239+
240+
/// Returns the name of the type encoded by the value `tp`, which must be
241+
/// one the values returned by [`lua_type`].
242+
/// *[-0, +0, -]*
115243
pub fn lua_typename(state: *mut lua_State, tp: c_int) -> *mut c_schar;
116-
pub fn lua_setmetatable(l: *mut lua_State, objindex: c_int) -> c_int;
117-
pub fn lua_getmetatable(l: *mut lua_State, objindex: c_int) -> c_int;
118244

119-
pub fn lua_tonumberx(l: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
120-
pub fn lua_tointegerx(l: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
245+
/// Pops a table from the stack and sets it as the new metatable for the
246+
/// value at the given acceptable `index`.
247+
/// *[-1, +0, -]*
248+
pub fn lua_setmetatable(l: *mut lua_State, index: c_int) -> c_int;
249+
pub fn lua_getmetatable(l: *mut lua_State, index: c_int) -> c_int;
250+
251+
pub fn lua_tonumberx(l: *mut lua_State, index: c_int, isnum: *mut c_int) -> lua_Number;
252+
pub fn lua_tointegerx(l: *mut lua_State, index: c_int, isnum: *mut c_int) -> lua_Integer;
121253

122-
pub fn lua_pcall(l: *mut lua_State, nargs: c_int, nresults: c_int, msgh: c_int) -> c_int;
254+
/// Calls a function in protected mode.
255+
/// *[-(nargs + 1), +(nresults|1), -]*
256+
///
257+
/// Both `nargs` and `nresults` have the same meaning as in `lua_call`. If
258+
/// there are no errors during the call, `lua_pcall` behaves exactly like
259+
/// `lua_call`. However, if there is any error, `lua_pcall` catches it,
260+
/// pushes a single value on the stack (the error message), and returns an
261+
/// error code. Like lua_call, `lua_pcall` always removes the function and
262+
/// its arguments from the stack.
263+
///
264+
/// If `errfunc` is 0, then the error message returned on the stack is
265+
/// exactly the original error message. Otherwise, `errfunc` is the stack
266+
/// index of an error handler function. (In the current implementation, this
267+
/// index cannot be a pseudo-index.) In case of runtime errors, this
268+
/// function will be called with the error message and its return value will
269+
/// be the message returned on the stack by `lua_pcall`.
270+
///
271+
/// Typically, the error handler function is used to add more debug
272+
/// information to the error message, such as a stack traceback. Such
273+
/// information cannot be gathered after the return of `lua_pcall`, since by
274+
/// then the stack has unwound.
275+
///
276+
/// The `lua_pcall` function returns 0 in case of success or one of the
277+
/// following error codes:
278+
/// - [`LUA_ERRRUN`]: a runtime error.
279+
///
280+
/// - [`LUA_ERRMEM`]: memory allocation error. For such errors, Lua does not
281+
/// call the error handler function.
282+
///
283+
/// - [`LUA_ERRERR`]: error while running the error handler function.
284+
pub fn lua_pcall(l: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
123285
pub fn lua_load(l: *mut lua_State, reader: lua_Reader, dt: *mut libc::c_void, chunkname: *const libc::c_char, mode: *const libc::c_char) -> c_int;
124286
pub fn lua_dump(l: *mut lua_State, writer: lua_Writer, data: *mut libc::c_void) -> c_int;
125287

288+
/// Generates a Lua error. The error message (which can actually be a Lua
289+
/// value of any type) must be on the stack top. This function does a long
290+
/// jump, and therefore never returns. (see [`luaL_error`]).
291+
/// *[-1, +0, v]*
126292
pub fn lua_error(l: *mut lua_State) -> c_int;
127-
pub fn lua_next(l: *mut lua_State, idx: c_int) -> c_int;
293+
pub fn lua_next(l: *mut lua_State, index: c_int) -> c_int;
128294
pub fn lua_concat(l: *mut lua_State, n: c_int);
129-
pub fn lua_len(l: *mut lua_State, idx: c_int);
295+
pub fn lua_len(l: *mut lua_State, index: c_int);
130296

131-
pub fn lua_insert(l: *mut lua_State, idx: c_int);
132-
pub fn lua_remove(l: *mut lua_State, idx: c_int);
297+
pub fn lua_insert(l: *mut lua_State, index: c_int);
298+
pub fn lua_remove(l: *mut lua_State, index: c_int);
133299

134300
pub fn luaopen_base(l: *mut lua_State);
135301
pub fn luaopen_bit(l: *mut lua_State);
@@ -144,18 +310,31 @@ extern "C" {
144310
// lauxlib functions.
145311
pub fn luaL_newstate() -> *mut lua_State;
146312
pub fn luaL_register(l: *mut lua_State, libname: *const c_schar, lr: *const luaL_Reg);
313+
314+
/// Raises an error. The error message format is given by `fmt` plus any
315+
/// extra arguments, following the same rules of `lua_pushfstring`. It also
316+
/// adds at the beginning of the message the file name and the line number
317+
/// where the error occurred, if this information is available.
318+
/// *[-0, +0, v]*
319+
///
320+
/// This function never returns, but it is an idiom to use it in C functions
321+
/// as return `luaL_error(args)`.
147322
pub fn luaL_error(l: *mut lua_State, fmt: *const c_schar, ...) -> c_int;
148323
pub fn luaL_openlibs(L: *mut lua_State);
149324
}
150325

151326
#[inline(always)]
152-
pub unsafe fn lua_getglobal(state: *mut lua_State, s: *const c_schar) {
153-
lua_getfield(state, LUA_GLOBALSINDEX, s);
327+
/// Pushes onto the stack the value of the global `name`.
328+
/// *[-0, +1, e]*
329+
pub unsafe fn lua_getglobal(state: *mut lua_State, name: *const c_schar) {
330+
lua_getfield(state, LUA_GLOBALSINDEX, name);
154331
}
155332

156333
#[inline(always)]
157-
pub unsafe fn lua_setglobal(state: *mut lua_State, s: *const c_schar) {
158-
lua_setfield(state, LUA_GLOBALSINDEX, s);
334+
/// Pops a value from the stack and sets it as the new value of global `name`.
335+
/// *[-1, +0, e]*
336+
pub unsafe fn lua_setglobal(state: *mut lua_State, name: *const c_schar) {
337+
lua_setfield(state, LUA_GLOBALSINDEX, name);
159338
}
160339

161340
#[inline(always)]
@@ -164,6 +343,13 @@ pub unsafe fn lua_pop(state: *mut lua_State, n: c_int) {
164343
}
165344

166345
#[inline(always)]
346+
/// Pushes a C function onto the stack. This function receives a pointer to a C
347+
/// function and pushes onto the stack a Lua value of type function that, when
348+
/// called, invokes the corresponding C function.
349+
/// `[-0, +1, m]`
350+
///
351+
/// Any function to be registered in Lua must follow the correct protocol to
352+
/// receive its parameters and return its results (see [`lua_CFunction`]).
167353
pub unsafe fn lua_pushcfunction(state: *mut lua_State, f: lua_CFunction) {
168354
lua_pushcclosure(state, f, 0);
169355
}
@@ -179,23 +365,34 @@ pub unsafe fn lua_newtable(state: *mut lua_State) {
179365
}
180366

181367
#[inline(always)]
368+
/// When a C function is created, it is possible to associate some values with
369+
/// it, thus creating a C closure; these values are called upvalues and are
370+
/// accessible to the function whenever it is called (see [`lua_pushcclosure`]).
371+
///
372+
/// Whenever a C function is called, its **upvalues** are located at specific
373+
/// pseudo-indices. These pseudo-indices are produced by the function
374+
/// `lua_upvalueindex`. The first value associated with a function is at
375+
/// position `lua_upvalueindex(1)`, and so on. Any access to
376+
/// `lua_upvalueindex(n)`, where n is greater than the number of upvalues of the
377+
/// current function (but not greater than 256), produces an acceptable (but
378+
/// invalid) index.
182379
pub fn lua_upvalueindex(i: c_int) -> c_int {
183380
LUA_GLOBALSINDEX - i
184381
}
185382

186383
#[inline(always)]
187-
pub unsafe fn lua_isfunction(state: *mut lua_State, idx: c_int) -> bool {
188-
lua_type(state, idx) == LUA_TFUNCTION
384+
pub unsafe fn lua_isfunction(state: *mut lua_State, index: c_int) -> bool {
385+
lua_type(state, index) == LUA_TFUNCTION
189386
}
190387

191388
#[inline(always)]
192-
pub unsafe fn lua_istable(state: *mut lua_State, idx: c_int) -> bool {
193-
lua_type(state, idx) == LUA_TTABLE
389+
pub unsafe fn lua_istable(state: *mut lua_State, index: c_int) -> bool {
390+
lua_type(state, index) == LUA_TTABLE
194391
}
195392

196393
#[inline(always)]
197-
pub unsafe fn lua_islightuserdata(state: *mut lua_State, idx: c_int) -> bool {
198-
lua_type(state, idx) == LUA_TLIGHTUSERDATA
394+
pub unsafe fn lua_islightuserdata(state: *mut lua_State, index: c_int) -> bool {
395+
lua_type(state, index) == LUA_TLIGHTUSERDATA
199396
}
200397

201398
#[inline(always)]
@@ -204,23 +401,23 @@ pub unsafe fn lua_isnil(state: *mut lua_State, index: c_int) -> bool {
204401
}
205402

206403
#[inline(always)]
207-
pub unsafe fn lua_isboolean(state: *mut lua_State, idx: c_int) -> bool {
208-
lua_type(state, idx) == LUA_TBOOLEAN
404+
pub unsafe fn lua_isboolean(state: *mut lua_State, index: c_int) -> bool {
405+
lua_type(state, index) == LUA_TBOOLEAN
209406
}
210407

211408
#[inline(always)]
212-
pub unsafe fn lua_isthread(state: *mut lua_State, idx: c_int) -> bool {
213-
lua_type(state, idx) == LUA_TTHREAD
409+
pub unsafe fn lua_isthread(state: *mut lua_State, index: c_int) -> bool {
410+
lua_type(state, index) == LUA_TTHREAD
214411
}
215412

216413
#[inline(always)]
217-
pub unsafe fn lua_isnone(state: *mut lua_State, idx: c_int) -> bool {
218-
lua_type(state, idx) == LUA_TNONE
414+
pub unsafe fn lua_isnone(state: *mut lua_State, index: c_int) -> bool {
415+
lua_type(state, index) == LUA_TNONE
219416
}
220417

221418
#[inline(always)]
222-
pub unsafe fn lua_isnoneornil(state: *mut lua_State, idx: c_int) -> bool {
223-
lua_type(state, idx) <= 0
419+
pub unsafe fn lua_isnoneornil(state: *mut lua_State, index: c_int) -> bool {
420+
lua_type(state, index) <= 0
224421
}
225422

226423
#[inline(always)]

0 commit comments

Comments
 (0)