@@ -5,7 +5,7 @@
5 , ScopedTypeVariables #-}
5 , ScopedTypeVariables #-}
6-----------------------------------------------------------------------------
6-----------------------------------------------------------------------------
8−-- Module : Graphics.UI.Gtk.WebKit.JavaScriptCore.JSC.Object
8+-- Module : Language.Javascript.JSC.Object
9-- Copyright : (c) Hamish Mackenzie
9-- Copyright : (c) Hamish Mackenzie
10-- License : MIT
10-- License : MIT
@@ -20,28 +20,32 @@ module Language.Javascript.JSC.Object (
20 , objGetProperty
20 , objGetProperty
21 , objCallAsFunction
21 , objCallAsFunction
26 , function
28 , function
34+import Prelude hiding ((!!))
32import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSBase
35import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSBase
33 (JSStringRef, JSObjectRef, JSValueRefRef, JSValueRef, JSContextRef)
36 (JSStringRef, JSObjectRef, JSValueRefRef, JSValueRef, JSContextRef)
34−import Foreign.C (CULong(..))
37+import Foreign.C (CUInt, CULong(..))
35import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
38import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
39+ (jsobjectsetpropertyatindex, jsobjectgetpropertyatindex,
36− (jsobjectcallasconstructor, jsobjectmakearray,
40+ jsobjectcallasconstructor, jsobjectmakearray,
37 jsobjectcallasfunction, jsobjectgetproperty, jsobjectsetproperty,
41 jsobjectcallasfunction, jsobjectgetproperty, jsobjectsetproperty,
38 JSPropertyAttributes, JSObjectCallAsFunctionCallback,
42 JSPropertyAttributes, JSObjectCallAsFunctionCallback,
39 jsobjectmakefunctionwithcallback)
43 jsobjectmakefunctionwithcallback)
40import Language.Javascript.JSC.Value
44import Language.Javascript.JSC.Value
41 (valMakeUndefined, valToObject, MakeValueRef(..),
45 (valMakeUndefined, valToObject, MakeValueRef(..),
42− MakeStringRef(..), MakeArgRefs(..))
46+ MakeStringRef(..), MakeArgRefs(..), rethrow)
43import Language.Javascript.JSC.Monad
47import Language.Javascript.JSC.Monad
44− (rethrow, JSC)
48+ (JSC)
45import Control.Monad.Trans.Reader (runReaderT, ask)
49import Control.Monad.Trans.Reader (runReaderT, ask)
46import Control.Monad.IO.Class (MonadIO(..))
50import Control.Monad.IO.Class (MonadIO(..))
47import Foreign (peekArray, nullPtr, withArrayLen)
51import Foreign (peekArray, nullPtr, withArrayLen)
@@ -66,28 +70,26 @@ instance MakeObjectRef v => MakeObjectRef (JSC v) where
67---- Property References ----
71---- Property References ----
68data JSPropRef = JSPropRef JSObjectRef JSStringRef
72data JSPropRef = JSPropRef JSObjectRef JSStringRef
73+ | JSPropIndexRef JSObjectRef CUInt
70class MakePropRef this where
75class MakePropRef this where
71 makePropRef :: this -> JSC JSPropRef
76 makePropRef :: this -> JSC JSPropRef
73instance MakePropRef JSPropRef where
78instance MakePropRef JSPropRef where
74− makePropRef (JSPropRef this name) = do
79+ makePropRef = return
75− rthis <- makeObjectRef this
76− rname <- makeStringRef name
77− return (JSPropRef rthis rname)
79instance MakePropRef prop => MakePropRef (JSC prop) where
81instance MakePropRef prop => MakePropRef (JSC prop) where
80− makePropRef prop = prop >>= makePropRef
82+ makePropRef prop = prop >>= makePropRef
82instance MakeObjectRef JSPropRef where
84instance MakeObjectRef JSPropRef where
83− makeObjectRef (JSPropRef this name) = rethrow $ objGetProperty this name
85+ makeObjectRef = objGetProperty
85instance MakeValueRef JSPropRef where
87instance MakeValueRef JSPropRef where
86− makeValueRef (JSPropRef this name) = rethrow $ objGetProperty this name
88+ makeValueRef = objGetProperty
88instance MakeArgRefs JSPropRef where
90instance MakeArgRefs JSPropRef where
89− makeArgRefs (JSPropRef this name) = do
91+ makeArgRefs p = do
90− rarg <- rethrow $ objGetProperty this name
92+ rarg <- objGetProperty p
91 return [rarg]
93 return [rarg]
93---- String global scope ----
95---- String global scope ----
@@ -101,34 +103,74 @@ instance MakeObjectRef String where
101 makeObjectRef name = do
103 makeObjectRef name = do
102 this <- global
104 this <- global
103 rname <- makeStringRef name
105 rname <- makeStringRef name
104− (rethrow $ objGetProperty this rname) >>= valToObject
106+ (rethrow $ objGetPropertyByName this rname) >>= valToObject
106global :: JSC JSObjectRef
108global :: JSC JSObjectRef
107global = ask >>= (liftIO . jscontextgetglobalobject)
109global = ask >>= (liftIO . jscontextgetglobalobject)
109−objSetProperty :: (MakeStringRef name, MakeValueRef val)
111+objSetPropertyByName :: (MakeStringRef name, MakeValueRef val)
110− => JSObjectRef
112+ => JSObjectRef
113− -> JSPropertyAttributes
115+ -> JSPropertyAttributes
114− -> JSValueRefRef
116+ -> JSValueRefRef
115− -> JSC ()
117+ -> JSC ()
116−objSetProperty this name val attributes exceptions = do
118+objSetPropertyByName this name val attributes exceptions = do
117 gctxt <- ask
119 gctxt <- ask
118 sName <- makeStringRef name
120 sName <- makeStringRef name
119 vref <- makeValueRef val
121 vref <- makeValueRef val
120 liftIO $ jsobjectsetproperty gctxt this sName vref attributes exceptions
122 liftIO $ jsobjectsetproperty gctxt this sName vref attributes exceptions
124+objSetPropertyAtIndex :: (MakeValueRef val)
130+objSetPropertyAtIndex this index val exceptions = do
132+ vref <- makeValueRef val
133+ liftIO $ jsobjectsetpropertyatindex gctxt this index vref exceptions
135+objSetProperty :: (MakeValueRef val)
139+objSetProperty (JSPropRef this name ) val = rethrow $ objSetPropertyByName this name val 0
140+objSetProperty (JSPropIndexRef this index) val = rethrow $ objSetPropertyAtIndex this index val
122−objGetProperty :: MakeStringRef name
142+objGetPropertyByName :: MakeStringRef name
123− => JSObjectRef
143+ => JSObjectRef
125− -> JSValueRefRef
145+ -> JSValueRefRef
126− -> JSC JSValueRef
146+ -> JSC JSValueRef
127−objGetProperty this name exceptions = do
147+objGetPropertyByName this name exceptions = do
128 gctxt <- ask
148 gctxt <- ask
129 sName <- makeStringRef name
149 sName <- makeStringRef name
130 liftIO $ jsobjectgetproperty gctxt this sName exceptions
150 liftIO $ jsobjectgetproperty gctxt this sName exceptions
152+objGetPropertyAtIndex :: JSObjectRef
156+objGetPropertyAtIndex this index exceptions = do
158+ liftIO $ jsobjectgetpropertyatindex gctxt this index exceptions
160+objGetProperty :: JSPropRef
162+objGetProperty (JSPropRef this name ) = rethrow $ objGetPropertyByName this name
163+objGetProperty (JSPropIndexRef this index) = rethrow $ objGetPropertyAtIndex this index
165+objGetProperty' :: JSPropRef
166+ -> JSC (JSObjectRef, JSValueRef)
167+objGetProperty' (JSPropRef this name) = do
168+ p <- rethrow $ objGetPropertyByName this name
170+objGetProperty' (JSPropIndexRef this index) = do
171+ p <- rethrow $ objGetPropertyAtIndex this index
132objCallAsFunction :: MakeArgRefs args
174objCallAsFunction :: MakeArgRefs args
133 => JSObjectRef
175 => JSObjectRef
134 -> JSObjectRef
176 -> JSObjectRef
@@ -158,18 +200,24 @@ this ! name = do
158 rname <- makeStringRef name
200 rname <- makeStringRef name
159 return (JSPropRef rthis rname)
201 return (JSPropRef rthis rname)
203+(!!) :: (MakeObjectRef this) => this -> CUInt -> JSC JSPropRef
205+ rthis <- makeObjectRef this
206+ return (JSPropIndexRef rthis index)
161(#) :: (MakePropRef prop, MakeArgRefs args)
208(#) :: (MakePropRef prop, MakeArgRefs args)
162 => prop -> args -> JSC JSValueRef
209 => prop -> args -> JSC JSValueRef
163prop # args = do
210prop # args = do
164− (JSPropRef this name) <- makePropRef prop
211+ rprop <- makePropRef prop
165− f <- (rethrow $ objGetProperty this name) >>= valToObject
212+ (this, f) <- objGetProperty' rprop
166 rethrow $ objCallAsFunction f this args
213 rethrow $ objCallAsFunction f this args
168(<#) :: (MakePropRef prop, MakeValueRef val)
215(<#) :: (MakePropRef prop, MakeValueRef val)
169− => prop -> val -> JSC ()
216+ => prop -> val -> JSC JSPropRef
170prop <# val = do
217prop <# val = do
171− (JSPropRef this name) <- makePropRef prop
218+ p <- makePropRef prop
172− rethrow $ objSetProperty this name val 0
219+ objSetProperty p val
174new :: (MakeObjectRef constructor, MakeArgRefs args)
222new :: (MakeObjectRef constructor, MakeArgRefs args)
175 => constructor -> args -> JSC JSValueRef
223 => constructor -> args -> JSC JSValueRef
@@ -177,6 +225,13 @@ new constructor args = do
177 f <- makeObjectRef constructor
225 f <- makeObjectRef constructor
178 rethrow $ objCallAsConstructor f args
226 rethrow $ objCallAsConstructor f args
228+call :: (MakeObjectRef function, MakeObjectRef this, MakeArgRefs args)
229+ => function -> this -> args -> JSC JSValueRef
230+call function this args = do
231+ rfunction <- makeObjectRef function
232+ rthis <- makeObjectRef this
233+ rethrow $ objCallAsFunction rfunction rthis args
180type JSObjectCallAsFunctionCallback' =
235type JSObjectCallAsFunctionCallback' =
181 JSContextRef
236 JSContextRef
182 -> JSValueRef
237 -> JSValueRef
@@ -228,7 +283,7 @@ makeArray args exceptions = do
228 liftIO $ withArrayLen rargs $ \ len ptr ->
283 liftIO $ withArrayLen rargs $ \ len ptr ->
229 jsobjectmakearray gctxt (fromIntegral len) ptr exceptions
284 jsobjectmakearray gctxt (fromIntegral len) ptr exceptions
231−array :: MakeArgRefs args => args -> JSC JSValueRef
286+array :: MakeArgRefs args => args -> JSC JSObjectRef
232array = rethrow . makeArray
287array = rethrow . makeArray
@@ -250,6 +305,5 @@ array = rethrow . makeArray