Create a new file in Lua/LuaFileSystem - lua

I've looked over the Lua & LuaFileSystem Docs and have yet to find a way to create a new file, I also scouted around on here but to the same end.
As a note, the solution I'm looking for has to be OS neutral to ensure portability, but I'm happy to get different answers for different systems.

Example (writing "Hello World" into test.txt):
$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> file = io.open("test.txt", "w")
> file:write("Hello World")
> file:close()
> ^D
$ cat test.txt
Hello World
See also: Lua IO tutorial

Related

Cannot install FFI library for Lua/LuaJIT

I have already correctly installed Lua, LuaJIT and Luarocks (I can run each of them in the terminal without any errors too). I'm trying to run the command luarocks install luaffi but it is returning an error
Error: No results matching query were found for Lua 5.4
To check if it is available for other Lua versions, use --check-lua-versions.
So I tried running luarocks install luaffi --check-lua-versions which also returns the following:
Checking if available for other Lua versions...
Checking for Lua 5.1...
Checking for Lua 5.2...
Checking for Lua 5.3...
Error: No results matching query were found for Lua 5.4.
luaffi is not available for any Lua versions. "
I'm completely lost, I cannot use ffi in my code because of this (It returns module "ffi" not found).
After the build (See: https://github.com/zhaozg/lua-ffi ) you can use Lua 5.1 to test it.
€ cat luajit_sleep.lua
--[[
Lua JIT ffi Example - Implementing a Lua sleep() function from C
]]
local ffi = require("ffi")
ffi.cdef[[void Sleep(int ms); int poll(struct pollfd *fds, unsigned long nfds, int timeout);]]
local sleep
if ffi.os == "Windows" then
function sleep(s) ffi.C.Sleep(s*1000) end
else
function sleep(s) ffi.C.poll(nil, 0, s*1000) end
end
return sleep
€ /bin/lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> sleep = require('luajit_sleep')
> sleep(1) -- CPU friendly sleep() function :-)
It uses (lsof | grep lua)...
/usr/local/lib/lua/5.1/ffi.so
EDIT - Found the Command to install: https://luarocks.org/modules/colesbury/luaffi
Do: luarocks install --server=https://luarocks.org/dev luaffi --verbose
Also goes to...
os.execute: chmod '0755' '/usr/local/lib/lua/5.1/ffi.so'
Results: 1
1 (number): 0
luaffi scm-1 is now installed in /usr/local (license: BSD)

Why does formatting a number round differently in LuaJIT vs. Lua?

