org.luaj.vm2.LuaError Java Examples

The following examples show how to use org.luaj.vm2.LuaError. 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: JavaInstance.java    From luaj with MIT License 6 votes vote down vote up
public LuaValue get(LuaValue key) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			return CoerceJavaToLua.coerce(f.get(m_instance));
		} catch (Exception e) {
			throw new LuaError(e);
		}
	LuaValue m = jclass.getMethod(key);
	if ( m != null )
		return m;
	Class c = jclass.getInnerClass(key);
	if ( c != null )
		return JavaClass.forClass(c);
	return super.get(key);
}
 
Example #2
Source File: LuaConversion.java    From Cubes with MIT License 6 votes vote down vote up
@Override
public Object toJava(LuaValue l, Class c) {
  Object o;
  if (l instanceof LuaObject) {
    o = ((LuaObject) l).getObject();
  } else if (l instanceof LuaUserdata) {
    o = ((LuaUserdata) l).m_instance;
  } else {
    throw new LuaError("Cannot convert Lua to Java: " + l.typename() + " cannot be converted to " + c.getName());
  }

  if (c.isInstance(o)) {
    return o;
  } else {
    throw new LuaError("Cannot convert Lua to Java: " + o.getClass().getName() + " cannot be converted to " + c.getName());
  }
}
 
Example #3
Source File: JavaInstance.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
public LuaValue get(LuaValue key) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			return CoerceJavaToLua.coerce(f.get(m_instance));
		} catch (Exception e) {
			throw new LuaError(e);
		}
	LuaValue m = jclass.getMethod(key);
	if ( m != null )
		return m;
	Class c = jclass.getInnerClass(key);
	if ( c != null )
		return JavaClass.forClass(c);
	return super.get(key);
}
 
Example #4
Source File: LuaViewManager.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * bind lua functions using method
 *
 * @param factory
 * @param methods
 * @return
 */
public static LuaTable bindMethods(Class<? extends LibFunction> factory, List<Method> methods) {
    LuaTable env = new LuaTable();
    try {
        if (methods != null) {
            for (int i = 0; i < methods.size(); i++) {
                LibFunction f = factory.newInstance();
                f.opcode = -1;
                f.method = methods.get(i);
                f.name = methods.get(i).getName();
                env.set(f.name, f);
            }
        }
    } catch (Exception e) {
        throw new LuaError("[Bind Failed] " + e);
    } finally {
        return env;
    }
}
 
Example #5
Source File: LuaViewManager.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * bind lua functions using opcode
 *
 * @param factory
 * @param methods
 * @return
 */
public static LuaTable bind(Class<? extends LibFunction> factory, List<String> methods) {
    LuaTable env = new LuaTable();
    try {
        if (methods != null) {
            for (int i = 0; i < methods.size(); i++) {
                LibFunction f = factory.newInstance();
                f.opcode = i;
                f.method = null;
                f.name = methods.get(i);
                env.set(f.name, f);
            }
        }
    } catch (Exception e) {
        throw new LuaError("[Bind Failed] " + e);
    } finally {
        return env;
    }
}
 
Example #6
Source File: JavaInstance.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
public LuaValue get(LuaValue key) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			return CoerceJavaToLua.coerce(f.get(m_instance));
		} catch (Exception e) {
			throw new LuaError(e);
		}
	LuaValue m = jclass.getMethod(key);
	if ( m != null )
		return m;
	Class c = jclass.getInnerClass(key);
	if ( c != null )
		return JavaClass.forClass(c);
	return super.get(key);
}
 
Example #7
Source File: LuajavaClassMembersTest.java    From luaj with MIT License 5 votes vote down vote up
public void testNoAttribute() {
	JavaClass f = JavaClass.forClass(A.class);
	LuaValue v = f.get("bogus");
	assertEquals( v, LuaValue.NIL );
	try { 
		f.set("bogus",ONE);			
		fail( "did not throw lua error as expected" );
	} catch ( LuaError e ) {}
}
 
Example #8
Source File: JavaInstance.java    From luaj with MIT License 5 votes vote down vote up
public void set(LuaValue key, LuaValue value) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			f.set(m_instance, CoerceLuaToJava.coerce(value, f.getType()));
			return;
		} catch (Exception e) {
			throw new LuaError(e);
		}
	super.set(key, value);
}
 
