#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
#endif
module Language.Javascript.JSaddle.Properties (
JSPropRef(..)
, MakePropRef(..)
, objGetPropertyByName
, objGetPropertyAtIndex
, objGetProperty
, objGetProperty'
, objSetPropertyByName
, objSetPropertyAtIndex
, objSetProperty
) where
import Control.Applicative ((<$>))
import Language.Javascript.JSaddle.PropRef (JSPropRef(..))
import Language.Javascript.JSaddle.Classes
(MakePropRef(..), MakeObjectRef(..), MakeValueRef(..),
MakeArgRefs(..), MakeStringRef(..))
import Language.Javascript.JSaddle.Monad (JSM)
import Language.Javascript.JSaddle.Types
(JSValueRefRef, JSObjectRef, JSPropertyAttributes,
Index(..), JSStringRef)
#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
#else
import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
(jsobjectgetpropertyatindex, jsobjectgetproperty,
jsobjectsetpropertyatindex, jsobjectsetproperty,
JSPropertyAttributes)
#endif
import Control.Monad.Trans.Reader (ask)
import Control.Monad.IO.Class (MonadIO(..))
import Language.Javascript.JSaddle.Exception (rethrow)
import Language.Javascript.JSaddle.Value (JSValueRef)
import Language.Javascript.JSaddle.Arguments ()
import Language.Javascript.JSaddle.String ()
instance MakePropRef JSPropRef where
makePropRef = return
instance MakePropRef prop => MakePropRef (JSM prop) where
makePropRef prop = prop >>= makePropRef
instance MakeObjectRef JSPropRef where
makeObjectRef = objGetProperty
instance MakeValueRef JSPropRef where
makeValueRef = objGetProperty
instance MakeArgRefs JSPropRef where
makeArgRefs p = do
rarg <- objGetProperty p
return [rarg]
objGetPropertyByName :: MakeStringRef name
=> JSObjectRef
-> name
-> JSValueRefRef
-> JSM JSValueRef
#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
objGetPropertyByName this name = liftIO . js_tryGetProp (makeStringRef name) this
foreign import javascript unsafe "try { $r=$2[$1] } catch(e) { $3[0] = e }"
js_tryGetProp :: JSStringRef -> JSObjectRef -> JSValueRefRef -> IO JSValueRef
#elif defined(USE_WEBKIT)
objGetPropertyByName this name exceptions = do
gctxt <- ask
liftIO $ jsobjectgetproperty gctxt this (makeStringRef name) exceptions
#else
objGetPropertyByName = undefined
#endif
objGetPropertyAtIndex :: JSObjectRef
-> Index
-> JSValueRefRef
-> JSM JSValueRef
#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
objGetPropertyAtIndex this index = liftIO . js_tryIndex index this
foreign import javascript unsafe "try { $r=$2[$1] } catch(e) { $3[0] = e }"
js_tryIndex :: Index -> JSObjectRef -> JSValueRefRef -> IO JSValueRef
#elif defined(USE_WEBKIT)
objGetPropertyAtIndex this index exceptions = do
gctxt <- ask
liftIO $ jsobjectgetpropertyatindex gctxt this index exceptions
#else
objGetPropertyAtIndex = undefined
#endif
objGetProperty :: JSPropRef
-> JSM JSValueRef
objGetProperty (JSPropRef this name ) =
rethrow $ objGetPropertyByName this name
objGetProperty (JSPropIndexRef this index) =
rethrow $ objGetPropertyAtIndex this index
objGetProperty' :: JSPropRef
-> JSM (JSObjectRef, JSValueRef)
objGetProperty' (JSPropRef this name) = do
p <- rethrow $ objGetPropertyByName this name
return (this, p)
objGetProperty' (JSPropIndexRef this index) = do
p <- rethrow $ objGetPropertyAtIndex this index
return (this, p)
objSetPropertyByName :: (MakeStringRef name, MakeValueRef val)
=> JSObjectRef
-> name
-> val
-> JSPropertyAttributes
-> JSValueRefRef
-> JSM ()
#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
objSetPropertyByName this name val attributes exceptions = do
vref <- makeValueRef val
liftIO $ js_trySetProp (makeStringRef name) this vref exceptions
foreign import javascript unsafe "try { $2[$1]=$3 } catch(e) { $4[0] = e }"
js_trySetProp :: JSStringRef -> JSObjectRef -> JSValueRef -> JSValueRefRef -> IO ()
#elif defined(USE_WEBKIT)
objSetPropertyByName this name val attributes exceptions = do
gctxt <- ask
vref <- makeValueRef val
liftIO $ jsobjectsetproperty gctxt this (makeStringRef name) vref attributes exceptions
#else
objSetPropertyByName = undefined
#endif
objSetPropertyAtIndex :: (MakeValueRef val)
=> JSObjectRef
-> Index
-> val
-> JSValueRefRef
-> JSM ()
#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
objSetPropertyAtIndex this index val exceptions = do
vref <- makeValueRef val
liftIO $ js_trySetAtIndex index this vref exceptions
foreign import javascript unsafe "try { $2[$1]=$3 } catch(e) { $4[0] = e }"
js_trySetAtIndex :: Index -> JSObjectRef -> JSValueRef -> JSValueRefRef -> IO ()
#elif defined(USE_WEBKIT)
objSetPropertyAtIndex this index val exceptions = do
gctxt <- ask
vref <- makeValueRef val
liftIO $ jsobjectsetpropertyatindex gctxt this index vref exceptions
#else
objSetPropertyAtIndex = undefined
#endif
objSetProperty :: (MakeValueRef val)
=> JSPropRef
-> val
-> JSM ()
objSetProperty (JSPropRef this name ) val = rethrow $ objSetPropertyByName this name val 0
objSetProperty (JSPropIndexRef this index) val = rethrow $ objSetPropertyAtIndex this index val