So remember when I was talking about a hot patching system for lua, which allows you to execute code in a running thread semi-permanently. Well I finished it, I didn’t get to do the way I had planned and go and patch Lua’s opcodes directly (it’s cool). Instead I added some “injection” routines to my ScriptManager and thread class. Oh if your wondering why injection is in quotation marks, its because its not real injection. Its Fake injection or pseudo injection.
First things first you need somewhere to store and handle your code “snippets”, As I said I used my ScriptManager and thread class. The ScriptManager handles the actual group running (since it holds all threads) and the thread class is where the code is actually stored.
I’ve said it once, I’ll say it again. I’m Lazy this is why I don’t use my own data structures and why reinvent the wheel. So I added the stuff to my thread class.
int AddInjection(std::string code);
int RemoveInjection(int index);//only index not really smart to overload
int PrintInjectionIndexes();//so you can find you code and remove
int RunInjections(); //duh
...
private:
std::vector<std::string code> m_injections;
Yeah. So what if I use STL. And in the
ScriptManager there is one function needed
GlobalRunInjections(). The first few functions are pretty simple just adding and removing elements from m_injections.
EchoInjectionIndexes is just too print out console info.
int Thread::EchoInjectionIndexes()
{
for(int i = 0; i < m_injections.size(); i++)
{
//I made my echo mirror printf for ease
glConsole::GetSingleton().Echo(“%d - %s\n”, i, m_injections[i].c_str());
}
return 0;
};
RunInjections this is what your after(well part of it). This is where all the code gets run for a particular thread.
int RunInjections()
{
for(int i = 0; i < m_injections.size(); i++)
{
//L is the thread
luaL_dostring(L, m_injections[i].c_str());
}
return 0;
};
OK the last piece of the puzzle. The new and improved yield function
int LuaYield(lua_State *L)
{
ScriptManager::GetSingleton().RunInjections(L);
return lua_yield(L, 1);
};
Just so you know this is
RunInjections():
void ScriptManager::RunInjections(lua_State *L)
{
for(int i = 0; i < m_threads.size(); i++)
if(L == *m_threads[i])
{
m_threads[i]->RunInjections();
break;
}
};
Hmm…So ScriptManager has a function called RunInjections but thread has a function with the same name. Its not a coincidence

OK so after all that you’ve learned that you just run the code with
luaL_dostring in the yield, well you could run them in the run function where
lua_resume is called. It shouldn’t make a difference.
Oh screenie. And before you ask. Yes its the pong again but using the “injection” system to render a purple square OMG.

Fake Injection - the implementation is an illusion
Now so you know this is one way of doing it. There are many variants and other completely different methods. If you do implement it differently leave a comment, a URL, whatever to let me and others know. Soon as I can omit code already in the thread(at load time aka the script), then we’ll be cooking with gas
Peace Out.