diff --git a/crates/lb/src/fs.rs b/crates/lb/src/fs.rs index d1bdfc2..21eb60b 100644 --- a/crates/lb/src/fs.rs +++ b/crates/lb/src/fs.rs @@ -38,7 +38,8 @@ type Result = std::result::Result; /// 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> { + pub async extern "Lua-C" fn read(path: &str) -> Result> { Ok(tokio::fs::read(path).await?) } - pub extern "Lua-C" fn read_sync(&self, path: &str) -> Result> { + pub extern "Lua-C" fn read_sync(path: &str) -> Result> { 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 { + pub async extern "Lua-C" fn read_dir(path: &str) -> Result { Ok(tokio::fs::read_dir(path).await?.into()) } - pub extern "Lua-C" fn read_dir_sync(&self, path: &str) -> Result { + pub extern "Lua-C" fn read_dir_sync(path: &str) -> Result { 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 { - self.glob_dir(".", pattern) + pub extern "Lua-C" fn glob(pattern: &str) -> Result { + Self::glob_dir(".", pattern) } - pub extern "Lua-C" fn glob_dir(&self, path: &str, pattern: &str) -> Result { + pub extern "Lua-C" fn glob_dir(path: &str, pattern: &str) -> Result { 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 { + pub extern "Lua-C" fn temp_dir() -> Result { Ok(tempfile::tempdir()?.into()) } - pub extern "Lua-C" fn temp_dir_in(&self, path: &str) -> Result { + pub extern "Lua-C" fn temp_dir_in(path: &str) -> Result { Ok(tempfile::tempdir_in(path)?.into()) } } diff --git a/crates/lb/src/net.rs b/crates/lb/src/net.rs index 6c0226d..2899945 100644 --- a/crates/lb/src/net.rs +++ b/crates/lb/src/net.rs @@ -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 { 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 { + extern "Lua-C" fn __parse_ipaddr(addr: &str) -> Result { 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, ) -> Result { 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 { + extern "Lua-C" fn __parse_skaddr(addr: &str) -> Result { 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 { + pub extern "Lua-C" fn tcp() -> Result { 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 { + pub extern "Lua-C" fn tcp6() -> Result { Ok(Some(TcpSocket::new_v6()?).into()) } pub async extern "Lua" fn bind_tcp( - &self, addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>, port: Option, ) -> Result { - 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, ) -> Result { - 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, ) -> Result { - 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()); diff --git a/crates/lb/src/task.lua b/crates/lb/src/task.lua index bca5aef..eee4c51 100644 --- a/crates/lb/src/task.lua +++ b/crates/lb/src/task.lua @@ -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 diff --git a/crates/lb/src/task.rs b/crates/lb/src/task.rs index ad65a4e..3e5cc85 100644 --- a/crates/lb/src/task.rs +++ b/crates/lb/src/task.rs @@ -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) }; diff --git a/crates/lb/tests/fs.lua b/crates/lb/tests/fs.lua index 808561b..ac596a1 100644 --- a/crates/lb/tests/fs.lua +++ b/crates/lb/tests/fs.lua @@ -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) diff --git a/crates/lb/tests/net.lua b/crates/lb/tests/net.lua index 1bc2fc5..a800206 100644 --- a/crates/lb/tests/net.lua +++ b/crates/lb/tests/net.lua @@ -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) diff --git a/crates/lb/tests/task.lua b/crates/lb/tests/task.lua index 5cd0843..c707333 100644 --- a/crates/lb/tests/task.lua +++ b/crates/lb/tests/task.lua @@ -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) diff --git a/crates/luaffi/src/lib.rs b/crates/luaffi/src/lib.rs index 5e1fc92..3855db4 100644 --- a/crates/luaffi/src/lib.rs +++ b/crates/luaffi/src/lib.rs @@ -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(); diff --git a/tests/main.lua b/tests/main.lua index 481f504..b7d16f0 100644 --- a/tests/main.lua +++ b/tests/main.lua @@ -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))