diff options
Diffstat (limited to 'testes/calls.lua')
-rw-r--r-- | testes/calls.lua | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/testes/calls.lua b/testes/calls.lua index 31028215..0dacb85a 100644 --- a/testes/calls.lua +++ b/testes/calls.lua | |||
@@ -1,5 +1,7 @@ | |||
1 | -- $Id: testes/calls.lua $ | 1 | -- $Id: testes/calls.lua $ |
2 | -- See Copyright Notice in file all.lua | 2 | -- See Copyright Notice in file lua.h |
3 | |||
4 | global <const> * | ||
3 | 5 | ||
4 | print("testing functions and calls") | 6 | print("testing functions and calls") |
5 | 7 | ||
@@ -22,7 +24,7 @@ assert(not pcall(type)) | |||
22 | 24 | ||
23 | 25 | ||
24 | -- testing local-function recursion | 26 | -- testing local-function recursion |
25 | fact = false | 27 | global fact = false |
26 | do | 28 | do |
27 | local res = 1 | 29 | local res = 1 |
28 | local function fact (n) | 30 | local function fact (n) |
@@ -63,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12) | |||
63 | 65 | ||
64 | print('+') | 66 | print('+') |
65 | 67 | ||
66 | t = nil -- 'declare' t | 68 | global t = nil -- 'declare' t |
67 | function f(a,b,c) local d = 'a'; t={a,b,c,d} end | 69 | function f(a,b,c) local d = 'a'; t={a,b,c,d} end |
68 | 70 | ||
69 | f( -- this line change must be valid | 71 | f( -- this line change must be valid |
@@ -75,7 +77,7 @@ assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') | |||
75 | 77 | ||
76 | t = nil -- delete 't' | 78 | t = nil -- delete 't' |
77 | 79 | ||
78 | function fat(x) | 80 | global function fat(x) |
79 | if x <= 1 then return 1 | 81 | if x <= 1 then return 1 |
80 | else return x*load("return fat(" .. x-1 .. ")", "")() | 82 | else return x*load("return fat(" .. x-1 .. ")", "")() |
81 | end | 83 | end |
@@ -107,7 +109,7 @@ end | |||
107 | 109 | ||
108 | _G.deep = nil -- "declaration" (used by 'all.lua') | 110 | _G.deep = nil -- "declaration" (used by 'all.lua') |
109 | 111 | ||
110 | function deep (n) | 112 | global function deep (n) |
111 | if n>0 then deep(n-1) end | 113 | if n>0 then deep(n-1) end |
112 | end | 114 | end |
113 | deep(10) | 115 | deep(10) |
@@ -352,7 +354,7 @@ assert(not load(function () return true end)) | |||
352 | 354 | ||
353 | -- small bug | 355 | -- small bug |
354 | local t = {nil, "return ", "3"} | 356 | local t = {nil, "return ", "3"} |
355 | f, msg = load(function () return table.remove(t, 1) end) | 357 | local f, msg = load(function () return table.remove(t, 1) end) |
356 | assert(f() == nil) -- should read the empty chunk | 358 | assert(f() == nil) -- should read the empty chunk |
357 | 359 | ||
358 | -- another small bug (in 5.2.1) | 360 | -- another small bug (in 5.2.1) |
@@ -388,7 +390,8 @@ assert(load("return _ENV", nil, nil, 123)() == 123) | |||
388 | 390 | ||
389 | 391 | ||
390 | -- load when _ENV is not first upvalue | 392 | -- load when _ENV is not first upvalue |
391 | local x; XX = 123 | 393 | global XX; local x |
394 | XX = 123 | ||
392 | local function h () | 395 | local function h () |
393 | local y=x -- use 'x', so that it becomes 1st upvalue | 396 | local y=x -- use 'x', so that it becomes 1st upvalue |
394 | return XX -- global name | 397 | return XX -- global name |
@@ -480,15 +483,22 @@ assert((function (a) return a end)() == nil) | |||
480 | 483 | ||
481 | print("testing binary chunks") | 484 | print("testing binary chunks") |
482 | do | 485 | do |
483 | local header = string.pack("c4BBc6BBB", | 486 | local headformat = "c4BBc6BiBI4BjBn" |
484 | "\27Lua", -- signature | 487 | local header = { -- header components |
485 | 0x55, -- version 5.5 (0x55) | 488 | "\27Lua", -- signature |
486 | 0, -- format | 489 | 0x55, -- version 5.5 (0x55) |
487 | "\x19\x93\r\n\x1a\n", -- data | 490 | 0, -- format |
488 | 4, -- size of instruction | 491 | "\x19\x93\r\n\x1a\n", -- a binary string |
489 | string.packsize("j"), -- sizeof(lua integer) | 492 | string.packsize("i"), -- size of an int |
490 | string.packsize("n") -- sizeof(lua number) | 493 | -0x5678, -- an int |
491 | ) | 494 | 4, -- size of an instruction |
495 | 0x12345678, -- an instruction (4 bytes) | ||
496 | string.packsize("j"), -- size of a Lua integer | ||
497 | -0x5678, -- a Lua integer | ||
498 | string.packsize("n"), -- size of a Lua float | ||
499 | -370.5, -- a Lua float | ||
500 | } | ||
501 | |||
492 | local c = string.dump(function () | 502 | local c = string.dump(function () |
493 | local a = 1; local b = 3; | 503 | local a = 1; local b = 3; |
494 | local f = function () return a + b + _ENV.c; end -- upvalues | 504 | local f = function () return a + b + _ENV.c; end -- upvalues |
@@ -500,17 +510,23 @@ do | |||
500 | assert(assert(load(c))() == 10) | 510 | assert(assert(load(c))() == 10) |
501 | 511 | ||
502 | -- check header | 512 | -- check header |
503 | assert(string.sub(c, 1, #header) == header) | 513 | local t = {string.unpack(headformat, c)} |
504 | -- check LUAC_INT and LUAC_NUM | ||
505 | local ci, cn = string.unpack("jn", c, #header + 1) | ||
506 | assert(ci == 0x5678 and cn == 370.5) | ||
507 | |||
508 | -- corrupted header | ||
509 | for i = 1, #header do | 514 | for i = 1, #header do |
515 | assert(t[i] == header[i]) | ||
516 | end | ||
517 | |||
518 | -- Testing corrupted header. | ||
519 | -- A single wrong byte in the head invalidates the chunk, | ||
520 | -- except for the Lua float check. (If numbers are long double, | ||
521 | -- the representation may need padding, and changing that padding | ||
522 | -- will not invalidate the chunk.) | ||
523 | local headlen = string.packsize(headformat) | ||
524 | headlen = headlen - string.packsize("n") -- remove float check | ||
525 | for i = 1, headlen do | ||
510 | local s = string.sub(c, 1, i - 1) .. | 526 | local s = string.sub(c, 1, i - 1) .. |
511 | string.char(string.byte(string.sub(c, i, i)) + 1) .. | 527 | string.char((string.byte(string.sub(c, i, i)) + 1) & 0xFF) .. |
512 | string.sub(c, i + 1, -1) | 528 | string.sub(c, i + 1, -1) |
513 | assert(#s == #c) | 529 | assert(#s == #c and s ~= c) |
514 | assert(not load(s)) | 530 | assert(not load(s)) |
515 | end | 531 | end |
516 | 532 | ||