Web Lua

lua-users home
wiki

About

This is a version of Lua running behind a web interface [1].

Version

Hit the version button to tell you what version of Lua it uses. e.g. It will currently display:


Lua 4.0  Copyright (C) 1994-2000 TeCGraf, PUC-Rio

Running Lua code

Try typing


print("hello world")

into the Lua Code area and hitting the lua button.

Lua compiled code

Lua compiles source code into an intermediate format to execute. If you hit the luac button you will see:


luac generates the follow output:



main <0:@/tmp/abcdefg> (4 instructions/16 bytes at 0x12345678)

0 params, 2 stacks, 0 locals, 2 strings, 0 numbers, 0 functions, 3 lines

     1	[2]	GETGLOBAL  	0	; print

     2	[2]	PUSHSTRING 	1	; "hello world"

     3	[2]	CALL       	0 0

     4	[2]	END

You can see elements of the print("hello world") code in the VM code. We won't go into detail about this here though.

Generating C code from Lua

If we hit the lua2c button you'll see:


lua2c generates the following:



static int MAIN(lua_State *L)

{

 lua_getglobal(L,"print");

 lua_pushstring(L,"hello world");

 lua_call(L,1,0);

 return 0;

}

A Lua script (called lua2c.lua) has generated the above code from the output of luac. If you want C and Lua to interact you use the Lua C API. To save time doing this, you can enter what you want to do in Lua code window and hit the lua2c button. This generates code which you can cut and paste into your C code.

Example

Lets say you want to read the value foo.x, where foo is a table containing a number x. We could type value = foo.x into WebLua and lua2c would give us:


static int MAIN(lua_State *L)

{

 lua_getglobal(L,"foo");

 lua_pushstring(L,"x");

 lua_gettable(L,-2);

 lua_remove(L,-2);

 lua_setglobal(L,"value");

 return 0;

}

Instead of setting the variable value we can cut and paste the code we need and grab the number using the Lua API, e.g.

 lua_getglobal(L,"foo");

 lua_pushstring(L,"x");

 lua_gettable(L,-2);

 lua_remove(L,-2);

 double value = lua_tonumber(L,-1); /* code we added */


RecentChanges · preferences
edit · history
Last edited April 6, 2003 12:24 am GMT (diff)