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

The following examples show how to use org.luaj.vm2.LuaValue#touserdata() . 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.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 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.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 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 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.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: 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 6
Source File: LuajavaClassMembersTest.java    From luaj with MIT License 5 votes vote down vote up
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 );
}
 
Example 7
Source File: LuajavaClassMembersTest.java    From luaj with MIT License 5 votes vote down vote up
public void testUniqueFactoryUncoercible() {
	JavaClass f = JavaClass.forClass(B.class);
	LuaValue constr = f.get("new");
	assertEquals( JavaConstructor.class, constr.getClass() );
	try { 
		LuaValue v = constr.call(LuaValue.userdataOf(new Object()));
		Object b = v.touserdata();
		// fail( "did not throw lua error as expected" );
		assertEquals( 0, ((B)b).m_int_field );
	} catch ( LuaError e ) {
	}
}
 
Example 8
Source File: LuaJavaCoercionTest.java    From luaj with MIT License 5 votes vote down vote up
public void testVarArgsProxy() {		
	String script = "return luajava.createProxy( \""+VarArgsInterface.class.getName()+"\", \n"+
		"{\n" +
		"	varargsMethod = function(a,...)\n" +
		"		return table.concat({a,...},'-')\n" +
		"	end,\n" +
		"	arrayargsMethod = function(a,array)\n" +
		"		return tostring(a)..(array and \n" +
		"			('-'..tostring(array.length)\n" +
		"			..'-'..tostring(array[1])\n" +
		"			..'-'..tostring(array[2])\n" +
		"			) or '-nil')\n" +
		"	end,\n" +
		"} )\n";
	Varargs chunk = globals.get("load").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]) );
	assertEquals( "foo-1-bar-nil", v.arrayargsMethod("foo", new String[] {"bar"}) );
	assertEquals( "foo-2-bar-etc", v.arrayargsMethod("foo", new String[] {"bar","etc"}) );
	assertEquals( "foo-3-bar-etc", v.arrayargsMethod("foo", new String[] {"bar","etc","etc"}) );
	assertEquals( "foo-nil", v.arrayargsMethod("foo", null) );
}
 
Example 9
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;
}