xref: /minix/external/bsd/lutok/dist/operations.cpp (revision 11be35a1)
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
create_module(state & s,const std::string & name,const std::map<std::string,cxx_function> & members)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 nresults The number of results to expect; -1 for any.
68 ///
69 /// \return The number of results left on the stack.
70 ///
71 /// \throw error If there is a problem processing the file.
72 unsigned int
do_file(state & s,const std::string & file,const int nresults)73 lutok::do_file(state& s, const std::string& file, const int nresults)
74 {
75     assert(nresults >= -1);
76     const int height = s.get_top();
77 
78     stack_cleaner cleaner(s);
79     try {
80         s.load_file(file);
81         s.pcall(0, nresults == -1 ? LUA_MULTRET : nresults, 0);
82     } catch (const lutok::api_error& e) {
83         throw lutok::error("Failed to load Lua file '" + file + "': " +
84                            e.what());
85     }
86     cleaner.forget();
87 
88     const int actual_results = s.get_top() - height;
89     assert(nresults == -1 || actual_results == nresults);
90     assert(actual_results >= 0);
91     return static_cast< unsigned int >(actual_results);
92 }
93 
94 
95 /// Processes a Lua script.
96 ///
97 /// This is a replacement for luaL_dostring but with proper error reporting
98 /// and stack control.
99 ///
100 /// \param s The Lua state.
101 /// \param str The string to process.
102 /// \param nresults The number of results to expect; -1 for any.
103 ///
104 /// \return The number of results left on the stack.
105 ///
106 /// \throw error If there is a problem processing the string.
107 unsigned int
do_string(state & s,const std::string & str,const int nresults)108 lutok::do_string(state& s, const std::string& str, const int nresults)
109 {
110     assert(nresults >= -1);
111     const int height = s.get_top();
112 
113     stack_cleaner cleaner(s);
114     try {
115         s.load_string(str);
116         s.pcall(0, nresults == -1 ? LUA_MULTRET : nresults, 0);
117     } catch (const lutok::api_error& e) {
118         throw lutok::error("Failed to process Lua string '" + str + "': " +
119                            e.what());
120     }
121     cleaner.forget();
122 
123     const int actual_results = s.get_top() - height;
124     assert(nresults == -1 || actual_results == nresults);
125     assert(actual_results >= 0);
126     return static_cast< unsigned int >(actual_results);
127 }
128 
129 
130 /// Convenience function to evaluate a Lua expression.
131 ///
132 /// \param s The Lua state.
133 /// \param expression The textual expression to evaluate.
134 /// \param nresults The number of results to leave on the stack.  Must be
135 ///     positive.
136 ///
137 /// \throw api_error If there is a problem evaluating the expression.
138 void
eval(state & s,const std::string & expression,const int nresults)139 lutok::eval(state& s, const std::string& expression, const int nresults)
140 {
141     assert(nresults > 0);
142     do_string(s, "return " + expression, nresults);
143 }
144