Java Code Examples for org.luaj.vm2.LuaString#s_metatable()

The following examples show how to use org.luaj.vm2.LuaString#s_metatable() . 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 VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public LuaValue call(LuaValue value, LuaValue table) {
    LuaValue mt = table.opttable(null);
    switch (value.type()) {
        case TNIL:
            LuaNil.s_metatable = mt;
            break;
        case TNUMBER:
            LuaNumber.s_metatable = mt;
            break;
        case TBOOLEAN:
            LuaBoolean.s_metatable = mt;
            break;
        case TSTRING:
            LuaString.s_metatable = mt;
            break;
        case TFUNCTION:
            LuaFunction.s_metatable = mt;
            break;
        case TTHREAD:
            LuaThread.s_metatable = mt;
            break;
        default:
            value.setmetatable(mt);
    }
    return value;
}
 
Example 2
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 3
Source File: StringLib.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.
 * 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: StringLib.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.
 * 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());
	
	env.set("string", string);
	if (!env.get("package").isnil()) env.get("package").get("loaded").set("string", string);
	if (LuaString.s_metatable == null) {
		LuaString.s_metatable = LuaValue.tableOf(new LuaValue[] { INDEX, string });
	}
	return string;
}
 
Example 5
Source File: StringLib.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) {
    LuaTable t = new LuaTable();
    bind(t, StringLib1.class, new String[]{
            "dump", "len", "lower", "reverse", "upper"});
    bind(t, StringLibV.class, new String[]{
            "byte", "char", "find", "format",
            "gmatch", "gsub", "match", "rep",
            "sub"});
    env.set("string", t);
    instance = t;
    if (LuaString.s_metatable == null)
        LuaString.s_metatable = tableOf(new LuaValue[]{INDEX, t});
    env.get("package").get("loaded").set("string", t);
    return t;
}
 
Example 6
Source File: DebugLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue call(LuaValue value, LuaValue table) {
	LuaValue mt = table.opttable(null);
	switch ( value.type() ) {
		case TNIL:      LuaNil.s_metatable      = mt; break;
		case TNUMBER:   LuaNumber.s_metatable   = mt; break;
		case TBOOLEAN:  LuaBoolean.s_metatable  = mt; break;
		case TSTRING:   LuaString.s_metatable   = mt; break;
		case TFUNCTION: LuaFunction.s_metatable = mt; break;
		case TTHREAD:   LuaThread.s_metatable   = mt; break;
		default: value.setmetatable( mt );
	}
	return value;
}
 
Example 7
Source File: DebugLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public LuaValue call(LuaValue value, LuaValue table) {
	LuaValue mt = table.opttable(null);
	switch ( value.type() ) {
		case TNIL:      LuaNil.s_metatable      = mt; break;
		case TNUMBER:   LuaNumber.s_metatable   = mt; break;
		case TBOOLEAN:  LuaBoolean.s_metatable  = mt; break;
		case TSTRING:   LuaString.s_metatable   = mt; break;
		case TFUNCTION: LuaFunction.s_metatable = mt; break;
		case TTHREAD:   LuaThread.s_metatable   = mt; break;
		default: value.setmetatable( mt );
	}
	return value;
}
 
Example 8
Source File: DebugLib.java    From luaj with MIT License 5 votes vote down vote up
public LuaValue call(LuaValue value, LuaValue table) {
	LuaValue mt = table.opttable(null);
	switch ( value.type() ) {
		case TNIL:      LuaNil.s_metatable      = mt; break;
		case TNUMBER:   LuaNumber.s_metatable   = mt; break;
		case TBOOLEAN:  LuaBoolean.s_metatable  = mt; break;
		case TSTRING:   LuaString.s_metatable   = mt; break;
		case TFUNCTION: LuaFunction.s_metatable = mt; break;
		case TTHREAD:   LuaThread.s_metatable   = mt; break;
		default: value.setmetatable( mt );
	}
	return value;
}
 
Example 9
Source File: SampleSandboxed.java    From luaj with MIT License 4 votes vote down vote up
public static void main(String[] args) {
	// Create server globals with just enough library support to compile user scripts.
	server_globals = new Globals();
	server_globals.load(new JseBaseLib());
	server_globals.load(new PackageLib());
	server_globals.load(new JseStringLib());

	// To load scripts, we occasionally need a math library in addition to compiler support.
	// To limit scripts using the debug library, they must be closures, so we only install LuaC.
	server_globals.load(new JseMathLib());
	LoadState.install(server_globals);
	LuaC.install(server_globals);

	// Set up the LuaString metatable to be read-only since it is shared across all scripts.
	LuaString.s_metatable = new ReadOnlyLuaTable(LuaString.s_metatable);

	// Example normal scripts that behave as expected.
	runScriptInSandbox( "return 'foo'" );
	runScriptInSandbox( "return ('abc'):len()" );
	runScriptInSandbox( "return getmetatable('abc')" );
	runScriptInSandbox( "return getmetatable('abc').len" );
	runScriptInSandbox( "return getmetatable('abc').__index" );

	// Example user scripts that attempt rogue operations, and will fail.
	runScriptInSandbox( "return setmetatable('abc', {})" );
	runScriptInSandbox( "getmetatable('abc').len = function() end" );
	runScriptInSandbox( "getmetatable('abc').__index = {}" );
	runScriptInSandbox( "getmetatable('abc').__index.x = 1" );
	runScriptInSandbox( "while true do print('loop') end" );
	
	// Example use of other shared metatables, which should also be made read-only.
	// This toy example allows booleans to be added to numbers.
	runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" );
	LuaBoolean.s_metatable = new ReadOnlyLuaTable(LuaValue.tableOf(new LuaValue[] {
			LuaValue.ADD, new TwoArgFunction() {
				public LuaValue call(LuaValue x, LuaValue y) {
					return LuaValue.valueOf(
							(x == TRUE ? 1.0 : x.todouble()) +
							(y == TRUE ? 1.0 : y.todouble()) );
				}
			},
	}));
	runScriptInSandbox( "return 5 + 6, 5 + true, false + 6" );
}