All library functions no longer require self

This commit is contained in:
lumi 2025-06-28 05:58:29 +10:00
parent ccae0046fb
commit 27c40c3244
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
9 changed files with 57 additions and 68 deletions

View File

@ -38,7 +38,8 @@ type Result<T> = std::result::Result<T, Error>;
/// Items exported by the `lb:fs` library. /// Items exported by the `lb:fs` library.
/// ///
/// This library can be acquired by calling `require` in Lua. /// This library can be acquired by calling
/// [`require("lb:fs")`](https://www.lua.org/manual/5.1/manual.html#pdf-require) in Lua.
/// ///
/// ```lua /// ```lua
/// local fs = require("lb:fs"); /// local fs = require("lb:fs");
@ -53,39 +54,39 @@ impl lb_fslib {
Self Self
} }
pub async extern "Lua-C" fn read(&self, path: &str) -> Result<Vec<u8>> { pub async extern "Lua-C" fn read(path: &str) -> Result<Vec<u8>> {
Ok(tokio::fs::read(path).await?) Ok(tokio::fs::read(path).await?)
} }
pub extern "Lua-C" fn read_sync(&self, path: &str) -> Result<Vec<u8>> { pub extern "Lua-C" fn read_sync(path: &str) -> Result<Vec<u8>> {
Ok(std::fs::read(path)?) Ok(std::fs::read(path)?)
} }
pub async extern "Lua-C" fn write(&self, path: &str, contents: &[u8]) -> Result<()> { pub async extern "Lua-C" fn write(path: &str, contents: &[u8]) -> Result<()> {
Ok(tokio::fs::write(path, contents).await?) Ok(tokio::fs::write(path, contents).await?)
} }
pub extern "Lua-C" fn write_sync(&self, path: &str, contents: &[u8]) -> Result<()> { pub extern "Lua-C" fn write_sync(path: &str, contents: &[u8]) -> Result<()> {
Ok(std::fs::write(path, contents)?) Ok(std::fs::write(path, contents)?)
} }
pub async extern "Lua-C" fn read_dir(&self, path: &str) -> Result<lb_read_dir> { pub async extern "Lua-C" fn read_dir(path: &str) -> Result<lb_read_dir> {
Ok(tokio::fs::read_dir(path).await?.into()) Ok(tokio::fs::read_dir(path).await?.into())
} }
pub extern "Lua-C" fn read_dir_sync(&self, path: &str) -> Result<lb_read_dir_sync> { pub extern "Lua-C" fn read_dir_sync(path: &str) -> Result<lb_read_dir_sync> {
Ok(std::fs::read_dir(path)?.into()) Ok(std::fs::read_dir(path)?.into())
} }
pub extern "Lua-C" fn walk_dir(&self, path: &str) -> lb_walk_dir { pub extern "Lua-C" fn walk_dir(path: &str) -> lb_walk_dir {
walkdir::WalkDir::new(path).into_iter().into() walkdir::WalkDir::new(path).into_iter().into()
} }
pub extern "Lua-C" fn glob(&self, pattern: &str) -> Result<lb_glob_dir> { pub extern "Lua-C" fn glob(pattern: &str) -> Result<lb_glob_dir> {
self.glob_dir(".", pattern) Self::glob_dir(".", pattern)
} }
pub extern "Lua-C" fn glob_dir(&self, path: &str, pattern: &str) -> Result<lb_glob_dir> { pub extern "Lua-C" fn glob_dir(path: &str, pattern: &str) -> Result<lb_glob_dir> {
let prefix = PathBuf::from(path); let prefix = PathBuf::from(path);
let iter = walkdir::WalkDir::new(path).min_depth(1).into_iter(); let iter = walkdir::WalkDir::new(path).min_depth(1).into_iter();
let matcher = globset::GlobSet::builder() let matcher = globset::GlobSet::builder()
@ -99,11 +100,11 @@ impl lb_fslib {
}) })
} }
pub extern "Lua-C" fn temp_dir(&self) -> Result<lb_temp_dir> { pub extern "Lua-C" fn temp_dir() -> Result<lb_temp_dir> {
Ok(tempfile::tempdir()?.into()) Ok(tempfile::tempdir()?.into())
} }
pub extern "Lua-C" fn temp_dir_in(&self, path: &str) -> Result<lb_temp_dir> { pub extern "Lua-C" fn temp_dir_in(path: &str) -> Result<lb_temp_dir> {
Ok(tempfile::tempdir_in(path)?.into()) Ok(tempfile::tempdir_in(path)?.into())
} }
} }

