Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This package provides an EDSL for calling JavaScript that can be used both from GHCJS and GHC. When using GHC the application is run using Warp and WebSockets to drive a small JavaScript helper.
Synopsis
- newtype Function = Function {}
- data Result
- = DeRefValResult JSValueRef Text
- | ValueToBoolResult Bool
- | ValueToNumberResult Double
- | ValueToStringResult JSStringReceived
- | ValueToJSONResult JSStringReceived
- | ValueToJSONValueResult Value
- | IsNullResult Bool
- | IsUndefinedResult Bool
- | StrictEqualResult Bool
- | InstanceOfResult Bool
- | PropertyNamesResult [JSStringReceived]
- | ThrowJSValue JSValueReceived
- | SyncResult
- newtype Object = Object JSVal
- type Mutable = Mutable_ ()
- class MakeArgs this where
- class ToJSVal a where
- class PToJSVal a where
- class PFromJSVal a where
- pFromJSVal :: JSVal -> a
- class FromJSVal a where
- fromJSVal :: JSVal -> JSM (Maybe a)
- fromJSValUnchecked :: JSVal -> JSM a
- fromJSValListOf :: JSVal -> JSM (Maybe [a])
- fromJSValUncheckedListOf :: JSVal -> JSM [a]
- class ToJSVal a => ToJSString a where
- toJSString :: a -> JSString
- class FromJSVal a => FromJSString a where
- fromJSString :: JSString -> a
- class MakeObject this where
- makeObject :: this -> JSM Object
- newtype JSM a = JSM {
- unJSM :: ReaderT JSContextRef IO a
- newtype JSVal = JSVal (IORef JSValueRef)
- newtype JSException = JSException JSVal
- newtype JSString = JSString Text
- data JSContextRef = JSContextRef {
- contextId :: Int64
- startTime :: UTCTime
- doSendCommand :: Command -> IO Result
- doSendAsyncCommand :: AsyncCommand -> IO ()
- addCallback :: Object -> JSCallAsFunction -> IO ()
- nextRef :: TVar JSValueRef
- doEnableLogging :: Bool -> IO ()
- finalizerThreads :: MVar (Set Text)
- animationFrameHandlers :: MVar [Double -> JSM ()]
- liveRefs :: MVar (Set Int64)
- class (Applicative m, MonadIO m) => MonadJSM m where
- type JSF = forall o. MakeObject o => IndexPreservingGetter o (JSM JSVal)
- type JSCallAsFunction = JSVal -> JSVal -> [JSVal] -> JSM ()
- newtype JSValueForSend = JSValueForSend JSValueRef
- data AsyncCommand
- = FreeRef Text JSValueForSend
- | FreeRefs Text
- | SetPropertyByName JSObjectForSend JSStringForSend JSValueForSend
- | SetPropertyAtIndex JSObjectForSend Int JSValueForSend
- | StringToValue JSStringForSend JSValueForSend
- | NumberToValue Double JSValueForSend
- | JSONValueToValue Value JSValueForSend
- | GetPropertyByName JSObjectForSend JSStringForSend JSValueForSend
- | GetPropertyAtIndex JSObjectForSend Int JSValueForSend
- | CallAsFunction JSObjectForSend JSObjectForSend [JSValueForSend] JSValueForSend
- | CallAsConstructor JSObjectForSend [JSValueForSend] JSValueForSend
- | NewEmptyObject JSValueForSend
- | NewAsyncCallback JSValueForSend
- | NewSyncCallback JSValueForSend
- | FreeCallback JSValueForSend
- | NewArray [JSValueForSend] JSValueForSend
- | EvaluateScript JSStringForSend JSValueForSend
- | SyncWithAnimationFrame JSValueForSend
- | StartSyncBlock
- | EndSyncBlock
- newtype SomeJSArray (m :: MutabilityType s) = SomeJSArray JSVal
- data Command
- = DeRefVal JSValueForSend
- | ValueToBool JSValueForSend
- | ValueToNumber JSValueForSend
- | ValueToString JSValueForSend
- | ValueToJSON JSValueForSend
- | ValueToJSONValue JSValueForSend
- | IsNull JSValueForSend
- | IsUndefined JSValueForSend
- | StrictEqual JSValueForSend JSValueForSend
- | InstanceOf JSValueForSend JSObjectForSend
- | PropertyNames JSObjectForSend
- | Sync
- data BatchResults
- data Results
- newtype JSValueReceived = JSValueReceived JSValueRef
- data Batch = Batch [Either AsyncCommand Command] Bool Int
- data JSNull = JSNull
- type JSUndefined = ()
- data JSValue
- newtype GHCJSPure a = GHCJSPure (JSM a)
- class IsJSVal a where
- type JSArray = SomeJSArray Immutable
- type MutableJSArray = SomeJSArray Mutable
- type STJSArray s = SomeJSArray (STMutable s)
- newtype Nullable a = Nullable a
- type JSadddleHasCallStack = () :: Constraint
- data MutabilityType s
- = Mutable_ s
- | Immutable_ s
- | STMutable s
- type Immutable = Immutable_ ()
- data IsItMutable
- type family Mutability (a :: MutabilityType s) :: IsItMutable where ...
- newtype JSStringReceived = JSStringReceived Text
- newtype JSStringForSend = JSStringForSend Text
- newtype JSObjectForSend = JSObjectForSend JSValueForSend
- array :: MakeArgs args => args -> JSM Object
- function :: JSCallAsFunction -> JSM Function
- (<##) :: (MakeObject this, ToJSVal val) => this -> Int -> val -> JSM ()
- (<#) :: (MakeObject this, ToJSString name, ToJSVal val) => this -> name -> val -> JSM ()
- (!) :: (MakeObject this, ToJSString name) => this -> name -> JSM JSVal
- bracket :: (HasCallStack, MonadMask m) => m a -> (a -> m c) -> (a -> m b) -> m b
- new :: (MakeObject constructor, MakeArgs args) => constructor -> args -> JSM JSVal
- catch :: (MonadCatch m, HasCallStack, Exception e) => m a -> (e -> m a) -> m a
- (!!) :: MakeObject this => this -> Int -> JSM JSVal
- create :: JSM Object
- (#) :: (MakeObject this, ToJSString name, MakeArgs args) => this -> name -> args -> JSM JSVal
- eval :: ToJSString script => script -> JSM JSVal
- liftJSM :: MonadJSM m => JSM a -> m a
- askJSM :: MonadJSM m => m JSContextRef
- runJSM :: MonadIO m => JSM a -> JSContextRef -> m a
- runJSaddle :: MonadIO m => JSContextRef -> JSM a -> m a
- syncPoint :: JSM ()
- syncAfter :: JSM a -> JSM a
- waitForAnimationFrame :: JSM Double
- nextAnimationFrame :: (Double -> JSM a) -> JSM a
- js :: (MakeObject s, ToJSString name) => name -> IndexPreservingGetter s (JSM JSVal)
- jss :: (ToJSString name, ToJSVal val) => name -> val -> forall o. MakeObject o => IndexPreservingGetter o (JSM ())
- jsf :: (ToJSString name, MakeArgs args) => name -> args -> JSF
- js0 :: ToJSString name => name -> JSF
- js1 :: (ToJSString name, ToJSVal a0) => name -> a0 -> JSF
- js2 :: (ToJSString name, ToJSVal a0, ToJSVal a1) => name -> a0 -> a1 -> JSF
- js3 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2) => name -> a0 -> a1 -> a2 -> JSF
- js4 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3) => name -> a0 -> a1 -> a2 -> a3 -> JSF
- js5 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3, ToJSVal a4) => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF
- jsg :: ToJSString a => a -> JSM JSVal
- jsgf :: (ToJSString name, MakeArgs args) => name -> args -> JSM JSVal
- jsg0 :: ToJSString name => name -> JSM JSVal
- jsg1 :: (ToJSString name, ToJSVal a0) => name -> a0 -> JSM JSVal
- jsg2 :: (ToJSString name, ToJSVal a0, ToJSVal a1) => name -> a0 -> a1 -> JSM JSVal
- jsg3 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2) => name -> a0 -> a1 -> a2 -> JSM JSVal
- jsg4 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3) => name -> a0 -> a1 -> a2 -> a3 -> JSM JSVal
- jsg5 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3, ToJSVal a4) => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSM JSVal
- (##) :: (MakeObject this, MakeArgs args) => this -> Int -> args -> JSM JSVal
- call :: (MakeObject f, MakeObject this, MakeArgs args) => f -> this -> args -> JSM JSVal
- obj :: JSM Object
- getProp :: JSString -> Object -> JSM JSVal
- unsafeGetProp :: JSString -> Object -> JSM JSVal
- setProp :: JSString -> JSVal -> Object -> JSM ()
- unsafeSetProp :: JSString -> JSVal -> Object -> JSM ()
- asyncFunction :: JSCallAsFunction -> JSM Function
- freeFunction :: Function -> JSM ()
- fun :: JSCallAsFunction -> JSCallAsFunction
- fromListIO :: [JSVal] -> JSM (SomeJSArray m)
- global :: Object
- listProps :: Object -> JSM [JSString]
- propertyNames :: MakeObject this => this -> JSM [JSString]
- properties :: MakeObject this => this -> JSM [JSVal]
- objCallAsFunction :: MakeArgs args => Object -> Object -> args -> JSM JSVal
- objCallAsConstructor :: MakeArgs args => Object -> args -> JSM JSVal
- nullObject :: Object
- valToObject :: ToJSVal value => value -> JSM Object
- objGetPropertyByName :: ToJSString name => Object -> name -> JSM JSVal
- objGetPropertyAtIndex :: Object -> Int -> JSM JSVal
- objSetPropertyByName :: (ToJSString name, ToJSVal val) => Object -> name -> val -> JSM ()
- objSetPropertyAtIndex :: ToJSVal val => Object -> Int -> val -> JSM ()
- sendCommand :: Command -> JSM Result
- textFromJSString :: JSString -> Text
- textToJSString :: Text -> JSString
- strToText :: JSString -> Text
- textToStr :: Text -> JSString
- showJSValue :: JSValue -> String
- isTruthy :: JSVal -> GHCJSPure Bool
- valToBool :: ToJSVal value => value -> JSM Bool
- valToNumber :: ToJSVal value => value -> JSM Double
- valToStr :: ToJSVal value => value -> JSM JSString
- valToText :: ToJSVal value => value -> JSM Text
- valToJSON :: ToJSVal value => value -> JSM JSString
- val :: ToJSVal value => value -> JSM JSVal
- jsNull :: JSVal
- valNull :: JSVal
- isNull :: JSVal -> GHCJSPure Bool
- valIsNull :: ToJSVal value => value -> JSM Bool
- jsUndefined :: JSVal
- valUndefined :: JSVal
- isUndefined :: JSVal -> GHCJSPure Bool
- valIsUndefined :: ToJSVal value => value -> JSM Bool
- maybeNullOrUndefined :: ToJSVal value => value -> JSM (Maybe JSVal)
- maybeNullOrUndefined' :: ToJSVal value => (JSVal -> JSM a) -> value -> JSM (Maybe a)
- toJSBool :: Bool -> JSVal
- jsTrue :: JSVal
- jsFalse :: JSVal
- valBool :: Bool -> JSVal
- valMakeNumber :: Double -> JSM JSVal
- valMakeString :: JSString -> JSM JSVal
- valMakeText :: Text -> JSM JSVal
- valMakeJSON :: Value -> JSM JSVal
- deRefVal :: ToJSVal value => value -> JSM JSValue
- valMakeRef :: JSValue -> JSM JSVal
- strictEqual :: (ToJSVal a, ToJSVal b) => a -> b -> JSM Bool
- instanceOf :: (ToJSVal value, MakeObject constructor) => value -> constructor -> JSM Bool
- ghcjsPure :: GHCJSPure a -> JSM a
- ghcjsPureMap :: (a -> b) -> GHCJSPure a -> GHCJSPure b
- ghcjsPureId :: a -> GHCJSPure a
- jsval :: IsJSVal a => a -> GHCJSPure JSVal
- toJSVal_aeson :: ToJSON a => a -> JSM JSVal
- toJSVal_pure :: PToJSVal a => a -> JSM JSVal
JSaddle EDSL
The JSM
monad gives us the context for evaluation. In keeping
with JavaScript the EDSL has
- Weakish typing - type classes are used to convert to JSValueRef and Object types
- Strict evaluation - function in the
JSM
monad can be passed in place of a value and will evaluated and converted to JSValueRef or Object and then passed on to JavaScript
JSaddle should be used to write wrappers for JavaScript libraries that provide more type safety.
Code Examples
The code examples in this documentation are executed with a runjs
function that executes the example code in the JSM monad and converts
the result to Text
with valToText
. It also catches unhandled
exceptions with catch
. The source code can be found in tests/TestJSaddle.hs
Where it makes sense code examples are given in two forms. One
that uses eval
to run a purely JavaScript version and one that
uses more of the JSaddle EDSL feature being demonstrated.
Calling Haskell from JavaScript
You can call back into Haskell from JavaScript using fun
to
convert a Haskell function in the JSM monad into a javascript
value.
GHCJS Support
When built with ghcjs the code works using JavaScript FFI by default.
GHC Support
When built with ghc the code runs a small Warp server that provides index.html and jsaddle.js files. When a browser is connected the code in jsaddle.js will open a WebSockets connection to the server and the server will run the Haskell code. The JSaddle parts will be executed by sending commands back to the browser.
Modules
Result of a Command
returned from the JavaScript context
Instances
See Object
Instances
ToJSVal Object Source # | |
MakeObject Object Source # | If we already have a Object we are fine |
Defined in Language.Javascript.JSaddle.Classes.Internal |
class MakeArgs this where Source #
Anything that can be used to make a list of JavaScript value references for use as function arguments
Instances
MakeArgs Value Source # | Makes an argument list with just a single JSON value |
MakeArgs JSVal Source # | A single JSVal can be used as the argument list |
MakeArgs JSCallAsFunction Source # | |
Defined in Language.Javascript.JSaddle.Object | |
MakeArgs JSNull Source # | Makes an argument list with just a single |
MakeArgs JSValue Source # | Makes an argument list with just a single JavaScript value from a |
MakeArgs Text Source # | Makes an argument list with just a single JavaScript string |
MakeArgs () Source # | This allows us to pass no arguments easily (altenative would be to use |
MakeArgs Bool Source # | Makes an argument list with just a single JavaScript boolean value |
MakeArgs Double Source # | Makes an argument list with just a single JavaScript number |
MakeArgs arg => MakeArgs (JSM arg) Source # | |
ToJSVal arg => MakeArgs [arg] Source # | |
(ToJSVal arg1, ToJSVal arg2) => MakeArgs (arg1, arg2) Source # | |
(ToJSVal arg1, ToJSVal arg2, ToJSVal arg3) => MakeArgs (arg1, arg2, arg3) Source # | |
(ToJSVal arg1, ToJSVal arg2, ToJSVal arg3, ToJSVal arg4) => MakeArgs (arg1, arg2, arg3, arg4) Source # | |
(ToJSVal arg1, ToJSVal arg2, ToJSVal arg3, ToJSVal arg4, ToJSVal arg5) => MakeArgs (arg1, arg2, arg3, arg4, arg5) Source # | |
(ToJSVal arg1, ToJSVal arg2, ToJSVal arg3, ToJSVal arg4, ToJSVal arg5, ToJSVal arg6) => MakeArgs (arg1, arg2, arg3, arg4, arg5, arg6) Source # | |
class ToJSVal a where Source #
Nothing
Instances
class PFromJSVal a where Source #
pFromJSVal :: JSVal -> a Source #
Instances
PFromJSVal JSVal Source # | |
Defined in GHCJS.Marshal.Pure pFromJSVal :: JSVal -> JSVal Source # | |
PFromJSVal MutableArrayBuffer Source # | |
Defined in JavaScript.TypedArray.ArrayBuffer.Internal pFromJSVal :: JSVal -> MutableArrayBuffer Source # | |
PFromJSVal () Source # | |
Defined in GHCJS.Marshal.Pure pFromJSVal :: JSVal -> () Source # |
class FromJSVal a where Source #
Nothing
fromJSVal :: JSVal -> JSM (Maybe a) Source #
fromJSValUnchecked :: JSVal -> JSM a Source #
fromJSValListOf :: JSVal -> JSM (Maybe [a]) Source #
fromJSValUncheckedListOf :: JSVal -> JSM [a] Source #
Instances
class ToJSVal a => ToJSString a where Source #
Anything that can be used to make a JavaScript string
toJSString :: a -> JSString Source #
Instances
ToJSString JSString Source # | If we already have a JSString we are fine |
Defined in Language.Javascript.JSaddle.Value toJSString :: JSString -> JSString Source # | |
ToJSString Text Source # | |
Defined in Language.Javascript.JSaddle.Value toJSString :: Text -> JSString Source # | |
ToJSString String Source # | |
Defined in Language.Javascript.JSaddle.Value toJSString :: String -> JSString Source # |
class FromJSVal a => FromJSString a where Source #
Anything that can be constructed from a JavaScript string
fromJSString :: JSString -> a Source #
Instances
FromJSString JSString Source # | |
Defined in Language.Javascript.JSaddle.Value fromJSString :: JSString -> JSString Source # | |
FromJSString Text Source # | |
Defined in Language.Javascript.JSaddle.Value fromJSString :: JSString -> Text Source # | |
FromJSString String Source # | |
Defined in Language.Javascript.JSaddle.Value fromJSString :: JSString -> String Source # |
class MakeObject this where Source #
Anything that can be used to make a JavaScript object reference
makeObject :: this -> JSM Object Source #
Instances
MakeObject JSVal Source # | |
Defined in Language.Javascript.JSaddle.Value | |
MakeObject Object Source # | If we already have a Object we are fine |
Defined in Language.Javascript.JSaddle.Classes.Internal | |
MakeObject v => MakeObject (JSM v) Source # | Object can be made by evaluating a function in |
Defined in Language.Javascript.JSaddle.Object |
The JSM
monad keeps track of the JavaScript execution context.
When using GHCJS it is IO
.
Given a JSM
function and a JSContextRef
you can run the
function like this...
runJSM jsmFunction javaScriptContext
JSM | |
|
Instances
Instances
NFData JSVal Source # | |
Defined in GHCJS.Prim.Internal | |
FromJSVal JSVal Source # | |
PFromJSVal JSVal Source # | |
Defined in GHCJS.Marshal.Pure pFromJSVal :: JSVal -> JSVal Source # | |
PToJSVal JSVal Source # | |
ToJSVal JSVal Source # | If we already have a JSVal we are fine |
ToJSVal JSCallAsFunction Source # | A callback to Haskell can be used as a JavaScript value. This will create
an anonymous JavaScript function object. Use |
Defined in Language.Javascript.JSaddle.Object toJSVal :: JSCallAsFunction -> JSM JSVal Source # toJSValListOf :: [JSCallAsFunction] -> JSM JSVal Source # | |
MakeArgs JSVal Source # | A single JSVal can be used as the argument list |
MakeArgs JSCallAsFunction Source # | |
Defined in Language.Javascript.JSaddle.Object | |
MakeObject JSVal Source # | |
Defined in Language.Javascript.JSaddle.Value |
newtype JSException Source #
Instances
Exception JSException Source # | |
Defined in Language.Javascript.JSaddle.Exception | |
Show JSException Source # | JavaScript exception >>> |
Defined in Language.Javascript.JSaddle.Exception showsPrec :: Int -> JSException -> ShowS # show :: JSException -> String # showList :: [JSException] -> ShowS # |
A wrapper around a JavaScript string
Instances
data JSContextRef Source #
Identifies a JavaScript execution context.
When using GHCJS this is just ()
since their is only one context.
When using GHC it includes the functions JSaddle needs to communicate
with the JavaScript context.
JSContextRef | |
|
class (Applicative m, MonadIO m) => MonadJSM m where Source #
Nothing
Instances
MonadJSM JSM Source # | |
MonadJSM m => MonadJSM (MaybeT m) Source # | |
MonadJSM m => MonadJSM (ExceptT e m) Source # | |
MonadJSM m => MonadJSM (IdentityT m) Source # | |
MonadJSM m => MonadJSM (ReaderT r m) Source # | |
MonadJSM m => MonadJSM (StateT s m) Source # | |
MonadJSM m => MonadJSM (StateT s m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (WriterT w m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (WriterT w m) Source # | |
MonadJSM m => MonadJSM (ContT r m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (RWST r w s m) Source # | |
(Monoid w, MonadJSM m) => MonadJSM (RWST r w s m) Source # | |
type JSF = forall o. MakeObject o => IndexPreservingGetter o (JSM JSVal) Source #
Java script function applications have this type
type JSCallAsFunction Source #
= JSVal | Function object |
-> JSVal | this |
-> [JSVal] | Function arguments |
-> JSM () | Only () (aka |
Type used for Haskell functions called from JavaScript.
newtype JSValueForSend Source #
Wrapper used when sending a JSVal
to the JavaScript context
Instances
data AsyncCommand Source #
Command sent to a JavaScript context for execution asynchronously
Instances
newtype SomeJSArray (m :: MutabilityType s) Source #
Instances
IsJSVal (SomeJSArray m) Source # | |
Defined in Language.Javascript.JSaddle.Types |
Command sent to a JavaScript context for execution synchronously
Instances
data BatchResults Source #
Instances
BatchResults Int BatchResults | |
Duplicate Int Int | |
Callback Int BatchResults JSValueReceived JSValueReceived JSValueReceived [JSValueReceived] | |
ProtocolError Text |
Instances
newtype JSValueReceived Source #
Wrapper used when receiving a JSVal
from the JavaScript context
Instances
FromJSON JSValueReceived Source # | |
Defined in Language.Javascript.JSaddle.Types parseJSON :: Value -> Parser JSValueReceived # parseJSONList :: Value -> Parser [JSValueReceived] # | |
ToJSON JSValueReceived Source # | |
Defined in Language.Javascript.JSaddle.Types toJSON :: JSValueReceived -> Value # toEncoding :: JSValueReceived -> Encoding # toJSONList :: [JSValueReceived] -> Value # toEncodingList :: [JSValueReceived] -> Encoding # omitField :: JSValueReceived -> Bool # | |
Show JSValueReceived Source # | |
Defined in Language.Javascript.JSaddle.Types showsPrec :: Int -> JSValueReceived -> ShowS # show :: JSValueReceived -> String # showList :: [JSValueReceived] -> ShowS # |
Batch of commands that can be sent together to the JavaScript context
Instances
FromJSON Batch Source # | |
Defined in Language.Javascript.JSaddle.Types | |
ToJSON Batch Source # | |
Generic Batch Source # | |
Show Batch Source # | |
NFData Batch Source # | |
Defined in Language.Javascript.JSaddle.Types | |
type Rep Batch Source # | |
Defined in Language.Javascript.JSaddle.Types type Rep Batch = D1 ('MetaData "Batch" "Language.Javascript.JSaddle.Types" "jsaddle-0.9.9.2-3xffynxuvva9oTUm9CO3iV" 'False) (C1 ('MetaCons "Batch" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [Either AsyncCommand Command]) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)))) |
JSNull | Type that represents a value that can only be null. Haskell of course has no null so we are adding this type. |
type JSUndefined Source #
= () | A type that can only be undefined in JavaScript. Using ()
because functions in JavaScript that have no return, impicitly
return undefined.
type JSBool = Bool -- ^ JavaScript boolean values map the |
An algebraic data type that can represent a JavaScript value. Any JavaScriptCore
JSVal
can be converted into this type.
ValNull | null |
ValUndefined | undefined |
ValBool Bool | true or false |
ValNumber Double | a number |
ValString Text | a string |
ValObject Object | an object |
Type we can give to functions that are pure when using ghcjs, but live in JSM when using jsaddle.
Some functions that can be pure in GHCJS cannot be implemented in
a pure way in JSaddle (because we need to know the JSContextRef).
Instead we implement versions of these functions in that return
`GHCJSPure a` instead of a
. To call them in a way that will
work when compiling with GHCJS use ghcjsPure
.
class IsJSVal a where Source #
Nothing
Instances
IsJSVal JSString Source # | |
IsJSVal (SomeArrayBuffer m) Source # | |
Defined in JavaScript.TypedArray.ArrayBuffer.Internal | |
IsJSVal (SomeJSArray m) Source # | |
Defined in Language.Javascript.JSaddle.Types | |
IsJSVal (SomeTypedArray e m) Source # | |
Defined in JavaScript.TypedArray.Internal.Types |
type MutableJSArray = SomeJSArray Mutable Source #
See MutableJSArray
type JSadddleHasCallStack = () :: Constraint Source #
Like HasCallStack, but only when jsaddle cabal flag check-unchecked is set
data MutabilityType s Source #
Mutable_ s | |
Immutable_ s | |
STMutable s |
type Immutable = Immutable_ () Source #
type family Mutability (a :: MutabilityType s) :: IsItMutable where ... Source #
newtype JSStringReceived Source #
Wrapper used when receiving a JSString
from the JavaScript context
Instances
FromJSON JSStringReceived Source # | |
Defined in Language.Javascript.JSaddle.Types parseJSON :: Value -> Parser JSStringReceived # parseJSONList :: Value -> Parser [JSStringReceived] # | |
ToJSON JSStringReceived Source # | |
Defined in Language.Javascript.JSaddle.Types toJSON :: JSStringReceived -> Value # toEncoding :: JSStringReceived -> Encoding # toJSONList :: [JSStringReceived] -> Value # toEncodingList :: [JSStringReceived] -> Encoding # omitField :: JSStringReceived -> Bool # | |
Show JSStringReceived Source # | |
Defined in Language.Javascript.JSaddle.Types showsPrec :: Int -> JSStringReceived -> ShowS # show :: JSStringReceived -> String # showList :: [JSStringReceived] -> ShowS # |
newtype JSStringForSend Source #
Wrapper used when sending a JString
to the JavaScript context
Instances
newtype JSObjectForSend Source #
Wrapper used when sending a Object
to the JavaScript context
Instances
array :: MakeArgs args => args -> JSM Object Source #
Make an JavaScript array from a list of values
>>>
testJSaddle $ eval "['Hello', 'World'][1]"
World>>>
testJSaddle $ array ["Hello", "World"] !! 1
World>>>
testJSaddle $ eval "['Hello', null, undefined, true, 1]"
Hello,,,true,1>>>
testJSaddle $ array ("Hello", JSNull, (), True, 1.0::Double)
Hello,,,true,1
:: JSCallAsFunction | Haskell function to call |
-> JSM Function | Returns a JavaScript function object that will call the Haskell one when it is called |
Make a JavaScript function object that wraps a Haskell function.
Calls made to the function will be synchronous where possible
(on GHCJS it uses on syncCallback2
with ContinueAsync
).
:: (MakeObject this, ToJSVal val) | |
=> this | Object to set the property on |
-> Int | Index of the property to set |
-> val | Value to set it to |
-> JSM () |
Set a JavaScript property
>>>
testJSaddle $ eval "var j = {}; j[6] = 1; j[6]"
1>>>
testJSaddle $ do {j <- obj; (j <## 6) 1; j!!6}
1
:: (MakeObject this, ToJSString name, ToJSVal val) | |
=> this | Object to set the property on |
-> name | Name of the property to set |
-> val | Value to set it to |
-> JSM () |
Set a JavaScript property
>>>
testJSaddle $ eval "var j = {}; j.x = 1; j.x"
1>>>
testJSaddle $ do {j <- obj; (j <# "x") 1; j!"x"}
1
:: (MakeObject this, ToJSString name) | |
=> this | Object to look on |
-> name | Name of the property to find |
-> JSM JSVal | Property reference |
Lookup a property based on its name.
>>>
testJSaddle $ eval "'Hello World'.length"
11>>>
testJSaddle $ val "Hello World" ! "length"
11
bracket :: (HasCallStack, MonadMask m) => m a -> (a -> m c) -> (a -> m b) -> m b #
Generalized abstracted pattern of safe resource acquisition and release
in the face of errors. The first action "acquires" some value, which
is "released" by the second action at the end. The third action "uses"
the value and its result is the result of the bracket
.
If an error is thrown during the use, the release still happens before the error is rethrown.
Note that this is essentially a type-specialized version of
generalBracket
. This function has a more common signature (matching the
signature from Control.Exception), and is often more convenient to use. By
contrast, generalBracket
is more expressive, allowing us to implement
other functions like bracketOnError
.
new :: (MakeObject constructor, MakeArgs args) => constructor -> args -> JSM JSVal Source #
Use this to create a new JavaScript object
If you pass more than 7 arguments to a constructor for a built in JavaScript type (like Date) then this function will fail.
>>>
testJSaddle $ new (jsg "Date") (2013, 1, 1)
Fri Feb 01 2013 00:00:00 GMT+... (...)
catch :: (MonadCatch m, HasCallStack, Exception e) => m a -> (e -> m a) -> m a #
Provide a handler for exceptions thrown during execution of the first
action. Note that type of the type of the argument to the handler will
constrain which exceptions are caught. See Control.Exception's
catch
.
:: MakeObject this | |
=> this | Object to look on |
-> Int | Index of the property to lookup |
-> JSM JSVal | Property reference |
Lookup a property based on its index.
>>>
testJSaddle $ eval "'Hello World'[6]"
W>>>
testJSaddle $ val "Hello World" !! 6
W
(#) :: (MakeObject this, ToJSString name, MakeArgs args) => this -> name -> args -> JSM JSVal infixr 2 Source #
Call a JavaScript function
>>>
testJSaddle $ eval "'Hello World'.indexOf('World')"
6>>>
testJSaddle $ val "Hello World" # "indexOf" $ ["World"]
6
:: ToJSString script | |
=> script | JavaScript to evaluate |
-> JSM JSVal |
Evaluates a script (like eval in java script)
>>>
testJSaddle $ eval "1+1"
2
askJSM :: MonadJSM m => m JSContextRef Source #
Gets the JavaScript context from the monad
runJSM :: MonadIO m => JSM a -> JSContextRef -> m a Source #
Runs a JSM
JavaScript function in a given JavaScript context.
runJSaddle :: MonadIO m => JSContextRef -> JSM a -> m a Source #
Alternative version of runJSM
waitForAnimationFrame :: JSM Double Source #
On GHCJS this is waitForAnimationFrame
.
On GHC it will delay the execution of the current batch of asynchronous
command when they are sent to JavaScript. It will not delay the Haskell
code execution. The time returned will be based on the Haskell clock
(not the JavaScript clock).
nextAnimationFrame :: (Double -> JSM a) -> JSM a Source #
Tries to executes the given code in the next animation frame callback. Avoid synchronous opperations where possible.
:: (MakeObject s, ToJSString name) | |
=> name | Name of the property to find |
-> IndexPreservingGetter s (JSM JSVal) |
Makes a getter for a particular property name.
js name = to (!name)
>>>
testJSaddle $ eval "'Hello World'.length"
11>>>
testJSaddle $ val "Hello World" ^. js "length"
11
:: (ToJSString name, ToJSVal val) | |
=> name | Name of the property to find |
-> val | |
-> forall o. MakeObject o | |
=> IndexPreservingGetter o (JSM ()) |
Makes a setter for a particular property name.
jss name = to (<#name)
>>>
testJSaddle $ eval "'Hello World'.length = 12"
12>>>
testJSaddle $ val "Hello World" ^. jss "length" 12
undefined
jsf :: (ToJSString name, MakeArgs args) => name -> args -> JSF Source #
Handy way to call a function
jsf name = to (\o -> o # name $ args)
>>>
testJSaddle $ val "Hello World" ^. jsf "indexOf" ["World"]
6
js0 :: ToJSString name => name -> JSF Source #
Handy way to call a function that expects no arguments
js0 name = jsf name ()
>>>
testJSaddle $ val "Hello World" ^. js0 "toLowerCase"
hello world
js1 :: (ToJSString name, ToJSVal a0) => name -> a0 -> JSF Source #
Handy way to call a function that expects one argument
js1 name a0 = jsf name [a0]
>>>
testJSaddle $ val "Hello World" ^. js1 "indexOf" "World"
6
js2 :: (ToJSString name, ToJSVal a0, ToJSVal a1) => name -> a0 -> a1 -> JSF Source #
Handy way to call a function that expects two arguments
js3 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2) => name -> a0 -> a1 -> a2 -> JSF Source #
Handy way to call a function that expects three arguments
js4 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3) => name -> a0 -> a1 -> a2 -> a3 -> JSF Source #
Handy way to call a function that expects four arguments
js5 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3, ToJSVal a4) => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF Source #
Handy way to call a function that expects five arguments
jsg :: ToJSString a => a -> JSM JSVal Source #
Handy way to get and hold onto a reference top level javascript
>>>
testJSaddle $ eval "w = console; w.log('Hello World')"
undefined>>>
testJSaddle $ do w <- jsg "console"; w ^. js1 "log" "Hello World"
undefined
jsgf :: (ToJSString name, MakeArgs args) => name -> args -> JSM JSVal Source #
Handy way to call a function
jsgf name = jsg name . to (# args)
>>>
testJSaddle $ eval "globalFunc = function (x) {return x.length;}"
function (x) {return x.length;}>>>
testJSaddle $ jsgf "globalFunc" ["World"]
5
jsg0 :: ToJSString name => name -> JSM JSVal Source #
Handy way to call a function that expects no arguments
jsg0 name = jsgf name ()
>>>
testJSaddle $ jsg0 "globalFunc" >>= valToText
A JavaScript exception was thrown! (may not reach Haskell code) TypeError:...undefine...
jsg1 :: (ToJSString name, ToJSVal a0) => name -> a0 -> JSM JSVal Source #
Handy way to call a function that expects one argument
jsg1 name a0 = jsgf name [a0]
>>>
testJSaddle $ jsg1 "globalFunc" "World"
5
jsg2 :: (ToJSString name, ToJSVal a0, ToJSVal a1) => name -> a0 -> a1 -> JSM JSVal Source #
Handy way to call a function that expects two arguments
jsg3 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2) => name -> a0 -> a1 -> a2 -> JSM JSVal Source #
Handy way to call a function that expects three arguments
jsg4 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3) => name -> a0 -> a1 -> a2 -> a3 -> JSM JSVal Source #
Handy way to call a function that expects four arguments
jsg5 :: (ToJSString name, ToJSVal a0, ToJSVal a1, ToJSVal a2, ToJSVal a3, ToJSVal a4) => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSM JSVal Source #
Handy way to call a function that expects five arguments
(##) :: (MakeObject this, MakeArgs args) => this -> Int -> args -> JSM JSVal infixr 2 Source #
Call a JavaScript function
>>>
testJSaddle $ eval "something = {}; something[6]=function (x) {return x.length;}; something[6]('World')"
5>>>
testJSaddle $ jsg "something" ## 6 $ ["World"]
5
call :: (MakeObject f, MakeObject this, MakeArgs args) => f -> this -> args -> JSM JSVal Source #
Call function with a given this
. In most cases you should use #
.
>>>
testJSaddle $ eval "(function(){return this;}).apply('Hello', [])"
Hello>>>
testJSaddle $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
Hello
Make an empty object using the default constuctor
>>>
testJSaddle $ eval "var a = {}; a.x = 'Hello'; a.x"
Hello>>>
testJSaddle $ do { a <- obj; (a <# "x") "Hello"; a ^. js "x" }
Hello
getProp :: JSString -> Object -> JSM JSVal Source #
get a property from an object. If accessing the property results in an exception, the exception is converted to a JSException. Since exception handling code prevents some optimizations in some JS engines, you may want to use unsafeGetProp instead
:: JSCallAsFunction | Haskell function to call |
-> JSM Function | Returns a JavaScript function object that will call the Haskell one when it is called |
Make a JavaScript function object that wraps a Haskell function. Calls made to the function will be asynchronous.
freeFunction :: Function -> JSM () Source #
fun :: JSCallAsFunction -> JSCallAsFunction Source #
Short hand ::JSCallAsFunction
so a haskell function can be passed to
a to a JavaScipt one.
>>>
testJSaddle $ eval "(function(f) {f('Hello');})(function (a) {console.log(a)})"
undefined>>>
:{
testJSaddle $ do result <- liftIO newEmptyMVar deRefVal $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ [arg1] -> do valToText arg1 >>= (liftIO . putMVar result) ] liftIO $ takeMVar result :} Hello
fromListIO :: [JSVal] -> JSM (SomeJSArray m) Source #
propertyNames :: MakeObject this => this -> JSM [JSString] Source #
properties :: MakeObject this => this -> JSM [JSVal] Source #
Get a list containing references to all the properties present on a given object
objCallAsFunction :: MakeArgs args => Object -> Object -> args -> JSM JSVal Source #
Call a JavaScript object as function. Consider using #
.
objCallAsConstructor :: MakeArgs args => Object -> args -> JSM JSVal Source #
Call a JavaScript object as a constructor. Consider using new
.
If you pass more than 7 arguments to a constructor for a built in JavaScript type (like Date) then this function will fail.
nullObject :: Object Source #
valToObject :: ToJSVal value => value -> JSM Object Source #
Given a JavaScript value get its object value. May throw JSException.
>>>
testJSaddle $ (valToObject JSNull >>= valToText) `catch` \ (JSException e) -> valToText e
null>>>
testJSaddle $ (valToObject () >>= valToText) `catch` \ (JSException e) -> valToText e
undefined>>>
testJSaddle $ valToObject True
true>>>
testJSaddle $ valToObject False
false>>>
testJSaddle $ valToObject (1.0 :: Double)
1>>>
testJSaddle $ valToObject (0.0 :: Double)
0>>>
testJSaddle $ valToObject ""
>>>
testJSaddle $ valToObject "1"
1
:: ToJSString name | |
=> Object | object to find the property on. |
-> name | name of the property. |
-> JSM JSVal | returns the property value. |
Get a property value given the object and the name of the property.
objGetPropertyAtIndex Source #
:: Object | object to find the property on. |
-> Int | index of the property. |
-> JSM JSVal | returns the property value. |
Get a property value given the object and the index of the property.
:: (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 () |
Set a property value given the object and the name of the property.
objSetPropertyAtIndex Source #
:: ToJSVal val | |
=> Object | object to find property on. |
-> Int | index of the property. |
-> val | new value to set the property to. |
-> JSM () |
Set a property value given the object and the index of the property.
textFromJSString :: JSString -> Text Source #
textToJSString :: Text -> JSString Source #
showJSValue :: JSValue -> String Source #
Show a JSValue but just say "object" if the value is a JavaScript object.
valToBool :: ToJSVal value => value -> JSM Bool Source #
Given a JavaScript value get its boolean value. All values in JavaScript convert to bool.
>>>
testJSaddle $ valToBool JSNull
false>>>
testJSaddle $ valToBool ()
false>>>
testJSaddle $ valToBool True
true>>>
testJSaddle $ valToBool False
false>>>
testJSaddle $ valToBool (1.0 :: Double)
true>>>
testJSaddle $ valToBool (0.0 :: Double)
false>>>
testJSaddle $ valToBool ""
false>>>
testJSaddle $ valToBool "1"
true
valToNumber :: ToJSVal value => value -> JSM Double Source #
Given a JavaScript value get its numeric value. May throw JSException.
>>>
testJSaddle $ show <$> valToNumber JSNull
0.0>>>
testJSaddle $ show <$> valToNumber ()
NaN>>>
testJSaddle $ show <$> valToNumber True
1.0>>>
testJSaddle $ show <$> valToNumber False
0.0>>>
testJSaddle $ show <$> valToNumber (1.0 :: Double)
1.0>>>
testJSaddle $ show <$> valToNumber (0.0 :: Double)
0.0>>>
testJSaddle $ show <$> valToNumber ""
0.0>>>
testJSaddle $ show <$> valToNumber "1"
1.0
valToStr :: ToJSVal value => value -> JSM JSString Source #
Given a JavaScript value get its string value (as a JavaScript string). May throw JSException.
>>>
testJSaddle $ strToText <$> valToStr JSNull
null>>>
testJSaddle $ strToText <$> valToStr ()
undefined>>>
testJSaddle $ strToText <$> valToStr True
true>>>
testJSaddle $ strToText <$> valToStr False
false>>>
testJSaddle $ strToText <$> valToStr (1.0 :: Double)
1>>>
testJSaddle $ strToText <$> valToStr (0.0 :: Double)
0>>>
testJSaddle $ strToText <$> valToStr ""
>>>
testJSaddle $ strToText <$> valToStr "1"
1
valToText :: ToJSVal value => value -> JSM Text Source #
Given a JavaScript value get its string value (as a Haskell Text
).
May throw JSException.
>>>
testJSaddle $ show <$> valToText JSNull
"null">>>
testJSaddle $ show <$> valToText ()
"undefined">>>
testJSaddle $ show <$> valToText True
"true">>>
testJSaddle $ show <$> valToText False
"false">>>
testJSaddle $ show <$> valToText (1.0 :: Double)
"1">>>
testJSaddle $ show <$> valToText (0.0 :: Double)
"0">>>
testJSaddle $ show <$> valToText ""
"">>>
testJSaddle $ show <$> valToText "1"
"1"
valToJSON :: ToJSVal value => value -> JSM JSString Source #
Given a JavaScript value get a JSON string value. May throw JSException.
>>>
testJSaddle $ strToText <$> valToJSON JSNull
null>>>
testJSaddle $ strToText <$> valToJSON ()
>>>
testJSaddle $ strToText <$> valToJSON True
true>>>
testJSaddle $ strToText <$> valToJSON False
false>>>
testJSaddle $ strToText <$> valToJSON (1.0 :: Double)
1>>>
testJSaddle $ strToText <$> valToJSON (0.0 :: Double)
0>>>
testJSaddle $ strToText <$> valToJSON ""
"">>>
testJSaddle $ strToText <$> valToJSON "1"
"1">>>
testJSaddle $ strToText <$> (obj >>= valToJSON)
{}
Convert to a JavaScript value (just an alias for toJSVal
)
valIsNull :: ToJSVal value => value -> JSM Bool Source #
Test a JavaScript value to see if it is null
jsUndefined :: JSVal Source #
valUndefined :: JSVal Source #
An undefined
JavaScript value
valIsUndefined :: ToJSVal value => value -> JSM Bool Source #
Test a JavaScript value to see if it is undefined
maybeNullOrUndefined :: ToJSVal value => value -> JSM (Maybe JSVal) Source #
Convert a JSVal to a Maybe JSVal (converting null and undefined to Nothing)
deRefVal :: ToJSVal value => value -> JSM JSValue Source #
Derefernce a value reference.
>>>
testJSaddle $ showJSValue <$> deRefVal JSNull
null>>>
testJSaddle $ showJSValue <$> deRefVal ()
undefined>>>
testJSaddle $ showJSValue <$> deRefVal True
true>>>
testJSaddle $ showJSValue <$> deRefVal False
false>>>
testJSaddle $ showJSValue <$> deRefVal (1.0 :: Double)
1.0>>>
testJSaddle $ showJSValue <$> deRefVal (0.0 :: Double)
0.0>>>
testJSaddle $ showJSValue <$> deRefVal ""
"">>>
testJSaddle $ showJSValue <$> deRefVal "1"
"1">>>
testJSaddle $ showJSValue <$> (valToObject True >>= deRefVal)
true>>>
testJSaddle $ showJSValue <$> (obj >>= deRefVal)
object
valMakeRef :: JSValue -> JSM JSVal Source #
Make a JavaScript value out of a JSValue
ADT.
>>>
testJSaddle $ valMakeRef ValNull
null>>>
testJSaddle $ valMakeRef ValUndefined
undefined>>>
testJSaddle $ valMakeRef (ValBool True)
true>>>
testJSaddle $ valMakeRef (ValNumber 1)
1>>>
testJSaddle $ valMakeRef (ValString $ T.pack "Hello")
Hello
instanceOf :: (ToJSVal value, MakeObject constructor) => value -> constructor -> JSM Bool Source #
ghcjsPure :: GHCJSPure a -> JSM a Source #
Used when you want to call a functions that is pure in GHCJS, but lives in the JSM in jsaddle.
ghcjsPureMap :: (a -> b) -> GHCJSPure a -> GHCJSPure b Source #
ghcjsPureId :: a -> GHCJSPure a Source #