Skip to content

Commit dd4a169

Browse files
Peter ColbergOberon00
authored andcommitted
Allow registration of classes and functions using an object
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
1 parent afa5eb6 commit dd4a169

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

luabind/scope.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <luabind/prefix.hpp>
2727
#include <luabind/config.hpp>
28+
#include <luabind/object.hpp>
2829
#include <luabind/lua_state_fwd.hpp>
2930
#include <memory>
3031

@@ -84,14 +85,20 @@ namespace luabind {
8485
class LUABIND_API module_
8586
{
8687
public:
87-
module_(lua_State* L_, char const* name);
88+
module_(object const& table);
89+
module_(lua_State* L, char const* name);
8890
void operator[](scope s);
8991

9092
private:
91-
lua_State* m_state;
92-
char const* m_name;
93+
object m_table;
9394
};
9495

96+
inline module_ module(object const& table)
97+
{
98+
return module_(table);
99+
}
100+
101+
95102
inline module_ module(lua_State* L, char const* name = 0)
96103
{
97104
return module_(L, name);

src/scope.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,37 +130,43 @@ namespace luabind {
130130

131131
} // namespace unnamed
132132

133-
module_::module_(lua_State* L, char const* name = 0)
134-
: m_state(L)
135-
, m_name(name)
133+
module_::module_(object const& table)
134+
: m_table(table)
136135
{
137136
}
138137

139-
void module_::operator[](scope s)
138+
module_::module_(lua_State* L, char const* name)
140139
{
141-
if (m_name)
140+
if (name)
142141
{
143-
lua_getglobal(m_state, m_name);
142+
lua_getglobal(L, name);
144143

145-
if (!lua_istable(m_state, -1))
144+
if (!lua_istable(L, -1))
146145
{
147-
lua_pop(m_state, 1);
146+
lua_pop(L, 1);
148147

149-
lua_newtable(m_state);
150-
lua_pushvalue(m_state, -1);
151-
lua_setglobal(m_state, m_name);
148+
lua_newtable(L);
149+
lua_pushvalue(L, -1);
150+
lua_setglobal(L, name);
152151
}
153152
}
154153
else
155154
{
156-
lua_pushglobaltable(m_state);
155+
lua_pushglobaltable(L);
157156
}
158157

159-
lua_pop_stack guard(m_state);
160-
161-
s.register_(m_state);
158+
m_table = object(from_stack(L, -1));
159+
lua_pop(L, 1);
162160
}
163161

162+
void module_::operator[](scope s)
163+
{
164+
lua_State* L = m_table.interpreter();
165+
m_table.push(L);
166+
lua_pop_stack guard(L);
167+
s.register_(L);
168+
}
169+
164170
struct namespace_::registration_ : detail::registration
165171
{
166172
registration_(char const* name)

test/test_scope.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ void test_main(lua_State* L)
100100
]
101101
];
102102

103+
object test = newtable(L);
104+
105+
module(test)
106+
[
107+
namespace_("inner")
108+
[
109+
def("h", &h)
110+
]
111+
];
103112

104113
DOSTRING(L, "assert(test.f() == 1)");
105114
DOSTRING(L, "assert(test.f(3) == 2)");
@@ -115,4 +124,8 @@ void test_main(lua_State* L)
115124
DOSTRING(L, "assert(test.inner.g(7) == 5)");
116125
DOSTRING(L, "assert(test.inner.f(4) == 3)");
117126
DOSTRING(L, "assert(test.inner.h() == 6)");
127+
128+
globals(L)["test_object"] = test;
129+
130+
DOSTRING(L, "assert(test_object.inner.h() == 6)");
118131
}

0 commit comments

Comments
 (0)