// Copyright 2012 Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file examples/raii.cpp29/// Demonstrates how RAII helps in keeping the Lua state consistent.30///31/// One of the major complains that is raised against the Lua C API is that it32/// is very hard to ensure it remains consistent during the execution of the33/// program. In the case of native C code, there exist many tools that help the34/// developer catch memory leaks, access to uninitialized variables, etc.35/// However, when using the Lua C API, none of these tools can validate that,36/// for example, the Lua stack remains balanced across calls.37///38/// Enter RAII. The RAII pattern, intensively applied by Lutok, helps the39/// developer in maintaining the Lua state consistent at all times in a40/// transparent manner. This example program attempts to illustrate this.4142#include <cassert>43#include <cstdlib>44#include <iostream>45#include <string>4647#include <lutok/operations.hpp>48#include <lutok/stack_cleaner.hpp>49#include <lutok/state.ipp>505152/// Prints the string-typed field of a table.53///54/// If the field contains a string, this function prints its value. If the55/// field contains any other type, this prints an error message.56///57/// \pre The top of the Lua stack in 'state' references a table.58///59/// \param state The Lua state.60/// \param field The name of the string-typed field.61static void62print_table_field(lutok::state& state, const std::string& field)63{64assert(state.is_table(-1));6566// Bring in some RAII magic: the stack_cleaner object captures the current67// height of the Lua stack at this point. Whenever the object goes out of68// scope, it will pop as many entries from the stack as necessary to restore69// the stack to its previous level.70//71// This ensures that, no matter how we exit the function, we do not leak72// objects in the stack.73lutok::stack_cleaner cleaner(state);7475// Stack contents: -1: table.76state.push_string(field);77// Stack contents: -2: table, -1: field name.78state.get_table(-2);79// Stack contents: -2: table, -1: field value.8081if (!state.is_string(-1)) {82std::cout << "The field " << field << " does not contain a string\n";83// Stack contents: -2: table, -1: field value.84//85// This is different than when we started! We should pop our extra86// value from the stack at this point. However, it is extremely common87// for software to have bugs (in this case, leaks) in error paths,88// mostly because such code paths are rarely exercised.89//90// By using the stack_cleaner object, we can be confident that the Lua91// stack will be cleared for us at this point, no matter what happened92// earlier on the stack nor how we exit the function.93return;94}9596std::cout << "String in field " << field << ": " << state.to_string(-1)97<< '\n';98// A well-behaved program explicitly pops anything extra from the stack to99// return it to its original state. Mostly for clarity.100state.pop(1);101102// Stack contents: -1: table. Same as when we started.103}104105106/// Program's entry point.107///108/// \return A system exit code.109int110main(void)111{112lutok::state state;113state.open_base();114115lutok::do_string(state, "example = {foo='hello', bar=123, baz='bye'}",1160, 0, 0);117118state.get_global("example");119print_table_field(state, "foo");120print_table_field(state, "bar");121print_table_field(state, "baz");122state.pop(1);123124return EXIT_SUCCESS;125}126127128