aboutsummaryrefslogtreecommitdiff
path: root/testes/calls.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/calls.lua')
-rw-r--r--testes/calls.lua64
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
4global <const> *
3 5
4print("testing functions and calls") 6print("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
25fact = false 27global fact = false
26do 28do
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
64print('+') 66print('+')
65 67
66t = nil -- 'declare' t 68global t = nil -- 'declare' t
67function f(a,b,c) local d = 'a'; t={a,b,c,d} end 69function f(a,b,c) local d = 'a'; t={a,b,c,d} end
68 70
69f( -- this line change must be valid 71f( -- 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
76t = nil -- delete 't' 78t = nil -- delete 't'
77 79
78function fat(x) 80global 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
110function deep (n) 112global function deep (n)
111 if n>0 then deep(n-1) end 113 if n>0 then deep(n-1) end
112end 114end
113deep(10) 115deep(10)
@@ -352,7 +354,7 @@ assert(not load(function () return true end))
352 354
353-- small bug 355-- small bug
354local t = {nil, "return ", "3"} 356local t = {nil, "return ", "3"}
355f, msg = load(function () return table.remove(t, 1) end) 357local f, msg = load(function () return table.remove(t, 1) end)
356assert(f() == nil) -- should read the empty chunk 358assert(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
391local x; XX = 123 393global XX; local x
394XX = 123
392local function h () 395local 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
481print("testing binary chunks") 484print("testing binary chunks")
482do 485do
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