Example #9
Source File: LibFunction.java    From luaj with MIT License 5 votes vote down vote up
/**
 * Bind a set of library functions, with an offset
 * <p>
 * An array of names is provided, and the first name is bound
 * with opcode = {@code firstopcode}, second with {@code firstopcode+1}, etc.
 * @param env The environment to apply to each bound function
 * @param factory the Class to instantiate for each bound function
 * @param names array of String names, one for each function.
 * @param firstopcode the first opcode to use
 * @see #bind(LuaValue, Class, String[])
 */
protected void bind(LuaValue env, Class factory,  String[] names, int firstopcode ) {
	try {
		for ( int i=0, n=names.length; i<n; i++ ) {
			LibFunction f = (LibFunction) factory.newInstance();
			f.opcode = firstopcode + i;
			f.name = names[i];
			env.set(f.name, f);
		}
	} catch ( Exception e ) {
		throw new LuaError( "bind failed: "+e );
	}
}
 
Example #10
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 #11
Source File: LexState.java    From luaj with MIT License 5 votes vote down vote up
void lexerror( String msg, int token ) {
	String cid = Lua.chunkid( source.tojstring() );
	L.pushfstring( cid+":"+linenumber+": "+msg );
	if ( token != 0 )
		L.pushfstring( "syntax error: "+msg+" near "+txtToken(token) );
	throw new LuaError(cid+":"+linenumber+": "+msg);
}
 
Example #12
Source File: LuajavaClassMembersTest.java    From luaj with MIT License 5 votes vote down vote up
public void testNoFactory() {
	JavaClass c = JavaClass.forClass(A.class);
	try {
		c.call();
		fail( "did not throw lua error as expected" );
	} catch ( LuaError e ) {
	}
}
 
Example #13
Source File: LuajavaClassMembersTest.java    From luaj with MIT License 5 votes vote down vote up
public void testUniqueFactoryUncoercible() {
	JavaClass f = JavaClass.forClass(B.class);
	LuaValue constr = f.get("new");
	assertEquals( JavaConstructor.class, constr.getClass() );
	try { 
		LuaValue v = constr.call(LuaValue.userdataOf(new Object()));
		Object b = v.touserdata();
		// fail( "did not throw lua error as expected" );
		assertEquals( 0, ((B)b).m_int_field );
	} catch ( LuaError e ) {
	}
}
 
Example #14
Source File: LuajavaClassMembersTest.java    From luaj with MIT License 5 votes vote down vote up
public void testOverloadedFactoryUncoercible() {
	JavaClass f = JavaClass.forClass(C.class);
	try { 
		Object c = f.call(LuaValue.userdataOf(new Object()));			
		// fail( "did not throw lua error as expected" );
		assertEquals( 0,     ((C)c).m_int_field );
		assertEquals( null,  ((C)c).m_string_field );
	} catch ( LuaError e ) {
	}
}
 
Example #15
Source File: JavaInstance.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue get(LuaValue key) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			return CoerceJavaToLua.coerce(f.get(m_instance));
		} catch (Exception e) {
			throw new LuaError(e);
		}
	LuaValue m = jclass.getMethod(key);
	if ( m != null )
		return m;
	return super.get(key);
}
 
Example #16
Source File: LuaJavaCoercionTest.java    From luaj with MIT License 5 votes vote down vote up
public void testLuaErrorCause() {
	String script = "luajava.bindClass( \""+SomeClass.class.getName()+"\"):someMethod()";
	LuaValue chunk = globals.get("load").call(LuaValue.valueOf(script));
	try {
		chunk.invoke(LuaValue.NONE);
		fail( "call should not have succeeded" );
	} catch ( LuaError lee ) {
		Throwable c = lee.getCause();
		assertEquals( SomeException.class, c.getClass() );
	}
}
 
