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

The following examples show how to use org.luaj.vm2.LuaValue#checkstring() . 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: CoerceLuaToJava.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public Object coerce(LuaValue value) {
    if (value.isnil())
        return null;
    if (targetType == TARGET_TYPE_STRING)
        return value.tojstring();
    LuaString s = value.checkstring();
    byte[] b = new byte[s.m_length];
    s.copyInto(0, b, 0, b.length);
    return b;
}
 
Example 2
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * string.reverse (s)
 * <p>
 * Returns a string that is the string s reversed.
 */
static LuaValue reverse(LuaValue arg) {
    LuaString s = arg.checkstring();
    int n = s.length();
    byte[] b = new byte[n];
    for (int i = 0, j = n - 1; i < n; i++, j--)
        b[j] = (byte) s.luaByte(i);
    return LuaString.valueOf(b);
}
 
Example 3
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 4
Source File: CoerceLuaToJava.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public Object coerce(LuaValue value) {
	if ( value.isnil() )
		return null;
	if ( targetType == TARGET_TYPE_STRING )
		return value.tojstring();
	LuaString s = value.checkstring();
	byte[] b = new byte[s.m_length];
	s.copyInto(0, b, 0, b.length);
	return b;
}
 
Example 5
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue call(LuaValue arg) {
	LuaString s = arg.checkstring();
	int n = s.length();
	byte[] b = new byte[n];
	for ( int i=0, j=n-1; i<n; i++, j-- )
		b[j] = (byte) s.luaByte(i);
	return LuaString.valueUsing( b );
}
 
Example 6
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 7
Source File: CoerceLuaToJava.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public Object coerce(LuaValue value) {
	if ( value.isnil() )
		return null;
	if ( targetType == TARGET_TYPE_STRING )
		return value.tojstring();
	LuaString s = value.checkstring();
	byte[] b = new byte[s.m_length];
	s.copyInto(0, b, 0, b.length);
	return b;
}
 
Example 8
Source File: StringLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public LuaValue call(LuaValue arg) {
	LuaString s = arg.checkstring();
	int n = s.length();
	byte[] b = new byte[n];
	for ( int i=0, j=n-1; i<n; i++, j-- )
		b[j] = (byte) s.luaByte(i);
	return LuaString.valueUsing( b );
}
 
Example 9
Source File: CoerceLuaToJava.java    From luaj with MIT License 5 votes vote down vote up
public Object coerce(LuaValue value) {
	if ( value.isnil() )
		return null;
	if ( targetType == TARGET_TYPE_STRING )
		return value.tojstring();
	LuaString s = value.checkstring();
	byte[] b = new byte[s.m_length];
	s.copyInto(0, b, 0, b.length);
	return b;
}
 
Example 10
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 11
Source File: StringLib.java    From luaj with MIT License 5 votes vote down vote up
public LuaValue call(LuaValue arg) {
	LuaString s = arg.checkstring();
	int n = s.length();
	byte[] b = new byte[n];
	for ( int i=0, j=n-1; i<n; i++, j-- )
		b[j] = (byte) s.luaByte(i);
	return LuaString.valueUsing( b );
}
 
Example 12
Source File: LuaPrint.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Print the state of a {@link LuaClosure} that is being executed
 *
 * @param cl      the {@link LuaClosure}
 * @param pc      the program counter
 * @param stack   the stack of {@link LuaValue}
 * @param top     the top of the stack
 * @param varargs any {@link Varargs} value that may apply
 */
public LuaPrint buildState(LuaClosure cl, int pc, LuaValue[] stack, int top, Varargs varargs) {
    // print opcode into buffer
    StringBuffer previous = ps;
    ps = new StringBuffer();
    buildOpCode(cl.p, pc);

    VenvyLog.i(ps + "");

    ps = previous;
    format(ps.toString(), 50);

    // print stack
    ps.append('[');
    for (int i = 0; i < stack.length; i++) {
        LuaValue v = stack[i];
        if (v == null)
            ps.append(STRING_FOR_NULL);
        else switch (v.type()) {
            case LuaValue.TSTRING:
                LuaString s = v.checkstring();
                ps.append(s.length() < 48 ?
                        s.tojstring() :
                        s.substring(0, 32).tojstring() + "...+" + (s.length() - 32) + "b");
                break;
            case LuaValue.TFUNCTION:
                ps.append(v.tojstring());
                break;
            case LuaValue.TUSERDATA:
                Object o = v.touserdata();
                if (o != null) {
                    String n = o.getClass().getName();
                    n = n.substring(n.lastIndexOf('.') + 1);
                    ps.append(n + ": " + Integer.toHexString(o.hashCode()));
                } else {
                    ps.append(v.toString());
                }
                break;
            default:
                ps.append(v.tojstring());
        }
        if (i + 1 == top)
            ps.append(']');
        ps.append(" | ");
    }
    ps.append(varargs);
    ps.append("\n");
    return this;
}
 
Example 13
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;
         }
     }