Skip to content

Commit

Permalink
Allow registration of classes and functions using an object
Browse files Browse the repository at this point in the history
This allows use of module() without pollution of the global environment, e.g.

local mylib = require("mylib")

extern "C" int luaopen_mylib(lua_State* L)
{
    using namespace luaponte;

    object mylib = newtable(L);

    module(mylib)
    [
        …
    ];

    mylib.push(L);

    return 1;
}

Conflicts:
	luaponte/scope.hpp
	src/scope.cpp
  • Loading branch information
Peter Colberg authored and Oberon00 committed Jul 4, 2013
1 parent afa5eb6 commit dd4a169
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
13 changes: 10 additions & 3 deletions luabind/scope.hpp
Expand Up @@ -25,6 +25,7 @@

#include <luabind/prefix.hpp>
#include <luabind/config.hpp>
#include <luabind/object.hpp>
#include <luabind/lua_state_fwd.hpp>
#include <memory>

Expand Down Expand Up @@ -84,14 +85,20 @@ namespace luabind {
class LUABIND_API module_
{
public:
module_(lua_State* L_, char const* name);
module_(object const& table);
module_(lua_State* L, char const* name);
void operator[](scope s);

private:
lua_State* m_state;
char const* m_name;
object m_table;
};

inline module_ module(object const& table)
{
return module_(table);
}


inline module_ module(lua_State* L, char const* name = 0)
{
return module_(L, name);
Expand Down
36 changes: 21 additions & 15 deletions src/scope.cpp
Expand Up @@ -130,37 +130,43 @@ namespace luabind {

} // namespace unnamed

module_::module_(lua_State* L, char const* name = 0)
: m_state(L)
, m_name(name)
module_::module_(object const& table)
: m_table(table)
{
}

void module_::operator[](scope s)
module_::module_(lua_State* L, char const* name)
{
if (m_name)
if (name)
{
lua_getglobal(m_state, m_name);
lua_getglobal(L, name);

if (!lua_istable(m_state, -1))
if (!lua_istable(L, -1))
{
lua_pop(m_state, 1);
lua_pop(L, 1);

lua_newtable(m_state);
lua_pushvalue(m_state, -1);
lua_setglobal(m_state, m_name);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, name);
}
}
else
{
lua_pushglobaltable(m_state);
lua_pushglobaltable(L);
}

lua_pop_stack guard(m_state);

s.register_(m_state);
m_table = object(from_stack(L, -1));
lua_pop(L, 1);
}

void module_::operator[](scope s)
{
lua_State* L = m_table.interpreter();
m_table.push(L);
lua_pop_stack guard(L);
s.register_(L);
}

struct namespace_::registration_ : detail::registration
{
registration_(char const* name)
Expand Down
13 changes: 13 additions & 0 deletions test/test_scope.cpp
Expand Up @@ -100,6 +100,15 @@ void test_main(lua_State* L)
]
];

object test = newtable(L);

module(test)
[
namespace_("inner")
[
def("h", &h)
]
];

DOSTRING(L, "assert(test.f() == 1)");
DOSTRING(L, "assert(test.f(3) == 2)");
Expand All @@ -115,4 +124,8 @@ void test_main(lua_State* L)
DOSTRING(L, "assert(test.inner.g(7) == 5)");
DOSTRING(L, "assert(test.inner.f(4) == 3)");
DOSTRING(L, "assert(test.inner.h() == 6)");

globals(L)["test_object"] = test;

DOSTRING(L, "assert(test_object.inner.h() == 6)");
}

0 comments on commit dd4a169

Please sign in to comment.