lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This post is about begging for some small changes in 5.2.

I have ported Lua to a lot of different platforms and new platforms keep appearing. On embedded systems it is not uncommon for there only to be a subset of the standard C runtime library and one can usually bet on not having stdio.

Porting Lua is usually not difficult. The list of things to do reads like, rewrite or omit liolib.c, loslib.c, lua.c and luac.c. Create dummy header files for the missing RTL headers e.g. errno.h, create stubs for a few missing functions e.g. localeconv(), getenv(), use luaconf.h to define a few str???() functions etc. My ambition here is have some changes that reduce the likelihood of having to change loadlib.c, ldblib.c and lauxlib.c.

I appreciate the introduction of luai_writestring() in 5.2. It removes the need to modify print() when porting Lua. A simple luaconf.h change instead. Thank you.

We could make this even easier with a luai_puterrstring() and luai_getconsolestring() which would be something like:

#define luai_puterrstring(s)	fputs((s), stderr)
#define luai_getconsolestring(b,l)	fgets((b), sizeof(char), (l), stderr)

Hence
ldblib.c (342)     fputs("lua_debug> ", stderr);
ldblib.c (343)     if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
ldblib.c (348)       fputs(lua_tostring(L, -1), stderr);
ldblib.c (349)       fputs("\n", stderr);

would become something like:

 luai_puterrstring("lua_debug> ");
 if (luai_getconsolestring(buffer, sizeof(buffer)) == 0 ||
   luai_puterrstring(lua_tostring(L, -1));
   luai_puterrstring("\n");

This would end my perpetual changes to ldblib.c, even if the macros evaluate to do nothing.



In lauxlib.c lines 497 thru 568 luaL_loadfile() et al
This seems to be an always change it piece of code which is not always easy due to the ungetc(). I welcome suggestions for removing the stdio dependency in this code.


and
lauxlib.c (754) fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
                   lua_tostring(L, -1));

Again the problem, no stdio or in the windows GUI case stderr goes nowhere.

Could we macroize this to :
  luai_puterrstring("PANIC: unprotected error in call to Lua API (");
  luai_puterrstring(lua_tostring(L, -1));
  luai_puterrstring(")\n");

and in loadlib.c line ~388 (my pet hate)
static int readable (const char *filename) {
  FILE *f = fopen(filename, "r");  /* try to open file */
  if (f == NULL) return 0;  /* open failed */
  fclose(f);
  return 1;
}

This could become luai_readable() in luaconf.h.


The porting guide then suddenly gets simpler with no changes to loadlib.c, ldblib.c or lauxlib.c required. It all gets done from luaconf.h.


David Burgess