Java Code Examples for org.luaj.vm2.LuaValue#tojstring()

The following examples show how to use org.luaj.vm2.LuaValue#tojstring() . 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: CoerceLuaToJava.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public Object coerce(LuaValue value) {
    switch (value.type()) {
        case LuaValue.TNUMBER:
            return value.isint() ? (Object) new Integer(value.toint()) : (Object) new Double(value.todouble());
        case LuaValue.TBOOLEAN:
            return value.toboolean() ? Boolean.TRUE : Boolean.FALSE;
        case LuaValue.TSTRING:
            return value.tojstring();
        case LuaValue.TUSERDATA:
            return value.optuserdata(targetType, null);
        case LuaValue.TNIL:
            return null;
        default:
            return value;
    }
}
 
Example 2
Source File: CoerceLuaToJava.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
public Object coerce(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TNUMBER:
		return value.isint()? (Object)new Integer(value.toint()): (Object)new Double(value.todouble());
	case LuaValue.TBOOLEAN:
		return value.toboolean()? Boolean.TRUE: Boolean.FALSE;
	case LuaValue.TSTRING:
		return value.tojstring();
	case LuaValue.TUSERDATA:
		return value.optuserdata(targetType, null);
	case LuaValue.TNIL:
		return null;
	default:
		return value;
	}
}
 
Example 3
Source File: CoerceLuaToJava.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
public Object coerce(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TNUMBER:
		return value.isint()? (Object)new Integer(value.toint()): (Object)new Double(value.todouble());
	case LuaValue.TBOOLEAN:
		return value.toboolean()? Boolean.TRUE: Boolean.FALSE;
	case LuaValue.TSTRING:
		return value.tojstring();
	case LuaValue.TUSERDATA:
		return value.optuserdata(targetType, null);
	case LuaValue.TNIL:
		return null;
	default:
		return value;
	}
}
 
Example 4
Source File: CoerceLuaToJava.java    From luaj with MIT License 6 votes vote down vote up
public Object coerce(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TNUMBER:
		return value.isint()? (Object)new Integer(value.toint()): (Object)new Double(value.todouble());
	case LuaValue.TBOOLEAN:
		return value.toboolean()? Boolean.TRUE: Boolean.FALSE;
	case LuaValue.TSTRING:
		return value.tojstring();
	case LuaValue.TUSERDATA:
		return value.optuserdata(targetType, null);
	case LuaValue.TNIL:
		return null;
	default:
		return value;
	}
}
 
Example 5
Source File: LuaJavaCoercionTest.java    From luaj with MIT License 6 votes vote down vote up
private void doOverloadedMethodTest( String typename, String value ) {
	String script = 
		"local a = luajava.newInstance('"+B.class.getName()+"');\n" +
		"local b = a:set(a:get"+typename+"())\n" +
		"local c = a:setr(a:get"+typename+"())\n" +
		"return b,c";
	Varargs chunk = globals.get("load").call(LuaValue.valueOf(script));
	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 );
}
 
Example 6
Source File: CoerceLuaToJava.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public Object coerce(LuaValue value) {
    if (value.isnil())
        return null;
    if (targetType == TARGET_TYPE_STRING)
        return value.tojstring();
    LuaString s = value.checkstring();
    byte[] b = new byte[s.m_length];
    s.copyInto(0, b, 0, b.length);
    return b;
}
 
Example 7
Source File: CoerceLuaToJava.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public Object coerce(LuaValue value) {
	if ( value.isnil() )
		return null;
	if ( targetType == TARGET_TYPE_STRING )
		return value.tojstring();
	LuaString s = value.checkstring();
	byte[] b = new byte[s.m_length];
	s.copyInto(0, b, 0, b.length);
	return b;
}
 
Example 8
Source File: CoerceLuaToJava.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public Object coerce(LuaValue value) {
	if ( value.isnil() )
		return null;
	if ( targetType == TARGET_TYPE_STRING )
		return value.tojstring();
	LuaString s = value.checkstring();
	byte[] b = new byte[s.m_length];
	s.copyInto(0, b, 0, b.length);
	return b;
}
 
