This is a bit OT, but I was also working on a multithreading problem yesterday, and ended up rolling a rather dirty C-hack to solve the following problem:
Given a Lua 5.2 function with upvalues that are only strings, numbers, booleans, nils, or _ENV, create a new closure that contains at most one upvalue (equal to the parent function's _ENV). Any non-_ENV upvalues should be converted to constants, using the value currently stored in the upvalue.
My excuse for writing this (rather odd) function was that string.dump is a great tool for moving simple functions from one thread to another; but, 'prepping' a function for transfer via string.dump can be harder than it needs to be, if said function happens to include a lot of upvalues that are simple types. In the case of such upvalues, just converting the upvalue ref to a constant ref is usually the behavior I want, and writing a function that hacks into the guts of Lua and does exactly that is relatively straightforward (in C, anyhow).
If anyone wants a copy of the code, let me know.
Similarly, of course, if anyone wants to tell me what a fool I am for solving this particular problem in this particular way, constructive criticism is always welcome :)
-Sven