From f7f85b0826f7006b8dcc040111cd0ef8d42c2352 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Mon, 19 Jan 2015 19:12:24 +0100 Subject: prepare to use ltablib.c from 5.3 for table library --- c-api/compat-5.3.c | 3 ++- compat53.lua | 24 ++++++++++++++++-------- lprefix.h | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index ecb22b0..e45c728 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c @@ -36,7 +36,8 @@ COMPAT53_API void lua_len (lua_State *L, int i) { switch (lua_type(L, i)) { case LUA_TSTRING: /* fall through */ case LUA_TTABLE: - lua_pushnumber(L, (int)lua_objlen(L, i)); + if (!luaL_callmeta(L, i, "__len")) + lua_pushnumber(L, (int)lua_objlen(L, i)); break; case LUA_TUSERDATA: if (luaL_callmeta(L, i, "__len")) diff --git a/compat53.lua b/compat53.lua index 03158f0..58e0a24 100644 --- a/compat53.lua +++ b/compat53.lua @@ -27,8 +27,8 @@ if lua_version < "5.3" then -- load utf8 library - local ok, utf8lib = pcall(require, "compat53.utf8") - if ok then + local utf8_ok, utf8lib = pcall(require, "compat53.utf8") + if utf8_ok then utf8 = utf8lib package.loaded["utf8"] = utf8lib if lua_version == "5.1" then @@ -37,12 +37,20 @@ if lua_version < "5.3" then end + -- load table library + local table_ok, tablib = pcall(require, "compat53.table") + if table_ok then + table = tablib + package.loaded["table"] = tablib + end + + -- use Roberto's struct module for string packing/unpacking for now -- maybe we'll later extract the functions from the 5.3 string -- library for greater compatiblity, but it uses the 5.3 buffer API -- which cannot easily be backported to Lua 5.1. - local ok, struct = pcall(require, "struct") - if ok then + local struct_ok, struct = pcall(require, "struct") + if struct_ok then string.pack = struct.pack string.packsize = struct.size string.unpack = struct.unpack @@ -132,8 +140,8 @@ if lua_version < "5.3" then end - -- update table library - do + -- update table library (if C module not available) + if not table_ok then local table_concat = table.concat function table.concat(list, sep, i, j) local mt = gmt(list) @@ -298,7 +306,7 @@ if lua_version < "5.3" then return table_unpack(list, i, j) end end - end + end -- update table library @@ -576,7 +584,7 @@ if lua_version < "5.3" then end - if not is_luajit52 then + if not table_ok and not is_luajit52 then table.pack = function(...) return { n = select('#', ...), ... } end diff --git a/lprefix.h b/lprefix.h index 788da4e..0d25065 100644 --- a/lprefix.h +++ b/lprefix.h @@ -52,5 +52,22 @@ # define luaopen_utf8 luaopen_compat53_utf8 #endif +#ifdef ltablib_c +# define luaopen_table luaopen_compat53_table +static int compat53_rawgeti (lua_State *L, int i, lua_Integer n) { + return lua_rawgeti(L, i, n); +} +# undef lua_rawgeti +# define lua_rawgeti compat53_rawgeti +static void compat53_rawseti (lua_State *L, int i, lua_Integer n) { + lua_rawseti(L, i, (int)n); +} +# undef lua_rawseti +# define lua_rawseti compat53_rawseti +# if LUA_VERSION_NUM == 501 +# define lua_compare(L, a, b, op) lua_lessthan(L, a, b) +# endif +#endif /* ltablib_c */ + #endif -- cgit v1.2.3-55-g6feb