feat(lua/util): remove dependency on luaL_setfuncs

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-11-03 12:16:23 -08:00
parent e2da8c1f4d
commit df7dbe17b5
5 changed files with 42 additions and 3 deletions

View file

@ -1 +1 @@
add_library(lua name.cpp numerics.cpp) add_library(lua util.cpp name.cpp numerics.cpp)

View file

@ -8,6 +8,7 @@ Author: Leonardo de Moura
#include <lua.hpp> #include <lua.hpp>
#include "util/debug.h" #include "util/debug.h"
#include "util/name.h" #include "util/name.h"
#include "bindings/lua/util.h"
namespace lean { namespace lean {
static int name_gc(lua_State * L); static int name_gc(lua_State * L);
@ -81,7 +82,7 @@ static int name_lt(lua_State * L) {
void init_name(lua_State * L) { void init_name(lua_State * L) {
luaL_newmetatable(L, "name.mt"); luaL_newmetatable(L, "name.mt");
luaL_setfuncs(L, name_m, 0); setfuncs(L, name_m, 0);
lua_pushcfunction(L, mk_name); lua_pushcfunction(L, mk_name);
lua_setglobal(L, "name"); lua_setglobal(L, "name");

View file

@ -10,6 +10,7 @@ Author: Leonardo de Moura
#include "util/debug.h" #include "util/debug.h"
#include "util/numerics/mpz.h" #include "util/numerics/mpz.h"
#include "util/numerics/mpq.h" #include "util/numerics/mpq.h"
#include "bindings/lua/util.h"
namespace lean { namespace lean {
template<typename T, char const * N, char const * M> template<typename T, char const * N, char const * M>
@ -99,7 +100,7 @@ public:
static void init(lua_State * L) { static void init(lua_State * L) {
luaL_newmetatable(L, M); luaL_newmetatable(L, M);
luaL_setfuncs(L, m, 0); setfuncs(L, m, 0);
lua_pushcfunction(L, mk); lua_pushcfunction(L, mk);
lua_setglobal(L, N); lua_setglobal(L, N);

25
src/bindings/lua/util.cpp Normal file
View file

@ -0,0 +1,25 @@
/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#ifdef LEAN_USE_LUA
#include <lua.hpp>
namespace lean {
/**
\brief luaL_setfuncs replacement. The function luaL_setfuncs is only available in Lua 5.2.
*/
void setfuncs(lua_State * L, luaL_Reg const * l, int nup) {
luaL_checkstack(L, nup, "too many upvalues");
for (; l->name != NULL; l++) {
// fill the table with given functions
for (int i = 0; i < nup; i++) // copy upvalues to the top
lua_pushvalue(L, -nup);
lua_pushcclosure(L, l->func, nup); // closure with those upvalues
lua_setfield(L, -(nup + 2), l->name);
}
lua_pop(L, nup); // remove upvalues
}
}
#endif

12
src/bindings/lua/util.h Normal file
View file

@ -0,0 +1,12 @@
/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#ifdef LEAN_USE_LUA
#include <lua.hpp>
namespace lean {
void setfuncs(lua_State * L, luaL_Reg const * l, int nup);
}
#endif