cluacunit

How to pop / clean Lua call stack from C


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.


Solution

  • 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);