[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How close to C do you make your binding?
- From: Sean Conner <sean@...>
- Date: Wed, 18 Jun 2014 12:29:33 -0400
It was thus said that the Great Andrew Starks once stated:
> We have our nanomsg binding that we recently remade. It was very C heavy
> and now it's the opposite. We're making an "nml.core" that looks *pretty
> much* exactly like the C API. The higher level binding wraps it in a
> Lua-esque API.
> If you have made bindings this way, how far do you take the idea of
> sticking to the C API in your core layer?
> Do you strive for perfect adherence to the original C documentation? Do you
> change things like "-1" or "0" to "false", where appropriate? Do you use
> multiple returns where their API is using pointers?
If I want to program in C, I know where it is.
Basically, I don't constrain myself to slavisly follow the C API. For
instance, when I wrapped the Berzerkly sockets API , at first I followed
the C API but hated it. I then changed it to make it nicer to use (existing
Unix networking tutorials be damned).
So, wereas in C, you do:
struct sockaddr_in addr;
int reuse = 1;
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
sock = socket(AF_INET,SOCK_STREAM,0);
bind(sock,(struct sockaddr *)&addr,sizeof(addr));
which is grind-grind-grind-grind, I worked the Lua API to be:
addr = org.conman.net.address("127.0.0.1","tcp",8080)
sock = org.conman.net.socket(addr.family,"tcp")
sock.reuseaddr = true
You can see I wrapped calls to setsockopt() in the handler for __index()
(view a list of all such "variables" here ).
addr = org.conman.net.address2("myserver.example.net","any","tcp",8080)
sock = org.conman.net.address(addr.family,"tcp")
where myserver.example.net can return an IPv4 *OR* an IPv6 address, and
it'll just work (FYI: org.conman.net.address2() returns a list of
In the general case, the more familiar I am with a particular C API, the
more likely I'll diverge wildly from it in a Lua wrapper.
-spc (Again, because if I want to program in C, I know where to find it)