diff options
author | moteus <mimir@newmail.ru> | 2012-09-05 16:09:46 +0400 |
---|---|---|
committer | moteus <mimir@newmail.ru> | 2012-09-05 16:09:46 +0400 |
commit | 46a94cbb43975e06bdb66f47974d09d7b3b36a22 (patch) | |
tree | ee7e36b7ec56d3f7de6367122d7b7f6ec72eb13f /src | |
parent | e52ac7f6beaf32bdebb7e3af33448e79df5cc6b6 (diff) |
Support Lua 5.2
Add tests from http://scite-ru.googlecode.com/
Add lakefile
Diffstat (limited to 'src')
-rw-r--r-- | src/l52util.c | 119 | ||||
-rw-r--r-- | src/l52util.h | 46 | ||||
-rw-r--r-- | src/lua_mtutil.c | 4 | ||||
-rw-r--r-- | src/luawin_dllerror.c | 3 | ||||
-rw-r--r-- | src/winreg.c | 9 |
5 files changed, 172 insertions, 9 deletions
diff --git a/src/l52util.c b/src/l52util.c new file mode 100644 index 0000000..5675114 --- /dev/null +++ b/src/l52util.c @@ -0,0 +1,119 @@ +#include "l52util.h"
+
+#include <memory.h>
+#include <assert.h>
+
+#if LUA_VERSION_NUM >= 502
+
+int luaL_typerror (lua_State *L, int narg, const char *tname) {
+ const char *msg = lua_pushfstring(L, "%s expected, got %s", tname,
+ luaL_typename(L, narg));
+ return luaL_argerror(L, narg, msg);
+}
+
+void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l){
+ if(libname) lua_newtable(L);
+ luaL_setfuncs(L, l, 0);
+}
+
+#else
+
+void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup){
+ luaL_checkstack(L, nup, "too many upvalues");
+ for (; l->name != NULL; l++) { /* fill the table with given functions */
+ int i;
+ for (i = 0; i < nup; i++) /* copy upvalues to the top */
+ lua_pushvalue(L, -nup);
+ lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
+ lua_setfield(L, -(nup + 2), l->name);
+ }
+ lua_pop(L, nup); /* remove upvalues */
+}
+
+void lua_rawgetp(lua_State *L, int index, const void *p){
+ index = lua_absindex(L, index);
+ lua_pushlightuserdata(L, (void *)p);
+ lua_rawget(L, index);
+}
+
+void lua_rawsetp (lua_State *L, int index, const void *p){
+ index = lua_absindex(L, index);
+ lua_pushlightuserdata(L, (void *)p);
+ lua_insert(L, -2);
+ lua_rawset(L, index);
+}
+
+#endif
+
+int lutil_newmetatablep (lua_State *L, const void *p) {
+ lua_rawgetp(L, LUA_REGISTRYINDEX, p);
+ if (!lua_isnil(L, -1)) /* name already in use? */
+ return 0; /* leave previous value on top, but return 0 */
+ lua_pop(L, 1);
+
+ lua_newtable(L); /* create metatable */
+ lua_pushvalue(L, -1); /* duplicate metatable to set*/
+ lua_rawsetp(L, LUA_REGISTRYINDEX, p);
+
+ return 1;
+}
+
+void lutil_getmetatablep (lua_State *L, const void *p) {
+ lua_rawgetp(L, LUA_REGISTRYINDEX, p);
+}
+
+void lutil_setmetatablep (lua_State *L, const void *p) {
+ lutil_getmetatablep(L, p);
+ assert(lua_istable(L,-1));
+ lua_setmetatable (L, -2);
+}
+
+int lutil_isudatap (lua_State *L, int ud, const void *p) {
+ if (lua_isuserdata(L, ud)){
+ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
+ int res;
+ lutil_getmetatablep(L,p); /* get correct metatable */
+ res = lua_rawequal(L, -1, -2); /* does it have the correct mt? */
+ lua_pop(L, 2); /* remove both metatables */
+ return res;
+ }
+ }
+ return 0;
+}
+
+void *lutil_checkudatap (lua_State *L, int ud, const void *p) {
+ void *up = lua_touserdata(L, ud);
+ if (up != NULL) { /* value is a userdata? */
+ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */
+ lutil_getmetatablep(L,p); /* get correct metatable */
+ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */
+ lua_pop(L, 2); /* remove both metatables */
+ return up;
+ }
+ }
+ }
+ luaL_typerror(L, ud, p); /* else error */
+ return NULL; /* to avoid warnings */
+}
+
+int lutil_createmetap (lua_State *L, const void *p, const luaL_Reg *methods) {
+ if (!lutil_newmetatablep(L, p))
+ return 0;
+
+ /* define methods */
+ luaL_setfuncs (L, methods, 0);
+
+ /* define metamethods */
+ lua_pushliteral (L, "__index");
+ lua_pushvalue (L, -2);
+ lua_settable (L, -3);
+
+ return 1;
+}
+
+void *lutil_newudatap_impl(lua_State *L, size_t size, const void *p){
+ void *obj = lua_newuserdata (L, size);
+ memset(obj, 0, size);
+ lutil_setmetatablep(L, p);
+ return obj;
+}
diff --git a/src/l52util.h b/src/l52util.h new file mode 100644 index 0000000..6391307 --- /dev/null +++ b/src/l52util.h @@ -0,0 +1,46 @@ +#ifndef _LZUTILS_H_
+#define _LZUTILS_H_
+
+#include "lua.h"
+#include "lauxlib.h"
+
+#if LUA_VERSION_NUM >= 502 // lua 5.2
+
+// lua_rawgetp
+// lua_rawsetp
+// luaL_setfuncs
+// lua_absindex
+
+
+#define lua_objlen lua_rawlen
+
+int luaL_typerror (lua_State *L, int narg, const char *tname);
+
+void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l);
+
+#else // lua 5.1
+
+// functions form lua 5.2
+
+# define lua_absindex(L, i) (((i)>0)?(i):((i)<=LUA_REGISTRYINDEX?(i):(lua_gettop(L)+(i)+1)))
+# define lua_rawlen lua_objlen
+
+void lua_rawgetp (lua_State *L, int index, const void *p);
+void lua_rawsetp (lua_State *L, int index, const void *p);
+void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
+
+#endif
+
+int lutil_newmetatablep (lua_State *L, const void *p);
+void lutil_getmetatablep (lua_State *L, const void *p);
+void lutil_setmetatablep (lua_State *L, const void *p);
+
+#define lutil_newudatap(L, TTYPE, TNAME) (TTYPE *)lutil_newudatap_impl(L, sizeof(TTYPE), TNAME)
+int lutil_isudatap (lua_State *L, int ud, const void *p);
+void *lutil_checkudatap (lua_State *L, int ud, const void *p);
+int lutil_createmetap (lua_State *L, const void *p, const luaL_Reg *methods);
+
+void *lutil_newudatap_impl (lua_State *L, size_t size, const void *p);
+
+#endif
+
diff --git a/src/lua_mtutil.c b/src/lua_mtutil.c index 5f6bea2..b7a927f 100644 --- a/src/lua_mtutil.c +++ b/src/lua_mtutil.c @@ -1,12 +1,14 @@ #include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
+#include "l52util.h"
+
/* push metatable to stack, create if not yet reg, the metatable of the table is its self */
int lua_opentablemt(lua_State *L, const char * libname, const luaL_reg * reg, int upval){
/*STACK:...<upvals>#*/
if(luaL_newmetatable(L, libname)){
/*STACK:...<upvals><tablemt>#*/
- luaL_openlib(L, NULL, reg, upval);
+ luaL_setfuncs(L, reg, upval);
lua_pushliteral(L, "__index");
/*STACK:...<tablemt><"__index">#*/
lua_pushvalue(L, -2);
diff --git a/src/luawin_dllerror.c b/src/luawin_dllerror.c index 68f0db0..d661083 100644 --- a/src/luawin_dllerror.c +++ b/src/luawin_dllerror.c @@ -12,8 +12,7 @@ void lua_dllerror(lua_State *L, DWORD dwErr){ /*
if lgk_dllerror is present in global env and equal to false dont raise error!
*/
- lua_pushliteral(L, lgk_dllerror);
- lua_rawget(L, LUA_GLOBALSINDEX);
+ lua_getglobal(L, lgk_dllerror);
if(lua_isrealfalse(L, -1)){
WIN_TRACEA("lua_dllerror:false");
diff --git a/src/winreg.c b/src/winreg.c index e3aafb3..5257166 100644 --- a/src/winreg.c +++ b/src/winreg.c @@ -5,13 +5,10 @@ #include <assert.h>
#define lua_assert assert
-#ifndef uintptr_t
- typedef unsigned uintptr_t;
-#endif
-
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
+#include "l52util.h"
#include "stdmacro.h"
#include "luamacro.h"
@@ -138,7 +135,7 @@ HKEY reg_aux_strtohkey(lua_State *L, const char * psz){ INT64 x;
if(atoINT64(psz, &x)){
WIN_TRACEA("DIGIT ROOTKEY %s", psz);
- return (HKEY)(uintptr_t)x;
+ return (HKEY)(size_t)x;
}else{
for(pph = ph; pph->name && stricmp(psz, pph->name); pph++);
if(!pph->data)luaL_error(L, "invalid prefix key '%s'", psz);
@@ -283,7 +280,7 @@ BOOL reg_aux_setvalue(lua_State *L, HKEY hKey, const TCHAR * pszVal, int type, i luaL_buffinit(L, &B);
if(lua_istable(L, i)){
int n;
- int last = luaL_getn(L, i);
+ int last = lua_objlen(L, i);
for (n = 1; n <= last; n++) {
lua_rawgeti(L, i, n);
luaL_addstring(&B, lua_checkstring(L, -1));
|