@@ -5,7 +5,7 @@
55 , ScopedTypeVariables #-}
66-----------------------------------------------------------------------------
77--
8−-- Module : Graphics.UI.Gtk.WebKit.JavaScriptCore.JSC.Object
8+-- Module : Language.Javascript.JSC.Object
99-- Copyright : (c) Hamish Mackenzie
1010-- License : MIT
1111--
@@ -20,28 +20,32 @@ module Language.Javascript.JSC.Object (
2020 , objGetProperty
2121 , objCallAsFunction
2222 , (!)
23+ , (!!)
2324 , (#)
2425 , (<#)
2526 , new
27+ , call
2628 , function
2729 , fun
2830 , array
2931 , global
3032) where
3133
34+import Prelude hiding ((!!))
3235import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSBase
3336 (JSStringRef, JSObjectRef, JSValueRefRef, JSValueRef, JSContextRef)
34−import Foreign.C (CULong(..))
37+import Foreign.C (CUInt, CULong(..))
3538import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
36− (jsobjectcallasconstructor, jsobjectmakearray,
39+ (jsobjectsetpropertyatindex, jsobjectgetpropertyatindex,
40+ jsobjectcallasconstructor, jsobjectmakearray,
3741 jsobjectcallasfunction, jsobjectgetproperty, jsobjectsetproperty,
3842 JSPropertyAttributes, JSObjectCallAsFunctionCallback,
3943 jsobjectmakefunctionwithcallback)
4044import Language.Javascript.JSC.Value
4145 (valMakeUndefined, valToObject, MakeValueRef(..),
42− MakeStringRef(..), MakeArgRefs(..))
46+ MakeStringRef(..), MakeArgRefs(..), rethrow)
4347import Language.Javascript.JSC.Monad
44− (rethrow, JSC)
48+ (JSC)
4549import Control.Monad.Trans.Reader (runReaderT, ask)
4650import Control.Monad.IO.Class (MonadIO(..))
4751import Foreign (peekArray, nullPtr, withArrayLen)
@@ -66,28 +70,26 @@ instance MakeObjectRef v => MakeObjectRef (JSC v) where
6670
6771---- Property References ----
6872data JSPropRef = JSPropRef JSObjectRef JSStringRef
73+ | JSPropIndexRef JSObjectRef CUInt
6974
7075class MakePropRef this where
7176 makePropRef :: this -> JSC JSPropRef
7277
7378instance MakePropRef JSPropRef where
74− makePropRef (JSPropRef this name) = do
75− rthis <- makeObjectRef this
76− rname <- makeStringRef name
77− return (JSPropRef rthis rname)
79+ makePropRef = return
7880
7981instance MakePropRef prop => MakePropRef (JSC prop) where
80− makePropRef prop = prop >>= makePropRef
82+ makePropRef prop = prop >>= makePropRef
8183
8284instance MakeObjectRef JSPropRef where
83− makeObjectRef (JSPropRef this name) = rethrow $ objGetProperty this name
85+ makeObjectRef = objGetProperty
8486
8587instance MakeValueRef JSPropRef where
86− makeValueRef (JSPropRef this name) = rethrow $ objGetProperty this name
88+ makeValueRef = objGetProperty
8789
8890instance MakeArgRefs JSPropRef where
89− makeArgRefs (JSPropRef this name) = do
90− rarg <- rethrow $ objGetProperty this name
91+ makeArgRefs p = do
92+ rarg <- objGetProperty p
9193 return [rarg]
9294
9395---- String global scope ----
@@ -101,34 +103,74 @@ instance MakeObjectRef String where
101103 makeObjectRef name = do
102104 this <- global
103105 rname <- makeStringRef name
104− (rethrow $ objGetProperty this rname) >>= valToObject
106+ (rethrow $ objGetPropertyByName this rname) >>= valToObject
105107
106108global :: JSC JSObjectRef
107109global = ask >>= (liftIO . jscontextgetglobalobject)
108110
109−objSetProperty :: (MakeStringRef name, MakeValueRef val)
110− => JSObjectRef
111− -> name
112− -> val
113− -> JSPropertyAttributes
114− -> JSValueRefRef
115− -> JSC ()
116−objSetProperty this name val attributes exceptions = do
111+objSetPropertyByName :: (MakeStringRef name, MakeValueRef val)
112+ => JSObjectRef
113+ -> name
114+ -> val
115+ -> JSPropertyAttributes
116+ -> JSValueRefRef
117+ -> JSC ()
118+objSetPropertyByName this name val attributes exceptions = do
117119 gctxt <- ask
118120 sName <- makeStringRef name
119121 vref <- makeValueRef val
120122 liftIO $ jsobjectsetproperty gctxt this sName vref attributes exceptions
121123
122−objGetProperty :: MakeStringRef name
123− => JSObjectRef
124− -> name
125− -> JSValueRefRef
126− -> JSC JSValueRef
127−objGetProperty this name exceptions = do
124+objSetPropertyAtIndex :: (MakeValueRef val)
125+ => JSObjectRef
126+ -> CUInt
127+ -> val
128+ -> JSValueRefRef
129+ -> JSC ()
130+objSetPropertyAtIndex this index val exceptions = do
131+ gctxt <- ask
132+ vref <- makeValueRef val
133+ liftIO $ jsobjectsetpropertyatindex gctxt this index vref exceptions
134+
135+objSetProperty :: (MakeValueRef val)
136+ => JSPropRef
137+ -> val
138+ -> JSC ()
139+objSetProperty (JSPropRef this name ) val = rethrow $ objSetPropertyByName this name val 0
140+objSetProperty (JSPropIndexRef this index) val = rethrow $ objSetPropertyAtIndex this index val
141+
142+objGetPropertyByName :: MakeStringRef name
143+ => JSObjectRef
144+ -> name
145+ -> JSValueRefRef
146+ -> JSC JSValueRef
147+objGetPropertyByName this name exceptions = do
128148 gctxt <- ask
129149 sName <- makeStringRef name
130150 liftIO $ jsobjectgetproperty gctxt this sName exceptions
131151
152+objGetPropertyAtIndex :: JSObjectRef
153+ -> CUInt
154+ -> JSValueRefRef
155+ -> JSC JSValueRef
156+objGetPropertyAtIndex this index exceptions = do
157+ gctxt <- ask
158+ liftIO $ jsobjectgetpropertyatindex gctxt this index exceptions
159+
160+objGetProperty :: JSPropRef
161+ -> JSC JSValueRef
162+objGetProperty (JSPropRef this name ) = rethrow $ objGetPropertyByName this name
163+objGetProperty (JSPropIndexRef this index) = rethrow $ objGetPropertyAtIndex this index
164+
165+objGetProperty' :: JSPropRef
166+ -> JSC (JSObjectRef, JSValueRef)
167+objGetProperty' (JSPropRef this name) = do
168+ p <- rethrow $ objGetPropertyByName this name
169+ return (this, p)
170+objGetProperty' (JSPropIndexRef this index) = do
171+ p <- rethrow $ objGetPropertyAtIndex this index
172+ return (this, p)
173+
132174objCallAsFunction :: MakeArgRefs args
133175 => JSObjectRef
134176 -> JSObjectRef
@@ -158,18 +200,24 @@ this ! name = do
158200 rname <- makeStringRef name
159201 return (JSPropRef rthis rname)
160202
203+(!!) :: (MakeObjectRef this) => this -> CUInt -> JSC JSPropRef
204+this !! index = do
205+ rthis <- makeObjectRef this
206+ return (JSPropIndexRef rthis index)
207+
161208(#) :: (MakePropRef prop, MakeArgRefs args)
162209 => prop -> args -> JSC JSValueRef
163210prop # args = do
164− (JSPropRef this name) <- makePropRef prop
165− f <- (rethrow $ objGetProperty this name) >>= valToObject
211+ rprop <- makePropRef prop
212+ (this, f) <- objGetProperty' rprop
166213 rethrow $ objCallAsFunction f this args
167214
168215(<#) :: (MakePropRef prop, MakeValueRef val)
169− => prop -> val -> JSC ()
216+ => prop -> val -> JSC JSPropRef
170217prop <# val = do
171− (JSPropRef this name) <- makePropRef prop
172− rethrow $ objSetProperty this name val 0
218+ p <- makePropRef prop
219+ objSetProperty p val
220+ return p
173221
174222new :: (MakeObjectRef constructor, MakeArgRefs args)
175223 => constructor -> args -> JSC JSValueRef
@@ -177,6 +225,13 @@ new constructor args = do
177225 f <- makeObjectRef constructor
178226 rethrow $ objCallAsConstructor f args
179227
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
234+
180235type JSObjectCallAsFunctionCallback' =
181236 JSContextRef
182237 -> JSValueRef
@@ -228,7 +283,7 @@ makeArray args exceptions = do
228283 liftIO $ withArrayLen rargs $ \ len ptr ->
229284 jsobjectmakearray gctxt (fromIntegral len) ptr exceptions
230285
231−array :: MakeArgRefs args => args -> JSC JSValueRef
286+array :: MakeArgRefs args => args -> JSC JSObjectRef
232287array = rethrow . makeArray
233288
234289
@@ -250,6 +305,5 @@ array = rethrow . makeArray
250305
251306
252307
253−
254308
255309