Creating C closures from Lua closures
Posted by publicdebates 5 days ago
Comments
Comment by widdershins 1 day ago
Or, if you're worried about performance/memory, you could allocate a struct for `CALLBACK` to return as userdata, containing the function pointer and `findex`. If you made a little allocator for these it would be extremely fast.
I'm sure I'm missing things, but the solution you chose feels like the nuclear option.
Comment by comex 1 day ago
This was probably posted in response to this other link from two days ago, which is about about JIT-compiling wndproc callbacks in particular; the comments discuss the “proper” way to do it, which is to use GWLP_USERDATA:
https://news.ycombinator.com/item?id=46259334
At least, that’s the proper way to do it if you control the entire application. But for what’s apparently a thin wrapper that exposes Win32 APIs directly to Lua, I can understand picking an approach that makes life easier for the Lua code author, even if it’s hackier under the hood. It also avoids the need to write custom behavior for each API.
Comment by publicdebates 21 hours ago
Comment by CodesInChaos 22 hours ago
GWLP_USERDATA should be the best option, though the API for setting it and setting the WNDPROC being separate looks error prone.
Comment by publicdebates 21 hours ago
Comment by not_a9 14 hours ago
Comment by halayli 21 hours ago
Comment by lkuty 20 hours ago
Comment by psychoslave 22 hours ago
https://news.ycombinator.com/item?id=46228597
https://news.ycombinator.com/item?id=46259334
Also talking about the Knuth boy/man test: https://news.ycombinator.com/item?id=46020151
Not a bad thing, but that really question if there is some active micro-
Comment by shakna 22 hours ago
Lua has its own allocator, which will also collect for you. lua_newuserdata. At the expense of having to set executable yourself, but without all the inbuilt inefficiencies the article points out.
Comment by publicdebates 21 hours ago
That said, I'm not sure that solution beats out this one. I'm using a linked list on top of an arena allocator in practice, which means allocations happen once every 0x10000 bytes. (A single C closure of mine takes up exactly 16 bytes, which includes the pointer to the next linked list node. I'm very happy with how it's designed.)
Comment by shakna 7 hours ago
void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);Comment by ufo 22 hours ago