[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:22:16 +0200
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:
/* 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,"-2 ; getVar");
#error cannot handle LOADK 1 -3 ; "X
lua_call(L,0,2);
lua_setglobal(L,"-1 ; x");
#error cannot handle NEWTABLE 0 0 0
lua_setglobal(L,"-4 ; T");
#error cannot handle NEWTABLE 0 0 2
lua_pushcclosure(L,F1,0);
lua_settable(L,-2);
lua_remove(L,-2);
lua_pushcclosure(L,F2,1);
lua_settable(L,-2);
lua_remove(L,-2);
lua_getglobal(L,"-7 ; setmetatable");
lua_getglobal(L,"-4 ; T");
#error cannot handle MOVE 3 0
lua_call(L,-1,3);
lua_getglobal(L,"-4 ; T");
lua_gettable(L,-2);
lua_remove(L,-2);
lua_setglobal(L,"-1 ; x");
return 3;
static int F1(lua_State *L)
{
lua_getglobal(L,"-1 ; getVar");
#error cannot handle MOVE 3 1
lua_call(L,0,LUA_MULTRET);
return lua_gettop(L);
return -2;
return 2;
static int F2(lua_State *L)
{
lua_getglobal(L,"-1 ; error");
#error cannot handle LOADK 4 -2 ; "T is read-only
#error cannot handle LOADK 5 -3 ; 2
lua_call(L,0,3);
return 6;
/* function proptotypes */
static int F1(lua_State *L);
static int F2(lua_State *L);
}
On Tue, Jul 20, 2010 at 12:33 PM, Thierry <th.douez@gmail.com> wrote:
> Hello All,
>
> Having start learning Lua for few days, I'm stuck ( or let say don't know
> how to start ) with this problem :
>
> Here is what is working the way I want ( all in Lua ) :
>
> --first :
>
> x = getVar( "X" )
>
> --getVar() is a C function I made which read
> -- some prefs data in my business environment.
>
> --then :
>
> 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"]
> ------------------------
>
> What I would like to do is to write these few lines in C.
> The table T should be a global var ( no library, modules )
>
> Any pointers, link or pseudo-code to start would be very much appreciated
>
> Regards,
> Thierry
>
>
>