Skip to content

Commit b3937df

Browse files
committed
Add real examples
- lua_load - luaL_loadbufferx - luaL_dostring - luaL_loadstring
1 parent b03202f commit b3937df

File tree

632 files changed

+47004
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

632 files changed

+47004
-0
lines changed

luaL_dostring/abs.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local abs = math.abs
2+
local expect_error = function() end
3+
4+
do --- smoke
5+
assert(abs(-1.5) == 1.5)
6+
assert(abs("-1.5") == 1.5)
7+
end
8+
9+
do --- argcheck
10+
expect_error(function() abs() end,
11+
"bad argument #1 to 'abs' (number expected, got no value)")
12+
expect_error(function() abs(false) end,
13+
"bad argument #1 to 'abs' (number expected, got boolean)")
14+
expect_error(function() abs("a") end,
15+
"bad argument #1 to 'abs' (number expected, got string)")
16+
end

luaL_dostring/alias_alloc.lua

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
do
3+
local t = {1}
4+
local x
5+
for i=1,100 do
6+
local v = {i}
7+
t[1] = v[1]
8+
x = v[1]
9+
end
10+
assert(x == 100 and t[1] == 100)
11+
end
12+
13+
do
14+
local t = {1}
15+
local x,y
16+
for i=1,100 do
17+
local v = {i}
18+
local w = {i+1}
19+
x = v[1]
20+
y = w[1]
21+
end
22+
assert(x == 100 and y == 101)
23+
end
24+
25+
do
26+
local mt = {}
27+
local t = setmetatable({}, mt)
28+
local x
29+
for i=1,100 do
30+
local v = {}
31+
setmetatable(v, getmetatable(t))
32+
assert(getmetatable(v) == mt)
33+
end
34+
end
35+
36+
-- See also sink_alloc.lua
37+
do
38+
local x,k={1,2},{3,4}
39+
for i=1,100 do x = {x[1]+k[1], x[2]+k[2]} end
40+
assert(x[1] == 301)
41+
assert(x[2] == 402)
42+
end
43+
44+
-- FLOAD for tab.asize/tab.array crossing NEWREF.
45+
do
46+
local t = {1}
47+
for i=1,100 do
48+
local v = {}
49+
local w = {}
50+
v[1] = t[1]
51+
w[1] = t[1]
52+
end
53+
end
54+

