Comments (8)
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.
from luad.
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 :)
from luad.
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?
from luad.
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;
from luad.
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:
- Compile all the LuaD files, including the luad/c package, on the command line (or in your IDE) with your project. This will cause object files (compiled code) to be emitted for LuaD as well as your project and automatically linked.
- Compile all the LuaD files, including the luad/c package, as a static library. This is done with the -lib option with DMD. Then compile your project with the generated .lib included in the command line or in your IDE.
To link with Lua, check out the readme.
I'd love to hear if you can get it working :)
from luad.
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!
from luad.
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.
from luad.
Closing, there is more documentation now, including samples
from luad.
Related Issues (20)
- An immutable bool will get converted to an integer on the Lua side.
- Lua program crashes when using delegates instead of function pointers HOT 3
- Lua coroutines/threads support HOT 2
- DUB Tag for v5.1.5 HOT 2
- dub compilation for osx and lua 5.1 HOT 3
- unittests crash on exit with dmd 2.067 and dmd 2.068 HOT 7
- Need more examples and some blogposts HOT 2
- wrapping of structs with private members
- lua 5.2 HOT 1
- Type mismatch for struct parameters crashes on Windows
- Struct binding with empty constructor fail
- Broken with latest DMD
- const
- nil doesn't seem to work properly. HOT 2
- Can't build example using dub HOT 2
- lua 5.3 support? HOT 1
- Nice Library!
- Multiple linker errors of: PIE object; recompile with -fPIC HOT 3
- a problem when using a LDC2 HOT 4
- Unittest don't build with DMD 2.088 and up (don't know the actual fail point)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from luad.