lua-users home
lua-l archive

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


Hello Thierry,
using an updated lua to c transformation tool (git clone
git://github.com/davidm/lua2c.git), here's the sources for Lua 5.1.
since your lua code doesn't work, the C code doesn't work either.

/* WARNING: This file was automatically generated by lua2c. */

#ifdef __cplusplus
extern "C" {
#endif
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#ifdef __cplusplus
}
#endif
#include <stdio.h>
#include <stdlib.h>


#include <string.h>


#include <assert.h>

/* function( t, k) */
static int lcf2 (lua_State * L) {
  enum { lc_nformalargs = 2 };
  lua_settop(L,2);

  /* return getVar( k ) */
  const int lc1 = lua_gettop(L);
  lua_getfield(L,LUA_ENVIRONINDEX,"getVar");
  lua_pushvalue(L,2);
  lua_call(L,1,LUA_MULTRET);
  return (lua_gettop(L) - lc1);
  assert(lua_gettop(L) == 2);
}


/* function(t,k,v) */
static int lcf3 (lua_State * L) {
  enum { lc_nformalargs = 3 };
  lua_settop(L,3);

  /* error("T is read-only", 2) */
  lua_getfield(L,LUA_ENVIRONINDEX,"error");
  lua_pushliteral(L,"T is read-only");
  lua_pushnumber(L,2);
  lua_call(L,2,0);
  assert(lua_gettop(L) == 3);
  return 0;
}


/* name: (main)
 * function(...) */
static int lcf_main (lua_State * L) {
  enum { lc_nformalargs = 0 };
  const int lc_nactualargs = lua_gettop(L);
  const int lc_nextra = (lc_nactualargs - lc_nformalargs);

  /* x  = getVar( "X" ) */
  lua_getfield(L,LUA_ENVIRONINDEX,"getVar");
  lua_pushliteral(L,"X");
  lua_call(L,1,1);
  lua_setfield(L,LUA_ENVIRONINDEX,"x");
  assert(lua_gettop(L) - lc_nextra == 0);

  /* T = {} */
  lua_newtable(L);
  lua_setfield(L,LUA_ENVIRONINDEX,"T");
  assert(lua_gettop(L) - lc_nextra == 0);

  /* local mT = {
   *    __index = function( t, k)
   *    return getVar( k )
   * end,
   *    __newindex = function(t,k,v)
   *    error("T is read-only", 2)
   * end
   * } */
  lua_createtable(L,0,2);
  lua_pushliteral(L,"__index");
  lua_pushcfunction(L,lcf2);
  lua_rawset(L,-3);
  lua_pushliteral(L,"__newindex");
  lua_pushcfunction(L,lcf3);
  lua_rawset(L,-3);
  assert(lua_gettop(L) - lc_nextra == 1);

  /* setmetatable( T, mT) */
  lua_getfield(L,LUA_ENVIRONINDEX,"setmetatable");
  lua_getfield(L,LUA_ENVIRONINDEX,"T");
  lua_pushvalue(L,(1 + lc_nextra));
  lua_call(L,2,0);
  assert(lua_gettop(L) - lc_nextra == 1);

  /* x = T[ "x"] */
  lua_getfield(L,LUA_ENVIRONINDEX,"T");
  lua_pushliteral(L,"x");
  lua_gettable(L,-2);
  lua_remove(L,-2);
  lua_setfield(L,LUA_ENVIRONINDEX,"x");
  assert(lua_gettop(L) - lc_nextra == 1);
  return 0;
}


/* from lua.c */
static int traceback (lua_State *L) {
  if (!lua_isstring(L, 1))  /* 'message' not a string? */
    return 1;  /* keep it intact */
  lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  if (!lua_istable(L, -1)) {
    lua_pop(L, 1);
    return 1;
  }
  lua_getfield(L, -1, "traceback");
  if (!lua_isfunction(L, -1)) {
    lua_pop(L, 2);
    return 1;
  }
  lua_pushvalue(L, 1);  /* pass error message */
  lua_pushinteger(L, 2);  /* skip this function and traceback */
  lua_call(L, 2, 1);  /* call debug.traceback */
  return 1;
}


static void lc_l_message (const char *pname, const char *msg) {
  if (pname) fprintf(stderr, "%s: ", pname);
  fprintf(stderr, "%s\n", msg);
  fflush(stderr);
}

static int lc_report (lua_State *L, int status) {
  if (status && !lua_isnil(L, -1)) {
    const char *msg = lua_tostring(L, -1);
    if (msg == NULL) msg = "(error object is not a string)";
    /*FIX-IMROVE:progname*/
    lc_l_message("lua", msg);
    lua_pop(L, 1);
  }
  return status;
}

static int lc_docall (lua_State *L, int narg, int clear) {
  int status;
  int base = lua_gettop(L) - narg;  /* function index */
  lua_pushcfunction(L, traceback);  /* push traceback function */
  lua_insert(L, base);  /* put it under chunk and args */
  /*FIX? signal(SIGINT, laction); */
  status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  /*FIX? signal(SIGINT, SIG_DFL); */
  lua_remove(L, base);  /* remove traceback function */
  /* force a complete garbage collection in case of errors */
  if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
  return status;
}

static int lc_dofile (lua_State *L, const char *name) {
  int status = luaL_loadfile(L, name) || lc_docall(L, 0, 1);
  return lc_report(L, status);
}

static int lc_dostring (lua_State *L, const char *s, const char *name) {
  int status = luaL_loadbuffer(L, s, strlen(s), name) || lc_docall(L, 0, 1);
  return lc_report(L, status);
}

static int lc_handle_luainit (lua_State *L) {
  const char *init = getenv(LUA_INIT);
  if (init == NULL) return 0;  /* status OK */
  else if (init[0] == '@')
    return lc_dofile(L, init+1);
  else
    return lc_dostring(L, init, "=" LUA_INIT);
}


typedef struct {
  int c;
  const char ** v;
} lc_args_t;


/* create global arg table */
static void lc_createarg(lua_State * L, const lc_args_t * const args) {
  int i;
  lua_newtable(L);
  for (i=0; i < args->c; i++) {
    lua_pushstring(L, args->v[i]);
    lua_rawseti(L, -2, i);
  }
  lua_setglobal(L, "arg");
}


static int lc_pmain(lua_State * L) {
  luaL_openlibs(L);

  const lc_args_t * const args = (lc_args_t*)lua_touserdata(L, 1);
  lc_createarg(L, args);

  lua_pushcfunction(L, traceback);

  const int status1 = lc_handle_luainit(L);
  if (status1 != 0) return 0;

  /* note: IMPROVE: closure not always needed here */
  lua_newtable(L); /* closure table */
  lua_pushcclosure(L, lcf_main, 1);
  int i;
  for (i=1; i < args->c; i++) {
    lua_pushstring(L, args->v[i]);
  }
  int status2 = lua_pcall(L, args->c-1, 0, -2);
  if (status2 != 0) {
    const char * msg = lua_tostring(L,-1);
    if (msg == NULL) msg = "(error object is not a string)";
    fputs(msg, stderr);
  }
  return 0;
}


int main(int argc, const char ** argv) {
  lc_args_t args = {argc, argv};
  lua_State * L = luaL_newstate();
  if (! L) { fputs("Failed creating Lua state.", stderr); exit(1); }

  int status = lua_cpcall(L, lc_pmain, &args);
  if (status != 0) {
    fputs(lua_tostring(L,-1), stderr);
  }

  lua_close(L);
  return 0;
}





On Tue, Jul 20, 2010 at 1:30 PM, Valerio Schiavoni
<valerio.schiavoni@gmail.com> wrote:
> For Thierry's convenience, here's the code, using Lua 4.0:
>
> /* This C code was generated by lua2c from the Lua code below.
>
>
> x  = getVar( "X" )
> T = {}
> local mT = {
>   __index = function( t, k)
>   return getVar( k )
> end,
>   __newindex = function(t,k,v)
>   error("T is read-only", 2)
> end
> }
> setmetatable( T, mT)
>
> x = T[ "x"]
> */
> static int MAIN(lua_State *L)
> {
>  lua_getglobal(L,"getVar");
>  lua_pushliteral(L,"X");
>  lua_call(L,1,1);
>  lua_setglobal(L,"x");
>  lua_newtable(L);
>  lua_setglobal(L,"T");
>  lua_newtable(L);
>  lua_pushliteral(L,"__index");
>  lua_pushcclosure(L,F1,0);
>  lua_pushliteral(L,"__newindex");
>  lua_pushcclosure(L,F2,0);
>  lua_settable(L,-5);
>  lua_settable(L,-3);
>  lua_getglobal(L,"setmetatable");
>  lua_getglobal(L,"T");
>  lua_pushvalue(L,1);
>  lua_call(L,2,0);
>  lua_getglobal(L,"T");
>  lua_pushliteral(L,"x");
>  lua_gettable(L,-2);
>  lua_remove(L,-2);
>  lua_setglobal(L,"x");
>  return 0;
> }
>
> static int F1(lua_State *L)
> {
>  lua_getglobal(L,"getVar");
>  lua_pushvalue(L,2);
>  lua_call(L,1,LUA_MULTRET);
>  return lua_gettop(L);
>  return 0;
> }
>
> static int F2(lua_State *L)
> {
>  lua_getglobal(L,"error");
>  lua_pushliteral(L,"T is read-only");
>  lua_pushnumber(L,2);
>  lua_call(L,2,0);
>  return 0;
> }
>
> /* function proptotypes */
> static int F1(lua_State *L);
> static int F2(lua_State *L);
>
>
> On Tue, Jul 20, 2010 at 1:24 PM, Luiz Henrique de Figueiredo
> <lhf@tecgraf.puc-rio.br> wrote:
>>> The following is generated by lua2c
>>> (http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lua2c) .
>>>  It expects Lua 4.0 bytecode, so it's not working code, but you might
>>> use it as basis perhaps:
>>
>> Better compile the Lua code with Lua 4 then before running lua2c.
>>
>