jsaddle-0.9.4.0: Interface for JavaScript that works with GHCJS and GHC

Safe HaskellNone
LanguageHaskell2010

Language.Javascript.JSaddle.Object

Contents

Description

Interface to JavaScript object

Synopsis

Documentation

newtype Object Source #

See Object

Constructors

Object JSVal 

Instances

MakeObject Object Source #

If we already have a Object we are fine

class MakeObject this where Source #

Anything that can be used to make a JavaScript object reference

Minimal complete definition

makeObject

Methods

makeObject :: this -> JSM Object Source #

Instances

MakeObject Object Source #

If we already have a Object we are fine

Property lookup

(!) Source #

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

(!!) Source #

Arguments

:: 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

js Source #

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

jss Source #

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 = 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

(<#) infixr 1 Source #

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

(<##) infixr 1 Source #

Arguments

:: (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

obj :: JSM Object Source #

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

create :: JSM Object Source #

create an empty object

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

newtype Function Source #

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).

Constructors

Function 

function Source #

Arguments

:: 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.

asyncFunction Source #

Arguments

:: JSCallAsFunction

Haskell function to call

-> JSM Function

Returns a JavaScript function object that will call the Haskell one when it is called

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 #

Arguments

 = JSVal

Function object

-> JSVal

this

-> [JSVal]

Function arguments

-> JSM ()

Only () (aka JSUndefined) can be returned because the function may need to be executed in a different thread. If you need to get a value out pass in a continuation function as an argument and invoke it from haskell.

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"] !! 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

global :: Object Source #

JavaScript's global object

Enumerating Properties

propertyNames :: MakeObject this => this -> JSM [JSString] Source #

Get a list containing the property names present on a given object >>> testJSaddle $ show . map strToText $ propertyNames obj [] >>> testJSaddle $ show . map strToText $ propertyNames (eval "({x:1, y:2})") ["x","y"]

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.

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 function to create one with a name.

MakeObject v => MakeObject (JSM v) Source #

Object can be made by evaluating a fnction in JSM as long as it returns something we can make into a Object.

Methods

makeObject :: JSM v -> JSM Object Source #