View File

@ -53,27 +53,27 @@ impl lb_netlib {
} }
/// An IPv4 address representing localhost: `127.0.0.1` /// An IPv4 address representing localhost: `127.0.0.1`
pub extern "Lua-C" fn localhost_v4(&self) -> lb_ipaddr { pub extern "Lua-C" fn localhost_v4() -> lb_ipaddr {
lb_ipaddr(Ipv4Addr::LOCALHOST.into()) lb_ipaddr(Ipv4Addr::LOCALHOST.into())
} }
/// An IPv6 address representing localhost: `::1` /// An IPv6 address representing localhost: `::1`
pub extern "Lua-C" fn localhost_v6(&self) -> lb_ipaddr { pub extern "Lua-C" fn localhost_v6() -> lb_ipaddr {
lb_ipaddr(Ipv6Addr::LOCALHOST.into()) lb_ipaddr(Ipv6Addr::LOCALHOST.into())
} }
/// An IPv4 address representing an unspecified address: `0.0.0.0` /// An IPv4 address representing an unspecified address: `0.0.0.0`
pub extern "Lua-C" fn unspecified_v4(&self) -> lb_ipaddr { pub extern "Lua-C" fn unspecified_v4() -> lb_ipaddr {
lb_ipaddr(Ipv4Addr::UNSPECIFIED.into()) lb_ipaddr(Ipv4Addr::UNSPECIFIED.into())
} }
/// An IPv6 address representing an unspecified address: `::` /// An IPv6 address representing an unspecified address: `::`
pub extern "Lua-C" fn unspecified_v6(&self) -> lb_ipaddr { pub extern "Lua-C" fn unspecified_v6() -> lb_ipaddr {
lb_ipaddr(Ipv6Addr::UNSPECIFIED.into()) lb_ipaddr(Ipv6Addr::UNSPECIFIED.into())
} }
/// An IPv4 address representing the broadcast address: `255.255.255.255` /// An IPv4 address representing the broadcast address: `255.255.255.255`
pub extern "Lua-C" fn broadcast_v4(&self) -> lb_ipaddr { pub extern "Lua-C" fn broadcast_v4() -> lb_ipaddr {
lb_ipaddr(Ipv4Addr::BROADCAST.into()) lb_ipaddr(Ipv4Addr::BROADCAST.into())
} }
@ -83,7 +83,6 @@ impl lb_netlib {
/// [`lb_socketaddr`], the IP address part of the socket address is returned. Otherwise, parses /// [`lb_socketaddr`], the IP address part of the socket address is returned. Otherwise, parses
/// `s` as an IP address string. Both IPv4 or IPv6 addresses are supported. /// `s` as an IP address string. Both IPv4 or IPv6 addresses are supported.
pub extern "Lua" fn ipaddr( pub extern "Lua" fn ipaddr(
&self,
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>, addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
) -> Result<lb_ipaddr> { ) -> Result<lb_ipaddr> {
if __istype(__ct.lb_ipaddr, addr) { if __istype(__ct.lb_ipaddr, addr) {
@ -91,11 +90,11 @@ impl lb_netlib {
} else if __istype(__ct.lb_socketaddr, addr) { } else if __istype(__ct.lb_socketaddr, addr) {
s.ip() s.ip()
} else { } else {
self.__parse_ipaddr(addr) Self::__parse_ipaddr(addr)
} }
} }
extern "Lua-C" fn __parse_ipaddr(&self, addr: &str) -> Result<lb_ipaddr> { extern "Lua-C" fn __parse_ipaddr(addr: &str) -> Result<lb_ipaddr> {
Ok(addr.parse()?) Ok(addr.parse()?)
} }
@ -109,28 +108,27 @@ impl lb_netlib {
/// ///
/// If `port` is not specified, `0` is used as the default. /// If `port` is not specified, `0` is used as the default.
pub extern "Lua" fn socketaddr( pub extern "Lua" fn socketaddr(
&self,
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>, addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
port: Option<u16>, port: Option<u16>,
) -> Result<lb_socketaddr> { ) -> Result<lb_socketaddr> {
if port != () { if port != () {
self.__new_skaddr(self.ipaddr(addr), port) Self::__new_skaddr(Self::ipaddr(addr), port)
} else { } else {
if __istype(__ct.lb_socketaddr, addr) { if __istype(__ct.lb_socketaddr, addr) {
__new(__ct.lb_socketaddr, addr) // copy constructor __new(__ct.lb_socketaddr, addr) // copy constructor
} else if __istype(__ct.lb_ipaddr, addr) { } else if __istype(__ct.lb_ipaddr, addr) {
self.__new_skaddr(addr, 0) // default port 0 Self::__new_skaddr(addr, 0) // default port 0
} else { } else {
self.__parse_skaddr(addr) Self::__parse_skaddr(addr)
} }
} }
} }
extern "Lua-C" fn __new_skaddr(&self, ip: &lb_ipaddr, port: u16) -> lb_socketaddr { extern "Lua-C" fn __new_skaddr(ip: &lb_ipaddr, port: u16) -> lb_socketaddr {
SocketAddr::new(ip.0, port).into() SocketAddr::new(ip.0, port).into()
} }
extern "Lua-C" fn __parse_skaddr(&self, addr: &str) -> Result<lb_socketaddr> { extern "Lua-C" fn __parse_skaddr(addr: &str) -> Result<lb_socketaddr> {
Ok(if let Ok(addr) = addr.parse() { Ok(if let Ok(addr) = addr.parse() {
SocketAddr::new(addr, 0).into() // default port 0 SocketAddr::new(addr, 0).into() // default port 0
} else { } else {
@ -141,54 +139,51 @@ impl lb_netlib {
/// Creates a new TCP socket configured for IPv4. /// Creates a new TCP socket configured for IPv4.
/// ///
/// See [`TcpSocket::new_v4`]. /// See [`TcpSocket::new_v4`].
pub extern "Lua-C" fn tcp(&self) -> Result<lb_tcpsocket> { pub extern "Lua-C" fn tcp() -> Result<lb_tcpsocket> {
Ok(Some(TcpSocket::new_v4()?).into()) Ok(Some(TcpSocket::new_v4()?).into())
} }
/// Creates a new TCP socket configured for IPv6. /// Creates a new TCP socket configured for IPv6.
/// ///
/// See [`TcpSocket::new_v6`]. /// See [`TcpSocket::new_v6`].
pub extern "Lua-C" fn tcp6(&self) -> Result<lb_tcpsocket> { pub extern "Lua-C" fn tcp6() -> Result<lb_tcpsocket> {
Ok(Some(TcpSocket::new_v6()?).into()) Ok(Some(TcpSocket::new_v6()?).into())
} }
pub async extern "Lua" fn bind_tcp( pub async extern "Lua" fn bind_tcp(
&self,
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>, addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
port: Option<u16>, port: Option<u16>,
) -> Result<lb_tcpsocket> { ) -> Result<lb_tcpsocket> {
let addr = self.socketaddr(addr, port); let addr = Self::socketaddr(addr, port);
let socket; let socket;
if addr.ip().is_v6() { if addr.ip().is_v6() {
socket = self.tcp6(); socket = Self::tcp6();
} else { } else {
socket = self.tcp(); socket = Self::tcp();
} }
socket.bind(addr); socket.bind(addr);
socket socket
} }
pub async extern "Lua" fn connect_tcp( pub async extern "Lua" fn connect_tcp(
&self,
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>, addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
port: Option<u16>, port: Option<u16>,
) -> Result<lb_tcpstream> { ) -> Result<lb_tcpstream> {
let addr = self.socketaddr(addr, port); let addr = Self::socketaddr(addr, port);
let socket; let socket;
if addr.ip().is_v6() { if addr.ip().is_v6() {
socket = self.tcp6(); socket = Self::tcp6();
} else { } else {
socket = self.tcp(); socket = Self::tcp();
} }
socket.connect(addr) socket.connect(addr)
} }
pub async extern "Lua" fn listen_tcp( pub async extern "Lua" fn listen_tcp(
&self,
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>, addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
port: Option<u16>, port: Option<u16>,
) -> Result<lb_tcplistener> { ) -> Result<lb_tcplistener> {
self.bind_tcp(addr, port).listen(1024) Self::bind_tcp(addr, port).listen(1024)
} }
} }
@ -200,7 +195,7 @@ impl lb_netlib {
/// ///
/// ```lua /// ```lua
/// local net = require("lb:net"); /// local net = require("lb:net");
/// local addr = net:ipaddr("127.0.0.1"); -- ipv4 loopback address /// local addr = net.ipaddr("127.0.0.1"); -- ipv4 loopback address
/// ///
/// assert(addr:is_v4()); /// assert(addr:is_v4());
/// assert(addr:is_loopback()); /// assert(addr:is_loopback());

View File

@ -1,10 +1,3 @@
-- include task functions in the global scope
local task = require("lb:task") local task = require("lb:task")
sleep = task.sleep
function sleep(ms) spawn = task.spawn
return task:sleep(ms)
end
function spawn(f, ...)
return task:spawn(f, ...)
end

View File

@ -35,20 +35,20 @@ impl lb_tasklib {
Self Self
} }
pub async extern "Lua-C" fn sleep(&self, ms: f64) { pub async extern "Lua-C" fn sleep(ms: f64) {
sleep(Duration::from_secs_f64(ms / 1000.)).await; sleep(Duration::from_secs_f64(ms / 1000.)).await;
} }
pub extern "Lua" fn spawn(&self, f: function, ...) -> lb_task { pub extern "Lua" fn spawn(f: function, ...) -> lb_task {
// pack the function and its arguments into a table and pass its ref to rust. // pack the function and its arguments into a table and pass its ref to rust.
// //
// this table is used from rust-side to call the function with its args, and it's also // this table is used from rust-side to call the function with its args, and it's also
// reused to store its return values that the task handle can return when awaited. the ref // reused to store its return values that the task handle can return when awaited. the ref
// is owned by the task handle and unref'ed when it's gc'ed. // is owned by the task handle and unref'ed when it's gc'ed.
self.__spawn(__ref(__tpack(f, variadic!()))) Self::__spawn(__ref(__tpack(f, variadic!())))
} }
extern "Lua-C" fn __spawn(&self, key: c_int) -> lb_task { extern "Lua-C" fn __spawn(key: c_int) -> lb_task {
let handle = spawn(async move |cx| { let handle = spawn(async move |cx| {
// SAFETY: key is always unique, created by __ref above. // SAFETY: key is always unique, created by __ref above.
let arg = unsafe { cx.new_ref_unchecked(key) }; let arg = unsafe { cx.new_ref_unchecked(key) };

View File

@ -5,13 +5,13 @@ describe("temp files", function()
test("temp_dir cleans itself", function() test("temp_dir cleans itself", function()
local path local path
do do
local dir = fs:temp_dir() local dir = fs.temp_dir()
path = dir:path() path = dir:path()
assert(path and path ~= "") assert(path and path ~= "")
fs:write(path .. "/test.txt", "test file") fs.write(path .. "/test.txt", "test file")
assert(fs:read(path .. "/test.txt") == "test file") assert(fs.read(path .. "/test.txt") == "test file")
end end
collectgarbage() collectgarbage()
assert(not pcall(fs.read, fs, path .. "/test.txt")) assert(not pcall(fs.read, path .. "/test.txt"))
end) end)
end) end)

View File

@ -4,14 +4,14 @@ if not ok then return end
describe("tcp", function() describe("tcp", function()
describe("socket", function() describe("socket", function()
test("bind", function() test("bind", function()
local socket = net:bind_tcp("127.0.0.1") local socket = net.bind_tcp("127.0.0.1")
-- binds to the correct port -- binds to the correct port
assert(tostring(socket:local_addr():ip()) == "127.0.0.1") assert(tostring(socket:local_addr():ip()) == "127.0.0.1")
assert(socket:local_addr():port() ~= 0) assert(socket:local_addr():port() ~= 0)
-- should not be able to rebind socket -- should not be able to rebind socket
assert(not pcall(socket.bind, socket, net:socketaddr("127.0.0.1"))) assert(not pcall(socket.bind, socket, net.socketaddr("127.0.0.1")))
end) end)
end) end)
end) end)

View File

@ -74,7 +74,7 @@ describe("sleep", function()
value = "value" value = "value"
end) end)
assert(value == nil) assert(value == nil)
task:sleep(100) -- implicit await: if it's synchronous, value wouldn't change task.sleep(100) -- implicit await: if it's synchronous, value wouldn't change
assert(value == "value") assert(value == "value")
end) end)
end) end)

View File

@ -351,7 +351,7 @@ impl<'r> MetatypeBuilder<'r> {
name: impl Display, name: impl Display,
f: impl FnOnce(&mut MetatypeFunctionBuilder), f: impl FnOnce(&mut MetatypeFunctionBuilder),
) -> &mut Self { ) -> &mut Self {
write!(self.lua, "__idx.{name} = ").unwrap(); write!(self.lua, "Self.{name} = ").unwrap();
f(&mut MetatypeFunctionBuilder::new(self)); f(&mut MetatypeFunctionBuilder::new(self));
writeln!(self.lua, ";").unwrap(); writeln!(self.lua, ";").unwrap();
self.has_index = true; self.has_index = true;
@ -359,7 +359,7 @@ impl<'r> MetatypeBuilder<'r> {
} }
pub fn index_raw(&mut self, name: impl Display, value: impl Display) -> &mut Self { pub fn index_raw(&mut self, name: impl Display, value: impl Display) -> &mut Self {
writeln!(self.lua, "__idx.{name} = {value};").unwrap(); writeln!(self.lua, "Self.{name} = {value};").unwrap();
self.has_index = true; self.has_index = true;
self self
} }
@ -395,13 +395,13 @@ impl<'r> Drop for MetatypeBuilder<'r> {
write!(reg.lua, r#"do local __mt = {{}}; "#).unwrap(); write!(reg.lua, r#"do local __mt = {{}}; "#).unwrap();
if *has_index { if *has_index {
write!(reg.lua, r#"local __idx = {{}}; __mt.__index = __idx; "#).unwrap(); write!(reg.lua, r#"local Self = {{}}; __mt.__index = Self; "#).unwrap();
} }
reg.cdef.push_str(cdef); reg.cdef.push_str(cdef);
reg.lua.push_str(lua.trim_end()); reg.lua.push_str(lua.trim_end());
writeln!(reg.lua, r#"__metatype(__ct.{ct}, __mt); end;"#).unwrap(); writeln!(reg.lua, r#" __metatype(__ct.{ct}, __mt); end;"#).unwrap();
for lua in lua_includes { for lua in lua_includes {
writeln!(reg.lua, "do {}\nend;", lua.trim_end()).unwrap(); writeln!(reg.lua, "do {}\nend;", lua.trim_end()).unwrap();

View File

@ -111,8 +111,8 @@ local function main(item)
end end
return main(create_group("", function() return main(create_group("", function()
local function glob(path, pat) local function include(path, pat)
for entry in fs:glob_dir(path, pat) do for entry in fs.glob_dir(path, pat) do
local path = entry:path() local path = entry:path()
local f, err = loadfile(path) local f, err = loadfile(path)
if not f then error(err) end if not f then error(err) end
@ -120,6 +120,6 @@ return main(create_group("", function()
end end
end end
glob("tests", "**/*.lua") include("tests", "**/*.lua")
glob("crates", "*/tests/**/*.lua") include("crates", "*/tests/**/*.lua")
end)) end))