@@ -25,6 +25,75 @@ constexpr void unused(Args&&...)
2525{
2626}
2727
28+ // These are for Lua versions prior to 5.2.0.
29+ #if LUA_VERSION_NUM < 502
30+ using lua_Unsigned = std::make_unsigned_t <lua_Integer>;
31+
32+ #if ! LUABRIDGE_ON_LUAU
33+ inline int lua_absindex (lua_State* L, int idx)
34+ {
35+ if (idx > LUA_REGISTRYINDEX && idx < 0 )
36+ return lua_gettop (L) + idx + 1 ;
37+ else
38+ return idx;
39+ }
40+ #endif
41+
42+ #define LUA_OPEQ 1
43+ #define LUA_OPLT 2
44+ #define LUA_OPLE 3
45+
46+ inline int lua_compare (lua_State* L, int idx1, int idx2, int op)
47+ {
48+ switch (op)
49+ {
50+ case LUA_OPEQ:
51+ return lua_equal (L, idx1, idx2);
52+
53+ case LUA_OPLT:
54+ return lua_lessthan (L, idx1, idx2);
55+
56+ case LUA_OPLE:
57+ return lua_equal (L, idx1, idx2) || lua_lessthan (L, idx1, idx2);
58+
59+ default :
60+ return 0 ;
61+ }
62+ }
63+
64+ #if ! LUABRIDGE_ON_LUAJIT
65+ inline void * luaL_testudata (lua_State* L, int ud, const char * tname)
66+ {
67+ void * p = lua_touserdata (L, ud);
68+ if (p == nullptr )
69+ return nullptr ;
70+
71+ if (! lua_getmetatable (L, ud))
72+ return nullptr ;
73+
74+ luaL_getmetatable (L, tname);
75+ if (! lua_rawequal (L, -1 , -2 ))
76+ p = nullptr ;
77+
78+ lua_pop (L, 2 );
79+ return p;
80+ }
81+ #endif
82+
83+ inline int get_length (lua_State* L, int idx)
84+ {
85+ return static_cast <int >(lua_objlen (L, idx));
86+ }
87+ #else // LUA_VERSION_NUM >= 502
88+ inline int get_length (lua_State* L, int idx)
89+ {
90+ lua_len (L, idx);
91+ const int len = static_cast <int >(luaL_checknumber (L, -1 ));
92+ lua_pop (L, 1 );
93+ return len;
94+ }
95+ #endif // LUA_VERSION_NUM < 502
96+
2897// These functions and defines are for Luau.
2998#if LUABRIDGE_ON_LUAU
3099inline int luaL_ref (lua_State* L, int idx)
@@ -216,80 +285,8 @@ inline lua_Integer to_integerx(lua_State* L, int idx, int* isnum)
216285
217286 return 0 ;
218287}
219-
220288#endif // LUA_VERSION_NUM < 503
221289
222- // These are for Lua versions prior to 5.2.0.
223- #if LUA_VERSION_NUM < 502
224- using lua_Unsigned = std::make_unsigned_t <lua_Integer>;
225-
226- #if ! LUABRIDGE_ON_LUAU
227- inline int lua_absindex (lua_State* L, int idx)
228- {
229- if (idx > LUA_REGISTRYINDEX && idx < 0 )
230- return lua_gettop (L) + idx + 1 ;
231- else
232- return idx;
233- }
234- #endif
235-
236- #define LUA_OPEQ 1
237- #define LUA_OPLT 2
238- #define LUA_OPLE 3
239-
240- inline int lua_compare (lua_State* L, int idx1, int idx2, int op)
241- {
242- switch (op)
243- {
244- case LUA_OPEQ:
245- return lua_equal (L, idx1, idx2);
246-
247- case LUA_OPLT:
248- return lua_lessthan (L, idx1, idx2);
249-
250- case LUA_OPLE:
251- return lua_equal (L, idx1, idx2) || lua_lessthan (L, idx1, idx2);
252-
253- default :
254- return 0 ;
255- }
256- }
257-
258- #if ! LUABRIDGE_ON_LUAJIT
259- inline void * luaL_testudata (lua_State* L, int ud, const char * tname)
260- {
261- void * p = lua_touserdata (L, ud);
262- if (p == nullptr )
263- return nullptr ;
264-
265- if (! lua_getmetatable (L, ud))
266- return nullptr ;
267-
268- luaL_getmetatable (L, tname);
269- if (! lua_rawequal (L, -1 , -2 ))
270- p = nullptr ;
271-
272- lua_pop (L, 2 );
273- return p;
274- }
275- #endif
276-
277- inline int get_length (lua_State* L, int idx)
278- {
279- return static_cast <int >(lua_objlen (L, idx));
280- }
281-
282- #else // LUA_VERSION_NUM >= 502
283- inline int get_length (lua_State* L, int idx)
284- {
285- lua_len (L, idx);
286- const int len = static_cast <int >(luaL_checknumber (L, -1 ));
287- lua_pop (L, 1 );
288- return len;
289- }
290-
291- #endif // LUA_VERSION_NUM < 502
292-
293290#ifndef LUA_OK
294291#define LUABRIDGE_LUA_OK 0
295292#else
0 commit comments