1use std::os::raw::{c_char, c_int, c_void};
6use std::ptr;
7
8use super::lauxlib::*;
9use super::lua::*;
10
11#[inline(always)]
12unsafe fn compat53_reverse(L: *mut lua_State, mut a: c_int, mut b: c_int) {
13 while a < b {
14 lua_pushvalue(L, a);
15 lua_pushvalue(L, b);
16 lua_replace(L, a);
17 lua_replace(L, b);
18 a += 1;
19 b -= 1;
20 }
21}
22
23pub unsafe fn lua_rotate(L: *mut lua_State, mut idx: c_int, mut n: c_int) {
28 idx = lua_absindex(L, idx);
29 if n > 0 {
30 for _ in 0..n {
32 lua_insert(L, idx);
33 }
34 return;
35 }
36 let n_elems = lua_gettop(L) - idx + 1;
37 if n < 0 {
38 n += n_elems;
39 }
40 if n > 0 && n < n_elems {
41 luaL_checkstack(L, 2, cstr!("not enough stack slots available"));
42 n = n_elems - n;
43 compat53_reverse(L, idx, idx + n - 1);
44 compat53_reverse(L, idx + n, idx + n_elems - 1);
45 compat53_reverse(L, idx, idx + n_elems - 1);
46 }
47}
48
49#[inline(always)]
50pub unsafe fn lua_isinteger(L: *mut lua_State, idx: c_int) -> c_int {
51 if lua_type(L, idx) == LUA_TNUMBER {
52 let n = lua_tonumber(L, idx);
53 let i = lua_tointeger(L, idx);
54 if n.to_bits() == (i as lua_Number).to_bits() {
56 return 1;
57 }
58 }
59 0
60}
61
62#[inline(always)]
63pub unsafe fn lua_tointeger(L: *mut lua_State, i: c_int) -> lua_Integer {
64 lua_tointegerx(L, i, ptr::null_mut())
65}
66
67#[inline(always)]
70pub unsafe fn lua_tointegerx(L: *mut lua_State, i: c_int, isnum: *mut c_int) -> lua_Integer {
71 let mut ok = 0;
72 let n = lua_tonumberx(L, i, &mut ok);
73 let n_int = n as lua_Integer;
74 if ok != 0 && (n - n_int as lua_Number).abs() < lua_Number::EPSILON {
75 if !isnum.is_null() {
76 *isnum = 1;
77 }
78 return n_int;
79 }
80 if !isnum.is_null() {
81 *isnum = 0;
82 }
83 0
84}
85
86#[inline(always)]
87pub unsafe fn lua_pushlstring(L: *mut lua_State, s: *const c_char, l: usize) -> *const c_char {
88 if l == 0 {
89 lua_pushlstring_(L, cstr!(""), 0)
90 } else {
91 lua_pushlstring_(L, s, l)
92 }
93}
94
95#[inline(always)]
96pub unsafe fn lua_getglobal(L: *mut lua_State, var: *const c_char) -> c_int {
97 lua_getglobal_(L, var);
98 lua_type(L, -1)
99}
100
101#[inline(always)]
102pub unsafe fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int {
103 lua_gettable_(L, idx);
104 lua_type(L, -1)
105}
106
107#[inline(always)]
108pub unsafe fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int {
109 lua_getfield_(L, idx, k);
110 lua_type(L, -1)
111}
112
113#[inline(always)]
114pub unsafe fn lua_geti(L: *mut lua_State, mut idx: c_int, n: lua_Integer) -> c_int {
115 idx = lua_absindex(L, idx);
116 lua_pushinteger(L, n);
117 lua_gettable(L, idx)
118}
119
120#[inline(always)]
121pub unsafe fn lua_rawget(L: *mut lua_State, idx: c_int) -> c_int {
122 lua_rawget_(L, idx);
123 lua_type(L, -1)
124}
125
126#[inline(always)]
127pub unsafe fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int {
128 let n = n.try_into().expect("cannot convert index to lua_Integer");
129 lua_rawgeti_(L, idx, n);
130 lua_type(L, -1)
131}
132
133#[inline(always)]
134pub unsafe fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_int {
135 lua_rawgetp_(L, idx, p);
136 lua_type(L, -1)
137}
138
139#[inline(always)]
140pub unsafe fn lua_getuservalue(L: *mut lua_State, idx: c_int) -> c_int {
141 lua_getuservalue_(L, idx);
142 lua_type(L, -1)
143}
144
145#[inline(always)]
146pub unsafe fn lua_seti(L: *mut lua_State, mut idx: c_int, n: lua_Integer) {
147 luaL_checkstack(L, 1, cstr!("not enough stack slots available"));
148 idx = lua_absindex(L, idx);
149 lua_pushinteger(L, n);
150 lua_insert(L, -2);
151 lua_settable(L, idx);
152}
153
154#[inline(always)]
155pub unsafe fn lua_rawseti(L: *mut lua_State, idx: c_int, n: lua_Integer) {
156 let n = n.try_into().expect("cannot convert index from lua_Integer");
157 lua_rawseti_(L, idx, n)
158}
159
160#[inline(always)]
161pub unsafe fn lua_dump(L: *mut lua_State, writer: lua_Writer, data: *mut c_void, _strip: c_int) -> c_int {
162 lua_dump_(L, writer, data)
163}
164
165#[inline(always)]
166pub unsafe fn lua_resume(L: *mut lua_State, from: *mut lua_State, narg: c_int, nres: *mut c_int) -> c_int {
167 let ret = lua_resume_(L, from, narg);
168 if (ret == LUA_OK || ret == LUA_YIELD) && !(nres.is_null()) {
169 *nres = lua_gettop(L);
170 }
171 ret
172}
173
174#[inline(always)]
179pub unsafe fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int {
180 if luaL_getmetafield_(L, obj, e) != 0 {
181 lua_type(L, -1)
182 } else {
183 LUA_TNIL
184 }
185}
186
187#[inline(always)]
188pub unsafe fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_int {
189 if luaL_newmetatable_(L, tname) != 0 {
190 lua_pushstring(L, tname);
191 lua_setfield(L, -2, cstr!("__name"));
192 1
193 } else {
194 0
195 }
196}
197
198pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize) -> *const c_char {
199 idx = lua_absindex(L, idx);
200 if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
201 match lua_type(L, idx) {
202 LUA_TNIL => {
203 lua_pushliteral(L, c"nil");
204 }
205 LUA_TSTRING | LUA_TNUMBER => {
206 lua_pushvalue(L, idx);
207 }
208 LUA_TBOOLEAN => {
209 if lua_toboolean(L, idx) == 0 {
210 lua_pushliteral(L, c"false");
211 } else {
212 lua_pushliteral(L, c"true");
213 }
214 }
215 t => {
216 let tt = luaL_getmetafield(L, idx, cstr!("__name"));
217 let name = if tt == LUA_TSTRING {
218 lua_tostring(L, -1)
219 } else {
220 lua_typename(L, t)
221 };
222 lua_pushfstring(L, cstr!("%s: %p"), name, lua_topointer(L, idx));
223 if tt != LUA_TNIL {
224 lua_replace(L, -2); }
226 }
227 };
228 } else if lua_isstring(L, -1) == 0 {
229 luaL_error(L, cstr!("'__tostring' must return a string"));
230 }
231 lua_tolstring(L, -1, len)
232}
233
234pub unsafe fn luaL_requiref(L: *mut lua_State, modname: *const c_char, openf: lua_CFunction, glb: c_int) {
235 luaL_checkstack(L, 3, cstr!("not enough stack slots available"));
236 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
237 if lua_getfield(L, -1, modname) == LUA_TNIL {
238 lua_pop(L, 1);
239 lua_pushcfunction(L, openf);
240 lua_pushstring(L, modname);
241 lua_call(L, 1, 1);
242 lua_pushvalue(L, -1);
243 lua_setfield(L, -3, modname);
244 }
245 if glb != 0 {
246 lua_pushvalue(L, -1);
247 lua_setglobal(L, modname);
248 }
249 lua_replace(L, -2);
250}
251
252pub unsafe fn luaL_loadbufferenv(
253 L: *mut lua_State,
254 data: *const c_char,
255 size: usize,
256 name: *const c_char,
257 mode: *const c_char,
258 mut env: c_int,
259) -> c_int {
260 if env != 0 {
261 env = lua_absindex(L, env);
262 }
263 let status = luaL_loadbufferx(L, data, size, name, mode);
264 if status == LUA_OK && env != 0 {
265 lua_pushvalue(L, env);
266 lua_setupvalue(L, -2, 1);
267 }
268 status
269}