@@ -9,7 +9,7 @@
99#endif
1010-----------------------------------------------------------------------------
1111--
12−-- Module : Language.Javascript.JSC.Object
12+-- Module : Language.Javascript.JSaddle.Object
1313-- Copyright : (c) Hamish Mackenzie
1414-- License : MIT
1515--
@@ -19,7 +19,7 @@
1919--
2020-----------------------------------------------------------------------------
2121
22−module Language.Javascript.JSC.Object (
22+module Language.Javascript.JSaddle.Object (
2323 JSObjectRef
2424 , MakeObjectRef(..)
2525
@@ -75,7 +75,7 @@ module Language.Javascript.JSC.Object (
7575) where
7676
7777import Prelude hiding ((!!))
78−import Language.Javascript.JSC.Types
78+import Language.Javascript.JSaddle.Types
7979 (JSPropertyNameArrayRef, JSStringRef, JSObjectRef, JSValueRefRef,
8080 JSValueRef, JSContextRef, Index)
8181import Foreign.C.Types (CSize(..), CULong(..), CUInt(..))
@@ -99,46 +99,46 @@ import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSContextRef
9999 (jscontextgetglobalobject)
100100import Foreign (peekArray, nullPtr, withArrayLen)
101101#endif
102−import Language.Javascript.JSC.Exception (rethrow)
103−import Language.Javascript.JSC.Value
102+import Language.Javascript.JSaddle.Exception (rethrow)
103+import Language.Javascript.JSaddle.Value
104104 (JSUndefined, valMakeUndefined, valToObject)
105−import Language.Javascript.JSC.PropRef (JSPropRef(..))
106−import Language.Javascript.JSC.Classes
105+import Language.Javascript.JSaddle.PropRef (JSPropRef(..))
106+import Language.Javascript.JSaddle.Classes
107107 (MakeValueRef(..), MakeStringRef(..), MakeArgRefs(..), MakePropRef(..),
108108 MakeObjectRef(..))
109−import Language.Javascript.JSC.Monad
110− (JSC)
109+import Language.Javascript.JSaddle.Monad
110+ (JSM)
111111import Control.Monad.Trans.Reader (runReaderT, ask)
112112import Control.Monad.IO.Class (MonadIO(..))
113113import qualified Control.Exception as E (catch)
114114import Control.Exception (SomeException)
115115import qualified Data.Text as T (pack)
116116import Foreign.Storable (Storable(..))
117−import Language.Javascript.JSC.Properties
117+import Language.Javascript.JSaddle.Properties
118118import Control.Lens
119119 (IndexPreservingGetter, to, (^.))
120−import Language.Javascript.JSC.String (textToStr)
120+import Language.Javascript.JSaddle.String (textToStr)
121121
122122-- | If we already have a JSObjectRef we are fine
123123instance MakeObjectRef JSObjectRef where
124124 makeObjectRef = return
125125
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
127127-- 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
129129 makeObjectRef v = v >>= makeObjectRef
130130
131131-- | Lookup a property based on its name. This function just constructs a JSPropRef
132132-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
133133-- to JavaScript's @.@ operator.
134134--
135−-- >>> testJSC $ eval "'Hello World'.length"
136−-- >>> testJSC $ val "Hello World" ! "length"
135+-- >>> testJSaddle $ eval "'Hello World'.length"
136+-- >>> testJSaddle $ val "Hello World" ! "length"
137137-- 11
138138(!) :: (MakeObjectRef this, MakeStringRef name)
139139 => this -- ^ Object to look on
140140 -> name -- ^ Name of the property to find
141− -> JSC JSPropRef -- ^ Property reference
141+ -> JSM JSPropRef -- ^ Property reference
142142this ! name = do
143143 rthis <- makeObjectRef this
144144 return (JSPropRef rthis rname)
@@ -149,13 +149,13 @@ this ! name = do
149149-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
150150-- to JavaScript's @[]@ operator.
151151--
152−-- >>> testJSC $ eval "'Hello World'[6]"
153−-- >>> testJSC $ val "Hello World" !! 6
152+-- >>> testJSaddle $ eval "'Hello World'[6]"
153+-- >>> testJSaddle $ val "Hello World" !! 6
154154-- W
155155(!!) :: (MakeObjectRef this)
156156 => this -- ^ Object to look on
157157 -> Index -- ^ Index of the property to lookup
158− -> JSC JSPropRef -- ^ Property reference
158+ -> JSM JSPropRef -- ^ Property reference
159159this !! index = do
160160 rthis <- makeObjectRef this
161161 return (JSPropIndexRef rthis index)
@@ -164,22 +164,22 @@ this !! index = do
164164--
165165-- > js name = to (!name)
166166--
167−-- >>> testJSC $ eval "'Hello World'.length"
168−-- >>> testJSC $ val "Hello World" ^. js "length"
167+-- >>> testJSaddle $ eval "'Hello World'.length"
168+-- >>> testJSaddle $ val "Hello World" ^. js "length"
169169-- 11
170170js :: (MakeObjectRef s, MakeStringRef name)
171171 => name -- ^ Name of the property to find
172− -> IndexPreservingGetter s (JSC JSPropRef)
172+ -> IndexPreservingGetter s (JSM JSPropRef)
173173js name = to (!name)
174174
175175-- | 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)
177177
178178-- | Handy way to call a function
179179--
180180-- > jsf name = js name . to (# args)
181181--
182−-- >>> testJSC $ val "Hello World" ^. jsf "indexOf" ["World"]
182+-- >>> testJSaddle $ val "Hello World" ^. jsf "indexOf" ["World"]
183183-- 6
184184jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSF
185185jsf name args = function . to (# args)
@@ -190,7 +190,7 @@ jsf name args = function . to (# args)
190190--
191191-- > js0 name = jsf name ()
192192--
193−-- >>> testJSC $ val "Hello World" ^. js0 "toLowerCase"
193+-- >>> testJSaddle $ val "Hello World" ^. js0 "toLowerCase"
194194-- hello world
195195js0 :: (MakeStringRef name) => name -> JSF
196196js0 name = jsf name ()
@@ -199,7 +199,7 @@ js0 name = jsf name ()
199199--
200200-- > js1 name a0 = jsf name [a0]
201201--
202−-- >>> testJSC $ val "Hello World" ^. js1 "indexOf" "World"
202+-- >>> testJSaddle $ val "Hello World" ^. js1 "indexOf" "World"
203203-- 6
204204js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSF
205205js1 name a0 = jsf name [a0]
@@ -228,20 +228,20 @@ js5 name a0 a1 a2 a3 a4 = jsf name (a0, a1, a2, a3, a4)
228228
229229-- | Handy way to get and hold onto a reference top level javascript
230230--
231−-- >>> testJSC $ eval "w = console; w.log('Hello World')"
232−-- >>> testJSC $ do w <- jsg "console"; w ^. js "log" # ["Hello World"]
231+-- >>> testJSaddle $ eval "w = console; w.log('Hello World')"
232+-- >>> testJSaddle $ do w <- jsg "console"; w ^. js "log" # ["Hello World"]
233233-- 11
234−jsg :: MakeStringRef a => a -> JSC JSPropRef
234+jsg :: MakeStringRef a => a -> JSM JSPropRef
235235jsg name = global ! name
236236
237237-- | Call a JavaScript function
238238--
239−-- >>> testJSC $ eval "'Hello World'.indexOf('World')"
240−-- >>> testJSC $ val "Hello World" ! "indexOf" # ["World"]
239+-- >>> testJSaddle $ eval "'Hello World'.indexOf('World')"
240+-- >>> testJSaddle $ val "Hello World" ! "indexOf" # ["World"]
241241-- 6
242242infixr 2 #
243243(#) :: (MakePropRef prop, MakeArgRefs args)
244− => prop -> args -> JSC JSValueRef
244+ => prop -> args -> JSM JSValueRef
245245prop # args = do
246246 rprop <- makePropRef prop
247247 (this, f) <- objGetProperty' rprop
@@ -249,14 +249,14 @@ prop # args = do
249249
250250-- | Call a JavaScript function
251251--
252−-- >>> testJSC $ eval "var j = {}; j.x = 1; j.x"
253−-- >>> testJSC $ do {j <- eval "({})"; j!"x" <# 1; j!"x"}
252+-- >>> testJSaddle $ eval "var j = {}; j.x = 1; j.x"
253+-- >>> testJSaddle $ do {j <- eval "({})"; j!"x" <# 1; j!"x"}
254254-- 1
255255infixr 0 <#
256256(<#) :: (MakePropRef prop, MakeValueRef val)
257257 => prop -- ^ Property to set
258258 -> val -- ^ Value to set it to
259− -> JSC JSPropRef -- ^ Reference to the property set
259+ -> JSM JSPropRef -- ^ Reference to the property set
260260prop <# val = do
261261 p <- makePropRef prop
262262 objSetProperty p val
@@ -264,23 +264,23 @@ prop <# val = do
264264
265265-- | Use this to create a new JavaScript object
266266--
267−-- >>> testJSC $ new "Date" (2013, 1, 1)
267+-- >>> testJSaddle $ new "Date" (2013, 1, 1)
268268-- Fri Feb 01 2013 00:00:00 GMT+1300 (NZDT)
269269new :: (MakeObjectRef constructor, MakeArgRefs args)
270270 => constructor
271271 -> args
272− -> JSC JSValueRef
272+ -> JSM JSValueRef
273273new constructor args = do
274274 f <- makeObjectRef constructor
275275 rethrow $ objCallAsConstructor f args
276276
277277-- | Call function with a given @this@. In most cases you should use '#'.
278278--
279−-- >>> testJSC $ eval "(function(){return this;}).apply('Hello', [])"
280−-- >>> testJSC $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
279+-- >>> testJSaddle $ eval "(function(){return this;}).apply('Hello', [])"
280+-- >>> testJSaddle $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
281281-- Hello
282282call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args)
283− => function -> this -> args -> JSC JSValueRef
283+ => function -> this -> args -> JSM JSValueRef
284284call function this args = do
285285 rfunction <- makeObjectRef function
286286 rthis <- makeObjectRef this
@@ -288,10 +288,10 @@ call function this args = do
288288
289289-- | Make an empty object using the default constuctor
290290--
291−-- >>> testJSC $ eval "var a = {}; a.x = 'Hello'; a.x"
292−-- >>> testJSC $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
291+-- >>> testJSaddle $ eval "var a = {}; a.x = 'Hello'; a.x"
292+-- >>> testJSaddle $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
293293-- Hello
294−obj :: JSC JSObjectRef
294+obj :: JSM JSObjectRef
295295#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
296296obj = liftIO $ newObj
297297#else
@@ -304,7 +304,7 @@ obj = do
304304type JSCallAsFunction = JSValueRef -- ^ Function object
305305 -> JSValueRef -- ^ this
306306 -> [JSValueRef] -- ^ Function arguments
307− -> JSC JSUndefined -- ^ Only 'JSUndefined' can be returned because
307+ -> JSM JSUndefined -- ^ Only 'JSUndefined' can be returned because
308308 -- the function may need to be executed in a
309309 -- different thread. If you need to get a
310310 -- value out pass in a continuation function
@@ -313,8 +313,8 @@ type JSCallAsFunction = JSValueRef -- ^ Function object
313313-- | Short hand @::JSCallAsFunction@ so a haskell function can be passed to
314314-- a to a JavaScipt one.
315315--
316−-- >>> testJSC $ 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) ]
316+-- >>> testJSaddle $ eval "(function(f) {f('Hello');})(function (a) {console.log(a)})"
317+-- >>> testJSaddle $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ args -> valToText (head args) >>= (liftIO . putStrLn . T.unpack) ]
318318-- Hello
319319-- undefined
320320fun :: JSCallAsFunction -> JSCallAsFunction
@@ -329,7 +329,7 @@ foreign import ccall "wrapper"
329329function :: MakeStringRef name
330330 => name -- ^ Name of the function
331331 -> 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
333333 -- call the Haskell one when it is called
334334#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
335335function name f = liftIO $ do
@@ -376,7 +376,7 @@ instance MakeArgRefs JSCallAsFunction where
376376#endif
377377 return [rarg]
378378
379−makeArray :: MakeArgRefs args => args -> JSValueRefRef -> JSC JSObjectRef
379+makeArray :: MakeArgRefs args => args -> JSValueRefRef -> JSM JSObjectRef
380380#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
381381makeArray args exceptions = makeArgRefs args >>= liftM castRef . liftIO . toArray
382382#else
@@ -389,17 +389,17 @@ makeArray args exceptions = do
389389
390390-- | Make an JavaScript array from a list of values
391391--
392−-- >>> testJSC $ eval "['Hello', 'World'][1]"
393−-- >>> testJSC $ array ["Hello", "World"] !! 1
392+-- >>> testJSaddle $ eval "['Hello', 'World'][1]"
393+-- >>> testJSaddle $ array ["Hello", "World"] !! 1
394394-- World
395−-- >>> testJSC $ eval "['Hello', null, undefined, true, 1]"
396−-- >>> testJSC $ array ("Hello", JSNull, (), True, 1.0::Double)
395+-- >>> testJSaddle $ eval "['Hello', null, undefined, true, 1]"
396+-- >>> testJSaddle $ array ("Hello", JSNull, (), True, 1.0::Double)
397397-- Hello,,,true,1
398−array :: MakeArgRefs args => args -> JSC JSObjectRef
398+array :: MakeArgRefs args => args -> JSM JSObjectRef
399399array = rethrow . makeArray
400400
401401-- | JavaScript's global object
402−global :: JSC JSObjectRef
402+global :: JSM JSObjectRef
403403#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
404404global = liftIO js_window
405405foreign import javascript unsafe "$r = window"
@@ -412,7 +412,7 @@ global = undefined
412412
413413-- | Get an array containing the property names present on a given object
414414#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
416416copyPropertyNames this = do
417417 gctxt <- ask
418418 rthis <- makeObjectRef this
@@ -434,7 +434,7 @@ propertyNamesList names = do
434434#endif
435435
436436-- | 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]
438438#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
439439propertyNames this = makeObjectRef this >>= liftIO . js_propertyNames >>= liftIO . fromArray
440440foreign import javascript unsafe "$r = []; h$forIn($1, function(n){$r.push(n);})"
@@ -446,7 +446,7 @@ propertyNames = undefined
446446#endif
447447
448448-- | 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]
450450properties this = propertyNames this >>= mapM (this !)
451451
452452-- | Call a JavaScript object as function. Consider using '#'.
@@ -455,7 +455,7 @@ objCallAsFunction :: MakeArgRefs args
455455 -> JSObjectRef
456456 -> args
457457 -> JSValueRefRef
458− -> JSC JSValueRef
458+ -> JSM JSValueRef
459459#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
460460objCallAsFunction function this args exceptions = do
461461 rargs <- makeArgRefs args >>= liftIO . toArray
@@ -477,7 +477,7 @@ objCallAsConstructor :: MakeArgRefs args
477477 => JSObjectRef
478478 -> args
479479 -> JSValueRefRef
480− -> JSC JSValueRef
480+ -> JSM JSValueRef
481481#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
482482objCallAsConstructor function args exceptions = do
483483 rargs <- makeArgRefs args >>= liftIO . toArray