Example 9
Source File: LDocument.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
@Override
public LuaValue call(LuaValue arg) {

    String id = arg.tojstring();
    View v = mContext.findViewById(id);
    if (v != null) {
        LView lView = new LView(v, mContext);
        lView.mAdded = true;
        return lView;
    }

    return NIL;
}
 
Example 10
Source File: DefaultLauncher.java    From luaj with MIT License 5 votes vote down vote up
private Object[] launchChunk(LuaValue chunk, Object[] arg) {
	LuaValue args[] = new LuaValue[arg.length];
	for (int i = 0; i < args.length; ++i)
		args[i] = CoerceJavaToLua.coerce(arg[i]);
	Varargs results = chunk.invoke(LuaValue.varargsOf(args));

	final int n = results.narg();
	Object return_values[] = new Object[n];
	for (int i = 0; i < n; ++i) {
		LuaValue r = results.arg(i+1);
		switch (r.type()) {
		case LuaValue.TBOOLEAN:
			return_values[i] = r.toboolean();
			break;
		case LuaValue.TNUMBER:
			return_values[i] = r.todouble();
			break;
		case LuaValue.TINT:
			return_values[i] = r.toint();
			break;
		case LuaValue.TNIL:
			return_values[i] = null;
			break;
		case LuaValue.TSTRING:
			return_values[i] = r.tojstring();
			break;
		case LuaValue.TUSERDATA:
			return_values[i] = r.touserdata();
			break;
		default:
			return_values[i] = r;
		}
	}
	return return_values;
}
 
Example 11
Source File: CoerceLuaToJava.java    From luaj with MIT License 5 votes vote down vote up
public Object coerce(LuaValue value) {
	if ( value.isnil() )
		return null;
	if ( targetType == TARGET_TYPE_STRING )
		return value.tojstring();
	LuaString s = value.checkstring();
	byte[] b = new byte[s.m_length];
	s.copyInto(0, b, 0, b.length);
	return b;
}
 
Example 12
Source File: DebugLib.java    From luaj with MIT License 5 votes vote down vote up
static String kname(Prototype p, int pc, int c) {
	if (Lua.ISK(c)) {  /* is 'c' a constant? */
		LuaValue k = p.k[Lua.INDEXK(c)];
		if (k.isstring()) {  /* literal constant? */
			return k.tojstring();  /* it is its own name */
		} /* else no reasonable name found */
	} else {  /* 'c' is a register */
		NameWhat what = getobjname(p, pc, c); /* search for 'c' */
	    if (what != null && "constant".equals(what.namewhat)) {  /* found a constant name? */
	      return what.name;  /* 'name' already filled */
	    }
	    /* else no reasonable name found */
	}
	return "?";  /* no reasonable name found */
}
 
Example 13
Source File: BaseLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue call(LuaValue arg1, LuaValue arg2) {
    throw new LuaError(arg1.isnil() ? null : arg1.tojstring(), arg2.optint(1));
}
 
Example 14
Source File: BaseLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue call(LuaValue arg1, LuaValue arg2) {
	throw arg1.isnil()? new LuaError(null, arg2.optint(1)): 
		arg1.isstring()? new LuaError(arg1.tojstring(), arg2.optint(1)): 
			new LuaError(arg1);
}
 
Example 15
Source File: BaseLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public LuaValue call(LuaValue arg1, LuaValue arg2) {
    throw arg1.isnil() ? new LuaError(null, arg2.optint(1)) : arg1.isstring() ? new
            LuaError(arg1.tojstring(), arg2.optint(1)) : new LuaError(arg1);
}
 
Example 16
Source File: BaseLib.java    From luaj with MIT License 4 votes vote down vote up
public LuaValue call(LuaValue arg1, LuaValue arg2) {
	if (arg1.isnil()) throw new LuaError(NIL);
	if (!arg1.isstring() || arg2.optint(1) == 0) throw new LuaError(arg1);
	throw new LuaError(arg1.tojstring(), arg2.optint(1));
}