1 // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <luabind/lua_include.hpp>
25 #include <luabind/scope.hpp>
26 #include <luabind/detail/debug.hpp>
27 #include <luabind/detail/stack_utils.hpp>
30 namespace luabind
{ namespace detail
{
32 registration::registration()
37 registration::~registration()
49 scope::scope(std::auto_ptr
<detail::registration
> reg
)
50 : m_chain(reg
.release())
54 scope::scope(scope
const& other
)
55 : m_chain(other
.m_chain
)
57 const_cast<scope
&>(other
).m_chain
= 0;
65 scope
& scope::operator,(scope s
)
74 for (detail::registration
* c
= m_chain
;; c
= c
->m_next
)
78 c
->m_next
= s
.m_chain
;
87 void scope::register_(lua_State
* L
) const
89 for (detail::registration
* r
= m_chain
; r
!= 0; r
= r
->m_next
)
91 LUABIND_CHECK_STACK(L
);
96 } // namespace luabind
104 lua_pop_stack(lua_State
* L
)
117 } // namespace unnamed
119 module_::module_(lua_State
* L
, char const* name
= 0)
125 void module_::operator[](scope s
)
129 lua_pushstring(m_state
, m_name
);
130 lua_gettable(m_state
, LUA_GLOBALSINDEX
);
132 if (!lua_istable(m_state
, -1))
136 lua_newtable(m_state
);
137 lua_pushstring(m_state
, m_name
);
138 lua_pushvalue(m_state
, -2);
139 lua_settable(m_state
, LUA_GLOBALSINDEX
);
144 lua_pushvalue(m_state
, LUA_GLOBALSINDEX
);
147 lua_pop_stack
guard(m_state
);
149 s
.register_(m_state
);
152 struct namespace_::registration_
: detail::registration
154 registration_(char const* name
)
159 void register_(lua_State
* L
) const
161 LUABIND_CHECK_STACK(L
);
162 assert(lua_gettop(L
) >= 1);
164 lua_pushstring(L
, m_name
);
167 detail::stack_pop
p(L
, 1); // pops the table on exit
169 if (!lua_istable(L
, -1))
174 lua_pushstring(L
, m_name
);
175 lua_pushvalue(L
, -2);
179 m_scope
.register_(L
);
186 namespace_::namespace_(char const* name
)
187 : scope(std::auto_ptr
<detail::registration
>(
188 m_registration
= new registration_(name
)))
192 namespace_
& namespace_::operator[](scope s
)
194 m_registration
->m_scope
.operator,(s
);
198 } // namespace luabind