GHC Wasm FFI Compilation

The GHC Wasm JSFFI only supports (apart from primitive types) JSVal and newtypes on top of JSVal, such as JSString. The issue here is that GHC doesn’t see that JSString is a newtype of JSVal, so it complains. (The error message could certainly be improved!)

The solution is to import the constructor of JSString:

import GHC.Wasm.Prim (JSString(..), fromJSString, toJSString)

Then your example will compile.


Note that this is analogous to “regular” C FFI: Compiling

import Foreign.C.Types (CInt)

foreign import capi "abs" absss :: CInt -> CInt

will fail with

    • Unacceptable result type in foreign declaration:
        ‘CInt’ cannot be marshalled in a foreign call
          because its data constructor is not in scope
    • When checking declaration:
        foreign import capi safe "abs" absss :: CInt -> CInt
    Suggested fix: Import the data constructor ‘CInt’ of ‘CInt’
   |
10 | foreign import capi "abs" absss :: CInt -> CInt
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 Likes