Safe Haskell | None |
---|
Interface to JavaScript object
- type JSObjectRef = Ptr OpaqueJSValue
- class MakeObjectRef this where
- makeObjectRef :: this -> JSC JSObjectRef
- (!) :: (MakeObjectRef this, MakeStringRef name) => this -> name -> JSC JSPropRef
- (!!) :: MakeObjectRef this => this -> CUInt -> JSC JSPropRef
- js :: (MakeObjectRef s, MakeStringRef name) => name -> IndexPreservingGetter s (JSC JSPropRef)
- jsg :: MakeStringRef a => a -> JSC JSPropRef
- (<#) :: (MakePropRef prop, MakeValueRef val) => prop -> val -> JSC JSPropRef
- (#) :: (MakePropRef prop, MakeArgRefs args) => prop -> args -> JSC JSValueRef
- new :: (MakeObjectRef constructor, MakeArgRefs args) => constructor -> args -> JSC JSValueRef
- call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args) => function -> this -> args -> JSC JSValueRef
- function :: MakeStringRef name => name -> JSCallAsFunction -> JSC JSObjectRef
- fun :: JSCallAsFunction -> JSCallAsFunction
- type JSCallAsFunction = JSValueRef -> JSValueRef -> [JSValueRef] -> JSC JSUndefined
- array :: MakeArgRefs args => args -> JSC JSObjectRef
- global :: JSC JSObjectRef
- copyPropertyNames :: MakeObjectRef this => this -> JSC JSPropertyNameArrayRef
- propertyNames :: MakeObjectRef this => this -> JSC [JSStringRef]
- objCallAsFunction :: MakeArgRefs args => JSObjectRef -> JSObjectRef -> args -> JSValueRefRef -> JSC JSValueRef
- objCallAsConstructor :: MakeArgRefs args => JSObjectRef -> args -> JSValueRefRef -> JSC JSValueRef
Documentation
type JSObjectRef = Ptr OpaqueJSValue
class MakeObjectRef this where
Anything that can be used to make a JavaScript object reference
makeObjectRef :: this -> JSC JSObjectRef
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 (JSC 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 |
-> JSC 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.
>>>
testJSC $ eval "'Hello World'.length"
>>>
testJSC $ val "Hello World" ! "length"
11
:: MakeObjectRef this | |
=> this | Object to look on |
-> CUInt | Index of the property to lookup |
-> JSC 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.
>>>
testJSC $ eval "'Hello World'[6]"
>>>
testJSC $ val "Hello World" !! 6
W
:: (MakeObjectRef s, MakeStringRef name) | |
=> name | Name of the property to find |
-> IndexPreservingGetter s (JSC JSPropRef) |
Makes a getter for a particular property name.
js name = to (!name)
>>>
testJSC $ eval "'Hello World'.length"
>>>
testJSC $ val "Hello World" ^. js "length"
11
jsg :: MakeStringRef a => a -> JSC JSPropRef
Handy way to get and hold onto a reference top level javascript
>>>
testJSC $ eval "w = console; w.log('Hello World')"
>>>
testJSC $ 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 |
-> JSC JSPropRef | Reference to the property set |
Call a JavaScript function
>>>
testJSC $ eval "var j = {}; j.x = 1; j.x"
>>>
testJSC $ do {j <- eval "({})"; j!"x" <# 1; j!"x"}
1
Calling JavaSctipt
(#) :: (MakePropRef prop, MakeArgRefs args) => prop -> args -> JSC JSValueRef
Call a JavaScript function
>>>
testJSC $ eval "'Hello World'.indexOf('World')"
>>>
testJSC $ val "Hello World" ! "indexOf" # ["World"]
6
new :: (MakeObjectRef constructor, MakeArgRefs args) => constructor -> args -> JSC JSValueRef
Use this to create a new JavaScript object
>>>
testJSC $ 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 -> JSC JSValueRef
Call function with a given this
. In most cases you should use '#'.
>>>
testJSC $ eval "(function(){return this;}).apply('Hello', [])"
>>>
testJSC $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
Hello
Calling Haskell From JavaScript
:: MakeStringRef name | |
=> name | Name of the function |
-> JSCallAsFunction | Haskell function to call |
-> JSC JSObjectRef | Returns a JavaScript function object that will call the Haskell one when it is called |
fun :: JSCallAsFunction -> JSCallAsFunction
Short hand ::JSCallAsFunction
so a haskell function can be passed to
a to a JavaScipt one.
>>>
testJSC $ eval "(function(f) {f('Hello');})(function (a) {console.log(a)})"
>>>
testJSC $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ args -> valToText (head args) >>= (liftIO . putStrLn . T.unpack) ]
Hello undefined
type JSCallAsFunction
= JSValueRef | Function object |
-> JSValueRef | this |
-> [JSValueRef] | Function arguments |
-> JSC 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 -> JSC JSObjectRef
Make an JavaScript array from a list of values
>>>
testJSC $ eval "['Hello', 'World'][1]"
>>>
testJSC $ array ["Hello", "World"] !! 1
World>>>
testJSC $ eval "['Hello', null, undefined, true, 1]"
>>>
testJSC $ array ("Hello", JSNull, (), True, 1.0::Double)
Hello,,,true,1
Global Object
global :: JSC JSObjectRef
JavaScript's global object
Enumerating Properties
copyPropertyNames :: MakeObjectRef this => this -> JSC JSPropertyNameArrayRef
Get an array containing the property names present on a given object
propertyNames :: MakeObjectRef this => this -> JSC [JSStringRef]
Get a list containing the property names present on a given object
Low level
objCallAsFunction :: MakeArgRefs args => JSObjectRef -> JSObjectRef -> args -> JSValueRefRef -> JSC JSValueRef
Call a JavaScript object as function. Consider using '#'.
objCallAsConstructor :: MakeArgRefs args => JSObjectRef -> args -> JSValueRefRef -> JSC JSValueRef
Call a JavaScript object as a constructor. Consider using new
.