What is userdata and lightuserdata in Lua? -


  • what userdata , lightuserdata in lua?
  • where need it?

i have been trying wrap head around time now, can't seem find tutorials/explanations understand.

why need them, why can't directly bind c functions lua metatables?

a userdata garbage-collected value of arbitrary size , content. create 1 c api, lua_newuserdata(), creates , pushes on stack , gives pointer content initialize see fit c.

it comparable calling malloc(). key distinction malloc() never need call free(), rather allow last reference evaporate , garbage collector reclaim storage eventually.

they useful holding data useful c, must managed lua. support individual metatables, key feature allows binding c or c++ objects lua. populate metatable methods written in c access, modify, and/or use content of userdata, , result object accessible lua. example of io library, stores c file * pointers in userdata, , provides bindings implement familiar read, write , similar methods. implementing __gc metamethod, io library makes sure 1 of file objects closes associated file * when collected.

a light userdata how represent pointer value in lua. create 1 calling lua_pushlightuserdata() pointer value. managed lua same way number is. useful when need name c object in way name can passed around within lua, object's lifetime not managed lua. numbers equal when have same value, light userdata compare equal when hold same pointer. numbers, exist long on stack or stored in variable, , not have individual metatables , not garbage collected.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -