I had some time to look at this in more depth. I believe that we can simplify it down to just add a ref to the code for each var and function during exec_global_code. Meaning, the script dispatch itself will hold the refs, not the typeinfo.
Then during the release of the script dispatch, we release the references, since we already go through the variables anyway to release their contents.
We'll have to add a loop to go through each function to release the code it references, though, but it shouldn't be much of a problem.
Do you think I should pursue this approach instead?