16a6c8299Sjmmv // Copyright 2012 Google Inc.
26a6c8299Sjmmv // All rights reserved.
36a6c8299Sjmmv //
46a6c8299Sjmmv // Redistribution and use in source and binary forms, with or without
56a6c8299Sjmmv // modification, are permitted provided that the following conditions are
66a6c8299Sjmmv // met:
76a6c8299Sjmmv //
86a6c8299Sjmmv // * Redistributions of source code must retain the above copyright
96a6c8299Sjmmv //   notice, this list of conditions and the following disclaimer.
106a6c8299Sjmmv // * Redistributions in binary form must reproduce the above copyright
116a6c8299Sjmmv //   notice, this list of conditions and the following disclaimer in the
126a6c8299Sjmmv //   documentation and/or other materials provided with the distribution.
136a6c8299Sjmmv // * Neither the name of Google Inc. nor the names of its contributors
146a6c8299Sjmmv //   may be used to endorse or promote products derived from this software
156a6c8299Sjmmv //   without specific prior written permission.
166a6c8299Sjmmv //
176a6c8299Sjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186a6c8299Sjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196a6c8299Sjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206a6c8299Sjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216a6c8299Sjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226a6c8299Sjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236a6c8299Sjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246a6c8299Sjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256a6c8299Sjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266a6c8299Sjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276a6c8299Sjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286a6c8299Sjmmv 
296a6c8299Sjmmv #include "utils/config/lua_module.hpp"
306a6c8299Sjmmv 
316a6c8299Sjmmv #include <lutok/stack_cleaner.hpp>
326a6c8299Sjmmv #include <lutok/state.ipp>
336a6c8299Sjmmv 
346a6c8299Sjmmv #include "utils/config/exceptions.hpp"
356a6c8299Sjmmv #include "utils/config/tree.ipp"
366a6c8299Sjmmv 
376a6c8299Sjmmv namespace config = utils::config;
386a6c8299Sjmmv 
396a6c8299Sjmmv 
406a6c8299Sjmmv namespace {
416a6c8299Sjmmv 
426a6c8299Sjmmv 
436a6c8299Sjmmv /// Gets the tree singleton stored in the Lua state.
446a6c8299Sjmmv ///
45*84dd42a1Sjmmv /// \param state The Lua state.  The registry must contain a key named
466a6c8299Sjmmv ///    "tree" with a pointer to the singleton.
476a6c8299Sjmmv ///
486a6c8299Sjmmv /// \return A reference to the tree associated with the Lua state.
496a6c8299Sjmmv ///
506a6c8299Sjmmv /// \throw syntax_error If the tree cannot be located.
516a6c8299Sjmmv config::tree&
get_global_tree(lutok::state & state)526a6c8299Sjmmv get_global_tree(lutok::state& state)
536a6c8299Sjmmv {
546a6c8299Sjmmv     lutok::stack_cleaner cleaner(state);
556a6c8299Sjmmv 
56*84dd42a1Sjmmv     state.push_value(lutok::registry_index);
57*84dd42a1Sjmmv     state.push_string("tree");
58*84dd42a1Sjmmv     state.get_table(-2);
59*84dd42a1Sjmmv     if (state.is_nil())
606a6c8299Sjmmv         throw config::syntax_error("Cannot find tree singleton; global state "
616a6c8299Sjmmv                                    "corrupted?");
62*84dd42a1Sjmmv     config::tree& tree = **state.to_userdata< config::tree* >();
63*84dd42a1Sjmmv     state.pop(1);
64*84dd42a1Sjmmv     return tree;
656a6c8299Sjmmv }
666a6c8299Sjmmv 
676a6c8299Sjmmv 
686a6c8299Sjmmv /// Gets a fully-qualified tree key from the state.
696a6c8299Sjmmv ///
706a6c8299Sjmmv /// \param state The Lua state.
716a6c8299Sjmmv /// \param table_index An index to the Lua stack pointing to the table being
726a6c8299Sjmmv ///     accessed.  If this table contains a tree_key metadata property, this is
736a6c8299Sjmmv ///     considered to be the prefix of the tree key.
746a6c8299Sjmmv /// \param field_index An index to the Lua stack pointing to the entry
756a6c8299Sjmmv ///     containing the name of the field being indexed.
766a6c8299Sjmmv ///
776a6c8299Sjmmv /// \return A dotted key.
786a6c8299Sjmmv ///
796a6c8299Sjmmv /// \throw invalid_key_error If the name of the key is invalid.
806a6c8299Sjmmv static std::string
get_tree_key(lutok::state & state,const int table_index,const int field_index)816a6c8299Sjmmv get_tree_key(lutok::state& state, const int table_index, const int field_index)
826a6c8299Sjmmv {
836a6c8299Sjmmv     PRE(state.is_string(field_index));
846a6c8299Sjmmv     const std::string field = state.to_string(field_index);
856a6c8299Sjmmv     if (!field.empty() && field[0] == '_')
866a6c8299Sjmmv         throw config::invalid_key_error(
876a6c8299Sjmmv             F("Configuration key cannot have an underscore as a prefix; "
886a6c8299Sjmmv               "found %s") % field);
896a6c8299Sjmmv 
906a6c8299Sjmmv     std::string tree_key;
916a6c8299Sjmmv     if (state.get_metafield(table_index, "tree_key")) {
926a6c8299Sjmmv         tree_key = state.to_string(-1) + "." + state.to_string(field_index - 1);
936a6c8299Sjmmv         state.pop(1);
946a6c8299Sjmmv     } else
956a6c8299Sjmmv         tree_key = state.to_string(field_index);
966a6c8299Sjmmv     return tree_key;
976a6c8299Sjmmv }
986a6c8299Sjmmv 
996a6c8299Sjmmv 
1006a6c8299Sjmmv static int redirect_newindex(lutok::state&);
1016a6c8299Sjmmv static int redirect_index(lutok::state&);
1026a6c8299Sjmmv 
1036a6c8299Sjmmv 
1046a6c8299Sjmmv /// Creates a table for a new configuration inner node.
1056a6c8299Sjmmv ///
1066a6c8299Sjmmv /// \post state(-1) Contains the new table.
1076a6c8299Sjmmv ///
1086a6c8299Sjmmv /// \param state The Lua state in which to push the table.
1096a6c8299Sjmmv /// \param tree_key The key to which the new table corresponds.
1106a6c8299Sjmmv static void
new_table_for_key(lutok::state & state,const std::string & tree_key)1116a6c8299Sjmmv new_table_for_key(lutok::state& state, const std::string& tree_key)
1126a6c8299Sjmmv {
1136a6c8299Sjmmv     state.new_table();
1146a6c8299Sjmmv     {
1156a6c8299Sjmmv         state.new_table();
1166a6c8299Sjmmv         {
1176a6c8299Sjmmv             state.push_string("__index");
1186a6c8299Sjmmv             state.push_cxx_function(redirect_index);
1196a6c8299Sjmmv             state.set_table(-3);
1206a6c8299Sjmmv 
1216a6c8299Sjmmv             state.push_string("__newindex");
1226a6c8299Sjmmv             state.push_cxx_function(redirect_newindex);
1236a6c8299Sjmmv             state.set_table(-3);
1246a6c8299Sjmmv 
1256a6c8299Sjmmv             state.push_string("tree_key");
1266a6c8299Sjmmv             state.push_string(tree_key);
1276a6c8299Sjmmv             state.set_table(-3);
1286a6c8299Sjmmv         }
1296a6c8299Sjmmv         state.set_metatable(-2);
1306a6c8299Sjmmv     }
1316a6c8299Sjmmv }
1326a6c8299Sjmmv 
1336a6c8299Sjmmv 
1346a6c8299Sjmmv /// Sets the value of an configuration node.
1356a6c8299Sjmmv ///
1366a6c8299Sjmmv /// \pre state(-3) The table to index.  If this is not _G, then the table
1376a6c8299Sjmmv ///     metadata must contain a tree_key property describing the path to
1386a6c8299Sjmmv ///     current level.
1396a6c8299Sjmmv /// \pre state(-2) The field to index into the table.  Must be a string.
1406a6c8299Sjmmv /// \pre state(-1) The value to set the indexed table field to.
1416a6c8299Sjmmv ///
1426a6c8299Sjmmv /// \param state The Lua state in which to operate.
1436a6c8299Sjmmv ///
1446a6c8299Sjmmv /// \return The number of result values on the Lua stack; always 0.
1456a6c8299Sjmmv ///
1466a6c8299Sjmmv /// \throw invalid_key_error If the provided key is invalid.
1476a6c8299Sjmmv /// \throw unknown_key_error If the key cannot be located.
1486a6c8299Sjmmv /// \throw value_error If the value has an unsupported type or cannot be
1496a6c8299Sjmmv ///     set on the key, or if the input table or index are invalid.
1506a6c8299Sjmmv static int
redirect_newindex(lutok::state & state)1516a6c8299Sjmmv redirect_newindex(lutok::state& state)
1526a6c8299Sjmmv {
1536a6c8299Sjmmv     if (!state.is_table(-3))
1546a6c8299Sjmmv         throw config::value_error("Indexed object is not a table");
1556a6c8299Sjmmv     if (!state.is_string(-2))
1566a6c8299Sjmmv         throw config::value_error("Invalid field in configuration object "
1576a6c8299Sjmmv                                   "reference; must be a string");
1586a6c8299Sjmmv 
1596a6c8299Sjmmv     const std::string dotted_key = get_tree_key(state, -3, -2);
1606a6c8299Sjmmv     try {
1616a6c8299Sjmmv         config::tree& tree = get_global_tree(state);
1626a6c8299Sjmmv         tree.set_lua(dotted_key, state, -1);
1636a6c8299Sjmmv     } catch (const config::value_error& e) {
1646a6c8299Sjmmv         throw config::value_error(F("Invalid value for key '%s' (%s)") %
1656a6c8299Sjmmv                                   dotted_key % e.what());
1666a6c8299Sjmmv     }
1676a6c8299Sjmmv 
1686a6c8299Sjmmv     // Now really set the key in the Lua table, but prevent direct accesses from
1696a6c8299Sjmmv     // the user by prefixing it.  We do this to ensure that re-setting the same
1706a6c8299Sjmmv     // key of the tree results in a call to __newindex instead of __index.
1716a6c8299Sjmmv     state.push_string("_" + state.to_string(-2));
1726a6c8299Sjmmv     state.push_value(-2);
1736a6c8299Sjmmv     state.raw_set(-5);
1746a6c8299Sjmmv 
1756a6c8299Sjmmv     return 0;
1766a6c8299Sjmmv }
1776a6c8299Sjmmv 
1786a6c8299Sjmmv 
1796a6c8299Sjmmv /// Indexes a configuration node.
1806a6c8299Sjmmv ///
1816a6c8299Sjmmv /// \pre state(-3) The table to index.  If this is not _G, then the table
1826a6c8299Sjmmv ///     metadata must contain a tree_key property describing the path to
1836a6c8299Sjmmv ///     current level.  If the field does not exist, a new table is created.
1846a6c8299Sjmmv /// \pre state(-1) The field to index into the table.  Must be a string.
1856a6c8299Sjmmv ///
1866a6c8299Sjmmv /// \param state The Lua state in which to operate.
1876a6c8299Sjmmv ///
1886a6c8299Sjmmv /// \return The number of result values on the Lua stack; always 1.
1896a6c8299Sjmmv ///
1906a6c8299Sjmmv /// \throw value_error If the input table or index are invalid.
1916a6c8299Sjmmv static int
redirect_index(lutok::state & state)1926a6c8299Sjmmv redirect_index(lutok::state& state)
1936a6c8299Sjmmv {
1946a6c8299Sjmmv     if (!state.is_table(-2))
1956a6c8299Sjmmv         throw config::value_error("Indexed object is not a table");
1966a6c8299Sjmmv     if (!state.is_string(-1))
1976a6c8299Sjmmv         throw config::value_error("Invalid field in configuration object "
1986a6c8299Sjmmv                                   "reference; must be a string");
1996a6c8299Sjmmv 
2006a6c8299Sjmmv     // Query if the key has already been set by a call to redirect_newindex.
2016a6c8299Sjmmv     state.push_string("_" + state.to_string(-1));
2026a6c8299Sjmmv     state.raw_get(-3);
2036a6c8299Sjmmv     if (!state.is_nil(-1))
2046a6c8299Sjmmv         return 1;
2056a6c8299Sjmmv     state.pop(1);
2066a6c8299Sjmmv 
2076a6c8299Sjmmv     state.push_value(-1);  // Duplicate the field name.
2086a6c8299Sjmmv     state.raw_get(-3);  // Get table[field] to see if it's defined.
2096a6c8299Sjmmv     if (state.is_nil(-1)) {
2106a6c8299Sjmmv         state.pop(1);
2116a6c8299Sjmmv 
2126a6c8299Sjmmv         // The stack is now the same as when we entered the function, but we
2136a6c8299Sjmmv         // know that the field is undefined and thus have to create a new
2146a6c8299Sjmmv         // configuration table.
2156a6c8299Sjmmv         INV(state.is_table(-2));
2166a6c8299Sjmmv         INV(state.is_string(-1));
2176a6c8299Sjmmv 
2186a6c8299Sjmmv         const config::tree& tree = get_global_tree(state);
2196a6c8299Sjmmv         const std::string tree_key = get_tree_key(state, -2, -1);
2206a6c8299Sjmmv         if (tree.is_set(tree_key)) {
2216a6c8299Sjmmv             // Publish the pre-recorded value in the tree to the Lua state,
2226a6c8299Sjmmv             // instead of considering this table key a new inner node.
2236a6c8299Sjmmv             tree.push_lua(tree_key, state);
2246a6c8299Sjmmv         } else {
2256a6c8299Sjmmv             state.push_string("_" + state.to_string(-1));
2266a6c8299Sjmmv             state.insert(-2);
2276a6c8299Sjmmv             state.pop(1);
2286a6c8299Sjmmv 
2296a6c8299Sjmmv             new_table_for_key(state, tree_key);
2306a6c8299Sjmmv 
2316a6c8299Sjmmv             // Duplicate the newly created table and place it deep in the stack
2326a6c8299Sjmmv             // so that the raw_set below leaves us with the return value of this
2336a6c8299Sjmmv             // function at the top of the stack.
2346a6c8299Sjmmv             state.push_value(-1);
2356a6c8299Sjmmv             state.insert(-4);
2366a6c8299Sjmmv 
2376a6c8299Sjmmv             state.raw_set(-3);
2386a6c8299Sjmmv             state.pop(1);
2396a6c8299Sjmmv         }
2406a6c8299Sjmmv     }
2416a6c8299Sjmmv     return 1;
2426a6c8299Sjmmv }
2436a6c8299Sjmmv 
2446a6c8299Sjmmv 
2456a6c8299Sjmmv }  // anonymous namespace
2466a6c8299Sjmmv 
2476a6c8299Sjmmv 
2486a6c8299Sjmmv /// Install wrappers for globals to set values in the configuration tree.
2496a6c8299Sjmmv ///
2506a6c8299Sjmmv /// This function installs wrappers to capture all accesses to global variables.
2516a6c8299Sjmmv /// Such wrappers redirect the reads and writes to the out_tree, which is the
2526a6c8299Sjmmv /// entity that defines what configuration variables exist.
2536a6c8299Sjmmv ///
2546a6c8299Sjmmv /// \param state The Lua state into which to install the wrappers.
2556a6c8299Sjmmv /// \param out_tree The tree with the layout definition and where the
2566a6c8299Sjmmv ///     configuration settings will be collected.
2576a6c8299Sjmmv void
redirect(lutok::state & state,tree & out_tree)2586a6c8299Sjmmv config::redirect(lutok::state& state, tree& out_tree)
2596a6c8299Sjmmv {
2606a6c8299Sjmmv     lutok::stack_cleaner cleaner(state);
2616a6c8299Sjmmv 
262*84dd42a1Sjmmv     state.get_global_table();
2636a6c8299Sjmmv     {
2646a6c8299Sjmmv         state.push_string("__index");
2656a6c8299Sjmmv         state.push_cxx_function(redirect_index);
2666a6c8299Sjmmv         state.set_table(-3);
2676a6c8299Sjmmv 
2686a6c8299Sjmmv         state.push_string("__newindex");
2696a6c8299Sjmmv         state.push_cxx_function(redirect_newindex);
2706a6c8299Sjmmv         state.set_table(-3);
271*84dd42a1Sjmmv     }
272*84dd42a1Sjmmv     state.set_metatable(-1);
2736a6c8299Sjmmv 
274*84dd42a1Sjmmv     state.push_value(lutok::registry_index);
2756a6c8299Sjmmv     state.push_string("tree");
2766a6c8299Sjmmv     config::tree** tree = state.new_userdata< config::tree* >();
2776a6c8299Sjmmv     *tree = &out_tree;
2786a6c8299Sjmmv     state.set_table(-3);
279*84dd42a1Sjmmv     state.pop(1);
2806a6c8299Sjmmv }
281