Safe Haskell | None |
---|---|
Language | Haskell2010 |
Interface to JavaScript object
- type JSObjectRef = Ptr OpaqueJSValue
- class MakeObjectRef this where
- makeObjectRef :: this -> JSM JSObjectRef
- (!) :: (MakeObjectRef this, MakeStringRef name) => this -> name -> JSM JSPropRef
- (!!) :: MakeObjectRef this => this -> Index -> JSM JSPropRef
- js :: (MakeObjectRef s, MakeStringRef name) => name -> IndexPreservingGetter s (JSM JSPropRef)
- type JSF = MakeObjectRef o => IndexPreservingGetter o (JSM JSValueRef)
- jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSF
- js0 :: MakeStringRef name => name -> JSF
- js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSF
- js2 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1) => name -> a0 -> a1 -> JSF
- js3 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2) => name -> a0 -> a1 -> a2 -> JSF
- js4 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2, MakeValueRef a3) => name -> a0 -> a1 -> a2 -> a3 -> JSF
- js5 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2, MakeValueRef a3, MakeValueRef a4) => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF
- jsg :: MakeStringRef a => a -> JSM JSPropRef
- (<#) :: (MakePropRef prop, MakeValueRef val) => prop -> val -> JSM JSPropRef
- (#) :: (MakePropRef prop, MakeArgRefs args) => prop -> args -> JSM JSValueRef
- new :: (MakeObjectRef constructor, MakeArgRefs args) => constructor -> args -> JSM JSValueRef
- call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args) => function -> this -> args -> JSM JSValueRef
- obj :: JSM JSObjectRef
- function :: MakeStringRef name => name -> JSCallAsFunction -> JSM JSObjectRef
- fun :: JSCallAsFunction -> JSCallAsFunction
- type JSCallAsFunction = JSValueRef -> JSValueRef -> [JSValueRef] -> JSM JSUndefined
- array :: MakeArgRefs args => args -> JSM JSObjectRef
- global :: JSM JSObjectRef
- propertyNames :: MakeObjectRef this => this -> JSM [JSStringRef]
- objCallAsFunction :: MakeArgRefs args => JSObjectRef -> JSObjectRef -> args -> JSValueRefRef -> JSM JSValueRef
- objCallAsConstructor :: MakeArgRefs args => JSObjectRef -> args -> JSValueRefRef -> JSM JSValueRef
Documentation
type JSObjectRef = Ptr OpaqueJSValue
class MakeObjectRef this where Source
Anything that can be used to make a JavaScript object reference
makeObjectRef :: this -> JSM JSObjectRef Source
MakeObjectRef JSObjectRef | If we already have a JSObjectRef we are fine |
MakeObjectRef JSPropRef | We can use a property as an object. |
MakeObjectRef JSNull | |
MakeObjectRef v => MakeObjectRef (JSM v) | JSObjectRef can be made by evaluating a function in |
Property lookup
:: (MakeObjectRef this, MakeStringRef name) | |
=> this | Object to look on |
-> name | Name of the property to find |
-> JSM JSPropRef | Property reference |
Lookup a property based on its name. This function just constructs a JSPropRef
the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
to JavaScript's .
operator.
>>>
testJSaddle $ eval "'Hello World'.length"
>>>
testJSaddle $ val "Hello World" ! "length"
11
:: MakeObjectRef this | |
=> this | Object to look on |
-> Index | Index of the property to lookup |
-> JSM JSPropRef | Property reference |
Lookup a property based on its index. This function just constructs a JSPropRef
the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
to JavaScript's []
operator.
>>>
testJSaddle $ eval "'Hello World'[6]"
>>>
testJSaddle $ val "Hello World" !! 6
W
:: (MakeObjectRef s, MakeStringRef name) | |
=> name | Name of the property to find |
-> IndexPreservingGetter s (JSM JSPropRef) |
Makes a getter for a particular property name.
js name = to (!name)
>>>
testJSaddle $ eval "'Hello World'.length"
>>>
testJSaddle $ val "Hello World" ^. js "length"
11
type JSF = MakeObjectRef o => IndexPreservingGetter o (JSM JSValueRef) Source
Java script function applications have this type
jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSF Source
Handy way to call a function
jsf name = js name . to (# args)
>>>
testJSaddle $ val "Hello World" ^. jsf "indexOf" ["World"]
6
js0 :: MakeStringRef 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 :: (MakeStringRef name, MakeValueRef 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 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1) => name -> a0 -> a1 -> JSF Source
Handy way to call a function that expects two arguments
js3 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2) => name -> a0 -> a1 -> a2 -> JSF Source
Handy way to call a function that expects three arguments
js4 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2, MakeValueRef a3) => name -> a0 -> a1 -> a2 -> a3 -> JSF Source
Handy way to call a function that expects four arguments
js5 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2, MakeValueRef a3, MakeValueRef a4) => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF Source
Handy way to call a function that expects five arguments
jsg :: MakeStringRef a => a -> JSM JSPropRef Source
Handy way to get and hold onto a reference top level javascript
>>>
testJSaddle $ eval "w = console; w.log('Hello World')"
>>>
testJSaddle $ do w <- jsg "console"; w ^. js "log" # ["Hello World"]
11
Setting the value of a property
:: (MakePropRef prop, MakeValueRef val) | |
=> prop | Property to set |
-> val | Value to set it to |
-> JSM JSPropRef | Reference to the property set |
Call a JavaScript function
>>>
testJSaddle $ eval "var j = {}; j.x = 1; j.x"
>>>
testJSaddle $ do {j <- eval "({})"; j!"x" <# 1; j!"x"}
1
Calling JavaSctipt
(#) :: (MakePropRef prop, MakeArgRefs args) => prop -> args -> JSM JSValueRef infixr 2 Source
Call a JavaScript function
>>>
testJSaddle $ eval "'Hello World'.indexOf('World')"
>>>
testJSaddle $ val "Hello World" ! "indexOf" # ["World"]
6
new :: (MakeObjectRef constructor, MakeArgRefs args) => constructor -> args -> JSM JSValueRef 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 "Date" (2013, 1, 1)
Fri Feb 01 2013 00:00:00 GMT+1300 (NZDT)
call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args) => function -> this -> args -> JSM JSValueRef Source
Call function with a given this
. In most cases you should use '#'.
>>>
testJSaddle $ eval "(function(){return this;}).apply('Hello', [])"
>>>
testJSaddle $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
Hello
obj :: JSM JSObjectRef Source
Make an empty object using the default constuctor
>>>
testJSaddle $ eval "var a = {}; a.x = 'Hello'; a.x"
>>>
testJSaddle $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
Hello
Calling Haskell From JavaScript
:: MakeStringRef name | |
=> name | Name of the function |
-> JSCallAsFunction | Haskell function to call |
-> JSM JSObjectRef | 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)})"
>>>
testJSaddle $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ args -> valToText (head args) >>= (liftIO . putStrLn . T.unpack) ]
Hello undefined
type JSCallAsFunction Source
= JSValueRef | Function object |
-> JSValueRef | this |
-> [JSValueRef] | 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 :: MakeArgRefs args => args -> JSM JSObjectRef Source
Make an JavaScript array from a list of values
>>>
testJSaddle $ eval "['Hello', 'World'][1]"
>>>
testJSaddle $ array ["Hello", "World"] !! 1
World>>>
testJSaddle $ eval "['Hello', null, undefined, true, 1]"
>>>
testJSaddle $ array ("Hello", JSNull, (), True, 1.0::Double)
Hello,,,true,1
Global Object
global :: JSM JSObjectRef Source
JavaScript's global object
Enumerating Properties
propertyNames :: MakeObjectRef this => this -> JSM [JSStringRef] Source
Get a list containing the property names present on a given object
Low level
objCallAsFunction :: MakeArgRefs args => JSObjectRef -> JSObjectRef -> args -> JSValueRefRef -> JSM JSValueRef Source
Call a JavaScript object as function. Consider using '#'.
objCallAsConstructor :: MakeArgRefs args => JSObjectRef -> args -> JSValueRefRef -> JSM JSValueRef 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.