{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
#ifdef ghcjs_HOST_OS
{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}
#endif
module Language.Javascript.JSaddle.Properties (
getProp, unsafeGetProp
, objGetPropertyByName
, objGetPropertyAtIndex
, setProp, unsafeSetProp
, objSetPropertyByName
, objSetPropertyAtIndex
) where
import Language.Javascript.JSaddle.Monad (JSM)
import Language.Javascript.JSaddle.Types (JSVal, Object(..))
import JavaScript.Object.Internal (getProp, unsafeGetProp, setProp, unsafeSetProp)
#ifdef ghcjs_HOST_OS
import GHCJS.Marshal (ToJSVal(..))
#else
import GHCJS.Marshal.Internal (ToJSVal(..))
import Language.Javascript.JSaddle.Native
(withObject, withToJSVal)
import Language.Javascript.JSaddle.Run
(AsyncCommand(..), sendLazyCommand, sendAsyncCommand)
#endif
import Language.Javascript.JSaddle.Arguments ()
import Language.Javascript.JSaddle.String ()
import Language.Javascript.JSaddle.Marshal.String (ToJSString(..))
objGetPropertyByName :: ToJSString name
=> Object
-> name
-> JSM JSVal
objGetPropertyByName :: forall name. ToJSString name => Object -> name -> JSM JSVal
objGetPropertyByName Object
this name
name = JSString -> Object -> JSM JSVal
unsafeGetProp (name -> JSString
forall a. ToJSString a => a -> JSString
toJSString name
name) Object
this
objGetPropertyAtIndex :: Object
-> Int
-> JSM JSVal
#ifdef ghcjs_HOST_OS
objGetPropertyAtIndex this index = js_tryIndex index this
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1,$2) => { return $2[$1]; })"
#else
"$r=$2[$1]"
#endif
js_tryIndex :: Int -> Object -> IO JSVal
#else
objGetPropertyAtIndex :: Object -> Int -> JSM JSVal
objGetPropertyAtIndex Object
this Int
index =
Object -> (JSObjectForSend -> JSM JSVal) -> JSM JSVal
forall (m :: * -> *) a.
MonadIO m =>
Object -> (JSObjectForSend -> m a) -> m a
withObject Object
this ((JSObjectForSend -> JSM JSVal) -> JSM JSVal)
-> (JSObjectForSend -> JSM JSVal) -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ \JSObjectForSend
rthis -> (JSValueForSend -> AsyncCommand) -> JSM JSVal
sendLazyCommand ((JSValueForSend -> AsyncCommand) -> JSM JSVal)
-> (JSValueForSend -> AsyncCommand) -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ JSObjectForSend -> Int -> JSValueForSend -> AsyncCommand
GetPropertyAtIndex JSObjectForSend
rthis Int
index
#endif
objSetPropertyByName :: (ToJSString name, ToJSVal val)
=> Object
-> name
-> val
-> JSM ()
objSetPropertyByName :: forall name val.
(ToJSString name, ToJSVal val) =>
Object -> name -> val -> JSM ()
objSetPropertyByName Object
this name
name val
val = do
JSVal
vref <- val -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal val
val
JSString -> JSVal -> Object -> JSM ()
unsafeSetProp (name -> JSString
forall a. ToJSString a => a -> JSString
toJSString name
name) JSVal
vref Object
this
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
#if __GLASGOW_HASKELL__ >= 900
"(($1,$2,$3) => { return $2[$1]=$3; })"
#else
"$2[$1]=$3"
#endif
js_trySetAtIndex :: Int -> Object -> JSVal -> IO ()
#else
objSetPropertyAtIndex :: forall val. ToJSVal val => Object -> Int -> val -> JSM ()
objSetPropertyAtIndex Object
this Int
index val
val =
Object -> (JSObjectForSend -> JSM ()) -> JSM ()
forall (m :: * -> *) a.
MonadIO m =>
Object -> (JSObjectForSend -> m a) -> m a
withObject Object
this ((JSObjectForSend -> JSM ()) -> JSM ())
-> (JSObjectForSend -> JSM ()) -> JSM ()
forall a b. (a -> b) -> a -> b
$ \JSObjectForSend
rthis ->
val -> (JSValueForSend -> JSM ()) -> JSM ()
forall val a.
ToJSVal val =>
val -> (JSValueForSend -> JSM a) -> JSM a
withToJSVal val
val ((JSValueForSend -> JSM ()) -> JSM ())
-> (JSValueForSend -> JSM ()) -> JSM ()
forall a b. (a -> b) -> a -> b
$ \JSValueForSend
rval ->
AsyncCommand -> JSM ()
sendAsyncCommand (AsyncCommand -> JSM ()) -> AsyncCommand -> JSM ()
forall a b. (a -> b) -> a -> b
$ JSObjectForSend -> Int -> JSValueForSend -> AsyncCommand
SetPropertyAtIndex JSObjectForSend
rthis Int
index JSValueForSend
rval
#endif