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

The following examples show how to use org.luaj.vm2.LuaValue#isstring() . 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 VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public Varargs invoke(Varargs args) {
            LuaString name = args.checkstring(1);
//            InputStream is = null;

            // get package path
            LuaValue path = package_.get(_PATH);
            if (!path.isstring())
                return valueOf("package.path is not a string");

            // get the searchpath function.
            Varargs v = package_.get(_SEARCHPATH).invoke(varargsOf(name, path));

            // Did we get a result?
            if (!v.isstring(1))
                return v.arg(2).tostring();
            LuaString filename = v.arg1().strvalue();

            // Try to load the file.
            v = globals.loadfile(filename.tojstring());
            if (v.arg1().isfunction())
                return LuaValue.varargsOf(v.arg1(), filename);

            // report error
            return varargsOf(NIL, valueOf("'" + filename + "': " + v.arg(2).tojstring()));
        }
 
Example 2
Source File: PackageLib.java    From luaj with MIT License 6 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString name = args.checkstring(1);
			
	// get package path
	LuaValue path = package_.get(_PATH);
	if ( ! path.isstring() )
		return valueOf("package.path is not a string");

	// get the searchpath function.
	Varargs v = package_.get(_SEARCHPATH).invoke(varargsOf(name, path));
	
	// Did we get a result?
	if (!v.isstring(1))
		return v.arg(2).tostring();
	LuaString filename = v.arg1().strvalue();

	// Try to load the file.
	v = globals.loadfile(filename.tojstring());
	if ( v.arg1().isfunction() )
		return LuaValue.varargsOf(v.arg1(), filename);
	
	// report error
	return varargsOf(NIL, valueOf("'"+filename+"': "+v.arg(2).tojstring()));
}
 
Example 3
Source File: PackageLib.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString name = args.checkstring(1);
	InputStream is = null;
			
	// get package path
	LuaValue path = package_.get(_PATH);
	if ( ! path.isstring() ) 
		return valueOf("package.path is not a string");

	// get the searchpath function.
	Varargs v = package_.get(_SEARCHPATH).invoke(varargsOf(name, path));
	
	// Did we get a result?
	if (!v.isstring(1))
		return v.arg(2).tostring();
	LuaString filename = v.arg1().strvalue();

	// Try to load the file.
	v = globals.loadfile(filename.tojstring()); 
	if ( v.arg1().isfunction() )
		return LuaValue.varargsOf(v.arg1(), filename);
	
	// report error
	return varargsOf(NIL, valueOf("'"+filename+"': "+v.arg(2).tojstring()));
}
 
Example 4
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 5
Source File: Utilities.java    From Lukkit with MIT License 6 votes vote down vote up
/**
 * Gets the Java object from a LuaValue.
 *
 * @param value the LuaValue
 * @return the Java object
 */
public static Object getObjectFromLuavalue(LuaValue value) {
    if (value.istable()) {
        return convertTable(value.checktable());
    } else if (value.isint()) {
        return value.checkint();
    } else if (value.islong()) {
        return value.checklong();
    } else if (value.isnumber()) {
        return value.checkdouble();
    } else if (value.isstring()) {
        return value.checkjstring();
    } else if (value.isboolean()) {
        return value.checkboolean();
    } else if (value.isnil()) {
        return null;
    } else {
        return value.checkuserdata();
    }
}
 
Example 6
Source File: StringLib.java    From luaj with MIT License 5 votes vote down vote up
public void add_value( Buffer lbuf, int soffset, int end, LuaValue repl ) {
	switch ( repl.type() ) {
	case LuaValue.TSTRING:
	case LuaValue.TNUMBER:
		add_s( lbuf, repl.strvalue(), soffset, end );
		return;
		
	case LuaValue.TFUNCTION:
		repl = repl.invoke( push_captures( true, soffset, end ) ).arg1();
		break;
		
	case LuaValue.TTABLE:
		// Need to call push_onecapture here for the error checking
		repl = repl.get( push_onecapture( 0, soffset, end ) );
		break;
		
	default:
		error( "bad argument: string/function/table expected" );
		return;
	}
	
	if ( !repl.toboolean() ) {
		repl = s.substring( soffset, end );
	} else if ( ! repl.isstring() ) {
		error( "invalid replacement value (a "+repl.typename()+")" );
	}
	lbuf.append( repl.strvalue() );
}
 
Example 7
Source File: DebugLib.java    From luaj with MIT License 5 votes vote down vote up
static String kname(Prototype p, int pc, int c) {
	if (Lua.ISK(c)) {  /* is 'c' a constant? */
		LuaValue k = p.k[Lua.INDEXK(c)];
		if (k.isstring()) {  /* literal constant? */
			return k.tojstring();  /* it is its own name */
		} /* else no reasonable name found */
	} else {  /* 'c' is a register */
		NameWhat what = getobjname(p, pc, c); /* search for 'c' */
	    if (what != null && "constant".equals(what.namewhat)) {  /* found a constant name? */
	      return what.name;  /* 'name' already filled */
	    }
	    /* else no reasonable name found */
	}
	return "?";  /* no reasonable name found */
}
 
Example 8
Source File: LuaSkinLoader.java    From beatoraja with GNU General Public License v3.0 5 votes vote down vote up
private static <T> T serializeLuaScript(LuaValue lv, Function<LuaFunction, T> asFunction, Function<String, T> asScript, Function<Integer, T> byId) {
	if (lv.isfunction()) {
		return asFunction.apply(lv.checkfunction());
	} else if (lv.isnumber() && byId != null) {
		return byId.apply(lv.toint());
	} else if (lv.isstring()) {
		return asScript.apply(lv.tojstring());
	} else {
		return null;
	}
}
 
