org.luaj.vm2.LuaBoolean Java Examples

The following examples show how to use org.luaj.vm2.LuaBoolean. 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: LuaInterpreter.java    From claudb with MIT License 6 votes vote down vote up
private RedisToken convert(Object result) {
  return Pattern1.<Object, RedisToken>build()
      .when(instanceOf(LuaTable.class))
        .then(table -> convertLuaTable((LuaTable) table))
      .when(instanceOf(LuaNumber.class))
        .then(number -> convertLuaNumber((LuaNumber) number))
      .when(instanceOf(LuaBoolean.class))
        .then(boolean_ -> convertLuaBoolean((LuaBoolean) boolean_))
      .when(instanceOf(LuaString.class))
        .then(string -> convertLuaString((LuaString) string))
      .when(instanceOf(Number.class))
        .then(number -> convertNumber((Number) number))
      .when(instanceOf(String.class))
        .then(string -> convertString((String) string))
      .when(instanceOf(Boolean.class))
        .then(boolean_ -> convertBoolean((Boolean) boolean_))
      .otherwise()
        .then(this::convertUnknown)
      .apply(result);
}
 
Example #3
Source File: YamlStorage.java    From Lukkit with MIT License 5 votes vote down vote up
@Override
protected LuaBoolean clearVaule(LuaString path) throws StorageObjectException {
    if (this.yamlConfiguration.get(path.checkjstring()) == null) {
        this.yamlConfiguration.set(path.checkjstring(), null);
        return LuaValue.TRUE;
    }

    return LuaValue.FALSE;
}
 
Example #4
Source File: LuaUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * convert a table to map
 *
 * @param table
 * @return
 */
public static HashMap<String, String> toMap(LuaTable table) {
    if (table != null) {
        final HashMap<String, String> result = new HashMap<String, String>();
        final LuaValue[] keys = table.keys();
        for (LuaValue key : keys) {
            LuaValue luaValue = table.get(key);
            String value = null;
            if (luaValue.istable()) {
                value = JsonUtil.toString(luaValue);
            } else {
                if (luaValue instanceof LuaBoolean) {
                    value = String.valueOf(luaValue.optboolean(false));
                } else if (luaValue instanceof LuaInteger) {
                    value = String.valueOf(luaValue.optint(0));
                } else if (luaValue instanceof LuaDouble) {
                    value = String.valueOf(luaValue.optdouble(0));
                } else if (luaValue instanceof LuaString) {
                    value = String.valueOf(luaValue.optstring(null));
                } else {
                    value = String.valueOf(luaValue);
                }
            }
            if (value != null) {
                result.put(key.optjstring(null), value);
            }
        }
        return result;
    }
    return null;
}
 
Example #5
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 #6
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 #7
Source File: YamlStorage.java    From Lukkit with MIT License 5 votes vote down vote up
@Override
public LuaBoolean setDefaultValue(LuaString path, LuaValue value) throws StorageObjectException {
    if (this.yamlConfiguration.get(path.checkjstring()) == null) {
        this.yamlConfiguration.set(path.checkjstring(), Utilities.getObjectFromLuavalue(value));
        return LuaValue.TRUE;
    }

    return LuaValue.FALSE;
}
 
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: JsonStorage.java    From Lukkit with MIT License 5 votes vote down vote up
@Override
protected LuaBoolean clearVaule(LuaString path) throws StorageObjectException {
    if (object.has(path.checkjstring())) {
        object.remove(path.checkjstring());
        return LuaValue.TRUE;
    }
    return LuaValue.FALSE;
}
 
Example #10
Source File: JsonStorage.java    From Lukkit with MIT License 5 votes vote down vote up
@Override
public LuaBoolean setDefaultValue(LuaString path, LuaValue value) throws StorageObjectException {
    if (object.has(path.checkjstring())) {
        setValue(path, value);
        return LuaValue.TRUE;
    }
    return LuaValue.FALSE;
}
 
Example #11
Source File: UDSystem.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Varargs invoke(Varargs args) {
    return LuaBoolean.FALSE;
}
 
Example #12
Source File: YamlStorage.java    From Lukkit with MIT License 4 votes vote down vote up
@Override
protected LuaBoolean exists(String path) {
    return yamlConfiguration.get(path) != null ? LuaValue.TRUE : LuaValue.FALSE;
}
 
Example #13
Source File: JsonStorage.java    From Lukkit with MIT License 4 votes vote down vote up
@Override
protected LuaBoolean exists(String path) {
    return object.has(path) ? LuaValue.TRUE : LuaValue.FALSE;
}
 
Example #14
Source File: LuaInterpreter.java    From claudb with MIT License 4 votes vote down vote up
private RedisToken convertLuaBoolean(LuaBoolean value) {
  return value.toboolean() ? integer(1) : nullString();
}
 
Example #15
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" );
}
 
Example #16
Source File: UDSystem.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Varargs invoke(Varargs args) {
    return LuaBoolean.TRUE;
}
 
Example #17
Source File: StorageObject.java    From Lukkit with MIT License 2 votes vote down vote up
/**
 * Checks if the key exists.
 *
 * @return the type
 */
protected abstract LuaBoolean exists(String path);
 
Example #18
Source File: StorageObject.java    From Lukkit with MIT License 2 votes vote down vote up
/**
 * Sets a value if it doesn't exist.
 *
 * @param path  the path of the key
 * @param value the value
 * @return true if the value is set, false if not
 * @throws StorageObjectException
 */
protected abstract LuaBoolean setDefaultValue(LuaString path, LuaValue value) throws StorageObjectException;
 
Example #19
Source File: StorageObject.java    From Lukkit with MIT License 2 votes vote down vote up
/**
 * Clears a value
 *
 * @param path the path of the key
 * @throws StorageObjectException
 */
protected abstract LuaBoolean clearVaule(LuaString path) throws StorageObjectException;