Fixed issue: #51

This commit is contained in:
UnlegitDqrk
2026-03-02 15:28:31 +01:00
parent 7aea99d650
commit 63edacbb5f
11 changed files with 55 additions and 3 deletions

View File

@@ -99,7 +99,7 @@ package org.luaj.vm2;
* {@link #INDEX}, {@link #NEWINDEX}, {@link #CALL}, {@link #MODE}, {@link #METATABLE}, * {@link #INDEX}, {@link #NEWINDEX}, {@link #CALL}, {@link #MODE}, {@link #METATABLE},
* {@link #ADD}, {@link #SUB}, {@link #DIV}, {@link #MUL}, {@link #POW}, * {@link #ADD}, {@link #SUB}, {@link #DIV}, {@link #MUL}, {@link #POW},
* {@link #MOD}, {@link #UNM}, {@link #LEN}, {@link #EQ}, {@link #LT}, * {@link #MOD}, {@link #UNM}, {@link #LEN}, {@link #EQ}, {@link #LT},
* {@link #LE}, {@link #TOSTRING}, and {@link #CONCAT}. * {@link #LE}, {@link #TOSTRING}, {@link #CONCAT}, {@link #PAIRS}, and {@link #IPAIRS}.
* *
* @see org.luaj.vm2.libs.jse.JsePlatform * @see org.luaj.vm2.libs.jse.JsePlatform
* @see org.luaj.vm2.libs.jme.JmePlatform * @see org.luaj.vm2.libs.jme.JmePlatform
@@ -264,6 +264,12 @@ public class LuaValue extends Varargs {
/** LuaString constant with value "__concat" for use as metatag */ /** LuaString constant with value "__concat" for use as metatag */
public static final LuaString CONCAT = valueOf("__concat"); public static final LuaString CONCAT = valueOf("__concat");
/** LuaString constant with value "__pairs" for use as metatag */
public static final LuaString PAIRS = valueOf("__pairs");
/** LuaString constant with value "__ipairs" for use as metatag */
public static final LuaString IPAIRS = valueOf("__ipairs");
/** LuaString constant with value "" */ /** LuaString constant with value "" */
public static final LuaString EMPTYSTRING = valueOf(""); public static final LuaString EMPTYSTRING = valueOf("");

View File

@@ -399,7 +399,9 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
this.next = next; this.next = next;
} }
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
return varargsOf( next, args.checktable(1), NIL ); LuaValue table = args.arg1();
LuaValue h = table.metatag(PAIRS);
return h.isnil()? varargsOf(next, table.checktable(), NIL): h.invoke(table);
} }
} }
@@ -407,7 +409,9 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
static final class ipairs extends VarArgFunction { static final class ipairs extends VarArgFunction {
inext inext = new inext(); inext inext = new inext();
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {
return varargsOf( inext, args.checktable(1), ZERO ); LuaValue table = args.arg1();
LuaValue h = table.metatag(IPAIRS);
return h.isnil()? varargsOf(inext, table.checktable(), ZERO): h.invoke(table);
} }
} }

View File

@@ -331,6 +331,48 @@ public class FragmentsTest extends TestSuite {
} }
} }
public void testPairsUsesMetatableOverride() {
runFragment(
LuaValue.varargsOf(new LuaValue[] {
LuaValue.valueOf("k"),
LuaValue.valueOf("v")
}),
"local t = setmetatable({}, {\n" +
" __pairs = function(self)\n" +
" local done = false\n" +
" return function()\n" +
" if done then return nil end\n" +
" done = true\n" +
" return 'k', 'v'\n" +
" end, self, nil\n" +
" end\n" +
"})\n" +
"for k, v in pairs(t) do\n" +
" return k, v\n" +
"end\n");
}
public void testIpairsUsesMetatableOverride() {
runFragment(
LuaValue.varargsOf(new LuaValue[] {
LuaValue.valueOf(7),
LuaValue.valueOf("x")
}),
"local t = setmetatable({}, {\n" +
" __ipairs = function(self)\n" +
" local done = false\n" +
" return function()\n" +
" if done then return nil end\n" +
" done = true\n" +
" return 7, 'x'\n" +
" end, self, 0\n" +
" end\n" +
"})\n" +
"for i, v in ipairs(t) do\n" +
" return i, v\n" +
"end\n");
}
public void testTableMove() { public void testTableMove() {
runFragment( runFragment(
LuaValue.varargsOf(new LuaValue[] { LuaValue.varargsOf(new LuaValue[] {