All library functions no longer require self
This commit is contained in:
parent
ccae0046fb
commit
27c40c3244
@ -38,7 +38,8 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// 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
|
||||
/// local fs = require("lb:fs");
|
||||
@ -53,39 +54,39 @@ impl lb_fslib {
|
||||
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?)
|
||||
}
|
||||
|
||||
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)?)
|
||||
}
|
||||
|
||||
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?)
|
||||
}
|
||||
|
||||
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)?)
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
pub extern "Lua-C" fn glob(&self, pattern: &str) -> Result<lb_glob_dir> {
|
||||
self.glob_dir(".", pattern)
|
||||
pub extern "Lua-C" fn glob(pattern: &str) -> Result<lb_glob_dir> {
|
||||
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 iter = walkdir::WalkDir::new(path).min_depth(1).into_iter();
|
||||
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())
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
@ -53,27 +53,27 @@ impl lb_netlib {
|
||||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
||||
@ -83,7 +83,6 @@ impl lb_netlib {
|
||||
/// [`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.
|
||||
pub extern "Lua" fn ipaddr(
|
||||
&self,
|
||||
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
|
||||
) -> Result<lb_ipaddr> {
|
||||
if __istype(__ct.lb_ipaddr, addr) {
|
||||
@ -91,11 +90,11 @@ impl lb_netlib {
|
||||
} else if __istype(__ct.lb_socketaddr, addr) {
|
||||
s.ip()
|
||||
} 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()?)
|
||||
}
|
||||
|
||||
@ -109,28 +108,27 @@ impl lb_netlib {
|
||||
///
|
||||
/// If `port` is not specified, `0` is used as the default.
|
||||
pub extern "Lua" fn socketaddr(
|
||||
&self,
|
||||
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
|
||||
port: Option<u16>,
|
||||
) -> Result<lb_socketaddr> {
|
||||
if port != () {
|
||||
self.__new_skaddr(self.ipaddr(addr), port)
|
||||
Self::__new_skaddr(Self::ipaddr(addr), port)
|
||||
} else {
|
||||
if __istype(__ct.lb_socketaddr, addr) {
|
||||
__new(__ct.lb_socketaddr, addr) // copy constructor
|
||||
} else if __istype(__ct.lb_ipaddr, addr) {
|
||||
self.__new_skaddr(addr, 0) // default port 0
|
||||
Self::__new_skaddr(addr, 0) // default port 0
|
||||
} 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()
|
||||
}
|
||||
|
||||
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() {
|
||||
SocketAddr::new(addr, 0).into() // default port 0
|
||||
} else {
|
||||
@ -141,54 +139,51 @@ impl lb_netlib {
|
||||
/// Creates a new TCP socket configured for IPv4.
|
||||
///
|
||||
/// 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())
|
||||
}
|
||||
|
||||
/// Creates a new TCP socket configured for IPv6.
|
||||
///
|
||||
/// 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())
|
||||
}
|
||||
|
||||
pub async extern "Lua" fn bind_tcp(
|
||||
&self,
|
||||
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
|
||||
port: Option<u16>,
|
||||
) -> Result<lb_tcpsocket> {
|
||||
let addr = self.socketaddr(addr, port);
|
||||
let addr = Self::socketaddr(addr, port);
|
||||
let socket;
|
||||
if addr.ip().is_v6() {
|
||||
socket = self.tcp6();
|
||||
socket = Self::tcp6();
|
||||
} else {
|
||||
socket = self.tcp();
|
||||
socket = Self::tcp();
|
||||
}
|
||||
socket.bind(addr);
|
||||
socket
|
||||
}
|
||||
|
||||
pub async extern "Lua" fn connect_tcp(
|
||||
&self,
|
||||
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
|
||||
port: Option<u16>,
|
||||
) -> Result<lb_tcpstream> {
|
||||
let addr = self.socketaddr(addr, port);
|
||||
let addr = Self::socketaddr(addr, port);
|
||||
let socket;
|
||||
if addr.ip().is_v6() {
|
||||
socket = self.tcp6();
|
||||
socket = Self::tcp6();
|
||||
} else {
|
||||
socket = self.tcp();
|
||||
socket = Self::tcp();
|
||||
}
|
||||
socket.connect(addr)
|
||||
}
|
||||
|
||||
pub async extern "Lua" fn listen_tcp(
|
||||
&self,
|
||||
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
|
||||
port: Option<u16>,
|
||||
) -> 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
|
||||
/// 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_loopback());
|
||||
|
@ -1,10 +1,3 @@
|
||||
-- include task functions in the global scope
|
||||
local task = require("lb:task")
|
||||
|
||||
function sleep(ms)
|
||||
return task:sleep(ms)
|
||||
end
|
||||
|
||||
function spawn(f, ...)
|
||||
return task:spawn(f, ...)
|
||||
end
|
||||
sleep = task.sleep
|
||||
spawn = task.spawn
|
||||
|
@ -35,20 +35,20 @@ impl lb_tasklib {
|
||||
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;
|
||||
}
|
||||
|
||||
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.
|
||||
//
|
||||
// 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
|
||||
// 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| {
|
||||
// SAFETY: key is always unique, created by __ref above.
|
||||
let arg = unsafe { cx.new_ref_unchecked(key) };
|
||||
|
@ -5,13 +5,13 @@ describe("temp files", function()
|
||||
test("temp_dir cleans itself", function()
|
||||
local path
|
||||
do
|
||||
local dir = fs:temp_dir()
|
||||
local dir = fs.temp_dir()
|
||||
path = dir:path()
|
||||
assert(path and path ~= "")
|
||||
fs:write(path .. "/test.txt", "test file")
|
||||
assert(fs:read(path .. "/test.txt") == "test file")
|
||||
fs.write(path .. "/test.txt", "test file")
|
||||
assert(fs.read(path .. "/test.txt") == "test file")
|
||||
end
|
||||
collectgarbage()
|
||||
assert(not pcall(fs.read, fs, path .. "/test.txt"))
|
||||
assert(not pcall(fs.read, path .. "/test.txt"))
|
||||
end)
|
||||
end)
|
||||
|
@ -4,14 +4,14 @@ if not ok then return end
|
||||
describe("tcp", function()
|
||||
describe("socket", 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
|
||||
assert(tostring(socket:local_addr():ip()) == "127.0.0.1")
|
||||
assert(socket:local_addr():port() ~= 0)
|
||||
|
||||
-- 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)
|
||||
|
@ -74,7 +74,7 @@ describe("sleep", function()
|
||||
value = "value"
|
||||
end)
|
||||
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")
|
||||
end)
|
||||
end)
|
||||
|
@ -351,7 +351,7 @@ impl<'r> MetatypeBuilder<'r> {
|
||||
name: impl Display,
|
||||
f: impl FnOnce(&mut MetatypeFunctionBuilder),
|
||||
) -> &mut Self {
|
||||
write!(self.lua, "__idx.{name} = ").unwrap();
|
||||
write!(self.lua, "Self.{name} = ").unwrap();
|
||||
f(&mut MetatypeFunctionBuilder::new(self));
|
||||
writeln!(self.lua, ";").unwrap();
|
||||
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 {
|
||||
writeln!(self.lua, "__idx.{name} = {value};").unwrap();
|
||||
writeln!(self.lua, "Self.{name} = {value};").unwrap();
|
||||
self.has_index = true;
|
||||
self
|
||||
}
|
||||
@ -395,13 +395,13 @@ impl<'r> Drop for MetatypeBuilder<'r> {
|
||||
write!(reg.lua, r#"do local __mt = {{}}; "#).unwrap();
|
||||
|
||||
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.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 {
|
||||
writeln!(reg.lua, "do {}\nend;", lua.trim_end()).unwrap();
|
||||
|
@ -111,8 +111,8 @@ local function main(item)
|
||||
end
|
||||
|
||||
return main(create_group("", function()
|
||||
local function glob(path, pat)
|
||||
for entry in fs:glob_dir(path, pat) do
|
||||
local function include(path, pat)
|
||||
for entry in fs.glob_dir(path, pat) do
|
||||
local path = entry:path()
|
||||
local f, err = loadfile(path)
|
||||
if not f then error(err) end
|
||||
@ -120,6 +120,6 @@ return main(create_group("", function()
|
||||
end
|
||||
end
|
||||
|
||||
glob("tests", "**/*.lua")
|
||||
glob("crates", "*/tests/**/*.lua")
|
||||
include("tests", "**/*.lua")
|
||||
include("crates", "*/tests/**/*.lua")
|
||||
end))
|
||||
|
Loading…
x
Reference in New Issue
Block a user