Posted 16 February 2013 - 12:34 PM
Since I started to play with CC I always wondered how to exit Lua prompt without typing the whole exit() ? And yes, I am THIS lazy, I'd love not to type those parenthesis. So when my understanding of Lua reached some critical level (actually metatables and environments), I added the lovely feature to my framework.
In general, the problem with exit() function within the Lua prompt is that this function is local to Lua prompt shell. Every time you type lua in command line, new instance of Lua prompt is created and the only way to inject some code into it is using global environment.
So. You can easily create global table and call it's properties from the prompt, but you can't define any shortcut to exit(), because it don't exist at the time your code runs… That what you think if do not know how environment stack works. And if you do not familiar with metatables, you may think that you can't run any code without parenthesis.
Here is the code. I keep it within file that is included on startup with dofile:
Here what it snippet does:
Defines a handler for accessing non-existing global properties, but fires only if exi tries to be evaluated. At this point and from global scope we can tell, that some sub-sub-sub-environment (the Lua prompt) have function named exit. So we just enumerating all the environments from the deepest (starting at 8 for example) up to the global to see if exit have a value. If it does, so the code just executes that function.
I think this is a good way to execute ANY code within Lua prompt and even without following a function call notation, just pure shortness :)/>
Hope this helps to approach the understanding of Lua.
P.S.: To pro's. I'm pretty noob in Lua, so if you see any way to make it more compact\beautiful\right way, please comment, I will be happy to learn something new.
In general, the problem with exit() function within the Lua prompt is that this function is local to Lua prompt shell. Every time you type lua in command line, new instance of Lua prompt is created and the only way to inject some code into it is using global environment.
So. You can easily create global table and call it's properties from the prompt, but you can't define any shortcut to exit(), because it don't exist at the time your code runs… That what you think if do not know how environment stack works. And if you do not familiar with metatables, you may think that you can't run any code without parenthesis.
Here is the code. I keep it within file that is included on startup with dofile:
--Set a shortcut for exiting lua prompt. Just type exi
--Developed for CC v1.48, Lua v5.1 by Pavlus (at net6000 com)
local g_mt=_G.__metatable or {}
local orig_ind=_G.__index or g_mt.__index
g_mt.__index = function(t,k)
if tostring(k)=='exi' then
for i=8,1,-1 do
if getfenv(i).exit then
write('Exited Lua prompt. exitcode=')
--exitcode thing - because I can't suppress output of 'nil' afterwards
return getfenv(i).exit()
end
end
end
if orig_ind then orig_ind(t,k) end
end
setmetatable(_G,g_mt)
Here what it snippet does:
Defines a handler for accessing non-existing global properties, but fires only if exi tries to be evaluated. At this point and from global scope we can tell, that some sub-sub-sub-environment (the Lua prompt) have function named exit. So we just enumerating all the environments from the deepest (starting at 8 for example) up to the global to see if exit have a value. If it does, so the code just executes that function.
I think this is a good way to execute ANY code within Lua prompt and even without following a function call notation, just pure shortness :)/>
Hope this helps to approach the understanding of Lua.
P.S.: To pro's. I'm pretty noob in Lua, so if you see any way to make it more compact\beautiful\right way, please comment, I will be happy to learn something new.