Java源码示例:org.luaj.vm2.LuaError

示例1
/**
 * 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;
    }
}
 
示例2
/**
 * 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;
    }
}
 
示例3
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);
}
 
示例4
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);
}
 
示例5
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);
}
 
示例6
@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());
  }
}
 
示例7
/**
 * 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);
    }
}
 
示例8
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);
}
 
示例9
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);
}
 
示例10
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
    }
 
示例11
/**
 * 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;
}
 
示例12
/** 
 * 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 );
	}
}
 
示例13
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);
}
 
示例14
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);
}
 
示例15
/** 
 * 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 );
	}
}
 
示例16
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);
}
 
示例17
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);
}
 
示例18
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);
}
 
示例19
/**
 * 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 );
	}
}
 
示例20
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);
}
 
示例21
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);
}
 
示例22
public void testNoFactory() {
	JavaClass c = JavaClass.forClass(A.class);
	try {
		c.call();
		fail( "did not throw lua error as expected" );
	} catch ( LuaError e ) {
	}
}
 
示例23
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 ) {
	}
}
 
示例24
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 ) {
	}
}
 
示例25
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 ) {}
}
 
示例26
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() );
	}
}
 
示例27
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);
    }
}
 
示例28
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;
}
 
示例29
public void runScriptFile(@NotNull String fileName) {
	try {
		globals.loadfile(fileName).call();
	} catch (LuaError err) {
		reportLuaError(err);
	}
}
 
示例30
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);
}