org.luaj.vm2.LuaInteger Java Examples

The following examples show how to use org.luaj.vm2.LuaInteger. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LuaJavaCoercionTest.java    From luaj with MIT License 6 votes vote down vote up
public void testIntArrayScoringTables() {
	int a = 5;
	LuaValue la = LuaInteger.valueOf(a);
	LuaTable tb = new LuaTable();
	tb.set( 1, la );
	LuaTable tc = new LuaTable();
	tc.set( 1, tb );
	
	int saa = CoerceLuaToJava.getCoercion(int.class).score(la);
	int sab = CoerceLuaToJava.getCoercion(int[].class).score(la);
	int sac = CoerceLuaToJava.getCoercion(int[][].class).score(la);
	assertTrue( saa < sab );
	assertTrue( saa < sac );
	int sba = CoerceLuaToJava.getCoercion(int.class).score(tb);
	int sbb = CoerceLuaToJava.getCoercion(int[].class).score(tb);
	int sbc = CoerceLuaToJava.getCoercion(int[][].class).score(tb);
	assertTrue( sbb < sba );
	assertTrue( sbb < sbc );
	int sca = CoerceLuaToJava.getCoercion(int.class).score(tc);
	int scb = CoerceLuaToJava.getCoercion(int[].class).score(tc);
	int scc = CoerceLuaToJava.getCoercion(int[][].class).score(tc);
	assertTrue( scc < sca );
	assertTrue( scc < scb );
}
 
Example #2
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
void fornum(LuaString varname, int line) {
/* fornum -> NAME = exp1,exp1[,exp1] forbody */
      FuncState fs = this.fs;
      int base = fs.freereg;
      this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_INDEX);
      this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_LIMIT);
      this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_STEP);
      this.new_localvar(varname);
      this.checknext('=');
      this.exp1(); /* initial value */
      this.checknext(',');
      this.exp1(); /* limit */
      if (this.testnext(','))
          this.exp1(); /* optional step */
      else { /* default step = 1 */
          fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(LuaInteger.valueOf(1)));
          fs.reserveregs(1);
      }
      this.forbody(base, line, 1, true);
  }
 
Example #3
Source File: LexState.java    From luaj with MIT License 6 votes vote down vote up
void fornum(LuaString varname, int line) {
	/* fornum -> NAME = exp1,exp1[,exp1] forbody */
	FuncState fs = this.fs;
	int base = fs.freereg;
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_INDEX);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_LIMIT);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_STEP);
	this.new_localvar(varname);
	this.checknext('=');
	this.exp1(); /* initial value */
	this.checknext(',');
	this.exp1(); /* limit */
	if (this.testnext(','))
		this.exp1(); /* optional step */
	else { /* default step = 1 */
		fs.codeK(fs.freereg, fs.numberK(LuaInteger.valueOf(1)));
		fs.reserveregs(1);
	}
	this.forbody(base, line, 1, true);
}
 
Example #4
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
void fornum(LuaString varname, int line) {
	/* fornum -> NAME = exp1,exp1[,exp1] forbody */
	FuncState fs = this.fs;
	int base = fs.freereg;
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_INDEX);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_LIMIT);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_STEP);
	this.new_localvar(varname);
	this.checknext('=');
	this.exp1(); /* initial value */
	this.checknext(',');
	this.exp1(); /* limit */
	if (this.testnext(','))
		this.exp1(); /* optional step */
	else { /* default step = 1 */
		fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(LuaInteger.valueOf(1)));
		fs.reserveregs(1);
	}
	this.forbody(base, line, 1, true);
}
 
Example #5
Source File: LexState.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
void fornum(LuaString varname, int line) {
	/* fornum -> NAME = exp1,exp1[,exp1] forbody */
	FuncState fs = this.fs;
	int base = fs.freereg;
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_INDEX);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_LIMIT);
	this.new_localvarliteral(RESERVED_LOCAL_VAR_FOR_STEP);
	this.new_localvar(varname);
	this.checknext('=');
	this.exp1(); /* initial value */
	this.checknext(',');
	this.exp1(); /* limit */
	if (this.testnext(','))
		this.exp1(); /* optional step */
	else { /* default step = 1 */
		fs.codeABx(Lua.OP_LOADK, fs.freereg, fs.numberK(LuaInteger.valueOf(1)));
		fs.reserveregs(1);
	}
	this.forbody(base, line, 1, true);
}
 
