This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
boq's profile picture

[1.73] ILuaObject are not returned properly from ILuaContext.executeMainThreadTask()

Started by boq, 01 March 2015 - 06:14 PM
boq #1
Posted 01 March 2015 - 07:14 PM
VERSION:ComputerCraft 1.73
DESCRIPTION:
When running this code, testAsync returns valid table with functions, while testSync returns empty table.
EXPECTED RESULT:
Identical values (table with three functions) returned from both methods


When returning from line 43, result is already empty map. Probably conversions look like that: (Java: ILuaObject) -> (Lua: table of functions) -> (Java: empty table) - functions are not converted.

Probably little bit obscure and I already found alternative solution (context.issueMainThreadTask(…), context.pullEvent("task_complete") and some extra object to hold results), but it would be nice to have this working out of box and not having to play with internal CC implementation details.
dan200 #2
Posted 01 March 2015 - 08:41 PM
Unfortunately, this is not possible. executeMainThreadTask is implemented as follow: the task is queued on the main thread, the main thread executes a task, which queues an event to the lua content when it completes. This event resumes the top-level co-routine of the computer, and is (hopefully) passed down through the various parallels, multishell, etc which the computer might be running, until it reaches the call location, where pullEvent wakes up and executeMainThreadTask returns. Because of this Java -> Lua -> Java transition, ILuaObjects are converted into tables-of-functions, which is converted back to Objects. Your workaround of storing the results externally and waiting for the correct "task_complete" event is a perfectly viable solution to this.
boq #3
Posted 01 March 2015 - 09:14 PM
Thanks for confirming my guesses. And yes, I'm already checking transaction id.

I just feel little iffy when using undocumented stuff - there is no documentation that connects return value of issueMainThreadTask with value in event and since event itself is not documented, it can change in newer versions.