From be58bb0bf683c5c15589ecf68367a1fbaa9e0a8f Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 16 Dec 2014 15:54:31 +0100 Subject: preliminary Lua 5.3 support Untested, but it might just work :). --- src/compat.h | 25 ++++++++++++++++---- src/deep.c | 2 +- src/keeper.c | 77 ++++++++++++++++++++++++++++++------------------------------ src/lanes.c | 8 +++---- src/tools.c | 9 ++++--- src/tools.h | 2 +- 6 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/compat.h b/src/compat.h index 496986b..6048974 100644 --- a/src/compat.h +++ b/src/compat.h @@ -5,9 +5,10 @@ #include "lualib.h" #include "lauxlib.h" -// code is now using Lua 5.2 API -// add Lua 5.2 API when building for Lua 5.1 +// code is now preferring Lua 5.3 API +// add some Lua 5.3-style API when building for Lua 5.1 #if LUA_VERSION_NUM == 501 +#define lua501_equal lua_equal #define lua_absindex( L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) +1) #define lua_pushglobaltable(L) lua_pushvalue( L, LUA_GLOBALSINDEX) #define lua_setuservalue lua_setfenv @@ -17,17 +18,31 @@ #define LUA_OK 0 #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value void luaL_requiref (lua_State* L, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources +#define lua503_dump( L, writer, data, strip) lua_dump( L, writer, data) #endif // LUA_VERSION_NUM == 501 // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way #if LUA_VERSION_NUM == 502 -#ifndef lua_equal // already defined when compatibility is active in luaconf.h -#define lua_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) -#endif // lua_equal +#ifndef lua501_equal // already defined when compatibility is active in luaconf.h +#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) +#endif // lua501_equal #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h #define lua_lessthan( L, a, b) lua_compare( L, a, b, LUA_OPLT) #endif // lua_lessthan #define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0) +#define lua503_dump( L, writer, data, strip) lua_dump( L, writer, data) #endif // LUA_VERSION_NUM == 502 +// wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way +#if LUA_VERSION_NUM == 503 +#ifndef lua501_equal // already defined when compatibility is active in luaconf.h +#define lua501_equal( L, a, b) lua_compare( L, a, b, LUA_OPEQ) +#endif // lua501_equal +#ifndef lua_lessthan // already defined when compatibility is active in luaconf.h +#define lua_lessthan( L, a, b) lua_compare( L, a, b, LUA_OPLT) +#endif // lua_lessthan +#define luaG_registerlibfuncs( L, _funcs) luaL_setfuncs( L, _funcs, 0) +#define lua503_dump lua_dump +#endif // LUA_VERSION_NUM == 503 + #endif // __COMPAT_H__ diff --git a/src/deep.c b/src/deep.c index 52a6485..b5d6aee 100644 --- a/src/deep.c +++ b/src/deep.c @@ -309,7 +309,7 @@ char const* push_deep_proxy( struct s_Universe* U, lua_State* L, DEEP_PRELUDE* p return "Bad idfunc(eOP_metatable): unexpected pushed value"; } luaG_pushdeepversion( L); // DPC proxy metatable deepversion deepversion - if( !lua_equal( L, -1, -2)) + if( !lua501_equal( L, -1, -2)) { lua_pop( L, 5); // return "Bad idfunc(eOP_metatable): mismatched deep version"; diff --git a/src/keeper.c b/src/keeper.c index 8bece77..ea061ce 100644 --- a/src/keeper.c +++ b/src/keeper.c @@ -58,22 +58,22 @@ typedef struct { - int first; - int count; - int limit; + lua_Integer first; + lua_Integer count; + lua_Integer limit; } keeper_fifo; // replaces the fifo ud by its uservalue on the stack -static keeper_fifo* prepare_fifo_access( lua_State* L, int idx) +static keeper_fifo* prepare_fifo_access( lua_State* L, int idx_) { - keeper_fifo* fifo = (keeper_fifo*) lua_touserdata( L, idx); + keeper_fifo* fifo = (keeper_fifo*) lua_touserdata( L, idx_); if( fifo != NULL) { - idx = lua_absindex( L, idx); + idx_ = lua_absindex( L, idx_); STACK_GROW( L, 1); // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around - lua_getuservalue( L, idx); - lua_replace( L, idx); + lua_getuservalue( L, idx_); + lua_replace( L, idx_); } return fifo; } @@ -94,18 +94,18 @@ static void fifo_new( lua_State* L) // in: expect fifo ... on top of the stack // out: nothing, removes all pushed values from the stack -static void fifo_push( lua_State* L, keeper_fifo* fifo, int _count) +static void fifo_push( lua_State* L, keeper_fifo* fifo_, lua_Integer count_) { - int idx = lua_gettop( L) - _count; - int start = fifo->first + fifo->count - 1; - int i; + int const idx = lua_gettop( L) - (int) count_; + lua_Integer start = fifo_->first + fifo_->count - 1; + lua_Integer i; // pop all additional arguments, storing them in the fifo - for( i = _count; i >= 1; -- i) + for( i = count_; i >= 1; -- i) { // store in the fifo the value at the top of the stack at the specified index, popping it from the stack lua_rawseti( L, idx, start + i); } - fifo->count += _count; + fifo_->count += count_; } // in: fifo @@ -113,28 +113,28 @@ static void fifo_push( lua_State* L, keeper_fifo* fifo, int _count) // expects exactly 1 value on the stack! // currently only called with a count of 1, but this may change in the future // function assumes that there is enough data in the fifo to satisfy the request -static void fifo_peek( lua_State* L, keeper_fifo* fifo, int count_) +static void fifo_peek( lua_State* L, keeper_fifo* fifo_, lua_Integer count_) { int i; STACK_GROW( L, count_); for( i = 0; i < count_; ++ i) { - lua_rawgeti( L, 1, fifo->first + i); + lua_rawgeti( L, 1, fifo_->first + i); } } // in: fifo // out: remove the fifo from the stack, push as many items as required on the stack (function assumes they exist in sufficient number) -static void fifo_pop( lua_State* L, keeper_fifo* fifo, int count_) +static void fifo_pop( lua_State* L, keeper_fifo* fifo_, lua_Integer count_) { - int fifo_idx = lua_gettop( L); // ... fifo + int const fifo_idx = lua_gettop( L); // ... fifo int i; // each iteration pushes a value on the stack! STACK_GROW( L, count_ + 2); // skip first item, we will push it last for( i = 1; i < count_; ++ i) { - int const at = fifo->first + i; + lua_Integer const at = fifo_->first + i; // push item on the stack lua_rawgeti( L, fifo_idx, at); // ... fifo val // remove item from the fifo @@ -143,7 +143,7 @@ static void fifo_pop( lua_State* L, keeper_fifo* fifo, int count_) } // now process first item { - int const at = fifo->first; + lua_Integer const at = fifo_->first; lua_rawgeti( L, fifo_idx, at); // ... fifo vals val lua_pushnil( L); // ... fifo vals val nil lua_rawseti( L, fifo_idx, at); // ... fifo vals val @@ -151,23 +151,23 @@ static void fifo_pop( lua_State* L, keeper_fifo* fifo, int count_) } { // avoid ever-growing indexes by resetting each time we detect the fifo is empty - int const new_count = fifo->count - count_; - fifo->first = (new_count == 0) ? 1 : (fifo->first + count_); - fifo->count = new_count; + lua_Integer const new_count = fifo_->count - count_; + fifo_->first = (new_count == 0) ? 1 : (fifo_->first + count_); + fifo_->count = new_count; } } // in: linda_ud expected at *absolute* stack slot idx // out: fifos[ud] static void* const fifos_key = (void*) prepare_fifo_access; -static void push_table( lua_State* L, int idx) +static void push_table( lua_State* L, int idx_) { STACK_GROW( L, 4); STACK_CHECK( L); - idx = lua_absindex( L, idx); + idx_ = lua_absindex( L, idx_); lua_pushlightuserdata( L, fifos_key); // ud fifos_key lua_rawget( L, LUA_REGISTRYINDEX); // ud fifos - lua_pushvalue( L, idx); // ud fifos ud + lua_pushvalue( L, idx_); // ud fifos ud lua_rawget( L, -2); // ud fifos fifos[ud] STACK_MID( L, 2); if( lua_isnil( L, -1)) @@ -175,7 +175,7 @@ static void push_table( lua_State* L, int idx) lua_pop( L, 1); // ud fifos // add a new fifos table for this linda lua_newtable( L); // ud fifos fifos[ud] - lua_pushvalue( L, idx); // ud fifos fifos[ud] ud + lua_pushvalue( L, idx_); // ud fifos fifos[ud] ud lua_pushvalue( L, -2); // ud fifos fifos[ud] ud fifos[ud] lua_rawset( L, -4); // ud fifos fifos[ud] } @@ -183,16 +183,16 @@ static void push_table( lua_State* L, int idx) STACK_END( L, 1); } -int keeper_push_linda_storage( struct s_Universe* U, lua_State* L, void* ptr, unsigned long magic_) +int keeper_push_linda_storage( struct s_Universe* U, lua_State* L, void* ptr_, unsigned long magic_) { - struct s_Keeper* K = keeper_acquire( U->keepers, magic_); - lua_State* KL = K ? K->L : NULL; + struct s_Keeper* const K = keeper_acquire( U->keepers, magic_); + lua_State* const KL = K ? K->L : NULL; if( KL == NULL) return 0; STACK_GROW( KL, 4); STACK_CHECK( KL); lua_pushlightuserdata( KL, fifos_key); // fifos_key lua_rawget( KL, LUA_REGISTRYINDEX); // fifos - lua_pushlightuserdata( KL, ptr); // fifos ud + lua_pushlightuserdata( KL, ptr_); // fifos ud lua_rawget( KL, -2); // fifos storage lua_remove( KL, -2); // storage if( !lua_istable( KL, -1)) @@ -323,11 +323,11 @@ int keepercall_receive( lua_State* L) //in: linda_ud key mincount [maxcount] int keepercall_receive_batched( lua_State* L) { - int const min_count = (int) lua_tointeger( L, 3); + lua_Integer const min_count = lua_tointeger( L, 3); if( min_count > 0) { keeper_fifo* fifo; - int const max_count = (int) luaL_optinteger( L, 4, min_count); + lua_Integer const max_count = luaL_optinteger( L, 4, min_count); lua_settop( L, 2); // ud key lua_insert( L, 1); // key ud push_table( L, 2); // key ud fifos @@ -357,7 +357,7 @@ int keepercall_receive_batched( lua_State* L) int keepercall_limit( lua_State* L) { keeper_fifo* fifo; - int limit = (int) lua_tointeger( L, 3); + lua_Integer limit = lua_tointeger( L, 3); push_table( L, 1); // ud key n fifos lua_replace( L, 1); // fifos key n lua_pop( L, 1); // fifos key @@ -429,7 +429,7 @@ int keepercall_set( lua_State* L) } else // set/replace contents stored at the specified key? { - int count = lua_gettop( L) - 2; // number of items we want to store + lua_Integer count = lua_gettop( L) - 2; // number of items we want to store keeper_fifo* fifo; // fifos key [val [, ...]] lua_pushvalue( L, 2); // fifos key [val [, ...]] key lua_rawget( L, 1); // fifos key [val [, ...]] fifo|nil @@ -466,7 +466,7 @@ int keepercall_set( lua_State* L) int keepercall_get( lua_State* L) { keeper_fifo* fifo; - int count = 1; + lua_Integer count = 1; if( lua_gettop( L) == 3) // ud key count { count = lua_tointeger( L, 3); @@ -482,7 +482,7 @@ int keepercall_get( lua_State* L) count = __min( count, fifo->count); // read value off the fifo fifo_peek( L, fifo, count); // fifo ... - return count; + return (int) count; } // no fifo was ever registered for this key, or it is empty return 0; @@ -491,7 +491,6 @@ int keepercall_get( lua_State* L) // in: linda_ud [, key [, ...]] int keepercall_count( lua_State* L) { - int top; push_table( L, 1); // ud keys fifos switch( lua_gettop( L)) { @@ -537,7 +536,7 @@ int keepercall_count( lua_State* L) lua_replace( L, 1); // out keys fifos // shifts all keys up in the stack. potentially slow if there are a lot of them, but then it should be bearable lua_insert( L, 2); // out fifos keys - while( (top = lua_gettop( L)) > 2) + while( lua_gettop( L) > 2) { keeper_fifo* fifo; lua_pushvalue( L, -1); // out fifos keys key diff --git a/src/lanes.c b/src/lanes.c index 7f641c3..9b95469 100644 --- a/src/lanes.c +++ b/src/lanes.c @@ -630,7 +630,7 @@ LUAG_FUNC( linda_receive) { int is_batched; lua_pushliteral( L, BATCH_SENTINEL); - is_batched = lua_equal( L, key_i, -1); + is_batched = lua501_equal( L, key_i, -1); lua_pop( L, 1); if( is_batched) { @@ -846,7 +846,7 @@ LUAG_FUNC( linda_get) { struct s_Linda* const linda = lua_toLinda( L, 1); int pushed; - int count = luaL_optint( L, 3, 1); + lua_Integer count = luaL_optinteger( L, 3, 1); luaL_argcheck( L, count >= 1, 3, "count should be >= 1"); luaL_argcheck( L, lua_gettop( L) <= 3, 4, "too many arguments"); @@ -1144,7 +1144,7 @@ static void* linda_id( lua_State* L, enum eDeepOp op_) case 2: // 2 parameters, a name and group, in that order linda_name = lua_tolstring( L, -2, &name_len); - linda_group = lua_tointeger( L, -1); + linda_group = (unsigned long) lua_tointeger( L, -1); break; } @@ -1982,7 +1982,7 @@ LUAG_FUNC( get_debug_threadname) LUAG_FUNC( set_thread_priority) { - int const prio = luaL_checkint( L, 1); + int const prio = (int) luaL_checkinteger( L, 1); // public Lanes API accepts a generic range -3/+3 // that will be remapped into the platform-specific scheduler priority scheme // On some platforms, -3 is equivalent to -2 and +3 to +2 diff --git a/src/tools.c b/src/tools.c index e0f4c40..bd1ea85 100644 --- a/src/tools.c +++ b/src/tools.c @@ -286,7 +286,7 @@ FuncSubType luaG_getfuncsubtype( lua_State *L, int _i) // the provided writer fails with code 666 // therefore, anytime we get 666, this means that lua_dump() attempted a dump // all other cases mean this is either a C or LuaJIT-fast function - dumpres = lua_dump( L, dummy_writer, NULL); + dumpres = lua503_dump( L, dummy_writer, NULL, 1); lua_pop( L, mustpush); if( dumpres == 666) { @@ -1192,7 +1192,7 @@ static void inter_copy_func( struct s_Universe* U, lua_State* L2, uint_t L2_cach // "value returned is the error code returned by the last call // to the writer" (and we only return 0) // not sure this could ever fail but for memory shortage reasons - if( lua_dump( L, buf_writer, &b) != 0) + if( lua503_dump( L, buf_writer, &b, 1) != 0) { luaL_error( L, "internal error: function dump failed."); } @@ -1400,9 +1400,8 @@ static bool_t inter_copy_one_( struct s_Universe* U, lua_State* L2, uint_t L2_ca case LUA_TNUMBER: /* LNUM patch support (keeping integer accuracy) */ - // TODO: support for integer in Lua 5.3 -#ifdef LUA_LNUM - if( lua_isinteger(L,i)) +#if defined LUA_LNUM || LUA_VERSION_NUM >= 503 + if( lua_isinteger( L, i)) { lua_Integer v = lua_tointeger( L, i); DEBUGSPEW_CODE( if( vt == VT_KEY) fprintf( stderr, INDENT_BEGIN "KEY: %d\n" INDENT_END, v)); diff --git a/src/tools.h b/src/tools.h index b868440..a4bcf43 100644 --- a/src/tools.h +++ b/src/tools.h @@ -132,7 +132,7 @@ extern void* const UNIVERSE_REGKEY; #endif #define ASSERT_L(c) _ASSERT_L(L,c) -#define STACK_GROW(L,n) do { if (!lua_checkstack(L,n)) luaL_error( L, "Cannot grow stack!" ); } while( 0) +#define STACK_GROW( L, n) do { if (!lua_checkstack(L,(int)(n))) luaL_error( L, "Cannot grow stack!" ); } while( 0) #define LUAG_FUNC( func_name ) static int LG_##func_name( lua_State* L) -- cgit v1.2.3-55-g6feb