Example 9
Source File: StringLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public void add_value( Buffer lbuf, int soffset, int end, LuaValue repl ) {
	switch ( repl.type() ) {
	case LuaValue.TSTRING:
	case LuaValue.TNUMBER:
		add_s( lbuf, repl.strvalue(), soffset, end );
		return;
		
	case LuaValue.TFUNCTION:
		repl = repl.invoke( push_captures( true, soffset, end ) ).arg1();
		break;
		
	case LuaValue.TTABLE:
		// Need to call push_onecapture here for the error checking
		repl = repl.get( push_onecapture( 0, soffset, end ) );
		break;
		
	default:
		error( "bad argument: string/function/table expected" );
		return;
	}
	
	if ( !repl.toboolean() ) {
		repl = s.substring( soffset, end );
	} else if ( ! repl.isstring() ) {
		error( "invalid replacement value (a "+repl.typename()+")" );
	}
	lbuf.append( repl.strvalue() );
}
 
Example 10
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public void add_value( Buffer lbuf, int soffset, int end, LuaValue repl ) {
	switch ( repl.type() ) {
	case LuaValue.TSTRING:
	case LuaValue.TNUMBER:
		add_s( lbuf, repl.strvalue(), soffset, end );
		return;
		
	case LuaValue.TFUNCTION:
		repl = repl.invoke( push_captures( true, soffset, end ) ).arg1();
		break;
		
	case LuaValue.TTABLE:
		// Need to call push_onecapture here for the error checking
		repl = repl.get( push_onecapture( 0, soffset, end ) );
		break;
		
	default:
		error( "bad argument: string/function/table expected" );
		return;
	}
	
	if ( !repl.toboolean() ) {
		repl = s.substring( soffset, end );
	} else if ( ! repl.isstring() ) {
		error( "invalid replacement value (a "+repl.typename()+")" );
	}
	lbuf.append( repl.strvalue() );
}
 
Example 11
Source File: BaseLib.java    From luaj with MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaValue ld = args.arg1();
	if (!ld.isstring() && !ld.isfunction()) {
		throw new LuaError("bad argument #1 to 'load' (string or function expected, got " + ld.typename() + ")");
	}
	String source = args.optjstring(2, ld.isstring()? ld.tojstring(): "=(load)");
	String mode = args.optjstring(3, "bt");
	LuaValue env = args.optvalue(4, globals);
	return loadStream(ld.isstring()? ld.strvalue().toInputStream():
		new StringInputStream(ld.checkfunction()), source, mode, env);
}
 
Example 12
Source File: IoLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public Varargs _io_output(LuaValue filename) {
	outfile = filename.isnil()? output(): 
			  filename.isstring()? ioopenfile(FTYPE_NAMED, filename.checkjstring(),"w"):
			  checkfile(filename);
	return outfile;
}
 
Example 13
Source File: IoLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public Varargs _io_input(LuaValue file) {
	infile = file.isnil()? input(): 
			file.isstring()? ioopenfile(FTYPE_NAMED, file.checkjstring(),"r"):
			checkfile(file);
	return infile;
}
 
Example 14
Source File: BaseLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public LuaValue call(LuaValue arg1, LuaValue arg2) {
	throw arg1.isnil()? new LuaError(null, arg2.optint(1)): 
		arg1.isstring()? new LuaError(arg1.tojstring(), arg2.optint(1)): 
			new LuaError(arg1);
}
 
Example 15
Source File: BaseLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public LuaValue call(LuaValue arg1, LuaValue arg2) {
    throw arg1.isnil() ? new LuaError(null, arg2.optint(1)) : arg1.isstring() ? new
            LuaError(arg1.tojstring(), arg2.optint(1)) : new LuaError(arg1);
}
 
Example 16
Source File: IoLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public Varargs _io_output(LuaValue filename) {
	outfile = filename.isnil()? output(): 
			  filename.isstring()? ioopenfile(FTYPE_NAMED, filename.checkjstring(),"w"):
			  checkfile(filename);
	return outfile;
}
 
Example 17
Source File: IoLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public Varargs _io_input(LuaValue file) {
	infile = file.isnil()? input(): 
			file.isstring()? ioopenfile(FTYPE_NAMED, file.checkjstring(),"r"):
			checkfile(file);
	return infile;
}
 
Example 18
Source File: IoLib.java    From luaj with MIT License 4 votes vote down vote up
public Varargs _io_input(LuaValue file) {
	infile = file.isnil()? input():
			file.isstring()? ioopenfile(FTYPE_NAMED, file.checkjstring(),"r"):
			checkfile(file);
	return infile;
}
 
Example 19
Source File: IoLib.java    From luaj with MIT License 4 votes vote down vote up
public Varargs _io_output(LuaValue filename) {
	outfile = filename.isnil()? output():
			  filename.isstring()? ioopenfile(FTYPE_NAMED, filename.checkjstring(),"w"):
			  checkfile(filename);
	return outfile;
}
 
Example 20
Source File: BaseLib.java    From luaj with MIT License 4 votes vote down vote up
public LuaValue call(LuaValue arg1, LuaValue arg2) {
	if (arg1.isnil()) throw new LuaError(NIL);
	if (!arg1.isstring() || arg2.optint(1) == 0) throw new LuaError(arg1);
	throw new LuaError(arg1.tojstring(), arg2.optint(1));
}