@@ -9,7 +9,7 @@
10-----------------------------------------------------------------------------
10-----------------------------------------------------------------------------
12−-- Module : Language.Javascript.JSC.Object
12+-- Module : Language.Javascript.JSaddle.Object
13-- Copyright : (c) Hamish Mackenzie
13-- Copyright : (c) Hamish Mackenzie
14-- License : MIT
14-- License : MIT
@@ -19,7 +19,7 @@
20-----------------------------------------------------------------------------
20-----------------------------------------------------------------------------
22−module Language.Javascript.JSC.Object (
22+module Language.Javascript.JSaddle.Object (
23 JSObjectRef
23 JSObjectRef
24 , MakeObjectRef(..)
24 , MakeObjectRef(..)
@@ -75,7 +75,7 @@ module Language.Javascript.JSC.Object (
77import Prelude hiding ((!!))
77import Prelude hiding ((!!))
78−import Language.Javascript.JSC.Types
78+import Language.Javascript.JSaddle.Types
79 (JSPropertyNameArrayRef, JSStringRef, JSObjectRef, JSValueRefRef,
79 (JSPropertyNameArrayRef, JSStringRef, JSObjectRef, JSValueRefRef,
80 JSValueRef, JSContextRef, Index)
80 JSValueRef, JSContextRef, Index)
81import Foreign.C.Types (CSize(..), CULong(..), CUInt(..))
81import Foreign.C.Types (CSize(..), CULong(..), CUInt(..))
@@ -99,46 +99,46 @@ import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSContextRef
99 (jscontextgetglobalobject)
99 (jscontextgetglobalobject)
100import Foreign (peekArray, nullPtr, withArrayLen)
100import Foreign (peekArray, nullPtr, withArrayLen)
102−import Language.Javascript.JSC.Exception (rethrow)
102+import Language.Javascript.JSaddle.Exception (rethrow)
103−import Language.Javascript.JSC.Value
103+import Language.Javascript.JSaddle.Value
104 (JSUndefined, valMakeUndefined, valToObject)
104 (JSUndefined, valMakeUndefined, valToObject)
105−import Language.Javascript.JSC.PropRef (JSPropRef(..))
105+import Language.Javascript.JSaddle.PropRef (JSPropRef(..))
106−import Language.Javascript.JSC.Classes
106+import Language.Javascript.JSaddle.Classes
107 (MakeValueRef(..), MakeStringRef(..), MakeArgRefs(..), MakePropRef(..),
107 (MakeValueRef(..), MakeStringRef(..), MakeArgRefs(..), MakePropRef(..),
108 MakeObjectRef(..))
108 MakeObjectRef(..))
109−import Language.Javascript.JSC.Monad
109+import Language.Javascript.JSaddle.Monad
111import Control.Monad.Trans.Reader (runReaderT, ask)
111import Control.Monad.Trans.Reader (runReaderT, ask)
112import Control.Monad.IO.Class (MonadIO(..))
112import Control.Monad.IO.Class (MonadIO(..))
113import qualified Control.Exception as E (catch)
113import qualified Control.Exception as E (catch)
114import Control.Exception (SomeException)
114import Control.Exception (SomeException)
115import qualified Data.Text as T (pack)
115import qualified Data.Text as T (pack)
116import Foreign.Storable (Storable(..))
116import Foreign.Storable (Storable(..))
117−import Language.Javascript.JSC.Properties
117+import Language.Javascript.JSaddle.Properties
118import Control.Lens
118import Control.Lens
119 (IndexPreservingGetter, to, (^.))
119 (IndexPreservingGetter, to, (^.))
120−import Language.Javascript.JSC.String (textToStr)
120+import Language.Javascript.JSaddle.String (textToStr)
122-- | If we already have a JSObjectRef we are fine
122-- | If we already have a JSObjectRef we are fine
123instance MakeObjectRef JSObjectRef where
123instance MakeObjectRef JSObjectRef where
124 makeObjectRef = return
124 makeObjectRef = return
126−-- | JSObjectRef can be made by evaluating a function in 'JSC' as long
126+-- | JSObjectRef can be made by evaluating a function in 'JSM' as long
127-- as it returns something we can make into a JSObjectRef.
127-- as it returns something we can make into a JSObjectRef.
128−instance MakeObjectRef v => MakeObjectRef (JSC v) where
128+instance MakeObjectRef v => MakeObjectRef (JSM v) where
129 makeObjectRef v = v >>= makeObjectRef
129 makeObjectRef v = v >>= makeObjectRef
131-- | Lookup a property based on its name. This function just constructs a JSPropRef
131-- | Lookup a property based on its name. This function just constructs a JSPropRef
132-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
132-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
133-- to JavaScript's @.@ operator.
133-- to JavaScript's @.@ operator.
135−-- >>> testJSC $ eval "'Hello World'.length"
135+-- >>> testJSaddle $ eval "'Hello World'.length"
136−-- >>> testJSC $ val "Hello World" ! "length"
136+-- >>> testJSaddle $ val "Hello World" ! "length"
138(!) :: (MakeObjectRef this, MakeStringRef name)
138(!) :: (MakeObjectRef this, MakeStringRef name)
139 => this -- ^ Object to look on
139 => this -- ^ Object to look on
140 -> name -- ^ Name of the property to find
140 -> name -- ^ Name of the property to find
141− -> JSC JSPropRef -- ^ Property reference
141+ -> JSM JSPropRef -- ^ Property reference
142this ! name = do
142this ! name = do
143 rthis <- makeObjectRef this
143 rthis <- makeObjectRef this
144 return (JSPropRef rthis rname)
144 return (JSPropRef rthis rname)
@@ -149,13 +149,13 @@ this ! name = do
149-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
149-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
150-- to JavaScript's @[]@ operator.
150-- to JavaScript's @[]@ operator.
152−-- >>> testJSC $ eval "'Hello World'[6]"
152+-- >>> testJSaddle $ eval "'Hello World'[6]"
153−-- >>> testJSC $ val "Hello World" !! 6
153+-- >>> testJSaddle $ val "Hello World" !! 6
155(!!) :: (MakeObjectRef this)
155(!!) :: (MakeObjectRef this)
156 => this -- ^ Object to look on
156 => this -- ^ Object to look on
157 -> Index -- ^ Index of the property to lookup
157 -> Index -- ^ Index of the property to lookup
158− -> JSC JSPropRef -- ^ Property reference
158+ -> JSM JSPropRef -- ^ Property reference
159this !! index = do
159this !! index = do
160 rthis <- makeObjectRef this
160 rthis <- makeObjectRef this
161 return (JSPropIndexRef rthis index)
161 return (JSPropIndexRef rthis index)
@@ -164,22 +164,22 @@ this !! index = do
165-- > js name = to (!name)
165-- > js name = to (!name)
167−-- >>> testJSC $ eval "'Hello World'.length"
167+-- >>> testJSaddle $ eval "'Hello World'.length"
168−-- >>> testJSC $ val "Hello World" ^. js "length"
168+-- >>> testJSaddle $ val "Hello World" ^. js "length"
170js :: (MakeObjectRef s, MakeStringRef name)
170js :: (MakeObjectRef s, MakeStringRef name)
171 => name -- ^ Name of the property to find
171 => name -- ^ Name of the property to find
172− -> IndexPreservingGetter s (JSC JSPropRef)
172+ -> IndexPreservingGetter s (JSM JSPropRef)
173js name = to (!name)
173js name = to (!name)
175-- | Java script function applications have this type
175-- | Java script function applications have this type
176−type JSF = MakeObjectRef o => IndexPreservingGetter o (JSC JSValueRef)
176+type JSF = MakeObjectRef o => IndexPreservingGetter o (JSM JSValueRef)
178-- | Handy way to call a function
178-- | Handy way to call a function
180-- > jsf name = js name . to (# args)
180-- > jsf name = js name . to (# args)
182−-- >>> testJSC $ val "Hello World" ^. jsf "indexOf" ["World"]
182+-- >>> testJSaddle $ val "Hello World" ^. jsf "indexOf" ["World"]
184jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSF
184jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSF
185jsf name args = function . to (# args)
185jsf name args = function . to (# args)
@@ -190,7 +190,7 @@ jsf name args = function . to (# args)
191-- > js0 name = jsf name ()
191-- > js0 name = jsf name ()
193−-- >>> testJSC $ val "Hello World" ^. js0 "toLowerCase"
193+-- >>> testJSaddle $ val "Hello World" ^. js0 "toLowerCase"
194-- hello world
194-- hello world
195js0 :: (MakeStringRef name) => name -> JSF
195js0 :: (MakeStringRef name) => name -> JSF
196js0 name = jsf name ()
196js0 name = jsf name ()
@@ -199,7 +199,7 @@ js0 name = jsf name ()
200-- > js1 name a0 = jsf name [a0]
200-- > js1 name a0 = jsf name [a0]
202−-- >>> testJSC $ val "Hello World" ^. js1 "indexOf" "World"
202+-- >>> testJSaddle $ val "Hello World" ^. js1 "indexOf" "World"
204js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSF
204js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSF
205js1 name a0 = jsf name [a0]
205js1 name a0 = jsf name [a0]
@@ -228,20 +228,20 @@ js5 name a0 a1 a2 a3 a4 = jsf name (a0, a1, a2, a3, a4)
229-- | Handy way to get and hold onto a reference top level javascript
229-- | Handy way to get and hold onto a reference top level javascript
231−-- >>> testJSC $ eval "w = console; w.log('Hello World')"
231+-- >>> testJSaddle $ eval "w = console; w.log('Hello World')"
232−-- >>> testJSC $ do w <- jsg "console"; w ^. js "log" # ["Hello World"]
232+-- >>> testJSaddle $ do w <- jsg "console"; w ^. js "log" # ["Hello World"]
234−jsg :: MakeStringRef a => a -> JSC JSPropRef
234+jsg :: MakeStringRef a => a -> JSM JSPropRef
235jsg name = global ! name
235jsg name = global ! name
237-- | Call a JavaScript function
237-- | Call a JavaScript function
239−-- >>> testJSC $ eval "'Hello World'.indexOf('World')"
239+-- >>> testJSaddle $ eval "'Hello World'.indexOf('World')"
240−-- >>> testJSC $ val "Hello World" ! "indexOf" # ["World"]
240+-- >>> testJSaddle $ val "Hello World" ! "indexOf" # ["World"]
242infixr 2 #
242infixr 2 #
243(#) :: (MakePropRef prop, MakeArgRefs args)
243(#) :: (MakePropRef prop, MakeArgRefs args)
244− => prop -> args -> JSC JSValueRef
244+ => prop -> args -> JSM JSValueRef
245prop # args = do
245prop # args = do
246 rprop <- makePropRef prop
246 rprop <- makePropRef prop
247 (this, f) <- objGetProperty' rprop
247 (this, f) <- objGetProperty' rprop
@@ -249,14 +249,14 @@ prop # args = do
250-- | Call a JavaScript function
250-- | Call a JavaScript function
252−-- >>> testJSC $ eval "var j = {}; j.x = 1; j.x"
252+-- >>> testJSaddle $ eval "var j = {}; j.x = 1; j.x"
253−-- >>> testJSC $ do {j <- eval "({})"; j!"x" <# 1; j!"x"}
253+-- >>> testJSaddle $ do {j <- eval "({})"; j!"x" <# 1; j!"x"}
255infixr 0 <#
255infixr 0 <#
256(<#) :: (MakePropRef prop, MakeValueRef val)
256(<#) :: (MakePropRef prop, MakeValueRef val)
257 => prop -- ^ Property to set
257 => prop -- ^ Property to set
258 -> val -- ^ Value to set it to
258 -> val -- ^ Value to set it to
259− -> JSC JSPropRef -- ^ Reference to the property set
259+ -> JSM JSPropRef -- ^ Reference to the property set
260prop <# val = do
260prop <# val = do
261 p <- makePropRef prop
261 p <- makePropRef prop
262 objSetProperty p val
262 objSetProperty p val
@@ -264,23 +264,23 @@ prop <# val = do
265-- | Use this to create a new JavaScript object
265-- | Use this to create a new JavaScript object
267−-- >>> testJSC $ new "Date" (2013, 1, 1)
267+-- >>> testJSaddle $ new "Date" (2013, 1, 1)
268-- Fri Feb 01 2013 00:00:00 GMT+1300 (NZDT)
268-- Fri Feb 01 2013 00:00:00 GMT+1300 (NZDT)
269new :: (MakeObjectRef constructor, MakeArgRefs args)
269new :: (MakeObjectRef constructor, MakeArgRefs args)
270 => constructor
270 => constructor
272− -> JSC JSValueRef
272+ -> JSM JSValueRef
273new constructor args = do
273new constructor args = do
274 f <- makeObjectRef constructor
274 f <- makeObjectRef constructor
275 rethrow $ objCallAsConstructor f args
275 rethrow $ objCallAsConstructor f args
277-- | Call function with a given @this@. In most cases you should use '#'.
277-- | Call function with a given @this@. In most cases you should use '#'.
279−-- >>> testJSC $ eval "(function(){return this;}).apply('Hello', [])"
279+-- >>> testJSaddle $ eval "(function(){return this;}).apply('Hello', [])"
280−-- >>> testJSC $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
280+-- >>> testJSaddle $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
282call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args)
282call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args)
283− => function -> this -> args -> JSC JSValueRef
283+ => function -> this -> args -> JSM JSValueRef
284call function this args = do
284call function this args = do
285 rfunction <- makeObjectRef function
285 rfunction <- makeObjectRef function
286 rthis <- makeObjectRef this
286 rthis <- makeObjectRef this
@@ -288,10 +288,10 @@ call function this args = do
289-- | Make an empty object using the default constuctor
289-- | Make an empty object using the default constuctor
291−-- >>> testJSC $ eval "var a = {}; a.x = 'Hello'; a.x"
291+-- >>> testJSaddle $ eval "var a = {}; a.x = 'Hello'; a.x"
292−-- >>> testJSC $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
292+-- >>> testJSaddle $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
294−obj :: JSC JSObjectRef
294+obj :: JSM JSObjectRef
295#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
295#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
296obj = liftIO $ newObj
296obj = liftIO $ newObj
@@ -304,7 +304,7 @@ obj = do
304type JSCallAsFunction = JSValueRef -- ^ Function object
304type JSCallAsFunction = JSValueRef -- ^ Function object
305 -> JSValueRef -- ^ this
305 -> JSValueRef -- ^ this
306 -> [JSValueRef] -- ^ Function arguments
306 -> [JSValueRef] -- ^ Function arguments
307− -> JSC JSUndefined -- ^ Only 'JSUndefined' can be returned because
307+ -> JSM JSUndefined -- ^ Only 'JSUndefined' can be returned because
308 -- the function may need to be executed in a
308 -- the function may need to be executed in a
309 -- different thread. If you need to get a
309 -- different thread. If you need to get a
310 -- value out pass in a continuation function
310 -- value out pass in a continuation function
@@ -313,8 +313,8 @@ type JSCallAsFunction = JSValueRef -- ^ Function object
313-- | Short hand @::JSCallAsFunction@ so a haskell function can be passed to
313-- | Short hand @::JSCallAsFunction@ so a haskell function can be passed to
314-- a to a JavaScipt one.
314-- a to a JavaScipt one.
316−-- >>> testJSC $ eval "(function(f) {f('Hello');})(function (a) {console.log(a)})"
316+-- >>> testJSaddle $ eval "(function(f) {f('Hello');})(function (a) {console.log(a)})"
317−-- >>> testJSC $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ args -> valToText (head args) >>= (liftIO . putStrLn . T.unpack) ]
317+-- >>> testJSaddle $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ args -> valToText (head args) >>= (liftIO . putStrLn . T.unpack) ]
319-- undefined
319-- undefined
320fun :: JSCallAsFunction -> JSCallAsFunction
320fun :: JSCallAsFunction -> JSCallAsFunction
@@ -329,7 +329,7 @@ foreign import ccall "wrapper"
329function :: MakeStringRef name
329function :: MakeStringRef name
330 => name -- ^ Name of the function
330 => name -- ^ Name of the function
331 -> JSCallAsFunction -- ^ Haskell function to call
331 -> JSCallAsFunction -- ^ Haskell function to call
332− -> JSC JSObjectRef -- ^ Returns a JavaScript function object that will
332+ -> JSM JSObjectRef -- ^ Returns a JavaScript function object that will
333 -- call the Haskell one when it is called
333 -- call the Haskell one when it is called
334#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
334#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
335function name f = liftIO $ do
335function name f = liftIO $ do
@@ -376,7 +376,7 @@ instance MakeArgRefs JSCallAsFunction where
377 return [rarg]
377 return [rarg]
379−makeArray :: MakeArgRefs args => args -> JSValueRefRef -> JSC JSObjectRef
379+makeArray :: MakeArgRefs args => args -> JSValueRefRef -> JSM JSObjectRef
380#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
380#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
381makeArray args exceptions = makeArgRefs args >>= liftM castRef . liftIO . toArray
381makeArray args exceptions = makeArgRefs args >>= liftM castRef . liftIO . toArray
@@ -389,17 +389,17 @@ makeArray args exceptions = do
390-- | Make an JavaScript array from a list of values
390-- | Make an JavaScript array from a list of values
392−-- >>> testJSC $ eval "['Hello', 'World'][1]"
392+-- >>> testJSaddle $ eval "['Hello', 'World'][1]"
393−-- >>> testJSC $ array ["Hello", "World"] !! 1
393+-- >>> testJSaddle $ array ["Hello", "World"] !! 1
395−-- >>> testJSC $ eval "['Hello', null, undefined, true, 1]"
395+-- >>> testJSaddle $ eval "['Hello', null, undefined, true, 1]"
396−-- >>> testJSC $ array ("Hello", JSNull, (), True, 1.0::Double)
396+-- >>> testJSaddle $ array ("Hello", JSNull, (), True, 1.0::Double)
397-- Hello,,,true,1
397-- Hello,,,true,1
398−array :: MakeArgRefs args => args -> JSC JSObjectRef
398+array :: MakeArgRefs args => args -> JSM JSObjectRef
399array = rethrow . makeArray
399array = rethrow . makeArray
401-- | JavaScript's global object
401-- | JavaScript's global object
402−global :: JSC JSObjectRef
402+global :: JSM JSObjectRef
403#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
403#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
404global = liftIO js_window
404global = liftIO js_window
405foreign import javascript unsafe "$r = window"
405foreign import javascript unsafe "$r = window"
@@ -412,7 +412,7 @@ global = undefined
413-- | Get an array containing the property names present on a given object
413-- | Get an array containing the property names present on a given object
414#if (!defined(ghcjs_HOST_OS) || !defined(USE_JAVASCRIPTFFI)) && defined(USE_WEBKIT)
414#if (!defined(ghcjs_HOST_OS) || !defined(USE_JAVASCRIPTFFI)) && defined(USE_WEBKIT)
415−copyPropertyNames :: MakeObjectRef this => this -> JSC JSPropertyNameArrayRef
415+copyPropertyNames :: MakeObjectRef this => this -> JSM JSPropertyNameArrayRef
416copyPropertyNames this = do
416copyPropertyNames this = do
417 gctxt <- ask
417 gctxt <- ask
418 rthis <- makeObjectRef this
418 rthis <- makeObjectRef this
@@ -434,7 +434,7 @@ propertyNamesList names = do
436-- | Get a list containing the property names present on a given object
436-- | Get a list containing the property names present on a given object
437−propertyNames :: MakeObjectRef this => this -> JSC [JSStringRef]
437+propertyNames :: MakeObjectRef this => this -> JSM [JSStringRef]
438#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
438#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
439propertyNames this = makeObjectRef this >>= liftIO . js_propertyNames >>= liftIO . fromArray
439propertyNames this = makeObjectRef this >>= liftIO . js_propertyNames >>= liftIO . fromArray
440foreign import javascript unsafe "$r = []; h$forIn($1, function(n){$r.push(n);})"
440foreign import javascript unsafe "$r = []; h$forIn($1, function(n){$r.push(n);})"
@@ -446,7 +446,7 @@ propertyNames = undefined
448-- | Get a list containing references to all the properties present on a given object
448-- | Get a list containing references to all the properties present on a given object
449−properties :: MakeObjectRef this => this -> JSC [JSPropRef]
449+properties :: MakeObjectRef this => this -> JSM [JSPropRef]
450properties this = propertyNames this >>= mapM (this !)
450properties this = propertyNames this >>= mapM (this !)
452-- | Call a JavaScript object as function. Consider using '#'.
452-- | Call a JavaScript object as function. Consider using '#'.
@@ -455,7 +455,7 @@ objCallAsFunction :: MakeArgRefs args
455 -> JSObjectRef
455 -> JSObjectRef
457 -> JSValueRefRef
457 -> JSValueRefRef
458− -> JSC JSValueRef
458+ -> JSM JSValueRef
459#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
459#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
460objCallAsFunction function this args exceptions = do
460objCallAsFunction function this args exceptions = do
461 rargs <- makeArgRefs args >>= liftIO . toArray
461 rargs <- makeArgRefs args >>= liftIO . toArray
@@ -477,7 +477,7 @@ objCallAsConstructor :: MakeArgRefs args
477 => JSObjectRef
477 => JSObjectRef
479 -> JSValueRefRef
479 -> JSValueRefRef
480− -> JSC JSValueRef
480+ -> JSM JSValueRef
481#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
481#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
482objCallAsConstructor function args exceptions = do
482objCallAsConstructor function args exceptions = do
483 rargs <- makeArgRefs args >>= liftIO . toArray
483 rargs <- makeArgRefs args >>= liftIO . toArray