Skip to content

Conversation

@reloginn
Copy link

@reloginn reloginn commented May 2, 2025

All functions are based on and fully comply with the Lua 5.3 manual (https://www.lua.org/manual/5.3/).

Features

  • Implemented functions string.char, string.byte, string.find, string.match, string.gmatch, string.gsub, string.rep, string.pack, string.unpack, string.packsize.
  • Added tests in string.lua for all new functions.
  • In Cargo.toml, the lsonar crate has been added - an implementation of Lua patterns in Pure Rust, without unsafe and dependencies (https://github.com/reloginn/lsonar).
  • In Cargo.toml, the lformat crate has been added - an implementation of string.format based on the sprintf crate, without unsafe and with (almost) no dependencies (https://github.com/reloginn/lformat).

Caveats

  • In the string.gsub function, the Value::Function(_) case is not implemented (I don't know how to call VM functions from Rust).

@Aeledfyr
Copy link
Collaborator

I've had a chance to read through this now, and it looks reasonable, though it'll need some cleanup and should probably be split into multiple smaller PRs. (The simplest split would be the base string ops; format; pattern matching; pack/unpack.)

  • string.format needs to determine the types of its arguments from the format string, so that it can handle tables with __tostring metamethods (while also supporting %p). I think this should be possible with your approach of forking sprintf, but you'd have to implement string.format over the &[FormatElement] slice directly, rather than using lformat::format.

  • The pattern matching functions need to be able to suspend in the middle of long operations;

    For example, evaluating this match (basically ("aaa", "a?a?a?aaa")) takes exponential time, and can hang the host program if the matching doesn't have suspend points.

    n = 30
    print(string.match(string.rep("a", n), string.rep("a?", n) .. string.rep("a", n)))

    Adding suspend points requires using Sequences, though the new-ish async-callback API makes that easier do by using Rust's async blocks / async functions.

  • For string.gsub, here's an example of how to call a Lua function from Rust, using async_sequence:

    piccolo/src/stdlib/table.rs

    Lines 374 to 396 in acb88fe

    let s = async_sequence(&ctx, |locals, mut seq| {
    let table = locals.stash(&ctx, table);
    let value = locals.stash(&ctx, value);
    async move {
    let length = if let Some(len) = length {
    len as i64
    } else {
    let call = seq.try_enter(|ctx, locals, _, stack| {
    let table = locals.fetch(&table);
    let call = meta_ops::len(ctx, Value::Table(table))
    .context("error while calling __len")?;
    Ok(prep_metaop_call(ctx, stack, locals, call))
    })?;
    if let Some(call) = call {
    seq.call(&call, 0).await?;
    }
    let len = seq.try_enter(|ctx, _, _, mut stack| {
    Ok(stack
    .consume::<i64>(ctx)
    .context("__len returned invalid length")?)
    })?;
    len
    };

    In this case it's calling the __len metamethod, but the core logic is the same. The difficult part is that the sequence needs to suspend every time it calls a Lua function, so we have to make the Rust code into an async sequence. (Or a normal Sequence, but that takes more work to write.)

  • string.pack and string.unpack look reasonable, but they should be moved to a submodule (ie. stdlib/string/pack.rs), and I'd personally prefer if they were split into a separate PR, given the complexity.

@reloginn reloginn marked this pull request as draft June 3, 2025 17:13
@reloginn
Copy link
Author

reloginn commented Aug 10, 2025

I started implementing all your fixes and began splitting this PR into smaller ones:
#126: string.pack, string.unpack, string.packsize
#127: string.rep
#128: string.format
#129: pattern matching

I will close this PR.

@reloginn reloginn marked this pull request as ready for review August 10, 2025 18:45
@reloginn reloginn closed this Aug 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants