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

The following examples show how to use org.luaj.vm2.LuaTable#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: PackageLib.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
/** Perform one-time initialization on the library by adding package functions
 * to the supplied environment, and returning it as the return value.
 * It also creates the package.preload and package.loaded tables for use by
 * other libraries.
 * @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();
	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 2
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 3
Source File: PackageLib.java    From luaj with MIT License 6 votes vote down vote up
/** Perform one-time initialization on the library by adding package functions
 * to the supplied environment, and returning it as the return value.
 * It also creates the package.preload and package.loaded tables for use by
 * other libraries.
 * @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();
	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_.set("config", FILE_SEP + "\n;\n?\n!\n-\n");
	package_.get(_LOADED).set("package", package_);
	env.set("package", package_);
	globals.package_ = this;
	return env;
}
 
Example 4
Source File: LuaMapping.java    From Cubes with MIT License 6 votes vote down vote up
public static LuaTable mapping(Object o) {
  try {
    LuaTable luaTable = new LuaTable();
    for (Field field : o.getClass().getFields()) {
      if (Modifier.isStatic(field.getModifiers())) continue;
      if (LuaValue.class.isAssignableFrom(field.getType())) {
        luaTable.set(field.getName(), (LuaValue) field.get(o));
      }
      if (field.getType().equals(Class.class)) {
        luaTable.set(field.getName(), mapping((Class<?>) field.get(o)));
      }
    }
    return new ReadOnlyLuaTable(luaTable);
  } catch (Exception e) {
    throw new CubesException("Failed to create lua api", e);
  }
}
 
Example 5
Source File: LuaJsonElement.java    From Lukkit with MIT License 6 votes vote down vote up
private LuaTable getTableFromElement(JsonElement element) {
    LuaTable finalTable = new LuaTable();

    if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();

        for (int i = 0; i < array.size(); i++) {
            // Add one to i to match Lua array indexing standards
            finalTable.set(i + 1, this.getValueFromElement(array.get(i)));
        }
    } else {
        JsonObject obj = element.getAsJsonObject();

        for (Map.Entry<String, JsonElement> objectEntry : obj.entrySet()) {
            finalTable.set(objectEntry.getKey(), this.getValueFromElement(objectEntry.getValue()));
        }
    }

    return finalTable;
}
 
Example 6
Source File: LuaViewManager.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * bind lua functions using method
 *
 * @param factory
 * @param methods
 * @return
 */
public static LuaTable bindMethods(Class<? extends LibFunction> factory, List<Method> methods) {
    LuaTable env = new LuaTable();
    try {
        if (methods != null) {
            for (int i = 0; i < methods.size(); i++) {
                LibFunction f = factory.newInstance();
                f.opcode = -1;
                f.method = methods.get(i);
                f.name = methods.get(i).getName();
                env.set(f.name, f);
            }
        }
    } catch (Exception e) {
        throw new LuaError("[Bind Failed] " + e);
    } finally {
        return env;
    }
}
 
Example 7
Source File: LuaViewManager.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * bind lua functions using opcode
 *
 * @param factory
 * @param methods
 * @return
 */
public static LuaTable bind(Class<? extends LibFunction> factory, List<String> methods) {
    LuaTable env = new LuaTable();
    try {
        if (methods != null) {
            for (int i = 0; i < methods.size(); i++) {
                LibFunction f = factory.newInstance();
                f.opcode = i;
                f.method = null;
                f.name = methods.get(i);
                env.set(f.name, f);
            }
        }
    } catch (Exception e) {
        throw new LuaError("[Bind Failed] " + e);
    } finally {
        return env;
    }
}
 
Example 8
Source File: DebugLib.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.
 * @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 9
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 10
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * convert map to LuaTable
 *
 * @param params
 * @return
 */
public static LuaValue toTable(Map<?, ?> params) {
    if (params != null) {
        LuaTable result = new LuaTable();
        if (params.size() > 0) {
            Object valueObj;
            LuaValue key;
            for (Object keyObj : params.keySet()) {
                valueObj = params.get(keyObj);
                key = toLuaValue(keyObj);
                if (key != LuaValue.NIL) {
                    result.set(key, toLuaValue(valueObj));
                }
            }
        }
        return result;
    }
    return LuaValue.NIL;
}
 
Example 11
Source File: LuaMapping.java    From Cubes with MIT License 6 votes vote down vote up
public static LuaTable mapping(Class<?> c) {
  try {
    LuaTable luaTable = new LuaTable();
    for (Field field : c.getFields()) {
      if (!Modifier.isStatic(field.getModifiers())) continue;
      if (LuaValue.class.isAssignableFrom(field.getType())) {
        luaTable.set(field.getName(), (LuaValue) field.get(null));
      }
      if (field.getType().equals(Class.class)) {
        luaTable.set(field.getName(), mapping((Class<?>) field.get(null)));
      }
    }
    return new ReadOnlyLuaTable(luaTable);
  } catch (Exception e) {
    throw new CubesException("Failed to create lua api", e);
  }
}
 
Example 12
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 13
Source File: LuaTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Test
public void testLuaTable() {
    final Globals theGlobals = new Globals();
    LuaC.install(theGlobals);
    final LuaTable theTable = new LuaTable();
    theTable.set("20", 200);
    Assert.assertEquals(200, theTable.get("20").toint(), 0);
}
 
Example 14
Source File: LVVideoPlugin.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Varargs invoke(Varargs args) {
    LuaTable table = LuaValue.tableOf();
    PlatformInfo platformInfo = platform.getPlatformInfo();
    if (!TextUtils.isEmpty(platformInfo.getVideoId())) {
        table.set("videoId", platformInfo.getVideoId());
    }
    IMediaControlListener mediaControlListener = platform.getMediaControlListener();
    if (mediaControlListener != null) {
        if (!TextUtils.isEmpty(mediaControlListener.getVideoEpisode())) {
            table.set("episode", mediaControlListener.getVideoEpisode());
        }
        if (!TextUtils.isEmpty(mediaControlListener.getVideoTitle())) {
            table.set("title", mediaControlListener.getVideoTitle());
        }
    }
    if (!TextUtils.isEmpty(platformInfo.getThirdPlatformId())) {
        table.set("platformId", platformInfo.getThirdPlatformId());
    }
    if (!TextUtils.isEmpty(platformInfo.getVideoCategory())) {
        table.set("category", platformInfo.getVideoCategory());
    }
    if (platformInfo.getExtendDict() != null && platformInfo.getExtendDict().size() > 0) {
        table.set("extendDict", LuaUtil.toTable(platformInfo.getExtendDict()));
    }
    return table;
}
 
Example 15
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 16
Source File: TableHashTest.java    From luaj with MIT License 5 votes vote down vote up
public void testNext() {
	final LuaTable t = new_Table();
	assertEquals( LuaValue.NIL, t.next(LuaValue.NIL) );
	
	// insert array elements
	t.set( 1, "one" );
	assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
	assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
	assertEquals( LuaValue.NIL, t.next(LuaValue.ONE) );
	t.set( 2, "two" );
	assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
	assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
	assertEquals( LuaValue.valueOf(2),     t.next(LuaValue.ONE).arg(1) );
	assertEquals( LuaValue.valueOf("two"), t.next(LuaValue.ONE).arg(2) );
	assertEquals( LuaValue.NIL, t.next(LuaValue.valueOf(2)) );
	
	// insert hash elements
	t.set( "aa", "aaa" );
	assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
	assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
	assertEquals( LuaValue.valueOf(2),     t.next(LuaValue.ONE).arg(1) );
	assertEquals( LuaValue.valueOf("two"), t.next(LuaValue.ONE).arg(2) );
	assertEquals( LuaValue.valueOf("aa"),  t.next(LuaValue.valueOf(2)).arg(1) );
	assertEquals( LuaValue.valueOf("aaa"), t.next(LuaValue.valueOf(2)).arg(2) );
	assertEquals( LuaValue.NIL, t.next(LuaValue.valueOf("aa")) );
	t.set( "bb", "bbb" );
	assertEquals( LuaValue.valueOf(1),     t.next(LuaValue.NIL).arg(1) );
	assertEquals( LuaValue.valueOf("one"), t.next(LuaValue.NIL).arg(2) );
	assertEquals( LuaValue.valueOf(2),     t.next(LuaValue.ONE).arg(1) );
	assertEquals( LuaValue.valueOf("two"), t.next(LuaValue.ONE).arg(2) );
	assertEquals( LuaValue.valueOf("aa"),  t.next(LuaValue.valueOf(2)).arg(1) );
	assertEquals( LuaValue.valueOf("aaa"), t.next(LuaValue.valueOf(2)).arg(2) );
	assertEquals( LuaValue.valueOf("bb"),  t.next(LuaValue.valueOf("aa")).arg(1) );
	assertEquals( LuaValue.valueOf("bbb"), t.next(LuaValue.valueOf("aa")).arg(2) );
	assertEquals( LuaValue.NIL, t.next(LuaValue.valueOf("bb")) );
}
 
Example 17
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 18
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 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: TableHashTest.java    From luaj with MIT License 4 votes vote down vote up
public void testIndexFunction() {
	final LuaTable t = new_Table();
	final LuaTable mt = new_Table();		
	
	final TwoArgFunction fb = new TwoArgFunction() {
		public LuaValue call(LuaValue tbl, LuaValue key) {
			assertEquals(tbl, t);
			return valueOf("from mt: "+key);
		}
	};
	
	// set basic values
	t.set( "ppp", "abc" );
	t.set( 123, "def" );
	mt.set(LuaValue.INDEX, fb);
	
	// check before setting metatable
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "nil", t.get("qqq").tojstring() );
	assertEquals( "nil", t.get(456).tojstring() );
	
	
	// check before setting metatable
	t.setmetatable(mt);
	assertEquals( mt, t.getmetatable() );
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "from mt: qqq", t.get("qqq").tojstring() );
	assertEquals( "from mt: 456", t.get(456).tojstring() );
	
	// use raw set
	t.rawset("qqq", "alt-qqq");
	t.rawset(456, "alt-456");
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "alt-qqq", t.get("qqq").tojstring() );
	assertEquals( "alt-456", t.get(456).tojstring() );

	// remove using raw set
	t.rawset("qqq", LuaValue.NIL);
	t.rawset(456, LuaValue.NIL);
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "from mt: qqq", t.get("qqq").tojstring() );
	assertEquals( "from mt: 456", t.get(456).tojstring() );

	// set metatable to null
	t.setmetatable(null);
	assertEquals( "abc", t.get("ppp").tojstring() );
	assertEquals( "def", t.get(123).tojstring() );
	assertEquals( "nil", t.get("qqq").tojstring() );
	assertEquals( "nil", t.get(456).tojstring() );
}