|
2 | 2 | UnboxedTuples, DeriveDataTypeable, GHCForeignImportPrim,
|
3 | 3 | MagicHash, FlexibleInstances, BangPatterns, Rank2Types, CPP #-}
|
4 | 4 |
|
5 |
| -{- | Basic interop between Haskell and JavaScript. |
6 |
| -
|
7 |
| - The principal type here is 'JSVal', which is a lifted type that contains |
8 |
| - a JavaScript reference. The 'JSVal' type is parameterized with one phantom |
9 |
| - type, and GHCJS.Types defines several type synonyms for specific variants. |
10 |
| -
|
11 |
| - The code in this module makes no assumptions about 'JSVal a' types. |
12 |
| - Operations that can result in a JS exception that can kill a Haskell thread |
13 |
| - are marked unsafe (for example if the 'JSVal' contains a null or undefined |
14 |
| - value). There are safe variants where the JS exception is propagated as |
15 |
| - a Haskell exception, so that it can be handled on the Haskell side. |
16 |
| -
|
17 |
| - For more specific types, like 'JSArray' or 'JSBool', the code assumes that |
18 |
| - the contents of the 'JSVal' actually is a JavaScript array or bool value. |
19 |
| - If it contains an unexpected value, the code can result in exceptions that |
20 |
| - kill the Haskell thread, even for functions not marked unsafe. |
21 |
| -
|
22 |
| - The code makes use of `foreign import javascript', enabled with the |
23 |
| - `JavaScriptFFI` extension, available since GHC 7.8. There are three different |
24 |
| - safety levels: |
25 |
| -
|
26 |
| - * unsafe: The imported code is run directly. returning an incorrectly typed |
27 |
| - value leads to undefined behaviour. JavaScript exceptions in the foreign |
28 |
| - code kill the Haskell thread. |
29 |
| - * safe: Returned values are replaced with a default value if they have |
30 |
| - the wrong type. JavaScript exceptions are caught and propagated as |
31 |
| - Haskell exceptions ('JSException'), so they can be handled with the |
32 |
| - standard "Control.Exception" machinery. |
33 |
| - * interruptible: The import is asynchronous. The calling Haskell thread |
34 |
| - sleeps until the foreign code calls the `$c` JavaScript function with |
35 |
| - the result. The thread is in interruptible state while blocked, so it |
36 |
| - can receive asynchronous exceptions. |
37 |
| -
|
38 |
| - Unlike the FFI for native code, it's safe to call back into Haskell |
39 |
| - (`h$run`, `h$runSync`) from foreign code in any of the safety levels. |
40 |
| - Since JavaScript is single threaded, no Haskell threads can run while |
41 |
| - the foreign code is running. |
42 |
| - -} |
43 |
| - |
44 |
| -module GHCJS.Foreign.Internal ( JSType(..) |
| 5 | +module GHCJS.Foreign.Internal ( JSTypeOf(..) |
45 | 6 | , jsTypeOf
|
46 |
| - , JSONType(..) |
| 7 | + , JSONTypeOf(..) |
47 | 8 | , jsonTypeOf
|
48 | 9 | -- , mvarRef
|
49 | 10 | , isTruthy
|
@@ -108,25 +69,25 @@ import qualified Data.Text.Lazy as TL (Text, toStrict, fromStrict)
|
108 | 69 | import Unsafe.Coerce
|
109 | 70 |
|
110 | 71 | -- types returned by JS typeof operator
|
111 |
| -data JSType = Undefined |
112 |
| - | Object |
113 |
| - | Boolean |
114 |
| - | Number |
115 |
| - | String |
116 |
| - | Symbol |
117 |
| - | Function |
118 |
| - | Other -- ^ implementation dependent |
119 |
| - deriving (Show, Eq, Ord, Enum, Typeable) |
| 72 | +data JSTypeOf = Undefined |
| 73 | + | Object |
| 74 | + | Boolean |
| 75 | + | Number |
| 76 | + | String |
| 77 | + | Symbol |
| 78 | + | Function |
| 79 | + | Other -- ^ implementation dependent |
| 80 | + deriving (Show, Eq, Ord, Enum, Typeable) |
120 | 81 |
|
121 | 82 | -- JSON value type
|
122 |
| -data JSONType = JSONNull |
123 |
| - | JSONInteger |
124 |
| - | JSONFloat |
125 |
| - | JSONBool |
126 |
| - | JSONString |
127 |
| - | JSONArray |
128 |
| - | JSONObject |
129 |
| - deriving (Show, Eq, Ord, Enum, Typeable) |
| 83 | +data JSONTypeOf = JSONNull |
| 84 | + | JSONInteger |
| 85 | + | JSONFloat |
| 86 | + | JSONBool |
| 87 | + | JSONString |
| 88 | + | JSONArray |
| 89 | + | JSONObject |
| 90 | + deriving (Show, Eq, Ord, Enum, Typeable) |
130 | 91 |
|
131 | 92 | fromJSBool :: JSVal -> Bool
|
132 | 93 | fromJSBool b = js_fromBool b
|
@@ -257,11 +218,11 @@ listProps :: JSVal a -> IO [JSString]
|
257 | 218 | listProps o = fmap unsafeCoerce . Prim.fromJSArray =<< js_listProps o
|
258 | 219 | {-# INLINE listProps #-}
|
259 | 220 | -}
|
260 |
| -jsTypeOf :: JSVal -> JSType |
| 221 | +jsTypeOf :: JSVal -> JSTypeOf |
261 | 222 | jsTypeOf r = tagToEnum# (js_jsTypeOf r)
|
262 | 223 | {-# INLINE jsTypeOf #-}
|
263 | 224 |
|
264 |
| -jsonTypeOf :: JSVal -> JSONType |
| 225 | +jsonTypeOf :: JSVal -> JSONTypeOf |
265 | 226 | jsonTypeOf r = tagToEnum# (js_jsonTypeOf r)
|
266 | 227 | {-# INLINE jsonTypeOf #-}
|
267 | 228 |
|
|
0 commit comments