Use lua_tointeger() instead of casting the result of lua_tonumber().
[luabind.git] / src / scope.cpp
blob0050a91e9b58c5987eeea23b17752f97046622c0
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>
30 #include <cassert>
32 namespace luabind { namespace detail {
34 registration::registration()
35 : m_next(0)
39 registration::~registration()
41 delete m_next;
44 } // namespace detail
46 scope::scope()
47 : m_chain(0)
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;
62 scope::~scope()
64 delete m_chain;
67 scope& scope::operator,(scope s)
69 if (!m_chain)
71 m_chain = s.m_chain;
72 s.m_chain = 0;
73 return *this;
76 for (detail::registration* c = m_chain;; c = c->m_next)
78 if (!c->m_next)
80 c->m_next = s.m_chain;
81 s.m_chain = 0;
82 break;
86 return *this;
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);
94 r->register_(L);
98 } // namespace luabind
100 namespace luabind {
102 namespace {
104 struct lua_pop_stack
106 lua_pop_stack(lua_State* L)
107 : m_state(L)
111 ~lua_pop_stack()
113 lua_pop(m_state, 1);
116 lua_State* m_state;
119 } // namespace unnamed
121 module_::module_(lua_State* L, char const* name = 0)
122 : m_state(L)
123 , m_name(name)
127 void module_::operator[](scope s)
129 if (m_name)
131 lua_pushstring(m_state, m_name);
132 lua_gettable(m_state, LUA_GLOBALSINDEX);
134 if (!lua_istable(m_state, -1))
136 lua_pop(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);
144 else
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)
157 : m_name(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);
167 lua_gettable(L, -2);
169 detail::stack_pop p(L, 1); // pops the table on exit
171 if (!lua_istable(L, -1))
173 lua_pop(L, 1);
175 lua_newtable(L);
176 lua_pushstring(L, m_name);
177 lua_pushvalue(L, -2);
178 lua_settable(L, -4);
181 m_scope.register_(L);
184 char const* m_name;
185 scope m_scope;
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);
197 return *this;
200 } // namespace luabind