0

I tried the utility method provided by luaj to call a lua file with command line args (this one http://lua-users.org/wiki/SourceCodeFormatter)

Globals globals = JsePlatform.standardGlobals();        
String script ="src/codeformatter.lua";
File f = new File(script);
LuaValue chunk = globals.loadfile(f.getCanonicalPath());
List<String> argList = Arrays.asList("--file","test.lua");                          
JsePlatform.luaMain(chunk, argList.toArray(new String[argList.size()]));

However i always get attempt to call nil where the code tries to access the arg table ( while i < table.getn(arg) do) - i tried other examples and they all result in the same error - luaj does not seem to set the "arg" table correctly - even a simply print arg[1] will not work.

Steve
  • 738
  • 1
  • 9
  • 30
  • Which lua version are you using? `table.getn` was removed. `arg` is not the default name of vararg parameters – hjpotter92 Nov 26 '15 at 10:39
  • Im using http://www.luaj.org/luaj/3.0/README.html which is based on lua 5.2 - nevertheless - even args[1] does not work – Steve Nov 26 '15 at 13:29
  • at the beginning of `codeformatter` file, put `local arg = {...}` and it should work. – hjpotter92 Nov 26 '15 at 14:05
  • did not help, still getting 30 attempt to call nil – Steve Nov 27 '15 at 13:40
  • please bear in mind that while the reference implementation's REPL provides a table `_G.arg`, the standard doesn't state anything about it anywhere, so there's actually no "correct" way of doing it. just do it any way you'd like and use that. often there's also just `local args={...}` at the beginning of a file, but that doesn't work when loaded using `require` – nonchip Nov 29 '15 at 16:35
  • Ok but that does not help me - what changes do i do need todo to make the script from the official lua page working with LuaJ ? Or if you know, i take any other Lua auto code formatter which i can use in java – Steve Nov 29 '15 at 18:58
  • NM - got it to work a combination of local args={} and replacing getn with #arg made it work - however the code formatter does not seem to do when i expected - nothing is really done – Steve Nov 29 '15 at 19:06

2 Answers2

0

LuaJ does not support table.getn anymore because it got removed in lua 5.1 - replace every occurances of table.getn with #varname - and init the args array with ocal args={...} at the top made it work.

Still, the code formatter does not really do what i expected it todo

Steve
  • 738
  • 1
  • 9
  • 30
0

There are two issues:

  • calls to table.getn(arg) should be replaced with #arg
  • the chunk's environment is not set up properly by luaj 3.0.1 so arg isn't set

However, as a workaround, you can capture the inputs using the varargs "..." syntax by adding a line at the top of codeformatter.lua such as

arg = {...}

Here is a code snippet to illustrate:

Globals globals = JsePlatform.standardGlobals();   
LuaValue chunk = globals.load(
        "arg = {...};" +
        "print(#arg, arg[1], arg[2])");
JsePlatform.luaMain(chunk, new String[] {"--file","test.lua"});

Produces output:

2   --file  test.lua
Jim Roseborough
  • 201
  • 1
  • 4