jsc-0.1.1.0: High level interface for webkit-javascriptcore

Safe HaskellNone

Language.Javascript.JSC.Object

Contents

Description

Interface to JavaScript object

Synopsis

Documentation

class MakeObjectRef this whereSource

Anything that can be used to make a JavaScript object reference

Instances

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 JSC as long as it returns something we can make into a JSObjectRef.

Property lookup

(!)Source

Arguments

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

(!!)Source

Arguments

:: MakeObjectRef this 
=> this

Object to look on

-> Index

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

jsSource

Arguments

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

type JSF = MakeObjectRef o => IndexPreservingGetter o (JSC JSValueRef)Source

Java script function applications have this type

jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSFSource

Handy way to call a function

 jsf name = js name . to (# args)
>>> testJSC $ val "Hello World" ^. jsf "indexOf" ["World"]
6

js0 :: MakeStringRef name => name -> JSFSource

Handy way to call a function that expects no arguments

 js0 name = jsf name ()
>>> testJSC $ val "Hello World" ^. js0 "toLowerCase"
hello world

js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSFSource

Handy way to call a function that expects one argument

 js1 name a0 = jsf name [a0]
>>> testJSC $ val "Hello World" ^. js1 "indexOf" "World"
6

js2 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1) => name -> a0 -> a1 -> JSFSource

Handy way to call a function that expects two arguments

js3 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2) => name -> a0 -> a1 -> a2 -> JSFSource

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

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

Handy way to call a function that expects five arguments

jsg :: MakeStringRef a => a -> JSC JSPropRefSource

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

(<#)Source

Arguments

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

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 JSValueRefSource

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 JSValueRefSource

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

obj :: JSC JSObjectRefSource

Make an empty object using the default constuctor

>>> testJSC $ eval "var a = {}; a.x = 'Hello'; a.x"
>>> testJSC $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
Hello

Calling Haskell From JavaScript

functionSource

Arguments

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

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 JSCallAsFunctionSource

Arguments

 = JSValueRef

Function object

-> JSValueRef

this

-> [JSValueRef]

Function arguments

-> JSC JSUndefined

Only 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 :: MakeArgRefs args => args -> JSC JSObjectRefSource

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 JSObjectRefSource

JavaScript's global object

Enumerating Properties

propertyNames :: MakeObjectRef this => this -> JSC [JSStringRef]Source

Get a list containing the property names present on a given object

Low level

objCallAsFunction :: MakeArgRefs args => JSObjectRef -> JSObjectRef -> args -> JSValueRefRef -> JSC JSValueRefSource

Call a JavaScript object as function. Consider using '#'.

objCallAsConstructor :: MakeArgRefs args => JSObjectRef -> args -> JSValueRefRef -> JSC JSValueRefSource

Call a JavaScript object as a constructor. Consider using new.