comparison mod_storage_appendmap/mod_storage_appendmap.lua @ 2431:623e23190c3e

mod_storage_appendmap: Escape Lua keywords
author Kim Alvefur <zash@zash.se>
date Mon, 02 Jan 2017 21:04:36 +0100
parents a2625a36c092
children 4548c3d685b4
comparison
equal deleted inserted replaced
2430:a2625a36c092 2431:623e23190c3e
27 return env; 27 return env;
28 end 28 end
29 return env[key]; 29 return env[key];
30 end 30 end
31 31
32 local keywords = {
33 ["do"] = true; ["and"] = true; ["else"] = true; ["break"] = true;
34 ["if"] = true; ["end"] = true; ["goto"] = true; ["false"] = true;
35 ["in"] = true; ["for"] = true; ["then"] = true; ["local"] = true;
36 ["or"] = true; ["nil"] = true; ["true"] = true; ["until"] = true;
37 ["elseif"] = true; ["function"] = true; ["not"] = true;
38 ["repeat"] = true; ["return"] = true; ["while"] = true;
39
40 -- _ENV is not technically a keyword but we need to treat it as such
41 ["_ENV"] = true;
42 };
43
32 function map:set_keys(user, keyvalues) 44 function map:set_keys(user, keyvalues)
33 local keys, values = {}, {}; 45 local keys, values = {}, {};
34 if _VERSION == "Lua 5.1" then 46 if _VERSION == "Lua 5.1" then
35 assert(keyvalues._ENV == nil, "'_ENV' is a restricted key"); 47 assert(keyvalues._ENV == nil, "'_ENV' is a restricted key");
36 end 48 end
37 for key, value in pairs(keyvalues) do 49 for key, value in pairs(keyvalues) do
38 module:log("debug", "user %s sets %q to %s", user, key, tostring(value)) 50 module:log("debug", "user %s sets %q to %s", user, key, tostring(value))
39 if type(key) ~= "string" or not key:find("^[%a_][%w_]*$") or key == "_ENV" then 51 if type(key) ~= "string" or not key:find("^[%a_][%w_]*$") or keywords[key] then
40 key = "_ENV[" .. dump(key) .. "]"; 52 key = "_ENV[" .. dump(key) .. "]";
41 end 53 end
42 table.insert(keys, key); 54 table.insert(keys, key);
43 if value == self.remove then 55 if value == self.remove then
44 table.insert(values, "nil") 56 table.insert(values, "nil")