# HG changeset patch
# Parent 8c0f1e6bb1268d2e461e2eb3df3ed06a34894619
Support Lua 5.2

diff -r 8c0f1e6bb126 src/if_lua.c
--- a/src/if_lua.c	Sun Jan 08 19:19:42 2012 +0900
+++ b/src/if_lua.c	Sun Jan 08 20:40:04 2012 +0900
@@ -12,6 +12,25 @@
 #include "vim.h"
 
 #include <lua.h>
+
+/*
+ * LUA_5_2_AND_OVER: defined as 1 for Lua 5.2 or over.
+ *
+ * LUA_VERSION_NUM macro is defined in lua.h.  Its values are...
+ *   - 501 for 5.1.x
+ *   - 502 for 5.2.x
+ */
+#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
+# define LUA_5_2_AND_OVER	1
+#else
+# define LUA_5_2_AND_OVER	0
+#endif
+
+#if LUA_5_2_AND_OVER
+/* To enable luaL_register/luaL_openlib() in 5.2 before include lauxlib.h */
+# define LUA_COMPAT_MODULE	1
+#endif
+
 #include <lualib.h>
 #include <lauxlib.h>
 
@@ -54,20 +73,28 @@
 #endif
 
 /* lauxlib */
-#define luaL_register dll_luaL_register
-#define luaL_typerror dll_luaL_typerror
 #define luaL_checklstring dll_luaL_checklstring
 #define luaL_checkinteger dll_luaL_checkinteger
 #define luaL_optinteger dll_luaL_optinteger
 #define luaL_checktype dll_luaL_checktype
 #define luaL_error dll_luaL_error
-#define luaL_loadfile dll_luaL_loadfile
-#define luaL_loadbuffer dll_luaL_loadbuffer
 #define luaL_newstate dll_luaL_newstate
 #define luaL_buffinit dll_luaL_buffinit
-#define luaL_prepbuffer dll_luaL_prepbuffer
 #define luaL_addlstring dll_luaL_addlstring
 #define luaL_pushresult dll_luaL_pushresult
+#if LUA_5_2_AND_OVER
+# define luaL_openlib dll_luaL_openlib
+# define luaL_loadfilex dll_luaL_loadfilex
+# define luaL_loadbufferx dll_luaL_loadbufferx
+# define luaL_prepbuffsize dll_luaL_prepbuffsize
+#else /* LUA_5_2_AND_OVER */
+# define luaL_register dll_luaL_register
+# define luaL_typerror dll_luaL_typerror
+# define luaL_loadfile dll_luaL_loadfile
+# define luaL_loadbuffer dll_luaL_loadbuffer
+# define luaL_prepbuffer dll_luaL_prepbuffer
+#endif /* LUA_5_2_AND_OVER */
+
 /* lua */
 #define lua_close dll_lua_close
 #define lua_gettop dll_lua_gettop
@@ -79,8 +106,6 @@
 #define lua_isstring dll_lua_isstring
 #define lua_type dll_lua_type
 #define lua_rawequal dll_lua_rawequal
-#define lua_tonumber dll_lua_tonumber
-#define lua_tointeger dll_lua_tointeger
 #define lua_toboolean dll_lua_toboolean
 #define lua_tolstring dll_lua_tolstring
 #define lua_touserdata dll_lua_touserdata
@@ -103,8 +128,20 @@
 #define lua_rawseti dll_lua_rawseti
 #define lua_remove dll_lua_remove
 #define lua_setmetatable dll_lua_setmetatable
-#define lua_call dll_lua_call
-#define lua_pcall dll_lua_pcall
+#if LUA_5_2_AND_OVER
+# define lua_tonumberx dll_lua_tonumberx
+# define lua_tointegerx dll_lua_tointegerx
+# define lua_callk dll_lua_callk
+# define lua_pcallk dll_lua_pcallk
+# define lua_getglobal dll_lua_getglobal
+# define lua_setglobal dll_lua_setglobal
+#else
+# define lua_tonumber dll_lua_tonumber
+# define lua_tointeger dll_lua_tointeger
+# define lua_call dll_lua_call
+# define lua_pcall dll_lua_pcall
+#endif /* LUA_5_2_AND_OVER */
+
 /* libs */
 #define luaopen_base dll_luaopen_base
 #define luaopen_table dll_luaopen_table
