Compare commits

...

2 Commits

Author SHA1 Message Date
57f391a950
Set more aggressive jit options by default 2025-06-30 15:56:45 +10:00
99aa11e903
Rename Annotation trait to Annotate 2025-06-30 15:20:40 +10:00
8 changed files with 160 additions and 137 deletions

View File

@ -67,7 +67,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the file does not exist or cannot be read. /// This function may throw if the file does not exist or could not be read.
pub async extern "Lua-C" fn read(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?)
} }
@ -78,7 +78,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the file does not exist or cannot be read. /// This function may throw if the file does not exist or could not be read.
pub extern "Lua-C" fn read_sync(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)?)
} }
@ -87,7 +87,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the file cannot be written. /// This function may throw if the file could not be written.
pub async extern "Lua-C" fn write(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?)
} }
@ -98,7 +98,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the file cannot be written. /// This function may throw if the file could not be written.
pub extern "Lua-C" fn write_sync(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)?)
} }
@ -107,7 +107,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the directory cannot be read. /// This function may throw if the directory could not be read.
pub async extern "Lua-C" fn read_dir(path: &str) -> Result<lb_read_dir> { pub async extern "Lua-C" fn read_dir(path: &str) -> Result<lb_read_dir> {
Ok(lb_read_dir::new(tokio::fs::read_dir(path).await?)) Ok(lb_read_dir::new(tokio::fs::read_dir(path).await?))
} }
@ -118,7 +118,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the directory cannot be read. /// This function may throw if the directory could not be read.
pub extern "Lua-C" fn read_dir_sync(path: &str) -> Result<lb_read_dir_sync> { pub extern "Lua-C" fn read_dir_sync(path: &str) -> Result<lb_read_dir_sync> {
Ok(lb_read_dir_sync::new(std::fs::read_dir(path)?)) Ok(lb_read_dir_sync::new(std::fs::read_dir(path)?))
} }
@ -156,7 +156,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the temporary directory cannot be created. /// This function may throw if the temporary directory could not be created.
pub extern "Lua-C" fn temp_dir() -> Result<lb_temp_dir> { pub extern "Lua-C" fn temp_dir() -> Result<lb_temp_dir> {
Ok(lb_temp_dir::new(tempfile::tempdir()?)) Ok(lb_temp_dir::new(tempfile::tempdir()?))
} }
@ -165,7 +165,7 @@ impl lb_fslib {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the temporary directory cannot be created. /// This function may throw if the temporary directory could not be created.
pub extern "Lua-C" fn temp_dir_in(path: &str) -> Result<lb_temp_dir> { pub extern "Lua-C" fn temp_dir_in(path: &str) -> Result<lb_temp_dir> {
Ok(lb_temp_dir::new(tempfile::tempdir_in(path)?)) Ok(lb_temp_dir::new(tempfile::tempdir_in(path)?))
} }
@ -186,7 +186,7 @@ impl lb_read_dir {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the directory cannot be read. /// This function may throw if the directory could not be read.
#[call] #[call]
pub async extern "Lua-C" fn next(&self) -> Result<Option<lb_dir_entry>> { pub async extern "Lua-C" fn next(&self) -> Result<Option<lb_dir_entry>> {
Ok(self Ok(self
@ -213,7 +213,7 @@ impl lb_read_dir_sync {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the directory cannot be read. /// This function may throw if the directory could not be read.
#[call] #[call]
pub extern "Lua-C" fn next(&self) -> Result<Option<lb_dir_entry_sync>> { pub extern "Lua-C" fn next(&self) -> Result<Option<lb_dir_entry_sync>> {
Ok(self Ok(self
@ -250,7 +250,7 @@ impl lb_dir_entry {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the file type cannot be determined. /// This function may throw if the file type could not be determined.
pub async extern "Lua-C" fn r#type(&self) -> Result<lb_file_type> { pub async extern "Lua-C" fn r#type(&self) -> Result<lb_file_type> {
Ok(lb_file_type::new(self.0.file_type().await?)) Ok(lb_file_type::new(self.0.file_type().await?))
} }
@ -259,7 +259,7 @@ impl lb_dir_entry {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the metadata cannot be retrieved. /// This function may throw if the metadata could not be retrieved.
pub async extern "Lua-C" fn metadata(&self) -> Result<lb_file_meta> { pub async extern "Lua-C" fn metadata(&self) -> Result<lb_file_meta> {
Ok(lb_file_meta::new(self.0.metadata().await?)) Ok(lb_file_meta::new(self.0.metadata().await?))
} }
@ -302,7 +302,7 @@ impl lb_dir_entry_sync {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the file type cannot be determined. /// This function may throw if the file type could not be determined.
pub extern "Lua-C" fn r#type(&self) -> Result<lb_file_type> { pub extern "Lua-C" fn r#type(&self) -> Result<lb_file_type> {
Ok(lb_file_type::new(self.0.file_type()?)) Ok(lb_file_type::new(self.0.file_type()?))
} }
@ -311,7 +311,7 @@ impl lb_dir_entry_sync {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the metadata cannot be retrieved. /// This function may throw if the metadata could not be retrieved.
pub extern "Lua-C" fn metadata(&self) -> Result<lb_file_meta> { pub extern "Lua-C" fn metadata(&self) -> Result<lb_file_meta> {
Ok(lb_file_meta::new(self.0.metadata()?)) Ok(lb_file_meta::new(self.0.metadata()?))
} }
@ -418,7 +418,7 @@ impl lb_file_meta {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the creation time cannot be retrieved. /// This function may throw if the creation time could not be retrieved.
pub extern "Lua-C" fn created(&self) -> Result<f64> { pub extern "Lua-C" fn created(&self) -> Result<f64> {
Ok(self Ok(self
.0 .0
@ -432,7 +432,7 @@ impl lb_file_meta {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the modification time cannot be retrieved. /// This function may throw if the modification time could not be retrieved.
pub extern "Lua-C" fn modified(&self) -> Result<f64> { pub extern "Lua-C" fn modified(&self) -> Result<f64> {
Ok(self Ok(self
.0 .0
@ -446,7 +446,7 @@ impl lb_file_meta {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the access time cannot be retrieved. /// This function may throw if the access time could not be retrieved.
pub extern "Lua-C" fn accessed(&self) -> Result<f64> { pub extern "Lua-C" fn accessed(&self) -> Result<f64> {
Ok(self Ok(self
.0 .0
@ -509,7 +509,7 @@ impl lb_walk_dir {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the directory cannot be read. /// This function may throw if the directory could not be read.
#[call] #[call]
pub extern "Lua-C" fn next(&self) -> Result<Option<lb_walk_dir_entry>> { pub extern "Lua-C" fn next(&self) -> Result<Option<lb_walk_dir_entry>> {
Ok(self Ok(self
@ -551,7 +551,7 @@ impl lb_walk_dir_entry {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the metadata cannot be retrieved. /// This function may throw if the metadata could not be retrieved.
pub extern "Lua-C" fn metadata(&self) -> Result<lb_file_meta> { pub extern "Lua-C" fn metadata(&self) -> Result<lb_file_meta> {
Ok(lb_file_meta::new(self.0.metadata()?)) Ok(lb_file_meta::new(self.0.metadata()?))
} }
@ -606,7 +606,7 @@ impl lb_glob_dir {
/// ///
/// # Errors /// # Errors
/// ///
/// This function may throw if the directory cannot be read. /// This function may throw if the directory could not be read.
#[call] #[call]
pub extern "Lua-C" fn next(&self) -> Result<Option<lb_walk_dir_entry>> { pub extern "Lua-C" fn next(&self) -> Result<Option<lb_walk_dir_entry>> {
while let Some(res) = self.iter.try_borrow_mut()?.next() { while let Some(res) = self.iter.try_borrow_mut()?.next() {

View File

@ -510,12 +510,12 @@ pub struct lb_socketaddr(#[opaque] SocketAddr);
#[metatype] #[metatype]
impl lb_socketaddr { impl lb_socketaddr {
/// Returns the IP part of this address. /// The IP part of this address.
pub extern "Lua-C" fn ip(&self) -> lb_ipaddr { pub extern "Lua-C" fn ip(&self) -> lb_ipaddr {
self.0.ip().into() self.0.ip().into()
} }
/// Returns the port part of this address. /// The port part of this address.
pub extern "Lua-C" fn port(&self) -> u16 { pub extern "Lua-C" fn port(&self) -> u16 {
self.0.port() self.0.port()
} }
@ -775,16 +775,16 @@ impl lb_tcpstream {
Ok(self.write.try_borrow_mut()?) Ok(self.write.try_borrow_mut()?)
} }
/// Gets the remote socket address of this stream. /// The local socket address that this stream is bound to.
pub extern "Lua-C" fn peer_addr(&self) -> Result<lb_socketaddr> {
Ok(self.read_half()?.peer_addr()?.into())
}
/// Gets the local socket address of this stream.
pub extern "Lua-C" fn local_addr(&self) -> Result<lb_socketaddr> { pub extern "Lua-C" fn local_addr(&self) -> Result<lb_socketaddr> {
Ok(self.read_half()?.local_addr()?.into()) Ok(self.read_half()?.local_addr()?.into())
} }
/// The remote socket address that this stream is connected to.
pub extern "Lua-C" fn peer_addr(&self) -> Result<lb_socketaddr> {
Ok(self.read_half()?.peer_addr()?.into())
}
/// Waits for this stream to be ready in the given half. /// Waits for this stream to be ready in the given half.
/// ///
/// The argument `half` can be `"read"` for the readable half, `"write"` for the writable half, /// The argument `half` can be `"read"` for the readable half, `"write"` for the writable half,
@ -936,7 +936,7 @@ impl lb_tcplistener {
} }
} }
/// Returns the local socket address that this listener is bound to. /// The local socket address that this listener is bound to.
pub extern "Lua-C" fn local_addr(&self) -> Result<lb_socketaddr> { pub extern "Lua-C" fn local_addr(&self) -> Result<lb_socketaddr> {
Ok(self.listener.local_addr()?.into()) Ok(self.listener.local_addr()?.into())
} }
@ -1019,12 +1019,12 @@ impl lb_tcplistener_stream {
} }
} }
/// Returns the local socket address that the listener is bound to. /// The local socket address that the listener is bound to.
pub extern "Lua-C" fn local_addr(&self) -> Result<lb_socketaddr> { pub extern "Lua-C" fn local_addr(&self) -> Result<lb_socketaddr> {
Ok(self.stream()?.local_addr()?.into()) Ok(self.stream()?.local_addr()?.into())
} }
/// Returns the remote socket address of this stream. /// The remote socket address that this stream is connected to.
pub extern "Lua-C" fn peer_addr(&self) -> Result<lb_socketaddr> { pub extern "Lua-C" fn peer_addr(&self) -> Result<lb_socketaddr> {
Ok(self.stream()?.peer_addr()?.into()) Ok(self.stream()?.peer_addr()?.into())
} }

View File

@ -14,6 +14,7 @@ pub type ErrorFn = dyn Fn(&luajit::Error);
pub struct Builder { pub struct Builder {
registry: Registry, registry: Registry,
report_err: Rc<ErrorFn>, report_err: Rc<ErrorFn>,
jit_opts: Vec<String>,
prohibit_globals: bool, prohibit_globals: bool,
} }
@ -25,6 +26,14 @@ impl Builder {
Some(trace) => eprintln!("unhandled lua error: {err}\n{trace}"), Some(trace) => eprintln!("unhandled lua error: {err}\n{trace}"),
None => eprintln!("unhandled lua error: {err}"), None => eprintln!("unhandled lua error: {err}"),
}), }),
jit_opts: vec![
// more aggressive jit options based on OpenResty's defaults
// https://github.com/openresty/luajit2#updated-jit-default-parameters
"maxtrace=8000".into(),
"maxrecord=16000".into(),
"minstitch=3".into(),
"maxmcode=40960".into(),
],
prohibit_globals: false, prohibit_globals: false,
} }
} }
@ -43,6 +52,18 @@ impl Builder {
self self
} }
pub fn jit_opt(&mut self, opt: impl AsRef<str>) -> &mut Self {
self.jit_opts.push(opt.as_ref().into());
self
}
pub fn jit_opts(&mut self, opts: impl IntoIterator<Item: AsRef<str>>) -> &mut Self {
for opt in opts {
self.jit_opt(opt);
}
self
}
pub fn module<T: Module>(&mut self) -> &mut Self { pub fn module<T: Module>(&mut self) -> &mut Self {
self.registry.preload::<T>(); self.registry.preload::<T>();
self self
@ -54,21 +75,11 @@ impl Builder {
state.eval(&chunk, 0, Some(0))?; state.eval(&chunk, 0, Some(0))?;
if self.prohibit_globals { if self.prohibit_globals {
let mut s = state.guard(); state_prohibit_globals(&mut state)?;
s.eval( }
&Chunk::new(luaify_chunk!({
return |self, key, value| { for opt in self.jit_opts.iter() {
error(("undeclared local variable '%s'").format(key), 2); state_set_jitopt(&mut state, opt)?;
};
})),
0,
Some(1),
)
.unwrap();
s.push(NewTable::new());
(s.push("__index"), s.push_idx(-3), s.set(-3));
(s.push("__newindex"), s.push_idx(-3), s.set(-3));
s.set_metatable(Index::globals());
} }
Ok(Runtime { Ok(Runtime {
@ -81,6 +92,60 @@ impl Builder {
} }
} }
fn state_prohibit_globals(state: &mut State) -> luajit::Result<()> {
let mut s = state.guard();
let chunk = Chunk::new(luaify_chunk!({
return |self, key, value| {
error(("undeclared local variable '%s'").format(key), 2);
};
}))
.with_path("[luby]");
s.eval(&chunk, 0, Some(1)).unwrap();
s.push(NewTable::new());
(s.push("__index"), s.push_idx(-3), s.set(-3));
(s.push("__newindex"), s.push_idx(-3), s.set(-3));
s.set_metatable(Index::globals());
Ok(())
}
fn state_set_jitopt(state: &mut State, opt: &str) -> luajit::Result<()> {
let mut s = state.guard();
if let Some((cmd, opt)) = parse_jitlib_cmd(opt)
&& let Ok(_) = s.require(format!("jit.{cmd}"), Some(1))
{
// require("jit.{cmd}").start(opt)
(s.push("start"), s.get(-2));
s.push(opt);
s.call(1, Some(0))?;
} else {
s.require("jit", Some(1)).unwrap();
match opt {
cmd @ ("on" | "off" | "flush") => {
// require("jit").<on|off|flush>()
(s.push(cmd), s.get(-2));
s.call(0, Some(0))?;
}
_ => {
// require("jit").opt.start(opt)
(s.push("opt"), s.get(-2));
(s.push("start"), s.get(-2));
s.push(opt);
s.call(1, Some(0))?;
}
}
}
Ok(())
}
fn parse_jitlib_cmd(cmd: &str) -> Option<(&str, &str)> {
match cmd {
"p" => Some(("p", "Flspv10")), // default -jp flags
"v" => Some(("v", "-")), // default -jv flags
"dump" => Some(("dump", "tirs")), // default -jdump flags
_ => cmd.split_once('='),
}
}
#[derive(Deref, DerefMut)] #[derive(Deref, DerefMut)]
pub struct Runtime { pub struct Runtime {
#[deref] #[deref]

View File

@ -645,7 +645,7 @@ impl<'r, 'm> MetatypeFunctionBuilder<'r, 'm> {
} }
} }
pub trait Annotation { pub trait Annotate {
fn annotation() -> impl Display; fn annotation() -> impl Display;
} }
@ -699,7 +699,7 @@ macro_rules! impl_primitive {
fn build(_b: &mut TypeBuilder) {} fn build(_b: &mut TypeBuilder) {}
} }
impl Annotation for $rty { impl Annotate for $rty {
fn annotation() -> impl Display { fn annotation() -> impl Display {
$lty $lty
} }
@ -867,9 +867,9 @@ impl_ptr!(Option<&mut T>, true);
macro_rules! impl_ref_annotation { macro_rules! impl_ref_annotation {
($ty:ty) => { ($ty:ty) => {
impl<T> Annotation for $ty impl<T> Annotate for $ty
where where
T: Annotation, T: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
display!("{}", T::annotation()) display!("{}", T::annotation())
@ -1005,9 +1005,9 @@ where
} }
} }
impl<T> Annotation for [T] impl<T> Annotate for [T]
where where
T: Annotation, T: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
display!("{}[]", T::annotation()) display!("{}[]", T::annotation())
@ -1035,9 +1035,9 @@ where
} }
} }
impl<T, const N: usize> Annotation for [T; N] impl<T, const N: usize> Annotate for [T; N]
where where
T: Annotation, T: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
display!("{}[]", T::annotation()) display!("{}[]", T::annotation())
@ -1106,25 +1106,25 @@ impl_externcfn!(fn(A, B, C, D, E, F, G) -> H);
impl_externcfn!(fn(A, B, C, D, E, F, G, H) -> I); impl_externcfn!(fn(A, B, C, D, E, F, G, H) -> I);
impl_externcfn!(fn(A, B, C, D, E, F, G, H, I) -> J); impl_externcfn!(fn(A, B, C, D, E, F, G, H, I) -> J);
impl<'s> Annotation for &'s [u8] { impl<'s> Annotate for &'s [u8] {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"string" "string"
} }
} }
impl<'s> Annotation for &'s str { impl<'s> Annotate for &'s str {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"string" "string"
} }
} }
impl Annotation for Vec<u8> { impl Annotate for Vec<u8> {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"string" "string"
} }
} }
impl Annotation for String { impl Annotate for String {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"string" "string"
} }

View File

@ -1,7 +1,7 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::{ use crate::{
__internal::{disp, display}, __internal::{disp, display},
Annotation, Annotate,
}; };
use std::{fmt::Display, marker::PhantomData}; use std::{fmt::Display, marker::PhantomData};
@ -9,7 +9,7 @@ enum Marker {}
pub struct any(Marker); pub struct any(Marker);
impl Annotation for any { impl Annotate for any {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"any" "any"
} }
@ -17,7 +17,7 @@ impl Annotation for any {
pub struct many(Marker); pub struct many(Marker);
impl Annotation for many { impl Annotate for many {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"..." "..."
} }
@ -25,7 +25,7 @@ impl Annotation for many {
pub struct nil(Marker); pub struct nil(Marker);
impl Annotation for nil { impl Annotate for nil {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"nil" "nil"
} }
@ -33,7 +33,7 @@ impl Annotation for nil {
pub struct lightuserdata(Marker); pub struct lightuserdata(Marker);
impl Annotation for lightuserdata { impl Annotate for lightuserdata {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"lightuserdata" "lightuserdata"
} }
@ -41,10 +41,10 @@ impl Annotation for lightuserdata {
pub struct table<K, V>(Marker, PhantomData<*mut [(K, V)]>); pub struct table<K, V>(Marker, PhantomData<*mut [(K, V)]>);
impl<K, V> Annotation for table<K, V> impl<K, V> Annotate for table<K, V>
where where
K: Annotation, K: Annotate,
V: Annotation, V: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
display!("table<{}, {}>", K::annotation(), V::annotation()) display!("table<{}, {}>", K::annotation(), V::annotation())
@ -53,7 +53,7 @@ where
pub struct function(Marker); pub struct function(Marker);
impl Annotation for function { impl Annotate for function {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"function" "function"
} }
@ -63,9 +63,9 @@ pub struct fun<I, O>(Marker, PhantomData<fn(I) -> O>);
macro_rules! impl_fun { macro_rules! impl_fun {
(fn($($arg:ident),*)) => { (fn($($arg:ident),*)) => {
impl<$($arg,)*> Annotation for fun<($($arg,)*), ()> impl<$($arg,)*> Annotate for fun<($($arg,)*), ()>
where where
$($arg: Annotation,)* $($arg: Annotate,)*
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
disp(|f| { disp(|f| {
@ -79,10 +79,10 @@ macro_rules! impl_fun {
}; };
(fn($($arg:ident),*) -> $ret:ident) => { (fn($($arg:ident),*) -> $ret:ident) => {
impl<$($arg,)* $ret> Annotation for fun<($($arg,)*), $ret> impl<$($arg,)* $ret> Annotate for fun<($($arg,)*), $ret>
where where
$($arg: Annotation,)* $($arg: Annotate,)*
$ret: Annotation, $ret: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
disp(|f| { disp(|f| {
@ -111,7 +111,7 @@ impl_fun!(fn(A, B, C, D, E, F, G, H, I) -> J);
pub struct userdata(Marker); pub struct userdata(Marker);
impl Annotation for userdata { impl Annotate for userdata {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"userdata" "userdata"
} }
@ -119,7 +119,7 @@ impl Annotation for userdata {
pub struct thread(Marker); pub struct thread(Marker);
impl Annotation for thread { impl Annotate for thread {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"thread" "thread"
} }
@ -127,7 +127,7 @@ impl Annotation for thread {
pub struct cdata(Marker); pub struct cdata(Marker);
impl Annotation for cdata { impl Annotate for cdata {
fn annotation() -> impl Display { fn annotation() -> impl Display {
"cdata" "cdata"
} }
@ -135,10 +135,10 @@ impl Annotation for cdata {
pub struct Either<T, U>(Marker, PhantomData<(T, U)>); pub struct Either<T, U>(Marker, PhantomData<(T, U)>);
impl<T, U> Annotation for Either<T, U> impl<T, U> Annotate for Either<T, U>
where where
T: Annotation, T: Annotate,
U: Annotation, U: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
display!("({} | {})", T::annotation(), U::annotation()) display!("({} | {})", T::annotation(), U::annotation())
@ -149,9 +149,9 @@ pub struct OneOf<X>(Marker, PhantomData<X>);
macro_rules! impl_oneof { macro_rules! impl_oneof {
($($ty:ident),+) => { ($($ty:ident),+) => {
impl<$($ty),+> Annotation for OneOf<($($ty,)+)> impl<$($ty),+> Annotate for OneOf<($($ty,)+)>
where where
$($ty: Annotation),+ $($ty: Annotate),+
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
disp(|f| { disp(|f| {
@ -174,18 +174,18 @@ impl_oneof!(A, B, C, D, E, F, G);
impl_oneof!(A, B, C, D, E, F, G, H); impl_oneof!(A, B, C, D, E, F, G, H);
impl_oneof!(A, B, C, D, E, F, G, H, I); impl_oneof!(A, B, C, D, E, F, G, H, I);
impl<T> Annotation for Option<T> impl<T> Annotate for Option<T>
where where
T: Annotation, T: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
display!("{}?", T::annotation()) display!("{}?", T::annotation())
} }
} }
impl<T, E> Annotation for Result<T, E> impl<T, E> Annotate for Result<T, E>
where where
T: Annotation, T: Annotate,
{ {
fn annotation() -> impl Display { fn annotation() -> impl Display {
display!("{}", T::annotation()) display!("{}", T::annotation())

View File

@ -70,7 +70,7 @@ fn generate_type(ty: &Ident) -> Result<TokenStream> {
} }
} }
impl #ffi::Annotation for #ty { impl #ffi::Annotate for #ty {
fn annotation() -> impl ::std::fmt::Display { #name } fn annotation() -> impl ::std::fmt::Display { #name }
} }

View File

@ -682,7 +682,7 @@ fn stub_lua_function(func: &mut ImplItemFn) -> Result<()> {
func.attrs.push(parse_quote!(#[allow(unused)])); func.attrs.push(parse_quote!(#[allow(unused)]));
func.block.stmts.clear(); func.block.stmts.clear();
func.block.stmts.push(parse_quote!( func.block.stmts.push(parse_quote!(
const fn has_annotation<T: #ffi::Annotation>() {} const fn has_annotation<T: #ffi::Annotate>() {}
)); ));
// convert `...` variadic to a `rest: luaffi::marker::Many` parameter // convert `...` variadic to a `rest: luaffi::marker::Many` parameter

View File

@ -231,60 +231,18 @@ fn init_tokio(args: &Args) -> tokio::runtime::Runtime {
} }
fn init_lua(args: &Args) -> lb::runtime::Runtime { fn init_lua(args: &Args) -> lb::runtime::Runtime {
let mut rt = { let mut rt = lb::runtime::Builder::new();
let mut rt = lb::runtime::Builder::new(); luby::open(&mut rt);
luby::open(&mut rt);
if args.dump.iter().find(|s| *s == "cdef").is_some() { if args.dump.iter().find(|s| *s == "cdef").is_some() {
print!("{}", rt.registry()); // for cdef debugging print!("{}", rt.registry()); // for cdef debugging
}
rt.unhandled_error(error_cb)
.prohibit_globals(!args.allow_globals)
.build()
.unwrap()
};
for arg in args.jit.iter() {
let mut s = rt.guard();
let res = if let Some((cmd, flags)) = parse_jitlib_cmd(arg)
&& let Ok(_) = s.require(format!("jit.{cmd}"), Some(1))
{
(s.push("start"), s.get(-2));
s.push(flags);
s.call(1, Some(0)) // require("jit.{cmd}").start(flags)
} else {
s.require("jit", Some(1)).unwrap();
match arg.as_str() {
cmd @ ("on" | "off" | "flush") => {
(s.push(cmd), s.get(-2));
s.call(0, Some(0)) // require("jit").[on/off/flush]()
}
flags => {
(s.push("opt"), s.get(-2));
(s.push("start"), s.get(-2));
s.push(flags);
s.call(1, Some(0)) // require("jit").opt.start(flags)
}
}
};
if let Err(err) = res {
drop(s);
rt.report_error(&err);
}
} }
rt rt.unhandled_error(error_cb)
} .prohibit_globals(!args.allow_globals)
.jit_opts(args.jit.iter())
fn parse_jitlib_cmd(s: &str) -> Option<(&str, &str)> { .build()
match s { .unwrap()
"p" => Some(("p", "Flspv10")), // default -jp flags
"v" => Some(("v", "-")), // default -jv flags
"dump" => Some(("dump", "tirs")), // default -jdump flags
_ => s.split_once('='),
}
} }
async fn main_async(args: Args, cx: &mut lb::runtime::Context) -> ExitCode { async fn main_async(args: Args, cx: &mut lb::runtime::Context) -> ExitCode {