Allow listing aliases with #alias with no args.

This commit is contained in:
Condorra 2024-09-27 22:45:12 +10:00
parent a215133ea6
commit ecdbb3cfa7
3 changed files with 54 additions and 7 deletions

View File

@ -246,7 +246,7 @@ pub fn install_lua_globals(
register_class_function!(mud_class_table, mudoutput_dont); register_class_function!(mud_class_table, mudoutput_dont);
register_class_function!(mud_class_table, mudoutput_subnegotiation); register_class_function!(mud_class_table, mudoutput_subnegotiation);
register_class_function!(mud_class_table, mudinput_line); register_class_function!(mud_class_table, mudinput_line);
register_class_function!(mud_class_table, "new", new_mud); register_stateless_class_function!(mud_class_table, "new", new_mud);
macro_rules! register_class_nop { macro_rules! register_class_nop {
($class_table: ident, $sym: ident) => { ($class_table: ident, $sym: ident) => {

View File

@ -5,8 +5,9 @@ use crate::{
GlobalLayoutCell, GlobalLayoutState, GlobalMemoCell, TermFrame, GlobalLayoutCell, GlobalLayoutState, GlobalMemoCell, TermFrame,
}; };
use gc_arena::{Gc, Rootable}; use gc_arena::{Gc, Rootable};
use itertools::Itertools;
use piccolo::{ use piccolo::{
self, async_sequence, Callback, CallbackReturn, Context, FromValue, Function, IntoValue, self, async_sequence, Callback, CallbackReturn, Context, Error, FromValue, Function, IntoValue,
SequenceReturn, StashedTable, StashedUserData, StashedValue, Table, UserData, Value, Variadic, SequenceReturn, StashedTable, StashedUserData, StashedValue, Table, UserData, Value, Variadic,
}; };
use std::{rc::Rc, str}; use std::{rc::Rc, str};
@ -17,10 +18,15 @@ use super::call_checking_metatable;
pub fn alias<'gc, 'a>(ctx: Context<'gc>) -> Callback<'gc> { pub fn alias<'gc, 'a>(ctx: Context<'gc>) -> Callback<'gc> {
Callback::from_fn(&ctx, move |ctx, _ex, mut stack| { Callback::from_fn(&ctx, move |ctx, _ex, mut stack| {
let info: Table = ctx.get_global("info")?; let info: Table = ctx.get_global("info")?;
let cur_frame: TermFrame = let cur_frame_id: TermFrame =
try_unwrap_frame(ctx, &info.get(ctx, ctx.intern_static(b"current_frame"))?)?; try_unwrap_frame(ctx, &info.get(ctx, ctx.intern_static(b"current_frame"))?)?;
let frames: Table = ctx.get_global("frames")?; let frames: Table = ctx.get_global("frames")?;
let cur_frame: Table = frames.get(ctx, cur_frame.0 as i64)?; let cur_frame: Table = frames.get(ctx, cur_frame_id.0 as i64)?;
let aliases: UserData = cur_frame.get(ctx, "aliases")?;
if stack.is_empty() {
return list_aliases(cur_frame_id, aliases, ctx);
}
let alias_match: piccolo::String = piccolo::String::from_value( let alias_match: piccolo::String = piccolo::String::from_value(
ctx, ctx,
@ -40,8 +46,6 @@ pub fn alias<'gc, 'a>(ctx: Context<'gc>) -> Callback<'gc> {
))?; ))?;
} }
let aliases: UserData = cur_frame.get(ctx, "aliases")?;
stack.push_back(aliases.into_value(ctx)); stack.push_back(aliases.into_value(ctx));
stack.push_back(alias_match.into_value(ctx)); stack.push_back(alias_match.into_value(ctx));
stack.push_back(sub_to.into_value(ctx)); stack.push_back(sub_to.into_value(ctx));
@ -57,6 +61,49 @@ pub fn alias<'gc, 'a>(ctx: Context<'gc>) -> Callback<'gc> {
}) })
} }
fn list_aliases<'gc>(
frame: TermFrame,
aliases: UserData<'gc>,
ctx: Context<'gc>,
) -> Result<CallbackReturn<'gc>, Error<'gc>> {
let seq = async_sequence(&ctx, |locals, mut seq| {
let frame = locals.stash(&ctx, intern_id(ctx, frame));
let aliases = locals.stash(&ctx, aliases);
async move {
call_checking_metatable::<StashedUserData>(&mut seq, aliases, "lua_table", &[]).await?;
let echo_func = seq.try_enter(|ctx, locals, _ex, mut stack| {
let alias_tab: Table = stack.consume(ctx)?;
stack.push_back(locals.fetch(&frame).into_value(ctx));
stack.push_back(
alias_tab
.iter()
.map(|(k, v)| {
Ok::<String, Error>(format!(
"{} => {}",
&String::from_value(ctx, k)?,
&String::from_value(ctx, v)?
))
})
.collect::<Result<Vec<String>, Error>>()?
.iter()
.join("\r\n")
.into_value(ctx),
);
Ok(locals.stash(
&ctx,
ctx.get_global::<Table>("commands")?
.get::<&str, Function>(ctx, "echo_frame")?,
))
})?;
seq.call(&echo_func, 0).await?;
Ok(SequenceReturn::Return)
}
});
Ok(CallbackReturn::Sequence(seq))
}
pub fn echo_frame_raw<'gc, 'a>( pub fn echo_frame_raw<'gc, 'a>(
ctx: Context<'gc>, ctx: Context<'gc>,
global_memo: &'a GlobalMemoCell, global_memo: &'a GlobalMemoCell,

View File

@ -471,7 +471,7 @@ pub(super) fn mudinput_line<'gc>(
}) })
} }
pub(super) fn new_mud<'gc>(ctx: Context<'gc>, _global_memo: &GlobalMemoCell) -> Callback<'gc> { pub(super) fn new_mud<'gc>(ctx: Context<'gc>) -> Callback<'gc> {
Callback::from_fn(&ctx, move |ctx, _ex, mut stack| { Callback::from_fn(&ctx, move |ctx, _ex, mut stack| {
let mud: Table = Table::from_value( let mud: Table = Table::from_value(
ctx, ctx,