Question

Let me make it simple.

I have multiple tests to run and after each test I do clear all the requires made by the current test using package.loaded[] = nil.

The problem is though we remove the required files from global namespace , I see that the ffi.cdef done by those files remain intact and when I do require agian, It throws an error as "attempt to redefine ''"

Do we have any method by which we can clear the cdef loaded? or can retrieve the cdef loaded, so that I can save the old cdef and restore back once the test is done? Or is there anyother method I can do the same task?

I am just looking for something like ffi.uncdef.

Was it helpful?

Solution

There is no such function, and for good reason.

First of all, setting package.loaded[pkg] = nil is not an official way to unload a module, especially C code. See one of my previous answers. Additionally, unloading cdefs is not that simple; what happens if you unload a cdef that has objects using it?

The only way to test code that runs cdefs is by creating a new lua_State or luajit process for each test.

(Also, it's Lua, not LUA)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top