Support __pairs and __ipairs
Added PAIRS and IPAIRS constants to LuaValue Merged pairs and ipairs into one class with a dynamic constructor, this is similar to Lua's pairsmeta function. Check for and call it's specific metamethod if it's available.
This commit is contained in:
@@ -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.lib.jse.JsePlatform
|
* @see org.luaj.vm2.lib.jse.JsePlatform
|
||||||
* @see org.luaj.vm2.lib.jme.JmePlatform
|
* @see org.luaj.vm2.lib.jme.JmePlatform
|
||||||
@@ -243,6 +243,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("");
|
||||||
|
|
||||||
|
|||||||
@@ -113,8 +113,8 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
|
|||||||
|
|
||||||
next next;
|
next next;
|
||||||
env.set("next", next = new next());
|
env.set("next", next = new next());
|
||||||
env.set("pairs", new pairs(next));
|
env.set("pairs", new pairsbase(PAIRS, NIL, next));
|
||||||
env.set("ipairs", new ipairs());
|
env.set("ipairs", new pairsbase(IPAIRS, ZERO, new inext()));
|
||||||
|
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
@@ -396,22 +396,25 @@ public class BaseLib extends TwoArgFunction implements ResourceFinder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// "pairs" (t) -> iter-func, t, nil
|
// pairsbase, // (t) -> iter-func, t, initial
|
||||||
static final class pairs extends VarArgFunction {
|
static final class pairsbase extends VarArgFunction {
|
||||||
final next next;
|
final LuaString method;
|
||||||
pairs(next next) {
|
final LuaValue initial;
|
||||||
this.next = next;
|
final VarArgFunction iter;
|
||||||
}
|
|
||||||
public Varargs invoke(Varargs args) {
|
pairsbase(LuaString method, LuaValue initial, VarArgFunction iter) {
|
||||||
return varargsOf( next, args.checktable(1), NIL );
|
this.method = method;
|
||||||
}
|
this.initial = initial;
|
||||||
|
this.iter = iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
// // "ipairs", // (t) -> iter-func, t, 0
|
|
||||||
static final class ipairs extends VarArgFunction {
|
|
||||||
inext inext = new inext();
|
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
return varargsOf( inext, args.checktable(1), ZERO );
|
LuaValue arg = args.arg1();
|
||||||
|
LuaValue t = arg.metatag(method);
|
||||||
|
if (!t.isnil())
|
||||||
|
// TODO: This can return more than 3 results.
|
||||||
|
return t.invoke(args.isvalue(1) ? arg : t);
|
||||||
|
return varargsOf(iter, args.checktable(1), initial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user