Login  Register

Cannot coerce a BlockCallback to ExternalCallback*

Posted by Chris Uppal-3 on Apr 15, 2006; 12:27pm
URL: https://forum.world.st/Cannot-coerce-a-BlockCallback-to-ExternalCallback-tp3378227.html

I think the following shows a problem in Dolphin, but I doubt if there's any
way to fix it, so this post is more of a warning for the records than a bug
report.

I've been attempting to interface to an external library which makes extensive
use of callbacks, and been having a Great Deal Of Difficulty (tm).  The problem
was that whenever I attempted to use one of the exported functions which took a
callback as parameter, the call would fail with:

    Unhandled exception - an InvalidExternalCall('Invalid arg 3:
    Cannot coerce a BlockCallback to ExternalCallback*')

Which seemed odd enough in itself.  But what made it really puzzling was that
after a while it would start working, and thereafter would work beautifully
(modulo the bugs in the external library).

I've just worked out what the problem is.  My ExternalLibrary's methods are of
the form:

    enumerateSuggestions: aHunspell word: aString callback: anExternalCallback
         <stdcall: sdword enumerateSuggestions void* char* ExternalCallback*>
         ^ self invalidCall.

That will only work if #asParameter has /already/ been sent to the
ExternalCallback in question, since it's only then that it (re-)generates its
#thunk.  If that hasn't been sent then the VM reports (correctly) that the call
failed, and #invalidCall reports (misleadingly) that it was a coercion failure.

Since I was playing with this stuff in a workspace, I had one ExternalCallback
instance (actually a BlockCallback) which I kept re-using.  It wasn't until I
happened to send #asParameter to that BlockCallback that it started to work.
And from then on it continued, most bafflingly, to work...

Applies to D5 and D6.

    -- chris