Example #6
Source File: FuncState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
int numberK(LuaValue r) {
	if ( r instanceof LuaDouble ) {
		double d = r.todouble();
		int i = (int) d;
		if ( d == (double) i ) 
			r = LuaInteger.valueOf(i);
	}
	return this.addk(r);
}
 
Example #7
Source File: RedisBinding.java    From claudb with MIT License 5 votes vote down vote up
private LuaValue toLuaNumber(IntegerRedisToken value) {
  Integer integer = value.getValue();
  if (integer == null) {
    return LuaValue.NIL;
  }
  return LuaInteger.valueOf(integer);
}
 
Example #8
Source File: LuaJavaCoercionTest.java    From luaj with MIT License 5 votes vote down vote up
public void testLuaTableToJavaIntArray() {
	LuaTable t = new LuaTable();
	t.set(1, LuaInteger.valueOf(222) );
	t.set(2, LuaInteger.valueOf(333) );
	int[] i = null;
	Object o = CoerceLuaToJava.coerce(t, int[].class);
	assertEquals( int[].class, o.getClass() );
	i = (int[]) o;
	assertEquals( 2, i.length );
	assertEquals( 222, i[0] );
	assertEquals( 333, i[1] );
}
 
Example #9
Source File: LuaJavaCoercionTest.java    From luaj with MIT License 5 votes vote down vote up
public void testLuaIntToJavaInt() {
	LuaInteger i = LuaInteger.valueOf(777);
	Object o = CoerceLuaToJava.coerce(i, int.class);
	assertEquals( Integer.class, o.getClass() );
	assertEquals( 777, ((Number)o).intValue() );
	o = CoerceLuaToJava.coerce(i, Integer.class);
	assertEquals( Integer.class, o.getClass() );
	assertEquals( new Integer(777), o );
}
 
Example #10
Source File: FuncState.java    From luaj with MIT License 5 votes vote down vote up
int numberK(LuaValue r) {
	if ( r instanceof LuaDouble ) {
		double d = r.todouble();
		int i = (int) d;
		if ( d == (double) i )
			r = LuaInteger.valueOf(i);
	}
	return this.addk(r);
}
 
Example #11
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testCallStringResult() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return 'hello' end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaValue theFunction = theGlobals.get("add");
    final LuaValue theValue = (LuaValue) theFunction.invoke(theArguments);
    Assert.assertTrue(theValue.isstring());
    Assert.assertEquals("hello", theValue.tojstring());
}
 
Example #12
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testCall() throws IOException {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Prototype thePrototype = theGlobals.compilePrototype(new StringReader("function add(a,b) return a + b end"), "script");
    new LuaClosure(thePrototype, theGlobals).call();
    final LuaValue theFunction = theGlobals.get("add");
    Assert.assertFalse(theFunction.isnil());
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[] {
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
    final LuaInteger theResult = (LuaInteger) theFunction.invoke(theArguments);
    Assert.assertEquals("300", theResult.tojstring());
}
 
Example #13
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testVarArgs() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final Varargs theArguments = LuaValue.varargsOf(new LuaValue[]{
            LuaInteger.valueOf(100),
            LuaInteger.valueOf(200)
    });
}
 
Example #14
Source File: LHttp.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
LResponse() {
    super();
    set("header", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            if (mHeader == null) {
                return LuaValue.NIL;
            }
            return LuaString.valueOf(mHeader);
        }
    });

    set("body", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            if (mBody == null) {
                return LuaValue.NIL;
            }
            return LuaString.valueOf(mBody);
        }
    });

    set("status", new ZeroArgFunction() {
        @Override
        public LuaValue call() {
            return LuaInteger.valueOf(mStatusCode);
        }
    });
}
 
Example #15
Source File: FuncState.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
int numberK(LuaValue r) {
	if ( r instanceof LuaDouble ) {
		double d = r.todouble();
		int i = (int) d;
		if ( d == (double) i ) 
			r = LuaInteger.valueOf(i);
	}
	return this.addk(r);
}
 
