Well, I found that I couldn't get it to work directly. There might be a way but from what I could tell BlitzMax won't let you put a BytePointer or any other kind of pointer (except another function pointer) in a function pointer. Maybe this is a features that they can add in a future release.
I did however figure out a way to sneak behind Blitz's back (with regards to type checking)...
Local mytempfunction() 'function pointer
Local mybytepointer:Byte Ptr=WhereverYouWant
'then to turn the machine code at the Byte Ptr into a callable function:
Local where:Int Ptr=Int Ptr(Byte Ptr(VarPtr(mytempfunction)))
mytempbank:TBank=StaticBank(where,4) 'not sure on the syntax - make it look at the function pointer memory 4-bytes
PokeInt where,Int(Int Ptr(mybytepointer)) 'not sure on syntax - overwrite the function pointer content with a byte pointer
mytempfunction() 'call it
Something like that - I don't remember the syntax for creating a static bank or doing the PokeInt off-hand. Basically what you're trying to do here is get the location of the function pointer using VarPtr (which actually works) - and btw you have to first turn a functionpointer into a bytepointer and then into an intpointer (that's the only way it would accept it for me anyway). Then you cludge a static bank onto the memory area of the function pointer variable itself, and write to that memory the pointer that you want the function to represent. Then you call the function. It should then jump to whatever routine you are pointing your byte pointer at - so it better be something executable. You can point it at the byte-pointer version of another function's function pointer if you like, but then you might as well do myfunction()=otherfunction. So I hope whatever you're pointing it at is valid executable code that can run and return to Blitz okay.
I think that's pretty much how I found to do it. BlitzMax wouldn't let me convert the function pointer to anything other than a byte pointer (if I remember rightly), and certainly wouldn't let something be put into the function pointer except for other function pointers. You might need to play with the type casting a little bit if this doesn't work exactly as is.
Hey Mark & Co, whatever happened to good old Peek and Poke without needing a bank? Trying to encourage good programming practice? ;-)
|