How is Lua code execution handled when using LuaJava?
I intend to use LuaJava to run scripts in my Java/Android applications.
- When I run Lua script, is it executed in some another thread?
- How do I manage execution of Lua code, how do I know that script finished execution? If I want my Java program to wait for Lua code to finish, what do I do then?
My first guess was to use some semaphores and invoke Java callbacks from Lua. How do I do it in a right way?
UPD WITH CODE
Java:
LoadScript(final String filename) {
this.luaState = LuaStateFactory.newLuaState();
this.luaState.openLibs();
this.luaStat开发者_JS百科e.LdoFile(filename);//launch Lua script to print lines
printLines();// print Java lines
}
void printLines(){
for(int i=0;i<100;i++)
System.out.println("From java: "+i);
}
Lua:
for i=0,100
do
print("From lua: ",i)
end
In console I see that Java printed its strings first, and then Lua did.
When I run Lua script, is it executed in some another thread?
No.
You will know that the script has "finished" execution when the next Java statement after the one that calls into the script is executed.
That happens because they're using different streams to write to std-out. They have different flushing behavior, so one will happen before the other. But the actual execution of the code is not threaded.
If you want to test this, just have the Lua script return something and then have your Java code get the top thing from the stack. It will always be what the Lua script generated.
精彩评论