luaL_dostring/alloc.lua

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
local assert = assert
2+
3+
do --- DCE or sink trivial TNEW or TDUP.
4+
for i=1,100 do local t={} end
5+
for i=1,100 do local t={1} end
6+
end
7+
8+
do --- Sink TNEW/TDUP + ASTORE/HSTORE.
9+
for i=1,100 do local t={i}; assert(t[1] == i) end
10+
for i=1,100 do local t={foo=i}; assert(t.foo == i) end
11+
for i=1,100 do local t={1,i}; assert(t[2] == i) end
12+
for i=1,100 do local t={bar=1,foo=i}; assert(t.foo == i) end
13+
end
14+
15+
do --- Sink outermost table of nested TNEW.
16+
local x
17+
for i=1,100 do
18+
local t = {[0]={{1,i}}}
19+
if i == 90 then x = t end
20+
assert(t[0][1][2] == i)
21+
end
22+
assert(x[0][1][2] == 90)
23+
for i=1,100 do
24+
local t = {foo={bar={baz=i}}}
25+
if i == 90 then x = t end
26+
assert(t.foo.bar.baz == i)
27+
end
28+
assert(x.foo.bar.baz == 90)
29+
end
30+
31+
do --- Sink one TNEW + FSTORE.
32+
for i=1,100 do local t = setmetatable({}, {}) end
33+
end
34+
35+
do --- Sink TDUP or TDUP + HSTORE. Guard of HREFK eliminated.
36+
local x
37+
for i=1,100 do local t = { foo = 1 }; x = t.foo; end
38+
assert(x == 1)
39+
for i=1,100 do local t = { foo = i }; x = t.foo; end
40+
assert(x == 100)
41+
end
42+
43+
do --- Sink of simplified complex add, unused in next iteration, drop PHI.
44+
local x={1,2}
45+
for i=1,100 do x = {x[1]+3, x[2]+4} end
46+
assert(x[1] == 301)
47+
assert(x[2] == 402)
48+
end
49+
50+
do --- Sink of complex add, unused in next iteration, drop PHI.
51+
local x,k={1.5,2.5},{3.5,4.5}
52+
for i=1,100 do x = {x[1]+k[1], x[2]+k[2]} end
53+
assert(x[1] == 351.5)
54+
assert(x[2] == 452.5)
55+
end
56+
57+
do --- Sink of TDUP with stored values that are both PHI and non-PHI.
58+
local x,k={1,2},{3,4}
59+
for i=1,100 do x = {x[1]+k[1], k[2]} end
60+
assert(x[1] == 301)
61+
assert(x[2] == 4)
62+
end
63+
64+
do --- Sink of CONV.
65+
local t = {1}
66+
local x,y
67+
for i=1,200 do
68+
local v = {i}
69+
local w = {i+1}
70+
x = v[1]
71+
y = w[1]
72+
if i > 100 then end
73+
end
74+
assert(x == 200 and y == 201)
75+
end
76+
77+
do --- Sink of stores with numbers.
78+
local x = {1.5, 0}
79+
for i=1,200 do x = {x[1]+1, 99.5}; x[2]=4.5; if i > 100 then end end
80+
assert(x[1] == 201.5)
81+
assert(x[2] == 4.5)
82+
end
83+
84+
do --- Sink of stores with constants.
85+
for i=1,100 do local t = {false}; t[1] = true; if i > 100 then g=t end end
86+
end
87+
88+
do --- Sink with two references to the same table.
89+
for i=1,200 do
90+
local t = {i}
91+
local q = t
92+
if i > 100 then assert(t == q) end
93+
end
94+
end
95+
96+
do --- point
97+
local point
98+
point = {
99+
new = function(self, x, y)
100+
return setmetatable({x=x, y=y}, self)
101+
end,
102+
__add = function(a, b)
103+
return point:new(a.x + b.x, a.y + b.y)
104+
end,
105+
}
106+
point.__index = point
107+
local a, b = point:new(1, 1), point:new(2, 2)
108+
for i=1,100 do a = (a + b) + b end
109+
assert(a.x == 401)
110+
assert(a.y == 401)
111+
assert(getmetatable(a) == point)
112+
for i=1,200 do a = (a + b) + b; if i > 100 then end end
113+
assert(a.x == 1201)
114+
assert(a.y == 1201)
115+
assert(getmetatable(a) == point)
116+
end
117+
118+
do --- untitled
119+
local t = {}
120+
for i=1,20 do t[i] = 1 end
121+
for i=1,20 do
122+
for a,b in ipairs(t) do
123+
local s = {i}
124+
end
125+
end
126+
end

