Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/external/bsd/lutok/dist Import lutok-0.3.
details: https://anonhg.NetBSD.org/src/rev/8cb775bbffa9
branches: trunk
changeset: 790648:8cb775bbffa9
user: jmmv <jmmv%NetBSD.org@localhost>
date: Fri Oct 18 23:35:23 2013 +0000
description:
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.
diffstat:
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(-)
diffs (truncated from 350 to 300 lines):
diff -r f7533bf7bc78 -r 8cb775bbffa9 external/bsd/lutok/dist/NEWS
--- a/external/bsd/lutok/dist/NEWS Fri Oct 18 22:49:40 2013 +0000
+++ b/external/bsd/lutok/dist/NEWS Fri Oct 18 23:35:23 2013 +0000
@@ -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 -r f7533bf7bc78 -r 8cb775bbffa9 external/bsd/lutok/dist/c_gate_test.cpp
--- a/external/bsd/lutok/dist/c_gate_test.cpp Fri Oct 18 22:49:40 2013 +0000
+++ b/external/bsd/lutok/dist/c_gate_test.cpp Fri Oct 18 23:35:23 2013 +0000
@@ -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 -r f7533bf7bc78 -r 8cb775bbffa9 external/bsd/lutok/dist/debug.hpp
--- a/external/bsd/lutok/dist/debug.hpp Fri Oct 18 22:49:40 2013 +0000
+++ b/external/bsd/lutok/dist/debug.hpp Fri Oct 18 23:35:23 2013 +0000
@@ -33,8 +33,11 @@
#define LUTOK_DEBUG_HPP
#include <string>
+#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L
+#include <memory>
+#else
#include <tr1/memory>
-
+#endif
namespace lutok {
@@ -56,7 +59,11 @@
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 -r f7533bf7bc78 -r 8cb775bbffa9 external/bsd/lutok/dist/state.cpp
--- a/external/bsd/lutok/dist/state.cpp Fri Oct 18 22:49:40 2013 +0000
+++ b/external/bsd/lutok/dist/state.cpp Fri Oct 18 23:35:23 2013 +0000
@@ -212,7 +212,7 @@
} // 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 @@
/// 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 @@
}
+/// 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 @@
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 @@
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 -r f7533bf7bc78 -r 8cb775bbffa9 external/bsd/lutok/dist/state.hpp
--- a/external/bsd/lutok/dist/state.hpp Fri Oct 18 22:49:40 2013 +0000
+++ b/external/bsd/lutok/dist/state.hpp Fri Oct 18 23:35:23 2013 +0000
@@ -33,7 +33,12 @@
#define LUTOK_STATE_HPP
#include <string>
+
+#if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L
+#include <memory>
+#else
#include <tr1/memory>
+#endif
namespace lutok {
@@ -50,8 +55,8 @@
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 @@
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 @@
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 -r f7533bf7bc78 -r 8cb775bbffa9 external/bsd/lutok/dist/state_test.cpp
--- a/external/bsd/lutok/dist/state_test.cpp Fri Oct 18 22:49:40 2013 +0000
+++ b/external/bsd/lutok/dist/state_test.cpp Fri Oct 18 23:35:23 2013 +0000
@@ -207,16 +207,6 @@
}
-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_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_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 @@
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_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)
{
Home |
Main Index |
Thread Index |
Old Index