diff options
author | Shmuel Zeigerman <solomuz0@gmail.com> | 2012-04-03 10:39:29 +0300 |
---|---|---|
committer | Shmuel Zeigerman <solomuz0@gmail.com> | 2012-04-03 10:39:29 +0300 |
commit | 6539761e2883147256aa366f87d7f1fb20e402b0 (patch) | |
tree | 12985c92639cc96e472b3e30973b406abeb30cee /src | |
parent | 97081f48cc687cf1240eed129e3beccb17003c47 (diff) | |
download | lrexlib-6539761e2883147256aa366f87d7f1fb20e402b0.tar.gz |
Lua 5.2 compatibility.
Diffstat (limited to 'src')
-rw-r--r-- | src/algo.h | 44 | ||||
-rw-r--r-- | src/common.c | 8 | ||||
-rw-r--r-- | src/common.h | 4 | ||||
-rw-r--r-- | src/gnu/lgnu.c | 15 | ||||
-rw-r--r-- | src/oniguruma/lonig.c | 20 | ||||
-rw-r--r-- | src/pcre/lpcre.c | 36 | ||||
-rw-r--r-- | src/posix/lposix.c | 15 | ||||
-rw-r--r-- | src/tre/ltre.c | 20 |
8 files changed, 78 insertions, 84 deletions
@@ -13,6 +13,12 @@ static int split_exec (TUserdata *ud, TArgExec *argE, int offset); static int compile_regex (lua_State *L, const TArgComp *argC, TUserdata **pud); static int generate_error (lua_State *L, const TUserdata *ud, int errcode); +#if LUA_VERSION_NUM == 501 +# define ALG_ENVIRONINDEX LUA_ENVIRONINDEX +#else +# define ALG_ENVIRONINDEX lua_upvalueindex(1) +#endif + #ifndef ALG_CHARSIZE # define ALG_CHARSIZE 1 #endif @@ -71,7 +77,7 @@ static int OptLimit (lua_State *L, int pos) { int a = lua_tointeger (L, pos); return a < 0 ? 0 : a; } - return luaL_typeerror (L, pos, "number or function"); + return luaL_typerror (L, pos, "number or function"); } @@ -92,7 +98,7 @@ static TUserdata* test_ud (lua_State *L, int pos) { TUserdata *ud; if (lua_getmetatable(L, pos) && - lua_rawequal(L, -1, LUA_ENVIRONINDEX) && + lua_rawequal(L, -1, ALG_ENVIRONINDEX) && (ud = (TUserdata *)lua_touserdata(L, pos)) != NULL) { lua_pop(L, 1); return ud; @@ -104,7 +110,7 @@ static TUserdata* test_ud (lua_State *L, int pos) static TUserdata* check_ud (lua_State *L) { TUserdata *ud = test_ud(L, 1); - if (ud == NULL) luaL_typeerror(L, 1, REX_TYPENAME); + if (ud == NULL) luaL_typerror(L, 1, REX_TYPENAME); return ud; } @@ -116,7 +122,7 @@ static void check_pattern (lua_State *L, int pos, TArgComp *argC) argC->ud = NULL; } else if ((argC->ud = test_ud (L, pos)) == NULL) - luaL_typeerror(L, pos, "string or "REX_TYPENAME); + luaL_typerror(L, pos, "string or "REX_TYPENAME); } static void checkarg_new (lua_State *L, TArgComp *argC) { @@ -134,7 +140,7 @@ static void checkarg_gsub (lua_State *L, TArgComp *argC, TArgExec *argE) { argE->reptype = lua_type (L, 3); if (argE->reptype != LUA_TSTRING && argE->reptype != LUA_TTABLE && argE->reptype != LUA_TFUNCTION) { - luaL_typeerror (L, 3, "string, table or function"); + luaL_typerror (L, 3, "string, table or function"); } argE->funcpos = 3; argE->funcpos2 = 4; @@ -659,3 +665,31 @@ static int algm_tfind (lua_State *L) { static int algm_exec (lua_State *L) { return generic_find_method (L, METHOD_EXEC); } + +static void alg_register (lua_State *L, const luaL_Reg *r_methods, + const luaL_Reg *r_functions, const char *name) { + /* create a new function environment to serve as a metatable for methods */ +#if LUA_VERSION_NUM == 501 + lua_newtable (L); + lua_pushvalue (L, -1); + lua_replace (L, LUA_ENVIRONINDEX); + luaL_register (L, NULL, r_methods); +#else + luaL_newmetatable(L, REX_TYPENAME); + lua_pushvalue(L, -1); + luaL_setfuncs (L, r_methods, 1); +#endif + lua_pushvalue(L, -1); /* mt.__index = mt */ + lua_setfield(L, -2, "__index"); + + /* register functions */ +#if LUA_VERSION_NUM == 501 + luaL_register (L, REX_LIBNAME, r_functions); +#else + lua_newtable(L); + lua_pushvalue(L, -2); + luaL_setfuncs (L, r_functions, 1); +#endif + lua_pushfstring (L, REX_VERSION" (for %s)", name); + lua_setfield (L, -2, "_VERSION"); +} diff --git a/src/common.c b/src/common.c index 0b444c0..e1adf2e 100644 --- a/src/common.c +++ b/src/common.c @@ -246,3 +246,11 @@ int bufferZ_next (TBuffer *buf, size_t *iter, size_t *num, const char **str) { } return 0; } + +#if LUA_VERSION_NUM > 501 +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); +} +#endif diff --git a/src/common.h b/src/common.h index 95aa79d..ee8dd52 100644 --- a/src/common.h +++ b/src/common.h @@ -6,8 +6,8 @@ #include "lua.h" -#if LUA_VERSION_NUM < 502 -#define luaL_typeerror luaL_typerror +#if LUA_VERSION_NUM > 501 + int luaL_typerror (lua_State *L, int narg, const char *tname); #endif /* REX_API can be overridden from the command line or Makefile */ diff --git a/src/gnu/lgnu.c b/src/gnu/lgnu.c index b937a81..c5da82e 100644 --- a/src/gnu/lgnu.c +++ b/src/gnu/lgnu.c @@ -140,7 +140,7 @@ static int compile_regex (lua_State *L, const TArgComp *argC, TGnu **pud) { ud->errmsg = res; ret = generate_error (L, ud, 0); } else { - lua_pushvalue (L, LUA_ENVIRONINDEX); + lua_pushvalue (L, ALG_ENVIRONINDEX); lua_setmetatable (L, -2); if (pud) *pud = ud; @@ -306,17 +306,6 @@ static const luaL_Reg r_functions[] = { /* Open the library */ REX_API int REX_OPENLIB (lua_State *L) { - /* create a new function environment to serve as a metatable for methods */ - lua_newtable (L); - lua_pushvalue (L, -1); - lua_replace (L, LUA_ENVIRONINDEX); - lua_pushvalue(L, -1); /* mt.__index = mt */ - lua_setfield(L, -2, "__index"); - luaL_register (L, NULL, r_methods); - - /* register functions */ - luaL_register (L, REX_LIBNAME, r_functions); - lua_pushliteral (L, REX_VERSION" (for GNU regexes)"); - lua_setfield (L, -2, "_VERSION"); + alg_register(L, r_methods, r_functions, "GNU regexes"); return 1; } diff --git a/src/oniguruma/lonig.c b/src/oniguruma/lonig.c index 0fa13f9..d97f922 100644 --- a/src/oniguruma/lonig.c +++ b/src/oniguruma/lonig.c @@ -97,7 +97,7 @@ static int getcflags (lua_State *L, int pos) { return res; } default: - return luaL_typeerror (L, pos, "number or string"); + return luaL_typerror (L, pos, "number or string"); } } @@ -217,7 +217,7 @@ static int compile_regex (lua_State *L, const TArgComp *argC, TOnig **pud) { ud = (TOnig*)lua_newuserdata (L, sizeof (TOnig)); memset (ud, 0, sizeof (TOnig)); /* initialize all members to 0 */ - lua_pushvalue (L, LUA_ENVIRONINDEX); + lua_pushvalue (L, ALG_ENVIRONINDEX); lua_setmetatable (L, -2); r = onig_new(&ud->reg, (CUC)argC->pattern, (CUC)argC->pattern + argC->patlen, @@ -343,22 +343,8 @@ REX_API int REX_OPENLIB (lua_State *L) { return luaL_error (L, "%s requires at least version %d of Oniguruma library", REX_LIBNAME, (int)ONIGURUMA_VERSION_MAJOR); } - onig_init(); onig_set_default_syntax(ONIG_SYNTAX_RUBY); - - /* create a new function environment to serve as a metatable for methods */ - lua_newtable (L); - lua_pushvalue (L, -1); - lua_replace (L, LUA_ENVIRONINDEX); - lua_pushvalue(L, -1); /* mt.__index = mt */ - lua_setfield(L, -2, "__index"); - luaL_register (L, NULL, r_methods); - - /* register functions */ - luaL_register (L, REX_LIBNAME, r_functions); - lua_pushliteral (L, REX_VERSION" (for Oniguruma)"); - lua_setfield (L, -2, "_VERSION"); - + alg_register(L, r_methods, r_functions, "Oniguruma"); return 1; } diff --git a/src/pcre/lpcre.c b/src/pcre/lpcre.c index 2e6dc6e..2ac56eb 100644 --- a/src/pcre/lpcre.c +++ b/src/pcre/lpcre.c @@ -89,7 +89,7 @@ const char chartables_typename[] = "chartables"; static void push_chartables_meta (lua_State *L) { lua_pushinteger (L, INDEX_CHARTABLES_META); - lua_rawget (L, LUA_ENVIRONINDEX); + lua_rawget (L, ALG_ENVIRONINDEX); } static int getcflags (lua_State *L, int pos) { @@ -113,7 +113,7 @@ static int getcflags (lua_State *L, int pos) { return res; } default: - return luaL_typeerror (L, pos, "number or string"); + return luaL_typerror (L, pos, "number or string"); } } @@ -190,7 +190,7 @@ static int compile_regex (lua_State *L, const TArgComp *argC, TPcre **pud) { ud = (TPcre*)lua_newuserdata (L, sizeof (TPcre)); memset (ud, 0, sizeof (TPcre)); /* initialize all members to 0 */ - lua_pushvalue (L, LUA_ENVIRONINDEX); + lua_pushvalue (L, ALG_ENVIRONINDEX); lua_setmetatable (L, -2); if (argC->locale) { @@ -204,7 +204,7 @@ static int compile_regex (lua_State *L, const TArgComp *argC, TPcre **pud) { else if (argC->tables) { tables = argC->tables; lua_pushinteger (L, INDEX_CHARTABLES_LINK); - lua_rawget (L, LUA_ENVIRONINDEX); + lua_rawget (L, ALG_ENVIRONINDEX); lua_pushvalue (L, -2); lua_pushvalue (L, argC->tablespos); lua_rawset (L, -3); @@ -403,35 +403,33 @@ REX_API int REX_OPENLIB (lua_State *L) { return luaL_error (L, "%s requires at least version %d of PCRE library", REX_LIBNAME, (int)PCRE_MAJOR); } - /* create a new function environment to serve as a metatable for methods */ - lua_newtable (L); - lua_pushvalue (L, -1); - lua_replace (L, LUA_ENVIRONINDEX); - lua_pushvalue(L, -1); /* mt.__index = mt */ - lua_setfield(L, -2, "__index"); - luaL_register (L, NULL, r_methods); - /* register functions */ - luaL_register (L, REX_LIBNAME, r_functions); - lua_pushliteral (L, REX_VERSION" (for PCRE)"); - lua_setfield (L, -2, "_VERSION"); + alg_register(L, r_methods, r_functions, "PCRE"); /* create a table and register it as a metatable for "chartables" userdata */ - lua_pushinteger (L, INDEX_CHARTABLES_META); lua_newtable (L); lua_pushliteral (L, "access denied"); lua_setfield (L, -2, "__metatable"); +#if LUA_VERSION_NUM == 501 luaL_register (L, NULL, chartables_meta); - lua_rawset (L, LUA_ENVIRONINDEX); + lua_rawseti (L, LUA_ENVIRONINDEX, INDEX_CHARTABLES_META); +#else + lua_pushvalue(L, -3); + luaL_setfuncs (L, chartables_meta, 1); + lua_rawseti (L, -3, INDEX_CHARTABLES_META); +#endif /* create a table for connecting "chartables" userdata to "regex" userdata */ - lua_pushinteger (L, INDEX_CHARTABLES_LINK); lua_newtable (L); lua_pushliteral (L, "k"); /* weak keys */ lua_setfield (L, -2, "__mode"); lua_pushvalue (L, -1); /* setmetatable (tb, tb) */ lua_setmetatable (L, -2); - lua_rawset (L, LUA_ENVIRONINDEX); +#if LUA_VERSION_NUM == 501 + lua_rawseti (L, LUA_ENVIRONINDEX, INDEX_CHARTABLES_LINK); +#else + lua_rawseti (L, -3, INDEX_CHARTABLES_LINK); +#endif return 1; } diff --git a/src/posix/lposix.c b/src/posix/lposix.c index 036945c..cfaaaf3 100644 --- a/src/posix/lposix.c +++ b/src/posix/lposix.c @@ -109,7 +109,7 @@ static int compile_regex (lua_State *L, const TArgComp *argC, TPosix **pud) { if (argC->cflags & REG_NOSUB) ud->r.re_nsub = 0; ud->match = (regmatch_t *) Lmalloc (L, (ALG_NSUB(ud) + 1) * sizeof (regmatch_t)); - lua_pushvalue (L, LUA_ENVIRONINDEX); + lua_pushvalue (L, ALG_ENVIRONINDEX); lua_setmetatable (L, -2); if (pud) *pud = ud; @@ -277,17 +277,6 @@ static const luaL_Reg r_functions[] = { /* Open the library */ REX_API int REX_OPENLIB (lua_State *L) { - /* create a new function environment to serve as a metatable for methods */ - lua_newtable (L); - lua_pushvalue (L, -1); - lua_replace (L, LUA_ENVIRONINDEX); - lua_pushvalue(L, -1); /* mt.__index = mt */ - lua_setfield(L, -2, "__index"); - luaL_register (L, NULL, r_methods); - - /* register functions */ - luaL_register (L, REX_LIBNAME, r_functions); - lua_pushliteral (L, REX_VERSION" (for POSIX regexes)"); - lua_setfield (L, -2, "_VERSION"); + alg_register(L, r_methods, r_functions, "POSIX regexes"); return 1; } diff --git a/src/tre/ltre.c b/src/tre/ltre.c index 121d07e..ff5cae3 100644 --- a/src/tre/ltre.c +++ b/src/tre/ltre.c @@ -108,7 +108,7 @@ static int compile_regex (lua_State *L, const TArgComp *argC, TPosix **pud) { if (argC->cflags & REG_NOSUB) ud->r.re_nsub = 0; ud->match = (regmatch_t *) Lmalloc (L, (ALG_NSUB(ud) + 1) * sizeof (regmatch_t)); - lua_pushvalue (L, LUA_ENVIRONINDEX); + lua_pushvalue (L, ALG_ENVIRONINDEX); lua_setmetatable (L, -2); if (pud) *pud = ud; @@ -340,19 +340,9 @@ static const luaL_Reg r_functions[] = { /* Open the library */ REX_API int REX_OPENLIB (lua_State *L) { - /* create a new function environment to serve as a metatable for methods */ - lua_newtable (L); - lua_pushvalue (L, -1); - lua_replace (L, LUA_ENVIRONINDEX); - lua_pushvalue(L, -1); /* mt.__index = mt */ - lua_setfield(L, -2, "__index"); - luaL_register (L, NULL, r_methods); - add_wide_lib (L, 1); - - /* register functions */ - luaL_register (L, REX_LIBNAME, r_functions); - add_wide_lib (L, 0); - lua_pushliteral (L, REX_VERSION" (for TRE regexes)"); - lua_setfield (L, -2, "_VERSION"); + alg_register(L, r_methods, r_functions, "TRE regexes"); + /* TODO: */ + /* add_wide_lib (L, 1); */ + /* add_wide_lib (L, 0); */ return 1; } |