lua enhancements

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

lua enhancements

Dan Marks
I have been doing quite a bit of work with Lua lately, and I have
some suggestions.  We are doing research in optical and signal
processing and have found lua to be excellent as a scripting language
to script experiments.  We have a software package called 3Dimager
that runs various types of hardware and experimental procedures
from simple lua scripts.

I have found a bug in lua, fairly minor.  When compiling and
running Lua under Win32, if you put a $debug directive at the top
of the file, it gives an error.  This is because the part of the
code that parses the directives does not know how to handle a
CR rather than a LF at the end of the line.  I forgot what my
one line patch to fix this was, but it was minor.

I have also made some enhancements.  We needed to make sure all of
the global variables were cleared out between calls to lua scripts,  so 
I added lua_clearvar, which is a command that clears out all of the global
variables (it adds the built in commands back in at the end):

void lua_clearvar(void)
{
   Word next;
   int loop,gotany;

   for (loop=0;loop<100;loop++) {
     next = 0;
     gotany=0;	 
     while (next < lua_ntable) {
		 if (s_ttype(next) != LUA_T_NIL) {
		   lua_beginblock();
		   lua_pushnil();
		   lua_rawsetglobal(lua_table[next].varname->str);
		   lua_endblock();
		   gotany=1;
		 }
		 next++;
	 }
	 if (!gotany)
		 break;
   }	
   luaI_predefine();
}

Unfortunately, this does not reset the list of tags or tag methods to
the default state.  I know this is a hack, so a real way to reset the
state of the interpreter to the totally cleared state would be nice
(which would garbage collect all of the data in the process).  But
this seems to work.

Also, I have modified the iolib.c library, so that when all the references
to a file object are lost, the file is automatically closed by a garbage
collect tag method.  If a file is closed manually, the pointer associated
with the file is reassigned a new tag number that is not the regular
iolib file tag number.  This ensures that dead pointers are never used.
I have also added feof() fopen() fclose() seek() and fflush(), and retained
compatibility with the old commands while adding new forms to the
read() and write() commands so that they can accept file pointers as
arguments (rather that just use _INPUT and _OUTPUT).  If a file is
fclosed that is currently either _INPUT or _OUTPUT, the variables
_INPUT or _OUTPUT are set to stdin or stdout, respectively.  I would like
to see some of these changes considered because I do not want script
users to be able to accidentally use a bad pointer. 

Anyways, lua is great, and it is very much facilitating our work.


Dan Marks
[hidden email]