How to catch a Haskell exception that is thrown in a Haskell callback function called by a C function?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 00:12:31

You have to do this manually, which would look like this:

  • Wrap your callback function in Haskell code that calls try, and then serializes the resulting Either SomeException () to a format you can process from C (you can use a StablePtr for the SomeException, but the point is that you need to handle the Either somehow).

  • Where your C code calls the callback, check whether the result was a Left exn and if so, propagate the error to the top level of your C code, freeing resources as appropriate along the way. This step is non-mechanical, since C does not have exceptions.

  • At the top level of your C code, re-serialize the exception or result and read it in a Haskell function that wraps your call to the C code, raising an exception or returning the result as appropriate.

I'm not aware of any example of a program that does this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!