Git Product home page Git Product logo

Comments (8)

jwhear avatar jwhear commented on August 24, 2024

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.

JakobOvrum avatar JakobOvrum commented on August 24, 2024

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.

jwhear avatar jwhear commented on August 24, 2024

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.

jwhear avatar jwhear commented on August 24, 2024

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.

JakobOvrum avatar JakobOvrum commented on August 24, 2024

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.

jwhear avatar jwhear commented on August 24, 2024

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.

JakobOvrum avatar JakobOvrum commented on August 24, 2024

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.

JakobOvrum avatar JakobOvrum commented on August 24, 2024

Closing, there is more documentation now, including samples

from luad.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.