Examples of LuaValue


Examples of org.luaj.vm2.LuaValue

 
  public void testExceptionMessage() {
    String script = "local c = luajava.bindClass( \""+SomeClass.class.getName()+"\" )\n" +
        "return pcall( c.someMethod, c )";
    Varargs vresult = _G.get("loadstring").call(LuaValue.valueOf(script)).invoke(LuaValue.NONE);
    LuaValue status = vresult.arg1();
    LuaValue message = vresult.arg(2);
    assertEquals( LuaValue.FALSE, status );   
    int index = message.toString().indexOf( "this is some message" );
    assertTrue( "bad message: "+message, index>=0 );   
  }
View Full Code Here

Examples of org.luaj.vm2.LuaValue

    assertTrue( "bad message: "+message, index>=0 );   
  }

  public void testLuaErrorCause() {
    String script = "luajava.bindClass( \""+SomeClass.class.getName()+"\"):someMethod()";
    LuaValue chunk = _G.get("loadstring").call(LuaValue.valueOf(script));
    try {
      chunk.invoke(LuaValue.NONE);
      fail( "call should not have succeeded" );
    } catch ( LuaError lee ) {
      Throwable c = lee.getCause();
      assertEquals( SomeException.class, c.getClass() );
    }
View Full Code Here

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

Examples of org.luaj.vm2.LuaValue

    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

Examples of org.luaj.vm2.LuaValue

  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

Examples of org.luaj.vm2.LuaValue

     
      // 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

Examples of org.luaj.vm2.LuaValue

  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

Examples of org.luaj.vm2.LuaValue

  }

  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

Examples of org.luaj.vm2.LuaValue

      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

Examples of org.luaj.vm2.LuaValue

    }
  }
  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
TOP
Copyright © 2018 www.massapi.com. 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.