Is it possible to get the original function used to create a coroutine ?
thread = coroutine.create(function()
-- Code
end)
f = get_function_from_thread(thread)
You can't do this out-of-the box, but you can always redefine coroutine.create:
local create=coroutine.create
local created={}
function coroutine.create(f)
local t=create(f)
created[t]=f
return t
end
function get_function_from_thread(t)
return created[t]
end
If you create lots of coroutines, consider setting created as a weak table.
Related
i already asked this question on stackoverflow and accepted an answer i think i did not really understand the answer and i got some more question, im embarrassed to necro it so im creating a new question
im learning lua and got to metatable part, in this example
local tb = {}
local meta = {}
function tb.new(s)
local super = {}
super.s = s
setmetatable(super,meta)
return super
end
function tb.add(s1,s2)
return s1.s..s2.s
end
meta.__add = tb.add
f= tb.new("W")
t= tb.new("E")
print(f+t)
when compiler gets tof = tb.new("W")
i think this happens
function tb.new("W") super.W = W return setmetatable(super,meta) return super end
so
print(f+t)
looks like
print(super+super)
how does
tb.add(super,super)
find the fields of super table using
return s1.s..s2.s
also as the
tb.new
function is called twice and
setmetatable(super,meta)
happens twice is there any difference between the first and second iteration? if any of the above are incorrect please correct me.
when compiler gets tof = tb.new("W") i think this happens function tb.new("W") super.W = W return setmetatable(super,meta) return super end
No. It's more like super['s'] = 'W'. That's how dot notation works. I hope that clarifies how Lua "finds the fields" later.
as the tb.new function is called twice and setmetatable(super,meta) happens twice is there any difference between the first and second iteration?
They're different, because the super variable is a new table each time. Any time you see {} (a table constructor), whether empty or not, it's creating an entirely new table. meta, however, is still the same table, because it only gets a table constructor once, outside of any function.
Here is an example
local CustomLoad = function(l) loadstring(l) end
CustomLoad("print('hi')")
Please let me know, beacuse I got into this like a week ago, and I've been trying to make it, yet I coudln't. Can some of you tell me if it's even possible?
If you aren't trying to run "print('hi')" in that example, then I do believe you're missing a return statement.
local CustomLoad = function(l) return loadstring(l) end
But if you are, then:
local CustomLoad = function(l) return loadstring(l) end
CustomLoad("print('hi')")()
or
local CustomLoad = function(l) local f = loadstring(l); f() end
CustomLoad("print('hi')")
Because loadstring creates a function, which, when run, executes the code in the string.
Hope that helped!
This is a weird question as I'm not so sure how to ask, but here's the problem:
BeforeTime = os.clock()
function NewFunction( Name, Value )
Name = function()
return Value
end
end
NewFunction( RunningTime, (os.clock()-BeforeTime) )
while true do
print(RunningTime()) -- will always return same value, i want the updated
end
The example above is not exactly my context, but its the easiest way to explain my problem.
I guess I could require that the parameter 'Value' needs to be a function, but is there another way?
NewFunction creates a function and assigns it to the Name parameter. But, in Lua, actual parameters are passed by value (like Java and C) and formal parameters are effectively local variables. The assigned value is never used and it can't be used outside the function.
To make a function return a non-empty list of values, use a return statement.
Here's a similar function for a timer:
local function CreateTimer()
local BeforeTime = os.clock()
return function()
return os.clock() - BeforeTime
end
end
You can use it like this:
local RunningTime = CreateTimer()
while true do
print(RunningTime())
end
Your code does not run because NewFunction doesn't do what you think it should.
If you add the line below before the loop, then it works fine.
RunningTime = function() return (os.clock()-BeforeTime) end
I can't figure out how to put the objects created by a simple function in a table, to have them figure as individual identities..
E.g.
local function spawncibo()
nuovoCibo = display.newImage("immagini/cibo/cibo001.png")
end
timer.performWithDelay(1500, spawncibo, -1)
I tried to do it with a for loop, but it doesn't work (if i try to print the table I always get a nil result).
Any suggestion would be immensely appreciated!
Providing I have correctly understood your question, you may try something like this :
local cibo = {}
local function spawncibo()
cibo[#cibo+1] = display.newImage(string.format(
"immagini/cibo/cibo%3d.png", #cibo+1))
end
timer.performWithDelay(1500, spawncibo, -1)
This will read files cibo001.png, cibo002.png, ... every 1.5 s and put all images into the cibo array.
local spawnedCibos = {}
local function spawncibo()
nuovoCibo = display.newImage("immagini/cibo/cibo001.png")
table.insert(spawnedCibos, nuovoCibo);
end
timer.performWithDelay(1500, spawncibo, -1);
local function enterFrameListener( event )
for index=#spawnedCibos, 1, -1 do
local cibo = spawnedCibos[index];
cibo.x = cibo.x + 1;
if cibo.x > display.contentWidth then
cibo:removeSelf();
table.remove(spawnedCibos, index);
end
end
end
You could try this...
local spawned = {} -- local table to hold all the spawned images
local timerHandle = nil -- local handle for the timer. It can later be used to cancel it if you want to
local function spawnCibo()
local nuovoCibo = display.newImage('immagini/cibo/cibo001.png')
table.insert(spawned, nuovoCibo) -- insert the new DisplayObject (neovoCibo) at the end of the spawned table.
end
local function frameListener()
for k, v in pairs(spawned) do -- iterate through all key, value pairs in the table
if (conditions) then -- you will probably want to change conditions to some sort of method to determine if you want to delete the cibo
display.remove(spawned[k]) -- remove the part of the object that gets rendered
spawned[k] = nil -- remove the reference to the object in the table, freeing it up for garbage collection
end
end
end
timer.performWithDelay(1500, spawnCibo, 0) -- call spawnCibo() every 1.5 seconds, forever (That's what the 0 is for) or until timer.cancel is called on timerHandle
Runtime:addEventListener('enterFrame', frameListener) --
If you've got any other questions, feel free to ask.
I'm generating some (non-html) documentation for a Lua library that I developed. I will generate the documentation by hand, but I'd appreciate some kind of automation if possible (i.e. generating skeletons for each function so I can fill them in)
I'd like to know if there's a way for lua to know the names of the parameters that a function takes, from outside it.
For example, is there a way to do this in Lua?
function foo(x,y)
... -- any code here
end
print( something ... foo ... something)
-- expected output: "x", "y"
Thanks a lot.
ok,here is the core code:
function getArgs(fun)
local args = {}
local hook = debug.gethook()
local argHook = function( ... )
local info = debug.getinfo(3)
if 'pcall' ~= info.name then return end
for i = 1, math.huge do
local name, value = debug.getlocal(2, i)
if '(*temporary)' == name then
debug.sethook(hook)
error('')
return
end
table.insert(args,name)
end
end
debug.sethook(argHook, "c")
pcall(fun)
return args
end
and you can use like this:
print(getArgs(fun))
Try my bytecode inspector library. In Lua 5.2 you'll be able to use debug.getlocal.
Take a look at debug.getinfo, but you probably need a parser for this task. I don't know of any way to fetch the parameters of a function from within Lua without actually running the function and inspecting its environment table (see debug.debug and debug.getlocal).
function GetArgs(func)
local args = {}
for i = 1, debug.getinfo(func).nparams, 1 do
table.insert(args, debug.getlocal(func, i));
end
return args;
end
function a(bc, de, fg)
end
for k, v in pairs(GetArgs(a)) do
print(k, v)
end
will print
1 bc
2 de
3 fg
Basically we use debug.getinfo to retrieve the nparams attribute (which gives us the information of how many parameters the function takes) and debug.getlocal to access the name of the parameters.
Tested and working with Lua 5.4
Take a look at the luadoc utility. It is sort of like Doxygen, but for Lua. It is intended to allow the documentation to be written in-line with the source code, but it could certainly be used to produce a template of the documentation structure to be fleshed out separately. Of course, the template mechanism will leave you with a maintenance issue down the road...