Blitz objects in Lua: Light userdata?

BlitzMax Forums/BlitzMax Programming/Blitz objects in Lua: Light userdata?

taxlerendiosk(Posted 2006) [#1]
I'm a beginner to Lua, and I'm trying to make my way around it. Am I right in thinking that "light userdata" is a good way to allow Lua to refer to Blitz objects? I think I've worked out how to send a pointer to an object *in* to Lua:
Function GetAMyType:Int(_st:Byte Ptr)
	Local mt:MyType = New MyType
	mt.text = "Hello world!"
	lua_pushlightuserdata(_st, Byte Ptr(mt))
	Return 1
End Function

...And use lua_register to allow this function to be called from Lua. Which is fine. But how do I send the light userdata from Lua back out to another Blitz function, and have it turn back into a valid Blitz object? There doesn't seem to be any way. I'd greatly appreciate any help on this.


Regular K(Posted 2006) [#2]
I used HandleFromObject and HandleToObject, but theres the problem of releasing the integer handle, that would create memory problems if you dont do it properly.
I hope I understood your question.


taxlerendiosk(Posted 2006) [#3]
I think what the HandleTo/From() funcs really do is stick the objects in a list and give you the allocated index as a "handle". (I just tried getting the handle of a random object and got the handle "1", so I think I'm right.) It might be necessary to do it that way - give all the objects an integer index in a list/array and pass the index in and out of Lua instead - but if it's possible to use a "real" address/pointer/reference/whatever, I'd prefer it.


taxlerendiosk(Posted 2006) [#4]
Aha - these seem to work:
Extern
	Function lua_pushblitzobject(_st:Byte Ptr, obj:Object) = "lua_pushlightuserdata"
	Function lua_toblitzobject:Object(_st:Byte Ptr,index:Int) = "lua_topointer"
End Extern