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

The following examples show how to use org.luaj.vm2.LuaValue#type() . 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: DebugLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public LuaValue call(LuaValue value, LuaValue table) {
    LuaValue mt = table.opttable(null);
    switch (value.type()) {
        case TNIL:
            LuaNil.s_metatable = mt;
            break;
        case TNUMBER:
            LuaNumber.s_metatable = mt;
            break;
        case TBOOLEAN:
            LuaBoolean.s_metatable = mt;
            break;
        case TSTRING:
            LuaString.s_metatable = mt;
            break;
        case TFUNCTION:
            LuaFunction.s_metatable = mt;
            break;
        case TTHREAD:
            LuaThread.s_metatable = mt;
            break;
        default:
            value.setmetatable(mt);
    }
    return value;
}
 
Example 2
Source File: CoerceLuaToJava.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
public int score(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TNUMBER:
		return inheritanceLevels( targetType, value.isint()? Integer.class: Double.class );
	case LuaValue.TBOOLEAN:
		return inheritanceLevels( targetType, Boolean.class );
	case LuaValue.TSTRING:
		return inheritanceLevels( targetType, String.class );
	case LuaValue.TUSERDATA:
		return inheritanceLevels( targetType, value.touserdata().getClass() );
	case LuaValue.TNIL:
		return SCORE_NULL_VALUE;
	default:
		return inheritanceLevels( targetType, value.getClass() );
	}
}
 
Example 3
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.TTABLE: {
		int n = value.length();
		Object a = Array.newInstance(componentType, n);
		for ( int i=0; i<n; i++ )
			Array.set(a, i, componentCoercion.coerce(value.get(i+1)));
		return a;
	}
	case LuaValue.TUSERDATA:
		return value.touserdata();
	case LuaValue.TNIL:
		return null;
	default: 
		return null;
	}
	
}
 
Example 4
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.TTABLE: {
            int n = value.length();
            Object a = Array.newInstance(componentType, n);
            for (int i = 0; i < n; i++)
                Array.set(a, i, componentCoercion.coerce(value.get(i + 1)));
            return a;
        }
        case LuaValue.TUSERDATA:
            return value.touserdata();
        case LuaValue.TNIL:
            return null;
        default:
            return null;
    }

}
 
Example 5
Source File: CoerceLuaToJava.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public int score(LuaValue value) {
    switch (value.type()) {
        case LuaValue.TNUMBER:
            return inheritanceLevels(targetType, value.isint() ? Integer.class : Double.class);
        case LuaValue.TBOOLEAN:
            return inheritanceLevels(targetType, Boolean.class);
        case LuaValue.TSTRING:
            return inheritanceLevels(targetType, String.class);
        case LuaValue.TUSERDATA:
            return inheritanceLevels(targetType, value.touserdata().getClass());
        case LuaValue.TNIL:
            return SCORE_NULL_VALUE;
        default:
            return inheritanceLevels(targetType, value.getClass());
    }
}
 
Example 6
Source File: CoerceLuaToJava.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
public int score(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TNUMBER:
		return inheritanceLevels( targetType, value.isint()? Integer.class: Double.class );
	case LuaValue.TBOOLEAN:
		return inheritanceLevels( targetType, Boolean.class );
	case LuaValue.TSTRING:
		return inheritanceLevels( targetType, String.class );
	case LuaValue.TUSERDATA:
		return inheritanceLevels( targetType, value.touserdata().getClass() );
	case LuaValue.TNIL:
		return SCORE_NULL_VALUE;
	default:
		return inheritanceLevels( targetType, value.getClass() );
	}
}
 
Example 7
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 8
Source File: DebugLib.java    From luaj with MIT License 5 votes vote down vote up
public LuaValue call(LuaValue value, LuaValue table) {
	LuaValue mt = table.opttable(null);
	switch ( value.type() ) {
		case TNIL:      LuaNil.s_metatable      = mt; break;
		case TNUMBER:   LuaNumber.s_metatable   = mt; break;
		case TBOOLEAN:  LuaBoolean.s_metatable  = mt; break;
		case TSTRING:   LuaString.s_metatable   = mt; break;
		case TFUNCTION: LuaFunction.s_metatable = mt; break;
		case TTHREAD:   LuaThread.s_metatable   = mt; break;
		default: value.setmetatable( mt );
	}
	return value;
}
 
Example 9
Source File: CoerceLuaToJava.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public int score(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TTABLE:
		return value.length()==0? 0: componentCoercion.score( value.get(1) );
	case LuaValue.TUSERDATA:
		return inheritanceLevels( componentType, value.touserdata().getClass().getComponentType() );
	case LuaValue.TNIL:
		return SCORE_NULL_VALUE;
	default: 
		return SCORE_UNCOERCIBLE;
	}
}
 
