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