1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include <cassert>
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <lua.hpp>
32*11be35a1SLionel Sambuc
33*11be35a1SLionel Sambuc #include <lutok/c_gate.hpp>
34*11be35a1SLionel Sambuc #include <lutok/debug.hpp>
35*11be35a1SLionel Sambuc #include <lutok/exceptions.hpp>
36*11be35a1SLionel Sambuc #include <lutok/state.ipp>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuc /// Internal implementation for lutok::debug.
40*11be35a1SLionel Sambuc struct lutok::debug::impl {
41*11be35a1SLionel Sambuc /// The Lua internal debug state.
42*11be35a1SLionel Sambuc lua_Debug lua_debug;
43*11be35a1SLionel Sambuc };
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc /// Constructor for an empty debug structure.
debug(void)47*11be35a1SLionel Sambuc lutok::debug::debug(void) :
48*11be35a1SLionel Sambuc _pimpl(new impl())
49*11be35a1SLionel Sambuc {
50*11be35a1SLionel Sambuc }
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc /// Destructor.
~debug(void)54*11be35a1SLionel Sambuc lutok::debug::~debug(void)
55*11be35a1SLionel Sambuc {
56*11be35a1SLionel Sambuc }
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc
59*11be35a1SLionel Sambuc /// Wrapper around lua_getinfo.
60*11be35a1SLionel Sambuc ///
61*11be35a1SLionel Sambuc /// \param s The Lua state.
62*11be35a1SLionel Sambuc /// \param what_ The second parameter to lua_getinfo.
63*11be35a1SLionel Sambuc ///
64*11be35a1SLionel Sambuc /// \warning Terminates execution if there is not enough memory to manipulate
65*11be35a1SLionel Sambuc /// the Lua stack.
66*11be35a1SLionel Sambuc void
get_info(state & s,const std::string & what_)67*11be35a1SLionel Sambuc lutok::debug::get_info(state& s, const std::string& what_)
68*11be35a1SLionel Sambuc {
69*11be35a1SLionel Sambuc lua_State* raw_state = state_c_gate(s).c_state();
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc if (lua_getinfo(raw_state, what_.c_str(), &_pimpl->lua_debug) == 0)
72*11be35a1SLionel Sambuc throw lutok::api_error::from_stack(s, "lua_getinfo");
73*11be35a1SLionel Sambuc }
74*11be35a1SLionel Sambuc
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc /// Wrapper around lua_getstack.
77*11be35a1SLionel Sambuc ///
78*11be35a1SLionel Sambuc /// \param s The Lua state.
79*11be35a1SLionel Sambuc /// \param level The second parameter to lua_getstack.
80*11be35a1SLionel Sambuc void
get_stack(state & s,const int level)81*11be35a1SLionel Sambuc lutok::debug::get_stack(state& s, const int level)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc lua_State* raw_state = state_c_gate(s).c_state();
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc lua_getstack(raw_state, level, &_pimpl->lua_debug);
86*11be35a1SLionel Sambuc }
87*11be35a1SLionel Sambuc
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc /// Accessor for the 'event' field of lua_Debug.
90*11be35a1SLionel Sambuc ///
91*11be35a1SLionel Sambuc /// \return Returns the 'event' field of the internal lua_Debug structure.
92*11be35a1SLionel Sambuc int
event(void) const93*11be35a1SLionel Sambuc lutok::debug::event(void) const
94*11be35a1SLionel Sambuc {
95*11be35a1SLionel Sambuc return _pimpl->lua_debug.event;
96*11be35a1SLionel Sambuc }
97*11be35a1SLionel Sambuc
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc /// Accessor for the 'name' field of lua_Debug.
100*11be35a1SLionel Sambuc ///
101*11be35a1SLionel Sambuc /// \return Returns the 'name' field of the internal lua_Debug structure.
102*11be35a1SLionel Sambuc std::string
name(void) const103*11be35a1SLionel Sambuc lutok::debug::name(void) const
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc assert(_pimpl->lua_debug.name != NULL);
106*11be35a1SLionel Sambuc return _pimpl->lua_debug.name;
107*11be35a1SLionel Sambuc }
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc
110*11be35a1SLionel Sambuc /// Accessor for the 'namewhat' field of lua_Debug.
111*11be35a1SLionel Sambuc ///
112*11be35a1SLionel Sambuc /// \return Returns the 'namewhat' field of the internal lua_Debug structure.
113*11be35a1SLionel Sambuc std::string
name_what(void) const114*11be35a1SLionel Sambuc lutok::debug::name_what(void) const
115*11be35a1SLionel Sambuc {
116*11be35a1SLionel Sambuc assert(_pimpl->lua_debug.namewhat != NULL);
117*11be35a1SLionel Sambuc return _pimpl->lua_debug.namewhat;
118*11be35a1SLionel Sambuc }
119*11be35a1SLionel Sambuc
120*11be35a1SLionel Sambuc
121*11be35a1SLionel Sambuc /// Accessor for the 'what' field of lua_Debug.
122*11be35a1SLionel Sambuc ///
123*11be35a1SLionel Sambuc /// \return Returns the 'what' field of the internal lua_Debug structure.
124*11be35a1SLionel Sambuc std::string
what(void) const125*11be35a1SLionel Sambuc lutok::debug::what(void) const
126*11be35a1SLionel Sambuc {
127*11be35a1SLionel Sambuc assert(_pimpl->lua_debug.what != NULL);
128*11be35a1SLionel Sambuc return _pimpl->lua_debug.what;
129*11be35a1SLionel Sambuc }
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc /// Accessor for the 'source' field of lua_Debug.
133*11be35a1SLionel Sambuc ///
134*11be35a1SLionel Sambuc /// \return Returns the 'source' field of the internal lua_Debug structure.
135*11be35a1SLionel Sambuc std::string
source(void) const136*11be35a1SLionel Sambuc lutok::debug::source(void) const
137*11be35a1SLionel Sambuc {
138*11be35a1SLionel Sambuc assert(_pimpl->lua_debug.source != NULL);
139*11be35a1SLionel Sambuc return _pimpl->lua_debug.source;
140*11be35a1SLionel Sambuc }
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc
143*11be35a1SLionel Sambuc /// Accessor for the 'currentline' field of lua_Debug.
144*11be35a1SLionel Sambuc ///
145*11be35a1SLionel Sambuc /// \return Returns the 'currentline' field of the internal lua_Debug structure.
146*11be35a1SLionel Sambuc int
current_line(void) const147*11be35a1SLionel Sambuc lutok::debug::current_line(void) const
148*11be35a1SLionel Sambuc {
149*11be35a1SLionel Sambuc return _pimpl->lua_debug.currentline;
150*11be35a1SLionel Sambuc }
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc
153*11be35a1SLionel Sambuc /// Accessor for the 'nups' field of lua_Debug.
154*11be35a1SLionel Sambuc ///
155*11be35a1SLionel Sambuc /// \return Returns the 'nups' field of the internal lua_Debug structure.
156*11be35a1SLionel Sambuc int
n_ups(void) const157*11be35a1SLionel Sambuc lutok::debug::n_ups(void) const
158*11be35a1SLionel Sambuc {
159*11be35a1SLionel Sambuc return _pimpl->lua_debug.nups;
160*11be35a1SLionel Sambuc }
161*11be35a1SLionel Sambuc
162*11be35a1SLionel Sambuc
163*11be35a1SLionel Sambuc /// Accessor for the 'linedefined' field of lua_Debug.
164*11be35a1SLionel Sambuc ///
165*11be35a1SLionel Sambuc /// \return Returns the 'linedefined' field of the internal lua_Debug structure.
166*11be35a1SLionel Sambuc int
line_defined(void) const167*11be35a1SLionel Sambuc lutok::debug::line_defined(void) const
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc return _pimpl->lua_debug.linedefined;
170*11be35a1SLionel Sambuc }
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc
173*11be35a1SLionel Sambuc /// Accessor for the 'lastlinedefined' field of lua_Debug.
174*11be35a1SLionel Sambuc ///
175*11be35a1SLionel Sambuc /// \return Returns the 'lastlinedefined' field of the internal lua_Debug
176*11be35a1SLionel Sambuc /// structure.
177*11be35a1SLionel Sambuc int
last_line_defined(void) const178*11be35a1SLionel Sambuc lutok::debug::last_line_defined(void) const
179*11be35a1SLionel Sambuc {
180*11be35a1SLionel Sambuc return _pimpl->lua_debug.lastlinedefined;
181*11be35a1SLionel Sambuc }
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc /// Accessor for the 'short_src' field of lua_Debug.
185*11be35a1SLionel Sambuc ///
186*11be35a1SLionel Sambuc /// \return Returns the 'short_src' field of the internal lua_Debug structure.
187*11be35a1SLionel Sambuc std::string
short_src(void) const188*11be35a1SLionel Sambuc lutok::debug::short_src(void) const
189*11be35a1SLionel Sambuc {
190*11be35a1SLionel Sambuc assert(_pimpl->lua_debug.short_src != NULL);
191*11be35a1SLionel Sambuc return _pimpl->lua_debug.short_src;
192*11be35a1SLionel Sambuc }
193