xref: /freebsd/contrib/kyua/utils/config/lua_module.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2012 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "utils/config/lua_module.hpp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis #include <lutok/stack_cleaner.hpp>
32*b0d29bc4SBrooks Davis #include <lutok/state.ipp>
33*b0d29bc4SBrooks Davis 
34*b0d29bc4SBrooks Davis #include "utils/config/exceptions.hpp"
35*b0d29bc4SBrooks Davis #include "utils/config/keys.hpp"
36*b0d29bc4SBrooks Davis #include "utils/config/tree.ipp"
37*b0d29bc4SBrooks Davis 
38*b0d29bc4SBrooks Davis namespace config = utils::config;
39*b0d29bc4SBrooks Davis namespace detail = utils::config::detail;
40*b0d29bc4SBrooks Davis 
41*b0d29bc4SBrooks Davis 
42*b0d29bc4SBrooks Davis namespace {
43*b0d29bc4SBrooks Davis 
44*b0d29bc4SBrooks Davis 
45*b0d29bc4SBrooks Davis /// Gets the tree singleton stored in the Lua state.
46*b0d29bc4SBrooks Davis ///
47*b0d29bc4SBrooks Davis /// \param state The Lua state.  The registry must contain a key named
48*b0d29bc4SBrooks Davis ///    "tree" with a pointer to the singleton.
49*b0d29bc4SBrooks Davis ///
50*b0d29bc4SBrooks Davis /// \return A reference to the tree associated with the Lua state.
51*b0d29bc4SBrooks Davis ///
52*b0d29bc4SBrooks Davis /// \throw syntax_error If the tree cannot be located.
53*b0d29bc4SBrooks Davis config::tree&
get_global_tree(lutok::state & state)54*b0d29bc4SBrooks Davis get_global_tree(lutok::state& state)
55*b0d29bc4SBrooks Davis {
56*b0d29bc4SBrooks Davis     lutok::stack_cleaner cleaner(state);
57*b0d29bc4SBrooks Davis 
58*b0d29bc4SBrooks Davis     state.push_value(lutok::registry_index);
59*b0d29bc4SBrooks Davis     state.push_string("tree");
60*b0d29bc4SBrooks Davis     state.get_table(-2);
61*b0d29bc4SBrooks Davis     if (state.is_nil(-1))
62*b0d29bc4SBrooks Davis         throw config::syntax_error("Cannot find tree singleton; global state "
63*b0d29bc4SBrooks Davis                                    "corrupted?");
64*b0d29bc4SBrooks Davis     config::tree& tree = **state.to_userdata< config::tree* >(-1);
65*b0d29bc4SBrooks Davis     state.pop(1);
66*b0d29bc4SBrooks Davis     return tree;
67*b0d29bc4SBrooks Davis }
68*b0d29bc4SBrooks Davis 
69*b0d29bc4SBrooks Davis 
70*b0d29bc4SBrooks Davis /// Gets a fully-qualified tree key from the state.
71*b0d29bc4SBrooks Davis ///
72*b0d29bc4SBrooks Davis /// \param state The Lua state.
73*b0d29bc4SBrooks Davis /// \param table_index An index to the Lua stack pointing to the table being
74*b0d29bc4SBrooks Davis ///     accessed.  If this table contains a tree_key metadata property, this is
75*b0d29bc4SBrooks Davis ///     considered to be the prefix of the tree key.
76*b0d29bc4SBrooks Davis /// \param field_index An index to the Lua stack pointing to the entry
77*b0d29bc4SBrooks Davis ///     containing the name of the field being indexed.
78*b0d29bc4SBrooks Davis ///
79*b0d29bc4SBrooks Davis /// \return A dotted key.
80*b0d29bc4SBrooks Davis ///
81*b0d29bc4SBrooks Davis /// \throw invalid_key_error If the name of the key is invalid.
82*b0d29bc4SBrooks Davis static std::string
get_tree_key(lutok::state & state,const int table_index,const int field_index)83*b0d29bc4SBrooks Davis get_tree_key(lutok::state& state, const int table_index, const int field_index)
84*b0d29bc4SBrooks Davis {
85*b0d29bc4SBrooks Davis     PRE(state.is_string(field_index));
86*b0d29bc4SBrooks Davis     const std::string field = state.to_string(field_index);
87*b0d29bc4SBrooks Davis     if (!field.empty() && field[0] == '_')
88*b0d29bc4SBrooks Davis         throw config::invalid_key_error(
89*b0d29bc4SBrooks Davis             F("Configuration key cannot have an underscore as a prefix; "
90*b0d29bc4SBrooks Davis               "found %s") % field);
91*b0d29bc4SBrooks Davis 
92*b0d29bc4SBrooks Davis     std::string tree_key;
93*b0d29bc4SBrooks Davis     if (state.get_metafield(table_index, "tree_key")) {
94*b0d29bc4SBrooks Davis         tree_key = state.to_string(-1) + "." + state.to_string(field_index - 1);
95*b0d29bc4SBrooks Davis         state.pop(1);
96*b0d29bc4SBrooks Davis     } else
97*b0d29bc4SBrooks Davis         tree_key = state.to_string(field_index);
98*b0d29bc4SBrooks Davis     return tree_key;
99*b0d29bc4SBrooks Davis }
100*b0d29bc4SBrooks Davis 
101*b0d29bc4SBrooks Davis 
102*b0d29bc4SBrooks Davis static int redirect_newindex(lutok::state&);
103*b0d29bc4SBrooks Davis static int redirect_index(lutok::state&);
104*b0d29bc4SBrooks Davis 
105*b0d29bc4SBrooks Davis 
106*b0d29bc4SBrooks Davis /// Creates a table for a new configuration inner node.
107*b0d29bc4SBrooks Davis ///
108*b0d29bc4SBrooks Davis /// \post state(-1) Contains the new table.
109*b0d29bc4SBrooks Davis ///
110*b0d29bc4SBrooks Davis /// \param state The Lua state in which to push the table.
111*b0d29bc4SBrooks Davis /// \param tree_key The key to which the new table corresponds.
112*b0d29bc4SBrooks Davis static void
new_table_for_key(lutok::state & state,const std::string & tree_key)113*b0d29bc4SBrooks Davis new_table_for_key(lutok::state& state, const std::string& tree_key)
114*b0d29bc4SBrooks Davis {
115*b0d29bc4SBrooks Davis     state.new_table();
116*b0d29bc4SBrooks Davis     {
117*b0d29bc4SBrooks Davis         state.new_table();
118*b0d29bc4SBrooks Davis         {
119*b0d29bc4SBrooks Davis             state.push_string("__index");
120*b0d29bc4SBrooks Davis             state.push_cxx_function(redirect_index);
121*b0d29bc4SBrooks Davis             state.set_table(-3);
122*b0d29bc4SBrooks Davis 
123*b0d29bc4SBrooks Davis             state.push_string("__newindex");
124*b0d29bc4SBrooks Davis             state.push_cxx_function(redirect_newindex);
125*b0d29bc4SBrooks Davis             state.set_table(-3);
126*b0d29bc4SBrooks Davis 
127*b0d29bc4SBrooks Davis             state.push_string("tree_key");
128*b0d29bc4SBrooks Davis             state.push_string(tree_key);
129*b0d29bc4SBrooks Davis             state.set_table(-3);
130*b0d29bc4SBrooks Davis         }
131*b0d29bc4SBrooks Davis         state.set_metatable(-2);
132*b0d29bc4SBrooks Davis     }
133*b0d29bc4SBrooks Davis }
134*b0d29bc4SBrooks Davis 
135*b0d29bc4SBrooks Davis 
136*b0d29bc4SBrooks Davis /// Sets the value of an configuration node.
137*b0d29bc4SBrooks Davis ///
138*b0d29bc4SBrooks Davis /// \pre state(-3) The table to index.  If this is not _G, then the table
139*b0d29bc4SBrooks Davis ///     metadata must contain a tree_key property describing the path to
140*b0d29bc4SBrooks Davis ///     current level.
141*b0d29bc4SBrooks Davis /// \pre state(-2) The field to index into the table.  Must be a string.
142*b0d29bc4SBrooks Davis /// \pre state(-1) The value to set the indexed table field to.
143*b0d29bc4SBrooks Davis ///
144*b0d29bc4SBrooks Davis /// \param state The Lua state in which to operate.
145*b0d29bc4SBrooks Davis ///
146*b0d29bc4SBrooks Davis /// \return The number of result values on the Lua stack; always 0.
147*b0d29bc4SBrooks Davis ///
148*b0d29bc4SBrooks Davis /// \throw invalid_key_error If the provided key is invalid.
149*b0d29bc4SBrooks Davis /// \throw unknown_key_error If the key cannot be located.
150*b0d29bc4SBrooks Davis /// \throw value_error If the value has an unsupported type or cannot be
151*b0d29bc4SBrooks Davis ///     set on the key, or if the input table or index are invalid.
152*b0d29bc4SBrooks Davis static int
redirect_newindex(lutok::state & state)153*b0d29bc4SBrooks Davis redirect_newindex(lutok::state& state)
154*b0d29bc4SBrooks Davis {
155*b0d29bc4SBrooks Davis     if (!state.is_table(-3))
156*b0d29bc4SBrooks Davis         throw config::value_error("Indexed object is not a table");
157*b0d29bc4SBrooks Davis     if (!state.is_string(-2))
158*b0d29bc4SBrooks Davis         throw config::value_error("Invalid field in configuration object "
159*b0d29bc4SBrooks Davis                                   "reference; must be a string");
160*b0d29bc4SBrooks Davis 
161*b0d29bc4SBrooks Davis     const std::string dotted_key = get_tree_key(state, -3, -2);
162*b0d29bc4SBrooks Davis     try {
163*b0d29bc4SBrooks Davis         config::tree& tree = get_global_tree(state);
164*b0d29bc4SBrooks Davis         tree.set_lua(dotted_key, state, -1);
165*b0d29bc4SBrooks Davis     } catch (const config::value_error& e) {
166*b0d29bc4SBrooks Davis         throw config::invalid_key_value(detail::parse_key(dotted_key),
167*b0d29bc4SBrooks Davis                                         e.what());
168*b0d29bc4SBrooks Davis     }
169*b0d29bc4SBrooks Davis 
170*b0d29bc4SBrooks Davis     // Now really set the key in the Lua table, but prevent direct accesses from
171*b0d29bc4SBrooks Davis     // the user by prefixing it.  We do this to ensure that re-setting the same
172*b0d29bc4SBrooks Davis     // key of the tree results in a call to __newindex instead of __index.
173*b0d29bc4SBrooks Davis     state.push_string("_" + state.to_string(-2));
174*b0d29bc4SBrooks Davis     state.push_value(-2);
175*b0d29bc4SBrooks Davis     state.raw_set(-5);
176*b0d29bc4SBrooks Davis 
177*b0d29bc4SBrooks Davis     return 0;
178*b0d29bc4SBrooks Davis }
179*b0d29bc4SBrooks Davis 
180*b0d29bc4SBrooks Davis 
181*b0d29bc4SBrooks Davis /// Indexes a configuration node.
182*b0d29bc4SBrooks Davis ///
183*b0d29bc4SBrooks Davis /// \pre state(-3) The table to index.  If this is not _G, then the table
184*b0d29bc4SBrooks Davis ///     metadata must contain a tree_key property describing the path to
185*b0d29bc4SBrooks Davis ///     current level.  If the field does not exist, a new table is created.
186*b0d29bc4SBrooks Davis /// \pre state(-1) The field to index into the table.  Must be a string.
187*b0d29bc4SBrooks Davis ///
188*b0d29bc4SBrooks Davis /// \param state The Lua state in which to operate.
189*b0d29bc4SBrooks Davis ///
190*b0d29bc4SBrooks Davis /// \return The number of result values on the Lua stack; always 1.
191*b0d29bc4SBrooks Davis ///
192*b0d29bc4SBrooks Davis /// \throw value_error If the input table or index are invalid.
193*b0d29bc4SBrooks Davis static int
redirect_index(lutok::state & state)194*b0d29bc4SBrooks Davis redirect_index(lutok::state& state)
195*b0d29bc4SBrooks Davis {
196*b0d29bc4SBrooks Davis     if (!state.is_table(-2))
197*b0d29bc4SBrooks Davis         throw config::value_error("Indexed object is not a table");
198*b0d29bc4SBrooks Davis     if (!state.is_string(-1))
199*b0d29bc4SBrooks Davis         throw config::value_error("Invalid field in configuration object "
200*b0d29bc4SBrooks Davis                                   "reference; must be a string");
201*b0d29bc4SBrooks Davis 
202*b0d29bc4SBrooks Davis     // Query if the key has already been set by a call to redirect_newindex.
203*b0d29bc4SBrooks Davis     state.push_string("_" + state.to_string(-1));
204*b0d29bc4SBrooks Davis     state.raw_get(-3);
205*b0d29bc4SBrooks Davis     if (!state.is_nil(-1))
206*b0d29bc4SBrooks Davis         return 1;
207*b0d29bc4SBrooks Davis     state.pop(1);
208*b0d29bc4SBrooks Davis 
209*b0d29bc4SBrooks Davis     state.push_value(-1);  // Duplicate the field name.
210*b0d29bc4SBrooks Davis     state.raw_get(-3);  // Get table[field] to see if it's defined.
211*b0d29bc4SBrooks Davis     if (state.is_nil(-1)) {
212*b0d29bc4SBrooks Davis         state.pop(1);
213*b0d29bc4SBrooks Davis 
214*b0d29bc4SBrooks Davis         // The stack is now the same as when we entered the function, but we
215*b0d29bc4SBrooks Davis         // know that the field is undefined and thus have to create a new
216*b0d29bc4SBrooks Davis         // configuration table.
217*b0d29bc4SBrooks Davis         INV(state.is_table(-2));
218*b0d29bc4SBrooks Davis         INV(state.is_string(-1));
219*b0d29bc4SBrooks Davis 
220*b0d29bc4SBrooks Davis         const config::tree& tree = get_global_tree(state);
221*b0d29bc4SBrooks Davis         const std::string tree_key = get_tree_key(state, -2, -1);
222*b0d29bc4SBrooks Davis         if (tree.is_set(tree_key)) {
223*b0d29bc4SBrooks Davis             // Publish the pre-recorded value in the tree to the Lua state,
224*b0d29bc4SBrooks Davis             // instead of considering this table key a new inner node.
225*b0d29bc4SBrooks Davis             tree.push_lua(tree_key, state);
226*b0d29bc4SBrooks Davis         } else {
227*b0d29bc4SBrooks Davis             state.push_string("_" + state.to_string(-1));
228*b0d29bc4SBrooks Davis             state.insert(-2);
229*b0d29bc4SBrooks Davis             state.pop(1);
230*b0d29bc4SBrooks Davis 
231*b0d29bc4SBrooks Davis             new_table_for_key(state, tree_key);
232*b0d29bc4SBrooks Davis 
233*b0d29bc4SBrooks Davis             // Duplicate the newly created table and place it deep in the stack
234*b0d29bc4SBrooks Davis             // so that the raw_set below leaves us with the return value of this
235*b0d29bc4SBrooks Davis             // function at the top of the stack.
236*b0d29bc4SBrooks Davis             state.push_value(-1);
237*b0d29bc4SBrooks Davis             state.insert(-4);
238*b0d29bc4SBrooks Davis 
239*b0d29bc4SBrooks Davis             state.raw_set(-3);
240*b0d29bc4SBrooks Davis             state.pop(1);
241*b0d29bc4SBrooks Davis         }
242*b0d29bc4SBrooks Davis     }
243*b0d29bc4SBrooks Davis     return 1;
244*b0d29bc4SBrooks Davis }
245*b0d29bc4SBrooks Davis 
246*b0d29bc4SBrooks Davis 
247*b0d29bc4SBrooks Davis }  // anonymous namespace
248*b0d29bc4SBrooks Davis 
249*b0d29bc4SBrooks Davis 
250*b0d29bc4SBrooks Davis /// Install wrappers for globals to set values in the configuration tree.
251*b0d29bc4SBrooks Davis ///
252*b0d29bc4SBrooks Davis /// This function installs wrappers to capture all accesses to global variables.
253*b0d29bc4SBrooks Davis /// Such wrappers redirect the reads and writes to the out_tree, which is the
254*b0d29bc4SBrooks Davis /// entity that defines what configuration variables exist.
255*b0d29bc4SBrooks Davis ///
256*b0d29bc4SBrooks Davis /// \param state The Lua state into which to install the wrappers.
257*b0d29bc4SBrooks Davis /// \param out_tree The tree with the layout definition and where the
258*b0d29bc4SBrooks Davis ///     configuration settings will be collected.
259*b0d29bc4SBrooks Davis void
redirect(lutok::state & state,tree & out_tree)260*b0d29bc4SBrooks Davis config::redirect(lutok::state& state, tree& out_tree)
261*b0d29bc4SBrooks Davis {
262*b0d29bc4SBrooks Davis     lutok::stack_cleaner cleaner(state);
263*b0d29bc4SBrooks Davis 
264*b0d29bc4SBrooks Davis     state.get_global_table();
265*b0d29bc4SBrooks Davis     {
266*b0d29bc4SBrooks Davis         state.push_string("__index");
267*b0d29bc4SBrooks Davis         state.push_cxx_function(redirect_index);
268*b0d29bc4SBrooks Davis         state.set_table(-3);
269*b0d29bc4SBrooks Davis 
270*b0d29bc4SBrooks Davis         state.push_string("__newindex");
271*b0d29bc4SBrooks Davis         state.push_cxx_function(redirect_newindex);
272*b0d29bc4SBrooks Davis         state.set_table(-3);
273*b0d29bc4SBrooks Davis     }
274*b0d29bc4SBrooks Davis     state.set_metatable(-1);
275*b0d29bc4SBrooks Davis 
276*b0d29bc4SBrooks Davis     state.push_value(lutok::registry_index);
277*b0d29bc4SBrooks Davis     state.push_string("tree");
278*b0d29bc4SBrooks Davis     config::tree** tree = state.new_userdata< config::tree* >();
279*b0d29bc4SBrooks Davis     *tree = &out_tree;
280*b0d29bc4SBrooks Davis     state.set_table(-3);
281*b0d29bc4SBrooks Davis     state.pop(1);
282*b0d29bc4SBrooks Davis }
283