From ff90f3f75e97f5f49e685829d77b65f2621d3e7b Mon Sep 17 00:00:00 2001 From: Alexandre Erwin Ittner Date: Wed, 17 Nov 2010 01:28:02 -0200 Subject: [PATCH] Convert library to Lua 5.2 Lua 5.2 brings some API changes; This commit converts the library to the new version and removes the backward compatibility code between Lua 5.0 and 5.1 (which makes no sense anymore). --- README | 8 ++++---- luaiconv.c | 30 +++++++++--------------------- test_iconv.lua | 2 +- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/README b/README index 6d63a74..191b7c7 100644 --- a/README +++ b/README @@ -11,8 +11,8 @@ sequence of corresponding characters in another codeset. The codesets are those specified in the iconv.new() call that returned the conversion descriptor, cd. -Lua-iconv 6 and later *requires* Lua 5.1. If you are using Lua 5.0, please -use the first release (lua-iconv-r1). +Lua-iconv 7 and later *requires* Lua 5.2. If you are using Lua 5.1, please +use the release 6; For Lua 5.0, use the first release (lua-iconv-r1). Details on iconv may be obtained in the Open Group's interface definition (http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html). @@ -39,7 +39,7 @@ untar the distribution package and, within the program directory, type: make install as root. The library will be compiled and installed on the in the correct -path (which is defined in lua5.1.pc). Compiling on systems without pkg-config +path (which is defined in lua5.2.pc). Compiling on systems without pkg-config requires manual changes in the Makefile (this includes Windows). @@ -49,7 +49,7 @@ requires manual changes in the Makefile (this includes Windows). Lua-iconv is a shared library that must be loaded in the Lua interpreter before use. From Lua 5.1 and later, you can simply do a - require "iconv" + local iconv = require("iconv") call to load up the library (that, of course, must be installed in a directory from package.cpath). diff --git a/luaiconv.c b/luaiconv.c index 2130000..4b08480 100644 --- a/luaiconv.c +++ b/luaiconv.c @@ -36,20 +36,9 @@ #include #define LIB_NAME "iconv" -#define LIB_VERSION LIB_NAME " 6" +#define LIB_VERSION LIB_NAME " 7" #define ICONV_TYPENAME "iconv_t" - -/* Compatibility between Lua 5.1+ and Lua 5.0 */ -#ifndef LUA_VERSION_NUM -#define LUA_VERSION_NUM 0 -#endif -#if LUA_VERSION_NUM < 501 -#define luaL_register(a, b, c) luaL_openlib((a), (b), (c), 0) -#endif - - -/* Emulates lua_(un)boxpointer from Lua 5.0 (don't exists on Lua 5.1) */ #define BOXPTR(L, p) (*(void**)(lua_newuserdata(L, sizeof(void*))) = (p)) #define UNBOXPTR(L, i) (*(void**)(lua_touserdata(L, i))) @@ -101,7 +90,7 @@ static int Liconv_open(lua_State *L) { static int Liconv(lua_State *L) { iconv_t cd = get_iconv_t(L, 1); - size_t ibleft = lua_strlen(L, 2); + size_t ibleft = lua_rawlen(L, 2); char *inbuf = (char*) luaL_checkstring(L, 2); char *outbuf; char *outbufs; @@ -211,14 +200,8 @@ static const luaL_reg inconvFuncs[] = { }; -static const luaL_reg iconvMT[] = { - { "__gc", Liconv_close }, - { NULL, NULL } -}; - - int luaopen_iconv(lua_State *L) { - luaL_register(L, LIB_NAME, inconvFuncs); + luaL_newlib(L, inconvFuncs); TBL_SET_INT_CONST(L, "ERROR_NO_MEMORY", ERROR_NO_MEMORY); TBL_SET_INT_CONST(L, "ERROR_INVALID", ERROR_INVALID); @@ -230,10 +213,15 @@ int luaopen_iconv(lua_State *L) { lua_settable(L, -3); luaL_newmetatable(L, ICONV_TYPENAME); + lua_pushliteral(L, "__index"); lua_pushvalue(L, -3); lua_settable(L, -3); - luaL_openlib(L, NULL, iconvMT, 0); + + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, Liconv_close); + lua_settable(L, -3); + lua_pop(L, 1); return 1; diff --git a/test_iconv.lua b/test_iconv.lua index 0cc9e3a..972778f 100644 --- a/test_iconv.lua +++ b/test_iconv.lua @@ -1,4 +1,4 @@ -require "iconv" +local iconv = require("iconv") -- Set your terminal encoding here -- local termcs = "iso-8859-1"