Reflection: Invoking functions

BlitzMax Forums/BlitzMax Programming/Reflection: Invoking functions

LordChaos(Posted 2008) [#1]
Today I wanted to use the new reflection features of BMax. However, to my surprise, I wasn't able to find support for regular functions.

Is there a way to make it work, or the possibility to add it in future?

Please don't tell me to use function pointers. In my app, I get to know the function arguments at runtime, thus I can't use function pointers because the prototypes are hardcoded.
Something like the object-array of the Invoke-Method which is passed as a chain of arguments would be perfect for me.

Thanks in advance!


johnnyfreak(Posted 2008) [#2]
i'm interested too in calling static method (functions) by refletcion. Reading the documentation seems it isn't supported.....


Otus(Posted 2008) [#3]
There is support for neither Functions nor Global fields using BRL.Reflection.

Could you use a Method that does nothing but calls the function? Of course, you still need an object and it's not always possible (or a good idea) to create a dummy object for calling it...

I think that's about the only way to call a function of an unknown type, since fields of a function type are invisible to reflection.


LordChaos(Posted 2008) [#4]
Unfortunately, besides the fact that this kind of workaround would be horrible, I still wouldn't know the arguments of the function which I would put in the method.


johnnyfreak(Posted 2008) [#5]
maybe something like this?

SuperStrict

Type TTest
	Field x:Int=0
	Global globalX:Int=0

	Method CallsetGlobalX:Int(x:Int) 
		Return TTest.setGlobalX(x)
	EndMethod
	
	Function setGlobalX:Int(x:Int)
		globalX = x; Return globalX
	EndFunction
EndType

Print TTest.globalX

Local staticCall:String="Call"
Local obj:TTest  = New TTest
Local id:TTypeId= TTypeId.ForObject( obj )

id.FindMethod(staticCall + "setGlobalX").Invoke obj , ["25"]

Print TTest.globalX



Otus(Posted 2008) [#6]
Unfortunately, besides the fact that this kind of workaround would be horrible, I still wouldn't know the arguments of the function which I would put in the method.


Yeah, it is horrible. Do you really need a function in the first place? Could it be a method instead? I don't think there's a good way to do what you are trying to do.


johnnyfreak(Posted 2008) [#7]
OT: why is my post older then otus one but it's before?
EDIT: not now but when time was in minutes...


LordChaos(Posted 2008) [#8]
@johnnyfreak: Thank you for your code. It's nice to see that it can be done theoretically this way. Sadly, I still woudln't like to implement it this way.

I'm designing some kind of extremly basic script stuff, where functions should be called from external files, like:

script:
function(1, 2, 3)


The implementation should look something (!) like this:

bmax:
Function Func(x:Int, y:Int, z:Int)

 Print x+y+z

End Function

tScript.AddFunc(Byte Ptr(Func), "function(int x, int y, int z)")


Of course I could do it in a different way with only one type of function with defined arguments. But a solution with reflection is a 100 times superior!

Is it technically harder to implement reflection on functions? :/ I don't understand why it it only for methods!


ziggy(Posted 2008) [#9]
You may take a look to BriskVM. It is a comercial max module that covers all this stuff in a wonderful way.


LordChaos(Posted 2008) [#10]
As a programmer. I'm aware of differenct script engines.

If I would like to use one, I would use one and would not create forum topics because I'm stuck on my own one.


ziggy(Posted 2008) [#11]
Ok, next time I'll remember that as a programer you don't need help.


Sejster(Posted 2008) [#12]
*pwned*

back to topic: I'm struggling with the same problem. Creating dummies to call methods that rather should be functions is horrible. There has to be an elegant workaround. I'll keep trying.


Sejster(Posted 2008) [#13]
Okay, I just came up with a solution to the problem in my case, but it's not universal: I created another type, which contains the former globals I want access to as fields. All original type objects link to objects of that new type, which (in my case) are no dummies, because they serve other purposes as well and aren't deleted after field/method access.

How about a reflection extension in the next official patch?


Otus(Posted 2008) [#14]
Is it technically harder to implement reflection on functions? :/ I don't understand why it it only for methods!


I imagine it's because of the way functions are handled in BlitzMax. They are a type of "object" that can be assigned to variables - unlike methods.

("Object", because they don't derive from Object.)

How about a reflection extension in the next official patch?


That would be nice!


FOODy(Posted 2008) [#15]
How about this little workaround/hack?



Just had to remove the obj-reference-push in the _Call Function from the Reflection Module.


- FOODy


Dreamora(Posted 2008) [#16]
How about use attributes to create entries for functions, globals and const?
That can even be automated through a pretty simple precompiler.


LordChaos(Posted 2008) [#17]
@FOODy: This looks pretty interesting. Will have a look at it!

@Dreamora: Don't really get your point. :/