[FUN] Some curious facts about Lua

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[FUN] Some curious facts about Lua

Egor Skriptunoff-2
1)
Function "require" with empty module name
   require("")
actually loads the module "init.lua" from your Lua modules forder.
I found it very handy: I put things-needed-in-every-script in "init.lua",
and all my scripts are usually started with
   require''
 
Of course, if you have hidden file ".lua", it will be loaded instead of "init.lua"
 
Strictly speaking, this useful feature should be considered as a "require" bug/quirk:
the statement require("") means user's intention to load module ".lua",
but another module "init.lua" is silently loaded instead when ".lua" is absent.
It acts as if this file "init,lua" in located in the folder with empty name.
But there is no folder with empty name on my disk :-)
 
 
2)
VARARG instruction is described as
   R(A), R(A+1), ..., R(A+B-2) = vararg
When B=1, there are no registers at the LHS,
so the instruction is actually "copy vararg into nothing".
It's funny that Lua sometimes generates this instruction with B=1.
For example, for this line of code:
   local x = 42, ...
(There is no need to optimize this, as all use cases are silly/erroneous)

Reply | Threaded
Open this post in threaded view
|

Re: [FUN] Some curious facts about Lua

Roberto Ierusalimschy
> 1)
> Function "require" with empty module name
>    require("")
> actually loads the module "init.lua" from your Lua modules forder.

What is the path (package.path)?

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: [FUN] Some curious facts about Lua

Egor Skriptunoff-2
On Thu, Jan 31, 2019 at 3:01 PM Roberto Ierusalimschy wrote:
> Function "require" with empty module name
>    require("")
> actually loads the module "init.lua" from your Lua modules forder.

What is the path (package.path)?



This feature works with the default value of (package.path).

The gist of this quirk is that both Linux and Windows
ignore superfluous path separators.
For example, the following file
   ~/My_Lua_Scripts/howling.at.the.lua
is accessible also through another path:
   ~///My_Lua_Scripts////////howling.at.the.lua

So, the pattern
   /usr/local/share/lua/5.3/?/init.lua
after replacing "?" with empty string becomes
   /usr/local/share/lua/5.3//init.lua
which is equivalent (from the OS viewpoint) to
   /usr/local/share/lua/5.3/init.lua