@@ -117,20 +154,28 @@
 #define luaL_openlibs dll_luaL_openlibs
 
 /* lauxlib */
-void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
-int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
 const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
 lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
 void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
+lua_State *(*dll_luaL_newstate) (void);
+void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
+void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
+void (*dll_luaL_pushresult) (luaL_Buffer *B);
+#if LUA_5_2_AND_OVER
+void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
+int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
+int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
+char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
+#else /* LUA_5_2_AND_OVER */
+void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
+int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
 int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
 int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
-lua_State *(*dll_luaL_newstate) (void);
-void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
-void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
-void (*dll_luaL_pushresult) (luaL_Buffer *B);
+#endif /* LUA_5_2_AND_OVER */
+
 /* lua */
 void       (*dll_lua_close) (lua_State *L);
 int (*dll_lua_gettop) (lua_State *L);
@@ -142,8 +187,6 @@
 int (*dll_lua_isstring) (lua_State *L, int idx);
 int (*dll_lua_type) (lua_State *L, int idx);
 int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
-lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
-lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
 int (*dll_lua_toboolean) (lua_State *L, int idx);
 const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
 void *(*dll_lua_touserdata) (lua_State *L, int idx);
@@ -166,8 +209,20 @@
 void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
 void (*dll_lua_remove) (lua_State *L, int idx);
 int (*dll_lua_setmetatable) (lua_State *L, int objindex);
+#if LUA_5_2_AND_OVER
+lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
+lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
+void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k);
+int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k);
+void (*dll_lua_setglobal) (lua_State *L, const char *var);
+void (*dll_lua_getglobal) (lua_State *L, const char *var);
+#else /* LUA_5_2_AND_OVER */
+lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
+lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
 void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
+#endif /* LUA_5_2_AND_OVER */
+
 /* libs */
 int (*dll_luaopen_base) (lua_State *L);
 int (*dll_luaopen_table) (lua_State *L);
@@ -187,20 +242,28 @@
 
 static const luaV_Reg luaV_dll[] = {
     /* lauxlib */
-    {"luaL_register", (luaV_function) &dll_luaL_register},
-    {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
     {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
     {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
     {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
     {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
     {"luaL_error", (luaV_function) &dll_luaL_error},
+    {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
+    {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
+    {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
+    {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
+#if LUA_5_2_AND_OVER
+    {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
+    {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
+    {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
+    {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
+#else /* LUA_5_2_AND_OVER */
+    {"luaL_register", (luaV_function) &dll_luaL_register},
+    {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
     {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
     {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
-    {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
-    {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
     {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
-    {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
-    {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
+#endif /* LUA_5_2_AND_OVER */
+
     /* lua */
     {"lua_close", (luaV_function) &dll_lua_close},
     {"lua_gettop", (luaV_function) &dll_lua_gettop},
@@ -212,8 +275,6 @@
     {"lua_isstring", (luaV_function) &dll_lua_isstring},
     {"lua_type", (luaV_function) &dll_lua_type},
     {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
-    {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
-    {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
     {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
     {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
     {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
@@ -236,8 +297,20 @@
     {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
     {"lua_remove", (luaV_function) &dll_lua_remove},
     {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
+#if LUA_5_2_AND_OVER
+    {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
+    {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
+    {"lua_callk", (luaV_function) &dll_lua_callk},
+    {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
+    {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
+    {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
+#else /* LUA_5_2_AND_OVER */
+    {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
+    {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
     {"lua_call", (luaV_function) &dll_lua_call},
     {"lua_pcall", (luaV_function) &dll_lua_pcall},
+#endif /* LUA_5_2_AND_OVER */
+
     /* libs */
     {"luaopen_base", (luaV_function) &dll_luaopen_base},
     {"luaopen_table", (luaV_function) &dll_luaopen_table},
@@ -470,7 +543,14 @@
 luaV_checkudata(lua_State *L, int ud, const char *tname)
 {
     void *p = luaV_toudata(L, ud, tname);
-    if (p == NULL) luaL_typerror(L, ud, tname);
+    if (p == NULL)
+    {
+#if LUA_5_2_AND_OVER
+	luaL_error(L, "type error: %d %s", ud, tname);
+#else /* LUA_5_2_AND_OVER */
+	luaL_typerror(L, ud, tname);
+#endif /* LUA_5_2_AND_OVER */
+    }
     return p;
 }
 
