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

The following examples show how to use org.luaj.vm2.LuaValue#isfunction() . 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: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public static Varargs invokeFunction(LuaValue target, LuaValue arg1) {
    try {
        return (target != null && target.isfunction()) ? target.invoke(arg1) : LuaValue.NIL;
    } catch (Exception e) {
        return LuaValue.NIL;
    }
}
 
Example 3
Source File: XLua.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void rawset(LuaValue key, LuaValue value) {
    if (value.isfunction())
        super.rawset(key, value);
    else
        error("Globals not allowed: " + key + "=" + value);
}
 
Example 4
Source File: LuaSkinLoader.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
private static <T> T serializeLuaScript(LuaValue lv, Function<LuaFunction, T> asFunction, Function<String, T> asScript, Function<Integer, T> byId) {
	if (lv.isfunction()) {
		return asFunction.apply(lv.checkfunction());
	} else if (lv.isnumber() && byId != null) {
		return byId.apply(lv.toint());
	} else if (lv.isstring()) {
		return asScript.apply(lv.tojstring());
	} else {
		return null;
	}
}
 
Example 5
Source File: XLua.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void set(int key, LuaValue value) {
    if (value.isfunction())
        super.set(key, value);
    else
        error("Globals not allowed: set " + value);
}
 
Example 6
Source File: SampleApplet.java    From luaj with MIT License 5 votes vote down vote up
public void paint(Graphics g) {
	LuaValue p = globals.get("paint");
	if (!p.isfunction())
		super.paint(g);
	else
		pcall.call(
				p,
				coerced == g ? graphics : (graphics = CoerceJavaToLua
						.coerce(coerced = g)));
}
 
Example 7
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public static LuaValue getOrCallFunction(LuaValue target, LuaValue arg1, LuaValue arg2) {
    try {
        return (target != null && target.isfunction()) ? target.call(arg1, arg2) : target;
    } catch (Exception e) {

        return LuaValue.NIL;
    }
}
 
Example 8
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 调用方法或者直接取数据
 *
 * @param target
 * @return
 */
public static LuaValue getOrCallFunction(LuaValue target) {
    try {
        return (target != null && target.isfunction()) ? target.call() : target;
    } catch (Exception e) {

        return LuaValue.NIL;
    }
}
 
Example 9
Source File: BaseLib.java    From luaj with MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue ld = args.arg1();
	if (!ld.isstring() && !ld.isfunction()) {
		throw new LuaError("bad argument #1 to 'load' (string or function expected, got " + ld.typename() + ")");
	}
	String source = args.optjstring(2, ld.isstring()? ld.tojstring(): "=(load)");
	String mode = args.optjstring(3, "bt");
	LuaValue env = args.optvalue(4, globals);
	return loadStream(ld.isstring()? ld.strvalue().toInputStream():
		new StringInputStream(ld.checkfunction()), source, mode, env);
}
 
Example 10
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public static Varargs invokeFunction(LuaValue target, LuaValue arg1, LuaValue arg2) {
    try {
        return (target != null && target.isfunction()) ? target.invoke(arg1, arg2) : LuaValue.NIL;
    } catch (Exception e) {
        return LuaValue.NIL;
    }
}
 
Example 11
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * call lua function ( return multiple values)
 *
 * @param target
 * @return
 */
public static Varargs invokeFunction(LuaValue target) {
    try {
        return (target != null && target.isfunction()) ? target.invoke() : LuaValue.NIL;
    } catch (Exception e) {

        return LuaValue.NIL;
    }
}
 
Example 12
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public static LuaValue callFunction(LuaValue target, LuaValue arg1, LuaValue arg2) {
    try {
        return (target != null && target.isfunction()) ? target.call(arg1, arg2) : LuaValue.NIL;
    } catch (Exception e) {
        e.printStackTrace();
        return LuaValue.NIL;
    }
}
 
Example 13
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public static LuaValue callFunction(LuaValue target, LuaValue arg1) {
    try {
        return (target != null && target.isfunction()) ? target.call(arg1) : LuaValue.NIL;
    } catch (Exception e) {
        e.printStackTrace();
        return LuaValue.NIL;
    }
}
 
Example 14
Source File: NewIndexFunction.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Varargs invoke(Varargs args) {
    LuaValue func = metatable.get(args.arg(2));
    if (func.isfunction()) {//函数调用
        func.invoke(varargsOf(args.arg(1), args.arg(3)));
    }
    return NONE;
}
 
Example 15
Source File: UIViewMethodMapper.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue mqttCallback(U view, Varargs varargs) {
    if (varargs.narg() > 1) {
        final LuaValue callbacks = varargs.optvalue(2, NIL);
        if (callbacks.isfunction()) {
            return view.setMqttCallback(callbacks);
        }
        return LuaValue.NIL;
    } else {
        return view.getMqttCallback();
    }
}
 
Example 16
Source File: UDBaseListOrRecyclerView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 调用 Callback 方法
 *
 * @param cell
 * @param position
 * @return
 */
