You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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.
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:
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))
})?;
ifletSome(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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All functions are based on and fully comply with the Lua 5.3 manual (https://www.lua.org/manual/5.3/).
Features
string.char,string.byte,string.find,string.match,string.gmatch,string.gsub,string.rep,string.pack,string.unpack,string.packsize.string.luafor all new functions.Cargo.toml, thelsonarcrate has been added - an implementation of Lua patterns in Pure Rust, without unsafe and dependencies (https://github.com/reloginn/lsonar).Cargo.toml, thelformatcrate has been added - an implementation ofstring.formatbased on thesprintfcrate, without unsafe and with (almost) no dependencies (https://github.com/reloginn/lformat).Caveats
string.gsubfunction, theValue::Function(_)case is not implemented (I don't know how to call VM functions from Rust).