Closed JakobOvrum closed 13 years ago
Indeed, it's an internal function for pushing functions to the stack. Here's an example on exposing D functions:
import luad.all;
import std.stdio;
int printTimes(int times, string message)
{
for(int i = 0; i <= times; i++)
writeln(message);
return times;
}
void main()
{
auto lua = new LuaState;
lua["printTimes"] = &printTimes;
lua.doString(`
printTimes(3, "hello, world!")
`);
}
Tell me if that helped you :)
Copying your code and building it, I get the following errors: lua_test.d(16): Error: lua["printTimes"] is not an lvalue lua_test.d(16): Error: cannot implicitly convert expression (& printTimes) of type int function(int times, string message) to luad.base.LuaObject
I'm using DMD 2.048 and a clone of the source as of yesterday. Perhaps the alias this forwarding isn't working?
Replacing line 16 with these two lines allows me to compile successfully, although now I have linker issues to work through. LuaTable globals = lua.globals; globals["printTimes"] = &printTimes;
Ah, looks like a bug in DMD with the use of "alias this". I'll look into it. In the mean-time, you can try using lua.set("printTimes", &printTimes);
instead.
If you have linker issues, make sure you're linking with Lua, as well as LuaD. To link with LuaD, you can do one of two things:
To link with Lua, check out the readme.
I'd love to hear if you can get it working :)
It was pretty easy to get it linking. I use DSSS, so I just added a reference to luad in my conf file and added a couple of link pragmas to my module which imports luad: version (build) { pragma(link, "lua5.1"); pragma(link, "DD-luad"); // DSSS adds a DD- prefix for some reason }
Also, the .set syntax works fine for me. Thanks for the timely replies!
Added printTimes example to the examples in the documentation.
Not closing this issue yet though, there aren't enough samples. My current priorities are getting class conversions properly done, then writing more extensive examples.
Closing, there is more documentation now, including samples
Jakob, could you add a sample demonstrating the correct way to expose a D function? I've found pushFunction in conversions.functions, but this is not made available by luad.all so I doubt I'm supposed to be using it directly.