@@ -20,8 +20,8 @@
20-----------------------------------------------------------------------------
20-----------------------------------------------------------------------------
22module Language.Javascript.JSaddle.Object (
22module Language.Javascript.JSaddle.Object (
23− JSObjectRef
23+ Object
24− , MakeObjectRef(..)
24+ , MakeObject(..)
26 -- * Property lookup
26 -- * Property lookup
@@ -76,21 +76,20 @@ module Language.Javascript.JSaddle.Object (
77import Prelude hiding ((!!))
77import Prelude hiding ((!!))
78import Language.Javascript.JSaddle.Types
78import Language.Javascript.JSaddle.Types
79− (JSPropertyNameArrayRef, JSStringRef, JSObjectRef, JSValueRefRef,
79+ (JSPropertyNameArrayRef, JSStringRef, Object(..), JSValueRefRef,
80− JSValueRef, JSContextRef, Index, castRef)
80+ JSValueRef, JSContextRef, Index)
81import Foreign.C.Types (CSize(..), CULong(..), CUInt(..), CULLong(..))
81import Foreign.C.Types (CSize(..), CULong(..), CUInt(..), CULLong(..))
82#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
82#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
83−import GHCJS.Types (nullRef)
83+import GHCJS.Types (nullRef, jsval)
84−import qualified JavaScript.Object.Internal as Object
85−import JavaScript.Object.Internal (Object(Object))
86−import GHCJS.Foreign.Callback (syncCallback2, OnBlocked(ThrowWouldBlock))
84+import GHCJS.Foreign.Callback (syncCallback2, OnBlocked(..), Callback)
87−--import GHCJS.Foreign (newObj, toArray, fromArray, syncCallback2, ForeignRetention(..))
85+import GHCJS.Marshal.Pure (pFromJSVal)
86+import JavaScript.Array (JSArray)
87+import qualified JavaScript.Array as Array (toListIO, fromListIO)
88+import JavaScript.Array.Internal (SomeJSArray(..))
89+import qualified JavaScript.Object as Object (create)
88import Control.Monad (liftM)
90import Control.Monad (liftM)
89import Control.Applicative ((<$>))
91import Control.Applicative ((<$>))
90−import qualified JavaScript.Array as Array
92+import Data.Coerce (coerce)
91−import JavaScript.Array (JSArray)
92−import GHCJS.Prim (JSRef)
93−import Data.JSString (JSString)
95import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
94import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
96 (jsobjectmake, jspropertynamearraygetnameatindex,
95 (jsobjectmake, jspropertynamearraygetnameatindex,
@@ -112,7 +111,7 @@ import Language.Javascript.JSaddle.Value
112import Language.Javascript.JSaddle.PropRef (JSPropRef(..))
111import Language.Javascript.JSaddle.PropRef (JSPropRef(..))
113import Language.Javascript.JSaddle.Classes
112import Language.Javascript.JSaddle.Classes
114 (MakeValueRef(..), MakeStringRef(..), MakeArgRefs(..), MakePropRef(..),
113 (MakeValueRef(..), MakeStringRef(..), MakeArgRefs(..), MakePropRef(..),
115− MakeObjectRef(..))
114+ MakeObject(..))
116import Language.Javascript.JSaddle.Monad
115import Language.Javascript.JSaddle.Monad
118import Control.Monad.Trans.Reader (runReaderT, ask)
117import Control.Monad.Trans.Reader (runReaderT, ask)
@@ -126,23 +125,16 @@ import Control.Lens
126 (IndexPreservingGetter, to, (^.))
125 (IndexPreservingGetter, to, (^.))
127import Language.Javascript.JSaddle.String (textToStr)
126import Language.Javascript.JSaddle.String (textToStr)
129−objectToRef (Object r) = r
128+-- | If we already have a Object we are fine
130−
129+instance MakeObject Object where
131−foreign import javascript unsafe
130+ makeObject = return
132− "$r = $1" jsRefToJSSTring :: JSRef -> JSString
131+ {-# INLINE makeObject #-}
134−foreign import javascript unsafe
135− "$r = $1" getJSArrayRef :: JSArray -> JSRef
138−-- | If we already have a JSObjectRef we are fine
139−instance MakeObjectRef JSObjectRef where
140− makeObjectRef = return
142−-- | JSObjectRef can be made by evaluating a function in 'JSM' as long
133+-- | Object can be made by evaluating a function in 'JSM' as long
143−-- as it returns something we can make into a JSObjectRef.
134+-- as it returns something we can make into a Object.
144−instance MakeObjectRef v => MakeObjectRef (JSM v) where
135+instance MakeObject v => MakeObject (JSM v) where
145− makeObjectRef v = v >>= makeObjectRef
136+ makeObject v = v >>= makeObject
137+ {-# INLINE makeObject #-}
147-- | Lookup a property based on its name. This function just constructs a JSPropRef
139-- | Lookup a property based on its name. This function just constructs a JSPropRef
148-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
140-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
@@ -151,15 +143,16 @@ instance MakeObjectRef v => MakeObjectRef (JSM v) where
151-- >>> testJSaddle $ eval "'Hello World'.length"
143-- >>> testJSaddle $ eval "'Hello World'.length"
152-- >>> testJSaddle $ val "Hello World" ! "length"
144-- >>> testJSaddle $ val "Hello World" ! "length"
154−(!) :: (MakeObjectRef this, MakeStringRef name)
146+(!) :: (MakeObject this, MakeStringRef name)
155 => this -- ^ Object to look on
147 => this -- ^ Object to look on
156 -> name -- ^ Name of the property to find
148 -> name -- ^ Name of the property to find
157 -> JSM JSPropRef -- ^ Property reference
149 -> JSM JSPropRef -- ^ Property reference
158this ! name = do
150this ! name = do
159− rthis <- makeObjectRef this
151+ rthis <- makeObject this
160 return (JSPropRef rthis rname)
152 return (JSPropRef rthis rname)
162 rname = makeStringRef name
154 rname = makeStringRef name
164-- | Lookup a property based on its index. This function just constructs a JSPropRef
157-- | Lookup a property based on its index. This function just constructs a JSPropRef
165-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
158-- the lookup is delayed until we use the JSPropRef. This makes it a bit lazy compared
@@ -168,13 +161,14 @@ this ! name = do
168-- >>> testJSaddle $ eval "'Hello World'[6]"
161-- >>> testJSaddle $ eval "'Hello World'[6]"
169-- >>> testJSaddle $ val "Hello World" !! 6
162-- >>> testJSaddle $ val "Hello World" !! 6
171−(!!) :: (MakeObjectRef this)
164+(!!) :: (MakeObject this)
172 => this -- ^ Object to look on
165 => this -- ^ Object to look on
173 -> Index -- ^ Index of the property to lookup
166 -> Index -- ^ Index of the property to lookup
174 -> JSM JSPropRef -- ^ Property reference
167 -> JSM JSPropRef -- ^ Property reference
175this !! index = do
168this !! index = do
176− rthis <- makeObjectRef this
169+ rthis <- makeObject this
177 return (JSPropIndexRef rthis index)
170 return (JSPropIndexRef rthis index)
179-- | Makes a getter for a particular property name.
173-- | Makes a getter for a particular property name.
@@ -183,13 +177,14 @@ this !! index = do
183-- >>> testJSaddle $ eval "'Hello World'.length"
177-- >>> testJSaddle $ eval "'Hello World'.length"
184-- >>> testJSaddle $ val "Hello World" ^. js "length"
178-- >>> testJSaddle $ val "Hello World" ^. js "length"
186−js :: (MakeObjectRef s, MakeStringRef name)
180+js :: (MakeObject s, MakeStringRef name)
187 => name -- ^ Name of the property to find
181 => name -- ^ Name of the property to find
188 -> IndexPreservingGetter s (JSM JSPropRef)
182 -> IndexPreservingGetter s (JSM JSPropRef)
189js name = to (!name)
183js name = to (!name)
191-- | Java script function applications have this type
186-- | Java script function applications have this type
192−type JSF = MakeObjectRef o => IndexPreservingGetter o (JSM JSValueRef)
187+type JSF = forall o . MakeObject o => IndexPreservingGetter o (JSM JSValueRef)
194-- | Handy way to call a function
189-- | Handy way to call a function
@@ -201,6 +196,7 @@ jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSF
201jsf name args = function . to (# args)
196jsf name args = function . to (# args)
203 function = js name
198 function = js name
205-- | Handy way to call a function that expects no arguments
201-- | Handy way to call a function that expects no arguments
@@ -210,6 +206,7 @@ jsf name args = function . to (# args)
210-- hello world
206-- hello world
211js0 :: (MakeStringRef name) => name -> JSF
207js0 :: (MakeStringRef name) => name -> JSF
212js0 name = jsf name ()
208js0 name = jsf name ()
214-- | Handy way to call a function that expects one argument
211-- | Handy way to call a function that expects one argument
@@ -219,27 +216,32 @@ js0 name = jsf name ()
220js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSF
217js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSF
221js1 name a0 = jsf name [a0]
218js1 name a0 = jsf name [a0]
223-- | Handy way to call a function that expects two arguments
221-- | Handy way to call a function that expects two arguments
224js2 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1) => name -> a0 -> a1 -> JSF
222js2 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1) => name -> a0 -> a1 -> JSF
225js2 name a0 a1 = jsf name (a0, a1)
223js2 name a0 a1 = jsf name (a0, a1)
227-- | Handy way to call a function that expects three arguments
226-- | Handy way to call a function that expects three arguments
228js3 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2)
227js3 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2)
229 => name -> a0 -> a1 -> a2 -> JSF
228 => name -> a0 -> a1 -> a2 -> JSF
230js3 name a0 a1 a2 = jsf name (a0, a1, a2)
229js3 name a0 a1 a2 = jsf name (a0, a1, a2)
232-- | Handy way to call a function that expects four arguments
232-- | Handy way to call a function that expects four arguments
233js4 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2,
233js4 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2,
234 MakeValueRef a3)
234 MakeValueRef a3)
235 => name -> a0 -> a1 -> a2 -> a3 -> JSF
235 => name -> a0 -> a1 -> a2 -> a3 -> JSF
236js4 name a0 a1 a2 a3 = jsf name (a0, a1, a2, a3)
236js4 name a0 a1 a2 a3 = jsf name (a0, a1, a2, a3)
238-- | Handy way to call a function that expects five arguments
239-- | Handy way to call a function that expects five arguments
239js5 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2,
240js5 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2,
240 MakeValueRef a3, MakeValueRef a4)
241 MakeValueRef a3, MakeValueRef a4)
241 => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF
242 => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF
242js5 name a0 a1 a2 a3 a4 = jsf name (a0, a1, a2, a3, a4)
243js5 name a0 a1 a2 a3 a4 = jsf name (a0, a1, a2, a3, a4)
245-- | Handy way to get and hold onto a reference top level javascript
247-- | Handy way to get and hold onto a reference top level javascript
@@ -249,6 +251,7 @@ js5 name a0 a1 a2 a3 a4 = jsf name (a0, a1, a2, a3, a4)
250jsg :: MakeStringRef a => a -> JSM JSPropRef
252jsg :: MakeStringRef a => a -> JSM JSPropRef
251jsg name = global ! name
253jsg name = global ! name
253-- | Call a JavaScript function
256-- | Call a JavaScript function
@@ -261,7 +264,9 @@ infixr 2 #
261prop # args = do
264prop # args = do
262 rprop <- makePropRef prop
265 rprop <- makePropRef prop
263 (this, f) <- objGetProperty' rprop
266 (this, f) <- objGetProperty' rprop
264− rethrow $ objCallAsFunction (castRef f) this args
268+ rethrow $ objCallAsFunction f' this args
266-- | Call a JavaScript function
271-- | Call a JavaScript function
@@ -277,6 +282,7 @@ prop <# val = do
277 p <- makePropRef prop
282 p <- makePropRef prop
278 objSetProperty p val
283 objSetProperty p val
281-- | Use this to create a new JavaScript object
287-- | Use this to create a new JavaScript object
@@ -285,39 +291,42 @@ prop <# val = do
286-- >>> testJSaddle $ new "Date" (2013, 1, 1)
292-- >>> testJSaddle $ new "Date" (2013, 1, 1)
287-- Fri Feb 01 2013 00:00:00 GMT+1300 (NZDT)
293-- Fri Feb 01 2013 00:00:00 GMT+1300 (NZDT)
288−new :: (MakeObjectRef constructor, MakeArgRefs args)
294+new :: (MakeObject constructor, MakeArgRefs args)
289 => constructor
295 => constructor
291 -> JSM JSValueRef
297 -> JSM JSValueRef
292new constructor args = do
298new constructor args = do
293− f <- makeObjectRef constructor
299+ f <- makeObject constructor
294 rethrow $ objCallAsConstructor f args
300 rethrow $ objCallAsConstructor f args
296-- | Call function with a given @this@. In most cases you should use '#'.
303-- | Call function with a given @this@. In most cases you should use '#'.
298-- >>> testJSaddle $ eval "(function(){return this;}).apply('Hello', [])"
305-- >>> testJSaddle $ eval "(function(){return this;}).apply('Hello', [])"
299-- >>> testJSaddle $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
306-- >>> testJSaddle $ do { test <- eval "(function(){return this;})"; call test (val "Hello") () }
301−call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args)
308+call :: (MakeObject function, MakeObject this, MakeArgRefs args)
302 => function -> this -> args -> JSM JSValueRef
309 => function -> this -> args -> JSM JSValueRef
303call function this args = do
310call function this args = do
304− rfunction <- makeObjectRef function
311+ rfunction <- makeObject function
305− rthis <- makeObjectRef this
312+ rthis <- makeObject this
306 rethrow $ objCallAsFunction rfunction rthis args
313 rethrow $ objCallAsFunction rfunction rthis args
308-- | Make an empty object using the default constuctor
316-- | Make an empty object using the default constuctor
310-- >>> testJSaddle $ eval "var a = {}; a.x = 'Hello'; a.x"
318-- >>> testJSaddle $ eval "var a = {}; a.x = 'Hello'; a.x"
311-- >>> testJSaddle $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
319-- >>> testJSaddle $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
313−obj :: JSM JSObjectRef
321+obj :: JSM Object
314#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
322#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
315−obj = liftIO (castRef . objectToRef <$> Object.create)
323+obj = liftIO Object.create
318 gctxt <- ask
326 gctxt <- ask
319− liftIO $ jsobjectmake gctxt nullPtr nullPtr
327+ liftIO $ Object <$> jsobjectmake gctxt nullPtr nullPtr
322-- | Type used for Haskell functions called from JavaScript.
331-- | Type used for Haskell functions called from JavaScript.
323type JSCallAsFunction = JSValueRef -- ^ Function object
332type JSCallAsFunction = JSValueRef -- ^ Function object
@@ -338,81 +347,79 @@ type JSCallAsFunction = JSValueRef -- ^ Function object
338-- undefined
347-- undefined
339fun :: JSCallAsFunction -> JSCallAsFunction
348fun :: JSCallAsFunction -> JSCallAsFunction
342#if (!defined(ghcjs_HOST_OS) || !defined(USE_JAVASCRIPTFFI)) && defined(USE_WEBKIT)
352#if (!defined(ghcjs_HOST_OS) || !defined(USE_JAVASCRIPTFFI)) && defined(USE_WEBKIT)
343foreign import ccall "wrapper"
353foreign import ccall "wrapper"
344 mkJSObjectCallAsFunctionCallback :: JSObjectCallAsFunctionCallback' -> IO JSObjectCallAsFunctionCallback
354 mkJSObjectCallAsFunctionCallback :: JSObjectCallAsFunctionCallback' -> IO JSObjectCallAsFunctionCallback
347−fromArray :: JSArray -> IO [JSStringRef]
348−fromArray a = fmap jsRefToJSSTring <$> Array.toListIO a
349−toArray :: [JSValueRef] -> IO JSArray
350−toArray a = Array.fromListIO (fmap castRef a)
352-- ^ Make a JavaScript function object that wraps a Haskell function.
357-- ^ Make a JavaScript function object that wraps a Haskell function.
353function :: MakeStringRef name
358function :: MakeStringRef name
354 => name -- ^ Name of the function
359 => name -- ^ Name of the function
355 -> JSCallAsFunction -- ^ Haskell function to call
360 -> JSCallAsFunction -- ^ Haskell function to call
356− -> JSM JSObjectRef -- ^ Returns a JavaScript function object that will
361+ -> JSM Object -- ^ Returns a JavaScript function object that will
357 -- call the Haskell one when it is called
362 -- call the Haskell one when it is called
358−-- #if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
363+#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
359−-- function name f = liftIO $ do
364+function name f = liftIO $ do
360−-- callback <- syncCallback2 ThrowWouldBlock True $ \this args -> do
365+ callback <- syncCallback2 ContinueAsync $ \this args -> do
361−-- rargs <- fromArray args
366+ rargs <- Array.toListIO (coerce args)
362−-- runReaderT (f this this rargs) () -- TODO pass function object through
367+ runReaderT (f this this rargs) () -- TODO pass function object through
363−-- makeFunctionWithCallback (makeStringRef name) callback
368+ makeFunctionWithCallback (makeStringRef name) callback
364−-- foreign import javascript unsafe "$r = function () { $2(this, arguments); }"
369+foreign import javascript unsafe "$r = function () { $2(this, arguments); }"
365−-- makeFunctionWithCallback :: JSStringRef -> JSFun (JSValueRef -> JSValueRefRef -> IO ()) -> IO JSObjectRef
370+ makeFunctionWithCallback :: JSStringRef -> Callback (JSValueRef -> JSValueRef -> IO ()) -> IO Object
366−-- #elif defined(USE_WEBKIT)
371+#elif defined(USE_WEBKIT)
367−-- function name f = do
372+function name f = do
368−-- gctxt <- ask
373+ gctxt <- ask
369−-- callback <- liftIO $ mkJSObjectCallAsFunctionCallback wrap
374+ callback <- liftIO $ mkJSObjectCallAsFunctionCallback wrap
370−-- liftIO $ jsobjectmakefunctionwithcallback gctxt (makeStringRef name) callback
375+ liftIO $ Object <$> jsobjectmakefunctionwithcallback gctxt (makeStringRef name) callback
372−-- wrap ctx fobj this argc argv exception = do
377+ wrap ctx fobj this argc argv exception = do
373−-- args <- peekArray (fromIntegral argc) argv
378+ args <- peekArray (fromIntegral argc) argv
374−-- (`runReaderT` ctx) $
379+ (`runReaderT` ctx) $
375−-- f fobj this args >>= makeValueRef
380+ f fobj this args >>= makeValueRef
376−-- `E.catch` \(e :: SomeException) -> do
381+ `E.catch` \(e :: SomeException) -> do
377−-- str <- runReaderT (makeValueRef . T.pack $ show e) ctx
382+ str <- runReaderT (makeValueRef . T.pack $ show e) ctx
378−-- poke exception str
383+ poke exception str
379−-- jsvaluemakeundefined ctx
384+ jsvaluemakeundefined ctx
381−-- function = undefined
386+function = undefined
386-- | A callback to Haskell can be used as a JavaScript value. This will create
389-- | A callback to Haskell can be used as a JavaScript value. This will create
387-- an anonymous JavaScript function object. Use 'function' to create one with
390-- an anonymous JavaScript function object. Use 'function' to create one with
388-- a name.
391-- a name.
389instance MakeValueRef JSCallAsFunction where
392instance MakeValueRef JSCallAsFunction where
390#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
393#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
391− makeValueRef a = castRef <$> function (jsRefToJSSTring nullRef :: JSStringRef) a
394+ makeValueRef f = jsval <$> function (pFromJSVal nullRef :: JSStringRef) f
393− makeValueRef a = castRef <$> function (jsRefToJSSTring nullPtr :: JSStringRef) a
396+ makeValueRef f = function (nullPtr :: JSStringRef) f >>= makeValueRef
398+ {-# INLINE makeValueRef #-}
396instance MakeArgRefs JSCallAsFunction where
400instance MakeArgRefs JSCallAsFunction where
397 makeArgRefs f = do
401 makeArgRefs f = do
398#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
402#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
399− rarg <- function (jsRefToJSSTring nullRef :: JSStringRef) f
403+ rarg <- jsval <$> function (pFromJSVal nullRef :: JSStringRef) f
401− rarg <- function (jsRefToJSSTring nullPtr :: JSStringRef) f
405+ rarg <- function (nullPtr:: JSStringRef) f >>= makeValueRef
403− return [castRef rarg]
407+ return [rarg]
408+ {-# INLINE makeArgRefs #-}
405−makeArray :: MakeArgRefs args => args -> JSValueRefRef -> JSM JSObjectRef
410+makeArray :: MakeArgRefs args => args -> JSValueRefRef -> JSM Object
406#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
411#if (defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)) || !defined(USE_WEBKIT)
407−--makeArray args exceptions = makeArgRefs args >>= liftIO . toArray
412+makeArray args exceptions = do
408−makeArray args exceptions = makeArgRefs args >>= liftM (castRef . getJSArrayRef) . liftIO . toArray
413+ rargs <- makeArgRefs args
414+ liftIO $ Object . jsval <$> Array.fromListIO rargs
410makeArray args exceptions = do
416makeArray args exceptions = do
411 gctxt <- ask
417 gctxt <- ask
412 rargs <- makeArgRefs args
418 rargs <- makeArgRefs args
413 liftIO $ withArrayLen rargs $ \ len ptr ->
419 liftIO $ withArrayLen rargs $ \ len ptr ->
414− jsobjectmakearray gctxt (fromIntegral len) ptr exceptions
420+ Object <$> jsobjectmakearray gctxt (fromIntegral len) ptr exceptions
422+{-# INLINE makeArray #-}
417-- | Make an JavaScript array from a list of values
424-- | Make an JavaScript array from a list of values
@@ -422,79 +429,89 @@ makeArray args exceptions = do
422-- >>> testJSaddle $ eval "['Hello', null, undefined, true, 1]"
429-- >>> testJSaddle $ eval "['Hello', null, undefined, true, 1]"
423-- >>> testJSaddle $ array ("Hello", JSNull, (), True, 1.0::Double)
430-- >>> testJSaddle $ array ("Hello", JSNull, (), True, 1.0::Double)
424-- Hello,,,true,1
431-- Hello,,,true,1
425−array :: MakeArgRefs args => args -> JSM JSObjectRef
432+array :: MakeArgRefs args => args -> JSM Object
426array = rethrow . makeArray
433array = rethrow . makeArray
428-- | JavaScript's global object
435-- | JavaScript's global object
429−global :: JSM JSObjectRef
436+global :: JSM Object
430#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
437#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
431global = liftIO js_window
438global = liftIO js_window
439+{-# INLINE global #-}
432foreign import javascript unsafe "$r = window"
440foreign import javascript unsafe "$r = window"
433− js_window :: IO JSObjectRef
441+ js_window :: IO Object
434#elif defined(USE_WEBKIT)
442#elif defined(USE_WEBKIT)
435−global = ask >>= (liftIO . jscontextgetglobalobject)
443+global = ask >>= (liftIO . fmap Object . jscontextgetglobalobject)
444+{-# INLINE global #-}
437global = undefined
446global = undefined
440-- | Get an array containing the property names present on a given object
449-- | Get an array containing the property names present on a given object
441#if (!defined(ghcjs_HOST_OS) || !defined(USE_JAVASCRIPTFFI)) && defined(USE_WEBKIT)
450#if (!defined(ghcjs_HOST_OS) || !defined(USE_JAVASCRIPTFFI)) && defined(USE_WEBKIT)
442−copyPropertyNames :: MakeObjectRef this => this -> JSM JSPropertyNameArrayRef
451+copyPropertyNames :: MakeObject this => this -> JSM JSPropertyNameArrayRef
443copyPropertyNames this = do
452copyPropertyNames this = do
444 gctxt <- ask
453 gctxt <- ask
445− rthis <- makeObjectRef this
454+ Object rthis <- makeObject this
446 liftIO $ jsobjectcopypropertynames gctxt rthis
455 liftIO $ jsobjectcopypropertynames gctxt rthis
456+{-# INLINE copyPropertyNames #-}
448-- | Get the number of names in a property name array
458-- | Get the number of names in a property name array
449propertyNamesCount :: MonadIO m => JSPropertyNameArrayRef -> m CSize
459propertyNamesCount :: MonadIO m => JSPropertyNameArrayRef -> m CSize
450propertyNamesCount names = liftIO $ jspropertynamearraygetcount names
460propertyNamesCount names = liftIO $ jspropertynamearraygetcount names
461+{-# INLINE propertyNamesCount #-}
452-- | Get a name out of a property name array
463-- | Get a name out of a property name array
453propertyNamesAt :: MonadIO m => JSPropertyNameArrayRef -> CSize -> m JSStringRef
464propertyNamesAt :: MonadIO m => JSPropertyNameArrayRef -> CSize -> m JSStringRef
454propertyNamesAt names index = liftIO $ jspropertynamearraygetnameatindex names index
465propertyNamesAt names index = liftIO $ jspropertynamearraygetnameatindex names index
466+{-# INLINE propertyNamesAt #-}
456-- | Convert property array to a list
468-- | Convert property array to a list
457propertyNamesList :: MonadIO m => JSPropertyNameArrayRef -> m [JSStringRef]
469propertyNamesList :: MonadIO m => JSPropertyNameArrayRef -> m [JSStringRef]
458propertyNamesList names = do
470propertyNamesList names = do
459 count <- propertyNamesCount names
471 count <- propertyNamesCount names
460 mapM (propertyNamesAt names) $ enumFromTo 0 (count - 1)
472 mapM (propertyNamesAt names) $ enumFromTo 0 (count - 1)
473+{-# INLINE propertyNamesList #-}
463-- | Get a list containing the property names present on a given object
476-- | Get a list containing the property names present on a given object
464−propertyNames :: MakeObjectRef this => this -> JSM [JSStringRef]
477+propertyNames :: MakeObject this => this -> JSM [JSStringRef]
465#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
478#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
466−propertyNames this = makeObjectRef this >>= liftIO . js_propertyNames >>= liftIO . fromArray
479+propertyNames this = makeObject this >>= liftIO . js_propertyNames >>= liftIO . (fmap (map pFromJSVal)) . Array.toListIO
480+{-# INLINE propertyNames #-}
467foreign import javascript unsafe "$r = []; h$forIn($1, function(n){$r.push(n);})"
481foreign import javascript unsafe "$r = []; h$forIn($1, function(n){$r.push(n);})"
468− js_propertyNames :: JSObjectRef -> IO JSArray
482+ js_propertyNames :: Object -> IO JSArray
469#elif defined(USE_WEBKIT)
483#elif defined(USE_WEBKIT)
470propertyNames this = copyPropertyNames this >>= propertyNamesList
484propertyNames this = copyPropertyNames this >>= propertyNamesList
485+{-# INLINE propertyNames #-}
472propertyNames = undefined
487propertyNames = undefined
475-- | Get a list containing references to all the properties present on a given object
490-- | Get a list containing references to all the properties present on a given object
476−properties :: MakeObjectRef this => this -> JSM [JSPropRef]
491+properties :: MakeObject this => this -> JSM [JSPropRef]
477properties this = propertyNames this >>= mapM (this !)
492properties this = propertyNames this >>= mapM (this !)
479-- | Call a JavaScript object as function. Consider using '#'.
494-- | Call a JavaScript object as function. Consider using '#'.
480objCallAsFunction :: MakeArgRefs args
495objCallAsFunction :: MakeArgRefs args
481− => JSObjectRef
496+ => Object
482− -> JSObjectRef
497+ -> Object
484 -> JSValueRefRef
499 -> JSValueRefRef
485 -> JSM JSValueRef
500 -> JSM JSValueRef
486#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
501#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
487objCallAsFunction function this args exceptions = do
502objCallAsFunction function this args exceptions = do
488− rargs <- (makeArgRefs args >>= liftIO . toArray) >>= (liftIO . Array.thaw)
503+ rargs <- makeArgRefs args >>= liftIO . Array.fromListIO
489 liftIO $ js_apply function this rargs exceptions
504 liftIO $ js_apply function this rargs exceptions
505+{-# INLINE objCallAsFunction #-}
490foreign import javascript unsafe "try { $r = $1.apply($2, $3) } catch(e) { $4[0] = e }"
506foreign import javascript unsafe "try { $r = $1.apply($2, $3) } catch(e) { $4[0] = e }"
491− js_apply :: JSObjectRef -> JSObjectRef -> JSValueRefRef -> JSValueRefRef -> IO JSValueRef
507+ js_apply :: Object -> Object -> JSValueRefRef -> JSValueRefRef -> IO JSValueRef
492#elif defined(USE_WEBKIT)
508#elif defined(USE_WEBKIT)
493−objCallAsFunction function this args exceptions = do
509+objCallAsFunction (Object function) (Object this) args exceptions = do
494 gctxt <- ask
510 gctxt <- ask
495 rargs <- makeArgRefs args
511 rargs <- makeArgRefs args
496 liftIO $ withArrayLen rargs $ \ largs pargs ->
512 liftIO $ withArrayLen rargs $ \ largs pargs ->
497 jsobjectcallasfunction gctxt function this (fromIntegral largs) pargs exceptions
513 jsobjectcallasfunction gctxt function this (fromIntegral largs) pargs exceptions
514+{-# INLINE objCallAsFunction #-}
499objCallAsFunction = undefined
516objCallAsFunction = undefined
@@ -504,13 +521,13 @@ objCallAsFunction = undefined
504-- If you pass more than 7 arguments to a constructor for a built in
521-- If you pass more than 7 arguments to a constructor for a built in
505-- JavaScript type (like Date) then this function will fail.
522-- JavaScript type (like Date) then this function will fail.
506objCallAsConstructor :: MakeArgRefs args
523objCallAsConstructor :: MakeArgRefs args
507− => JSObjectRef
524+ => Object
509 -> JSValueRefRef
526 -> JSValueRefRef
510 -> JSM JSValueRef
527 -> JSM JSValueRef
511#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
528#if defined(ghcjs_HOST_OS) && defined(USE_JAVASCRIPTFFI)
512objCallAsConstructor function args exceptions = do
529objCallAsConstructor function args exceptions = do
513− rargs <- (makeArgRefs args >>= liftIO . toArray) >>= (liftIO . Array.thaw)
530+ rargs <- makeArgRefs args >>= liftIO . Array.fromListIO
514 liftIO $ js_new function rargs exceptions
531 liftIO $ js_new function rargs exceptions
515foreign import javascript unsafe "\
532foreign import javascript unsafe "\
@@ -539,9 +556,9 @@ foreign import javascript unsafe "\
539 catch(e) {\
556 catch(e) {\
540 $3[0] = e;\
557 $3[0] = e;\
542− js_new :: JSObjectRef -> JSValueRefRef -> JSValueRefRef -> IO JSValueRef
559+ js_new :: Object -> JSValueRefRef -> JSValueRefRef -> IO JSValueRef
543#elif defined(USE_WEBKIT)
560#elif defined(USE_WEBKIT)
544−objCallAsConstructor function args exceptions = do
561+objCallAsConstructor (Object function) args exceptions = do
545 gctxt <- ask
562 gctxt <- ask
546 rargs <- makeArgRefs args
563 rargs <- makeArgRefs args
547 liftIO $ withArrayLen rargs $ \ largs pargs ->
564 liftIO $ withArrayLen rargs $ \ largs pargs ->