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

The following examples show how to use org.luaj.vm2.LuaValue#set() . 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 HtmlNative with Apache License 2.0 6 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	globals.debuglib = this;
	LuaTable debug = new LuaTable();
	debug.set("debug", new debug());
	debug.set("gethook", new gethook());
	debug.set("getinfo", new getinfo());
	debug.set("getlocal", new getlocal());
	debug.set("getmetatable", new getmetatable());
	debug.set("getregistry", new getregistry());
	debug.set("getupvalue", new getupvalue());
	debug.set("getuservalue", new getuservalue());
	debug.set("sethook", new sethook());
	debug.set("setlocal", new setlocal());
	debug.set("setmetatable", new setmetatable());
	debug.set("setupvalue", new setupvalue());
	debug.set("setuservalue", new setuservalue());
	debug.set("traceback", new traceback());
	debug.set("upvalueid", new upvalueid());
	debug.set("upvaluejoin", new upvaluejoin());
	env.set("debug", debug);
	env.get("package").get("loaded").set("debug", debug);
	return debug;
}
 
Example 2
Source File: JseMathLib.java    From luaj with MIT License 6 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * <P>Specifically, adds all library functions that can be implemented directly
 * in JSE but not JME: acos, asin, atan, atan2, cosh, exp, log, pow, sinh, and tanh.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	super.call(modname, env);
	LuaValue math = env.get("math");
	math.set("acos", new acos());
	math.set("asin", new asin());
	LuaValue atan =  new atan2();
	math.set("atan", atan);
	math.set("atan2", atan);
	math.set("cosh", new cosh());
	math.set("exp", new exp());
	math.set("log", new log());
	math.set("pow", new pow());
	math.set("sinh", new sinh());
	math.set("tanh", new tanh());
	return math;
}
 
Example 3
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * Creates a metatable that uses __INDEX to fall back on itself to support string
 * method operations.
 * If the shared strings metatable instance is null, will set the metatable as
 * the global shared metatable for strings.
 * <P>
 * All tables and metatables are read-write by default so if this will be used in 
 * a server environment, sandboxing should be used.  In particular, the 
 * {@link LuaString#s_metatable} table should probably be made read-only.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, typically a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	LuaTable string = new LuaTable();
	string.set("byte", new byte_());
	string.set("char", new char_());
	string.set("dump", new dump());
	string.set("find", new find());
	string.set("format", new format());
	string.set("gmatch", new gmatch());
	string.set("gsub", new gsub());
	string.set("len", new len());
	string.set("lower", new lower());
	string.set("match", new match());
	string.set("rep", new rep());
	string.set("reverse", new reverse());
	string.set("sub", new sub());
	string.set("upper", new upper());
	LuaTable mt = LuaValue.tableOf(
			new LuaValue[] { INDEX, string });
	env.set("string", string);
	env.get("package").get("loaded").set("string", string);
	if (LuaString.s_metatable == null)
		LuaString.s_metatable = mt;
	return string;
}
 
Example 4
Source File: PackageLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public LuaValue call(LuaValue modname, LuaValue env) {
    globals = env.checkglobals();
    globals.set("require", new require());
    package_ = new LuaTable();
    package_.set(_LOADED, new LuaTable());
    package_.set(_PRELOAD, new LuaTable());
    package_.set(_PATH, LuaValue.valueOf(DEFAULT_LUA_PATH));
    package_.set(_LOADLIB, new loadlib());
    package_.set(_SEARCHPATH, new searchpath());
    LuaTable searchers = new LuaTable();
    searchers.set(1, preload_searcher = new preload_searcher());
    searchers.set(2, lua_searcher = new lua_searcher());
    searchers.set(3, java_searcher = new java_searcher());
    package_.set(_SEARCHERS, searchers);
    package_.get(_LOADED).set("package", package_);
    env.set("package", package_);
    globals.package_ = this;
    return env;
}
 
Example 5
Source File: OsLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, typically a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	LuaTable os = new LuaTable();
	for (int i = 0; i < NAMES.length; ++i)
		os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
	env.set("os", os);
	env.get("package").get("loaded").set("os", os);
	return os;
}
 
Example 6
Source File: CoroutineLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	LuaTable coroutine = new LuaTable();
	coroutine.set("create", new create());
	coroutine.set("resume", new resume());
	coroutine.set("running", new running());
	coroutine.set("status", new status());
	coroutine.set("yield", new yield());
	coroutine.set("wrap", new wrap());
	env.set("coroutine", coroutine);
	env.get("package").get("loaded").set("coroutine", coroutine);
	return coroutine;
}
 
