GithubHelp home page GithubHelp logo

Comments (3)

SteveKChiu avatar SteveKChiu commented on June 18, 2024

For Lua operators, you can just bind the function to special meta name, for example:

LuaBinding(L).beginClass<MyClass>("MyClass")
    ...
    .addFunction("__add", &MyClass::add)
    ...
.endClass();

For a complete list of Lua operator meta methods you can see Lua reference, under Metatables and Metamethods section.

Lua function overloads on the other hand will never work too well. For one thing, the Lua type conversion for number and string will make automatically bind to overloaded functions nearly impossible, and there is no way to enumerate C++ overloaded functions. Combine with other difficulty, I don't think there is a way to do that in compile time.

So if you plan to add a Lua overloads function (a function that do different things based on argument types), the best way is to use CFunction convention, and dispatch by yourself.

If you are talking about adding overloaded C++ function to Lua (pick one the overloaded function), you just need to static_cast to the proper function type before adding it. For example:

static int test(string, int);
static string test(string);

LuaBinding(L).beginModule("utils")

    // this will bind int test(string, int)
    .addFunction("test_1", static_cast<int(*)(string, int)>(&test))

    // this will bind string test(string), by using our LUA_FN macro
    // LUA_FN(return_type, func_name, func_arg_types...)
    .addFunction("test_2", LUA_FN(string, test, string))

    .endModule();

from lua-intf.

krisr avatar krisr commented on June 18, 2024

Thanks for the quick response Steve. This was very helpful. I recognize it's a bit tricky to support overloading automatically so I appreciate that it probably doesn't make sense. I'm glad to hear that I can access the metaclass when adding operators - other libraries I was using were not working with the metaclass directly.

I'm currently writing an automatic binding generator for your project using libclang. If you are interested, I'd be happy to share the source when I'm further along.

from lua-intf.

SteveKChiu avatar SteveKChiu commented on June 18, 2024

Thanks for contribution. Yeah, I think it is interesting topic, and want to take a look at it.

from lua-intf.

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.