It seems when I return a table from a function I lose they keys. Not sure if this is how Lua should be functioning.
For example
function main()
local someTable = {}
someTable["foo"] = "bar"
print(someTable["foo"])
return someTable
end
local test = main()
print(test["foo"])
for k, v in pairs(test) do
print(k, v)
end
bar
nil
1 bar
Your code is ok and shows the expected behaviour in a standard Lua environment like the Lua Online Demo.
bar
bar
foo
bar
So either there is an issue with the environment you're running that script in or there is a difference between the code you posted here and the code you're running ony our machine.
Related
but this will get confusing for sure.
I'm still very new to LUA and one thing i haven't worked much yet with is metatables.
I need to find a way to create a meta table which runs a function on editing values. This is not a problem if i stay on "one level", so the first index. Then i can simply use the __newindex to run it. But what i'm trying to do is to run the function whenever any value is changed.
This would require some way to set any table inside the metatable to be again a metatable running the same function as the "main" metatable
In my use case that would be a "save" function:
function MySaveFunction(tbl)
FileSave(my_settings_path, tbl)
end
MyTable = setmetatable()
MyTable.Value = value --> run MySaveFunction(MyTable.Value)
MyTable.SubTable = {} --> run setmetatable() on SubTable
MyTable.SubTable.Value = value --> run MySaveFunction(MyTable.SubTable.Value)
MyTable.SubTable.SubSubTable = {} --> run setmetatable() on SubSubTable
MyTable.SubTable.SubSubTable.Value = value --> run MySaveFunction(MyTable.SubTable.SubSubTable.Value)
MyTable.SubTable.SubSubSubTable = {} --> run setmetatable() on SubSubSubTable
MyTable.SubTable.SubSubSubTable.Value = value --> run MySaveFunction(MyTable.SubTable.SubSubSubTable.Value)
Hope someone can help me <.<
First thing to take a note of is that __newindex and __index metamethods are only triggered when they handle nil value in target table. If you want to track every single change, you can't just use __newindex, because once you write a value, subsequent calls won't do anything. Instead, you need to use a proxy table.
Another important thing to consider is tracking paths of accessed members.
Last, but not least, you need to remember to use raw access functions like e.g. rawget when implementing your handlers. Otherwise, you may encounter stack overflows or other weird behaviour.
Let's have a trivial example to illustrate the problems:
local mt = {}
function mt.__newindex (t, key, value)
if type(value) == "table" then
rawset(t, key, setmetatable(value, mt)) -- Set the metatable for nested table
-- Using `t[key] = setmetatable(value, mt)` here would cause an overflow.
else
print(t, key, "=", value) -- We expect to see output in stdout for each write
rawset(t, key, value)
end
end
local root = setmetatable({}, mt)
root.first = 1 -- table: 0xa40c30 first = 1
root.second = 2 -- table: 0xa40c30 second = 2
root.nested_table = {} -- /nothing/
root.nested_table.another = 4 -- table: 0xa403a0 another = 4
root.first = 5 -- /nothing/
Now, we need to deal with them. Let's start with a way to create a proxy table:
local
function make_proxy (data)
local proxy = {}
local metatable = {
__index = function (_, key) return rawget(data, key) end,
__newindex = function (_, key, value)
if type(value) == "table" then
rawset(data, key, make_proxy(value))
else
print(data, key, "=", value) -- Or your save function here!
rawset(data, key, value)
end
end
}
return setmetatable(proxy, metatable) -- setmetatable() simply returns `proxy`
end
This way you have three tables: proxy, metatable and data. User accesses proxy, but because it's completely empty on each access either __index or __newindex metamethods from metatable are called. Those handlers access data table to retrieve or set the actual values that user is interested in.
Run this in the same way as previously and you will get an improvement:
local root = make_proxy{}
root.first = 1 -- table: 0xa40c30 first = 1
root.second = 2 -- table: 0xa40c30 second = 2
root.nested_table = {} -- /nothing/
root.nested_table.another = 4 -- table: 0xa403a0 another = 4
root.first = 5 -- table: 0xa40c30 first = 5
This should give you an overview on why you should use a proxy table here and how to handle metamethods for it.
What's left is how to identify the path of the field that you are accessing. That part is covered in another answer to another question. I don't see a reason to duplicate it.
Ok so I've been searching for a while and didn't get the answer. I imagine someone has this same problem but I was not able to solve this problem. I'm new to Lua, having some experience with Python but not being a programmer :S.
So I'm doing a metatable to handle complex numbers, following the tutorials here: http://www.dcc.ufrj.br/~fabiom/lua/
so I implement creation, addition, printing and equal comparison:
local mt={}
local function new(r,i)
return setmetatable({real = r or 0, im = i or 0},mt)
end
local function is_complex (v)
return getmetatable(v)==mt
end
local function add (c1,c2)
if not is_complex(c1) then
return new(c1+c2.real,c2.im)
end
if not is_complex(c2) then
return new(c1.real+c2,c1.im)
end
return new(c1.real + c2.real,c1.im + c2.im)
end
local function eq(c1,c2)
return (c1.real==c2.real) and (c1.im==c2.im)
end
local function modulus(c)
return (math.sqrt(c.real^2 + c.im^2))
end
local function tos(c)
return tostring(c.real).."+"..tostring(c.im).."i"
end
mt.new=new
mt.__add=add
mt.__tostring=tos
mt.__eq=eq
mt.__len=modulus
return mt
Then I make a small tests:
complex2=require "complex2"
print (complex2)
c1=complex2.new(3,2)
c2=complex2.new(3,4)
print (c1)
print (c2)
print(#{1,2})
print(#c2)
print(complex2.__len(c2))
print(#complex2.new(4,3))
and I get:
table: 0000000003EADBC0
3+2i
3+4i
2
0
5
0
So, What am I dong wrong? is something with calling the #, when i try to debug in the other cases the program goes to the module into the function but the # operand gets like ignored. Modulus function is working and can be called in the module... I'm sorry for such a long and I'm sure obvious question but I tried everything I could already. Thank you
May be the problem is about Lua version.
http://www.lua.org/manual/5.2/manual.html#2.4
"len": the # operation
works since Lua 5.2
and it works only with tables
So, Thank you, and you are right. I thought I had selected 5.2 but in the interpreter it was running 5.1 :S. Now i did:
complex2=require "complex2"
print (complex2)
c1=complex2.new(3,2)
c2=complex2.new(3,4)
print (c1)
print (c2)
print (c1+c2)
print(#{1,2})
print(#c2)
print(complex2.__len(c2))
print(complex2.__len(complex2.new(3,3)))
print(#complex2.new(4,3))
print(getmetatable(c2))
And i got:
table: 000000000047E1C0
3+2i
3+4i
6+6i
2
5
5
4.2426406871193
5
table: 000000000047E1C0
Everything under control ^^, at least the code was working as supposed xD
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.
I'm kinda new to Lua (not really done much with it yet) and I'm trying to wrap my mind around metatables. I have had them working before but now (after a number of months) I have encountered something really weird.
What should this script print when run?
__mt = {}
__mt.__index = function(table, key)
print("In __index")
return 99
end
test = {}
test.x = 5
setmetatable(test, __mt)
print(test.x)
Personally, I would expect it to print "In __index" (from the metamethod) followed by 99. However, whenever I run it I get 5. Nothing I do can get the index metamethod to run. It just acts like I'm using rawget() instead.
Curiously, adding
print(getmetatable(test).__index(test, "x"))
will do the right thing. The metatable is there, __index() is correct, it just isn't being called.
Is this a bug or am I just doing something stupid? I can't tell.
The metamethod (in old terminology also called fallback) called __index is only called if the key x does not exist in the table, when you access t.x. Try print(t.y) instead!
Added: Yes, using a proxy table.
function doubletable(T)
local store = T or {}
local mt = {}
mt.__index = function (t, k) return store[k] and 2*store[k] end
mt.__newindex = store
return setmetatable({}, mt)
end
t = doubletable({a=1, b=3})
t.c = 7
print(t.a, t.b, t.c)
-- output: 2 6 14
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...