Example 7
Source File: PackageLib.java    From luaj with MIT License 5 votes vote down vote up
public LuaValue call( LuaValue arg ) {
	LuaString name = arg.checkstring();
	LuaValue loaded = package_.get(_LOADED);
	LuaValue result = loaded.get(name);
	if ( result.toboolean() ) {
		if ( result == _SENTINEL )
			error("loop or previous error loading module '"+name+"'");
		return result;
	}
	
	/* else must load it; iterate over available loaders */
	LuaTable tbl = package_.get(_SEARCHERS).checktable();
	StringBuffer sb = new StringBuffer();
	Varargs loader = null;
	for ( int i=1; true; i++ ) {
		LuaValue searcher = tbl.get(i);
		if ( searcher.isnil() ) {
			error( "module '"+name+"' not found: "+name+sb );
	    }
					
	    /* call loader with module name as argument */
		loader = searcher.invoke(name);
		if ( loader.isfunction(1) )
			break;
		if ( loader.isstring(1) )
			sb.append( loader.tojstring(1) );
	}
	
	// load the module using the loader
	loaded.set(name, _SENTINEL);
	result = loader.arg1().call(name, loader.arg(2));
	if ( ! result.isnil() )
		loaded.set( name, result );
	else if ( (result = loaded.get(name)) == _SENTINEL )
		loaded.set( name, result = LuaValue.TRUE );
	return result;
}
 
Example 8
Source File: Bit32Lib.java    From luaj with MIT License 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	LuaTable t = new LuaTable();
	bind(t, Bit32LibV.class, new String[] {
		"band", "bnot", "bor", "btest", "bxor", "extract", "replace"
	});
	bind(t, Bit32Lib2.class, new String[] {
		"arshift", "lrotate", "lshift", "rrotate", "rshift"
	});
	env.set("bit32", t);
	if (!env.get("package").isnil()) env.get("package").get("loaded").set("bit32", t);
	return t;
}
 
Example 9
Source File: TableLib.java    From luaj with MIT License 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, typically a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	LuaTable table = new LuaTable();
	table.set("concat", new concat());
	table.set("insert", new insert());
	table.set("pack", new pack());
	table.set("remove", new remove());
	table.set("sort", new sort());
	table.set("unpack", new unpack());
	env.set("table", table);
	if (!env.get("package").isnil()) env.get("package").get("loaded").set("table", table);
	return NIL;
}
 
Example 10
Source File: BaseLib.java    From luaj with MIT License 5 votes vote down vote up
/** Perform one-time initialization on the library by adding base functions
 * to the supplied environment, and returning it as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	globals.finder = this;
	globals.baselib = this;
	env.set( "_G", env );
	env.set( "_VERSION", Lua._VERSION );
	env.set("assert", new _assert());
	env.set("collectgarbage", new collectgarbage());
	env.set("dofile", new dofile());
	env.set("error", new error());
	env.set("getmetatable", new getmetatable());
	env.set("load", new load());
	env.set("loadfile", new loadfile());
	env.set("pcall", new pcall());
	env.set("print", new print(this));
	env.set("rawequal", new rawequal());
	env.set("rawget", new rawget());
	env.set("rawlen", new rawlen());
	env.set("rawset", new rawset());
	env.set("select", new select());
	env.set("setmetatable", new setmetatable());
	env.set("tonumber", new tonumber());
	env.set("tostring", new tostring());
	env.set("type", new type());
	env.set("xpcall", new xpcall());

	next next;
	env.set("next", next = new next());
	env.set("pairs", new pairs(next));
	env.set("ipairs", new ipairs());
	
	return env;
}
 
Example 11
Source File: OsLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	LuaTable os = new LuaTable();
	for (int i = 0; i < NAMES.length; ++i)
		os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
	env.set("os", os);
	env.get("package").get("loaded").set("os", os);
	return os;
}
 
Example 12
Source File: Bit32Lib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	LuaTable t = new LuaTable();
	bind(t, Bit32LibV.class, new String[] {
		"band", "bnot", "bor", "btest", "bxor", "extract", "replace"
	});
	bind(t, Bit32Lib2.class, new String[] {
		"arshift", "lrotate", "lshift", "rrotate", "rshift"
	});
	env.set("bit32", t);
	env.get("package").get("loaded").set("bit32", t);
	return t;
}
 
Example 13
Source File: BaseLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue call(LuaValue modname, LuaValue env) {
        globals = env.checkglobals();
//        globals.finder = this;// finder 由Globals自行设置
        globals.baselib = this;
        env.set("_G", env);
        env.set("_VERSION", Lua._VERSION);
        env.set("assert", new _assert());
        env.set("collectgarbage", new collectgarbage());
//        env.set("dofile", new dofile());//TODO 裁剪掉for LuaView
        env.set("error", new error());
        env.set("getmetatable", new getmetatable());
        env.set("load", new load());
        env.set("loadfile", new loadfile());
        env.set("pcall", new pcall());
        env.set("print", new print(this));
        env.set("rawequal", new rawequal());
        env.set("rawget", new rawget());
        env.set("rawlen", new rawlen());
        env.set("rawset", new rawset());
        env.set("select", new select());
        env.set("setmetatable", new setmetatable());
        env.set("tonumber", new tonumber());
        env.set("tostring", new tostring());
        env.set("type", new type());
        env.set("xpcall", new xpcall());

        //extend for luaview
        new com.taobao.luaview.vm.extend.BaseLib(this, globals).extend(env);

        next next;
        env.set("next", next = new next());
        env.set("pairs", new pairs(next));
        env.set("ipairs", new ipairs());

        return env;
    }
 
Example 14
Source File: LibFunction.java    From luaj with MIT License 5 votes vote down vote up
/**
 * Bind a set of library functions, with an offset
 * <p>
 * An array of names is provided, and the first name is bound
 * with opcode = {@code firstopcode}, second with {@code firstopcode+1}, etc.
 * @param env The environment to apply to each bound function
 * @param factory the Class to instantiate for each bound function
 * @param names array of String names, one for each function.
 * @param firstopcode the first opcode to use
 * @see #bind(LuaValue, Class, String[])
 */
