1*c697fb7fSBrooks Davis // Copyright 2012 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 /// \file examples/raii.cpp
30*c697fb7fSBrooks Davis /// Demonstrates how RAII helps in keeping the Lua state consistent.
31*c697fb7fSBrooks Davis ///
32*c697fb7fSBrooks Davis /// One of the major complains that is raised against the Lua C API is that it
33*c697fb7fSBrooks Davis /// is very hard to ensure it remains consistent during the execution of the
34*c697fb7fSBrooks Davis /// program. In the case of native C code, there exist many tools that help the
35*c697fb7fSBrooks Davis /// developer catch memory leaks, access to uninitialized variables, etc.
36*c697fb7fSBrooks Davis /// However, when using the Lua C API, none of these tools can validate that,
37*c697fb7fSBrooks Davis /// for example, the Lua stack remains balanced across calls.
38*c697fb7fSBrooks Davis ///
39*c697fb7fSBrooks Davis /// Enter RAII. The RAII pattern, intensively applied by Lutok, helps the
40*c697fb7fSBrooks Davis /// developer in maintaining the Lua state consistent at all times in a
41*c697fb7fSBrooks Davis /// transparent manner. This example program attempts to illustrate this.
42*c697fb7fSBrooks Davis
43*c697fb7fSBrooks Davis #include <cassert>
44*c697fb7fSBrooks Davis #include <cstdlib>
45*c697fb7fSBrooks Davis #include <iostream>
46*c697fb7fSBrooks Davis #include <string>
47*c697fb7fSBrooks Davis
48*c697fb7fSBrooks Davis #include <lutok/operations.hpp>
49*c697fb7fSBrooks Davis #include <lutok/stack_cleaner.hpp>
50*c697fb7fSBrooks Davis #include <lutok/state.ipp>
51*c697fb7fSBrooks Davis
52*c697fb7fSBrooks Davis
53*c697fb7fSBrooks Davis /// Prints the string-typed field of a table.
54*c697fb7fSBrooks Davis ///
55*c697fb7fSBrooks Davis /// If the field contains a string, this function prints its value. If the
56*c697fb7fSBrooks Davis /// field contains any other type, this prints an error message.
57*c697fb7fSBrooks Davis ///
58*c697fb7fSBrooks Davis /// \pre The top of the Lua stack in 'state' references a table.
59*c697fb7fSBrooks Davis ///
60*c697fb7fSBrooks Davis /// \param state The Lua state.
61*c697fb7fSBrooks Davis /// \param field The name of the string-typed field.
62*c697fb7fSBrooks Davis static void
print_table_field(lutok::state & state,const std::string & field)63*c697fb7fSBrooks Davis print_table_field(lutok::state& state, const std::string& field)
64*c697fb7fSBrooks Davis {
65*c697fb7fSBrooks Davis assert(state.is_table(-1));
66*c697fb7fSBrooks Davis
67*c697fb7fSBrooks Davis // Bring in some RAII magic: the stack_cleaner object captures the current
68*c697fb7fSBrooks Davis // height of the Lua stack at this point. Whenever the object goes out of
69*c697fb7fSBrooks Davis // scope, it will pop as many entries from the stack as necessary to restore
70*c697fb7fSBrooks Davis // the stack to its previous level.
71*c697fb7fSBrooks Davis //
72*c697fb7fSBrooks Davis // This ensures that, no matter how we exit the function, we do not leak
73*c697fb7fSBrooks Davis // objects in the stack.
74*c697fb7fSBrooks Davis lutok::stack_cleaner cleaner(state);
75*c697fb7fSBrooks Davis
76*c697fb7fSBrooks Davis // Stack contents: -1: table.
77*c697fb7fSBrooks Davis state.push_string(field);
78*c697fb7fSBrooks Davis // Stack contents: -2: table, -1: field name.
79*c697fb7fSBrooks Davis state.get_table(-2);
80*c697fb7fSBrooks Davis // Stack contents: -2: table, -1: field value.
81*c697fb7fSBrooks Davis
82*c697fb7fSBrooks Davis if (!state.is_string(-1)) {
83*c697fb7fSBrooks Davis std::cout << "The field " << field << " does not contain a string\n";
84*c697fb7fSBrooks Davis // Stack contents: -2: table, -1: field value.
85*c697fb7fSBrooks Davis //
86*c697fb7fSBrooks Davis // This is different than when we started! We should pop our extra
87*c697fb7fSBrooks Davis // value from the stack at this point. However, it is extremely common
88*c697fb7fSBrooks Davis // for software to have bugs (in this case, leaks) in error paths,
89*c697fb7fSBrooks Davis // mostly because such code paths are rarely exercised.
90*c697fb7fSBrooks Davis //
91*c697fb7fSBrooks Davis // By using the stack_cleaner object, we can be confident that the Lua
92*c697fb7fSBrooks Davis // stack will be cleared for us at this point, no matter what happened
93*c697fb7fSBrooks Davis // earlier on the stack nor how we exit the function.
94*c697fb7fSBrooks Davis return;
95*c697fb7fSBrooks Davis }
96*c697fb7fSBrooks Davis
97*c697fb7fSBrooks Davis std::cout << "String in field " << field << ": " << state.to_string(-1)
98*c697fb7fSBrooks Davis << '\n';
99*c697fb7fSBrooks Davis // A well-behaved program explicitly pops anything extra from the stack to
100*c697fb7fSBrooks Davis // return it to its original state. Mostly for clarity.
101*c697fb7fSBrooks Davis state.pop(1);
102*c697fb7fSBrooks Davis
103*c697fb7fSBrooks Davis // Stack contents: -1: table. Same as when we started.
104*c697fb7fSBrooks Davis }
105*c697fb7fSBrooks Davis
106*c697fb7fSBrooks Davis
107*c697fb7fSBrooks Davis /// Program's entry point.
108*c697fb7fSBrooks Davis ///
109*c697fb7fSBrooks Davis /// \return A system exit code.
110*c697fb7fSBrooks Davis int
main(void)111*c697fb7fSBrooks Davis main(void)
112*c697fb7fSBrooks Davis {
113*c697fb7fSBrooks Davis lutok::state state;
114*c697fb7fSBrooks Davis state.open_base();
115*c697fb7fSBrooks Davis
116*c697fb7fSBrooks Davis lutok::do_string(state, "example = {foo='hello', bar=123, baz='bye'}",
117*c697fb7fSBrooks Davis 0, 0, 0);
118*c697fb7fSBrooks Davis
119*c697fb7fSBrooks Davis state.get_global("example");
120*c697fb7fSBrooks Davis print_table_field(state, "foo");
121*c697fb7fSBrooks Davis print_table_field(state, "bar");
122*c697fb7fSBrooks Davis print_table_field(state, "baz");
123*c697fb7fSBrooks Davis state.pop(1);
124*c697fb7fSBrooks Davis
125*c697fb7fSBrooks Davis return EXIT_SUCCESS;
126*c697fb7fSBrooks Davis }
127