Fixed table.unpack

This commit is contained in:
Enyby
2018-09-16 16:03:09 +03:00
committed by GitHub
parent d0ed5b80e5
commit 8fbca25dce

View File

@@ -898,6 +898,11 @@ public class LuaTable extends LuaValue implements Metatable {
/** Unpack the elements from i to j inclusive */
public Varargs unpack(int i, int j) {
if (j < i) return NONE;
int count = j - i;
if (count < 0) throw new LuaError("too many results to unpack: greater " + Integer.MAX_VALUE); // integer overflow
int max = 0x00ffffff;
if (count >= max) throw new LuaError("too many results to unpack: " + count + " (max is " + max + ')');
int n = j + 1 - i;
switch (n) {
case 0: return NONE;
@@ -906,10 +911,14 @@ public class LuaTable extends LuaValue implements Metatable {
default:
if (n < 0)
return NONE;
LuaValue[] v = new LuaValue[n];
while (--n >= 0)
v[n] = get(i+n);
return varargsOf(v);
try {
LuaValue[] v = new LuaValue[n];
while (--n >= 0)
v[n] = get(i+n);
return varargsOf(v);
} catch (OutOfMemoryError e) {
throw new LuaError("too many results to unpack [out of memory]: " + n);
}
}
}