Example #16
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * convert a table to map
 *
 * @param table
 * @return
 */
public static HashMap<String, String> toMap(LuaTable table) {
    if (table != null) {
        final HashMap<String, String> result = new HashMap<String, String>();
        final LuaValue[] keys = table.keys();
        for (LuaValue key : keys) {
            LuaValue luaValue = table.get(key);
            String value = null;
            if (luaValue.istable()) {
                value = JsonUtil.toString(luaValue);
            } else {
                if (luaValue instanceof LuaBoolean) {
                    value = String.valueOf(luaValue.optboolean(false));
                } else if (luaValue instanceof LuaInteger) {
                    value = String.valueOf(luaValue.optint(0));
                } else if (luaValue instanceof LuaDouble) {
                    value = String.valueOf(luaValue.optdouble(0));
                } else if (luaValue instanceof LuaString) {
                    value = String.valueOf(luaValue.optstring(null));
                } else {
                    value = String.valueOf(luaValue);
                }
            }
            if (value != null) {
                result.put(key.optjstring(null), value);
            }
        }
        return result;
    }
    return null;
}
 
Example #17
Source File: FuncState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
int numberK(LuaValue r) {
	if ( r instanceof LuaDouble ) {
		double d = r.todouble();
		int i = (int) d;
		if ( d == (double) i ) 
			r = LuaInteger.valueOf(i);
	}
	return this.addk(r);
}
 
Example #18
Source File: LexState.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public LuaValue nval() {
	return (_nval == null? LuaInteger.valueOf(info): _nval);
}
 
Example #19
Source File: CoerceJavaToLua.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue coerce(Object javaValue) {
    Number n = (Number) javaValue;
    return LuaInteger.valueOf(n.intValue());
}
 
Example #20
Source File: CoerceJavaToLua.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public LuaValue coerce( Object javaValue ) {
	Character c = (Character) javaValue;
	return LuaInteger.valueOf( c.charValue() );
}
 
Example #21
Source File: CoerceJavaToLua.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public LuaValue coerce( Object javaValue ) {
	Number n = (Number) javaValue;
	return LuaInteger.valueOf( n.intValue() );
}
 
Example #22
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue nval() {
	return (_nval == null? LuaInteger.valueOf(info): _nval);
}
 
Example #23
Source File: CoerceJavaToLua.java    From luaj with MIT License 4 votes vote down vote up
public LuaValue coerce( Object javaValue ) {
	Number n = (Number) javaValue;
	return LuaInteger.valueOf( n.intValue() );
}
 
Example #24
Source File: CoerceJavaToLua.java    From luaj with MIT License 4 votes vote down vote up
public LuaValue coerce( Object javaValue ) {
	Character c = (Character) javaValue;
	return LuaInteger.valueOf( c.charValue() );
}
 
Example #25
Source File: LexState.java    From luaj with MIT License 4 votes vote down vote up
public LuaValue nval() {
	return (_nval == null? LuaInteger.valueOf(info): _nval);
}
 
Example #26
Source File: CoerceJavaToLua.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue coerce( Object javaValue ) {
	Character c = (Character) javaValue;
	return LuaInteger.valueOf( c.charValue() );
}
 
Example #27
Source File: CoerceJavaToLua.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue coerce( Object javaValue ) {
	Number n = (Number) javaValue;
	return LuaInteger.valueOf( n.intValue() );
}
 
Example #28
Source File: LuaJavaCoercionTest.java    From luaj with MIT License 4 votes vote down vote up
public void testJavaIntToLuaInt() {
	Integer i = Integer.valueOf(777);
	LuaValue v = CoerceJavaToLua.coerce(i);
	assertEquals( LuaInteger.class, v.getClass() );
	assertEquals( 777, v.toint() );
}
 
Example #29
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue nval() {
    return (_nval == null ? LuaInteger.valueOf(info) : _nval);
}
 
Example #30
Source File: CoerceJavaToLua.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue coerce(Object javaValue) {
    Character c = (Character) javaValue;
    return LuaInteger.valueOf(c.charValue());
}