Idiomatic C API

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Idiomatic C API

Soni "They/Them" L.
Lua's C API is not idiomatic. I don't see setjmp/longjmp used by things
like POSIX (as far as I know nothing in POSIX says you have to use
setjmp/longjmp to handle errors) or libuv.

It would be more idiomatic to return error codes, and have the caller
propagate errors. (Using setjmp/longjmp internally is fine tho.)

This would also increase compatibility with some libraries and
programming languages.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: Idiomatic C API

Coda Highland
On Sat, Feb 24, 2018 at 9:33 PM, Soni "They/Them" L. <[hidden email]> wrote:
> Lua's C API is not idiomatic. I don't see setjmp/longjmp used by things like
> POSIX (as far as I know nothing in POSIX says you have to use setjmp/longjmp
> to handle errors) or libuv.
>
> It would be more idiomatic to return error codes, and have the caller
> propagate errors. (Using setjmp/longjmp internally is fine tho.)
>
> This would also increase compatibility with some libraries and programming
> languages.

It is, as is the case with so many things, a tradeoff.

setjmp/longjmp is faster at runtime and simpler to code. It does of
course have a lot of limitations you have to be careful with, but
error codes also have a lot of things you have to be careful with --
among other things, using error codes implies using out parameters,
which is pretty smelly in its own right.

Certainly the compatibility note is, well, noteworthy. But it doesn't
seem to be worth the cost of refactoring the entire Lua codebase and
API (thereby breaking literally every single C extension ever written)
to accomplish that when you can write API wrappers to adapt Lua to
work in the other environments (possibly using C++ mode).

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Idiomatic C API

Sean Conner
It was thus said that the Great Coda Highland once stated:

> On Sat, Feb 24, 2018 at 9:33 PM, Soni "They/Them" L. <[hidden email]> wrote:
> > Lua's C API is not idiomatic. I don't see setjmp/longjmp used by things like
> > POSIX (as far as I know nothing in POSIX says you have to use setjmp/longjmp
> > to handle errors) or libuv.
> >
> > It would be more idiomatic to return error codes, and have the caller
> > propagate errors. (Using setjmp/longjmp internally is fine tho.)
> >
> > This would also increase compatibility with some libraries and programming
> > languages.
>
> It is, as is the case with so many things, a tradeoff.
>
> setjmp/longjmp is faster at runtime and simpler to code. It does of
> course have a lot of limitations you have to be careful with, but
> error codes also have a lot of things you have to be careful with --
> among other things, using error codes implies using out parameters,
> which is pretty smelly in its own right.

  True.  I've only used setjmp()/longjmp() once [1] in over 25 years of C
coding and it saved a ton of code and simplified the design (otherwise, it
was over 500 instances of "rc = internalfunction(); if (rc != 0) return rc;"
and a more complex design).  I even turned it into a Lua module and the use
of setjmp()/longjmp() in both my code and Lua never caused an issue.

> Certainly the compatibility note is, well, noteworthy. But it doesn't
> seem to be worth the cost of refactoring the entire Lua codebase and
> API (thereby breaking literally every single C extension ever written)
> to accomplish that when you can write API wrappers to adapt Lua to
> work in the other environments (possibly using C++ mode).

  Well, Soni is certainly free to do the work and see how well it goes.

  -spc

[1] https://github.com/spc476/mc6809
        It's an MC6809 CPU emulator as a library.


Reply | Threaded
Open this post in threaded view
|

Re: Idiomatic C API

Soni "They/Them" L.
In reply to this post by Coda Highland


On 2018-02-25 01:28 AM, Coda Highland wrote:

> On Sat, Feb 24, 2018 at 9:33 PM, Soni "They/Them" L. <[hidden email]> wrote:
>> Lua's C API is not idiomatic. I don't see setjmp/longjmp used by things like
>> POSIX (as far as I know nothing in POSIX says you have to use setjmp/longjmp
>> to handle errors) or libuv.
>>
>> It would be more idiomatic to return error codes, and have the caller
>> propagate errors. (Using setjmp/longjmp internally is fine tho.)
>>
>> This would also increase compatibility with some libraries and programming
>> languages.
> It is, as is the case with so many things, a tradeoff.
>
> setjmp/longjmp is faster at runtime and simpler to code. It does of
> course have a lot of limitations you have to be careful with, but
> error codes also have a lot of things you have to be careful with --
> among other things, using error codes implies using out parameters,
> which is pretty smelly in its own right.
>
> Certainly the compatibility note is, well, noteworthy. But it doesn't
> seem to be worth the cost of refactoring the entire Lua codebase and
> API (thereby breaking literally every single C extension ever written)
> to accomplish that when you can write API wrappers to adapt Lua to
> work in the other environments (possibly using C++ mode).

Don't forget that Lua is stackless, so any errors only have to go as far
as about a handful of stack frames anyway.

(Unless you're using something like lua_call. Not something I see often,
tho. Plus, you'd get to handle cleanup (freeing OS resources) *while
keeping the original backtrace*!)

> /s/ Adam
>

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: Idiomatic C API

ThePhD
As a small side note, someone is actually attempting to rewrite Lua and Lua's C API to be more performance-friendly with C++[1]. They might come up with other good ideas relevant to making the API more idiomatic, maybe!

[1] https://github.com/Rseding91/LuaPlusPlus

On Sun, Feb 25, 2018 at 8:19 AM, Soni "They/Them" L. <[hidden email]> wrote:


On 2018-02-25 01:28 AM, Coda Highland wrote:
On Sat, Feb 24, 2018 at 9:33 PM, Soni "They/Them" L. <[hidden email]> wrote:
Lua's C API is not idiomatic. I don't see setjmp/longjmp used by things like
POSIX (as far as I know nothing in POSIX says you have to use setjmp/longjmp
to handle errors) or libuv.

It would be more idiomatic to return error codes, and have the caller
propagate errors. (Using setjmp/longjmp internally is fine tho.)

This would also increase compatibility with some libraries and programming
languages.
It is, as is the case with so many things, a tradeoff.

setjmp/longjmp is faster at runtime and simpler to code. It does of
course have a lot of limitations you have to be careful with, but
error codes also have a lot of things you have to be careful with --
among other things, using error codes implies using out parameters,
which is pretty smelly in its own right.

Certainly the compatibility note is, well, noteworthy. But it doesn't
seem to be worth the cost of refactoring the entire Lua codebase and
API (thereby breaking literally every single C extension ever written)
to accomplish that when you can write API wrappers to adapt Lua to
work in the other environments (possibly using C++ mode).

Don't forget that Lua is stackless, so any errors only have to go as far as about a handful of stack frames anyway.

(Unless you're using something like lua_call. Not something I see often, tho. Plus, you'd get to handle cleanup (freeing OS resources) *while keeping the original backtrace*!)

/s/ Adam


--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.