public LuaValue onCellClicked(LuaValue cell, int position) {
    int section = getSectionByPosition(position);
    int row = getRowInSectionByPosition(position);
    LuaValue cellData = getCell(getId(position, section, row));
    if (cellData != null) {
         LuaValue callback = cellData.get("Callback");
        if (callback.isfunction()) {
            return LuaUtil.callFunction(callback, cell, LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row));
        } else if (callback.istable()) {
            return LuaUtil.callFunction(LuaUtil.getFunction(callback, "Click", "click"), cell, LuaUtil.toLuaInt(section), LuaUtil.toLuaInt(row));
        }
    }
    return NIL;
}
 
Example 17
Source File: DebugLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public Varargs invoke(Varargs args) {
    int a = 1;
    LuaThread thread = args.isthread(a) ? args.checkthread(a++) : globals.running;
    LuaValue func = args.arg(a++);
    String what = args.optjstring(a++, "flnStu");
    DebugLib.CallStack callstack = callstack(thread);

    // find the stack info
    DebugLib.CallFrame frame;
    if (func.isnumber()) {
        frame = callstack.getCallFrame(func.toint());
        if (frame == null)
            return NONE;
        func = frame.f;
    } else if (func.isfunction()) {
        frame = callstack.findCallFrame(func);
    } else {
        return argerror(a - 2, "function or level");
    }

    // start a table
    DebugInfo ar = callstack.auxgetinfo(what, (LuaFunction) func, frame);
    LuaTable info = new LuaTable();
    if (what.indexOf('S') >= 0) {
        info.set(WHAT, LUA);
        info.set(SOURCE, valueOf(ar.source));
        info.set(SHORT_SRC, valueOf(ar.short_src));
        info.set(LINEDEFINED, valueOf(ar.linedefined));
        info.set(LASTLINEDEFINED, valueOf(ar.lastlinedefined));
    }
    if (what.indexOf('l') >= 0) {
        info.set(CURRENTLINE, valueOf(ar.currentline));
    }
    if (what.indexOf('u') >= 0) {
        info.set(NUPS, valueOf(ar.nups));
        info.set(NPARAMS, valueOf(ar.nparams));
        info.set(ISVARARG, ar.isvararg ? ONE : ZERO);
    }
    if (what.indexOf('n') >= 0) {
        info.set(NAME, LuaValue.valueOf(ar.name != null ? ar.name : "?"));
        info.set(NAMEWHAT, LuaValue.valueOf(ar.namewhat));
    }
    if (what.indexOf('t') >= 0) {
        info.set(ISTAILCALL, ZERO);
    }
    if (what.indexOf('L') >= 0) {
        LuaTable lines = new LuaTable();
        info.set(ACTIVELINES, lines);
        DebugLib.CallFrame cf;
        for (int l = 1; (cf = callstack.getCallFrame(l)) != null; ++l)
            if (cf.f == func)
                lines.insert(-1, valueOf(cf.currentline()));
    }
    if (what.indexOf('f') >= 0) {
        if (func != null)
            info.set(FUNC, func);
    }
    return info;
}
 
Example 18
Source File: DebugLib.java    From luaj with MIT License 4 votes vote down vote up
public Varargs invoke(Varargs args) {
	int a=1;
	LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running;
	LuaValue func = args.arg(a++);
	String what = args.optjstring(a++, "flnStu");
	DebugLib.CallStack callstack = callstack(thread);

	// find the stack info
	DebugLib.CallFrame frame;
	if ( func.isnumber() ) {
		frame = callstack.getCallFrame(func.toint());
		if (frame == null)
			return NONE;
		func = frame.f;
	} else if ( func.isfunction() ) {
		frame = callstack.findCallFrame(func);
	} else {
		return argerror(a-2, "function or level");
	}

	// start a table
	DebugInfo ar = callstack.auxgetinfo(what, (LuaFunction) func, frame);
	LuaTable info = new LuaTable();
	if (what.indexOf('S') >= 0) {
		info.set(WHAT, LUA);
		info.set(SOURCE, valueOf(ar.source));
		info.set(SHORT_SRC, valueOf(ar.short_src));
		info.set(LINEDEFINED, valueOf(ar.linedefined));
		info.set(LASTLINEDEFINED, valueOf(ar.lastlinedefined));
	}
	if (what.indexOf('l') >= 0) {
		info.set( CURRENTLINE, valueOf(ar.currentline) );
	}
	if (what.indexOf('u') >= 0) {
		info.set(NUPS, valueOf(ar.nups));
		info.set(NPARAMS, valueOf(ar.nparams));
		info.set(ISVARARG, ar.isvararg? ONE: ZERO);
	}
	if (what.indexOf('n') >= 0) {
		info.set(NAME, LuaValue.valueOf(ar.name!=null? ar.name: "?"));
		info.set(NAMEWHAT, LuaValue.valueOf(ar.namewhat));
	}
	if (what.indexOf('t') >= 0) {
		info.set(ISTAILCALL, ZERO);
	}
	if (what.indexOf('L') >= 0) {
		LuaTable lines = new LuaTable();
		info.set(ACTIVELINES, lines);
		DebugLib.CallFrame cf;
		for (int l = 1; (cf=callstack.getCallFrame(l)) != null; ++l)
			if (cf.f == func)
				lines.insert(-1, valueOf(cf.currentline()));
	}
	if (what.indexOf('f') >= 0) {
		if (func != null)
			info.set( FUNC, func );
	}
	return info;
}
 
