@@ -20,13 +20,14 @@ print"If this test crashes, see its file ('cstack.lua')"
2020
2121
2222-- get and print original limit
23- local origlimit = debug.setcstacklimit (400 )
23+ local origlimit <const> = debug.setcstacklimit (400 )
2424print (" default stack limit: " .. origlimit )
2525
26+
2627-- Do the tests using the original limit. Or else you may want to change
2728-- 'currentlimit' to lower values to avoid a seg. fault or to higher
2829-- values to check whether they are reliable.
29- local currentlimit = origlimit
30+ local currentlimit <const> = origlimit
3031debug.setcstacklimit (currentlimit )
3132print (" current stack limit: " .. currentlimit )
3233
@@ -107,12 +108,16 @@ end
107108
108109do print (" testing changes in C-stack limit" )
109110
111+ -- Just an alternative limit, different from the current one
112+ -- (smaller to avoid stack overflows)
113+ local alterlimit <const> = currentlimit * 8 // 10
114+
110115 assert (not debug.setcstacklimit (0 )) -- limit too small
111116 assert (not debug.setcstacklimit (50000 )) -- limit too large
112117 local co = coroutine.wrap (function ()
113- return debug.setcstacklimit (400 )
118+ return debug.setcstacklimit (alterlimit )
114119 end )
115- assert (co () == false ) -- cannot change C stack inside coroutine
120+ assert (not co ()) -- cannot change C stack inside coroutine
116121
117122 local n
118123 local function foo () n = n + 1 ; foo () end
@@ -123,28 +128,32 @@ do print("testing changes in C-stack limit")
123128 return n
124129 end
125130
126- -- set limit to 400
127- assert (debug.setcstacklimit (400 ) == currentlimit )
128- local lim400 = check ()
131+ -- set limit to 'alterlimit'
132+ assert (debug.setcstacklimit (alterlimit ) == currentlimit )
133+ local limalter <const> = check ()
129134 -- set a very low limit (given that there are already several active
130135 -- calls to arrive here)
131- local lowlimit = 38
132- assert (debug.setcstacklimit (lowlimit ) == 400 )
133- assert (check () < lowlimit - 30 )
134- assert (debug.setcstacklimit (600 ) == lowlimit )
135- local lim600 = check ()
136- assert (lim600 == lim400 + 200 )
136+ local lowlimit <const> = 38
137+ assert (debug.setcstacklimit (lowlimit ) == alterlimit )
138+ -- usable limit is much lower, due to active calls
139+ local actuallow = check ()
140+ assert (actuallow < lowlimit - 30 )
141+ -- now, add 'lowlimit' extra slots, which should all be available
142+ assert (debug.setcstacklimit (lowlimit + lowlimit ) == lowlimit )
143+ local lim2 <const> = check ()
144+ assert (lim2 == actuallow + lowlimit )
137145
138146
139147 -- 'setcstacklimit' works inside protected calls. (The new stack
140148 -- limit is kept when 'pcall' returns.)
141149 assert (pcall (function ()
142- assert (debug.setcstacklimit (400 ) == 600 )
143- assert (check () <= lim400 )
150+ assert (debug.setcstacklimit (alterlimit ) == lowlimit * 2 )
151+ assert (check () <= limalter )
144152 end ))
145153
146- assert (check () == lim400 )
147- assert (debug.setcstacklimit (origlimit ) == 400 ) -- restore original limit
154+ assert (check () == limalter )
155+ -- restore original limit
156+ assert (debug.setcstacklimit (origlimit ) == alterlimit )
148157end
149158
150159
0 commit comments