1*c697fb7fSBrooks Davis // Copyright 2011 Google Inc. 2*c697fb7fSBrooks Davis // All rights reserved. 3*c697fb7fSBrooks Davis // 4*c697fb7fSBrooks Davis // Redistribution and use in source and binary forms, with or without 5*c697fb7fSBrooks Davis // modification, are permitted provided that the following conditions are 6*c697fb7fSBrooks Davis // met: 7*c697fb7fSBrooks Davis // 8*c697fb7fSBrooks Davis // * Redistributions of source code must retain the above copyright 9*c697fb7fSBrooks Davis // notice, this list of conditions and the following disclaimer. 10*c697fb7fSBrooks Davis // * Redistributions in binary form must reproduce the above copyright 11*c697fb7fSBrooks Davis // notice, this list of conditions and the following disclaimer in the 12*c697fb7fSBrooks Davis // documentation and/or other materials provided with the distribution. 13*c697fb7fSBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors 14*c697fb7fSBrooks Davis // may be used to endorse or promote products derived from this software 15*c697fb7fSBrooks Davis // without specific prior written permission. 16*c697fb7fSBrooks Davis // 17*c697fb7fSBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18*c697fb7fSBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19*c697fb7fSBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20*c697fb7fSBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21*c697fb7fSBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22*c697fb7fSBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23*c697fb7fSBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24*c697fb7fSBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25*c697fb7fSBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26*c697fb7fSBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27*c697fb7fSBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*c697fb7fSBrooks Davis 29*c697fb7fSBrooks Davis /// \file state.hpp 30*c697fb7fSBrooks Davis /// Provides the state wrapper class for the Lua C state. 31*c697fb7fSBrooks Davis 32*c697fb7fSBrooks Davis #if !defined(LUTOK_STATE_HPP) 33*c697fb7fSBrooks Davis #define LUTOK_STATE_HPP 34*c697fb7fSBrooks Davis 35*c697fb7fSBrooks Davis #include <string> 36*c697fb7fSBrooks Davis 37*c697fb7fSBrooks Davis #if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L 38*c697fb7fSBrooks Davis #include <memory> 39*c697fb7fSBrooks Davis #else 40*c697fb7fSBrooks Davis #include <tr1/memory> 41*c697fb7fSBrooks Davis #endif 42*c697fb7fSBrooks Davis 43*c697fb7fSBrooks Davis namespace lutok { 44*c697fb7fSBrooks Davis 45*c697fb7fSBrooks Davis 46*c697fb7fSBrooks Davis class debug; 47*c697fb7fSBrooks Davis class state; 48*c697fb7fSBrooks Davis 49*c697fb7fSBrooks Davis 50*c697fb7fSBrooks Davis /// The type of a C++ function that can be bound into Lua. 51*c697fb7fSBrooks Davis /// 52*c697fb7fSBrooks Davis /// Functions of this type are free to raise exceptions. These will not 53*c697fb7fSBrooks Davis /// propagate into the Lua C API. However, any such exceptions will be reported 54*c697fb7fSBrooks Davis /// as a Lua error and their type will be lost. 55*c697fb7fSBrooks Davis typedef int (*cxx_function)(state&); 56*c697fb7fSBrooks Davis 57*c697fb7fSBrooks Davis 58*c697fb7fSBrooks Davis /// Stack index constant pointing to the registry table. 59*c697fb7fSBrooks Davis extern const int registry_index; 60*c697fb7fSBrooks Davis 61*c697fb7fSBrooks Davis 62*c697fb7fSBrooks Davis /// A RAII model for the Lua state. 63*c697fb7fSBrooks Davis /// 64*c697fb7fSBrooks Davis /// This class holds the state of the Lua interpreter during its existence and 65*c697fb7fSBrooks Davis /// provides wrappers around several Lua library functions that operate on such 66*c697fb7fSBrooks Davis /// state. 67*c697fb7fSBrooks Davis /// 68*c697fb7fSBrooks Davis /// These wrapper functions differ from the C versions in that they use the 69*c697fb7fSBrooks Davis /// implicit state hold by the class, they use C++ types where appropriate and 70*c697fb7fSBrooks Davis /// they use exceptions to report errors. 71*c697fb7fSBrooks Davis /// 72*c697fb7fSBrooks Davis /// The wrappers intend to be as lightweight as possible but, in some 73*c697fb7fSBrooks Davis /// situations, they are pretty complex because they need to do extra work to 74*c697fb7fSBrooks Davis /// capture the errors reported by the Lua C API. We prefer having fine-grained 75*c697fb7fSBrooks Davis /// error control rather than efficiency, so this is OK. 76*c697fb7fSBrooks Davis class state { 77*c697fb7fSBrooks Davis struct impl; 78*c697fb7fSBrooks Davis 79*c697fb7fSBrooks Davis /// Pointer to the shared internal implementation. 80*c697fb7fSBrooks Davis #if defined(_LIBCPP_VERSION) || __cplusplus >= 201103L 81*c697fb7fSBrooks Davis std::shared_ptr< impl > _pimpl; 82*c697fb7fSBrooks Davis #else 83*c697fb7fSBrooks Davis std::tr1::shared_ptr< impl > _pimpl; 84*c697fb7fSBrooks Davis #endif 85*c697fb7fSBrooks Davis 86*c697fb7fSBrooks Davis void* new_userdata_voidp(const size_t); 87*c697fb7fSBrooks Davis void* to_userdata_voidp(const int); 88*c697fb7fSBrooks Davis 89*c697fb7fSBrooks Davis friend class state_c_gate; 90*c697fb7fSBrooks Davis explicit state(void*); 91*c697fb7fSBrooks Davis void* raw_state(void); 92*c697fb7fSBrooks Davis 93*c697fb7fSBrooks Davis public: 94*c697fb7fSBrooks Davis state(void); 95*c697fb7fSBrooks Davis ~state(void); 96*c697fb7fSBrooks Davis 97*c697fb7fSBrooks Davis void close(void); 98*c697fb7fSBrooks Davis void get_global(const std::string&); 99*c697fb7fSBrooks Davis void get_global_table(void); 100*c697fb7fSBrooks Davis bool get_metafield(const int, const std::string&); 101*c697fb7fSBrooks Davis bool get_metatable(const int); 102*c697fb7fSBrooks Davis void get_table(const int); 103*c697fb7fSBrooks Davis int get_top(void); 104*c697fb7fSBrooks Davis void insert(const int); 105*c697fb7fSBrooks Davis bool is_boolean(const int); 106*c697fb7fSBrooks Davis bool is_function(const int); 107*c697fb7fSBrooks Davis bool is_nil(const int); 108*c697fb7fSBrooks Davis bool is_number(const int); 109*c697fb7fSBrooks Davis bool is_string(const int); 110*c697fb7fSBrooks Davis bool is_table(const int); 111*c697fb7fSBrooks Davis bool is_userdata(const int); 112*c697fb7fSBrooks Davis void load_file(const std::string&); 113*c697fb7fSBrooks Davis void load_string(const std::string&); 114*c697fb7fSBrooks Davis void new_table(void); 115*c697fb7fSBrooks Davis template< typename Type > Type* new_userdata(void); 116*c697fb7fSBrooks Davis bool next(const int); 117*c697fb7fSBrooks Davis void open_all(void); 118*c697fb7fSBrooks Davis void open_base(void); 119*c697fb7fSBrooks Davis void open_string(void); 120*c697fb7fSBrooks Davis void open_table(void); 121*c697fb7fSBrooks Davis void pcall(const int, const int, const int); 122*c697fb7fSBrooks Davis void pop(const int); 123*c697fb7fSBrooks Davis void push_boolean(const bool); 124*c697fb7fSBrooks Davis void push_cxx_closure(cxx_function, const int); 125*c697fb7fSBrooks Davis void push_cxx_function(cxx_function); 126*c697fb7fSBrooks Davis void push_integer(const int); 127*c697fb7fSBrooks Davis void push_nil(void); 128*c697fb7fSBrooks Davis void push_string(const std::string&); 129*c697fb7fSBrooks Davis void push_value(const int); 130*c697fb7fSBrooks Davis void raw_get(const int); 131*c697fb7fSBrooks Davis void raw_set(const int); 132*c697fb7fSBrooks Davis void set_global(const std::string&); 133*c697fb7fSBrooks Davis void set_metatable(const int); 134*c697fb7fSBrooks Davis void set_table(const int); 135*c697fb7fSBrooks Davis bool to_boolean(const int); 136*c697fb7fSBrooks Davis long to_integer(const int); 137*c697fb7fSBrooks Davis template< typename Type > Type* to_userdata(const int); 138*c697fb7fSBrooks Davis std::string to_string(const int); 139*c697fb7fSBrooks Davis int upvalue_index(const int); 140*c697fb7fSBrooks Davis }; 141*c697fb7fSBrooks Davis 142*c697fb7fSBrooks Davis 143*c697fb7fSBrooks Davis } // namespace lutok 144*c697fb7fSBrooks Davis 145*c697fb7fSBrooks Davis #endif // !defined(LUTOK_STATE_HPP) 146