Example #17
Source File: LuaScript.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public <T> T runOptional(String method, T defaultValue, Object... args) {
    try {
        if (!getScript().get(method).isfunction()) {
            return defaultValue;
        }

        int startIndex = 1;

        if(parent==null) {
            startIndex = 0;
        }

        LuaValue []luaArgs = new LuaValue[args.length+startIndex];

        if(parent!=null) {
            luaArgs[0] = CoerceJavaToLua.coerce(parent);
        }


        for (int i = startIndex;i<luaArgs.length;++i) {
            luaArgs[i] = CoerceJavaToLua.coerce(args[i-startIndex]);
        }

        if(defaultValue==null) {
            run(method, luaArgs);
            return null;
        }

        return (T) CoerceLuaToJava.coerce(
                run(method, luaArgs),
                defaultValue.getClass());
    } catch (LuaError e) {
        throw new ModError("Error when call:" + method+"."+scriptFile,e);
    }
}
 
Example #18
Source File: LuaEngine.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public LuaValue call(String method, Object arg1) {
	try {
		LuaValue methodForData = globals.get(method);
		return methodForData.call(CoerceJavaToLua.coerce(arg1));
	} catch (LuaError err) {
		reportLuaError(err);
	}
	return LuaValue.NIL;
}
 
Example #19
Source File: LuaEngine.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public void runScriptFile(@NotNull String fileName) {
	try {
		globals.loadfile(fileName).call();
	} catch (LuaError err) {
		reportLuaError(err);
	}
}
 
Example #20
Source File: LuaPackage.java    From Cubes with MIT License 5 votes vote down vote up
public LuaPackage(final String path) {
  super(new Object());
  this.path = path;
  
  LuaTable metatable = new LuaTable();
  metatable.rawset("__index", new TwoArgFunction() {
    @Override
    public LuaValue call(LuaValue arg1, LuaValue arg2) {
      if (arg2.checkjstring().equals("class")) {
        try {
          Class<?> cla = Class.forName(path);
          return new LuaClass(cla);
        } catch (ClassNotFoundException e) {
          throw new LuaError("No such class: " + path);
        }
      }
      String name = path.isEmpty() ? arg2.checkjstring() : path + "." + arg2.checkjstring();
      return new LuaPackage(name);
    }
  });
  metatable.rawset("__tostring", new ZeroArgFunction() {
    @Override
    public LuaValue call() {
      if (path.isEmpty()) return LuaValue.valueOf("[JPackage Root]");
      return LuaValue.valueOf("[JPackage: " + path + "]");
    }
  });
  metatable.rawset("__metatable", LuaValue.FALSE);
  setmetatable(metatable);
}
 
Example #21
Source File: LuaJsonElement.java    From Lukkit with MIT License 5 votes vote down vote up
private LuaValue getValueFromElement(JsonElement element) {
    if (element.isJsonArray() || element.isJsonObject()) {
        return this.getTableFromElement(element);
    } else if (element.isJsonNull()) {
        return LuaValue.NIL;
    } else if (element.isJsonPrimitive()) {
        JsonPrimitive primitiveValue = element.getAsJsonPrimitive();

        if (primitiveValue.isBoolean()) {
            return LuaValue.valueOf(primitiveValue.getAsBoolean());
        } else if (primitiveValue.isString()) {
            return LuaValue.valueOf(primitiveValue.getAsString());
        } else if (primitiveValue.isNumber()) {
            Number numberValue = primitiveValue.getAsNumber();

            if (numberValue instanceof Double)       return LuaValue.valueOf(numberValue.doubleValue());
            else if (numberValue instanceof Integer) return LuaValue.valueOf(numberValue.intValue());
            else if (numberValue instanceof Short)   return LuaValue.valueOf(numberValue.shortValue());
            else if (numberValue instanceof Long)    return LuaValue.valueOf(numberValue.longValue());
            else if (numberValue instanceof Float)   return LuaValue.valueOf(numberValue.floatValue());
            else if (numberValue instanceof Byte)    return LuaValue.valueOf(numberValue.byteValue());
        }
    } else {
        LuaError error = new LuaError("A LuaJsonElement object was passed an unsupported value other than that supported by LuaJ. Value: " + element.toString());
        LuaEnvironment.addError(error);
        error.printStackTrace();
    }

    return LuaValue.NIL;
}
 
Example #22
Source File: LibFunction.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Bind a set of library functions, with an offset
 * <p/>
 * An array of names is provided, and the first name is bound
 * with opcode = {@code firstopcode}, second with {@code firstopcode+1}, etc.
 *
 * @param env         The environment to apply to each bound function
 * @param factory     the Class to instantiate for each bound function
 * @param names       array of String names, one for each function.
 * @param firstopcode the first opcode to use
 * @see #bind(LuaValue, Class, String[])
 */
