BRL.Reflection Fixes/Hacks

BlitzMax Forums/BlitzMax Module Tweaks/BRL.Reflection Fixes/Hacks

Otus(Posted 2009) [#1]
First a couple of fixes to sections marked "TODO", then a more involved hack. The bottom of the post contains a copy of my current reflection.bmx file.

TTypeId.ForName does not work very well with arrays. Eg. fails with "Object[][][]" if no such object exists and throws an error on "Objec[]". Fix: replace with:
	Function ForName:TTypeId( name$ )
		_Update
		If name.EndsWith( "[]" )
			name=name[..name.length-2]
			Local elementType:TTypeId = ForName( name )
			If Not elementType Return Null
			Return elementType.ArrayType()
		Else
			Return TTypeId( _nameMap.ValueForKey( name.ToLower() ) )
		EndIf
	End Function


TTypeId.EnumMethods says "TO DO: handle overrides!". Fix: add a compare method to Type TMethod...
	Method Compare( with:Object )
		Return _index-TMethod( with )._index
	End Method


...and replace EnumMethods with:
	Method EnumMethods:TList( list:TList=Null )
		If Not list list=New TList
		If _super _super.EnumMethods list
		For Local t:TMethod=EachIn _methods
			list.AddLast t
		Next
		list.Sort
		'Remove overridden methods
		Local prev:TMethod
		For Local t:TMethod = EachIn list
			If prev And Not t.Compare(prev) Then list.Remove prev
			prev = t
		Next
		Return list
	End Method


Lastly, I have a hack that makes TTypeId.ForObject almost 10x faster* for non-array objects. However, it involves using one of the "reserved" members on BBClass (see brl.blitz/blitz_object.h). They don't seem to be used at the moment, but...

(* It becomes O(1) instead of the current O(log n) of a map lookup, so with very many classes there should be a greater speedup.)

In reflection.cpp add: (9-11 are the reserved members)
BBObject *bbRefGetClassTypeId( BBObject **clas ){
	return clas[9];
}

void bbRefSetClassTypeId( BBObject **clas, BBObject *t ){
	clas[9] = t;
}

(A cleaner hack would modify BBClass in brl.blitz/blitz_object.h&c and some files that depend on those, but that would require rebuilding all modules.)

In reflection.bmx add:


That works, but a couple of more changes allow complete removal of the TClass type:


Here's the file complete with all the changes above: