ghcjs / ghcjs-base

base library for GHCJS for JavaScript interaction and marshalling, used by higher level libraries like JSC
MIT License
45 stars 67 forks source link

Export constructor for JSString #84

Closed s9gf4ult closed 7 years ago

s9gf4ult commented 7 years ago

Is there any reason to not export JSString constructor? It is implemented as newtype around JSVal and not exported constructor cause to use unsafeCoerce :: JSVal -> JSString and vise versa instead of coerce or explicit packing/unpacking.

hamishmack commented 7 years ago

IIRC pToJSVal and pFromJSVal should work (and jsval will also get the JSVal for a JSString). However if you want to be sure your code is portable to GHC you might need to use toJSVal and fromJSVal.

For instance in jsaddle (which tries to provide a ghcjs-base compatible interface on GHC), JSString is newtype JSString = JSString Text. This allows for pure conversion to/from Text (Data.JSString.Text), but requires conversion to/from JSVal must be done with toJSVal and fromJSVal (the monad is needed to determine the JavaScript context to create the JSVal in).