Lua C - Is there a way to keep a value in C, but still have it not garbage collected?

277 Views Asked by At

I am creating a thread in lua c, but i want to keep it just in C, without making it a variable in the environment/etc. But when i throw away the thread value that gets pushed by lua_newthread, it gets garbace collected shortly after, and so becomes useless. In this thread i want to run arbitrary lua code passed by the user, allowing them to use the exclusive functions/variables that are isolated from other threads.

Is there a way to mark a thread/userdata/table value as non-garbage-collectable?

2

There are 2 best solutions below

0
On BEST ANSWER

That is possible. You can add a call to gc_mark with your TValue's gcobj as an argument into gc_markroots.

3
On

No, you have to reference it somewhere or disable GC. Use lua_ref to reference is in mostly-hidden place: registry. (debug.getregistry())