lua-users home
lua-l archive

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


>From nr@cs.virginia.edu Mon Jun 22 23:52:23 1998
>
>There are two changes I'd like to see in Lua, both of which would make
>it a little easier to connect C code to Lua.
>
>I try to be careful in my code to use const char * when I know I'm
>dealing with immutable strings.  Unfortunately, many Lua functions use
>parameter types of char *, even when const char * would be correct, so
>I get a slew of warning messages.  It would be nice if the signatures
>were updated properly.

We have talked about this once and somehow decided that it was more trouble
than benefit. But this was a while ago.

>I believe that *all* strings are immutable in
>Lua, so that all instances of char * could be replaced with const char*.

There maybe one or two cases where strings returned by Lua to C are mutable,
ie., strings inside Lua.
But Lua certainly does not need to change strings in host space.
In this sense, you're right.

>The other change I'd like to see is minor: I'd like the C version of
>lua_error to have the same signature as printf.  This should 
>relatively easy to implement in ANSI C using <stdarg.h> and vsprintf.

I'm glad to say that this is available in 3.1. It's called luaL_verror.
It's not part of the official API because it's a convenience function,
written enirely with the API, with no inside information. See lauxlib.c.
Just for reference -- or if you're using a previous version, here it is:

void luaL_verror (char *fmt, ...)
{
  char buff[500];
  va_list argp;
  va_start(argp, fmt);
  vsprintf(buff, fmt, argp);
  va_end(argp);
  lua_error(buff);
}

Unfortunately, until the new ANSI C is out, there is no way to avoid the
fixed size buffer and the danger for overflow in vsprintf.
--lhf