historical paper online

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

historical paper online

Luiz Henrique de Figueiredo
The first paper describing Lua (1.1)

  L. H. de Figueiredo, R. Ierusalimschy, W. Celes, 
  "The design and implementation of a language for extending applications", 
  Proceedings of XXI Brazilian Seminar on Software and Hardware (1994) 273-283.

(of some historical interest) is now available online in HTML at
	
	http://www.tecgraf.puc-rio.br/lua/semish94.html

and mirror locations.
--lhf

Reply | Threaded
Open this post in threaded view
|

constant tables code

Ashley Fryer-2
I've put together a few helper macros for creating tables of constant values
in Lua.  They are useful in situations where you would like scripts to have
access to a set of values without allowing the scripts to modify the values
or the set.

For example, I have used this code to generate a table of the VK_xxx and
DIK_xxx key constants for Windows programming.  It could also be useful for
generating a table of error strings, mathematical constants, etc.

Here's an example...

First, define the table in C.  I find it convenient to group the CONST_TABLE
definitions together with my lua_register() calls.

    int num_var = 42;
    char str_var[] = "this is str_var";

    CONST_TABLE_BEGIN( "MY_TABLE" )
        CONST_NUM_VAR( num_var )
        CONST_STR_VAR( str_var )
        CONST_NUM( "foo",  7 )
        CONST_STR( "bar", "this is bar")
    CONST_TABLE_END


Then, once the table is initialized, you can do this in Lua:

    print( MY_TABLE.num_var )
    42
    print( MY_TABLE.str_var )
    this is str_var
    print( MY_TABLE.foo )
    7
    print( MY_TABLE.bar )
    this is bar

The table is "constant" because all of these things generate an error:

    MY_TABLE = 9
    MY_TABLE.str_var = 5
    MY_TABLE.new_var = "new variable"

The error message is:

    Cannot modify constant table: MY_TABLE

One unfortunate limitation of the macros is that you can't directly use
#defined values.  This is because the C pre-processor isn't reentrant.  So,
for example, this fails:

  #define PI 3.14

  CONST_TABLE_BEGIN( "MATH_TABLE" )
      CONST_NUM( "PI", PI )
  CONST_TABLE_END

My workaround is:

  #define PI 3.14

  CONST_TABLE_BEGIN( "MATH_TABLE" )
      double pi = PI;
      CONST_NUM( "PI", pi )
  CONST_TABLE_END

Perhaps someone can think of a better solution.

The code follows.  Anyone is free to use it any way, and I hope that someone
DOES find it useful.  :-)

ashley

--- cut here ---

#define CONST_TABLE_BEGIN( TABLE_NAME ) \
    {\
    lua_Object table = lua_createtable();\
    lua_pushobject( table );\
    lua_setglobal( TABLE_NAME );\
    lua_dostring( "function tmp() error(\
    \"Cannot modify constant table: " TABLE_NAME "\" ) end" );

#define CONST_NUM( NAME, VALUE) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) NAME );\
    lua_pushnumber( VALUE );\
    lua_settable();

#define CONST_NUM_VAR( NAME ) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) #NAME );\
    lua_pushnumber( NAME );\
    lua_settable();

#define CONST_STR( NAME, VALUE ) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) NAME );\
    lua_pushstring( VALUE );\
    lua_settable();

#define CONST_STR_VAR( NAME ) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) #NAME );\
    lua_pushstring( ( char * ) NAME );\
    lua_settable();

#define CONST_TABLE_END \
    int tag = lua_newtag();\
    lua_pushobject( table );\
    lua_settag( tag );\
    lua_pushobject( table );\
    lua_pushobject( lua_getglobal( "tmp" ) );\
    lua_settagmethod( tag, "settable" );\
    lua_pushobject( table );\
    lua_pushobject( lua_getglobal( "tmp" ) );\
    lua_settagmethod( tag, "setglobal" );\
    }

--- cut here ---


Reply | Threaded
Open this post in threaded view
|

Sv: constant tables code

Erik Hougaard
----- Original Message ----- 
From: Ashley Fryer <[hidden email]>
> I've put together a few helper macros for creating tables of constant values
> in Lua.  They are useful in situations where you would like scripts to have
> access to a set of values without allowing the scripts to modify the values
> or the set.

Very nice, but ths is one of the things that I'm playing with... To make a table (not lua table) so all these constants would get replaced with their actual values on parse time, so there was no need for lua to do the lookup. In a application with many constants that should give a performance gain !

/Erik

Reply | Threaded
Open this post in threaded view
|

RE: constant tables code

Ashley Fryer-2
In reply to this post by Ashley Fryer-2
Thanks to some excellent feedback from lhf, we're already up to version 1.1
of the constant table code.

This version is compatible with plain C, doesn't have the temporary
variable, and uses a lua_beginblock/lua_endblock pair.

Also, lhf points out that the following code DOES work:

#define PI 3.14
CONST_TABLE_BEGIN("MY_TABLE")
    CONST_NUM( "PI", PI )
CONST_TABLE_END

So there's reason to rejoice.  :-)

Thanks, lhf!

ashley

--- cut here ---

#define CONST_TABLE_BEGIN( TABLE_NAME ) \
    lua_beginblock(); {\
    lua_Object err_func;\
    int tag;\
    lua_Object table = lua_createtable();\
    lua_pushobject( table );\
    lua_setglobal( TABLE_NAME );\
    lua_dostring( "return function () error(\
    \"Cannot modify constant table: " TABLE_NAME "\" ) end" );\
    err_func = lua_lua2C( 1 );

#define CONST_NUM( NAME, VALUE) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) NAME );\
    lua_pushnumber( VALUE );\
    lua_settable();

#define CONST_NUM_VAR( NAME ) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) #NAME );\
    lua_pushnumber( NAME );\
    lua_settable();

#define CONST_STR( NAME, VALUE ) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) NAME );\
    lua_pushstring( VALUE );\
    lua_settable();

#define CONST_STR_VAR( NAME ) \
    lua_pushobject( table );\
    lua_pushstring( ( char * ) #NAME );\
    lua_pushstring( ( char * ) NAME );\
    lua_settable();

#define CONST_TABLE_END \
    tag = lua_newtag();\
    lua_pushobject( table );\
    lua_settag( tag );\
    lua_pushobject( table );\
    lua_pushobject( err_func );\
    lua_settagmethod( tag, "settable" );\
    lua_pushobject( table );\
    lua_pushobject( err_func );\
    lua_settagmethod( tag, "setglobal" );\
    } lua_endblock();

--- cut here ---