From 0f3fe0452d899d4e00046dae53d3705ea990cccf Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Sat, 8 Dec 2007 01:20:30 +0000 Subject: [PATCH] Add table.getn() for compatibility --- src/core/org/luaj/lib/TableLib.java | 31 ++++++++++++++++++++--------- src/test/res/table.lua | 12 +++++------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/core/org/luaj/lib/TableLib.java b/src/core/org/luaj/lib/TableLib.java index d7f110bb..10d01f0c 100644 --- a/src/core/org/luaj/lib/TableLib.java +++ b/src/core/org/luaj/lib/TableLib.java @@ -36,6 +36,7 @@ public class TableLib extends LFunction { public static final String[] NAMES = { "table", "concat", + "getn", "insert", "maxn", "remove", @@ -44,10 +45,11 @@ public class TableLib extends LFunction { private static final int INSTALL = 0; private static final int CONCAT = 1; - private static final int INSERT = 2; - private static final int MAXN = 3; - private static final int REMOVE = 4; - private static final int SORT = 5; + private static final int GETN = 2; + private static final int INSERT = 3; + private static final int MAXN = 4; + private static final int REMOVE = 5; + private static final int SORT = 6; public static void install( LTable globals ) { LTable table = new LTable(); @@ -114,12 +116,10 @@ public class TableLib extends LFunction { } 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. - * 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. + * Get length of table t. */ case INSERT: { int n = vm.gettop(); @@ -129,6 +129,19 @@ public class TableLib extends LFunction { table.luaInsertPos( pos, value ); 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) * diff --git a/src/test/res/table.lua b/src/test/res/table.lua index a2cd8800..2ffb8e31 100644 --- a/src/test/res/table.lua +++ b/src/test/res/table.lua @@ -21,17 +21,17 @@ tryconcat( {} ) -- insert, maxn print( '-- insert, maxn tests' ) 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') -print( table.concat(t,'-'), table.maxn(t), #t ) +print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) ) 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') -print( table.concat(t,'-'), table.maxn(t), #t ) +print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) ) 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') -print( table.concat(t,'-'), table.maxn(t), #t ) +print( table.concat(t,'-'), table.maxn(t), #t, table.getn(t) ) print( t[10] ) print( table.maxn({}), #{} )