Fix table.remove:
(1) When nothing is removed, it returns zero results, not 1 nil result. (2) Ignore requests to remove elements past the length of the table.
This commit is contained in:
@@ -22,9 +22,9 @@
|
|||||||
package org.luaj.lib;
|
package org.luaj.lib;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.luaj.vm.LFunction;
|
import org.luaj.vm.LFunction;
|
||||||
|
import org.luaj.vm.LNil;
|
||||||
import org.luaj.vm.LString;
|
import org.luaj.vm.LString;
|
||||||
import org.luaj.vm.LTable;
|
import org.luaj.vm.LTable;
|
||||||
import org.luaj.vm.LValue;
|
import org.luaj.vm.LValue;
|
||||||
@@ -176,7 +176,9 @@ public class TableLib extends LFunction {
|
|||||||
LTable table = vm.totable(2);
|
LTable table = vm.totable(2);
|
||||||
int pos = (n>=3? vm.tointeger(3): 0);
|
int pos = (n>=3? vm.tointeger(3): 0);
|
||||||
vm.resettop();
|
vm.resettop();
|
||||||
vm.pushlvalue( table.luaRemovePos(pos) );
|
LValue removed = table.luaRemovePos( pos );
|
||||||
|
if ( removed != LNil.NIL )
|
||||||
|
vm.pushlvalue( removed );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -343,9 +343,12 @@ public class LTable extends LValue {
|
|||||||
* @param pos position to remove, or 0 to remove last element
|
* @param pos position to remove, or 0 to remove last element
|
||||||
*/
|
*/
|
||||||
public LValue luaRemovePos(int ikey) {
|
public LValue luaRemovePos(int ikey) {
|
||||||
|
int n = luaLength();
|
||||||
if ( ikey == 0 )
|
if ( ikey == 0 )
|
||||||
if ( (ikey = luaLength()) <= 0 )
|
ikey = n;
|
||||||
return LNil.NIL;
|
if ( ikey <= 0 || ikey > n )
|
||||||
|
return LNil.NIL;
|
||||||
|
|
||||||
LValue removed = get(ikey);
|
LValue removed = get(ikey);
|
||||||
LValue replaced;
|
LValue replaced;
|
||||||
do {
|
do {
|
||||||
|
|||||||
Reference in New Issue
Block a user