[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: writing this Lua Code in C ( newbie )
- From: Valerio Schiavoni <valerio.schiavoni@...>
- Date: Tue, 20 Jul 2010 13:30:22 +0200
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.
>