luaL_dostring/andor.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
do --- smoke
2+
local x = ((1 or false) and true) or false
3+
assert(x == true)
4+
end
5+
6+
do --- allcases
7+
local basiccases = {
8+
{"nil", nil},
9+
{"false", false},
10+
{"true", true},
11+
{"10", 10},
12+
}
13+
14+
local mem = {basiccases} -- for memoization
15+
16+
local function allcases (n)
17+
if mem[n] then return mem[n] end
18+
local res = {}
19+
-- include all smaller cases
20+
for _, v in ipairs(allcases(n - 1)) do
21+
res[#res + 1] = v
22+
end
23+
for i = 1, n - 1 do
24+
for _, v1 in ipairs(allcases(i)) do
25+
for _, v2 in ipairs(allcases(n - i)) do
26+
res[#res + 1] = {
27+
"(" .. v1[1] .. " and " .. v2[1] .. ")",
28+
v1[2] and v2[2]
29+
}
30+
res[#res + 1] = {
31+
"(" .. v1[1] .. " or " .. v2[1] .. ")",
32+
v1[2] or v2[2]
33+
}
34+
end
35+
end
36+
end
37+
mem[n] = res -- memoize
38+
return res
39+
end
40+
41+
for _, v in pairs(allcases(4)) do
42+
local res = (loadstring or load)("return " .. v[1])()
43+
if res ~= v[2] then
44+
error(string.format("bad conditional eval\n%s\nexpected: %s\ngot: %s",
45+
v[1], tostring(v[2]), tostring(res)))
46+
end
47+
end
48+
end
49+
50+
do --- tracefib
51+
-- 0001 KSHORT 1 2
52+
-- 0002 ISGE 0 1
53+
-- 0003 JMP 1 => 0006
54+
-- 0004 KSHORT 1 1
55+
-- 0005 JMP 1 => 0013
56+
-- ^^^ must be 2
57+
-- fix in jmp_patchtestreg
58+
local function fib(n) return (n < 2) and 1 or fib(n-1)+fib(n-2) end
59+
assert(fib(5) == 8)
60+
assert(fib(10) == 89)
61+
end

luaL_dostring/api_call.lua

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
-- local ctest = require("ctest")
2+
3+
local function ret0() end
4+
local function ret1() return 1 end
5+
local function ret2() return 1,2 end
6+
local function ret3() return 1,2,3 end
7+
local function retva(...) return ... end
8+
local function ret1va(...) return 1,... end
9+
10+
local function pack(...)
11+
return { n = select('#', ...), ... }
12+
end
13+
14+
local function ck(res, ...)
15+
local ok = pack(...)
16+
if res.n ~= ok.n then error("nresults wrong: "..res.n.." ~= "..ok.n, 2) end
17+
for i=1,res.n do
18+
if res[i] ~= ok[i] then
19+
error("result["..i.."] wrong: "..tostring(res[i]).." ~= "..tostring(ok[i]), 2)
20+
end
21+
end
22+
end
23+
24+
local function test_adjust_results(testfunc)
25+
26+
local function cc(nres, f, ...)
27+
return pack(testfunc(nres, f, ...))
28+
end
29+
30+
ck(cc(0, ret0))
31+
ck(cc(0, ret1))
32+
ck(cc(0, ret2))
33+
ck(cc(0, ret3))
34+
ck(cc(0, retva))
35+
36+
ck(cc(1, ret0), nil)
37+
ck(cc(1, ret1), 1)
38+
ck(cc(1, ret2), 1)
39+
ck(cc(1, ret3), 1)
40+
ck(cc(1, retva), nil)
41+
ck(cc(1, retva, 1), 1)
42+
43+
ck(cc(2, ret0), nil, nil)
44+
ck(cc(2, ret1), 1, nil)
45+
ck(cc(2, ret2), 1, 2)
46+
ck(cc(2, ret3), 1, 2)
47+
ck(cc(2, retva), nil, nil)
48+
ck(cc(2, retva, 1), 1, nil)
49+
ck(cc(2, retva, 1, 2), 1, 2)
50+
51+
ck(cc(-1, ret0))
52+
ck(cc(-1, ret1), 1)
53+
ck(cc(-1, ret2), 1, 2)
54+
ck(cc(-1, ret3), 1, 2, 3)
55+
ck(cc(-1, retva))
56+
ck(cc(-1, retva, 1), 1)
57+
ck(cc(-1, retva, 1, 2), 1, 2)
58+
end
59+
60+
-- test_adjust_results(ctest.call)
61+
-- test_adjust_results(ctest.pcall_err)
62+
63+
64+
local function gcshrink()
65+
for i=1,10 do collectgarbage() end
66+
end
67+
68+
-- assert(select('#', ctest.call(2000, gcshrink)) == 2000)
69+
gcshrink()
70+
-- assert(select('#', ctest.call(7000, gcshrink)) == 7000)
71+
gcshrink()
72+
73+
local function test_yield(resume, yield)
74+
local function inpcall()
75+
ck(pack(yield(6, 7)), 18, 19)
76+
end
77+
local co = coroutine.create(function(...)
78+
ck(pack(...), 11, 12)
79+
ck(pack(yield(1, 2)))
80+
ck(pack(yield()), 13, 14, 15)
81+
ck(pack(yield(3, 4, 5)), 16, 17)
82+
assert(pcall(inpcall) == true)
83+
return 8, 9
84+
end)
85+
86+
ck(pack(resume(co, 11, 12)), true, 1, 2)
87+
ck(pack(resume(co)), true)
88+
ck(pack(resume(co, 13, 14, 15)), true, 3, 4, 5)
89+
ck(pack(resume(co, 16, 17)), true, 6, 7)
90+
ck(pack(resume(co, 18, 19)), true, 8, 9)
91+
assert(resume(co) == false)
92+
end
93+
94+
test_yield(coroutine.resume, coroutine.yield)
95+
-- test_yield(ctest.resume, coroutine.yield)
96+
-- test_yield(coroutine.resume, ctest.yield)
97+
-- test_yield(ctest.resume, ctest.yield)
98+

0 commit comments

Comments
 (0)