#ifdef ghcjs_HOST_OS
#endif
module Language.Javascript.JSaddle.Properties (
objGetPropertyByName
, objGetPropertyAtIndex
, objSetPropertyByName
, objSetPropertyAtIndex
) where
import Language.Javascript.JSaddle.Classes
(ToJSVal(..), ToJSString(..))
import Language.Javascript.JSaddle.Monad (askJSM, JSM)
import Language.Javascript.JSaddle.Types (Object(..))
#ifdef ghcjs_HOST_OS
import Language.Javascript.JSaddle.Types (JSString)
#else
import Language.Javascript.JSaddle.Native
(withObject, withJSString, withToJSVal)
import Language.Javascript.JSaddle.WebSockets
(AsyncCommand(..), sendLazyCommand, sendAsyncCommand)
#endif
import Language.Javascript.JSaddle.Value (JSVal)
import Language.Javascript.JSaddle.Arguments ()
import Language.Javascript.JSaddle.String ()
objGetPropertyByName :: ToJSString name
=> Object
-> name
-> JSM JSVal
#ifdef ghcjs_HOST_OS
objGetPropertyByName this name = js_tryGetProp (toJSString name) this
foreign import javascript unsafe "$r=$2[$1]"
js_tryGetProp :: JSString -> Object -> IO JSVal
#else
objGetPropertyByName this name =
withObject this $ \rthis ->
withJSString (toJSString name) $
sendLazyCommand . GetPropertyByName rthis
#endif
objGetPropertyAtIndex :: Object
-> Int
-> JSM JSVal
#ifdef ghcjs_HOST_OS
objGetPropertyAtIndex this index = js_tryIndex index this
foreign import javascript unsafe "$r=$2[$1]"
js_tryIndex :: Int -> Object -> IO JSVal
#else
objGetPropertyAtIndex this index =
withObject this $ \rthis -> sendLazyCommand $ GetPropertyAtIndex rthis index
#endif
objSetPropertyByName :: (ToJSString name, ToJSVal val)
=> Object
-> name
-> val
-> JSM ()
#ifdef ghcjs_HOST_OS
objSetPropertyByName this name val = do
vref <- toJSVal val
js_trySetProp (toJSString name) this vref
foreign import javascript unsafe "$2[$1]=$3"
js_trySetProp :: JSString -> Object -> JSVal -> IO ()
#else
objSetPropertyByName this name val = do
let name' = toJSString name
withObject this $ \rthis ->
withJSString name' $ \rname ->
withToJSVal val $ \rval ->
sendAsyncCommand $ SetPropertyByName rthis rname rval
#endif
objSetPropertyAtIndex :: (ToJSVal val)
=> Object
-> Int
-> val
-> JSM ()
#ifdef ghcjs_HOST_OS
objSetPropertyAtIndex this index val = do
vref <- toJSVal val
js_trySetAtIndex index this vref
foreign import javascript unsafe "$2[$1]=$3"
js_trySetAtIndex :: Int -> Object -> JSVal -> IO ()
#else
objSetPropertyAtIndex this index val = do
gctxt <- askJSM
withObject this $ \rthis ->
withToJSVal val $ \rval ->
sendAsyncCommand $ SetPropertyAtIndex rthis index rval
#endif