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 #define LUABIND_BUILDING
25 #include <luabind/lua_include.hpp>
27 #include <luabind/scope.hpp>
28 #include <luabind/detail/debug.hpp>
29 #include <luabind/detail/stack_utils.hpp>
32 namespace luabind
{ namespace detail
{
34 registration::registration()
39 registration::~registration()
51 scope::scope(std::auto_ptr
<detail::registration
> reg
)
52 : m_chain(reg
.release())
56 scope::scope(scope
const& other
)
57 : m_chain(other
.m_chain
)
59 const_cast<scope
&>(other
).m_chain
= 0;
67 scope
& scope::operator,(scope s
)
76 for (detail::registration
* c
= m_chain
;; c
= c
->m_next
)
80 c
->m_next
= s
.m_chain
;
89 void scope::register_(lua_State
* L
) const
91 for (detail::registration
* r
= m_chain
; r
!= 0; r
= r
->m_next
)
93 LUABIND_CHECK_STACK(L
);
98 } // namespace luabind
106 lua_pop_stack(lua_State
* L
)
119 } // namespace unnamed
121 module_::module_(lua_State
* L
, char const* name
= 0)
127 void module_::operator[](scope s
)
131 lua_pushstring(m_state
, m_name
);
132 lua_gettable(m_state
, LUA_GLOBALSINDEX
);
134 if (!lua_istable(m_state
, -1))
138 lua_newtable(m_state
);
139 lua_pushstring(m_state
, m_name
);
140 lua_pushvalue(m_state
, -2);
141 lua_settable(m_state
, LUA_GLOBALSINDEX
);
146 lua_pushvalue(m_state
, LUA_GLOBALSINDEX
);
149 lua_pop_stack
guard(m_state
);
151 s
.register_(m_state
);
154 struct namespace_::registration_
: detail::registration
156 registration_(char const* name
)
161 void register_(lua_State
* L
) const
163 LUABIND_CHECK_STACK(L
);
164 assert(lua_gettop(L
) >= 1);
166 lua_pushstring(L
, m_name
);
169 detail::stack_pop
p(L
, 1); // pops the table on exit
171 if (!lua_istable(L
, -1))
176 lua_pushstring(L
, m_name
);
177 lua_pushvalue(L
, -2);
181 m_scope
.register_(L
);
188 namespace_::namespace_(char const* name
)
189 : scope(std::auto_ptr
<detail::registration
>(
190 m_registration
= new registration_(name
)))
194 namespace_
& namespace_::operator[](scope s
)
196 m_registration
->m_scope
.operator,(s
);
200 } // namespace luabind