xref: /minix3/external/bsd/lutok/dist/examples/bindings.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2012 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 /// \file examples/bindings.cpp
30*11be35a1SLionel Sambuc /// Showcases how to define Lua functions from C++ code.
31*11be35a1SLionel Sambuc ///
32*11be35a1SLionel Sambuc /// A major selling point of Lua is that it is very easy too hook native C and
33*11be35a1SLionel Sambuc /// C++ functions into the runtime environment so that Lua can call them.  The
34*11be35a1SLionel Sambuc /// purpose of this example program is to show how this is done by using Lutok.
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc #include <cassert>
37*11be35a1SLionel Sambuc #include <cstdlib>
38*11be35a1SLionel Sambuc #include <iostream>
39*11be35a1SLionel Sambuc #include <map>
40*11be35a1SLionel Sambuc #include <sstream>
41*11be35a1SLionel Sambuc #include <stdexcept>
42*11be35a1SLionel Sambuc #include <string>
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc #include <lutok/exceptions.hpp>
45*11be35a1SLionel Sambuc #include <lutok/operations.hpp>
46*11be35a1SLionel Sambuc #include <lutok/state.ipp>
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc /// Calculates the factorial of a given number.
50*11be35a1SLionel Sambuc ///
51*11be35a1SLionel Sambuc /// \param i The postivie number to calculate the factorial of.
52*11be35a1SLionel Sambuc ///
53*11be35a1SLionel Sambuc /// \return The factorial of i.
54*11be35a1SLionel Sambuc static int
factorial(const int i)55*11be35a1SLionel Sambuc factorial(const int i)
56*11be35a1SLionel Sambuc {
57*11be35a1SLionel Sambuc     assert(i >= 0);
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc     if (i == 0)
60*11be35a1SLionel Sambuc         return 1;
61*11be35a1SLionel Sambuc     else
62*11be35a1SLionel Sambuc         return i * factorial(i - 1);
63*11be35a1SLionel Sambuc }
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc 
66*11be35a1SLionel Sambuc /// A custom factorial function for Lua.
67*11be35a1SLionel Sambuc ///
68*11be35a1SLionel Sambuc /// \pre stack(-1) contains the number to calculate the factorial of.
69*11be35a1SLionel Sambuc /// \post stack(-1) contains the result of the operation.
70*11be35a1SLionel Sambuc ///
71*11be35a1SLionel Sambuc /// \param state The Lua state from which to get the function arguments and into
72*11be35a1SLionel Sambuc ///     which to push the results.
73*11be35a1SLionel Sambuc ///
74*11be35a1SLionel Sambuc /// \return The number of results pushed onto the stack, i.e. 1.
75*11be35a1SLionel Sambuc ///
76*11be35a1SLionel Sambuc /// \throw std::runtime_error If the input parameters are invalid.  Note that
77*11be35a1SLionel Sambuc ///     Lutok will convert this exception to lutok::error.
78*11be35a1SLionel Sambuc static int
lua_factorial(lutok::state & state)79*11be35a1SLionel Sambuc lua_factorial(lutok::state& state)
80*11be35a1SLionel Sambuc {
81*11be35a1SLionel Sambuc     if (!state.is_number())
82*11be35a1SLionel Sambuc         throw std::runtime_error("Argument to factorial must be an integer");
83*11be35a1SLionel Sambuc     const int i = state.to_integer();
84*11be35a1SLionel Sambuc     if (i < 0)
85*11be35a1SLionel Sambuc         throw std::runtime_error("Argument to factorial must be positive");
86*11be35a1SLionel Sambuc     state.push_integer(factorial(i));
87*11be35a1SLionel Sambuc     return 1;
88*11be35a1SLionel Sambuc }
89*11be35a1SLionel Sambuc 
90*11be35a1SLionel Sambuc 
91*11be35a1SLionel Sambuc /// Program's entry point.
92*11be35a1SLionel Sambuc ///
93*11be35a1SLionel Sambuc /// \param argc Length of argv.  Must be 2.
94*11be35a1SLionel Sambuc /// \param argv Command-line arguments to the program.  The first argument to
95*11be35a1SLionel Sambuc ///     the tool has to be a number.
96*11be35a1SLionel Sambuc ///
97*11be35a1SLionel Sambuc /// \return A system exit code.
98*11be35a1SLionel Sambuc int
main(int argc,char ** argv)99*11be35a1SLionel Sambuc main(int argc, char** argv)
100*11be35a1SLionel Sambuc {
101*11be35a1SLionel Sambuc     if (argc != 2) {
102*11be35a1SLionel Sambuc         std::cerr << "Usage: bindings <number>\n";
103*11be35a1SLionel Sambuc         return EXIT_FAILURE;
104*11be35a1SLionel Sambuc     }
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc     // Create a new Lua session and load the print() function.
107*11be35a1SLionel Sambuc     lutok::state state;
108*11be35a1SLionel Sambuc     state.open_base();
109*11be35a1SLionel Sambuc 
110*11be35a1SLionel Sambuc     // Construct a 'module' that contains an entry point to our native factorial
111*11be35a1SLionel Sambuc     // function.  A module is just a Lua table that contains a mapping of names
112*11be35a1SLionel Sambuc     // to functions.  Instead of creating a module by using our create_module()
113*11be35a1SLionel Sambuc     // helper function, we could have used push_cxx_function on the state to
114*11be35a1SLionel Sambuc     // define the function ourselves.
115*11be35a1SLionel Sambuc     std::map< std::string, lutok::cxx_function > module;
116*11be35a1SLionel Sambuc     module["factorial"] = lua_factorial;
117*11be35a1SLionel Sambuc     lutok::create_module(state, "native", module);
118*11be35a1SLionel Sambuc 
119*11be35a1SLionel Sambuc     // Use a little Lua script to call our native factorial function providing
120*11be35a1SLionel Sambuc     // it the first argument passed to the program.  Note that this will error
121*11be35a1SLionel Sambuc     // out in a controlled manner if the passed argument is not an integer.  The
122*11be35a1SLionel Sambuc     // important thing to notice is that the exception comes from our own C++
123*11be35a1SLionel Sambuc     // binding and that it has been converted to a lutok::error.
124*11be35a1SLionel Sambuc     std::ostringstream script;
125*11be35a1SLionel Sambuc     script << "print(native.factorial(" << argv[1] << "))";
126*11be35a1SLionel Sambuc     try {
127*11be35a1SLionel Sambuc         lutok::do_string(state, script.str());
128*11be35a1SLionel Sambuc         return EXIT_SUCCESS;
129*11be35a1SLionel Sambuc     } catch (const lutok::error& e) {
130*11be35a1SLionel Sambuc         std::cerr << "ERROR: " << e.what() << '\n';
131*11be35a1SLionel Sambuc         return EXIT_FAILURE;
132*11be35a1SLionel Sambuc     }
133*11be35a1SLionel Sambuc }
134