Package org.luaj.vm2

Examples of org.luaj.vm2.LuaTable


  protected LuaTable new_Table() {
    return new LuaTable();
  }
 
  protected LuaTable new_Table(int n,int m) {
    return new LuaTable(n,m);
  }
View Full Code Here


  protected LuaTable new_Table(int n,int m) {
    return new LuaTable(n,m);
  }
 
  public void testSetRemove() {
    LuaTable t = new_Table();
   
    assertEquals( 0, t.getHashLength() );
    assertEquals( 0, t.length() );
    assertEquals( 0, t.keyCount() );
   
    String[] keys = { "abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "wxy", "z01",
        "cd", "ef", "g", "hi", "jk", "lm", "no", "pq", "rs", };   
    int[] capacities = { 0, 2, 4, 4, 7, 7, 7, 10, 10, 14, 14, 14, 14, 19, 19, 19, 19, 25, 25, 25 };
    for ( int i = 0; i < keys.length; ++i ) {
      assertEquals( capacities[i], t.getHashLength() );
      String si = "Test Value! "+i;
      t.set( keys[i], si );
      assertEquals( 0, t.length() );
      assertEquals( i+1, t.keyCount() );
    }
    assertEquals( capacities[keys.length], t.getHashLength() );
    for ( int i = 0; i < keys.length; ++i ) {
      LuaValue vi = LuaString.valueOf( "Test Value! "+i );
      assertEquals( vi, t.get( keys[i] ) );
      assertEquals( vi, t.get( LuaString.valueOf(keys[i]) ) );
      assertEquals( vi, t.rawget( keys[i] ) );
      assertEquals( vi, t.rawget( keys[i] ) );
    }

    // replace with new values
    for ( int i = 0; i < keys.length; ++i ) {
      t.set( keys[i], LuaString.valueOf( "Replacement Value! "+i ) );
      assertEquals( 0, t.length() );
      assertEquals( keys.length, t.keyCount() );
      assertEquals( capacities[keys.length], t.getHashLength() );
    }
    for ( int i = 0; i < keys.length; ++i ) {
      LuaValue vi = LuaString.valueOf( "Replacement Value! "+i );
      assertEquals( vi, t.get( keys[i] ) );
    }

    // remove
    for ( int i = 0; i < keys.length; ++i ) {
      t.set( keys[i], LuaValue.NIL );
      assertEquals( 0, t.length() );
      assertEquals( keys.length-i-1, t.keyCount() );
      if ( i<keys.length-1 )
        assertEquals( capacities[keys.length], t.getHashLength() );
      else
        assertTrue( 0<=t.getHashLength()  );
    }
    for ( int i = 0; i < keys.length; ++i ) {
      assertEquals( LuaValue.NIL, t.get( keys[i] ) );     
    }
  }
