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
27 #include <luabind/lua_include.hpp>
29 #include <luabind/config.hpp>
30 #include <luabind/weak_ref.hpp>
40 } // namespace unnamed
42 LUABIND_API
void get_weak_table(lua_State
* L
)
44 lua_pushlightuserdata(L
, &weak_table_tag
);
45 lua_rawget(L
, LUA_REGISTRYINDEX
);
53 lua_pushliteral(L
, "__mode");
54 lua_pushliteral(L
, "v");
57 lua_setmetatable(L
, -2);
59 lua_pushlightuserdata(L
, &weak_table_tag
);
61 lua_rawset(L
, LUA_REGISTRYINDEX
);
65 } // namespace luabind
72 impl(lua_State
* main
, lua_State
* s
, int index
)
78 lua_pushvalue(s
, index
);
79 ref
= luaL_ref(s
, -2);
85 get_weak_table(state
);
86 luaL_unref(state
, -1, ref
);
100 weak_ref::weak_ref(lua_State
* main
, lua_State
* L
, int index
)
101 : m_impl(new impl(main
, L
, index
))
106 weak_ref::weak_ref(weak_ref
const& other
)
107 : m_impl(other
.m_impl
)
109 if (m_impl
) ++m_impl
->count
;
112 weak_ref::~weak_ref()
114 if (m_impl
&& --m_impl
->count
== 0)
120 weak_ref
& weak_ref::operator=(weak_ref
const& other
)
122 weak_ref(other
).swap(*this);
126 void weak_ref::swap(weak_ref
& other
)
128 std::swap(m_impl
, other
.m_impl
);
131 int weak_ref::id() const
137 // L may not be the same pointer as
138 // was used when creating this reference
139 // since it may be a thread that shares
140 // the same globals table.
141 void weak_ref::get(lua_State
* L
) const
146 lua_rawgeti(L
, -1, m_impl
->ref
);
150 lua_State
* weak_ref::state() const
153 return m_impl
->state
;
156 } // namespace luabind