Now that I've got my Haxe code and C++ code nicely statically linked together, I've hit a slight snag.
I need to pass a function pointer to the C++ code, which the C++ code will store and call back later.
My Haxe code looks something like this:
public inline function setFunc(func:Int->Void):Void
{
untyped __global__.CoreSetFunc(func);
}
And Haxe compiles this into the following C++ code (with the stack-marking code removed here for clarity):
Void Core_obj::setFunc(Dynamic func)
{
::CoreSetRender(func);
return null();
}
As you can see, it's changed the "Int->Void" function pointer into a "Dynamic" type, so my C++ code is receiving a "Dynamic" type.
The question is, how do I cleanly and properly call the function - passing that "int" parameter - that's wrapped up inside that "Dynamic" type?
I can just directly call "func(10);" and the function executes. But when I trace the parameter passed to it, it's not "10" as expected, but is zero.
(I also experimented a little to try to work out what was going on and called "func(10, 10);" - yes, this is quite wrong, as the function only takes one parameter and I'm giving it two - and the interesting thing is that the function does then receive "10" as the parameter. But this isn't a proper solution, of course, because I'm doing it wrong and while it works in one place, in another place, it causes a segmentation fault.)
Is there some method of "Dynamic" that allows me to call the function wrapped inside it, passing the parameter? Or, failing that, some way to safely cast the "Dynamic" into the "void (*)(int)" type that C++ understands?
(And is there any calling conventions / stack protocol stuff I need to be aware of? The "Int->Void" functions passed to the C++ code are always static functions. No "this" pointer is involved here.)