From d6a35253578e76e30f1d2351ff57a815e40f2fff Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 6 Nov 2007 23:33:11 +0000 Subject: [PATCH] Fix return value of table.remove() --- .../java/lua/addon/luacompat/LuaCompat.java | 3 ++- src/main/java/lua/value/LTable.java | 16 +++++++++++----- src/test/res/table.lua | 14 +++++++------- src/test/res/table.luac | Bin 4741 -> 4853 bytes 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/addon/java/lua/addon/luacompat/LuaCompat.java b/src/addon/java/lua/addon/luacompat/LuaCompat.java index 1024b717..a9be8a7c 100644 --- a/src/addon/java/lua/addon/luacompat/LuaCompat.java +++ b/src/addon/java/lua/addon/luacompat/LuaCompat.java @@ -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]) diff --git a/src/main/java/lua/value/LTable.java b/src/main/java/lua/value/LTable.java index b7df6343..f294c12e 100644 --- a/src/main/java/lua/value/LTable.java +++ b/src/main/java/lua/value/LTable.java @@ -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 ) { - put( pos, LNil.NIL ); + 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; } } diff --git a/src/test/res/table.lua b/src/test/res/table.lua index facaeb79..a2cd8800 100644 --- a/src/test/res/table.lua +++ b/src/test/res/table.lua @@ -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 diff --git a/src/test/res/table.luac b/src/test/res/table.luac index 91b14f2e2ab89781cefd22f84dc632e378ced544..065eaa9419901654eb334bf47183537425c4a56f 100644 GIT binary patch delta 275 zcmZow{i-@aopJd_4M}D(28LD!HiotXo{R?=7#QX-FfhhAFfdH^XV#NKm11mU2TC{K zlxF0@EA5CR&CgsWGg^TiRc4z&;#yE4c1CKiM