From 9b0eec67b2e263eb41d1beb36e3329a2a9419668 Mon Sep 17 00:00:00 2001 From: jmmv Date: Fri, 18 Oct 2013 23:35:23 +0000 Subject: [PATCH] Import lutok-0.3. The main reason for this update is the addition of support for Lua 5.2, which we will want to import sometime. Released on 2013/06/14. * Issue 1: Added support for Lua 5.2 while maintaining support for Lua 5.1. Applications using Lutok can be modified to use the new interface in this new version and thus support both Lua releases. However, because of incompatible changes to the Lua API, this release of Lutok is incompatible with previous releases as well. * Issue 3: Tweaked configure to look for Lua using the pkg-config names lua-5.2 and lua-5.1. These are the names used by FreeBSD. Interface changes: * New global constants: registry_index. * New methods added to the state class: get_global_table. * Removed global constants: globals_index. --- external/bsd/lutok/dist/NEWS | 23 +++++++ external/bsd/lutok/dist/c_gate_test.cpp | 2 +- external/bsd/lutok/dist/debug.hpp | 9 ++- external/bsd/lutok/dist/state.cpp | 34 +++++++++- external/bsd/lutok/dist/state.hpp | 14 ++++- external/bsd/lutok/dist/state_test.cpp | 84 ++++++++++++------------- 6 files changed, 116 insertions(+), 50 deletions(-) diff --git a/external/bsd/lutok/dist/NEWS b/external/bsd/lutok/dist/NEWS index d1920e5cf675..f748129bdb7d 100644 --- a/external/bsd/lutok/dist/NEWS +++ b/external/bsd/lutok/dist/NEWS @@ -1,3 +1,26 @@ +Changes in version 0.3 +====================== + +Released on 2013/06/14. + +* Issue 1: Added support for Lua 5.2 while maintaining support for Lua + 5.1. Applications using Lutok can be modified to use the new + interface in this new version and thus support both Lua releases. + However, because of incompatible changes to the Lua API, this release + of Lutok is incompatible with previous releases as well. + +* Issue 3: Tweaked configure to look for Lua using the pkg-config names + lua-5.2 and lua-5.1. These are the names used by FreeBSD. + +Interface changes: + +* New global constants: registry_index. + +* New methods added to the state class: get_global_table. + +* Removed global constants: globals_index. + + Changes in version 0.2 ====================== diff --git a/external/bsd/lutok/dist/c_gate_test.cpp b/external/bsd/lutok/dist/c_gate_test.cpp index 59115534be8f..33e3d10da457 100644 --- a/external/bsd/lutok/dist/c_gate_test.cpp +++ b/external/bsd/lutok/dist/c_gate_test.cpp @@ -38,7 +38,7 @@ ATF_TEST_CASE_WITHOUT_HEAD(connect); ATF_TEST_CASE_BODY(connect) { - lua_State* raw_state = lua_open(); + lua_State* raw_state = luaL_newstate(); ATF_REQUIRE(raw_state != NULL); { diff --git a/external/bsd/lutok/dist/debug.hpp b/external/bsd/lutok/dist/debug.hpp index d566032c5a00..6fc074d95d41 100644 --- a/external/bsd/lutok/dist/debug.hpp +++ b/external/bsd/lutok/dist/debug.hpp @@ -33,8 +33,11 @@ #define LUTOK_DEBUG_HPP #include +#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L +#include +#else #include - +#endif namespace lutok { @@ -56,7 +59,11 @@ class debug { struct impl; /// Pointer to the shared internal implementation. +#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L + std::shared_ptr< impl > _pimpl; +#else std::tr1::shared_ptr< impl > _pimpl; +#endif public: debug(void); diff --git a/external/bsd/lutok/dist/state.cpp b/external/bsd/lutok/dist/state.cpp index a7d0952087e8..56281db62bd6 100644 --- a/external/bsd/lutok/dist/state.cpp +++ b/external/bsd/lutok/dist/state.cpp @@ -212,7 +212,7 @@ cxx_function_trampoline(lua_State* raw_state) } // anonymous namespace -const int lutok::globals_index = LUA_GLOBALSINDEX; +const int lutok::registry_index = LUA_REGISTRYINDEX; /// Internal implementation for lutok::state. @@ -241,7 +241,7 @@ struct lutok::state::impl { /// session. As soon as the object is destroyed, the session is terminated. lutok::state::state(void) { - lua_State* lua = lua_open(); + lua_State* lua = luaL_newstate(); if (lua == NULL) throw lutok::error("lua open failed"); _pimpl.reset(new impl(lua, true)); @@ -308,6 +308,26 @@ lutok::state::get_global(const std::string& name) } +/// Pushes a reference to the global table onto the stack. +/// +/// This is a wrapper around the incompatible differences between Lua 5.1 and +/// 5.2 to access to the globals table. +/// +/// \post state(-1) Contains the reference to the globals table. +void +lutok::state::get_global_table(void) +{ +#if LUA_VERSION_NUM >= 502 + lua_pushvalue(_pimpl->lua_state, registry_index); + lua_pushinteger(_pimpl->lua_state, LUA_RIDX_GLOBALS); + lua_gettable(_pimpl->lua_state, -2); + lua_remove(_pimpl->lua_state, -2); +#else + lua_pushvalue(_pimpl->lua_state, LUA_GLOBALSINDEX); +#endif +} + + /// Wrapper around luaL_getmetafield. /// /// \param index The second parameter to luaL_getmetafield. @@ -570,9 +590,14 @@ lutok::state::open_base(void) void lutok::state::open_string(void) { +#if LUA_VERSION_NUM >= 502 + luaL_requiref(_pimpl->lua_state, LUA_STRLIBNAME, luaopen_string, 1); + lua_pop(_pimpl->lua_state, 1); +#else lua_pushcfunction(_pimpl->lua_state, luaopen_string); if (lua_pcall(_pimpl->lua_state, 0, 0, 0) != 0) throw lutok::api_error::from_stack(*this, "luaopen_string"); +#endif } @@ -584,9 +609,14 @@ lutok::state::open_string(void) void lutok::state::open_table(void) { +#if LUA_VERSION_NUM >= 502 + luaL_requiref(_pimpl->lua_state, LUA_TABLIBNAME, luaopen_table, 1); + lua_pop(_pimpl->lua_state, 1); +#else lua_pushcfunction(_pimpl->lua_state, luaopen_table); if (lua_pcall(_pimpl->lua_state, 0, 0, 0) != 0) throw lutok::api_error::from_stack(*this, "luaopen_table"); +#endif } diff --git a/external/bsd/lutok/dist/state.hpp b/external/bsd/lutok/dist/state.hpp index 9dd610ae96da..9f883908ea5f 100644 --- a/external/bsd/lutok/dist/state.hpp +++ b/external/bsd/lutok/dist/state.hpp @@ -33,7 +33,12 @@ #define LUTOK_STATE_HPP #include + +#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L +#include +#else #include +#endif namespace lutok { @@ -50,8 +55,8 @@ class state; typedef int (*cxx_function)(state&); -/// Stack index constant pointing to the globals table (_G). -extern const int globals_index; +/// Stack index constant pointing to the registry table. +extern const int registry_index; /// A RAII model for the Lua state. @@ -72,7 +77,11 @@ class state { struct impl; /// Pointer to the shared internal implementation. +#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L + std::shared_ptr< impl > _pimpl; +#else std::tr1::shared_ptr< impl > _pimpl; +#endif void* new_userdata_voidp(const size_t); void* to_userdata_voidp(const int); @@ -87,6 +96,7 @@ public: void close(void); void get_global(const std::string&); + void get_global_table(void); bool get_metafield(const int, const std::string&); bool get_metatable(const int = -1); void get_table(const int = -2); diff --git a/external/bsd/lutok/dist/state_test.cpp b/external/bsd/lutok/dist/state_test.cpp index 269de81f3393..dc75ceef4006 100644 --- a/external/bsd/lutok/dist/state_test.cpp +++ b/external/bsd/lutok/dist/state_test.cpp @@ -207,16 +207,6 @@ ATF_TEST_CASE_BODY(get_global__ok) } -ATF_TEST_CASE_WITHOUT_HEAD(get_global__fail); -ATF_TEST_CASE_BODY(get_global__fail) -{ - lutok::state state; - lua_pushinteger(raw(state), 3); - lua_replace(raw(state), LUA_GLOBALSINDEX); - REQUIRE_API_ERROR("lua_getglobal", state.get_global("test_variable")); -} - - ATF_TEST_CASE_WITHOUT_HEAD(get_global__undefined); ATF_TEST_CASE_BODY(get_global__undefined) { @@ -227,6 +217,20 @@ ATF_TEST_CASE_BODY(get_global__undefined) } +ATF_TEST_CASE_WITHOUT_HEAD(get_global_table); +ATF_TEST_CASE_BODY(get_global_table) +{ + lutok::state state; + ATF_REQUIRE(luaL_dostring(raw(state), "global_variable = 'hello'") == 0); + state.get_global_table(); + lua_pushstring(raw(state), "global_variable"); + lua_gettable(raw(state), -2); + ATF_REQUIRE(lua_isstring(raw(state), -1)); + ATF_REQUIRE(std::strcmp("hello", lua_tostring(raw(state), -1)) == 0); + lua_pop(raw(state), 2); +} + + ATF_TEST_CASE_WITHOUT_HEAD(get_metafield__ok); ATF_TEST_CASE_BODY(get_metafield__ok) { @@ -356,20 +360,6 @@ ATF_TEST_CASE_BODY(get_top) } -ATF_TEST_CASE_WITHOUT_HEAD(globals_index); -ATF_TEST_CASE_BODY(globals_index) -{ - lutok::state state; - ATF_REQUIRE(luaL_dostring(raw(state), "global_variable = 'hello'") == 0); - lua_pushvalue(raw(state), lutok::globals_index); - lua_pushstring(raw(state), "global_variable"); - lua_gettable(raw(state), -2); - ATF_REQUIRE(lua_isstring(raw(state), -1)); - ATF_REQUIRE(std::strcmp("hello", lua_tostring(raw(state), -1)) == 0); - lua_pop(raw(state), 2); -} - - ATF_TEST_CASE_WITHOUT_HEAD(insert); ATF_TEST_CASE_BODY(insert) { @@ -793,12 +783,14 @@ ATF_TEST_CASE_BODY(pcall__ok) lutok::state state; luaL_loadstring(raw(state), "function mul(a, b) return a * b; end"); state.pcall(0, 0, 0); - lua_getfield(raw(state), LUA_GLOBALSINDEX, "mul"); + state.get_global_table(); + lua_pushstring(raw(state), "mul"); + lua_gettable(raw(state), -2); lua_pushinteger(raw(state), 3); lua_pushinteger(raw(state), 5); state.pcall(2, 1, 0); ATF_REQUIRE_EQ(15, lua_tointeger(raw(state), -1)); - lua_pop(raw(state), 1); + lua_pop(raw(state), 2); } @@ -1084,8 +1076,25 @@ ATF_TEST_CASE_BODY(raw_set__explicit) } -ATF_TEST_CASE_WITHOUT_HEAD(set_global__ok); -ATF_TEST_CASE_BODY(set_global__ok) +ATF_TEST_CASE_WITHOUT_HEAD(registry_index); +ATF_TEST_CASE_BODY(registry_index) +{ + lutok::state state; + lua_pushvalue(raw(state), lutok::registry_index); + lua_pushstring(raw(state), "custom_variable"); + lua_pushstring(raw(state), "custom value"); + lua_settable(raw(state), -3); + lua_pop(raw(state), 1); + ATF_REQUIRE(luaL_dostring(raw(state), + "return custom_variable == nil") == 0); + ATF_REQUIRE(lua_isboolean(raw(state), -1)); + ATF_REQUIRE(lua_toboolean(raw(state), -1)); + lua_pop(raw(state), 1); +} + + +ATF_TEST_CASE_WITHOUT_HEAD(set_global); +ATF_TEST_CASE_BODY(set_global) { lutok::state state; lua_pushinteger(raw(state), 3); @@ -1097,18 +1106,6 @@ ATF_TEST_CASE_BODY(set_global__ok) } -ATF_TEST_CASE_WITHOUT_HEAD(set_global__fail); -ATF_TEST_CASE_BODY(set_global__fail) -{ - lutok::state state; - lua_pushinteger(raw(state), 3); - lua_replace(raw(state), LUA_GLOBALSINDEX); - lua_pushinteger(raw(state), 4); - REQUIRE_API_ERROR("lua_setglobal", state.set_global("test_variable")); - lua_pop(raw(state), 1); -} - - ATF_TEST_CASE_WITHOUT_HEAD(set_metatable__top); ATF_TEST_CASE_BODY(set_metatable__top) { @@ -1321,8 +1318,8 @@ ATF_INIT_TEST_CASES(tcs) { ATF_ADD_TEST_CASE(tcs, close); ATF_ADD_TEST_CASE(tcs, get_global__ok); - ATF_ADD_TEST_CASE(tcs, get_global__fail); ATF_ADD_TEST_CASE(tcs, get_global__undefined); + ATF_ADD_TEST_CASE(tcs, get_global_table); ATF_ADD_TEST_CASE(tcs, get_metafield__ok); ATF_ADD_TEST_CASE(tcs, get_metafield__undefined); ATF_ADD_TEST_CASE(tcs, get_metatable__top); @@ -1332,7 +1329,6 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, get_table__nil); ATF_ADD_TEST_CASE(tcs, get_table__unknown_index); ATF_ADD_TEST_CASE(tcs, get_top); - ATF_ADD_TEST_CASE(tcs, globals_index); ATF_ADD_TEST_CASE(tcs, insert); ATF_ADD_TEST_CASE(tcs, is_boolean__empty); ATF_ADD_TEST_CASE(tcs, is_boolean__top); @@ -1386,8 +1382,8 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, raw_get__explicit); ATF_ADD_TEST_CASE(tcs, raw_set__top); ATF_ADD_TEST_CASE(tcs, raw_set__explicit); - ATF_ADD_TEST_CASE(tcs, set_global__ok); - ATF_ADD_TEST_CASE(tcs, set_global__fail); + ATF_ADD_TEST_CASE(tcs, registry_index); + ATF_ADD_TEST_CASE(tcs, set_global); ATF_ADD_TEST_CASE(tcs, set_metatable__top); ATF_ADD_TEST_CASE(tcs, set_metatable__explicit); ATF_ADD_TEST_CASE(tcs, set_table__ok);