lördag 20 mars 2010

argv, arrrgh (lua port endeavours)

In my attempts to specify a file for my ported version of Lua to read and "execute" I have come upon unexpected problems. Or, I might have misunderstood the argument list to lua.exe. Thing is, that if I call lua like this:

char* argv[MAX_ARGS];

argv[1] = 0;
lua_main(1, argv);

Lua fires up the interactive mode, and I can give it lua lines to execute (on target, whoa) through stdin dialog text fields in the IDE/emulator. I can make tables, assign table elements, add things to other things, et cetera, without any problems. No memory problems at all.

In this scenario however:

char* filename = "aloha.lua";

argv[1] = filename;
argv[2] = 0;
lua_main(2, argv);

...lua crashes, saying there isn't enough memory.

Possible causes, I guess, are:
  1. I made some mistake handling argv. Maybe Lua actually does something with argv[0]. Since I leave it uninitialized, it's not really nice to play with. Maybe I should set argc to 1, since the program name is presupposed anyway.
  2. Lua needs more memory reading from a file than from the interactive mode. Not very likely though. Reading from stdin or from a file should be quite analoguous. Also, I moved system heap from internal memory to external memory and increased heap stack about a hundred times, counting in megabytes. Should've put an end to lack of memory if it really was that problem.
  3. "Something goes wrong in there." Which, eventually (a couple of micro or milliseconds later), makes lua think it's out of memory. Hmmm.... coming to think of it, the increase in memory should've led to a noticeable (even by human standards) extra time before memory ran out (supposing wild allocation all over the place!). So, maybe Lua did not experience an increase in memory. So, maybe Lua does not take its memory from the heap in the straight forward manner I've presumed? Maybe memory resources are set up in luaconfig.h or similar...? Ah... the plot thickens.
  4. I misunderstood argument passing. Maybe the file name is supposed to hold pre-compiled lua byte code or something. But then why would it work when passing lua on Windows a file holding ASCII representation of intended functionality?Nah, this theory I tentatively write off.
Anyway, it's good fun tracking the cause of this. I have a feeling I might take an unexpected, non-linear step up the computer science learning curve, doing this, haha.




Inga kommentarer:

Skicka en kommentar