Lua: Pushing a Object onto the Stack

BlitzMax Forums/BlitzMax Programming/Lua: Pushing a Object onto the Stack

Ragz(Posted 2006) [#1]
Is it possible to push an object from a blitz type onto the stack so that later on lua can pass it to blitz? Right now I'm keeping all the objects in an array and passing the index to lua.


taxlerendiosk(Posted 2006) [#2]
I'm writing an alternative Blitz Lua module at the moment, this is a bit from it that should help you out. It passes the Blitz object as a Lua variable of type "light userdata", which is basically like a "Byte Ptr" or "void *".

Warning: It's your responsibility to make sure that this pointer is still pointing to something later on when you want to pop it back out using lua_toblitzobject, since Blitz's garbage collector will not know that a reference exists inside Lua and will happily collect the object if no reference exists in your Blitz program. (I've got a plan to stop this & have objects which will not be collected until both the Blitz *and* Lua garbage collectors want rid of it, but I haven't put it into action yet.) So you will still need those arrays of all your objects, even if you don't appear to actually be using them.

Extern
	Function lua_pushblitzobject( lua_state:Byte Ptr, blitzobject:Object )="lua_pushlightuserdata"

	Function lua_toblitzobject:Object( lua_state:Byte Ptr, index:Int )="lua_touserdata"
End Extern



Ragz(Posted 2006) [#3]
Well I was planning to replace the arrays with lists, and then push the TLinks to Lua, that should work right?

Those functions are good to go, or do I need to modify them to my needs in any way?


taxlerendiosk(Posted 2006) [#4]
Not sure why you'd want to pass TLinks instead of the actual object itself, which should work just as well, but sure.

You should be able to just drop that block into some code that imports axe's module, and use them for any Blitz object at all. Just remember to make sure something in Blitz "knows" about your object, like a list or an array.


Ragz(Posted 2006) [#5]
Ah right, you are right about Objects making more sense, not sure why I wanted to pass TLinks either :P

Well thanks for the help, should be very useful :)