| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Language.Javascript.JSaddle.Object
Contents
Description
Interface to JavaScript object
- data Object
- class MakeObject this where
- makeObject :: this -> JSM Object
- (!) :: (MakeObject this, ToJSString name) => this -> name -> JSM JSVal
- (!!) :: MakeObject this => this -> Index -> 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 -> Index -> val -> JSM ()
- (#) :: (MakeObject this, ToJSString name, MakeArgs args) => this -> name -> args -> JSM JSVal
- (##) :: (MakeObject this, MakeArgs args) => this -> Index -> 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
- data Function = Function {
- functionCallback :: HaskellCallback
- functionObject :: Object
- function :: ToJSString name => name -> JSCallAsFunction -> JSM Function
- freeFunction :: MonadIO m => Function -> m ()
- fun :: JSCallAsFunction -> JSCallAsFunction
- type JSCallAsFunction = JSVal -> JSVal -> [JSVal] -> JSM JSUndefined
- array :: MakeArgs args => args -> JSM Object
- global :: JSM Object
- propertyNames :: MakeObject this => this -> JSM [JSString]
- properties :: MakeObject this => this -> JSM [JSVal]
- objCallAsFunction :: MakeArgs args => Object -> Object -> args -> MutableJSArray -> JSM JSVal
- objCallAsConstructor :: MakeArgs args => Object -> args -> MutableJSArray -> JSM JSVal
- nullObject :: Object
Documentation
class MakeObject this where Source
Anything that can be used to make a JavaScript object reference
Methods
makeObject :: this -> JSM Object Source
Instances
| MakeObject Object Source | If we already have a Object we are fine |
Property lookup
Arguments
| :: (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
Arguments
| :: MakeObject this | |
| => this | Object to look on |
| -> Index | 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" !! 6W
Arguments
| :: (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
Arguments
| :: (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"11>>>testJSaddle $ val "Hello World" ^. js "length"11
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')"** Message: console message: @1: Hello World undefined>>>testJSaddle $ do w <- jsg "console"; w ^. js1 "log" "Hello World"** Message: console message:...@0: 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"TypeError:...undefined...is not an objec...
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
Arguments
| :: (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
Arguments
| :: (MakeObject this, ToJSVal val) | |
| => this | Object to set the property on |
| -> Index | 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 -> Index -> 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
Calling Haskell From JavaScript
Make a JavaScript function object that wraps a Haskell function.
Constructors
| Function | |
Fields
| |
Arguments
| :: ToJSString name | |
| => name | Name of the function |
| -> JSCallAsFunction | Haskell function to call |
| -> JSM Function | Returns a JavaScript function object that will call the Haskell one when it is called |
freeFunction :: MonadIO m => Function -> m () 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)})"** Message: console message: @1: Hello undefined>>>testJSaddle $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ args -> valToText (head args) >>= (liftIO . putStrLn . T.unpack) ]Hello undefined
type JSCallAsFunction Source
Arguments
| = JSVal | Function object |
| -> JSVal | this |
| -> [JSVal] | Function arguments |
| -> JSM JSUndefined | Only |
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
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"] !! 1World>>>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
Get a list containing the property names present on a given object
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 -> MutableJSArray -> JSM JSVal Source
Call a JavaScript object as function. Consider using '#'.
objCallAsConstructor :: MakeArgs args => Object -> args -> MutableJSArray -> 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.