示例#1
0
 /** Runs a chunk of Lua code from an input stream. */
 protected void runChunk(InputStream in) throws IOException {
   try {
     long start = System.nanoTime();
     luaState.setTop(0);
     luaState.load(in, "console");
     luaState.call(0, LuaState.MULTRET);
     long stop = System.nanoTime();
     for (int i = 1; i <= luaState.getTop(); i++) {
       if (i > 1) {
         System.out.print(", ");
       }
       switch (luaState.type(i)) {
         case BOOLEAN:
           System.out.print(Boolean.valueOf(luaState.toBoolean(i)));
           break;
         case NUMBER:
         case STRING:
           System.out.print(luaState.toString(i));
           break;
         default:
           System.out.print(luaState.typeName(i));
       }
     }
     System.out.print("\t#msec=");
     System.out.print(String.format("%.3f", (stop - start) / 1000000.0));
     System.out.println();
   } catch (LuaRuntimeException e) {
     e.printLuaStackTrace();
     e.printStackTrace();
   } catch (LuaException e) {
     System.err.println(e.getMessage());
   }
 }
示例#2
0
  /** Tests the call of a Java function which throws a Lua runtime exception. */
  @Test
  public void testLuaRuntimeException() throws Exception {
    // Push function
    luaState.pushJavaFunction(new LuaRuntimeExceptionFunction());

    // Push arguments
    LuaRuntimeException luaRuntimeException = null;
    try {
      luaState.call(0, 0);
    } catch (LuaRuntimeException e) {
      luaRuntimeException = e;
    }
    assertNotNull(luaRuntimeException);
    Throwable cause = luaRuntimeException.getCause();
    assertNotNull(cause);
    assertTrue(cause instanceof LuaRuntimeException);
  }
示例#3
0
  /** Tests the call of a Lua function which invokes the Lua error function. */
  @Test
  public void testLuaError() throws Exception {
    // Load program
    luaState.openLibs();
    StringBuffer sb = new StringBuffer();
    sb.append("function A ()\n");
    sb.append("    B()\n");
    sb.append("end\n");
    sb.append("\n");
    sb.append("function B ()\n");
    sb.append("    C()\n");
    sb.append("end\n");
    sb.append("\n");
    sb.append("function C ()\n");
    sb.append("    error(\"msg\")\n");
    sb.append("end\n");
    sb.append("\n");
    sb.append("A()\n");
    luaState.load(sb.toString(), "=testLuaError");

    // Run
    LuaRuntimeException luaRuntimeException = null;
    try {
      luaState.call(0, 0);
    } catch (LuaRuntimeException e) {
      luaRuntimeException = e;
    }
    assertTrue(luaRuntimeException.getMessage().endsWith("msg"));
    LuaStackTraceElement[] luaStackTrace = luaRuntimeException.getLuaStackTrace();
    assertEquals(5, luaStackTrace.length);
    assertEquals(new LuaStackTraceElement("error", null, -1), luaStackTrace[0]);
    assertEquals(new LuaStackTraceElement("C", "testLuaError", 10), luaStackTrace[1]);
    assertEquals(new LuaStackTraceElement("B", "testLuaError", 6), luaStackTrace[2]);
    assertEquals(new LuaStackTraceElement("A", "testLuaError", 2), luaStackTrace[3]);
    assertEquals(new LuaStackTraceElement(null, "testLuaError", 13), luaStackTrace[4]);
  }