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
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 1 addition & 13 deletions COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ likely not be implemented due to differences between piccolo and PUC-Lua.
| ⚫️ | `load(chunk[, chunkname, mode, env])` | | |
| ⚫️ | `loadfile([filename, mode, env])` | | |
| 🔵 | `next(table [, index])` | | |
| 🟡 | `pairs(t)` | By default, PUC-Lua return `iter, table, nil` where as piccolo returns `iter, table`. Also how `__pairs` works differs[^1] | |
| 🔵 | `pairs(t)` | By default, PUC-Lua return `iter, table, nil` where as piccolo returns `iter, table`. | |
| 🔵 | `pcall(f, args...)` | | |
| 🔵 | `print(args...)` | | |
| ⚫️ | `rawequal(v1, v2)` | | |
Expand All @@ -56,18 +56,6 @@ likely not be implemented due to differences between piccolo and PUC-Lua.
| ⚫️ | `warn(msg, args...)` | | |
| ⚫️ | `xpcall(f, msgh, args...)` | | |

[^1]:
Given the code below, calling `pairs(t)`, PUC-Lua returns `1, 2, 3`, while piccolo returns `1, 2, 3, 4`. The documentation from PUC-Lua does state that `pairs(t)` "\[where] `t` has a metamethod `__pairs`, calls it with `t` as argument and returns the first three results from the call."

```lua
t = {}
tm = {}
function tm:__pairs()
return 1, 2, 3, 4
end
setmetatable(t, tm)
```

[^0]: Hedging b/c I don't know PUC-Lua like my reverse palm, and there might be differing behaviors if you poke both implementations to death, but that's not what this document is for.

## Coroutine
Expand Down
21 changes: 20 additions & 1 deletion src/stdlib/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,32 @@ 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(
self: Pin<&mut Self>,
_ctx: Context<'gc>,
_exec: Execution<'gc, '_>,
mut stack: Stack<'gc, '_>,
) -> Result<SequencePoll<'gc>, Error<'gc>> {
if stack.len() > 3 {
stack.drain(3..);
}
Ok(SequencePoll::Return)
}
}

let pairs = mt.get_value(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)),
});
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/scripts/pairs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,26 @@ do
local a, b = inext(t, math.maxinteger)
assert(a == -9223372036854775808 and b == 4)
end

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