newproxy

87 views
Skip to first unread message

Alexander Gladysh

unread,
Jun 18, 2011, 4:17:00 PM6/18/11
to lua-cookbo...@googlegroups.com
Hi, all!

Anyone have a handy description of newproxy() function?

Or maybe care to submit a snippet for the Cookbook?

Thanks,
Alexander.

Patrick Donnelly

unread,
Jun 18, 2011, 7:55:59 PM6/18/11
to lua-cookbo...@googlegroups.com
On Sat, Jun 18, 2011 at 4:17 PM, Alexander Gladysh <agla...@gmail.com> wrote:
> Hi, all!
>
> Anyone have a handy description of newproxy() function?
>
> Or maybe care to submit a snippet for the Cookbook?

It's been taken out of Lua 5.2; is it worth talking about?


--
- Patrick Donnelly

Alexander Gladysh

unread,
Jun 18, 2011, 8:00:56 PM6/18/11
to lua-cookbo...@googlegroups.com

It is — Lua 5.1 is here to stay (from my point of view at least) until
Mike Pall says otherwise.

Alexander.

steve donovan

unread,
Jun 19, 2011, 3:27:40 AM6/19/11
to lua-cookbo...@googlegroups.com
On Sat, Jun 18, 2011 at 10:17 PM, Alexander Gladysh <agla...@gmail.com> wrote:
> Or maybe care to submit a snippet for the Cookbook?

Here is a strict array wrapper:

https://gist.github.com/759589

The newproxy magic is simple:

local obj = newproxy(true)
local MT = getmetatable(obj)

Since it returns us a bit of userdata with a metatable (the true
parameter) we can then override __len for the object.

For 5.2, __len can be overriden for regular tables so there is less
need for newproxy(). The only thing it can do is define __gc. I
briefly got excited about this for constructing module finalizers,
etc, but apparently there are doubts about the robustness of this
practice.

steve d.

Alexander Gladysh

unread,
Jun 22, 2011, 3:20:39 AM6/22/11
to lua-cookbo...@googlegroups.com
On Sun, Jun 19, 2011 at 11:27, steve donovan <steve.j...@gmail.com> wrote:
> On Sat, Jun 18, 2011 at 10:17 PM, Alexander Gladysh <agla...@gmail.com> wrote:
>> Or maybe care to submit a snippet for the Cookbook?

> Here is a strict array wrapper:

Thank you!

> https://gist.github.com/759589
>
> The newproxy magic is simple:
>
>    local obj = newproxy(true)
>    local MT = getmetatable(obj)
>
> Since it returns us a bit of userdata with a metatable (the true
> parameter) we can then override __len for the object.
>
> For 5.2, __len can be overriden for regular tables so there is less
> need for newproxy(). The only thing it can do is define __gc.  I
> briefly got excited about this for constructing module finalizers,
> etc, but apparently there are doubts about the robustness of this
> practice.

__gc is the only thing I use newproxy() for. It is very useful for
managing pools of objects (for example, network connections). If code
that uses the connection forgot to release it, __gc handler will do it
(eventually).

Alexander.

steve donovan

unread,
Jun 22, 2011, 4:10:13 AM6/22/11
to lua-cookbo...@googlegroups.com
On Wed, Jun 22, 2011 at 9:20 AM, Alexander Gladysh <agla...@gmail.com> wrote:
> __gc is the only thing I use newproxy() for. It is very useful for
> managing pools of objects (for example, network connections). If code
> that uses the connection forgot to release it, __gc handler will do it
> (eventually).

That struck me as the obvious use. Well, in Lua 5.2, it is fairly
trivial to implement newproxy as an extension ;)

BTW, the snippet shows another use of newproxy which I didn't
emphasize enough. And that is to make a really paranoid data type.
Since it is a udata proxy, a user cannot use the regular table
functions to break any invariants.

Naturally, one loses a little performance. But LuaJIT can be
surprisingly intelligent in optimizing these patterns.

steve d.

Reply all
Reply to author
Forward
0 new messages