lua-users home
lua-l archive

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


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 [1], 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;

	inet_pton(AF_INET,"192.168.1.10",&addr.sin_addr.s_addr);
	addr.sin_family = AF_INET;
	addr.sin_port   = htons(8080);
	sock = socket(AF_INET,SOCK_STREAM,0);
	setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
	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
	sock:bind(addr)

  You can see I wrapped calls to setsockopt() in the handler for __index()
(view a list of all such "variables" here [2]).

	addr = org.conman.net.address2("myserver.example.net","any","tcp",8080)
	sock = org.conman.net.address(addr[1].family,"tcp")
	sock:connect(addr)

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
addresses).

  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)

[1]	https://github.com/spc476/lua-conmanorg/blob/master/src/net.c

[2]	https://github.com/spc476/lua-conmanorg/blob/master/src/net.c#L790