Parameter implicit "arg" in functions not work in Lua - lua

I have a problem in use implicit parameter arg in functions.
The code not works. The documentation, http://www.lua.org/pil/5.2.html, should works.
function listar_um (...)
for i,v in ipairs(arg) do
print("usando args " .. arg[i])
end
end
listar_um("Olá", 1, "Dois")
This code works with the declaring variable lista.
function listar_um (...)
lista = {...}
for i,v in ipairs(lista) do
print("não usando args " .. lista[i])
end
end
listar_um("Olá", 1, "Dois")
Why the first example does not work?
Script for test: http://www.codeshare.io/IPwRJ
Execute on-line script: http://www.compileonline.com/execute_lua_online.php
Thanks.

The first edition of PiL talks about Lua 5.0. The use of arg is available in Lua 5.0, while it's removed since Lua 5.1
You can find it in Lua 5.0 reference manual, but not in Lua 5.1 reference manual.
The edition the online interpreter uses is Lua 5.2, you can find out by print(_VERSION).
Edit: after some tests, it seems that arg is still available in Lua 5.1, but not working in Lua 5.2.

Related

unpack() not available on Lua 5.4?

I am reading a few tutorials on Lua and am trying to figure out how to use unpack(). I found an example that goes like this:
t = { "the", "quick", "brown" }
print (unpack (t))
The output should be "the quick brown".
What actually happens is this: "stdin:1: attempt to call a nil value (global 'unpack')".
How can I make unpack() work?
My Info:
OS: Mac OS 10.8
Lua: 5.4.2
Since Lua 5.2 unpack function is now at table.unpack
Function unpack was moved into the table library and therefore must be called as table.unpack.

Mediawiki scribunto lua module do not know builtin functions

I am having a problem with calling Lua built-in functions using Scribunto.
I created basic module Module:Item
local p = {};
function p.test(frame)
print("Hello World!")
end
return p
Which I call in different page as {{#invoke: Item | test}}
and I receive a following error:
Lua error in Module:Item at line 3: attempt to call global 'print' (a nil value).
Backtrace:
1. (tail call): ?
2. Module:Item:3: in function "chunk"
3. mw.lua:511: ?
4. (tail call): ?
5. [C]: in function "xpcall"
6. MWServer.lua:99: in function "handleCall"
7. MWServer.lua:313: in function "dispatch"
8. MWServer.lua:52: in function "execute"
9. mw_main.lua:7: in main chunk
10. [C]: ?
Since print is Lua built-in function I have the feeling the problem will be somewhere in setting on the pc.
However, when I imported wiki Infoboxes, they are working OK.
Versions:
Linux Mint Tara - Cinnamon based on ubuntu 18
MediaWiki 1.31.7
Scribunto (106fbf4) 17:24, 15 May 2018
Lua 5.1.5
Any help pointing where the problem can be is highly appreciated.
Scribunto intentionally doesn't include print. The "Removed functions and packages" section in its manual says this about it:
This was discussed on wikitech-l and it was decided that it should be omitted in favour of return values, to improve code quality. If necessary, mw.log() may be used to output information to the debug console.

attempt to index a nil value (global 'math'))

I am using lua to make scripts in my game,
after updating the lua libs version from 5.1 to 5.3, an error appears on the game engine every time I use
math.X in a function
error :
Lua run doFile failed.attempt to index a nil value (global 'math'))
original code :
local value = math.random(1,10)
do you have any solutions ?

Is it possible to block functions from compiling by using luajit?

So im debugging my lua scripts now, and i here i came to question - is it possible to block function from compiling by using jit.attach? (something like this)
local function jitcatch(dat)
local sour = string.sub( jit.util.funcinfo(dat).source, 2 )
if sour == 'test.lua' then jit.off(dat) end
end
jit.attach( jitcatch, 'bc' )
jit.off(true, true) will disable jit compiling for the current script. jit.off(function) will disable jit compiling for a specific function.

Getting arg to work in a varag function in Lua 5.2 (integrated in Delphi)

When using the Lua 5.2 API, the code below prints "nil"
function __debug(szName, ...)
print(type(arg));
end
__debug("s", 1, 2, 3, 4);
But this code does work when using Lua 5.1, and prints "table"
If you are referring to vararg function, the arg table was deprecated already in Lua 5.1. In Lua 5.2, you can use table.pack to create arg if you need it:
function debug(name, ...)
local arg = table.pack(...)
print(name)
for i=1,arg.n do
print(i, arg[i])
end
end
That's because arg has been deprecated since Lua 5.1. It only remained as a compatibility feature.
References: Lua 5.1 manual, unofficial LuaFaq
a workaround is using this line to generate a table called arg:
local arg={...}

Resources