{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Rank2Types #-}
#ifdef ghcjs_HOST_OS
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE JavaScriptFFI #-}
{-# OPTIONS_GHC -Wno-dodgy-exports -Wno-dodgy-imports #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Language.Javascript.JSaddle.Object (
Object(..)
, MakeObject(..)
, (!)
, (!!)
, js
, jss
, JSF
, jsf
, js0
, js1
, js2
, js3
, js4
, js5
, jsg
, jsgf
, jsg0
, jsg1
, jsg2
, jsg3
, jsg4
, jsg5
, (<#)
, (<##)
, (#)
, (##)
, new
, call
, obj
, create
, getProp
, unsafeGetProp
, setProp
, unsafeSetProp
, Function(..)
, function
, asyncFunction
, freeFunction
, fun
, JSCallAsFunction
, fromListIO
, array
, global
, listProps
, propertyNames
, properties
, objCallAsFunction
, objCallAsConstructor
, nullObject
) where
import Prelude hiding ((!!))
import Data.Coerce (coerce)
#ifdef ghcjs_HOST_OS
import GHCJS.Types (nullRef)
#if __GLASGOW_HASKELL__ >= 900
import GHC.JS.Foreign.Callback
#else
import GHCJS.Foreign.Callback
#endif
(releaseCallback, syncCallback2, asyncCallback2, OnBlocked(..), Callback)
import GHCJS.Marshal (ToJSVal(..))
import JavaScript.Array (MutableJSArray)
import qualified JavaScript.Array as Array (toListIO, fromListIO)
import JavaScript.Array.Internal (SomeJSArray(..))
import JavaScript.Object (create, listProps)
import Language.Javascript.JSaddle.Monad (JSM)
import Language.Javascript.JSaddle.Types
(JSString, Object(..),
JSVal(..), JSCallAsFunction)
#else
import GHCJS.Marshal.Internal (ToJSVal(..))
import Language.Javascript.JSaddle.Native
(newAsyncCallback, newSyncCallback, callAsFunction, callAsConstructor)
import Language.Javascript.JSaddle.Monad (askJSM, JSM)
import Language.Javascript.JSaddle.Types
(JSValueForSend(..), AsyncCommand(..), JSString, Object(..),
SomeJSArray(..), JSVal(..), JSCallAsFunction, JSContextRef(..))
import JavaScript.Object.Internal (create, listProps)
import Language.Javascript.JSaddle.Run (sendAsyncCommand)
#endif
import JavaScript.Array.Internal (fromListIO)
import Language.Javascript.JSaddle.Value (valToObject)
import Language.Javascript.JSaddle.Classes (MakeObject(..))
import Language.Javascript.JSaddle.Marshal.String (ToJSString(..))
import Language.Javascript.JSaddle.Arguments (MakeArgs(..))
import Control.Monad.IO.Class (MonadIO(..))
import Language.Javascript.JSaddle.Properties
import Control.Lens (IndexPreservingGetter, to)
import Data.IORef (newIORef, readIORef)
import System.IO.Unsafe (unsafePerformIO)
instance MakeObject v => MakeObject (JSM v) where
makeObject :: JSM v -> JSM Object
makeObject JSM v
v = JSM v
v JSM v -> (v -> JSM Object) -> JSM Object
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= v -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject
(!) :: (MakeObject this, ToJSString name)
=> this
-> name
-> JSM JSVal
this
this ! :: forall this name.
(MakeObject this, ToJSString name) =>
this -> name -> JSM JSVal
! name
name = do
Object
rthis <- this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this
Object -> name -> JSM JSVal
forall name. ToJSString name => Object -> name -> JSM JSVal
objGetPropertyByName Object
rthis name
name
(!!) :: (MakeObject this)
=> this
-> Int
-> JSM JSVal
this
this !! :: forall this. MakeObject this => this -> Int -> JSM JSVal
!! Int
index = do
Object
rthis <- this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this
Object -> Int -> JSM JSVal
objGetPropertyAtIndex Object
rthis Int
index
js :: (MakeObject s, ToJSString name)
=> name
-> IndexPreservingGetter s (JSM JSVal)
js :: forall s name.
(MakeObject s, ToJSString name) =>
name -> IndexPreservingGetter s (JSM JSVal)
js name
name = (s -> JSM JSVal) -> Optic' p f s (JSM JSVal)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (s -> name -> JSM JSVal
forall this name.
(MakeObject this, ToJSString name) =>
this -> name -> JSM JSVal
! name
name)
jss :: (ToJSString name, ToJSVal val)
=> name
-> val
-> forall o . MakeObject o => IndexPreservingGetter o (JSM ())
jss :: forall name val.
(ToJSString name, ToJSVal val) =>
name
-> val
-> forall o. MakeObject o => IndexPreservingGetter o (JSM ())
jss name
name val
val = (o -> JSM ()) -> Optic' p f o (JSM ())
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (\o
o -> o
o o -> name -> val -> JSM ()
forall this name val.
(MakeObject this, ToJSString name, ToJSVal val) =>
this -> name -> val -> JSM ()
<# name
name (val -> JSM ()) -> val -> JSM ()
forall a b. (a -> b) -> a -> b
$ val
val)
jsf :: (ToJSString name, MakeArgs args) => name -> args -> JSF
jsf :: forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSF
jsf name
name args
args = (o -> JSM JSVal) -> Optic' p f o (JSM JSVal)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to (\o
o -> o
o o -> name -> args -> JSM JSVal
forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
# name
name (args -> JSM JSVal) -> args -> JSM JSVal
forall a b. (a -> b) -> a -> b
$ args
args)
type JSF = forall o . MakeObject o => IndexPreservingGetter o (JSM JSVal)
js0 :: (ToJSString name) => name -> JSF
js0 :: forall name. ToJSString name => name -> JSF
js0 name
name = name -> () -> JSF
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSF
jsf name
name ()
js1 :: (ToJSString name, ToJSVal a0) => name -> a0 -> JSF
js1 :: forall name a0. (ToJSString name, ToJSVal a0) => name -> a0 -> JSF
js1 name
name a0
a0 = name -> [a0] -> JSF
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSF
jsf name
name [a0
a0]
js2 :: (ToJSString name, ToJSVal a0, ToJSVal a1) => name -> a0 -> a1 -> JSF
js2 :: forall name a0 a1.
(ToJSString name, ToJSVal a0, ToJSVal a1) =>
name -> a0 -> a1 -> JSF
js2 name
name a0
a0 a1
a1 = name -> (a0, a1) -> JSF
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSF
jsf name
name (a0
a0, a1
a1)
js3 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2)
=> name -> a0 -> a1 -> a2 -> JSF
js3 :: forall name a0 a1 a2.
(ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2) =>
name -> a0 -> a1 -> a2 -> JSF
js3 name
name a0
a0 a1
a1 a2
a2 = name -> (a0, a1, a2) -> JSF
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSF
jsf name
name (a0
a0, a1
a1, a2
a2)
js4 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2,
ToJSVal a3)
=> name -> a0 -> a1 -> a2 -> a3 -> JSF
js4 :: forall name a0 a1 a2 a3.
(ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2,
ToJSVal a3) =>
name -> a0 -> a1 -> a2 -> a3 -> JSF
js4 name
name a0
a0 a1
a1 a2
a2 a3
a3 = name -> (a0, a1, a2, a3) -> JSF
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSF
jsf name
name (a0
a0, a1
a1, a2
a2, a3
a3)
js5 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2,
ToJSVal a3, ToJSVal a4)
=> name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF
js5 :: forall name a0 a1 a2 a3 a4.
(ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3,
ToJSVal a4) =>
name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF
js5 name
name a0
a0 a1
a1 a2
a2 a3
a3 a4
a4 = name -> (a0, a1, a2, a3, a4) -> JSF
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSF
jsf name
name (a0
a0, a1
a1, a2
a2, a3
a3, a4
a4)
jsg :: ToJSString a => a -> JSM JSVal
jsg :: forall a. ToJSString a => a -> JSM JSVal
jsg a
name = Object
global Object -> a -> JSM JSVal
forall this name.
(MakeObject this, ToJSString name) =>
this -> name -> JSM JSVal
! a
name
jsgf :: (ToJSString name, MakeArgs args) => name -> args -> JSM JSVal
jsgf :: forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSM JSVal
jsgf name
name = Object
global Object -> name -> args -> JSM JSVal
forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
# name
name
jsg0 :: (ToJSString name) => name -> JSM JSVal
jsg0 :: forall a. ToJSString a => a -> JSM JSVal
jsg0 name
name = name -> () -> JSM JSVal
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSM JSVal
jsgf name
name ()
jsg1 :: (ToJSString name, ToJSVal a0) => name -> a0 -> JSM JSVal
jsg1 :: forall name a0.
(ToJSString name, ToJSVal a0) =>
name -> a0 -> JSM JSVal
jsg1 name
name a0
a0 = name -> [a0] -> JSM JSVal
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSM JSVal
jsgf name
name [a0
a0]
jsg2 :: (ToJSString name, ToJSVal a0, ToJSVal a1) => name -> a0 -> a1 -> JSM JSVal
jsg2 :: forall name a0 a1.
(ToJSString name, ToJSVal a0, ToJSVal a1) =>
name -> a0 -> a1 -> JSM JSVal
jsg2 name
name a0
a0 a1
a1 = name -> (a0, a1) -> JSM JSVal
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSM JSVal
jsgf name
name (a0
a0, a1
a1)
jsg3 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2)
=> name -> a0 -> a1 -> a2 -> JSM JSVal
jsg3 :: forall name a0 a1 a2.
(ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2) =>
name -> a0 -> a1 -> a2 -> JSM JSVal
jsg3 name
name a0
a0 a1
a1 a2
a2 = name -> (a0, a1, a2) -> JSM JSVal
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSM JSVal
jsgf name
name (a0
a0, a1
a1, a2
a2)
jsg4 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2,
ToJSVal a3)
=> name -> a0 -> a1 -> a2 -> a3 -> JSM JSVal
jsg4 :: forall name a0 a1 a2 a3.
(ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2,
ToJSVal a3) =>
name -> a0 -> a1 -> a2 -> a3 -> JSM JSVal
jsg4 name
name a0
a0 a1
a1 a2
a2 a3
a3 = name -> (a0, a1, a2, a3) -> JSM JSVal
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSM JSVal
jsgf name
name (a0
a0, a1
a1, a2
a2, a3
a3)
jsg5 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2,
ToJSVal a3, ToJSVal a4)
=> name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSM JSVal
jsg5 :: forall name a0 a1 a2 a3 a4.
(ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3,
ToJSVal a4) =>
name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSM JSVal
jsg5 name
name a0
a0 a1
a1 a2
a2 a3
a3 a4
a4 = name -> (a0, a1, a2, a3, a4) -> JSM JSVal
forall name args.
(ToJSString name, MakeArgs args) =>
name -> args -> JSM JSVal
jsgf name
name (a0
a0, a1
a1, a2
a2, a3
a3, a4
a4)
infixr 2 #
(#) :: (MakeObject this, ToJSString name, MakeArgs args)
=> this -> name -> args -> JSM JSVal
# :: forall this name args.
(MakeObject this, ToJSString name, MakeArgs args) =>
this -> name -> args -> JSM JSVal
(#) this
this name
name args
args = do
Object
rthis <- this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this
JSVal
f <- Object -> name -> JSM JSVal
forall name. ToJSString name => Object -> name -> JSM JSVal
objGetPropertyByName Object
rthis name
name
Object
f' <- JSVal -> JSM Object
forall value. ToJSVal value => value -> JSM Object
valToObject JSVal
f
Object -> Object -> args -> JSM JSVal
forall args. MakeArgs args => Object -> Object -> args -> JSM JSVal
objCallAsFunction Object
f' Object
rthis args
args
infixr 2 ##
(##) :: (MakeObject this, MakeArgs args)
=> this -> Int -> args -> JSM JSVal
## :: forall this args.
(MakeObject this, MakeArgs args) =>
this -> Int -> args -> JSM JSVal
(##) this
this Int
index args
args = do
Object
rthis <- this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this
JSVal
f <- Object -> Int -> JSM JSVal
objGetPropertyAtIndex Object
rthis Int
index
Object
f' <- JSVal -> JSM Object
forall value. ToJSVal value => value -> JSM Object
valToObject JSVal
f
Object -> Object -> args -> JSM JSVal
forall args. MakeArgs args => Object -> Object -> args -> JSM JSVal
objCallAsFunction Object
f' Object
rthis args
args
infixr 1 <#
(<#) :: (MakeObject this, ToJSString name, ToJSVal val)
=> this
-> name
-> val
-> JSM ()
<# :: forall this name val.
(MakeObject this, ToJSString name, ToJSVal val) =>
this -> name -> val -> JSM ()
(<#) this
this name
name val
val = do
Object
rthis <- this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this
Object -> name -> val -> JSM ()
forall name val.
(ToJSString name, ToJSVal val) =>
Object -> name -> val -> JSM ()
objSetPropertyByName Object
rthis name
name val
val
infixr 1 <##
(<##) :: (MakeObject this, ToJSVal val)
=> this
-> Int
-> val
-> JSM ()
<## :: forall this val.
(MakeObject this, ToJSVal val) =>
this -> Int -> val -> JSM ()
(<##) this
this Int
index val
val = do
Object
rthis <- this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this
Object -> Int -> val -> JSM ()
forall val. ToJSVal val => Object -> Int -> val -> JSM ()
objSetPropertyAtIndex Object
rthis Int
index val
val
new :: (MakeObject constructor, MakeArgs args)
=> constructor
-> args
-> JSM JSVal
new :: forall constructor args.
(MakeObject constructor, MakeArgs args) =>
constructor -> args -> JSM JSVal
new constructor
constructor args
args = do
Object
f <- constructor -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject constructor
constructor
Object -> args -> JSM JSVal
forall args. MakeArgs args => Object -> args -> JSM JSVal
objCallAsConstructor Object
f args
args
call :: (MakeObject f, MakeObject this, MakeArgs args)
=> f -> this -> args -> JSM JSVal
call :: forall f this args.
(MakeObject f, MakeObject this, MakeArgs args) =>
f -> this -> args -> JSM JSVal
call f
f this
this args
args = do
Object
rfunction <- f -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject f
f
Object
rthis <- this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this
Object -> Object -> args -> JSM JSVal
forall args. MakeArgs args => Object -> Object -> args -> JSM JSVal
objCallAsFunction Object
rfunction Object
rthis args
args
obj :: JSM Object
obj :: JSM Object
obj = JSM Object
create
fun :: JSCallAsFunction -> JSCallAsFunction
fun :: JSCallAsFunction -> JSCallAsFunction
fun = JSCallAsFunction -> JSCallAsFunction
forall a. a -> a
id
#ifdef ghcjs_HOST_OS
data Function = Function {functionCallback :: Callback (JSVal -> JSVal -> IO ()), functionObject :: Object}
#else
newtype Function = Function {Function -> Object
functionObject :: Object}
#endif
#ifdef ghcjs_HOST_OS
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(($1) => { return function () { $1(this, arguments); }; })"
#else
"$r = function () { $1(this, arguments); }"
#endif
makeFunctionWithCallback :: Callback (JSVal -> JSVal -> IO ()) -> IO Object
#endif
function :: JSCallAsFunction
-> JSM Function
#ifdef ghcjs_HOST_OS
function f = do
callback <- syncCallback2 ContinueAsync $ \this args -> do
rargs <- Array.toListIO (coerce args)
f this this rargs
Function callback <$> makeFunctionWithCallback callback
#else
function :: JSCallAsFunction -> JSM Function
function JSCallAsFunction
f = do
Object
object <- JSCallAsFunction -> JSM Object
newSyncCallback JSCallAsFunction
f
Function -> JSM Function
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Function -> JSM Function) -> Function -> JSM Function
forall a b. (a -> b) -> a -> b
$ Object -> Function
Function Object
object
#endif
asyncFunction :: JSCallAsFunction
-> JSM Function
#ifdef ghcjs_HOST_OS
asyncFunction f = do
callback <- asyncCallback2 $ \this args -> do
rargs <- Array.toListIO (coerce args)
f this this rargs
Function callback <$> makeFunctionWithCallback callback
#else
asyncFunction :: JSCallAsFunction -> JSM Function
asyncFunction JSCallAsFunction
f = do
Object
object <- JSCallAsFunction -> JSM Object
newAsyncCallback JSCallAsFunction
f
Function -> JSM Function
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return (Function -> JSM Function) -> Function -> JSM Function
forall a b. (a -> b) -> a -> b
$ Object -> Function
Function Object
object
#endif
freeFunction :: Function -> JSM ()
#ifdef ghcjs_HOST_OS
freeFunction (Function callback _) = liftIO $
releaseCallback callback
#else
freeFunction :: Function -> JSM ()
freeFunction (Function (Object (JSVal IORef JSValueRef
objectRef))) = do
JSValueRef
n <- IO JSValueRef -> JSM JSValueRef
forall a. IO a -> JSM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO JSValueRef -> JSM JSValueRef)
-> IO JSValueRef -> JSM JSValueRef
forall a b. (a -> b) -> a -> b
$ IORef JSValueRef -> IO JSValueRef
forall a. IORef a -> IO a
readIORef IORef JSValueRef
objectRef
AsyncCommand -> JSM ()
sendAsyncCommand (JSValueForSend -> AsyncCommand
FreeCallback (JSValueRef -> JSValueForSend
JSValueForSend JSValueRef
n))
#endif
instance ToJSVal Function where
toJSVal :: Function -> JSM JSVal
toJSVal = Object -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal (Object -> JSM JSVal)
-> (Function -> Object) -> Function -> JSM JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Function -> Object
functionObject
instance ToJSVal JSCallAsFunction where
toJSVal :: JSCallAsFunction -> JSM JSVal
toJSVal JSCallAsFunction
f = Function -> Object
functionObject (Function -> Object) -> JSM Function -> JSM Object
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSCallAsFunction -> JSM Function
function JSCallAsFunction
f JSM Object -> (Object -> JSM JSVal) -> JSM JSVal
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Object -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal
instance MakeArgs JSCallAsFunction where
makeArgs :: JSCallAsFunction -> JSM [JSVal]
makeArgs JSCallAsFunction
f = do
JSVal
rarg <- Function -> Object
functionObject (Function -> Object) -> JSM Function -> JSM Object
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> JSCallAsFunction -> JSM Function
function JSCallAsFunction
f JSM Object -> (Object -> JSM JSVal) -> JSM JSVal
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Object -> JSM JSVal
forall a. ToJSVal a => a -> JSM JSVal
toJSVal
[JSVal] -> JSM [JSVal]
forall a. a -> JSM a
forall (m :: * -> *) a. Monad m => a -> m a
return [JSVal
rarg]
array :: MakeArgs args => args -> JSM Object
array :: forall args. MakeArgs args => args -> JSM Object
array args
args = do
[JSVal]
rargs <- args -> JSM [JSVal]
forall this. MakeArgs this => this -> JSM [JSVal]
makeArgs args
args
JSVal -> Object
Object (JSVal -> Object)
-> (SomeJSArray Any -> JSVal) -> SomeJSArray Any -> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SomeJSArray Any -> JSVal
forall a b. Coercible a b => a -> b
coerce (SomeJSArray Any -> Object) -> JSM (SomeJSArray Any) -> JSM Object
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [JSVal] -> JSM (SomeJSArray Any)
forall (m :: MutabilityType (*)). [JSVal] -> JSM (SomeJSArray m)
fromListIO [JSVal]
rargs
#ifdef ghcjs_HOST_OS
foreign import javascript unsafe
#if __GLASGOW_HASKELL__ >= 900
"(() => { return globalThis; })"
#else
"$r = globalThis"
#endif
global :: Object
#else
global :: Object
global :: Object
global = JSVal -> Object
Object (JSVal -> Object)
-> (IO (IORef JSValueRef) -> JSVal)
-> IO (IORef JSValueRef)
-> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IORef JSValueRef -> JSVal
JSVal (IORef JSValueRef -> JSVal)
-> (IO (IORef JSValueRef) -> IORef JSValueRef)
-> IO (IORef JSValueRef)
-> JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (IORef JSValueRef) -> IORef JSValueRef
forall a. IO a -> a
unsafePerformIO (IO (IORef JSValueRef) -> Object)
-> IO (IORef JSValueRef) -> Object
forall a b. (a -> b) -> a -> b
$ JSValueRef -> IO (IORef JSValueRef)
forall a. a -> IO (IORef a)
newIORef JSValueRef
4
#endif
propertyNames :: MakeObject this => this -> JSM [JSString]
propertyNames :: forall this. MakeObject this => this -> JSM [JSString]
propertyNames this
this = this -> JSM Object
forall this. MakeObject this => this -> JSM Object
makeObject this
this JSM Object -> (Object -> JSM [JSString]) -> JSM [JSString]
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Object -> JSM [JSString]
listProps
properties :: MakeObject this => this -> JSM [JSVal]
properties :: forall this. MakeObject this => this -> JSM [JSVal]
properties this
this = this -> JSM [JSString]
forall this. MakeObject this => this -> JSM [JSString]
propertyNames this
this JSM [JSString] -> ([JSString] -> JSM [JSVal]) -> JSM [JSVal]
forall a b. JSM a -> (a -> JSM b) -> JSM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (JSString -> JSM JSVal) -> [JSString] -> JSM [JSVal]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (this
this this -> JSString -> JSM JSVal
forall this name.
(MakeObject this, ToJSString name) =>
this -> name -> JSM JSVal
!)
objCallAsFunction :: MakeArgs args
=> Object
-> Object
-> args
-> JSM JSVal
#ifdef ghcjs_HOST_OS
objCallAsFunction f this args = do
rargs <- makeArgs args >>= liftIO . Array.fromListIO
liftIO $ js_apply f this rargs
foreign import javascript safe
#if __GLASGOW_HASKELL__ >= 900
"(($1,$2,$3) => { return $1.apply($2, $3); })"
#else
"$r = $1.apply($2, $3)"
#endif
js_apply :: Object -> Object -> MutableJSArray -> IO JSVal
#else
objCallAsFunction :: forall args. MakeArgs args => Object -> Object -> args -> JSM JSVal
objCallAsFunction Object
f Object
this args
args = do
[JSVal]
rargs <- args -> JSM [JSVal]
forall this. MakeArgs this => this -> JSM [JSVal]
makeArgs args
args
Object -> Object -> [JSVal] -> JSM JSVal
callAsFunction Object
f Object
this [JSVal]
rargs
#endif
objCallAsConstructor :: MakeArgs args
=> Object
-> args
-> JSM JSVal
#ifdef ghcjs_HOST_OS
objCallAsConstructor f args = do
rargs <- makeArgs args >>= liftIO . Array.fromListIO
liftIO $ js_new f rargs
foreign import javascript safe
#if __GLASGOW_HASKELL__ >= 900
"(($1,$2) => {\
switch($2.length) {\
case 0 : $r = new $1(); break;\
case 1 : $r = new $1($2[0]); break;\
case 2 : $r = new $1($2[0],$2[1]); break;\
case 3 : $r = new $1($2[0],$2[1],$2[2]); break;\
case 4 : $r = new $1($2[0],$2[1],$2[2],$2[3]); break;\
case 5 : $r = new $1($2[0],$2[1],$2[2],$2[3],$2[4]); break;\
case 6 : $r = new $1($2[0],$2[1],$2[2],$2[3],$2[4],$2[5]); break;\
case 7 : $r = new $1($2[0],$2[1],$2[2],$2[3],$2[4],$2[5],$2[6]); break;\
default:\
var temp = function() {\
ret = $1.apply(this, $2);\
};\
temp.prototype = $1.prototype;\
var i = new temp();\
if(ret instanceof Object) {\
$r = ret;\
} else {\
i.constructor = $1;\
$r = i;\
}\
}\
return $r;\
})"
#else
"switch($2.length) {\
case 0 : $r = new $1(); break;\
case 1 : $r = new $1($2[0]); break;\
case 2 : $r = new $1($2[0],$2[1]); break;\
case 3 : $r = new $1($2[0],$2[1],$2[2]); break;\
case 4 : $r = new $1($2[0],$2[1],$2[2],$2[3]); break;\
case 5 : $r = new $1($2[0],$2[1],$2[2],$2[3],$2[4]); break;\
case 6 : $r = new $1($2[0],$2[1],$2[2],$2[3],$2[4],$2[5]); break;\
case 7 : $r = new $1($2[0],$2[1],$2[2],$2[3],$2[4],$2[5],$2[6]); break;\
default:\
var temp = function() {\
ret = $1.apply(this, $2);\
};\
temp.prototype = $1.prototype;\
var i = new temp();\
if(ret instanceof Object) {\
$r = ret;\
} else {\
i.constructor = $1;\
$r = i;\
}\
}"
#endif
js_new :: Object -> MutableJSArray -> IO JSVal
#else
objCallAsConstructor :: forall args. MakeArgs args => Object -> args -> JSM JSVal
objCallAsConstructor Object
f args
args = do
[JSVal]
rargs <- args -> JSM [JSVal]
forall this. MakeArgs this => this -> JSM [JSVal]
makeArgs args
args
Object -> [JSVal] -> JSM JSVal
callAsConstructor Object
f [JSVal]
rargs
#endif
nullObject :: Object
#ifdef ghcjs_HOST_OS
nullObject = Object nullRef
#else
nullObject :: Object
nullObject = JSVal -> Object
Object (JSVal -> Object)
-> (IO (IORef JSValueRef) -> JSVal)
-> IO (IORef JSValueRef)
-> Object
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IORef JSValueRef -> JSVal
JSVal (IORef JSValueRef -> JSVal)
-> (IO (IORef JSValueRef) -> IORef JSValueRef)
-> IO (IORef JSValueRef)
-> JSVal
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (IORef JSValueRef) -> IORef JSValueRef
forall a. IO a -> a
unsafePerformIO (IO (IORef JSValueRef) -> Object)
-> IO (IORef JSValueRef) -> Object
forall a b. (a -> b) -> a -> b
$ JSValueRef -> IO (IORef JSValueRef)
forall a. a -> IO (IORef a)
newIORef JSValueRef
0
#endif