lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Dear Everyone,

I STRONGLY disagree with always defaulting to the C API. While it's good for quick things and hobby projects, the C API lacks the ability to scale. Writing large applications where you're writing -- and re-writing (or copy-pasting) -- the same code over and over again with slight tweaks just to call a function or something is not okay. For example, consider some basic lua code to call a function:

function f ( a, b, c )
    print("adding b and c, and string arg " .. a)
    return b + c
end

f( "bark", 2.5, 3.5 )

From the Lua Reference Manual (http://www.lua.org/manual/5.3/manual.html#lua_call), this turns into this:

     lua_getglobal(L, "f");                  /* function to be called */
     lua_pushliteral(L, "bark");                       /* 1st argument */
     lua_pushnumber(L, 2.5);                          /* 3rd argument */
     lua_pushnumber(L, 3.5);                          /* 3rd argument */
     lua_call(L, 3, 1);     /* call 'f' with 3 arguments and 1 result */
"Well, that's not so bad".

Yeah, it's not so bad!

Then you add that you don't want to call it with plain literals, you want to use an element from a lua table. So, you need to add 2 more function calls... IF your table is accessible as a global. If it's not, prepare to tunnel... oh, and then you need to make sure you remove everything off the stack BUT the argument you want. That's a lua_remove... Oooh, but you don't want to remove everything, because the table `t` you were digging into gets used later, too! Yeah, well, lua_rotate doesn't exist in the 5.1 API.... And don't forget, if you want to pull out an integer from the 5.3 API, you can't set a double and then pull out an integer, because lua checks that internally now and returns you a hefty 0 if you specifically pushed an integer from somewhere else in the C API...

And we haven't even started on how to deal with multiple return values or setting the value of that return into something else.

Now, because you don't feel like re-typing all of that a bunch of times.... well, just copy-paste it. Enter 2 or 3 or 4 off-by-one errors, doing you and everyone else about 0 favors and wasting your time as you keep smacking yourself in the forehead for forgetting to remember to up the number of arguments you're calling the function with from 3 to 4.

We haven't even gotten on how you would call anything more complex than a C free function that adheres to lua's strict conventions.

This is BAD. It's error-prone. It's sluggish. It's just not what you want out of an API. If I can do

f("hi", 24, 56)

In Lua, I deserve something that allows me to do

f("hi", 24, 56)

In C++. We can tote how easy it is to use the Lua C API -- and it's fine for implementing things quickly or being the underpinnings of a library -- but it is NOT ideal. It is NOT the best we can do. We can do better. And we do deserve to have libraries that make it better. Libraries that allow us to do [ Selfish plug ]:

sol::function f = table["f"];
f("hi", 24", 56);

Sincerely,
ThePhD

On Mon, Mar 14, 2016 at 6:25 AM, Peter Pimley <peter.pimley@gmail.com> wrote:
"Sooner, not later, you are going to run into something that requires you to understand what is going on."

I agree.  The C API looks big and scary at first, but once you get the fundamentals you'll find (if you're comfortable with C) that it's actually quite easy to use and incredibly well thought out.  You'll soon be able to write a quick C function to perform some function without really having to stop and think.

On 13 March 2016 at 17:20, Andrew Starks <andrew@starksfam.org> wrote:

On Sun, Mar 13, 2016 at 09:51 Nagaev Boris <bnagaev@gmail.com> wrote:
On Sun, Mar 13, 2016 at 5:42 PM, Jose Marin <jose_marin2@yahoo.com.br> wrote:
> Hi!
>
>
> What's the best/easier/faster Lua binding library, in your opinion?
>
> Thanks
>
>
> Jose
>
>

Lua C API.


--


Best regards,
Boris Nagaev


IMHO this is the only correct answer, if you are new to Lua. Sooner, not later, you are going to run into something that requires you to understand what is going on. Any binding generator is going to be built upon the C API and without a working understanding of how that works, you're likely to be lost.

You'll "get" it quickly. You may hate how tedious it feels at first, but you're likely to get a feel for how to avoid the tediousness or at least understand it, in short order.

To contrast, The C API is simple whereas binding generators attempt to make things easy. Start with simple, if you need to be successful.

- Andrew