protected void bind(LuaValue env, Class factory,  String[] names, int firstopcode ) {
	try {
		for ( int i=0, n=names.length; i<n; i++ ) {
			LibFunction f = (LibFunction) factory.newInstance();
			f.opcode = firstopcode + i;
			f.name = names[i];
			env.set(f.name, f);
		}
	} catch ( Exception e ) {
		throw new LuaError( "bind failed: "+e );
	}
}
 
Example 15
Source File: LibFunction.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Bind a set of library functions, with an offset
 * <p/>
 * An array of names is provided, and the first name is bound
 * with opcode = {@code firstopcode}, second with {@code firstopcode+1}, etc.
 *
 * @param env         The environment to apply to each bound function
 * @param factory     the Class to instantiate for each bound function
 * @param names       array of String names, one for each function.
 * @param firstopcode the first opcode to use
 * @see #bind(LuaValue, Class, String[])
 */
protected void bind(LuaValue env, Class factory, String[] names, int firstopcode) {
    try {
        for (int i = 0, n = names.length; i < n; i++) {
            LibFunction f = (LibFunction) factory.newInstance();
            f.opcode = firstopcode + i;
            f.name = names[i];
            env.set(f.name, f);
        }
    } catch (Exception e) {
        throw new LuaError("bind failed: " + e);
    }
}
 
Example 16
Source File: MathLib.java    From luaj with MIT License 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, typically a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	LuaTable math = new LuaTable(0,30);
	math.set("abs", new abs());
	math.set("ceil", new ceil());
	math.set("cos", new cos());
	math.set("deg", new deg());
	math.set("exp", new exp(this));
	math.set("floor", new floor());
	math.set("fmod", new fmod());
	math.set("frexp", new frexp());
	math.set("huge", LuaDouble.POSINF );
	math.set("ldexp", new ldexp());
	math.set("max", new max());
	math.set("min", new min());
	math.set("modf", new modf());
	math.set("pi", Math.PI );
	math.set("pow", new pow());
	random r;
	math.set("random", r = new random());
	math.set("randomseed", new randomseed(r));
	math.set("rad", new rad());
	math.set("sin", new sin());
	math.set("sqrt", new sqrt());
	math.set("tan", new tan());
	env.set("math", math);
	if (!env.get("package").isnil()) env.get("package").get("loaded").set("math", math);
	return math;
}
 
Example 17
Source File: OsLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, typically a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	LuaTable os = new LuaTable();
	for (int i = 0; i < NAMES.length; ++i)
		os.set(NAMES[i], new OsLibFunc(i, NAMES[i]));
	env.set("os", os);
	env.get("package").get("loaded").set("os", os);
	return os;
}
 
Example 18
Source File: CoroutineLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
/** Perform one-time initialization on the library by creating a table
 * containing the library functions, adding that table to the supplied environment,
 * adding the table to package.loaded, and returning table as the return value.
 * @param modname the module name supplied if this is loaded via 'require'.
 * @param env the environment to load into, which must be a Globals instance.
 */
public LuaValue call(LuaValue modname, LuaValue env) {
	globals = env.checkglobals();
	LuaTable coroutine = new LuaTable();
	coroutine.set("create", new create());
	coroutine.set("resume", new resume());
	coroutine.set("running", new running());
	coroutine.set("status", new status());
	coroutine.set("yield", new yield());
	coroutine.set("wrap", new wrap());
	env.set("coroutine", coroutine);
	env.get("package").get("loaded").set("coroutine", coroutine);
	return coroutine;
}
 
Example 19
Source File: TableLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue t = tableOf(args, 1);
	t.set("n", args.narg());
	return t;
}
 
Example 20
Source File: TableLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue t = tableOf(args, 1);
	t.set("n", args.narg());
	return t;
}