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(); int n = vm.gettop();
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);
table.luaRemovePos( pos ); vm.settop(0);
vm.pushlvalue( table.luaRemovePos( pos ) );
} }
/** table.sort (table [, comp]) /** 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 * Remove an element from the list part of the table
* @param pos position to remove, or 0 to remove last element * @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 ) { if ( pos > m_arrayEntries ) {
LValue val = get( pos );
if ( val != LNil.NIL )
put( pos, LNil.NIL ); put( pos, LNil.NIL );
return val;
} else { } else {
final int n = m_vector.length - 1;
final int index = Math.max(0,pos<=0? m_arrayEntries: pos)-1; final int index = Math.max(0,pos<=0? m_arrayEntries: pos)-1;
if ( index < 0 ) if ( index < 0 )
return; return LNil.NIL;
System.arraycopy(m_vector, index+1, m_vector, index, m_vector.length-1-index); LValue val = m_vector[index];
m_vector[m_vector.length-1] = LNil.NIL; System.arraycopy(m_vector, index+1, m_vector, index, n-index);
m_vector[n] = LNil.NIL;
--m_arrayEntries; --m_arrayEntries;
return val;
} }
} }

View File

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

Binary file not shown.