{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
#ifdef ghcjs_HOST_OS
{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}
#endif
-----------------------------------------------------------------------------
--
-- Module      :  Language.Javascript.JSaddle.Properties
-- Copyright   :  (c) Hamish Mackenzie
-- License     :  MIT
--
-- Maintainer  :  Hamish Mackenzie <Hamish.K.Mackenzie@googlemail.com>
--
-- | Low level JavaScript object property access.  In most cases you
--   should use "Language.Javascript.JSaddle.Object" instead.
--
--   This module is mostly here to implement functions needed to use
--   'JSPropRef'.
--
-----------------------------------------------------------------------------

module Language.Javascript.JSaddle.Properties (
  -- * Getting Property Values
    getProp, unsafeGetProp
  , objGetPropertyByName
  , objGetPropertyAtIndex
  -- * Setting Property Values
  , 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(..))

-- | Get a property value given the object and the name of the property.
objGetPropertyByName :: ToJSString name
                     => Object         -- ^ object to find the property on.
                     -> name           -- ^ name of the property.
                     -> JSM JSVal      -- ^ returns the property value.
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

-- | Get a property value given the object and the index of the property.
objGetPropertyAtIndex :: Object    -- ^ object to find the property on.
                      -> Int       -- ^ index of the property.
                      -> JSM JSVal -- ^ returns the property value.
#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

-- | Set a property value given the object and the name of the property.
objSetPropertyByName :: (ToJSString name, ToJSVal val)
                     => Object               -- ^ object to set the property on.
                     -> name                 -- ^ name of the property.
                     -> val                  -- ^ new value to set the property to.
                     -> 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

-- | Set a property value given the object and the index of the property.
objSetPropertyAtIndex :: (ToJSVal val)
                      => Object         -- ^ object to find property on.
                      -> Int          -- ^ index of the property.
                      -> val            -- ^ new value to set the property to.
                      -> 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