5

I'm trying to port the haskell library minisat to JavaScript using ghcjs, for integration in a larger haskell-ghcjs project.

minisat contains a couple of ffi imports from a c library. I've manged to compile the c library to javascript using emscripten, and to export the functions that minisat requires. So far, so good.

However, there are a couple of imports that look like this:

foreign import ccall safe minisat_solve :: Solver -> Int -> Ptr (Lit) -> IO (Bool)

which imports a function that looks like this:

int minisat_solve(minisat_solver *s, int len, minisat_Lit *ps)

My understanding, from the documentation, is that when emscripten exports a function that takes or returns a pointer, the pointer becomes a JavaScript number type.

The ghcjs documentation suggests that it should be possible to leave the existing foreign imports in place, by appropriately wrapping a JavaScript function. However, ghcjs represents pointer types as roughly a pair consisting of a JavaScript object and number.

I think the wrapper code should be roughly

function h$minisat_solve(...){
   ...
   minisat_solve(...)
   ...
}

function minisat_solve = Module.cwrap('minisat_solve',...,...)

But I'm stumped by the type mismatch.

So, here's the challenge: Explain how to properly wrap an emscripten export for ccall import by ghcjs, using the above wrapper code as an example (or a counterexample, if I've got it completely wrong)

1 Answers1

0

Pointer types can be converted to and from integers: https://hackage.haskell.org/package/base-4.10.0.0/docs/Foreign-Ptr.html#t:IntPtr . Thus, you ought to be able to convert to / from any format that emscripten requires using those functions.

bitonic
  • 126
  • 1
  • 7