Lua Threads for Computer Mod
My friend just told me about Minetest a few days ago after I started writing my own Minecraft clone in C++ and Lua (after M****s*** bought Mojang). I immediately abandoned my project and switched to Minetest, and am very happy with it.
I'm interested in writing a ComputerCraft-type mod for Minetest. I plan to use digilines instead of peripheral cables, and computers will have external terminals and storage devices, similarly to RedPower's computers and unlike ComputerCraft's. Obviously, they will run (sandboxed) lua. Mesecons' luacontrollers are hard to use because you can't have libraries and such and you can't have loops or functions. This is why I want to make my own mod - in addition to the fact that I want more peripherals like hard and floppy drives and terminals with keyboard entry. I can easily handle the libraries and peripherals part, but just removing the loop and function disabling code from luacontrollers won't work - someone could run 'while true do end' and hold up the whole server indefinitely.
Is there any way for me to spawn my own Lua thread that will run indefinitely but independently from the main server thread? I suspect stuff from here would have to be implemented into the core for this to work, if it isn't already. I found minetest/builtin/async/init.lua and minetest/builtin/common/async_event.lua, but I couldn't figure out how to use them. It seems they can only be used by the main menu. Will they do what I want?
Maybe I could just give my sandboxed code a special loadstring that puts coroutine.yield()'s in appropriate places (inside non-tiny loops, in potentially recursive functions, etc)? This would probably involve converting the whole program into a tree first, which would be expensive and basically like compiling it twice.
Is it possible for me to spawn my own threads? I could write a C++ library and load that with Lua if necessary, but would that be necessary?
I am currently using version 0.4.10 from the Gentoo package manager. I plan to do a manual install so I can fetch updates with git soon, however.
EDIT: edited stuff