130 posts
Location
Here
Posted 09 April 2016 - 06:43 PM
Source:
http://www.lua.org/m...5.1/manual.html
You can change the environment of a Lua function or the running thread by calling setfenv. You can get the environment of a Lua function or the running thread by calling getfenv.
Since LuaJ implements coroutines as threads, does this work?
Edited on 09 April 2016 - 04:43 PM
8543 posts
Posted 09 April 2016 - 07:16 PM
Does what work?
130 posts
Location
Here
Posted 09 April 2016 - 10:18 PM
Switching between coroutines(implemented as threads via LuaJ) using setfenv and getfenv? Or does it only change the thread that the function is able to run in and not which thread is currently running?
Edited on 09 April 2016 - 08:21 PM
3057 posts
Location
United States of America
Posted 09 April 2016 - 10:20 PM
Switching between coroutines(implemented as threads via LuaJ) using setfenv and getfenv?
That's not what setfenv or getfenv do. They manipulate environments (_G)
130 posts
Location
Here
Posted 09 April 2016 - 10:26 PM
I understand that bit. The question is asking if your able to manipulate threads using setfenv getfenv as far as determining which thread a function can run in or which thread is currently running. The text provided in the OP is from the link (Lua 5.1 docs) which is a bit confusing and is why im asking. Thanks
Edited on 09 April 2016 - 08:26 PM
3057 posts
Location
United States of America
Posted 09 April 2016 - 10:53 PM
You know, I really am not sure. If you want a function to do different things based on which thread it is running in, you could probably check that using getfenv( 0 ). From the documentation, getfenv( 0 ) returns the global environment, which is another name for the current thread's environment.
7083 posts
Location
Tasmania (AU)
Posted 10 April 2016 - 08:40 AM
determining which thread a function can run in or which thread is currently running
If a given coroutine - bearing in mind that all CC code is running within a coroutine, even if you perform the TLCO discussed
here, and this isn't true for all other Lua environments - doesn't share the environment of a function it tries to execute, that won't stop the function from executing. For example, the environment tables used by APIs are in no way shared or available to those used by the CraftOS shell script.
You could maybe take a guess as to which coroutine was active using getfenv(), assuming you knew the identities of all the tables and coroutines in play and exactly how they were being put to use. It'd be a somewhat convoluted system and you'd have to be sure everyone who worked within it wanted to play by your rules if you wanted to ensure accuracy.
This is one of those cases where you'd be better off explaining the overall goal you're trying to achieve, rather than asking about the individual steps you've decided to take to reach it. There may be a better way.