Skip to content

Commit 8377ff6

Browse files
authored
Merge pull request #11 from msva/proper-backward-compat
lmpack.c: rework of compat-defines (fixes #9)
2 parents cfee6e5 + 9d217f9 commit 8377ff6

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

lmpack.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
* compilation.
1616
*/
1717
#define LUA_LIB
18+
#include <limits.h>
1819
#include <stdlib.h>
1920
#include <string.h>
20-
#include <limits.h>
2121

22-
#include <lua.h>
2322
#include <lauxlib.h>
23+
#include <lua.h>
24+
#include <luaconf.h>
2425

2526
#ifdef MPACK_USE_AMALGAMATION
2627
# define MPACK_API static
@@ -34,15 +35,19 @@
3435
#define SESSION_META_NAME "mpack.Session"
3536
#define NIL_NAME "mpack.NIL"
3637

37-
#if LUA_VERSION_NUM > 501
3838
/*
3939
* TODO(tarruda): When targeting lua 5.3 and being compiled with `long long`
4040
* support(not -ansi), we should make use of lua 64 bit integers for
4141
* representing msgpack integers, since `double` can't represent the full range.
4242
*/
43-
typedef luaL_Reg luaL_reg;
44-
#define luaL_register(L, name, lreg) (luaL_setfuncs((L), (lreg), 0))
45-
#define lua_objlen(L, idx) (lua_rawlen(L, (idx)))
43+
44+
#ifndef luaL_reg
45+
/* Taken from Lua5.1's lauxlib.h */
46+
#define luaL_reg luaL_Reg
47+
#endif
48+
49+
#if LUA_VERSION_NUM > 501
50+
#define luaL_register(L,n,f) luaL_setfuncs(L,f,0)
4651
#endif
4752

4853
typedef struct {
@@ -198,7 +203,13 @@ static mpack_uint32_t lmpack_objlen(lua_State *L, int *is_array)
198203
assert(top = lua_gettop(L));
199204

200205
if ((type = lua_type(L, -1)) != LUA_TTABLE) {
206+
#if LUA_VERSION_NUM >= 502
207+
len = lua_rawlen(L, -1);
208+
#elif LUA_VERSION_NUM == 501
201209
len = lua_objlen(L, -1);
210+
#else
211+
#error You have either broken or too old Lua installation. This library requires Lua>=5.1
212+
#endif
202213
goto end;
203214
}
204215

0 commit comments

Comments
 (0)