Java Code Examples for org.luaj.vm2.lib.jse.CoerceJavaToLua#coerce()

The following examples show how to use org.luaj.vm2.lib.jse.CoerceJavaToLua#coerce() . 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: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public static Varargs callFunction(LuaValue target, Object... objs) {
    if (target != null && target.isfunction()) {
        LuaValue[] args = null;
        if (objs != null && objs.length > 0) {
            args = new LuaValue[objs.length];

            for (int i = 0; i < objs.length; i++) {
                args[i] = CoerceJavaToLua.coerce(objs[i]);
            }
        }
        if (args != null) {
            return target.invoke(LuaValue.varargsOf(args));
        } else {
            return target.call();
        }
    }
    return LuaValue.NIL;
}
 
Example 2
Source File: UDView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * get native view
 *
 * @return
 */
public LuaValue getNativeView() {
    View view = getView();
    if (view instanceof ILVNativeViewProvider) {
        view = ((ILVNativeViewProvider) view).getNativeView();
    }
    return view != null ? CoerceJavaToLua.coerce(view) : LuaValue.NIL;
}
 
Example 3
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * convert object to LuaValue
 *
 * @param value
 * @return
 */
public static LuaValue toLuaValue(Object value) {
    try {
        if (value instanceof Integer) {
            return LuaValue.valueOf((Integer) value);
        } else if (value instanceof Long) {
            return LuaValue.valueOf((Long) value);
        } else if (value instanceof Double) {
            return LuaValue.valueOf((Double) value);
        } else if (value instanceof String) {
            return LuaValue.valueOf((String) value);
        } else if (value instanceof Boolean) {
            return LuaValue.valueOf((Boolean) value);
        } else if (value instanceof byte[]) {
            return LuaValue.valueOf((byte[]) value);
        } else if (value instanceof List) {
            return toTable((List) value);
        } else if (value instanceof Map) {
            return toTable((Map) value);
        } else {
            return CoerceJavaToLua.coerce(value);
        }
    } catch (Exception e) {

        return LuaValue.NIL;
    }
}
 
Example 4
Source File: LuajActivity.java    From luaj with MIT License 5 votes vote down vote up
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	LuajView view = new LuajView(this);
	setContentView(view);
	try {
		LuaValue activity = CoerceJavaToLua.coerce(this);
		LuaValue viewobj = CoerceJavaToLua.coerce(view);
		view.globals.loadfile("activity.lua").call(activity, viewobj);
	} catch ( Exception e ) {
		e.printStackTrace();
	}
}
 
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: LuaScript.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public <T> T runOptional(String method, T defaultValue, Object... args) {
    try {
        if (!getScript().get(method).isfunction()) {
            return defaultValue;
        }

        int startIndex = 1;

        if(parent==null) {
            startIndex = 0;
        }

        LuaValue []luaArgs = new LuaValue[args.length+startIndex];

        if(parent!=null) {
            luaArgs[0] = CoerceJavaToLua.coerce(parent);
        }


        for (int i = startIndex;i<luaArgs.length;++i) {
            luaArgs[i] = CoerceJavaToLua.coerce(args[i-startIndex]);
        }

        if(defaultValue==null) {
            run(method, luaArgs);
            return null;
        }

        return (T) CoerceLuaToJava.coerce(
                run(method, luaArgs),
                defaultValue.getClass());
    } catch (LuaError e) {
        throw new ModError("Error when call:" + method+"."+scriptFile,e);
    }
}
 
Example 7
Source File: LuaScriptEngine.java    From luaj with MIT License 4 votes vote down vote up
static private LuaValue toLua(Object javaValue) {
	return javaValue == null? LuaValue.NIL:
		javaValue instanceof LuaValue? (LuaValue) javaValue:
		CoerceJavaToLua.coerce(javaValue);
}
 
Example 8
Source File: LuaScript.java    From remixed-dungeon with GNU General Public License v3.0 4 votes vote down vote up
public LuaScript(String scriptFile, @Nullable Object parent)
{
    this.parent = parent;
    this.scriptFile = scriptFile;
    onlyParentArgs[0] = CoerceJavaToLua.coerce(parent);
}
 
Example 9
Source File: JsonStorage.java    From Lukkit with MIT License 4 votes vote down vote up
@Override
public LuaValue getValue(LuaString path) throws StorageObjectException {
    return CoerceJavaToLua.coerce(object.get(path.checkjstring()));
}
 
Example 10
Source File: YamlStorage.java    From Lukkit with MIT License 4 votes vote down vote up
@Override
public LuaValue getValue(LuaString path) throws StorageObjectException {
    return CoerceJavaToLua.coerce(this.yamlConfiguration.get(path.tojstring()));
}