Post subject: JPC-RR builtin functions returning on the wrong thread
Sand
He/Him
Player (143)
Joined: 6/26/2018
Posts: 175
JPC-RR has a number of builtin or "callback" functions, defined in Java but callable from Lua code. These are defined in methods whose name starts with luaCB_ in the JPC-RR source code and include such functions as print_console_message (which gets aliased to print), jpcrr.wait_event, jpcrr.status, jpcrr.window.create, and others. There is a bug that causes these builtin functions to return on the wrong thread. Instead of returning on the thread of the currently running coroutine, they always return on the main thread (the singleton LuaPlugin.luaState). Calling any of these functions in a coroutine causes strange effects. I had noticed way back in my Mega Man DOS TAS that I couldn't call print in a coroutine. Now I have a simple patch to fix it. This is relative to commit 6ab255ce10b2dd10b0ac3ab2e2be708e0b26eeaa (JPC-RR-r11.8-rc2).
Language: diff

--- a/org/jpc/plugins/LuaPlugin.java +++ b/org/jpc/plugins/LuaPlugin.java @@ -228,7 +228,7 @@ public class LuaPlugin implements ActionListener, Plugin l.error("Attempted to call method on dead object"); return 0; } else - return ((Integer)callbackMethod.invoke(onObject, luaState, LuaPlugin.this)).intValue(); + return ((Integer)callbackMethod.invoke(onObject, l, LuaPlugin.this)).intValue(); } catch(InvocationTargetException e) { if(e.getCause() instanceof LuaError) throw (LuaError)e.getCause(); //Pass runtime exceptions through.
Here is a demonstration script:
Language: lua

local function f() coroutine.yield(123) print("hello") coroutine.yield(456) end local co = coroutine.create(f) print(coroutine.resume(co)) print(coroutine.resume(co)) print(coroutine.resume(co)) print(coroutine.resume(co))
The coroutine is meant to yield one value, print "hello", yield another value, yield nil, then die. Without the patch, the script produces the wrong output. Notice that print("hello") does not actually print, but somehow causes the coroutine to yield as if in error, with "hello" as an error message.
true	123
false	hello
false	cannot resume dead coroutine
false	cannot resume dead coroutine
With the patch, it's correct:
true	123
hello
true	456
true
false	cannot resume dead coroutine
Site Admin, Skilled player (1254)
Joined: 4/17/2010
Posts: 11475
Location: Lake Char­gogg­a­gogg­man­chaugg­a­gogg­chau­bun­a­gung­a­maugg
Poke Ilari on #lsnes irc.
Warning: When making decisions, I try to collect as much data as possible before actually deciding. I try to abstract away and see the principles behind real world events and people's opinions. I try to generalize them and turn into something clear and reusable. I hate depending on unpredictable and having to make lottery guesses. Any problem can be solved by systems thinking and acting.