After looking at Emilarity’s tutorial on wrapping Roblox objects (Tutorial), I had one (and a half) major questions about using newproxy or setmetatable, and the question really had to do with userdata.
My question is, what’s so special about userdata, and why would you choose to use newproxy to achieve wrapper functions instead of setmetatable.
I believe that both could be used, but why choose one in particular over the other?
And about my second question was an explanation about the __mode metamethod, but it would be alright if you didn’t answer this question as I already know a lot of the logistics of it It would be nice to hear answers however.
there’s no reason to use newproxy over setmetatable anymore, it was an undocumented feature of lua 5.1 that was useful for implementing __len (not possible with tables in standard lua 5.1) or observing gc behavior but since __gc doesn’t exist in luau and __len can be used on tables now it doesn’t have a use case anymore
p.s: __mode controls gc behaviour on a table, if no references are left to a key (k) or value (v) then it’ll get garbage collected similar to a local variable; specifying s also internally resizes the table to an optimal capacity during gc but can result in missed entries during iteration
Userdata is a type in Lua that is intended for the arbitrary C data that programmers store in Lua types. It is often referred as a bridge or a means to communicate with external objects/data which Lua is unable to address itself. Now with regards to wrapping objects or proxying Roblox objects and their respective functions, newproxy and setmetatable is where the difference lies.