Using string.format() (which supposedly defers to C's sprintf())) to format a number in LuaJIT rounds differently than every other Lua interpreter I've tried:
$ lua -v
Lua 5.4.1 Copyright (C) 1994-2020 Lua.org, PUC-Rio
$ lua -e 'print(string.format("%.4f", 32.90625))'
32.9062
$ luajit -v
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2020 Mike Pall. http://luajit.org/
$ luajit -e 'print(string.format("%.4f", 32.90625))'
32.9063
Why does this happen and how can I‌ make it stop? I'd like to tell LuaJIT to round the same direction every other Lua interpreter does.

Why -l option in lua interpreter acts strangely?

You can execute a sequence of chunks by giving them all as arguments
to the stand-alone interpreter, with the -l option. For instance, if
you have a file a with a single statement x=1 and another file b with
the statement print(x), the command line
prompt> lua -la -lb
will run the chunk in a, then the one in b, which will print the
expected 1.
The above is from the following link: https://www.lua.org/pil/1.1.html. Yet, when I was trying it out, I got a syntax error.
So, in file a.lua, I have only one line, which is a=1. Then in file b.lua, I have also only one line print("the value of a is:",a) . Then,
:~$ lua -i -la -lb
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
the value of a is: true
>
:~$
:~$ lua -la -lb
the value of a is: true
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
Why did it print out "the value of a is: true"? rather than "the value of a is: 1"?
Any comments are greatly appreciated.
The error is because you are use a = 1 in your file a.lua, unlike in the example where a.lua contains x = 1 and b.lua contains print(x).
Using a rather then x means your using the same a variable being written to when -la completes, changing it to true.
This happens because the option -l name is equal to name = require("name"). When require completes, on a file that returns no result to it, require will return true.
your command lua -la -lb would look something like this to lua:
a = require("a") --this returning true after it completes
b = require("b") --this printing the value of `a` which will always be `true`
Sources:
Similar question answered on Lua-Users: command-line -l option issue
Egor Skriptunoff's Comment on this question

local luarocks package install (luarepl) does not work

I'm new to luarocks and I just tried to install luarepl.
The installation apparently went fine:
$ luarocks install luarepl
Installing https://luarocks.org/luarepl-0.8-1.rockspec...
Using https://luarocks.org/luarepl-0.8-1.rockspec... switching to 'build' mode
Updating manifest for /Users/me/.luarocks/lib/luarocks/rocks-5.1
luarepl 0.8-1 is now built and installed in /Users/me/.luarocks (license: MIT/X11)
but if I try to launch the executable, it seems that the installation is broken:
$ ~/.luarocks/bin/rep.lua
/usr/local/bin/lua5.1: ...cks/lib/luarocks/rocks-5.1/luarepl/0.8-1/bin/rep.lua:23: module 'repl.console' not found:
no field package.preload['repl.console']
no file './repl/console.lua'
no file '/usr/local/share/lua/5.1/repl/console.lua'
no file '/usr/local/share/lua/5.1/repl/console/init.lua'
no file '/usr/local/lib/lua/5.1/repl/console.lua'
no file '/usr/local/lib/lua/5.1/repl/console/init.lua'
no file './repl/console.so'
no file '/usr/local/lib/lua/5.1/repl/console.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './repl.so'
no file '/usr/local/lib/lua/5.1/repl.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
[C]: in function 'require'
...cks/lib/luarocks/rocks-5.1/luarepl/0.8-1/bin/rep.lua:23: in main chunk
[C]: ?
I look into the ~/.luarocks dir:
$ cd ~ ; find .luarocks
.luarocks
.luarocks/bin
.luarocks/bin/rep.lua
.luarocks/config.lua
.luarocks/lib
.luarocks/lib/luarocks
.luarocks/lib/luarocks/rocks-5.1
.luarocks/lib/luarocks/rocks-5.1/luarepl
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/bin
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/bin/rep.lua
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/doc
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/doc/COPYING
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/doc/IDEAS.md
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/doc/plugins.md
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/doc/README.md
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/doc/Roadmap.md
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/luarepl-0.8-1.rockspec
.luarocks/lib/luarocks/rocks-5.1/luarepl/0.8-1/rock_manifest
.luarocks/lib/luarocks/rocks-5.1/manifest
.luarocks/share
.luarocks/share/lua
.luarocks/share/lua/5.1
.luarocks/share/lua/5.1/repl
.luarocks/share/lua/5.1/repl/console.lua
.luarocks/share/lua/5.1/repl/init.lua
.luarocks/share/lua/5.1/repl/plugins
.luarocks/share/lua/5.1/repl/plugins/autoreturn.lua
.luarocks/share/lua/5.1/repl/plugins/completion.lua
.luarocks/share/lua/5.1/repl/plugins/example.lua
.luarocks/share/lua/5.1/repl/plugins/filename_completion.lua
.luarocks/share/lua/5.1/repl/plugins/history.lua
.luarocks/share/lua/5.1/repl/plugins/keep_last_eval.lua
.luarocks/share/lua/5.1/repl/plugins/linenoise.lua
.luarocks/share/lua/5.1/repl/plugins/pretty_print.lua
.luarocks/share/lua/5.1/repl/plugins/rcfile.lua
.luarocks/share/lua/5.1/repl/plugins/rlwrap.lua
.luarocks/share/lua/5.1/repl/plugins/semicolon_suppress_output.lua
.luarocks/share/lua/5.1/repl/sync.lua
.luarocks/share/lua/5.1/repl/utils.lua
and I can find that stuff.
I tried adding a line to config.lua to load packages from local installation to no avail:
$ cat ~/.luarocks/config.lua
local_by_default=true
(removing it has no effect)
did I miss some obvious step?
running luarocks with no arguments gives me:
CONFIGURATION
Lua version: 5.1
Configuration files:
System: /usr/local/etc/luarocks51/config-5.1.lua (ok)
User : /Users/me/.luarocks/config.lua (ok)
Rocks trees in use:
/Users/me/.luarocks ("user")
/usr/local ("system")
it seems that the user rock tree is not in the package path:
$ lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> print(package.path)
./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua
why?
From https://github.com/luarocks/luarocks/wiki/Using-LuaRocks:
Most LuaRocks installations will feature two rocks trees:
"system" rock tree (used by default)
"user" rock tree
To be able to use the module, we need to make sure that Lua can find that dkjson.lua file when we run require("dkjson"). You can check your Lua paths from the Lua environment, using
print(package.path)
print(package.cpath)
These variables can be pre-configured from outside Lua, using the LUA_PATH and LUA_CPATH environment variables.
If you installed both Lua and LuaRocks in their default directories (/usr/local on Linux and Mac OSX), then the "system" tree is /usr/local and it will work by default. However, the "user" tree (for installing rocks without admin privileges) is not detected by Lua by default. For that we'll need to configure these environment variables.
LuaRocks offers a semi-automated way to do this. If you type the following command:
luarocks path --bin
it will print commands suitable for your platform for setting up your environment. On typical Unix terminal environments, you can type this:
eval $(luarocks path --bin)
and it apply the changes, temporarily, to your shell. To have these variables set permanently, you have to configure the environment variables to your shell configuration (for example, by adding the above line to your .bashrc file if your shell is Bash).

Luac: read version 1280.1; expected at most 80.1

I try to load a .lc file that I compiled with luac beforehand:
print("Hello World!")
and
luac -o helloworld.lc helloworld.lua
in order to get the compilation. However, as I am importing that module I am getting:
\lua\ai\helloworld.lc too new: read version 1280.1; expected at most 80.1
This is my luac -v
E:\lua\lua-5.0.3\bin>luac -v
Lua 5.0.3 Copyright (C) 1994-2006 Tecgraf, PUC-Rio
The game I am modding is actually using Lua 5.0 which is why I don't really understand why I am seeing this error..

Resources