View previous topic :: View next topic |
Author |
Message |
clayasaurus
Joined: 21 May 2004 Posts: 857
|
Posted: Mon Mar 28, 2005 12:53 pm Post subject: lua bindings |
|
|
added initial lua bindings to the trunk.
they are not perfect, but they are a good start : )
if you find something wrong with them, feel free to change them.
www.lua.org
edit: updated the bindings to use typedef instead of alias for lua_CFunction and other function definitions.
Last edited by clayasaurus on Thu Dec 22, 2005 6:38 pm; edited 2 times in total |
|
Back to top |
|
|
clayasaurus
Joined: 21 May 2004 Posts: 857
|
Posted: Wed Mar 30, 2005 12:26 am Post subject: |
|
|
sample function implementation
Code: |
import lua.lua,
lua.lauxlib,
lua.lualib;
import std.stdio, std.date;
/* the Lua interpreter */
lua_State* L;
int luaadd ( int x, int y )
{
int sum;
/* the function name */
lua_getglobal(L, "add");
/* the first argument */
lua_pushnumber(L, x);
/* the second argument */
lua_pushnumber(L, y);
/* call the function with 2
arguments, return 1 result */
lua_call(L, 2, 1);
/* get the result */
sum = cast(int)lua_tonumber(L, -1);
lua_pop(L, 1);
return sum;
}
extern(C)
{
static int average(lua_State *L)
{
/* get number of arguments */
int n = lua_gettop(L);
double sum = 0;
int i;
/* loop through each argument */
for (i = 1; i <= n; i++)
{
/* total the arguments */
sum += lua_tonumber(L, i);
}
/* push the average */
lua_pushnumber(L, sum / n);
/* push the sum */
lua_pushnumber(L, sum);
/* return the number of results */
return 2;
}
static int dtime(lua_State *L)
{
d_time time = getUTCtime();
char *str = toDateString(time);
lua_pushstring(L, str);
return 1;
}
}
int main ( )
{
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
lua_baselibopen(L);
/* register our function */
lua_register(L, "average", cast(lua_CFunction)&average);
lua_register(L, "dtime", cast(lua_CFunction)&dtime);
/* run the script */
lua_dofile(L, "test.lua");
sum = luaadd(2,2); // makes sum = 4
writefln("sum is ", sum)
/* cleanup Lua */
lua_close(L);
return 0;
}
|
test.lua
Code: |
print("hello lua world...")
-- test2
function add ( x, y )
return x + y
end
-- test3
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)
time = dtime()
print("The time is ", time)
|
results...
hello lua world...
The average is 30
The sum is 150
The time is Wed Mar 30 2005
sum is 4 |
|
Back to top |
|
|
xammy
Joined: 01 Feb 2007 Posts: 6 Location: Magdeburg, Germany
|
Posted: Wed Jul 23, 2008 1:57 pm Post subject: Lua library |
|
|
I've done some work on a Lua library with the following features:
* lua states and buffers are wrapped in OOP style.
* protected calls are made exception safe and the call chain-information in put into the exception trace. using standard C lua, the app would segfault on exception, because of setjmp () and getjmp () in the lua lib itself.
* you can register functions, methods and class-constructors directly to a given LuaState via a mixin.
This reduces the given example to the following code (using Tango, but that's only interesting for Date/Time and I/O stuff:
Code: | import lua.all;
import tango.io.Stdout, tango.text.locale.Locale, tango.time.Clock;
LuaState state;
int luaadd (int x, int y)
{
int sum;
state.getGlobal("add"); // push the internal lua add function
state.pushNumber (x); // push x
state.pushNumber (y); // push y
state.call (false, 2, 1); // do an unprotected call, push 2 arguments and expect 1 result
sum = state.toInteger (-1); // get the result
state.pop ();
return sum;
}
int average (LuaState L)
{
int n = L.getTop ();
double sum = 0.0;
int i;
for (i = 1; i <= n; i++)
{
sum += L.toNumber (i); // add each
}
L.pushNumber (sum / n); // push the average
L.pushNumber (sum); // push the sum
return 2; // number of return values
}
int dtime (LuaState L)
{
auto locale = new Locale;
L.pushString (locale ("{:ddd MMMM dd yyyy}", Clock.now));
return 1;
}
int main (char[][] args)
{
state = new LuaState (); // creates state and loads BASE module.
// we register both D functions in lua
mixin (mixinLuaRegisterFunction ("state", "Test.average", "average"));
mixin (mixinLuaRegisterFunction ("state", "Test.dtime", "dtime"));
state.doFile (true, "test.lua");
int sum = luaadd (2, 2);
Stdout.formatln ("sum is {}", sum);
return 0;
} |
Anyone interested in that, so I might add it to the bindings ?! |
|
Back to top |
|
|
clayasaurus
Joined: 21 May 2004 Posts: 857
|
Posted: Wed Jul 23, 2008 5:48 pm Post subject: |
|
|
If you think it would be useful to someone like yourself in the future, go ahead and add it. Nobody controls the bindings project, people just put useful code there. Plus, the D community is rather small, so I'd be surprised if anyone else answered you.
So sure, go ahead and add it. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|