Safe Haskell | None |
---|---|
Language | Haskell2010 |
Interface to JavaScript object
Synopsis
- newtype Object = Object JSVal
- class MakeObject this where
- (!) :: (MakeObject this, ToJSString name) => this -> name -> JSM JSVal
- (!!) :: MakeObject this => this -> Int -> JSM JSVal
- js :: (MakeObject s, ToJSString name) => name -> IndexPreservingGetter s (JSM JSVal)
- jss :: (ToJSString name, ToJSVal val) => name -> val -> forall o. MakeObject o => IndexPreservingGetter o (JSM ())
- type JSF = forall o. MakeObject o => IndexPreservingGetter o (JSM JSVal)
- 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, ToJSString name, ToJSVal val) => this -> name -> val -> JSM ()
- (<##) :: (MakeObject this, ToJSVal val) => this -> Int -> val -> JSM ()
- (#) :: (MakeObject this, ToJSString name, MakeArgs args) => this -> name -> args -> JSM JSVal
- (##) :: (MakeObject this, MakeArgs args) => this -> Int -> args -> JSM JSVal
- new :: (MakeObject constructor, MakeArgs args) => constructor -> args -> JSM JSVal
- call :: (MakeObject f, MakeObject this, MakeArgs args) => f -> this -> args -> JSM JSVal
- obj :: JSM Object
- create :: JSM Object
- getProp :: JSString -> Object -> JSM JSVal
- unsafeGetProp :: JSString -> Object -> JSM JSVal
- setProp :: JSString -> JSVal -> Object -> JSM ()
- unsafeSetProp :: JSString -> JSVal -> Object -> JSM ()
- newtype Function = Function {}
- function :: JSCallAsFunction -> JSM Function
- asyncFunction :: JSCallAsFunction -> JSM Function
- freeFunction :: Function -> JSM ()
- fun :: JSCallAsFunction -> JSCallAsFunction
- type JSCallAsFunction = JSVal -> JSVal -> [JSVal] -> JSM ()
- fromListIO :: [JSVal] -> JSM (SomeJSArray m)
- array :: MakeArgs args => args -> JSM Object
- 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
Documentation
See Object
Instances
MakeObject Object Source # | If we already have a Object we are fine |
Defined in Language.Javascript.JSaddle.Classes.Internal | |
ToJSVal Object 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 fnction in |
Defined in Language.Javascript.JSaddle.Object |
Property lookup
:: (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
:: 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 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 | |
-> 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
type JSF = forall o. MakeObject o => IndexPreservingGetter o (JSM JSVal) Source #
Java script function applications have this type
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
Setting the value of a property
:: (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, 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
Calling JavaSctipt
(#) :: (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
(##) :: (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
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+... (...)
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
Calling Haskell From JavaScript
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
).
:: 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.
:: JSCallAsFunction | Haskell function to call |
-> JSM Function | Returns a JavaScript function object that will call the Haskell one when it is called |
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
type JSCallAsFunction Source #
= JSVal | Function object |
-> JSVal | this |
-> [JSVal] | Function arguments |
-> JSM () | Only () (aka |
Type used for Haskell functions called from JavaScript.
Object Constructors
There is no good way to support calling haskell code as a JavaScript
constructor for the same reason that the return type of
JSCallAsFunction
is JSUndefined
.
Instead of writing a constructor in Haskell write a function that takes a continuation. Create the JavaScript object and pass it to the continuation.
Arrays
fromListIO :: [JSVal] -> JSM (SomeJSArray m) Source #
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
Global Object
Enumerating Properties
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
Low level
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 #
Orphan instances
MakeArgs JSCallAsFunction Source # | |
ToJSVal JSCallAsFunction Source # | A callback to Haskell can be used as a JavaScript value. This will create
an anonymous JavaScript function object. Use |
toJSVal :: JSCallAsFunction -> JSM JSVal Source # toJSValListOf :: [JSCallAsFunction] -> JSM JSVal Source # | |
MakeObject v => MakeObject (JSM v) Source # | Object can be made by evaluating a fnction in |