2

So in 5.1 of Lua, newproxy was deprecated. In 5.2, it got removed. But why? What should I use now to construct a userdata?

I'm trying to implement pseudo-classes for fun and userdatas are way easier. They make implementing immutability and restricting writing to certain keys much easier.

incapaz
  • 349
  • 1
  • 3
  • 11
  • You can't create userdata from Lua script. Use tables and their metatables. – Egor Skriptunoff Feb 20 '20 at 20:42
  • @EgorSkriptunoff Lua tables are...fat. An empty table takes around 40 bytes. It also makes immutability harder to implement. Since `__newindex` is *always* invoked on non-tables, that makes it easier for me. Unless there is another way. Proxy tables are still too much work. – incapaz Feb 20 '20 at 20:43
  • An empty table takes 72 bytes. newproxy [is not needed](https://stackoverflow.com/a/23593617/1847592) in Lua 5.2+ – Egor Skriptunoff Feb 20 '20 at 22:01
  • "*They make implementing immutability and restricting writing to certain keys much easier.*" How? If you implement the `index` and `newindex` metamethods, you can do the same thing with an empty table as you can do with a userdata. And you *have* to implement those on a userdata if you want to give them "members". So what's the difference, besides the function you use to create them? – Nicol Bolas Feb 20 '20 at 22:20
  • You always can create simple C module with this function – moteus Feb 21 '20 at 08:53

2 Answers2

2

You probably shouldn't have been using newproxy like that at all, I am not sure it was even doing what you thought it was doing.

The newproxy function was undocumented, it was likely removed because it was made redundant by documented features in 5.2.

8.1.6 newproxy removed.

This was always an 'undocumented' function in Lua 5.1, and it was considered unnecessary, since it was chiefly used to write finalizers. Since the __gc metamethod now works for Lua tables, this workaround is no longer needed. - What Lua 5.1 code breaks in Lua 5.2

You also wont find anything in the Lua 5.2 Manual - Section 8 Incompatibilities with the Previous Version regarding newproxy as it was undocumented.

Nifim
  • 4,758
  • 2
  • 12
  • 31
2

In 5.1, there were some inconsistencies between the metatable functionality of a userdata and a table (the # operator didn't call __len in 5.1 when used on a table). 5.2 removed those inconsistencies, so there is no difference between a table's metatable and a userdata's metatable.

As such, there is no need for it. It is very easy to write a near-exact equivalent to newproxy, with the only difference being the type of the object returned:

function newproxy(new_meta)
    local proxy = {}

    if(new_meta == true) then
        local mt = {}
        setmetatable(proxy, mt)
    elseif(new_meta == false)
    else
        --new_meta must have a metatable.
        local mt = getmetatable(new_meta)
        setmetatable(proxy, mt)
    end

    return proxy
end

Of course, since you're writing a new function anyway, you could also give it an improved API. Such as a way to create proxies with a given metatable, rather than giving it a table/userdata that has a metatable.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982