[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to approach this?
- From: Andrew Starks <andrew.starks@...>
- Date: Tue, 16 Sep 2014 04:14:49 -0500
On Tuesday, September 16, 2014, Udo Schroeter <udo.schroeter@gmail.com> wrote:
Hey Dirk,
I'm relatively new to Lua, so take this with a grain of salt.
I would advise against returning a normal table. There would be no way to protect internal fields if you store them there, but more importantly keeping the connection between the Lua table and your window object itself alive could potentially be a major hassle.
Instead, have a look at the liolib.c as an example. There, they use a userdata base object for each file, and associated with it is the standard metatable for files. However, in your case, you would associate your window userdata object with a brand new table when it's constructed. The user can then modify that metatable to set event handlers or other data.
This has the added advantage that you can always easily retrieve the user-defined event handler for any given action from the C side by accessing the metatable.
I have the same caveat, as well, but offer my understanding, mostly for fun.
I believe thet this is exactly what lightuserdata is for: identity. The window handle is used to create your lightuserdata object. When the callback fires, this window handle will match your lightuserdata, which can be used as an index in a lookup table that is stored in the registry.
In 5.3, I believe that this lightuserdata can also be used directly as a uservalue, if you'd like your object to be stored as userdata. The lightuserdata is used for identity and perhaps other C-side structures might be stored in the full userdata? However, I don't believe thst this is required for safety. If your worried about someone messing with it, you could use the `__metatable` field, but since you can't change the value of lightuserdata, it's hard to see the damage caused that is beyond the scope of Lua.
Anyway, I used this technique in my yet-to-be-finished NanoMsg binding. I'm 0-10+ in helpful advice, so I'll be interested in other answers.
-Andrew