Fix return value of table.remove()

This commit is contained in:
James Roseborough
2007-11-06 23:33:11 +00:00
parent f398387222
commit d6a3525357
4 changed files with 20 additions and 13 deletions

View File

@@ -723,7 +723,8 @@ public class LuaCompat extends LFunction {
int n = vm.gettop();
LTable table = vm.totable(2);
int pos = (n>=3? vm.tointeger(3): 0);
table.luaRemovePos( pos );
vm.settop(0);
vm.pushlvalue( table.luaRemovePos( pos ) );
}
/** table.sort (table [, comp])

View File

@@ -536,16 +536,22 @@ public class LTable extends LValue {
* Remove an element from the list part of the table
* @param pos position to remove, or 0 to remove last element
*/
public void luaRemovePos(int pos) {
public LValue luaRemovePos(int pos) {
if ( pos > m_arrayEntries ) {
LValue val = get( pos );
if ( val != LNil.NIL )
put( pos, LNil.NIL );
return val;
} else {
final int n = m_vector.length - 1;
final int index = Math.max(0,pos<=0? m_arrayEntries: pos)-1;
if ( index < 0 )
return;
System.arraycopy(m_vector, index+1, m_vector, index, m_vector.length-1-index);
m_vector[m_vector.length-1] = LNil.NIL;
return LNil.NIL;
LValue val = m_vector[index];
System.arraycopy(m_vector, index+1, m_vector, index, n-index);
m_vector[n] = LNil.NIL;
--m_arrayEntries;
return val;
}
}

View File

@@ -39,19 +39,19 @@ print( table.maxn({}), #{} )
print( '-- remove tests' )
t = { "one", "two", "three", "four", "five", "six", "seven", [10]="ten", a='aaa', b='bbb', c='ccc' }
print( table.concat(t,'-'), table.maxn(t), #t )
table.remove(t)
print( table.remove(t) )
print( table.concat(t,'-'), table.maxn(t) )
table.remove(t,1)
print( table.remove(t,1) )
print( table.concat(t,'-'), table.maxn(t) )
table.remove(t,3)
print( table.remove(t,3) )
print( table.concat(t,'-'), table.maxn(t) )
table.remove(t,5)
print( table.remove(t,5) )
print( table.concat(t,'-'), table.maxn(t), t[10] )
table.remove(t,10)
print( table.remove(t,10) )
print( table.concat(t,'-'), table.maxn(t), t[10] )
table.remove(t,-1)
print( table.remove(t,-1) )
print( table.concat(t,'-'), table.maxn(t), t[10] )
table.remove(t,-1)
print( table.remove(t,-1) )
print( table.concat(t,'-'), table.maxn(t), t[10] )
-- sort

Binary file not shown.