protected void bind(LuaValue env, Class factory, String[] names, int firstopcode) {
    try {
        for (int i = 0, n = names.length; i < n; i++) {
            LibFunction f = (LibFunction) factory.newInstance();
            f.opcode = firstopcode + i;
            f.name = names[i];
            env.set(f.name, f);
        }
    } catch (Exception e) {
        throw new LuaError("bind failed: " + e);
    }
}
 
Example #23
Source File: JavaInstance.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public void set(LuaValue key, LuaValue value) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			f.set(m_instance, CoerceLuaToJava.coerce(value, f.getType()));
			return;
		} catch (Exception e) {
			throw new LuaError(e);
		}
	super.set(key, value);
}
 
Example #24
Source File: JavaInstance.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public void set(LuaValue key, LuaValue value) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			f.set(m_instance, CoerceLuaToJava.coerce(value, f.getType()));
			return;
		} catch (Exception e) {
			throw new LuaError(e);
		}
	super.set(key, value);
}
 
Example #25
Source File: LexState.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
void lexerror(String msg, int token) {
        String cid = Lua.chunkid(source.tojstring());
        L.pushfstring(cid + ":" + linenumber + ": " + msg);
        if (token != 0)
            L.pushfstring("syntax error: " + msg + " near " + txtToken(token));
//		throw new LuaError(cid+":"+linenumber+": "+msg);
        throw new LuaError(cid + ":" + linenumber + ": " + msg + " near " + txtToken(token));//modified by song
    }
 
Example #26
Source File: LibFunction.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
/** 
 * Bind a set of library functions, with an offset  
 * <p>
 * An array of names is provided, and the first name is bound 
 * with opcode = {@code firstopcode}, second with {@code firstopcode+1}, etc. 
 * @param env The environment to apply to each bound function 
 * @param factory the Class to instantiate for each bound function
 * @param names array of String names, one for each function.
 * @param firstopcode the first opcode to use  
 * @see #bind(LuaValue, Class, String[])  
 */
protected void bind(LuaValue env, Class factory,  String[] names, int firstopcode ) {
	try {
		for ( int i=0, n=names.length; i<n; i++ ) {
			LibFunction f = (LibFunction) factory.newInstance();
			f.opcode = firstopcode + i;
			f.name = names[i];
			env.set(f.name, f);
		}
	} catch ( Exception e ) {
		throw new LuaError( "bind failed: "+e );
	}
}
 
Example #27
Source File: ScriptBundleLoadDelegate.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * load a prototype
 *
 * @param scriptFile
 * @return
 */
private Prototype loadPrototype( ScriptFile scriptFile) {
    if (LoadState.instance != null && scriptFile != null) {
        try {
            return LoadState.instance.undump(new BufferedInputStream(new ByteArrayInputStream(scriptFile.scriptData)), scriptFile.getFilePath());//TODO 低端机性能上可以进一步优化
        } catch (LuaError error) {
        } catch (Exception e) {
        }
    }
    return null;
}
 
Example #28
Source File: LexState.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
void lexerror( String msg, int token ) {
	String cid = Lua.chunkid( source.tojstring() );
	L.pushfstring( cid+":"+linenumber+": "+msg );
	if ( token != 0 )
		L.pushfstring( "syntax error: "+msg+" near "+txtToken(token) );
	throw new LuaError(cid+":"+linenumber+": "+msg);
}
 
Example #29
Source File: LexState.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
void lexerror( String msg, int token ) {
	String cid = Lua.chunkid( source.tojstring() );
	L.pushfstring( cid+":"+linenumber+": "+msg );
	if ( token != 0 )
		L.pushfstring( "syntax error: "+msg+" near "+txtToken(token) );
	throw new LuaError(cid+":"+linenumber+": "+msg);
}
 
Example #30
Source File: JavaInstance.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public void set(LuaValue key, LuaValue value) {
	if ( jclass == null )
		jclass = JavaClass.forClass(m_instance.getClass());
	Field f = jclass.getField(key);
	if ( f != null )
		try {
			f.set(m_instance, CoerceLuaToJava.coerce(value, f.getType()));
			return;
		} catch (Exception e) {
			throw new LuaError(e);
		}
	super.set(key, value);
}