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