Example 10
Source File: CoerceLuaToJava.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public int score(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TTABLE:
		return value.length()==0? 0: componentCoercion.score( value.get(1) );
	case LuaValue.TUSERDATA:
		return inheritanceLevels( componentType, value.touserdata().getClass().getComponentType() );
	case LuaValue.TNIL:
		return SCORE_NULL_VALUE;
	default: 
		return SCORE_UNCOERCIBLE;
	}
}
 
Example 11
Source File: StringLib.java    From luaj with MIT License 5 votes vote down vote up
public void add_value( Buffer lbuf, int soffset, int end, LuaValue repl ) {
	switch ( repl.type() ) {
	case LuaValue.TSTRING:
	case LuaValue.TNUMBER:
		add_s( lbuf, repl.strvalue(), soffset, end );
		return;
		
	case LuaValue.TFUNCTION:
		repl = repl.invoke( push_captures( true, soffset, end ) ).arg1();
		break;
		
	case LuaValue.TTABLE:
		// Need to call push_onecapture here for the error checking
		repl = repl.get( push_onecapture( 0, soffset, end ) );
		break;
		
	default:
		error( "bad argument: string/function/table expected" );
		return;
	}
	
	if ( !repl.toboolean() ) {
		repl = s.substring( soffset, end );
	} else if ( ! repl.isstring() ) {
		error( "invalid replacement value (a "+repl.typename()+")" );
	}
	lbuf.append( repl.strvalue() );
}
 
Example 12
Source File: CoerceLuaToJava.java    From luaj with MIT License 5 votes vote down vote up
public int score(LuaValue value) {
	switch ( value.type() ) {
	case LuaValue.TTABLE:
		return value.length()==0? 0: componentCoercion.score( value.get(1) );
	case LuaValue.TUSERDATA:
		return inheritanceLevels( componentType, value.touserdata().getClass().getComponentType() );
	case LuaValue.TNIL:
		return SCORE_NULL_VALUE;
	default: 
		return SCORE_UNCOERCIBLE;
	}
}
 
Example 13
Source File: LuaPrint.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
void buildValue(StringBuffer ps, LuaValue v) {
    switch (v.type()) {
        case LuaValue.TSTRING:
            buildString(ps, (LuaString) v);
            break;
        default:
            ps.append(v.tojstring());

    }
}
 
Example 14
Source File: DumpState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
void dumpConstants( Prototype f) throws IOException {
	 LuaValue[] k = f.k;
	int i, n = k.length;
	dumpInt(n);
	for (i = 0; i < n; i++) {
		 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 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);
			}
			break;
		case LuaValue.TSTRING:
			writer.write(LuaValue.TSTRING);
			dumpString((LuaString)o);
			break;
		default:
			throw new IllegalArgumentException("bad type for " + o);			
		}
	}
	n = f.p.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpFunction(f.p[i]);
}
 
Example 15
Source File: DumpState.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
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);
			}
			break;
		case LuaValue.TSTRING:
			writer.write(LuaValue.TSTRING);
			dumpString((LuaString)o);
			break;
		default:
			throw new IllegalArgumentException("bad type for " + o);			
		}
	}
	n = f.p.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpFunction(f.p[i]);
}
 
Example 16
Source File: DumpState.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
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);
			}
			break;
		case LuaValue.TSTRING:
			writer.write(LuaValue.TSTRING);
			dumpString((LuaString)o);
			break;
		default:
			throw new IllegalArgumentException("bad type for " + o);			
		}
	}
	n = f.p.length;
	dumpInt(n);
	for (i = 0; i < n; i++)
		dumpFunction(f.p[i]);
}
 
Example 17
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 2 votes vote down vote up
/**
 * is string
 *
 * @param target
 * @return
 */
public static boolean isString(LuaValue target) {
    return target != null && target.type() == LuaValue.TSTRING;
}
 
Example 18
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 2 votes vote down vote up
/**
 * is number
 *
 * @param target
 * @return
 */
public static boolean isNumber(LuaValue target) {
    return target != null && target.type() == LuaValue.TNUMBER;
}
 
Example 19
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 2 votes vote down vote up
/**
 * is function
 *
 * @param target
 * @return
 */
public static boolean isFunction(LuaValue target) {
    return target != null && target.type() == LuaValue.TFUNCTION;
}
 
Example 20
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 2 votes vote down vote up
/**
 * is table
 *
 * @param target
 * @return
 */
public static boolean isTable(LuaValue target) {
    return target != null && target.type() == LuaValue.TTABLE;
}