// Copyright 2011 Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include <cassert>2930#include <lua.hpp>3132#include <lutok/c_gate.hpp>33#include <lutok/debug.hpp>34#include <lutok/exceptions.hpp>35#include <lutok/state.ipp>363738/// Internal implementation for lutok::debug.39struct lutok::debug::impl {40/// The Lua internal debug state.41lua_Debug lua_debug;42};434445/// Constructor for an empty debug structure.46lutok::debug::debug(void) :47_pimpl(new impl())48{49}505152/// Destructor.53lutok::debug::~debug(void)54{55}565758/// Wrapper around lua_getinfo.59///60/// \param s The Lua state.61/// \param what_ The second parameter to lua_getinfo.62///63/// \warning Terminates execution if there is not enough memory to manipulate64/// the Lua stack.65void66lutok::debug::get_info(state& s, const std::string& what_)67{68lua_State* raw_state = state_c_gate(s).c_state();6970if (lua_getinfo(raw_state, what_.c_str(), &_pimpl->lua_debug) == 0)71throw lutok::api_error::from_stack(s, "lua_getinfo");72}737475/// Wrapper around lua_getstack.76///77/// \param s The Lua state.78/// \param level The second parameter to lua_getstack.79void80lutok::debug::get_stack(state& s, const int level)81{82lua_State* raw_state = state_c_gate(s).c_state();8384lua_getstack(raw_state, level, &_pimpl->lua_debug);85}868788/// Accessor for the 'event' field of lua_Debug.89///90/// \return Returns the 'event' field of the internal lua_Debug structure.91int92lutok::debug::event(void) const93{94return _pimpl->lua_debug.event;95}969798/// Accessor for the 'name' field of lua_Debug.99///100/// \return Returns the 'name' field of the internal lua_Debug structure.101std::string102lutok::debug::name(void) const103{104assert(_pimpl->lua_debug.name != NULL);105return _pimpl->lua_debug.name;106}107108109/// Accessor for the 'namewhat' field of lua_Debug.110///111/// \return Returns the 'namewhat' field of the internal lua_Debug structure.112std::string113lutok::debug::name_what(void) const114{115assert(_pimpl->lua_debug.namewhat != NULL);116return _pimpl->lua_debug.namewhat;117}118119120/// Accessor for the 'what' field of lua_Debug.121///122/// \return Returns the 'what' field of the internal lua_Debug structure.123std::string124lutok::debug::what(void) const125{126assert(_pimpl->lua_debug.what != NULL);127return _pimpl->lua_debug.what;128}129130131/// Accessor for the 'source' field of lua_Debug.132///133/// \return Returns the 'source' field of the internal lua_Debug structure.134std::string135lutok::debug::source(void) const136{137assert(_pimpl->lua_debug.source != NULL);138return _pimpl->lua_debug.source;139}140141142/// Accessor for the 'currentline' field of lua_Debug.143///144/// \return Returns the 'currentline' field of the internal lua_Debug structure.145int146lutok::debug::current_line(void) const147{148return _pimpl->lua_debug.currentline;149}150151152/// Accessor for the 'nups' field of lua_Debug.153///154/// \return Returns the 'nups' field of the internal lua_Debug structure.155int156lutok::debug::n_ups(void) const157{158return _pimpl->lua_debug.nups;159}160161162/// Accessor for the 'linedefined' field of lua_Debug.163///164/// \return Returns the 'linedefined' field of the internal lua_Debug structure.165int166lutok::debug::line_defined(void) const167{168return _pimpl->lua_debug.linedefined;169}170171172/// Accessor for the 'lastlinedefined' field of lua_Debug.173///174/// \return Returns the 'lastlinedefined' field of the internal lua_Debug175/// structure.176int177lutok::debug::last_line_defined(void) const178{179return _pimpl->lua_debug.lastlinedefined;180}181182183/// Accessor for the 'short_src' field of lua_Debug.184///185/// \return Returns the 'short_src' field of the internal lua_Debug structure.186std::string187lutok::debug::short_src(void) const188{189assert(_pimpl->lua_debug.short_src != NULL);190return _pimpl->lua_debug.short_src;191}192193194