Lines Matching refs:state

32 #include <lutok/state.ipp>
43 /// Gets the tree singleton stored in the Lua state.
45 /// \param state The Lua state. The registry must contain a key named
48 /// \return A reference to the tree associated with the Lua state.
52 get_global_tree(lutok::state& state)
54 lutok::stack_cleaner cleaner(state);
56 state.push_value(lutok::registry_index);
57 state.push_string("tree");
58 state.get_table(-2);
59 if (state.is_nil())
60 throw config::syntax_error("Cannot find tree singleton; global state "
62 config::tree& tree = **state.to_userdata< config::tree* >();
63 state.pop(1);
68 /// Gets a fully-qualified tree key from the state.
70 /// \param state The Lua state.
81 get_tree_key(lutok::state& state, const int table_index, const int field_index)
83 PRE(state.is_string(field_index));
84 const std::string field = state.to_string(field_index);
91 if (state.get_metafield(table_index, "tree_key")) {
92 tree_key = state.to_string(-1) + "." + state.to_string(field_index - 1);
93 state.pop(1);
95 tree_key = state.to_string(field_index);
100 static int redirect_newindex(lutok::state&);
101 static int redirect_index(lutok::state&);
106 /// \post state(-1) Contains the new table.
108 /// \param state The Lua state in which to push the table.
111 new_table_for_key(lutok::state& state, const std::string& tree_key)
113 state.new_table();
115 state.new_table();
117 state.push_string("__index");
118 state.push_cxx_function(redirect_index);
119 state.set_table(-3);
121 state.push_string("__newindex");
122 state.push_cxx_function(redirect_newindex);
123 state.set_table(-3);
125 state.push_string("tree_key");
126 state.push_string(tree_key);
127 state.set_table(-3);
129 state.set_metatable(-2);
136 /// \pre state(-3) The table to index. If this is not _G, then the table
139 /// \pre state(-2) The field to index into the table. Must be a string.
140 /// \pre state(-1) The value to set the indexed table field to.
142 /// \param state The Lua state in which to operate.
151 redirect_newindex(lutok::state& state)
153 if (!state.is_table(-3))
155 if (!state.is_string(-2))
159 const std::string dotted_key = get_tree_key(state, -3, -2);
161 config::tree& tree = get_global_tree(state);
162 tree.set_lua(dotted_key, state, -1);
171 state.push_string("_" + state.to_string(-2));
172 state.push_value(-2);
173 state.raw_set(-5);
181 /// \pre state(-3) The table to index. If this is not _G, then the table
184 /// \pre state(-1) The field to index into the table. Must be a string.
186 /// \param state The Lua state in which to operate.
192 redirect_index(lutok::state& state)
194 if (!state.is_table(-2))
196 if (!state.is_string(-1))
201 state.push_string("_" + state.to_string(-1));
202 state.raw_get(-3);
203 if (!state.is_nil(-1))
205 state.pop(1);
207 state.push_value(-1); // Duplicate the field name.
208 state.raw_get(-3); // Get table[field] to see if it's defined.
209 if (state.is_nil(-1)) {
210 state.pop(1);
215 INV(state.is_table(-2));
216 INV(state.is_string(-1));
218 const config::tree& tree = get_global_tree(state);
219 const std::string tree_key = get_tree_key(state, -2, -1);
221 // Publish the pre-recorded value in the tree to the Lua state,
223 tree.push_lua(tree_key, state);
225 state.push_string("_" + state.to_string(-1));
226 state.insert(-2);
227 state.pop(1);
229 new_table_for_key(state, tree_key);
234 state.push_value(-1);
235 state.insert(-4);
237 state.raw_set(-3);
238 state.pop(1);
254 /// \param state The Lua state into which to install the wrappers.
258 config::redirect(lutok::state& state, tree& out_tree)
260 lutok::stack_cleaner cleaner(state);
262 state.get_global_table();
264 state.push_string("__index");
265 state.push_cxx_function(redirect_index);
266 state.set_table(-3);
268 state.push_string("__newindex");
269 state.push_cxx_function(redirect_newindex);
270 state.set_table(-3);
272 state.set_metatable(-1);
274 state.push_value(lutok::registry_index);
275 state.push_string("tree");
276 config::tree** tree = state.new_userdata< config::tree* >();
278 state.set_table(-3);
279 state.pop(1);