File tree Expand file tree Collapse file tree 3 files changed +44
-18
lines changed Expand file tree Collapse file tree 3 files changed +44
-18
lines changed Original file line number Diff line number Diff line change 25
25
26
26
#include < luabind/prefix.hpp>
27
27
#include < luabind/config.hpp>
28
+ #include < luabind/object.hpp>
28
29
#include < luabind/lua_state_fwd.hpp>
29
30
#include < memory>
30
31
@@ -84,14 +85,20 @@ namespace luabind {
84
85
class LUABIND_API module_
85
86
{
86
87
public:
87
- module_ (lua_State* L_, char const * name);
88
+ module_ (object const & table);
89
+ module_ (lua_State* L, char const * name);
88
90
void operator [](scope s);
89
91
90
92
private:
91
- lua_State* m_state;
92
- char const * m_name;
93
+ object m_table;
93
94
};
94
95
96
+ inline module_ module (object const & table)
97
+ {
98
+ return module_ (table);
99
+ }
100
+
101
+
95
102
inline module_ module (lua_State* L, char const * name = 0 )
96
103
{
97
104
return module_ (L, name);
Original file line number Diff line number Diff line change @@ -130,37 +130,43 @@ namespace luabind {
130
130
131
131
} // namespace unnamed
132
132
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)
136
135
{
137
136
}
138
137
139
- void module_::operator [](scope s )
138
+ module_::module_ (lua_State* L, char const * name )
140
139
{
141
- if (m_name )
140
+ if (name )
142
141
{
143
- lua_getglobal (m_state, m_name );
142
+ lua_getglobal (L, name );
144
143
145
- if (!lua_istable (m_state , -1 ))
144
+ if (!lua_istable (L , -1 ))
146
145
{
147
- lua_pop (m_state , 1 );
146
+ lua_pop (L , 1 );
148
147
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 );
152
151
}
153
152
}
154
153
else
155
154
{
156
- lua_pushglobaltable (m_state );
155
+ lua_pushglobaltable (L );
157
156
}
158
157
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 );
162
160
}
163
161
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
+
164
170
struct namespace_ ::registration_ : detail::registration
165
171
{
166
172
registration_ (char const * name)
Original file line number Diff line number Diff line change @@ -100,6 +100,15 @@ void test_main(lua_State* L)
100
100
]
101
101
];
102
102
103
+ object test = newtable (L);
104
+
105
+ module (test)
106
+ [
107
+ namespace_ (" inner" )
108
+ [
109
+ def (" h" , &h)
110
+ ]
111
+ ];
103
112
104
113
DOSTRING (L, " assert(test.f() == 1)" );
105
114
DOSTRING (L, " assert(test.f(3) == 2)" );
@@ -115,4 +124,8 @@ void test_main(lua_State* L)
115
124
DOSTRING (L, " assert(test.inner.g(7) == 5)" );
116
125
DOSTRING (L, " assert(test.inner.f(4) == 3)" );
117
126
DOSTRING (L, " assert(test.inner.h() == 6)" );
127
+
128
+ globals (L)[" test_object" ] = test;
129
+
130
+ DOSTRING (L, " assert(test_object.inner.h() == 6)" );
118
131
}
You can’t perform that action at this time.
0 commit comments