webmcp
changeset 259:41be09ce6aa3
Allow tables as sub-values of request variables (won't be cloned)
author | jbe |
---|---|
date | Sun Mar 15 01:14:21 2015 +0100 (2015-03-15) |
parents | 46f0083889a9 |
children | f491011ebe16 |
files | framework/env/request/add_variable.lua |
line diff
1.1 --- a/framework/env/request/add_variable.lua Sun Mar 15 01:08:00 2015 +0100 1.2 +++ b/framework/env/request/add_variable.lua Sun Mar 15 01:14:21 2015 +0100 1.3 @@ -5,18 +5,23 @@ 1.4 value -- optional value for initialization 1.5 ) 1.6 1.7 -Marks a field of a table to be re-initialized for every request. If this variable (i.e. the field of the table) is modified before the first requst is being handled (e.g. during configuration or pre-/post-fork initializers), then the modified value will be used for re-initialization on every request. See env/request/__init.lua for an example. 1.8 +Marks a field of a table to be re-initialized for every request. If this variable (i.e. the field of the table) is modified before the first requst is being handled (e.g. during configuration or pre-/post-fork initializers), then the modified value will be used for re-initialization on every request. If the (modified) value is a table, the table will be cloned on every request to avoid side-effects between different requests. Note, however, that only the first level of the table is cloned. See env/request/__init.lua for examples. 1.9 1.10 --]]-- 1.11 1.12 function request.add_variable(tbl, key, value) 1.13 - local initialized_value 1.14 + local initval, istable 1.15 tbl[key] = value 1.16 request.add_initializer(function(first) 1.17 if first then 1.18 - initialized_value = tbl[key] 1.19 + initval = tbl[key] 1.20 + istable = type(initval) == "table" 1.21 else 1.22 - tbl[key] = initialized_value 1.23 + if istable then 1.24 + tbl[key] = table.new(initval) 1.25 + else 1.26 + tbl[key] = initval 1.27 + end 1.28 end 1.29 end) 1.30 end