diff options
Diffstat (limited to 'testes/vararg.lua')
| -rw-r--r-- | testes/vararg.lua | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/testes/vararg.lua b/testes/vararg.lua index 92f720cb..5711f78b 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | print('testing vararg') | 4 | print('testing vararg') |
| 5 | 5 | ||
| 6 | local function f (a, ...=t) | 6 | local function f (a, ...|t) |
| 7 | local x = {n = select('#', ...), ...} | 7 | local x = {n = select('#', ...), ...} |
| 8 | assert(x.n == t.n) | 8 | assert(x.n == t.n) |
| 9 | for i = 1, x.n do | 9 | for i = 1, x.n do |
| @@ -20,7 +20,7 @@ local function c12 (...) | |||
| 20 | return res, 2 | 20 | return res, 2 |
| 21 | end | 21 | end |
| 22 | 22 | ||
| 23 | local function vararg (...=t) return t end | 23 | local function vararg (... | t) return t end |
| 24 | 24 | ||
| 25 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end | 25 | local call = function (f, args) return f(table.unpack(args, 1, args.n)) end |
| 26 | 26 | ||
| @@ -153,8 +153,8 @@ end | |||
| 153 | 153 | ||
| 154 | 154 | ||
| 155 | do -- vararg parameter used in nested functions | 155 | do -- vararg parameter used in nested functions |
| 156 | local function foo (... = tab1) | 156 | local function foo (... | tab1) |
| 157 | return function (... = tab2) | 157 | return function (... | tab2) |
| 158 | return {tab1, tab2} | 158 | return {tab1, tab2} |
| 159 | end | 159 | end |
| 160 | end | 160 | end |
| @@ -165,16 +165,51 @@ do -- vararg parameter used in nested functions | |||
| 165 | end | 165 | end |
| 166 | 166 | ||
| 167 | do -- vararg parameter is read-only | 167 | do -- vararg parameter is read-only |
| 168 | local st, msg = load("return function (... = t) t = 10 end") | 168 | local st, msg = load("return function (... | t) t = 10 end") |
| 169 | assert(string.find(msg, "const variable 't'")) | 169 | assert(string.find(msg, "const variable 't'")) |
| 170 | 170 | ||
| 171 | local st, msg = load[[ | 171 | local st, msg = load[[ |
| 172 | local function foo (... = extra) | 172 | local function foo (... | extra) |
| 173 | return function (...) extra = nil end | 173 | return function (...) extra = nil end |
| 174 | end | 174 | end |
| 175 | ]] | 175 | ]] |
| 176 | assert(string.find(msg, "const variable 'extra'")) | 176 | assert(string.find(msg, "const variable 'extra'")) |
| 177 | end | 177 | end |
| 178 | 178 | ||
| 179 | |||
| 180 | do -- _ENV as vararg parameter | ||
| 181 | local st, msg = load[[ | ||
| 182 | local function aux (... | _ENV) | ||
| 183 | global <const> a | ||
| 184 | a = 10 | ||
| 185 | end ]] | ||
| 186 | assert(string.find(msg, "const variable 'a'")) | ||
| 187 | end | ||
| 188 | |||
| 189 | |||
| 190 | do -- access to vararg parameter | ||
| 191 | local function notab (keys, t, ... | v) | ||
| 192 | for _, k in pairs(keys) do | ||
| 193 | assert(t[k] == v[k]) | ||
| 194 | end | ||
| 195 | assert(t.n == v.n) | ||
| 196 | end | ||
| 197 | |||
| 198 | local t = table.pack(10, 20, 30) | ||
| 199 | local keys = {-1, 0, 1, t.n, t.n + 1, 1.0, 1.1, "n", print, "k", "1"} | ||
| 200 | notab(keys, t, 10, 20, 30) -- ensure stack space | ||
| 201 | local m = collectgarbage"count" | ||
| 202 | notab(keys, t, 10, 20, 30) | ||
| 203 | -- 'notab' does not create any table/object | ||
| 204 | assert(m == collectgarbage"count") | ||
| 205 | |||
| 206 | -- writing to the vararg table | ||
| 207 | local function foo (... | t) | ||
| 208 | t[1] = t[1] + 10 | ||
| 209 | return t[1] | ||
| 210 | end | ||
| 211 | assert(foo(10, 30) == 20) | ||
| 212 | end | ||
| 213 | |||
| 179 | print('OK') | 214 | print('OK') |
| 180 | 215 | ||
