Package org.luaj.vm2

Examples of org.luaj.vm2.LuaValue


      "  end,\n" +
      "} )\n";
    Varargs chunk = _G.get("loadstring").call(LuaValue.valueOf(script));
    if ( ! chunk.arg1().toboolean() )
      fail( chunk.arg(2).toString() );
    LuaValue result = chunk.arg1().call();
    Object u = result.touserdata();
    VarArgsInterface v = (VarArgsInterface) u;
    assertEquals( "foo", v.varargsMethod("foo") );
    assertEquals( "foo-bar", v.varargsMethod("foo", "bar") );
    assertEquals( "foo-bar-etc", v.varargsMethod("foo", "bar", "etc") );
    assertEquals( "foo-0-nil-nil", v.arrayargsMethod("foo", new String[0]) );
View Full Code Here


    if ( ! chunk.arg1().toboolean() )
      fail( chunk.arg(2).toString() );
    Varargs results = chunk.arg1().invoke();
    int nresults = results.narg();
    assertEquals( 2, nresults );
    LuaValue b = results.arg(1);
    LuaValue c = results.arg(2);
    String sb = b.tojstring();
    String sc = c.tojstring();
    assertEquals( "set("+typename+") "+value, sb );
    assertEquals( "setr("+typename+") "+value, sc );
  }
View Full Code Here

  private static final int [] samehash = { 0, 1, -1, 2, -2, 4, 8, 16, 32, Integer.MAX_VALUE, Integer.MIN_VALUE };
  private static final double [] diffhash = { .5, 1, 1.5, 1, .5, 1.5, 1.25, 2.5 };
 
  public void testDoubleHashCode() {
    for ( int i=0; i<samehash.length; i++ ) {
      LuaValue j = LuaInteger.valueOf(samehash[i]);
      LuaValue d = LuaDouble.valueOf(samehash[i]);
      int hj = j.hashCode();
      int hd = d.hashCode();
      assertEquals(hj, hd);
    }
    for ( int i=0; i<diffhash.length; i+=2 ) {
      LuaValue c = LuaValue.valueOf(diffhash[i+0]);
      LuaValue d = LuaValue.valueOf(diffhash[i+1]);
      int hc = c.hashCode();
      int hd = d.hashCode();
      assertTrue("hash codes are same: "+hc,hc!=hd);
    }
  }
View Full Code Here

     
      // 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() );
        chunk = LuaJC.getInstance().load(is, "script", _G);
      } else {
        chunk = (LuaValue) Class.forName("script").newInstance();
      }
      chunk.setfenv(_G);
 
      // call with arguments
      LuaValue[] vargs = new LuaValue[args.length];
      for ( int i=0; i<args.length; i++ )
        vargs[i] = LuaValue.valueOf(args[i]);
      Varargs cargs = LuaValue.varargsOf(vargs);
      Varargs v = chunk.invoke(cargs);
     
      // print the result
      for ( int i=1; i<=v.narg(); i++ )
        System.out.println("result["+i+"]: "+v.arg(i));
    } catch ( Throwable e ) {
View Full Code Here

  void dumpConstants(final Prototype f) throws IOException {
    final LuaValue[] k = f.k;
    int i, n = k.length;
    dumpInt(n);
    for (i = 0; i < n; i++) {
      final LuaValue o = k[i];
      switch ( o.type() ) {
      case LuaValue.TNIL:
        writer.write(LuaValue.TNIL);
        break;
      case LuaValue.TBOOLEAN:
        writer.write(LuaValue.TBOOLEAN);
        dumpChar(o.toboolean() ? 1 : 0);
        break;
      case LuaValue.TNUMBER:
        switch (NUMBER_FORMAT) {
        case NUMBER_FORMAT_FLOATS_OR_DOUBLES:
          writer.write(LuaValue.TNUMBER);
          dumpDouble(o.todouble());
          break;
        case NUMBER_FORMAT_INTS_ONLY:
          if ( ! ALLOW_INTEGER_CASTING && ! o.isint() )
            throw new java.lang.IllegalArgumentException("not an integer: "+o);
          writer.write(LuaValue.TNUMBER);
          dumpInt(o.toint());
          break;
        case NUMBER_FORMAT_NUM_PATCH_INT32:
          if ( o.isint() ) {
            writer.write(LuaValue.TINT);
            dumpInt(o.toint());
          } else {
            writer.write(LuaValue.TNUMBER);
            dumpDouble(o.todouble());
          }
          break;
        default:
          throw new IllegalArgumentException("number format not supported: "+NUMBER_FORMAT);
        }
View Full Code Here

  }

  private Varargs ioread(File f, Varargs args) throws IOException {
    int i,n=args.narg();
    LuaValue[] v = new LuaValue[n];
    LuaValue ai,vi;
    LuaString fmt;
    for ( i=0; i<n; ) {
      item: switch ( (ai = args.arg(i+1)).type() ) {
        case LuaValue.TNUMBER:
          vi = freadbytes(f,ai.toint());
          break item;
        case LuaValue.TSTRING:
          fmt = ai.checkstring();
          if ( fmt.m_length == 2 && fmt.m_bytes[fmt.m_offset] == '*' ) {
            switch ( fmt.m_bytes[fmt.m_offset+1] ) {
            case 'n': vi = freadnumber(f); break item;
            case 'l': vi = freadline(f); break item;
            case 'a': vi = freadall(f); break item;
View Full Code Here

      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 ) {
View Full Code Here

    }
  }
  public void testUniqueFactoryCoercible() {
    JavaClass c = JavaClass.forClass(B.class);
    assertEquals( JavaClass.class, c.getClass() );
    LuaValue constr = c.get("new");
    assertEquals( JavaConstructor.class, constr.getClass() );
    LuaValue v = constr.call(NUMS);
    Object b = v.touserdata();
    assertEquals( B.class, b.getClass() );
    assertEquals( 123, ((B)b).m_int_field );
    Object b0 = constr.call().touserdata();
    assertEquals( B.class, b0.getClass() );
    assertEquals( 0, ((B)b0).m_int_field );
View Full Code Here

    assertEquals( B.class, b0.getClass() );
    assertEquals( 0, ((B)b0).m_int_field );
  }
  public void testUniqueFactoryUncoercible() {
    JavaClass f = JavaClass.forClass(B.class);
    LuaValue constr = f.get("new");
    assertEquals( JavaConstructor.class, constr.getClass() );
    try {
      LuaValue v = constr.call(LuaValue.userdataOf(new Object()));
      Object b = v.touserdata();
      // fail( "did not throw lua error as expected" );
      assertEquals( 0, ((B)b).m_int_field );
    } catch ( LuaError e ) {
    }
  }
View Full Code Here

    } catch ( LuaError e ) {
    }
  }
  public void testOverloadedFactoryCoercible() {
    JavaClass f = JavaClass.forClass(C.class);
    LuaValue constr = f.get("new");
    assertEquals( JavaConstructor.Overload.class, constr.getClass() );
    Object c = constr.call().touserdata();
    Object ci = constr.call(LuaValue.valueOf(123)).touserdata();
    Object cs = constr.call(LuaValue.valueOf("abc")).touserdata();
    Object csi = constr.call( LuaValue.valueOf("def"), LuaValue.valueOf(456) ).touserdata();
    assertEquals( C.class, c.getClass() );
    assertEquals( C.class, ci.getClass() );
    assertEquals( C.class, cs.getClass() );
    assertEquals( C.class, csi.getClass() );
    assertEquals( null,  ((C)c).m_string_field );
View Full Code Here

TOP

Related Classes of org.luaj.vm2.LuaValue

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.