Lua<->Blitzmax: overide __eq

BlitzMax Forums/BlitzMax Programming/Lua<->Blitzmax: overide __eq

Derron(Posted 2016) [#1]
Hi

I tried to redirect "==" comparisons from LUA back to Blitzmax as this is the only way to allow a comparison of "BlitzMax-objects" within Lua (aka "full user data"-objects).

So I did like maxLua (and my frameworks lua-code) already does for "super/index/newindex/": I added a hook for "__gc" to the metatables.


For some objects the whole thing is called correctly (I see debug messages of my "CompareObject()"-function), but for some BlitzMax objects it isn't.


...registering the BlitzMax-environment to lua..

		lua_pushcfunction(getLuaState(), CompareObjectsSelf)
		lua_setfield(getLuaState(), -2, "__gc")


and the finally called "CompareObjects":
	Method CompareObjects:Int()
		print "compare objects"
		Local obj1:Object, obj2:object

		if lua_isnil(getLuaState(), -1)
			print "obj1 is nil"
		else
			obj1 = lua_unboxobject(getLuaState(), -1)
		endif
		if lua_isnil(getLuaState(), 1)
			print "obj2 is nil"
		else
			obj2 = lua_unboxobject(getLuaState(), 1)
		endif

		if obj1
			print "got obj1: " + TTypeID.ForObject(obj1).name()
		else
			print "got obj1: NULL"
		endif
		if obj2
			print "got obj2: " + TTypeID.ForObject(obj2).name()
		else
			print "got obj2: NULL"
		endif

		lua_pushboolean(getLuaState(), obj1 = obj2)
'		lua_pushinteger(getLuaState(), obj1 = obj2)
		
		return True
		' obj1 = obj2
	End Method



As said, for some objects this function is called ... and for some others not.
I think I also mixed the "indices" some how ... as the returned objects get compared in Lua-functions not comparing them at all (just "using" them ... eg. as parents of some fields. "MyParent.myField" might then print "MyParent").
But it might also be the case, that "__eq" is called in some other situations too (accessing fields-objects of a BlitzMax-exposed-object: MyObject.children.GetName())


What I want to get working, is:
- fetching a BlitzMax object from within Lua
- fetching another BM object ...
- comparing them in LUA whether they are equal or not


For now I exposed a "AreEqual:int(o1:object,o2:object)" helper function from BlitzMax to Lua, which does the comparison in BlitzMax rather than Lua. This works, but feels a bit "unnatural" for the Lua-coder then.


How did you solve it - or how would you solve it?


I assume I never really understood how the whole index-thing works with Lua - it is more a "try and error" thing to get it work.


bye
Ron


Derron(Posted 2016) [#2]
Ah ... ok ... forget about it...

I registered for "__gc" rather than "__eq" - which explains the odd problems.


Fixing that, fixes the whole issue.


bye
Ron