Example 19
Source File: DebugLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public Varargs invoke(Varargs args) {
	int a=1;
	LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running; 
	LuaValue func = args.arg(a++);
	String what = args.optjstring(a++, "flnStu");
	DebugLib.CallStack callstack = callstack(thread);

	// find the stack info
	DebugLib.CallFrame frame;
	if ( func.isnumber() ) {
		frame = callstack.getCallFrame(func.toint());
		if (frame == null)
			return NONE;
		func = frame.f;
	} else if ( func.isfunction() ) {
		frame = callstack.findCallFrame(func);
	} else {
		return argerror(a-2, "function or level");
	}

	// start a table
	DebugInfo ar = callstack.auxgetinfo(what, (LuaFunction) func, frame);
	LuaTable info = new LuaTable();
	if (what.indexOf('S') >= 0) {
		info.set(WHAT, LUA);
		info.set(SOURCE, valueOf(ar.source));
		info.set(SHORT_SRC, valueOf(ar.short_src));
		info.set(LINEDEFINED, valueOf(ar.linedefined));
		info.set(LASTLINEDEFINED, valueOf(ar.lastlinedefined));
	}
	if (what.indexOf('l') >= 0) {
		info.set( CURRENTLINE, valueOf(ar.currentline) );
	}
	if (what.indexOf('u') >= 0) {
		info.set(NUPS, valueOf(ar.nups));
		info.set(NPARAMS, valueOf(ar.nparams));
		info.set(ISVARARG, ar.isvararg? ONE: ZERO);
	}
	if (what.indexOf('n') >= 0) {
		info.set(NAME, LuaValue.valueOf(ar.name!=null? ar.name: "?"));
		info.set(NAMEWHAT, LuaValue.valueOf(ar.namewhat));
	}
	if (what.indexOf('t') >= 0) {
		info.set(ISTAILCALL, ZERO);
	}
	if (what.indexOf('L') >= 0) {
		LuaTable lines = new LuaTable();
		info.set(ACTIVELINES, lines);
		DebugLib.CallFrame cf;
		for (int l = 1; (cf=callstack.getCallFrame(l)) != null; ++l)
			if (cf.f == func)
				lines.insert(-1, valueOf(cf.currentline()));
	}
	if (what.indexOf('f') >= 0) {
		if (func != null)
			info.set( FUNC, func );
	}
	return info;
}
 
Example 20
Source File: DebugLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public Varargs invoke(Varargs args) {
	int a=1;
	LuaThread thread = args.isthread(a)? args.checkthread(a++): globals.running; 
	LuaValue func = args.arg(a++);
	String what = args.optjstring(a++, "flnStu");
	DebugLib.CallStack callstack = callstack(thread);

	// find the stack info
	DebugLib.CallFrame frame;
	if ( func.isnumber() ) {
		frame = callstack.getCallFrame(func.toint());
		if (frame == null)
			return NONE;
		func = frame.f;
	} else if ( func.isfunction() ) {
		frame = callstack.findCallFrame(func);
	} else {
		return argerror(a-2, "function or level");
	}

	// start a table
	DebugInfo ar = callstack.auxgetinfo(what, (LuaFunction) func, frame);
	LuaTable info = new LuaTable();
	if (what.indexOf('S') >= 0) {
		info.set(WHAT, LUA);
		info.set(SOURCE, valueOf(ar.source));
		info.set(SHORT_SRC, valueOf(ar.short_src));
		info.set(LINEDEFINED, valueOf(ar.linedefined));
		info.set(LASTLINEDEFINED, valueOf(ar.lastlinedefined));
	}
	if (what.indexOf('l') >= 0) {
		info.set( CURRENTLINE, valueOf(ar.currentline) );
	}
	if (what.indexOf('u') >= 0) {
		info.set(NUPS, valueOf(ar.nups));
		info.set(NPARAMS, valueOf(ar.nparams));
		info.set(ISVARARG, ar.isvararg? ONE: ZERO);
	}
	if (what.indexOf('n') >= 0) {
		info.set(NAME, LuaValue.valueOf(ar.name!=null? ar.name: "?"));
		info.set(NAMEWHAT, LuaValue.valueOf(ar.namewhat));
	}
	if (what.indexOf('t') >= 0) {
		info.set(ISTAILCALL, ZERO);
	}
	if (what.indexOf('L') >= 0) {
		LuaTable lines = new LuaTable();
		info.set(ACTIVELINES, lines);
		DebugLib.CallFrame cf;
		for (int l = 1; (cf=callstack.getCallFrame(l)) != null; ++l)
			if (cf.f == func)
				lines.insert(-1, valueOf(cf.currentline()));
	}
	if (what.indexOf('f') >= 0) {
		if (func != null)
			info.set( FUNC, func );
	}
	return info;
}