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