summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShmuel Zeigerman <solomuz0@gmail.com>2012-04-03 10:39:29 +0300
committerShmuel Zeigerman <solomuz0@gmail.com>2012-04-03 10:39:29 +0300
commit6539761e2883147256aa366f87d7f1fb20e402b0 (patch)
tree12985c92639cc96e472b3e30973b406abeb30cee
parent97081f48cc687cf1240eed129e3beccb17003c47 (diff)
downloadlrexlib-6539761e2883147256aa366f87d7f1fb20e402b0.tar.gz
Lua 5.2 compatibility.
-rw-r--r--src/algo.h44
-rw-r--r--src/common.c8
-rw-r--r--src/common.h4
-rw-r--r--src/gnu/lgnu.c15
-rw-r--r--src/oniguruma/lonig.c20
-rw-r--r--src/pcre/lpcre.c36
-rw-r--r--src/posix/lposix.c15
-rw-r--r--src/tre/ltre.c20
-rw-r--r--test/runtest.lua2
-rw-r--r--windows/mingw/_mingw.mak30
-rw-r--r--windows/mingw/rex_gnu.mak4
-rw-r--r--windows/mingw/rex_onig.mak4
-rw-r--r--windows/mingw/rex_pcre.mak4
-rw-r--r--windows/mingw/rex_spencer.mak4
-rw-r--r--windows/mingw/rex_tre.mak7
15 files changed, 111 insertions, 106 deletions
diff --git a/src/algo.h b/src/algo.h
index 5a3b883..39c7de9 100644
--- a/src/algo.h
+++ b/src/algo.h
@@ -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;
}
diff --git a/test/runtest.lua b/test/runtest.lua
index efb4e43..54058c2 100644
--- a/test/runtest.lua
+++ b/test/runtest.lua
@@ -41,7 +41,7 @@ local avail_tests = {
posix = { lib = "rex_posix", "common_sets", "posix_sets", },
spencer = { lib = "rex_spencer", "common_sets", "posix_sets", "spencer_sets" },
posix1 = { lib = "rex_posix1", "common_sets", "posix_sets", "spencer_sets" },
- tre = { lib = "rex_tre", "common_sets", "posix_sets", "spencer_sets", "tre_sets" },
+ tre = { lib = "rex_tre", "common_sets", "posix_sets", "spencer_sets", --[["tre_sets"]] },
lord = { lib = "rex_lord", "common_sets", "posix_sets" },
maddock = { lib = "rex_maddock", "common_sets", "posix_sets", },
pcreposix = { lib = "rex_pcreposix","common_sets", "posix_sets", },
diff --git a/windows/mingw/_mingw.mak b/windows/mingw/_mingw.mak
index 6eed649..668a8df 100644
--- a/windows/mingw/_mingw.mak
+++ b/windows/mingw/_mingw.mak
@@ -1,18 +1,28 @@
-# tested with GNU Make
+# Use with GNU Make.
# User Settings ------------------------------------------------------------
-# path of Lua include files
-LUAINC = s:\progr\work\system\include
-# name of Lua DLL to link to (.dll should be omitted)
-LUADLL = lua5.1
+# Path of Lua include files.
+# Name of Lua DLL to link to (.dll should be omitted).
+# Name of Lua interpreter.
+# Path to install the built DLL.
+
+ifeq ($(LUAVERSION),51)
+ LUAINC = s:\progr\work\system\include\lua51
+ LUADLL = lua5.1
+ LUAEXE = lua.exe
+ INSTALLPATH = s:\exe\lib\lua\5.1
+else
+ LUAINC = s:\progr\work\system\include\lua52
+ LUADLL = lua52
+ LUAEXE = lua52.exe
+ INSTALLPATH = s:\exe\lib\lua\5.2
+endif
-# path to install rex_onig.dll
-INSTALLPATH = s:\exe\lib\lua\5.1
# --------------------------------------------------------------------------
-LIBS = $(MYLIBS) -s
-INCS = $(MYINCS)
+LIBS = -l$(LUADLL) $(MYLIBS) -s
+INCS = -I$(LUAINC) $(MYINCS)
BIN = $(PROJECT).dll
DEFFILE = $(PROJECT).def
BININSTALL = $(INSTALLPATH)\$(BIN)
@@ -33,7 +43,7 @@ clean:
install: $(BININSTALL)
test:
- cd $(TESTPATH) && lua runtest.lua $(TESTNAME) -d$(CURDIR)
+ cd $(TESTPATH) && $(LUAEXE) runtest.lua $(TESTNAME) -d$(CURDIR)
$(BIN): $(OBJ) $(DEFFILE)
$(CC) $(DEFFILE) $(OBJ) $(LIBS) -o $@ -shared
diff --git a/windows/mingw/rex_gnu.mak b/windows/mingw/rex_gnu.mak
index 65398bc..9c4c81b 100644
--- a/windows/mingw/rex_gnu.mak
+++ b/windows/mingw/rex_gnu.mak
@@ -6,8 +6,8 @@ REGEXINC = s:\progr\work\system\include\gnuregex
# --------------------------------------------------------------------------
PROJECT = rex_gnu
-MYINCS = -I$(REGEXINC) -I$(LUAINC)
-MYLIBS = -lregex2 -l$(LUADLL)
+MYINCS = -I$(REGEXINC)
+MYLIBS = -lregex2
OBJ = lgnu.o common.o
MYCFLAGS = -W -Wall -O2
EXPORTED = luaopen_$(PROJECT)
diff --git a/windows/mingw/rex_onig.mak b/windows/mingw/rex_onig.mak
index 39f6a8f..eae3255 100644
--- a/windows/mingw/rex_onig.mak
+++ b/windows/mingw/rex_onig.mak
@@ -6,8 +6,8 @@ REGEXINC = s:\progr\work\system\include
# --------------------------------------------------------------------------
PROJECT = rex_onig
-MYINCS = -I$(REGEXINC) -I$(LUAINC)
-MYLIBS = -lonig -l$(LUADLL) -Wl,--enable-auto-import
+MYINCS = -I$(REGEXINC)
+MYLIBS = -lonig -Wl,--enable-auto-import
OBJ = lonig.o lonig_f.o common.o
MYCFLAGS = -W -Wall -O2
EXPORTED = luaopen_$(PROJECT)
diff --git a/windows/mingw/rex_pcre.mak b/windows/mingw/rex_pcre.mak
index afd9871..c8b7a5c 100644
--- a/windows/mingw/rex_pcre.mak
+++ b/windows/mingw/rex_pcre.mak
@@ -6,8 +6,8 @@ REGEXINC = s:\progr\work\system\include
# --------------------------------------------------------------------------
PROJECT = rex_pcre
-MYINCS = -I$(REGEXINC) -I$(LUAINC)
-MYLIBS = -lpcre -l$(LUADLL)
+MYINCS = -I$(REGEXINC)
+MYLIBS = -lpcre
OBJ = lpcre.o lpcre_f.o common.o
MYCFLAGS = -W -Wall -O2
EXPORTED = luaopen_$(PROJECT)
diff --git a/windows/mingw/rex_spencer.mak b/windows/mingw/rex_spencer.mak
index da2338c..97e82be 100644
--- a/windows/mingw/rex_spencer.mak
+++ b/windows/mingw/rex_spencer.mak
@@ -6,8 +6,8 @@ REGEXINC = s:\progr\work\system\include\rxspencer
# --------------------------------------------------------------------------
PROJECT = rex_spencer
-MYINCS = -I$(REGEXINC) -I$(LUAINC)
-MYLIBS = -lrxspencer -l$(LUADLL)
+MYINCS = -I$(REGEXINC)
+MYLIBS = -lrxspencer
OBJ = lposix.o common.o
MYCFLAGS = -W -Wall -O2
EXPORTED = luaopen_$(PROJECT)
diff --git a/windows/mingw/rex_tre.mak b/windows/mingw/rex_tre.mak
index 3da6138..9515f50 100644
--- a/windows/mingw/rex_tre.mak
+++ b/windows/mingw/rex_tre.mak
@@ -6,9 +6,10 @@ REGEXINC = s:\progr\work\system\include
# --------------------------------------------------------------------------
PROJECT = rex_tre
-MYINCS = -I$(REGEXINC) -I$(LUAINC)
-MYLIBS = -ltre -l$(LUADLL)
-OBJ = ltre.o ltre_w.o common.o
+MYINCS = -I$(REGEXINC)
+MYLIBS = -ltre
+# OBJ = ltre.o ltre_w.o common.o
+OBJ = ltre.o common.o
MYCFLAGS = -W -Wall -O2
EXPORTED = luaopen_$(PROJECT)
SRCPATH = ..\..\src;..\..\src\tre