Top Level Coroutine Override
#1
Posted 02 November 2012 - 03:02 PM
This has to be like my first topic or something, maybe even my first post, I'm not sure.
Anyway recently I wanted to design a networking API, but the problem was I couldn't run a top level coroutine, after talking to people and disputing with them on IRC for the last few days and being told it was very difficult or impossible I devised a way to launch functions at top level, by basically messing around with the shell and bios, getting them to quit and then launch my own code.
There is a lot of possibilities with this, and the full power could probably only be realised in the hands of an Advanced/Pro Lua Coder, but here is my attempt at some community contribution. I only started learning Lua a few weeks to a month ago. So I welcome any advice or input.
I've not made any test code for this, but it should all work how I've described it. It should let you spawn whatever top level programs you want to.
The example I have loads the shell per normal, but does not start rednet for redstone input. Just to show that it works.
You're welcome to use the code how you like, but I ask that you keep my credit on it.
You should use this as your startup file, it might work not as startup file, but I never tested it that way.
http://pastebin.com/2SQAzrWi
Enjoy, and please let me know what you think.
Edit: Re-uploaded to Pastebin via my account so I can start pushing updates and fixes.
#2
Posted 06 November 2012 - 08:31 AM
#4
Posted 06 November 2012 - 10:17 PM
This is pretty much what I wanted. Awesome.
#5
Posted 07 November 2012 - 06:21 AM
#6
Posted 07 November 2012 - 07:39 AM
#7
Posted 08 November 2012 - 06:23 AM
#8
Posted 08 November 2012 - 06:35 AM
#9
Posted 08 November 2012 - 07:10 AM
#10
Posted 08 November 2012 - 07:32 AM
#11
Posted 08 November 2012 - 08:48 AM
#12
Posted 08 November 2012 - 08:59 AM
#13
Posted 08 November 2012 - 09:02 AM
wilcomega, on 08 November 2012 - 08:59 AM, said:
#14
Posted 08 November 2012 - 09:07 AM
#16
Posted 08 November 2012 - 09:17 AM
PixelToast, on 08 November 2012 - 09:12 AM, said:
I optimize CPU bound code for CPU and memory bound code for memory (which almost never happens in CC, it is all interrupt/hardware bound stuff in general) and the rest for understandability and functionality.
#17
Posted 07 January 2013 - 07:48 PM
Also I didn't want people using a shell on my Mainframe computers.
Wow it's so good to see all the great replies, Thanks guys
#18
Posted 15 January 2013 - 08:56 AM
#19
Posted 15 January 2013 - 06:28 PM
I must admit the getParentShell is quite smart though. I have tried replacing the printError function in my programs but it refuses to work on ccEmu, not sure about real CC
the below code still uses the normal printError
local pE=printError
rawset(_G,printError,function(...)
print("functional")
sleep(5)
printError=pE
return printError(...)
end)
printError('me')
EDIT, derpity derp derp.... no quotes
local pE=printError
local tO={}
for i=1,5 do
if rawget(getfenv(i),"printError")~=nil and not tO[getfenv(i)] then
tO[getfenv(i)]=true
rawset(getfenv(i),"printError",function(...)
print("functional")
sleep(5)
printError=pE
return printError(...)
end)
end
end
you can then replace the print and sleep with whatever you need
#20
Posted 15 January 2013 - 09:12 PM
bbqroast, on 15 January 2013 - 08:56 AM, said:
Nope that's the point, you don't have to change ROM files, I've recently been looking in to something more advanced than this for overriding the bios.
KaoS, on 15 January 2013 - 06:28 PM, said:
I must admit the getParentShell is quite smart though. I have tried replacing the printError function in my programs but it refuses to work on ccEmu, not sure about real CC
the below code still uses the normal printError
local pE=printError
rawset(_G,printError,function(...)
print("functional")
sleep(5)
printError=pE
return printError(...)
end)
printError('me')
EDIT, derpity derp derp.... no quotes
local pE=printError
local tO={}
for i=1,5 do
if rawget(getfenv(i),"printError")~=nil and not tO[getfenv(i)] then
tO[getfenv(i)]=true
rawset(getfenv(i),"printError",function(...)
print("functional")
sleep(5)
printError=pE
return printError(...)
end)
end
end
you can then replace the print and sleep with whatever you need
I'm sorry you don't understand it
I wish I'd know about rawset back when I wrote this code, fortunately there was no metatables that are using __newindex so it didn't make much difference.
I'm glad people are still looking at this post. I look forward to it one day being useful.
I will likely use it in my operating system, just to get rid of Rednet and the Shell running in the background. Free up some stuff and make sure they don't interfere. I tend to do things outside of the book so the less other things running the better.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











