[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Suggestion: Lua 5.3 -- a facility for creating and manipulting Lua file handles (C API)
- From: Sean Conner <sean@...>
- Date: Mon, 18 Nov 2013 18:54:09 -0500
It was thus said that the Great Sir Pogsalot once stated:
> I've felt a bit ridiculous trying to mirror some of the C structs when
> "exporting" them to Lua as tables, but I wanted people to be able to follow
> along with a C socket tutorial and expect the same member fields.
I've explicitely rejected trying to slavisly follow a C API when writing a
wrapper for Lua, because if I wanted to write in C, I'd write in C. I want
to write Lua in Lua, if that makes any sense. And thus, I try to adapt a
Lua interface to be more Luaish, or, at least what makes it easier for *me*
to write Lua code.
As an example, I could have just wrapped setrlimit() [1]
process.setrlimit(process.RLIMIT_CPU,{ rlim_cur = 4 , rlim_max = 4})
but like I said, if I wanted C, I have C. Instead, I spent the time writing
the interface so I could do:
process.limits.soft.cpu = 4
Yes, behind the scenes, it calls setrlimit(), but I don't have to both
with that in Lua. In fact, I can also do:
print(process.limits.soft.cpu) -- call getrlimit()
> Mostly I've been kept up nights about the tables I return from my
> getaddrinfo(). I really have to keep myself from molding it into a
> friendlier table, but why I am unhappy with luasocket is because I have so
> much trouble figuring out what to expect from its functions. The
> documentation only goes so far...
Why are you afraid of molding the results of getaddrinfo() into something
friendlier (or easier)? Is it because you want people using a C tutorial
for networking to follow along in Lua? To me, that seems rather odd. I
just went ahead and made the results in Lua friendlier for my own usage. My
own interface [2] is called thusly:
results = net.address2('www.google.com','any','tcp','www')
for i = 1 , #results do print(results[i]) end
> ip:74.125.229.208:80
> ip:74.125.229.209:80
> ip:74.125.229.210:80
> ip:74.125.229.211:80
> ip:74.125.229.212:80
> ip6:[2607:f8b0:4008:801::]:80
Also, the results have already been converted into struct sockaddr so they
can be used directly by socket:connect().
> check out my monstrocity:
> https://github.com/Pogs/lsock/blob/master/lsock.c#L1560
It's perhaps a bit shorter than what I wrote [2] so I don't consider it
that monstrous. But we might be operating under different definitions of
"monstrous."
-spc (Embrace Lua; if you want C, you know where to find it ... )
[1] https://github.com/spc476/lua-conmanorg/blob/master/src/process.c#L985
[2] https://github.com/spc476/lua-conmanorg/blob/master/src/net.c#L531