xref: /netbsd-src/external/bsd/lutok/dist/operations.cpp (revision a15637525a498a84b68d8b44b1c7ec01de4b564a)
1*a1563752Sjmmv // Copyright 2011 Google Inc.
2*a1563752Sjmmv // All rights reserved.
3*a1563752Sjmmv //
4*a1563752Sjmmv // Redistribution and use in source and binary forms, with or without
5*a1563752Sjmmv // modification, are permitted provided that the following conditions are
6*a1563752Sjmmv // met:
7*a1563752Sjmmv //
8*a1563752Sjmmv // * Redistributions of source code must retain the above copyright
9*a1563752Sjmmv //   notice, this list of conditions and the following disclaimer.
10*a1563752Sjmmv // * Redistributions in binary form must reproduce the above copyright
11*a1563752Sjmmv //   notice, this list of conditions and the following disclaimer in the
12*a1563752Sjmmv //   documentation and/or other materials provided with the distribution.
13*a1563752Sjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*a1563752Sjmmv //   may be used to endorse or promote products derived from this software
15*a1563752Sjmmv //   without specific prior written permission.
16*a1563752Sjmmv //
17*a1563752Sjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*a1563752Sjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*a1563752Sjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*a1563752Sjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*a1563752Sjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*a1563752Sjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*a1563752Sjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*a1563752Sjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*a1563752Sjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*a1563752Sjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*a1563752Sjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*a1563752Sjmmv 
29*a1563752Sjmmv #include <cassert>
30*a1563752Sjmmv 
31*a1563752Sjmmv #include <lua.hpp>
32*a1563752Sjmmv 
33*a1563752Sjmmv #include "exceptions.hpp"
34*a1563752Sjmmv #include "operations.hpp"
35*a1563752Sjmmv #include "stack_cleaner.hpp"
36*a1563752Sjmmv #include "state.hpp"
37*a1563752Sjmmv 
38*a1563752Sjmmv 
39*a1563752Sjmmv /// Creates a module: i.e. a table with a set of methods in it.
40*a1563752Sjmmv ///
41*a1563752Sjmmv /// \param s The Lua state.
42*a1563752Sjmmv /// \param name The name of the module to create.
43*a1563752Sjmmv /// \param members The list of member functions to add to the module.
44*a1563752Sjmmv void
create_module(state & s,const std::string & name,const std::map<std::string,cxx_function> & members)45*a1563752Sjmmv lutok::create_module(state& s, const std::string& name,
46*a1563752Sjmmv                      const std::map< std::string, cxx_function >& members)
47*a1563752Sjmmv {
48*a1563752Sjmmv     stack_cleaner cleaner(s);
49*a1563752Sjmmv     s.new_table();
50*a1563752Sjmmv     for (std::map< std::string, cxx_function >::const_iterator
51*a1563752Sjmmv          iter = members.begin(); iter != members.end(); iter++) {
52*a1563752Sjmmv         s.push_string((*iter).first);
53*a1563752Sjmmv         s.push_cxx_function((*iter).second);
54*a1563752Sjmmv         s.set_table(-3);
55*a1563752Sjmmv     }
56*a1563752Sjmmv     s.set_global(name);
57*a1563752Sjmmv }
58*a1563752Sjmmv 
59*a1563752Sjmmv 
60*a1563752Sjmmv /// Loads and processes a Lua file.
61*a1563752Sjmmv ///
62*a1563752Sjmmv /// This is a replacement for luaL_dofile but with proper error reporting
63*a1563752Sjmmv /// and stack control.
64*a1563752Sjmmv ///
65*a1563752Sjmmv /// \param s The Lua state.
66*a1563752Sjmmv /// \param file The file to load.
67*a1563752Sjmmv /// \param nresults The number of results to expect; -1 for any.
68*a1563752Sjmmv ///
69*a1563752Sjmmv /// \return The number of results left on the stack.
70*a1563752Sjmmv ///
71*a1563752Sjmmv /// \throw error If there is a problem processing the file.
72*a1563752Sjmmv unsigned int
do_file(state & s,const std::string & file,const int nresults)73*a1563752Sjmmv lutok::do_file(state& s, const std::string& file, const int nresults)
74*a1563752Sjmmv {
75*a1563752Sjmmv     assert(nresults >= -1);
76*a1563752Sjmmv     const int height = s.get_top();
77*a1563752Sjmmv 
78*a1563752Sjmmv     stack_cleaner cleaner(s);
79*a1563752Sjmmv     try {
80*a1563752Sjmmv         s.load_file(file);
81*a1563752Sjmmv         s.pcall(0, nresults == -1 ? LUA_MULTRET : nresults, 0);
82*a1563752Sjmmv     } catch (const lutok::api_error& e) {
83*a1563752Sjmmv         throw lutok::error("Failed to load Lua file '" + file + "': " +
84*a1563752Sjmmv                            e.what());
85*a1563752Sjmmv     }
86*a1563752Sjmmv     cleaner.forget();
87*a1563752Sjmmv 
88*a1563752Sjmmv     const int actual_results = s.get_top() - height;
89*a1563752Sjmmv     assert(nresults == -1 || actual_results == nresults);
90*a1563752Sjmmv     assert(actual_results >= 0);
91*a1563752Sjmmv     return static_cast< unsigned int >(actual_results);
92*a1563752Sjmmv }
93*a1563752Sjmmv 
94*a1563752Sjmmv 
95*a1563752Sjmmv /// Processes a Lua script.
96*a1563752Sjmmv ///
97*a1563752Sjmmv /// This is a replacement for luaL_dostring but with proper error reporting
98*a1563752Sjmmv /// and stack control.
99*a1563752Sjmmv ///
100*a1563752Sjmmv /// \param s The Lua state.
101*a1563752Sjmmv /// \param str The string to process.
102*a1563752Sjmmv /// \param nresults The number of results to expect; -1 for any.
103*a1563752Sjmmv ///
104*a1563752Sjmmv /// \return The number of results left on the stack.
105*a1563752Sjmmv ///
106*a1563752Sjmmv /// \throw error If there is a problem processing the string.
107*a1563752Sjmmv unsigned int
do_string(state & s,const std::string & str,const int nresults)108*a1563752Sjmmv lutok::do_string(state& s, const std::string& str, const int nresults)
109*a1563752Sjmmv {
110*a1563752Sjmmv     assert(nresults >= -1);
111*a1563752Sjmmv     const int height = s.get_top();
112*a1563752Sjmmv 
113*a1563752Sjmmv     stack_cleaner cleaner(s);
114*a1563752Sjmmv     try {
115*a1563752Sjmmv         s.load_string(str);
116*a1563752Sjmmv         s.pcall(0, nresults == -1 ? LUA_MULTRET : nresults, 0);
117*a1563752Sjmmv     } catch (const lutok::api_error& e) {
118*a1563752Sjmmv         throw lutok::error("Failed to process Lua string '" + str + "': " +
119*a1563752Sjmmv                            e.what());
120*a1563752Sjmmv     }
121*a1563752Sjmmv     cleaner.forget();
122*a1563752Sjmmv 
123*a1563752Sjmmv     const int actual_results = s.get_top() - height;
124*a1563752Sjmmv     assert(nresults == -1 || actual_results == nresults);
125*a1563752Sjmmv     assert(actual_results >= 0);
126*a1563752Sjmmv     return static_cast< unsigned int >(actual_results);
127*a1563752Sjmmv }
128*a1563752Sjmmv 
129*a1563752Sjmmv 
130*a1563752Sjmmv /// Convenience function to evaluate a Lua expression.
131*a1563752Sjmmv ///
132*a1563752Sjmmv /// \param s The Lua state.
133*a1563752Sjmmv /// \param expression The textual expression to evaluate.
134*a1563752Sjmmv /// \param nresults The number of results to leave on the stack.  Must be
135*a1563752Sjmmv ///     positive.
136*a1563752Sjmmv ///
137*a1563752Sjmmv /// \throw api_error If there is a problem evaluating the expression.
138*a1563752Sjmmv void
eval(state & s,const std::string & expression,const int nresults)139*a1563752Sjmmv lutok::eval(state& s, const std::string& expression, const int nresults)
140*a1563752Sjmmv {
141*a1563752Sjmmv     assert(nresults > 0);
142*a1563752Sjmmv     do_string(s, "return " + expression, nresults);
143*a1563752Sjmmv }
144