View Full Code Here

      assertEquals( LuaValue.NIL, t.get( keys[i] ) );     
    }
  }
 
  public void testIndexMetatag() {
    LuaTable t = new_Table();
    LuaTable mt = new_Table();
    LuaTable fb = new_Table();
   
    // set basic values
    t.set( "ppp", "abc" );
    t.set( 123, "def" );
    mt.set(LuaValue.INDEX, fb);
    fb.set( "qqq", "ghi" );
    fb.set( 456, "jkl" );
   
    // check before setting metatable
    assertEquals( "abc", t.get("ppp").tojstring() );
    assertEquals( "def", t.get(123).tojstring() );
    assertEquals( "nil", t.get("qqq").tojstring() );
    assertEquals( "nil", t.get(456).tojstring() );
    assertEquals( "nil", fb.get("ppp").tojstring() );
    assertEquals( "nil", fb.get(123).tojstring() );
    assertEquals( "ghi", fb.get("qqq").tojstring() );
    assertEquals( "jkl", fb.get(456).tojstring() );
    assertEquals( "nil", mt.get("ppp").tojstring() );
    assertEquals( "nil", mt.get(123).tojstring() );
    assertEquals( "nil", mt.get("qqq").tojstring() );
    assertEquals( "nil", mt.get(456).tojstring() );
   
    // check before setting metatable
    t.setmetatable(mt);
    assertEquals( mt, t.getmetatable() );
    assertEquals( "abc", t.get("ppp").tojstring() );
    assertEquals( "def", t.get(123).tojstring() );
    assertEquals( "ghi", t.get("qqq").tojstring() );
    assertEquals( "jkl", t.get(456).tojstring() );
    assertEquals( "nil", fb.get("ppp").tojstring() );
    assertEquals( "nil", fb.get(123).tojstring() );
    assertEquals( "ghi", fb.get("qqq").tojstring() );
    assertEquals( "jkl", fb.get(456).tojstring() );
    assertEquals( "nil", mt.get("ppp").tojstring() );
    assertEquals( "nil", mt.get(123).tojstring() );
    assertEquals( "nil", mt.get("qqq").tojstring() );
    assertEquals( "nil", mt.get(456).tojstring() );
View Full Code Here

    assertEquals( "nil", t.get("qqq").tojstring() );
    assertEquals( "nil", t.get(456).tojstring() );
  }   

  public void testIndexFunction() {
    final LuaTable t = new_Table();
    final LuaTable mt = new_Table();   
   
    final TwoArgFunction fb = new TwoArgFunction() {
      public LuaValue call(LuaValue tbl, LuaValue key) {
        assertEquals(tbl, t);
        return valueOf("from mt: "+key);
      }
    };
   
    // set basic values
    t.set( "ppp", "abc" );
    t.set( 123, "def" );
    mt.set(LuaValue.INDEX, fb);
   
    // check before setting metatable
    assertEquals( "abc", t.get("ppp").tojstring() );
    assertEquals( "def", t.get(123).tojstring() );
    assertEquals( "nil", t.get("qqq").tojstring() );
View Full Code Here

    assertEquals( "nil", t.get("qqq").tojstring() );
    assertEquals( "nil", t.get(456).tojstring() );
  }
 
  public void testNext() {
    final LuaTable t = new_Table();
    assertEquals( LuaValue.NIL, t.next(LuaValue.NIL) );
   
    // insert array elements
    t.set( 1, "one" );
    assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
    assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
    assertEquals( LuaValue.NIL, t.next(LuaValue.ONE) );
    t.set( 2, "two" );
    assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
    assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
    assertEquals( LuaValue.valueOf(2),     t.next(LuaValue.ONE).arg(1) );
    assertEquals( LuaValue.valueOf("two"), t.next(LuaValue.ONE).arg(2) );
    assertEquals( LuaValue.NIL, t.next(LuaValue.valueOf(2)) );
   
    // insert hash elements
    t.set( "aa", "aaa" );
    assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
    assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
    assertEquals( LuaValue.valueOf(2),     t.next(LuaValue.ONE).arg(1) );
    assertEquals( LuaValue.valueOf("two"), t.next(LuaValue.ONE).arg(2) );
    assertEquals( LuaValue.valueOf("aa"),  t.next(LuaValue.valueOf(2)).arg(1) );
    assertEquals( LuaValue.valueOf("aaa"), t.next(LuaValue.valueOf(2)).arg(2) );
    assertEquals( LuaValue.NIL, t.next(LuaValue.valueOf("aa")) );
    t.set( "bb", "bbb" );
    assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
    assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
    assertEquals( LuaValue.valueOf(2),     t.next(LuaValue.ONE).arg(1) );
    assertEquals( LuaValue.valueOf("two"), t.next(LuaValue.ONE).arg(2) );
    assertEquals( LuaValue.valueOf("aa"),  t.next(LuaValue.valueOf(2)).arg(1) );
    assertEquals( LuaValue.valueOf("aaa"), t.next(LuaValue.valueOf(2)).arg(2) );
    assertEquals( LuaValue.valueOf("bb"),  t.next(LuaValue.valueOf("aa")).arg(1) );
    assertEquals( LuaValue.valueOf("bbb"), t.next(LuaValue.valueOf("aa")).arg(2) );
    assertEquals( LuaValue.NIL, t.next(LuaValue.valueOf("bb")) );
  }
View Full Code Here

  public static void main(String[] args) throws Exception {
    System.out.println(script);
    try {
     
      // create an environment to run in
      LuaTable _G = JsePlatform.standardGlobals();
     
      // compile into a chunk, or load as a class
      LuaValue chunk;
      if ( ! (args.length>0 && args[0].equals("nocompile")) ) {
        InputStream is =  new ByteArrayInputStream( script.getBytes() );
View Full Code Here

   */
  public OsLib() {
  }

  public LuaValue init() {
    LuaTable t = new LuaTable();
    bind(t, this.getClass(), NAMES, CLOCK);
    env.set("os", t);
    PackageLib.instance.LOADED.set("os", t);
    return t;
  }
View Full Code Here

  }
 
  public LuaValue call(LuaValue arg) {
   
    // io lib functions
    LuaTable t = new LuaTable();
    bind(t, IoLibV.class, IO_NAMES );
   
    // create file methods table
    filemethods = new LuaTable();
    bind(filemethods, IoLibV.class, FILE_NAMES, FILE_CLOSE );

    // set up file metatable
    LuaTable mt = new LuaTable();
    bind(mt, IoLibV.class, new String[] { "__index" }, IO_INDEX );
    t.setmetatable( mt );
   
    // all functions link to library instance
    setLibInstance( t );
View Full Code Here

* Tests for tables used as lists.
*/
public class TableArrayTest extends TestCase {
 
  protected LuaTable new_Table() {
    return new LuaTable();
  }
View Full Code Here

  protected LuaTable new_Table() {
    return new LuaTable();
  }
 
  protected LuaTable new_Table(int n,int m) {
    return new LuaTable(n,m);
  }
View Full Code Here

TOP

Related Classes of org.luaj.vm2.LuaTable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.