Fix return value for table.remove() and table.insert()

This commit is contained in:
James Roseborough
2015-03-15 05:45:50 +00:00
parent 4c3bb1d709
commit 06a9ddbb88
3 changed files with 32 additions and 21 deletions

View File

@@ -974,6 +974,7 @@ Files are no longer hosted at LuaForge.
<li>collectgarbage() now behaves same as collectgarbage("collect") (fixes issue #41).</li> <li>collectgarbage() now behaves same as collectgarbage("collect") (fixes issue #41).</li>
<li>Allow access to Java inner classes using lua field syntax (fixes issue #40).</li> <li>Allow access to Java inner classes using lua field syntax (fixes issue #40).</li>
<li>List keyeq() and keyindex() methods as abstract on LuaTable.Entry (issue #37).</li> <li>List keyeq() and keyindex() methods as abstract on LuaTable.Entry (issue #37).</li>
<li>Fix return value for table.remove() and table.insert() (issue #39)</li>
</ul></td></tr> </ul></td></tr>
</table></td></tr></table> </table></td></tr></table>

View File

@@ -90,18 +90,23 @@ public class TableLib extends TwoArgFunction {
} }
} }
// "insert" (table, [pos,] value) -> prev-ele // "insert" (table, [pos,] value)
static class insert extends TableLibFunction { static class insert extends VarArgFunction {
public LuaValue call(LuaValue list) { public Varargs invoke(Varargs args) {
return argerror(2, "value expected"); switch (args.narg()) {
} case 0: case 1: {
public LuaValue call(LuaValue table, LuaValue value) { return argerror(2, "value expected");
table.checktable().insert(table.length()+1,value); }
return NONE; case 2: {
} LuaTable table = args.arg1().checktable();
public LuaValue call(LuaValue table, LuaValue pos, LuaValue value) { table.insert(table.length()+1,args.arg(2));
table.checktable().insert(pos.checkint(),value); return NONE;
return NONE; }
default: {
args.arg1().checktable().insert(args.checkint(2),args.arg(3));
return NONE;
}
}
} }
} }
@@ -115,23 +120,22 @@ public class TableLib extends TwoArgFunction {
} }
// "remove" (table [, pos]) -> removed-ele // "remove" (table [, pos]) -> removed-ele
static class remove extends TableLibFunction { static class remove extends VarArgFunction {
public LuaValue call(LuaValue list) { public Varargs invoke(Varargs args) {
return list.checktable().remove(0); return args.arg1().checktable().remove(args.optint(2, 0));
}
public LuaValue call(LuaValue list, LuaValue pos) {
return list.checktable().remove(pos.checkint());
} }
} }
// "sort" (table [, comp]) // "sort" (table [, comp])
static class sort extends TwoArgFunction { static class sort extends VarArgFunction {
public LuaValue call(LuaValue table, LuaValue compare) { public Varargs invoke(Varargs args) {
table.checktable().sort(compare.isnil()? NIL: compare.checkfunction()); args.arg1().checktable().sort(
args.arg(2).isnil()? NIL: args.arg(2).checkfunction());
return NONE; return NONE;
} }
} }
// "unpack", // (list [,i [,j]]) -> result1, ... // "unpack", // (list [,i [,j]]) -> result1, ...
static class unpack extends VarArgFunction { static class unpack extends VarArgFunction {
public Varargs invoke(Varargs args) { public Varargs invoke(Varargs args) {

View File

@@ -594,5 +594,11 @@ public class FragmentsTest extends TestSuite {
public void testBalancedMatchOnEmptyString() { public void testBalancedMatchOnEmptyString() {
runFragment(LuaValue.NIL, "return (\"\"):match(\"%b''\")\n"); runFragment(LuaValue.NIL, "return (\"\"):match(\"%b''\")\n");
} }
public void testReturnValueForTableRemove() {
runFragment(LuaValue.NONE, "return table.remove({ })");
}
public void testTypeOfTableRemoveReturnValue() {
runFragment(LuaValue.valueOf("nil"), "local k = table.remove({ }) return type(k)");
}
} }
} }