Add table.getn() for compatibility
This commit is contained in:
@@ -36,6 +36,7 @@ public class TableLib extends LFunction {
|
|||||||
public static final String[] NAMES = {
|
public static final String[] NAMES = {
|
||||||
"table",
|
"table",
|
||||||
"concat",
|
"concat",
|
||||||
|
"getn",
|
||||||
"insert",
|
"insert",
|
||||||
"maxn",
|
"maxn",
|
||||||
"remove",
|
"remove",
|
||||||
@@ -44,10 +45,11 @@ public class TableLib extends LFunction {
|
|||||||
|
|
||||||
private static final int INSTALL = 0;
|
private static final int INSTALL = 0;
|
||||||
private static final int CONCAT = 1;
|
private static final int CONCAT = 1;
|
||||||
private static final int INSERT = 2;
|
private static final int GETN = 2;
|
||||||
private static final int MAXN = 3;
|
private static final int INSERT = 3;
|
||||||
private static final int REMOVE = 4;
|
private static final int MAXN = 4;
|
||||||
private static final int SORT = 5;
|
private static final int REMOVE = 5;
|
||||||
|
private static final int SORT = 6;
|
||||||
|
|
||||||
public static void install( LTable globals ) {
|
public static void install( LTable globals ) {
|
||||||
LTable table = new LTable();
|
LTable table = new LTable();
|
||||||
@@ -115,11 +117,9 @@ public class TableLib extends LFunction {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* table.insert (table, [pos,] value)
|
/* table.getn (table)
|
||||||
*
|
*
|
||||||
* Inserts element value at position pos in table, shifting up other elements to open space, if necessary.
|
* Get length of table t.
|
||||||
* The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call
|
|
||||||
* table.insert(t,x) inserts x at the end of table t.
|
|
||||||
*/
|
*/
|
||||||
case INSERT: {
|
case INSERT: {
|
||||||
int n = vm.gettop();
|
int n = vm.gettop();
|
||||||
@@ -130,6 +130,19 @@ public class TableLib extends LFunction {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* table.insert (table, [pos,] value)
|
||||||
|
*
|
||||||
|
* Inserts element value at position pos in table, shifting up other elements to open space, if necessary.
|
||||||
|
* The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call
|
||||||
|
* table.insert(t,x) inserts x at the end of table t.
|
||||||
|
*/
|
||||||
|
case GETN: {
|
||||||
|
LTable table = vm.totable(2);
|
||||||
|
vm.resettop();
|
||||||
|
vm.pushinteger(table.luaLength());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/* table.maxn (table)
|
/* table.maxn (table)
|
||||||
*
|
*
|
||||||
* Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical
|
* Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical
|
||||||
|
|||||||
@@ -21,17 +21,17 @@ tryconcat( {} )
|
|||||||
-- insert, maxn
|
-- insert, maxn
|
||||||
print( '-- insert, maxn tests' )
|
print( '-- insert, maxn tests' )
|
||||||
local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' }
|
local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' }
|
||||||
print( table.concat(t,'-'), table.maxn(t), #t )
|
print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) )
|
||||||
table.insert(t,'six')
|
table.insert(t,'six')
|
||||||
print( table.concat(t,'-'), table.maxn(t), #t )
|
print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) )
|
||||||
table.insert(t,1,'seven')
|
table.insert(t,1,'seven')
|
||||||
print( table.concat(t,'-'), table.maxn(t), #t )
|
print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) )
|
||||||
table.insert(t,4,'eight')
|
table.insert(t,4,'eight')
|
||||||
print( table.concat(t,'-'), table.maxn(t), #t )
|
print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) )
|
||||||
table.insert(t,7,'nine')
|
table.insert(t,7,'nine')
|
||||||
print( table.concat(t,'-'), table.maxn(t), #t )
|
print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) )
|
||||||
table.insert(t,10,'ten')
|
table.insert(t,10,'ten')
|
||||||
print( table.concat(t,'-'), table.maxn(t), #t )
|
print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) )
|
||||||
print( t[10] )
|
print( t[10] )
|
||||||
print( table.maxn({}), #{} )
|
print( table.maxn({}), #{} )
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user