Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pop / clean Lua call stack from C

Tags:

c

lua

cunit

I want to pop / clean the Lua call stack while within a C function called from Lua. Is this possible?

Background:
I want my C library and its extension scripts to use the same test framework. (I am aware various unit testing tools exist for Lua. I don't care; I want one report) I'm wrapping CUnit in a thin layer of Lua. CUnit provides a choice of fatal and non-fatal test assertions. Fatal assertions cause an immediate longjmp out of the test and back into the framework runner. This seems like it would do bad things to the Lua VM if I did not clean the stack first.

The stack will probably look something like:

#0. C:   assert_wrapper_fcn(test, fatal)
#1. Lua: assert_fcn(bool)
#2. Lua: test_fcn()
#3. C:   runner(&test_fcn)

I want to clean up everything between #0 and #3. I know the method signatures of test_fcn() and assert_fcn(bool), but that's it.

like image 287
gibbss Avatar asked Dec 17 '25 23:12

gibbss


2 Answers

To empty the stack is really easy. Just use lua_settop with 0 as argument.

lua_settop(L, 0);
like image 133
prapin Avatar answered Dec 19 '25 14:12

prapin


Not sure I'm understanding the question quite right... but to clear out Lua's stack:

int stackSize = lua_gettop(L);
lua_pop(L, stackSize);
like image 39
Graham Perks Avatar answered Dec 19 '25 13:12

Graham Perks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!