@@ -112,8 +112,8 @@ import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
112112 jspropertynamearraygetcount, jsobjectcopypropertynames,
113113 jsobjectcallasconstructor, jsobjectmakearray,
114114 jsobjectcallasfunction,
115− JSObjectCallAsFunctionCallback,
116− jsobjectmakefunctionwithcallback, JSObjectCallAsFunctionCallback')
115+ JSObjectCallAsFunctionCallback, mkJSObjectCallAsFunctionCallback,
116+ jsobjectmakefunctionwithcallback)
117117import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSValueRef
118118 (jsvaluemakeundefined)
119119import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSContextRef
@@ -121,8 +121,8 @@ import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSContextRef
121121import Foreign (peekArray, nullPtr, withArrayLen)
122122import Foreign.Ptr (freeHaskellFunPtr)
123123import Language.Javascript.JSaddle.Native
124− (makeNewJSVal, wrapJSString, withJSVals, withObject, withJSString,
125− withToJSVal)
124+ (makeNewJSString, makeNewJSVal, wrapJSString, withJSVals,
125+ withObject, withJSString, withToJSVal)
126126import System.IO.Unsafe (unsafePerformIO)
127127import Foreign.ForeignPtr (newForeignPtr_)
128128#endif
@@ -147,8 +147,9 @@ import Data.Text (Text)
147147-- $setup
148148-- >>> import Language.Javascript.JSaddle.Test (testJSaddle)
149149-- >>> import Language.Javascript.JSaddle.Evaluate (eval)
150−-- >>> import Language.Javascript.JSaddle.Value (val)
150+-- >>> import Language.Javascript.JSaddle.Value (val, valToText, JSNull(..))
151151-- >>> import Control.Lens.Operators ((^.))
152+-- >>> import qualified Data.Text as T (unpack)
152153
153154-- | Object can be made by evaluating a fnction in 'JSM' as long
154155-- as it returns something we can make into a Object.
@@ -159,6 +160,7 @@ instance MakeObject v => MakeObject (JSM v) where
159160-- | Lookup a property based on its name.
160161--
161162-- >>> testJSaddle $ eval "'Hello World'.length"
163+-- 11
162164-- >>> testJSaddle $ val "Hello World" ! "length"
163165-- 11
164166(!) :: (MakeObject this, ToJSString name)
@@ -175,6 +177,7 @@ this ! name = do
175177-- | Lookup a property based on its index.
176178--
177179-- >>> testJSaddle $ eval "'Hello World'[6]"
180+-- W
178181-- >>> testJSaddle $ val "Hello World" !! 6
179182-- W
180183(!!) :: (MakeObject this)
@@ -191,6 +194,7 @@ this !! index = do
191194-- > js name = to (!name)
192195--
193196-- >>> testJSaddle $ eval "'Hello World'.length"
197+-- 11
194198-- >>> testJSaddle $ val "Hello World" ^. js "length"
195199-- 11
196200js :: (MakeObject s, ToJSString name)
@@ -204,6 +208,7 @@ js name = to (!name)
204208-- > jss name = to (<#name)
205209--
206210-- >>> testJSaddle $ eval "'Hello World'.length"
211+-- 11
207212-- >>> testJSaddle $ val "Hello World" ^. js "length"
208213-- 11
209214jss :: (ToJSString name, ToJSVal val)
@@ -275,8 +280,13 @@ js5 name a0 a1 a2 a3 a4 = jsf name (a0, a1, a2, a3, a4)
275280-- | Handy way to get and hold onto a reference top level javascript
276281--
277282-- >>> testJSaddle $ eval "w = console; w.log('Hello World')"
278−-- >>> testJSaddle $ do w <- jsg "console"; w ^. js "log" # ["Hello World"]
279−-- 11
283+-- ** Message: console message: @1: Hello World
284+-- <BLANKLINE>
285+-- undefined
286+-- >>> testJSaddle $ do w <- jsg "console"; w ^. js1 "log" "Hello World"
287+-- ** Message: console message: [native code] @0: Hello World
288+-- <BLANKLINE>
289+-- undefined
280290jsg :: ToJSString a => a -> JSM JSVal
281291jsg name = global ! name
282292{-# INLINE jsg #-}
@@ -285,9 +295,10 @@ jsg name = global ! name
285295--
286296-- > jsgf name = jsg name . to (# args)
287297--
288−-- >>> testJSaddle $ eval "globalFunc = function(x) {x.length}"
298+-- >>> testJSaddle $ eval "globalFunc = function (x) {return x.length;}"
299+-- function (x) {return x.length;}
289300-- >>> testJSaddle $ jsgf "globalFunc" ["World"]
290−-- 6
301+-- 5
291302jsgf :: (ToJSString name, MakeArgs args) => name -> args -> JSM JSVal
292303jsgf name = global # name
293304{-# INLINE jsgf #-}
@@ -297,7 +308,7 @@ jsgf name = global # name
297308-- > jsg0 name = jsgf name ()
298309--
299310-- >>> testJSaddle $ jsg0 "globalFunc"
300−-- hello world
311+-- TypeError: undefined is not an object (evaluating 'x.length')
301312jsg0 :: (ToJSString name) => name -> JSM JSVal
302313jsg0 name = jsgf name ()
303314{-# INLINE jsg0 #-}
@@ -307,7 +318,7 @@ jsg0 name = jsgf name ()
307318-- > jsg1 name a0 = jsgf name [a0]
308319--
309320-- >>> testJSaddle $ jsg1 "globalFunc" "World"
310−-- 6
321+-- 5
311322jsg1 :: (ToJSString name, ToJSVal a0) => name -> a0 -> JSM JSVal
312323jsg1 name a0 = jsgf name [a0]
313324{-# INLINE jsg1 #-}
@@ -340,6 +351,7 @@ jsg5 name a0 a1 a2 a3 a4 = jsgf name (a0, a1, a2, a3, a4)
340351-- | Call a JavaScript function
341352--
342353-- >>> testJSaddle $ eval "'Hello World'.indexOf('World')"
354+-- 6
343355-- >>> testJSaddle $ val "Hello World" # "indexOf" $ ["World"]
344356-- 6
345357infixr 2 #
@@ -354,8 +366,10 @@ infixr 2 #
354366
355367-- | Call a JavaScript function
356368--
357−-- >>> testJSaddle $ eval "something[6]('World')"
358−-- >>> testJSaddle $ val something ## 6 ["World"]
369+-- >>> testJSaddle $ eval "something = {}; something[6]=function (x) {return x.length;}; something[6]('World')"
370+-- 5
371+-- >>> testJSaddle $ jsg "something" ## 6 $ ["World"]
372+-- 5
359373infixr 2 ##
360374(##) :: (MakeObject this, MakeArgs args)
361375 => this -> Index -> args -> JSM JSVal
@@ -369,7 +383,8 @@ infixr 2 ##
369383-- | Set a JavaScript property
370384--
371385-- >>> testJSaddle $ eval "var j = {}; j.x = 1; j.x"
372−-- >>> testJSaddle $ do {j <- eval "({})"; j <# "x" 1; j!"x"}
386+-- 1
387+-- >>> testJSaddle $ do {j <- obj; (j <# "x") 1; j!"x"}
373388-- 1
374389infixr 1 <#
375390(<#) :: (MakeObject this, ToJSString name, ToJSVal val)
@@ -385,7 +400,8 @@ infixr 1 <#
385400-- | Set a JavaScript property
386401--
387402-- >>> testJSaddle $ eval "var j = {}; j[6] = 1; j[6]"
388−-- >>> testJSaddle $ do {j <- eval "({})"; j <## 6 1; j!!6}
403+-- 1
404+-- >>> testJSaddle $ do {j <- obj; (j <## 6) 1; j!!6}
389405-- 1
390406infixr 1 <##
391407(<##) :: (MakeObject this, ToJSVal val)
@@ -417,6 +433,7 @@ new constructor args = do
417433-- | Call function with a given @this@. In most cases you should use '#'.
418434--
419435-- >>> testJSaddle $ eval "(function(){return this;}).apply('Hello', [])"
436+-- Hello
420437-- >>> testJSaddle $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
421438-- Hello
422439call :: (MakeObject f, MakeObject this, MakeArgs args)
@@ -430,7 +447,8 @@ call f this args = do
430447-- | Make an empty object using the default constuctor
431448--
432449-- >>> testJSaddle $ eval "var a = {}; a.x = 'Hello'; a.x"
433−-- >>> testJSaddle $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
450+-- Hello
451+-- >>> testJSaddle $ do { a <- obj; (a <# "x") "Hello"; a ^. js "x" }
434452-- Hello
435453obj :: JSM Object
436454#ifdef ghcjs_HOST_OS
@@ -456,6 +474,9 @@ type JSCallAsFunction = JSVal -- ^ Function object
456474-- a to a JavaScipt one.
457475--
458476-- >>> testJSaddle $ eval "(function(f) {f('Hello');})(function (a) {console.log(a)})"
477+-- ** Message: console message: @1: Hello
478+-- <BLANKLINE>
479+-- undefined
459480-- >>> testJSaddle $ call (eval "(function(f) {f('Hello');})") global [fun $ \ _ _ args -> valToText (head args) >>= (liftIO . putStrLn . T.unpack) ]
460481-- Hello
461482-- undefined
@@ -463,11 +484,6 @@ fun :: JSCallAsFunction -> JSCallAsFunction
463484fun = id
464485{-# INLINE fun #-}
465486
466−#if !defined(ghcjs_HOST_OS)
467−foreign import ccall "wrapper"
468− mkJSObjectCallAsFunctionCallback :: JSObjectCallAsFunctionCallback' -> IO JSObjectCallAsFunctionCallback
469−#endif
470−
471487#ifdef ghcjs_HOST_OS
472488type HaskellCallback = Callback (JSVal -> JSVal -> IO ())
473489#else
@@ -557,9 +573,11 @@ makeArray args exceptions = do
557573-- | Make an JavaScript array from a list of values
558574--
559575-- >>> testJSaddle $ eval "['Hello', 'World'][1]"
576+-- World
560577-- >>> testJSaddle $ array ["Hello", "World"] !! 1
561578-- World
562579-- >>> testJSaddle $ eval "['Hello', null, undefined, true, 1]"
580+-- Hello,,,true,1
563581-- >>> testJSaddle $ array ("Hello", JSNull, (), True, 1.0::Double)
564582-- Hello,,,true,1
565583array :: MakeArgs args => args -> JSM Object
@@ -634,7 +652,7 @@ propertyNamesCount names = liftIO $ jspropertynamearraygetcount names
634652
635653-- | Get a name out of a property name array
636654propertyNamesAt :: MonadIO m => JSPropertyNameArray -> CSize -> m JSString
637−propertyNamesAt names index = liftIO $ jspropertynamearraygetnameatindex names index >>= wrapJSString
655+propertyNamesAt names index = liftIO $ jspropertynamearraygetnameatindex names index >>= makeNewJSString
638656{-# INLINE propertyNamesAt #-}
639657
640658-- | Convert property array to a list