@@ -20,23 +20,26 @@ module Graphics.UI.Gtk.WebKit.JavaScriptCore.JSC.Object (
2020 , objGetProperty
2121 , objCallAsFunction
2222 , (!)
23− , (.!)
2423 , (#)
2524 , (<#)
25+ , new
2626 , function
2727 , fun
28+ , array
29+ , global
2830) where
2931
3032import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSBase
3133 (JSStringRef, JSObjectRef, JSValueRefRef, JSValueRef, JSContextRef)
3234import Foreign.C (CULong(..))
3335import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
34− (jsobjectcallasfunction, jsobjectgetproperty, jsobjectsetproperty,
36+ (jsobjectcallasconstructor, jsobjectmakearray,
37+ jsobjectcallasfunction, jsobjectgetproperty, jsobjectsetproperty,
3538 JSPropertyAttributes, JSObjectCallAsFunctionCallback,
3639 jsobjectmakefunctionwithcallback)
3740import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSC.Value
3841 (valMakeUndefined, valToObject, MakeValueRef(..),
39− MakeStringRef(..))
42+ MakeStringRef(..), MakeArgRefs(..))
4043import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSC.Monad
4144 (rethrow, JSC)
4245import Control.Monad.Trans.Reader (runReaderT, ask)
@@ -48,6 +51,60 @@ import qualified Data.Text as T (pack)
4851import Foreign.Storable (Storable(..))
4952import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSValueRef
5053 (jsvaluemakeundefined)
54+import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSContextRef
55+ (jscontextgetglobalobject)
56+
57+---- Object Referene ----
58+class MakeObjectRef this where
59+ makeObjectRef :: this -> JSC JSObjectRef
60+
61+instance MakeObjectRef JSObjectRef where
62+ makeObjectRef = return
63+
64+instance MakeObjectRef v => MakeObjectRef (JSC v) where
65+ makeObjectRef v = v >>= makeObjectRef
66+
67+---- Property References ----
68+data JSPropRef = JSPropRef JSObjectRef JSStringRef
69+
70+class MakePropRef this where
71+ makePropRef :: this -> JSC JSPropRef
72+
73+instance MakePropRef JSPropRef where
74+ makePropRef (JSPropRef this name) = do
75+ rthis <- makeObjectRef this
76+ rname <- makeStringRef name
77+ return (JSPropRef rthis rname)
78+
79+instance MakePropRef prop => MakePropRef (JSC prop) where
80+ makePropRef prop = prop >>= makePropRef
81+
82+instance MakeObjectRef JSPropRef where
83+ makeObjectRef (JSPropRef this name) = rethrow $ objGetProperty this name
84+
85+instance MakeValueRef JSPropRef where
86+ makeValueRef (JSPropRef this name) = rethrow $ objGetProperty this name
87+
88+instance MakeArgRefs JSPropRef where
89+ makeArgRefs (JSPropRef this name) = do
90+ rarg <- rethrow $ objGetProperty this name
91+ return [rarg]
92+
93+---- String global scope ----
94+instance MakePropRef String where
95+ makePropRef name = do
96+ this <- global
97+ rname <- makeStringRef name
98+ return (JSPropRef this rname)
99+
100+instance MakeObjectRef String where
101+ makeObjectRef name = do
102+ this <- global
103+ rname <- makeStringRef name
104+ (rethrow $ objGetProperty this rname) >>= valToObject
105+
106+global :: JSC JSObjectRef
107+global = ask >>= (liftIO . jscontextgetglobalobject)
51108
52109objSetProperty :: (MakeStringRef name, MakeValueRef val)
53110 => JSObjectRef
@@ -72,34 +129,54 @@ objGetProperty this name exceptions = do
72129 sName <- makeStringRef name
73130 liftIO $ jsobjectgetproperty gctxt this sName exceptions
74131
75−objCallAsFunction :: MakeValueRef arg
132+objCallAsFunction :: MakeArgRefs args
76133 => JSObjectRef
77134 -> JSObjectRef
78− -> [arg]
135+ -> args
79136 -> JSValueRefRef
80137 -> JSC JSValueRef
81138objCallAsFunction function this args exceptions = do
82139 gctxt <- ask
83− rargs <- mapM makeValueRef args
140+ rargs <- makeArgRefs args
84141 liftIO $ withArrayLen rargs $ \ largs pargs ->
85142 jsobjectcallasfunction gctxt function this (fromIntegral largs) pargs exceptions
86143
87−this ! name = return (this, name)
88−
89−exp .! name2 = do
90− (this1, name1) <- exp
91− this2 <- (rethrow $ objGetProperty this1 name1) >>= valToObject
92− this2 ! name2
93−
94−exp # args = do
95− (this, name) <- exp
144+objCallAsConstructor :: MakeArgRefs args
145+ => JSObjectRef
146+ -> args
147+ -> JSValueRefRef
148+ -> JSC JSValueRef
149+objCallAsConstructor function args exceptions = do
150+ gctxt <- ask
151+ rargs <- makeArgRefs args
152+ liftIO $ withArrayLen rargs $ \ largs pargs ->
153+ jsobjectcallasconstructor gctxt function (fromIntegral largs) pargs exceptions
154+
155+(!) :: (MakeObjectRef this, MakeStringRef name) => this -> name -> JSC JSPropRef
156+this ! name = do
157+ rthis <- makeObjectRef this
158+ rname <- makeStringRef name
159+ return (JSPropRef rthis rname)
160+
161+(#) :: (MakePropRef prop, MakeArgRefs args)
162+ => prop -> args -> JSC JSValueRef
163+prop # args = do
164+ (JSPropRef this name) <- makePropRef prop
96165 f <- (rethrow $ objGetProperty this name) >>= valToObject
97166 rethrow $ objCallAsFunction f this args
98167
99−exp <# val = do
100− (this, name) <- exp
168+(<#) :: (MakePropRef prop, MakeValueRef val)
169+ => prop -> val -> JSC ()
170+prop <# val = do
171+ (JSPropRef this name) <- makePropRef prop
101172 rethrow $ objSetProperty this name val 0
102173
174+new :: (MakeObjectRef constructor, MakeArgRefs args)
175+ => constructor -> args -> JSC JSValueRef
176+new constructor args = do
177+ f <- makeObjectRef constructor
178+ rethrow $ objCallAsConstructor f args
179+
103180type JSObjectCallAsFunctionCallback' =
104181 JSContextRef
105182 -> JSValueRef
@@ -109,12 +186,14 @@ type JSObjectCallAsFunctionCallback' =
109186 -> JSValueRefRef
110187 -> IO JSValueRef
111188
189+type JSCallAsFunction = JSValueRef -> JSValueRef -> [JSValueRef] -> JSC ()
190+
112191foreign import ccall "wrapper"
113192 mkJSObjectCallAsFunctionCallback :: JSObjectCallAsFunctionCallback' -> IO JSObjectCallAsFunctionCallback
114193
115−function :: (MakeStringRef name, MakeValueRef result)
194+function :: MakeStringRef name
116195 => name
117− -> (JSValueRef -> JSValueRef -> [JSValueRef] -> JSC result)
196+ -> JSCallAsFunction
118197 -> JSC JSObjectRef
119198function name f = do
120199 gctxt <- ask
@@ -127,14 +206,50 @@ function name f = do
127206 flip runReaderT ctx $ do
128207 f fobj this args >>= makeValueRef
129208 `E.catch` \(e :: SomeException) -> do
130− str <- runReaderT (makeValueRef $ show e) ctx
209+ str <- runReaderT (makeValueRef . T.pack $ show e) ctx
131210 poke exception str
132211 jsvaluemakeundefined ctx
133212
134−data JSCallAsFunction result = JSCallAsFunction (JSValueRef -> JSValueRef -> [JSValueRef] -> JSC result)
213+fun :: JSCallAsFunction -> JSCallAsFunction
214+fun f = f
215+
216+instance MakeValueRef JSCallAsFunction where
217+ makeValueRef f = function (nullPtr::JSStringRef) f
218+
219+instance MakeArgRefs JSCallAsFunction where
220+ makeArgRefs f = do
221+ rarg <- function (nullPtr::JSStringRef) f
222+ return [rarg]
223+
224+makeArray :: MakeArgRefs args => args -> JSValueRefRef -> JSC JSObjectRef
225+makeArray args exceptions = do
226+ gctxt <- ask
227+ rargs <- makeArgRefs args
228+ liftIO $ withArrayLen rargs $ \ len ptr ->
229+ jsobjectmakearray gctxt (fromIntegral len) ptr exceptions
230+
231+array :: MakeArgRefs args => args -> JSC JSValueRef
232+array = rethrow . makeArray
233+
234+
235+
236+
237+
238+
239+
240+
241+
242+
243+
244+
245+
246+
247+
248+
249+
250+
251+
252+
135253
136−fun = JSCallAsFunction
137254
138−instance MakeValueRef result => MakeValueRef (JSCallAsFunction result) where
139− makeValueRef (JSCallAsFunction f) = function (nullPtr::JSStringRef) f
140255