lua-users home
lua-l archive

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


While I got no experience in Pyhton's C API I recently did a larger native library interface with Google's V8. Also there a function call from native code to script context takes several lines:

# having a persistent handle to a function (which blocks garbage collection for this)
# turn it to a "live" handle for the current execution context
# create a value list object for the arguments and initialize it with the arguments
# make the call (ignoring exceptions for simplicity)
# look what type the return value is and handle error if not.
# and when its valid turn it to its native representative

So for me there is some complication from native to script context in all environments I know of.

I prefer the V8 style to the stack style of Lua tough. I hate coding stack machines as it's error prone and hard to debug. While I still love my HP48G and it's reverse polish notation beyond quick hand calculations coding algorithms is hard. I remember the problems with my Lua/C interface of forgetting to popping one thing from the stack and run into overflow far down the line, or popping one too much and get an error somewhere else etc.


On Tue, Mar 15, 2016 at 5:49 AM, Hisham <h@hisham.hm> wrote:
On 14 March 2016 at 18:22, Jonathan Goble <jcgoble3@gmail.com> wrote:
> On Mon, Mar 14, 2016 at 5:06 PM, ThePhD <jm3689@columbia.edu> wrote:
>> Dear Everyone,
>>
>> [long rant]
>
> I agree. Although I've not dug into it much, and not tried to write
> any code for it, I prefer Python's C API over Lua's, because Python
> defines a `PyObject *` pointer that can be used to point to an
> arbitrary Python value, without need for complicated stack operations.
> And building on that, Python functions written in C receive their
> positional arguments as a `PyObject *` tuple passed directly as an
> actual argument to the C function. (The method of unpacking that tuple
> into C values is a bit clunky, but in my uneducated opinion, worth the
> trade-off.)
>
> I'd love to see something similar to that for Lua.

Python's C API looks much simpler on surface, indeed, but it has one
complication that breaks the deal: garbage collection. This is the one
thing that Lua's stack oriented API gives you for free, and it's its
killer feature. Ask anyone who has coded significantly with Python's C
API and the one thing they'll complain you about is having to deal
explicitly with reference counting for GC.

-- Hisham