Java Code Examples for org.luaj.vm2.LuaValue#TUSERDATA

The following examples show how to use org.luaj.vm2.LuaValue#TUSERDATA . 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 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 2
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 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.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 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 5
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 6
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.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 7
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 8
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.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 9
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * parse a value to given type
 *
 * @param type
 * @param value
 * @return
 */
static Object parseValue(int type, LuaValue value) {
    switch (type) {
        case LuaValue.TBOOLEAN:
            if (isBoolean(value)) return value.checkboolean();
            break;
        case LuaValue.TNUMBER:
            if (isNumber(value)) return value.checknumber();
            break;
        case LuaValue.TSTRING:
            if (isString(value)) return value.checkjstring();
            break;
        case LuaValue.TTABLE:
            if (isTable(value)) return value.checktable();
            break;
        case LuaValue.TFUNCTION:
            if (isFunction(value)) return value.checkfunction();
            break;
        case LuaValue.TUSERDATA:
            if (isUserdata(value)) return value.checkuserdata();
            break;
        case LuaValue.TVALUE:
            return value;
    }
    return null;
}
 
Example 10
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 11
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 12
Source File: CoerceLuaToJava.java    From luaj with MIT License 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 13
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 14
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 15
Source File: CoerceLuaToJava.java    From VideoOS-Android-SDK 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 16
Source File: IoLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public int type() {
	return LuaValue.TUSERDATA;
}
 
Example 17
Source File: LuaPrint.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Print the state of a {@link LuaClosure} that is being executed
 *
 * @param cl      the {@link LuaClosure}
 * @param pc      the program counter
 * @param stack   the stack of {@link LuaValue}
 * @param top     the top of the stack
 * @param varargs any {@link Varargs} value that may apply
 */
public LuaPrint buildState(LuaClosure cl, int pc, LuaValue[] stack, int top, Varargs varargs) {
    // print opcode into buffer
    StringBuffer previous = ps;
    ps = new StringBuffer();
    buildOpCode(cl.p, pc);

    VenvyLog.i(ps + "");

    ps = previous;
    format(ps.toString(), 50);

    // print stack
    ps.append('[');
    for (int i = 0; i < stack.length; i++) {
        LuaValue v = stack[i];
        if (v == null)
            ps.append(STRING_FOR_NULL);
        else switch (v.type()) {
            case LuaValue.TSTRING:
                LuaString s = v.checkstring();
                ps.append(s.length() < 48 ?
                        s.tojstring() :
                        s.substring(0, 32).tojstring() + "...+" + (s.length() - 32) + "b");
                break;
            case LuaValue.TFUNCTION:
                ps.append(v.tojstring());
                break;
            case LuaValue.TUSERDATA:
                Object o = v.touserdata();
                if (o != null) {
                    String n = o.getClass().getName();
                    n = n.substring(n.lastIndexOf('.') + 1);
                    ps.append(n + ": " + Integer.toHexString(o.hashCode()));
                } else {
                    ps.append(v.toString());
                }
                break;
            default:
                ps.append(v.tojstring());
        }
        if (i + 1 == top)
            ps.append(']');
        ps.append(" | ");
    }
    ps.append(varargs);
    ps.append("\n");
    return this;
}
 
Example 18
Source File: IoLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public int type() {
	return LuaValue.TUSERDATA;
}
 
Example 19
Source File: IoLib.java    From luaj with MIT License 4 votes vote down vote up
public int type() {
	return LuaValue.TUSERDATA;
}
 
Example 20
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 2 votes vote down vote up
/**
 * is userdata
 *
 * @param target
 * @return
 */
public static boolean isUserdata(LuaValue target) {
    return target != null && target.type() == LuaValue.TUSERDATA;
}