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