Moonscript uses \ to call methods so can someone explain to me why the code below does not work:
> file = io\open("mix.exs", "rb")
[string "tmp"]:1: calling 'open' on bad self (string expected, got table)
but when you call it to read the file it does ?
> file\read!
"Code.ensure_loaded?(Hex) and Hex.start
The io.open function expects to get a string as the first argument but io\open (like io:open in lua itself) is actually passing the io table as the first argument. That is it is a method call.
io\open("file", "mode")/io:open("file", "mode") are syntactic sugar for io.open(io, "file", "mode").
This is why file\read! works without an explicit argument because file gets passed as the first argument to the read("file", "format") function.
Moonscript uses \ to call methods
to call member methods. as in a\b c, ... translates to a.b(a,c,...).
this doesn't work here because io.open is a static function (io.open(what,how)), not a member (io.open(self,what,how)).
you couldn't call io:open in Lua either. the only place where io functions allow for being called as members is when you want to read/write stdio.
but when you call it to read the file it does ?
because now it's a member method of the file. you're actually still using io.read there, but the file object has io as a metatable index, therefore allowing you to access the same function via file.read, and since file\read! translates to file.read(file) it's the same thing.
so essentially the answer boils down to "because io:open doesn't work in Lua".
Related
How to output the contents of a function?
function a()
print("Hello,World")
end
print(func_toString(a))
I hope the result of func_toString(a) could be
function a()
print("Hello,World")
end
or just
print("Hello,World")
It is assumed that the source code is executed directly without precompiling or embedding.
How to do this?
well, it's not completely impossible, the code in some cases can be read from a lua file, for example:
function a()
print("Hello,World")
end
local function get_source_code(f)
local t = debug.getinfo (f)
if t.linedefined < 0 then print("source",t.source); return end
local name = t.source:gsub("^#","")
local i = 0
local text = {}
for line in io.lines(name) do
i=i+1
if i >= t.linedefined then text[#text+1] = line end
if i >= t.lastlinedefined then break end
end
return table.concat(text,"\n")
end
print( get_source_code(a) )
maybe that will be enough.
This is not possible from within the running Lua program.
Lua provides the debug library to inspect functions and variable. This allows you to obtain the source (the path or string where the function is defined) and the line range on which the function is defined. In the vast majority of cases this might actually be enough: Just find the first occurrence of function(...) on the first line and the first occurrence of end on the last line, then string.sub the relevant portion. This is however rather error prone; consider e.g.
function a() function b() end end
a()
func_toString(a)
since both functions are on the same line, you can't distinguish them - the debug library only provides you line info, nothing more. You could try to distinguish them by their signature since hacks exist to obtain the method signature, but that would fail here as well since both functions have the same signature. You could try gsubing them out based on their names, but remember that functions can be anonymous in Lua.
Lua also provides the string.dump function to obtain the bytecode of a function. I highly doubt that this is of any use to you; theoretically you could decompile it to get back a "Lua" representation of what the function does, but it would hardly be recognizable or readable.
I'm trying to change the behavior of a Lua function, by adding code to the start or end of it. Because this is a mod for a game, I can't edit the function directly, so I have to override it instead. I accomplish this by storing a reference to the original function in a local variable, then redefining the function with my own, which calls the original one along with any prefix or postfix code I need to add, like so:
local base_exampleFunction = ExampleBaseGameClass.exampleFunction
function ExampleBaseGameClass.exampleFunction(param1, param2)
--Prefix code goes here
base_exampleFunction(param1, param2);
--Postfix code goes here
end
This works fine for functions defined with the ClassName.functionName syntax, but some functions use ClassName:functionName instead, which from what I understand, are functions that pass a reference to the class instance as the first parameter. I can't figure out how to prefix/postfix these functions, as I get the following error when declaring a variable to hold the original function if I try the same approach:
attempted index: exampleFunction of non-table: null
Is there a way to make this work?
: functions are just scary ways of saying "the first argument is self".
So, ExampleBaseGameClass:exampleFunction(param2) is equivelent to ExampleBaseGameClass:exampleFunction(ExampleBaseGameClass, param2)! It's just got self at the beginning, and functions declared with : will have an invisible self variable appear out of nowhere.
local a = {}
function a.b(self)
print(self)
end
function a:c()
print(self)
end
-- a.c(a) == a:c()
-- a:b() == a.b(a)
-- a:b() == a:c()
Using this idea, we can simply prepend an argument (it does not have to be called "self", it just has to be the first argument).
This should work, unless there is a part of your Lua environment (eg, funky metatables) which would prevent such a thing:
local base_exampleFunction = ExampleBaseGameClass.exampleFunction
function ExampleBaseGameClass.exampleFunction(self, param1, param2)
--Prefix code goes here
base_exampleFunction(self, param1, param2);
--Postfix code goes here
end
The lua library takes advantage of the first argument being the calling object for it's string library. Notice how ("hello"):gsub() works--by passing the string itself as the first argument!
I would need to call a function that is declared as a string
i.e local abc = "function x() print('math.sqrt(25)') end"
i've tried load(abc) that gives me no errors but nothing is printed and print(load(abc)) that returns function: 0000000000ebcea0
I've made another tries but nothing seems to suit my case (or probably i'm just bad)
Please read the manual...
If there are no syntactic errors, load returns the compiled chunk as a
function
If you want that function to be called, call it.
local abc = "function x() print('math.sqrt(25)') end"
local loadedFunction = load(abc)
loadedFunction()
or simply
load(abc)()
Of course you should check wether load actually succeeded.
If you want something to happen you probably should also call the function that has been defined by the function you loaded.
load(abc)()
x()
I have been getting the following error with this section of code:
[ERROR] lua/entities/cook/init.lua:58: attempt to index global 'self' (a nil value)1. cooked - lua/entities/cook/init.lua:58
The function starts at line 57, and when I remove line 58 (local Pos = self.Entity:GetPos() , it just gives the same error message for line 61.
function cooked()
local Pos = self.Entity:GetPos()
local roll = math.random(1, 5);
if roll == 5 then
self.Entity:EmitSound("phx/explode06.wav")
self.Entity:Remove()
else
local createfood = ents.Create("food")
createfood:SetPos(Pos + Vector(0,10,100))
createfood:Spawn()
self:SendLua("GAMEMODE:AddNotify(\"You finish cooking the food and package the product!\", NOTIFY_GENERIC, 4)")
end
end
It's unclear what self should be. The error says it's a global, which is consistent with the code you have shown.
But, self is almost exclusively used as a formal parameter to a function and an implicit one at that.
When self is implicit, the function is called a method because the intent is for it to access fields in a table passed into self. The method value is almost always held by a field in the same table (or at least, available as such via metamethods).
The colon syntax in a function definition creates a method.
If cooked was a method then it would make sense for it to access self. But cooked is a global.
You might have meant:
function sometable:cooked()
-- ...
-- self is an implicit formal parameter
-- ...
end
How to read the Lua statement above:
access sometable as a table
assign its "cooked" field the function value created by the function definition.
(The function definition is compiled from the method syntax, so in the body, self is the first formal parameter, which is implicit.)
The method could be called like:
sometable:cooked() -- passes sometable as self
The colon syntax in a field function call is a method call.
How to read the Lua statement above:
access sometable as a table,
index its "cooked" field,
call its value as a function, passing sometable as the first parameter,
discard the result list.
Oddities:
The syntax for methods is just "syntactic sugar." Methods values are no different than other function values:
A function created from a function definition using the any syntax can be called using any function call syntax.
A method need not be called using the method call syntax.
A non-method can be called using the method call syntax.
self is not reserved so it can be used as any identifier is.
Self is nil, so how do you call cooked()?
It has to be self.cooked(self) or self:cooked(), where self is the table you want to use as self in your function.
How do I call a function that needs to be called from above its creation? I read something about forward declarations, but Google isn't being helpful in this case. What is the correct syntax for this?
Lua is a dynamic language and functions are just a kind of value that can be called with the () operator. So you don't really need to forward declare the function so much as make sure that the variable in scope when you call it is the variable you think it is.
This is not an issue at all for global variables containing functions, since the global environment is the default place to look to resolve a variable name. For local functions, however, you need to make sure the local variable is already in scope at the lexical point where you need to call the value it stores, and also make sure that at run time it is really holding a value that can be called.
For example, here is a pair of mutually recursive local functions:
local a,b
a = function() return b() end
b = function() return a() end
Of course, that is also an example of using tail calls to allow infinite recursion that does nothing, but the point here is the declarations. By declaring the variables with local before either has a function stored in it, those names are known to be local variables in lexical scope of the rest of the example. Then the two functions are stored, each referring to the other variable.
You can forward declare a function by declaring its name before declaring the actual function body:
local func1
local func2 = function()
func1()
end
func1 = function()
--do something
end
However forward declarations are only necessary when declaring functions with local scope. That is generally what you want to do, but Lua also supports a syntax more like C, in which case forward declaration is not necessary:
function func2()
func1()
end
function func1()
--do something
end
Testing under the embedded lua in Freeswitch, forward declaration does not work:
fmsg("CRIT", "It worked.")
function fmsg(infotype, msg)
freeswitch.consoleLog(infotype, msg .. "\n")
end
result:
[ERR] mod_lua.cpp:203 /usr/local/freeswitch/scripts/foo.lua:1: attempt to call global 'fmsg' (a nil value)
Reversing the order does (duh) work.
To comprehend how forward referencing in Lua works compared to C, you must understand the a fundamental difference between C compilation and the Lua execution.
In C, forward referencing is a compile time mechanism. Hence if you include a forward declaration template in a C module then any of your code following will employ this template in compiling the call. You may or may not include the function implementation in the same module, in which case both declarations must be semantically identical or the compiler will error. Since this is a compile time construct, the compiled code can be executed in any order.
In Lua, forward referencing is runtime mechanism, in that the compiled function generates a function prototype internally within the code, but this is only accessible as a runtime Lua variable or value after the execution has
passed over the declaration creating a Lua closure. Here the declaration order within the source is immaterial. It is the execution order that is important: if the closure hasn't been bound to the variable yet, then the execution will throw a "nil value" exception.If you are using a local variable to hold the function value, then normal local scoping rules still apply: the local declaration must precede its use in the source and must be within scope, otherwise the compiler will compile in the wrong global or outer local reference. So forward referencing using locals as discussed in other answer will work, but only if the Protos are bound to closures before the first call is executed.
Doesn't work for me if I try to call the function before definition. I am using this Lua script in nginx conf.
lua entry thread aborted: runtime error: lua_redirect.lua:109: attempt to call global 'throwErrorIfAny' (a nil value)
Code snippet -
...
throwErrorIfAny()
...
function throwErrorIfAny()
ngx.say("request not allowed")
ngx.exit(ngx.HTTP_OK)
end
Given some other answers have also pointed out that it didn't work for them either, it is possible that forward declaration of Lua doesn't work with other tools.
PS : It works fine if I put the function definition before and then call it after wards.
If you use OOP you can call any function member prior its "definition".
local myClass = {}
local myClass_mt = { __index = myClass }
local function f1 (self)
print("f1")
self:later() --not yet "delared" local function
end
local function f2 (self)
print("f2")
self:later() --not yet "declared" local function
end
--...
--later in your source declare the "later" function:
local function later (self)
print("later")
end
function myClass.new() -- constructor
local this = {}
this = {
f1 = f1,
f2 = f2,
later = later, --you can access the "later" function through "self"
}
setmetatable(this, myClass_mt)
return this
end
local instance = myClass.new()
instance:f1()
instance:f2()
Program output:
f1
later
f2
later