1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
33 #include "exceptions.hpp"
34 #include "operations.hpp"
35 #include "stack_cleaner.hpp"
39 /// Creates a module: i.e. a table with a set of methods in it.
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.
45 lutok::create_module(state& s, const std::string& name,
46 const std::map< std::string, cxx_function >& members)
48 stack_cleaner cleaner(s);
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);
60 /// Loads and processes a Lua file.
62 /// This is a replacement for luaL_dofile but with proper error reporting
63 /// and stack control.
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
72 /// \return The number of results left on the stack.
74 /// \throw error If there is a problem processing the file.
76 lutok::do_file(state& s, const std::string& file, const int nargs,
77 const int nresults, const int errfunc)
79 assert(nresults >= -1);
80 const int height = s.get_top() - nargs;
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 + "': " +
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);
100 /// Processes a Lua script.
102 /// This is a replacement for luaL_dostring but with proper error reporting
103 /// and stack control.
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
112 /// \return The number of results left on the stack.
114 /// \throw error If there is a problem processing the string.
116 lutok::do_string(state& s, const std::string& str, const int nargs,
117 const int nresults, const int errfunc)
119 assert(nresults >= -1);
120 const int height = s.get_top() - nargs;
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 + "': " +
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);
140 /// Convenience function to evaluate a Lua expression.
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
147 /// \throw api_error If there is a problem evaluating the expression.
149 lutok::eval(state& s, const std::string& expression, const int nresults)
151 assert(nresults > 0);
152 do_string(s, "return " + expression, 0, nresults, 0);