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 "exceptions.hpp" 34 #include "operations.hpp" 35 #include "stack_cleaner.hpp" 36 #include "state.hpp" 37 38 39 /// Creates a module: i.e. a table with a set of methods in it. 40 /// 41 /// \param s The Lua state. 42 /// \param name The name of the module to create. 43 /// \param members The list of member functions to add to the module. 44 void 45 lutok::create_module(state& s, const std::string& name, 46 const std::map< std::string, cxx_function >& members) 47 { 48 stack_cleaner cleaner(s); 49 s.new_table(); 50 for (std::map< std::string, cxx_function >::const_iterator 51 iter = members.begin(); iter != members.end(); iter++) { 52 s.push_string((*iter).first); 53 s.push_cxx_function((*iter).second); 54 s.set_table(-3); 55 } 56 s.set_global(name); 57 } 58 59 60 /// Loads and processes a Lua file. 61 /// 62 /// This is a replacement for luaL_dofile but with proper error reporting 63 /// and stack control. 64 /// 65 /// \param s The Lua state. 66 /// \param file The file to load. 67 /// \param nargs The number of arguments on the stack to pass to the file. 68 /// \param nresults The number of results to expect; -1 for any. 69 /// \param errfunc If not 0, index of a function in the stack to act as an 70 /// error handler. 71 /// 72 /// \return The number of results left on the stack. 73 /// 74 /// \throw error If there is a problem processing the file. 75 unsigned int 76 lutok::do_file(state& s, const std::string& file, const int nargs, 77 const int nresults, const int errfunc) 78 { 79 assert(nresults >= -1); 80 const int height = s.get_top() - nargs; 81 82 try { 83 s.load_file(file); 84 if (nargs > 0) 85 s.insert(-nargs - 1); 86 s.pcall(nargs, nresults == -1 ? LUA_MULTRET : nresults, 87 errfunc == 0 ? 0 : errfunc - 1); 88 } catch (const lutok::api_error& e) { 89 throw lutok::error("Failed to load Lua file '" + file + "': " + 90 e.what()); 91 } 92 93 const int actual_results = s.get_top() - height; 94 assert(nresults == -1 || actual_results == nresults); 95 assert(actual_results >= 0); 96 return static_cast< unsigned int >(actual_results); 97 } 98 99 100 /// Processes a Lua script. 101 /// 102 /// This is a replacement for luaL_dostring but with proper error reporting 103 /// and stack control. 104 /// 105 /// \param s The Lua state. 106 /// \param str The string to process. 107 /// \param nargs The number of arguments on the stack to pass to the chunk. 108 /// \param nresults The number of results to expect; -1 for any. 109 /// \param errfunc If not 0, index of a function in the stack to act as an 110 /// error handler. 111 /// 112 /// \return The number of results left on the stack. 113 /// 114 /// \throw error If there is a problem processing the string. 115 unsigned int 116 lutok::do_string(state& s, const std::string& str, const int nargs, 117 const int nresults, const int errfunc) 118 { 119 assert(nresults >= -1); 120 const int height = s.get_top() - nargs; 121 122 try { 123 s.load_string(str); 124 if (nargs > 0) 125 s.insert(-nargs - 1); 126 s.pcall(nargs, nresults == -1 ? LUA_MULTRET : nresults, 127 errfunc == 0 ? 0 : errfunc - 1); 128 } catch (const lutok::api_error& e) { 129 throw lutok::error("Failed to process Lua string '" + str + "': " + 130 e.what()); 131 } 132 133 const int actual_results = s.get_top() - height; 134 assert(nresults == -1 || actual_results == nresults); 135 assert(actual_results >= 0); 136 return static_cast< unsigned int >(actual_results); 137 } 138 139 140 /// Convenience function to evaluate a Lua expression. 141 /// 142 /// \param s The Lua state. 143 /// \param expression The textual expression to evaluate. 144 /// \param nresults The number of results to leave on the stack. Must be 145 /// positive. 146 /// 147 /// \throw api_error If there is a problem evaluating the expression. 148 void 149 lutok::eval(state& s, const std::string& expression, const int nresults) 150 { 151 assert(nresults > 0); 152 do_string(s, "return " + expression, 0, nresults, 0); 153 } 154