on develop help sign in
add withJSBool and getJSProp
commit2ea6efe56d7dca9ecfa855c43e6008b968880b08 authorAli Abrar <aliabrar@gmail.com> dateSun 26 Jul 2015, 03:56 (11 years ago) parent300ef93
1 file changed+17
src/Foreign/JavaScript/TH.hs+17
src/Foreign/JavaScript/TH.hs+17
@@ -74,6 +74,7 @@ class Monad m => MonadJS x m | m -> x where
7474 fromJSString :: JSRef x -> m String
7575 fromJSArray :: JSRef x -> m [JSRef x]
7676 fromJSNumber :: JSRef x -> m Double
77+ withJSBool :: Bool -> (JSRef x -> m r) -> m r
7778 withJSString :: String -> (JSRef x -> m r) -> m r
7879 withJSNumber :: Double -> (JSRef x -> m r) -> m r
7980 withJSArray :: [JSRef x] -> (JSRef x -> m r) -> m r
@@ -82,6 +83,7 @@ class Monad m => MonadJS x m | m -> x where
8283 mkJSFun :: ([JSRef x] -> m (JSRef x)) -> m (JSFun x) --TODO: Support 'this', exceptions
8384 freeJSFun :: JSFun x -> m ()
8485 setJSProp :: String -> JSRef x -> JSRef x -> m ()
86+ getJSProp :: String -> JSRef x -> m (JSRef x)
8587 withJSNode :: Node -> (JSRef x -> m r) -> m r
8688
8789#ifdef __GHCJS__
@@ -103,6 +105,7 @@ instance MonadJS JSCtx_IO IO where
103105 fromJSNumber (JSRef_IO r) = do
104106 Just n <- JS.fromJSRef $ JS.castRef r
105107 return n
108+ withJSBool b f = f $ JSRef_IO $ JS.castRef $ JS.toJSBool b
106109 withJSString s f = f $ JSRef_IO $ JS.castRef $ JS.toJSString s
107110 withJSNumber n f = do
108111 r <- JS.toJSRef n
@@ -121,6 +124,9 @@ instance MonadJS JSCtx_IO IO where
121124 liftM (JSFun . JSRef_IO) $ funWithArguments cb
122125 freeJSFun (JSFun (JSRef_IO r)) = JS.release $ JS.castRef r
123126 setJSProp s (JSRef_IO o) (JSRef_IO v) = JS.setProp s o v
127+ getJSProp s (JSRef_IO o) = do
128+ r <- JS.getProp s o
129+ return $ JSRef_IO r
124130 withJSNode n f = f $ JSRef_IO $ JS.castRef $ unNode n
125131
126132foreign import javascript unsafe "new Uint8Array($1_1.buf, $1_2, $2)" extractByteArray :: Ptr CChar -> Int -> IO (JS.JSRef (JSRef JSCtx_IO))
@@ -187,6 +193,10 @@ instance MonadJS (JSCtx_JavaScriptCore x) (WithWebView x IO) where
187193 allocaBytes (fromIntegral l) $ \ps -> do
188194 _ <- jsstringgetutf8cstring'_ s ps (fromIntegral l)
189195 peekCString ps
196+ withJSBool b a = do
197+ jsContext <- askJSContext
198+ valRef <- liftIO $ jsvaluemakeboolean jsContext bRef
199+ a $ JSRef_JavaScriptCore valRef
190200 withJSString str a = do
191201 jsContext <- askJSContext
192202 withJSStringRaw str $ \strRef -> do
@@ -237,6 +247,10 @@ instance MonadJS (JSCtx_JavaScriptCore x) (WithWebView x IO) where
237247 withJSStringRaw propName $ \propNameRaw -> do
238248 jsContext <- askJSContext
239249 liftIO $ jsobjectsetproperty jsContext objRef propNameRaw valRef 0 nullPtr --TODO: property attribute, exceptions
250+ getJSProp propName (JSRef_JavaScriptCore objRef) = do
251+ withJSStringRaw propName $ \propNameRaw -> do
252+ jsContext <- askJSContext
253+ liftIO $ jsobjectgetproperty jsContext objRef propNameRaw 0 nullPtr --TODO: property attribute, exceptions
240254 withJSNode (Node n) f = do
241255 jsContext <- askJSContext
242256 withForeignPtr n $ \nPtr -> do
@@ -266,6 +280,9 @@ instance FromJS x () where
266280instance FromJS x Bool where
267281 fromJS = fromJSBool
268282
283+instance ToJS x Bool where
284+ withJS = withJSBool
285+
269286instance FromJS x String where
270287 fromJS = fromJSString
271288