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>
28 namespace luabind
{ namespace detail
{
30 registration::registration()
35 registration::~registration()
47 scope::scope(std::auto_ptr
<detail::registration
> reg
)
48 : m_chain(reg
.release())
52 scope::scope(scope
const& other
)
53 : m_chain(other
.m_chain
)
55 const_cast<scope
&>(other
).m_chain
= 0;
63 scope
& scope::operator,(scope s
)
72 for (detail::registration
* c
= m_chain
;; c
= c
->m_next
)
76 c
->m_next
= s
.m_chain
;
85 void scope::register_(lua_State
* L
) const
87 for (detail::registration
* r
= m_chain
; r
!= 0; r
= r
->m_next
)
89 int n
= lua_gettop(L
);
93 assert(n
== lua_gettop(L
));
97 } // namespace luabind
105 lua_pop_stack(lua_State
* L
)
118 } // namespace unnamed
120 module_::module_(lua_State
* L
, char const* name
= 0)
126 void module_::operator[](scope s
)
130 lua_pushstring(m_state
, m_name
);
131 lua_gettable(m_state
, LUA_GLOBALSINDEX
);
133 if (!lua_istable(m_state
, -1))
137 lua_newtable(m_state
);
138 lua_pushstring(m_state
, m_name
);
139 lua_pushvalue(m_state
, -2);
140 lua_settable(m_state
, LUA_GLOBALSINDEX
);
145 lua_pushvalue(m_state
, LUA_GLOBALSINDEX
);
148 lua_pop_stack
guard(m_state
);
150 s
.register_(m_state
);
153 struct namespace_::registration_
: detail::registration
155 registration_(char const* name
)
160 void register_(lua_State
* L
) const
162 int n
= lua_gettop(L
);
166 lua_pushstring(L
, m_name
);
169 if (!lua_istable(L
, -1))
174 lua_pushstring(L
, m_name
);
175 lua_pushvalue(L
, -2);
179 m_scope
.register_(L
);
183 assert(lua_gettop(L
) == n
);
190 namespace_::namespace_(char const* name
)
191 : scope(std::auto_ptr
<detail::registration
>(
192 m_registration
= new registration_(name
)))
196 namespace_
& namespace_::operator[](scope s
)
198 m_registration
->m_scope
.operator,(s
);
202 } // namespace luabind