Beispiel #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());
   }
 }
Beispiel #2
0
  /**
   * Creates a new instance with the specified command line arguments. The arguments are passed to
   * Lua as the <code>argv</code> global variable.
   *
   * @param args
   */
  public LuaConsole(String[] args) {
    luaState = new LuaState();

    // Process arguments
    luaState.newTable(args.length, 0);
    for (int i = 0; i < args.length; i++) {
      luaState.pushString(args[i]);
      luaState.rawSet(-2, i + 1);
    }
    luaState.setGlobal("argv");

    // Open standard libraries
    luaState.openLibs();

    // Set buffer mode
    luaState.load("io.stdout:setvbuf(\"no\")", "setvbuf");
    luaState.call(0, 0);
    luaState.load("io.stderr:setvbuf(\"no\")", "setvbuf");
    luaState.call(0, 0);
  }