Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match pairs behavior to PUC-Rio #76

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: initial patch for pairs to match PUC-Rio
Jengamon committed Jun 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit c81c4a72dfdedeae28684569362e89cacea6698a
20 changes: 19 additions & 1 deletion src/stdlib/base.rs
Original file line number Diff line number Diff line change
@@ -212,13 +212,31 @@ pub fn load_base<'gc>(ctx: Context<'gc>) {
Value::UserData(u) => u.metatable(),
_ => None,
} {
/// Simply matches PUC-Rio behavior of returning the first 3 elements of the __pairs metacall
#[derive(Collect)]
#[collect(require_static)]
struct PairsReturn;

impl<'gc> Sequence<'gc> for PairsReturn {
fn poll(
&mut self,
_ctx: Context<'gc>,
_exec: Execution<'gc, '_>,
mut stack: Stack<'gc, '_>,
) -> Result<SequencePoll<'gc>, Error<'gc>> {
// QUESTION how do you just consume the data w/o doing anything?
_ = stack.drain(3..).count();
Ok(SequencePoll::Return)
}
}

let pairs = mt.get(ctx, MetaMethod::Pairs);
if !pairs.is_nil() {
let function = meta_ops::call(ctx, pairs)?;
stack.replace(ctx, (table, Value::Nil));
return Ok(CallbackReturn::Call {
function,
then: None,
then: Some(BoxSequence::new(&ctx, PairsReturn)),
});
}
}
16 changes: 15 additions & 1 deletion tests/scripts/pairs.lua
Original file line number Diff line number Diff line change
@@ -60,4 +60,18 @@ do
for i = 1,10 do
assert(t2[i] == i, i)
end
end
end

do
local t = {}
setmetatable(t, {
__pairs = function()
return 1, 2, 3, 4
end
})
a, b, c, d = pairs(t)
assert(a == 1)
assert(b == 2)
assert(c == 3)
assert(d == nil)
end