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

The following examples show how to use org.luaj.vm2.LuaTable#get() . 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: LuaUtils.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
static Map<String, String> luaTableToMap(LuaTable value) {
    Map<String, String> params = new HashMap<>();

    LuaValue[] keys = value.keys();

    for (LuaValue k : keys) {
        if (k.isstring()) {
            LuaValue v = value.get(k);
            if (v.isstring()) {
                params.put(k.tojstring(), v.tojstring());
            }
        }
    }

    return params;
}
 
Example 2
Source File: LuaConversion.java    From Cubes with MIT License 6 votes vote down vote up
public static void complexToJava(LuaValue l, Object o) {
  try {
    Field[] fields = o.getClass().getFields();
    LuaTable table = l.checktable();
    for (Field field : fields) {
      if (!(Modifier.isPublic(field.getModifiers()) || field.isAnnotationPresent(LuaInclude.class)) || field.isAnnotationPresent(LuaExclude.class))
        continue;
      if (Modifier.isFinal(field.getModifiers())) continue;
      String name = field.getName();
      Class<?> type = field.getType();
      LuaValue v = table.get(name);
      Object instance = convertToJava(type, v);
      field.set(o, instance);
    }
  } catch (Exception e) {
    Log.warning(e);
  }
}
 
Example 3
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 4
Source File: PackageLib.java    From XPrivacyLua with GNU General Public License v3.0 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 5
Source File: PackageLib.java    From HtmlNative with Apache License 2.0 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 6
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 7
Source File: PackageLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 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 if (globals != null && (result = globals.lazyLoad(name.checkjstring())) != null){//TODO add by song, 如果是加载自定义的内容则使用globals加载
             loaded.set(name, _SENTINEL);
             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);
         try {
             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;
         } catch (Exception e) {
             return NIL;
         }
     }
 
Example 8
Source File: IoLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
private void setLibInstance(LuaTable t) {
	LuaValue[] k = t.keys();
	for ( int i=0, n=k.length; i<n; i++ )
		((IoLibV) t.get(k[i])).iolib = this;
}
 
Example 9
Source File: IoLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
private void setLibInstance(LuaTable t) {
	LuaValue[] k = t.keys();
	for ( int i=0, n=k.length; i<n; i++ )
		((IoLibV) t.get(k[i])).iolib = this;
}
 
Example 10
Source File: IoLib.java    From luaj with MIT License 4 votes vote down vote up
private void setLibInstance(LuaTable t) {
	LuaValue[] k = t.keys();
	for ( int i=0, n=k.length; i<n; i++ )
		((IoLibV) t.get(k[i])).iolib = this;
}