Implement the foundations for annotation generation

This commit is contained in:
2025-06-28 04:15:27 +10:00
parent 6a4c726965
commit e05e2f4cb3
9 changed files with 510 additions and 268 deletions

View File

@@ -32,7 +32,7 @@ impl lb_chanlib {
(send, recv)
}
extern "Lua" fn bounded(self, cap: number) {
extern "Lua" fn bounded(self, cap: u32) {
assert(cap >= 0, "channel capacity must be nonnegative");
let (send, recv) = (__new(__ct.lb_sender), __new(__ct.lb_receiver));
self.__bounded(cap, send, recv);

View File

@@ -7,7 +7,7 @@
//!
//! See [`lb_netlib`] for items exported by this library.
use derive_more::{From, FromStr};
use luaffi::{cdef, metatype};
use luaffi::{cdef, marker::OneOf, metatype};
use std::{
net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
time::Duration,
@@ -82,7 +82,10 @@ impl lb_netlib {
/// If `s` is an [`lb_ipaddr`], a copy of that value is returned. If `s` is an
/// [`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: any) -> Result<lb_ipaddr> {
pub extern "Lua" fn ipaddr(
&self,
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
) -> Result<lb_ipaddr> {
if __istype(__ct.lb_ipaddr, addr) {
__new(__ct.lb_ipaddr, addr) // copy constructor
} else if __istype(__ct.lb_socketaddr, addr) {
@@ -105,7 +108,11 @@ impl lb_netlib {
/// socket address string. Both IPv4 and IPv6 addresses are supported.
///
/// If `port` is not specified, `0` is used as the default.
pub extern "Lua" fn socketaddr(&self, addr: any, port: any) -> Result<lb_socketaddr> {
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)
} else {
@@ -145,7 +152,11 @@ impl lb_netlib {
Ok(Some(TcpSocket::new_v6()?).into())
}
pub async extern "Lua" fn bind_tcp(&self, addr: any, port: any) -> Result<lb_tcpsocket> {
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 socket;
if addr.ip().is_v6() {
@@ -157,7 +168,11 @@ impl lb_netlib {
socket
}
pub async extern "Lua" fn connect_tcp(&self, addr: any, port: any) -> Result<lb_tcpstream> {
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 socket;
if addr.ip().is_v6() {
@@ -168,7 +183,11 @@ impl lb_netlib {
socket.connect(addr)
}
pub async extern "Lua" fn listen_tcp(&self, addr: any, port: any) -> Result<lb_tcplistener> {
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)
}
}
@@ -208,7 +227,7 @@ impl lb_ipaddr {
}
/// Returns the string `"v4"` if this is an IPv4 address or `"v6"` if this is an IPv6 address.
pub extern "Lua" fn family(&self) -> string {
pub extern "Lua" fn family(&self) -> String {
if self.is_v6() { "v6" } else { "v4" }
}
@@ -313,7 +332,10 @@ impl lb_socketaddr {
/// Sets the IP part of this address.
///
/// This function accepts the same arguments as [`ipaddr`](lb_netlib::ipaddr).
pub extern "Lua" fn set_ip(&mut self, addr: any) -> &mut Self {
pub extern "Lua" fn set_ip(
&mut self,
addr: OneOf<(&lb_ipaddr, &lb_socketaddr, &str)>,
) -> &mut Self {
if __istype(__ct.lb_ipaddr, addr) {
self.__set_ip(addr);
} else if __istype(__ct.lb_socketaddr, addr) {
@@ -338,7 +360,7 @@ impl lb_socketaddr {
}
/// Sets the port part of this address.
pub extern "Lua" fn set_port(&mut self, port: integer) -> &mut Self {
pub extern "Lua" fn set_port(&mut self, port: u16) -> &mut Self {
self.__set_port(port);
self
}

View File

@@ -7,7 +7,11 @@
//!
//! See [`lb_tasklib`] for items exported by this library.
use crate::runtime::spawn;
use luaffi::{cdef, metatype};
use luaffi::{
cdef,
marker::{function, many},
metatype,
};
use luajit::{LUA_MULTRET, Type};
use std::{ffi::c_int, time::Duration};
use tokio::{task::JoinHandle, time::sleep};