@@ -23,6 +23,14 @@ module Language.Javascript.JSC.Object (
28 -- * Setting the value of a property
36 -- * Setting the value of a property
@@ -32,6 +40,7 @@ module Language.Javascript.JSC.Object (
36 -- * Calling Haskell From JavaScript
45 -- * Calling Haskell From JavaScript
37 , function
46 , function
@@ -68,12 +77,13 @@ import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSBase
68 JSValueRef, JSContextRef)
77 JSValueRef, JSContextRef)
69import Foreign.C (CSize(..), CUInt(..), CULong(..))
78import Foreign.C (CSize(..), CUInt(..), CULong(..))
70import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
79import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
71− (jspropertynamearraygetnameatindex, jspropertynamearraygetcount,
80+ (jsobjectmake, jspropertynamearraygetnameatindex,
81+ jspropertynamearraygetcount, jsobjectcopypropertynames,
72− jsobjectcopypropertynames, jsobjectsetpropertyatindex,
82+ jsobjectsetpropertyatindex, jsobjectgetpropertyatindex,
73− jsobjectgetpropertyatindex, jsobjectcallasconstructor,
83+ jsobjectcallasconstructor, jsobjectmakearray,
74− jsobjectmakearray, jsobjectcallasfunction, jsobjectgetproperty,
84+ jsobjectcallasfunction, jsobjectgetproperty, jsobjectsetproperty,
75− jsobjectsetproperty, JSPropertyAttributes,
85+ JSPropertyAttributes, JSObjectCallAsFunctionCallback,
76− JSObjectCallAsFunctionCallback, jsobjectmakefunctionwithcallback)
86+ jsobjectmakefunctionwithcallback)
77import Language.Javascript.JSC.Exception (rethrow)
87import Language.Javascript.JSC.Exception (rethrow)
78import Language.Javascript.JSC.Value
88import Language.Javascript.JSC.Value
79 (JSUndefined, valMakeUndefined, valToObject)
89 (JSUndefined, valMakeUndefined, valToObject)
@@ -152,6 +162,60 @@ js :: (MakeObjectRef s, MakeStringRef name)
152 -> IndexPreservingGetter s (JSC JSPropRef)
162 -> IndexPreservingGetter s (JSC JSPropRef)
153js name = to (!name)
163js name = to (!name)
165+-- | Java script function applications have this type
166+type JSF = MakeObjectRef o => IndexPreservingGetter o (JSC JSValueRef)
168+-- | Handy way to call a function
170+-- > jsf name = js name . to (# args)
172+-- >>> testJSC $ val "Hello World" ^. jsf "indexOf" ["World"]
174+jsf :: (MakeStringRef name, MakeArgRefs args) => name -> args -> JSF
175+jsf name args = function . to (# args)
179+-- | Handy way to call a function that expects no arguments
181+-- > js0 name = jsf name ()
183+-- >>> testJSC $ val "Hello World" ^. js0 "toLowerCase"
185+js0 :: (MakeStringRef name) => name -> JSF
186+js0 name = jsf name ()
188+-- | Handy way to call a function that expects one argument
190+-- > js1 name a0 = jsf name [a0]
192+-- >>> testJSC $ val "Hello World" ^. js1 "indexOf" "World"
194+js1 :: (MakeStringRef name, MakeValueRef a0) => name -> a0 -> JSF
195+js1 name a0 = jsf name [a0]
197+-- | Handy way to call a function that expects two arguments
198+js2 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1) => name -> a0 -> a1 -> JSF
199+js2 name a0 a1 = jsf name (a0, a1)
201+-- | Handy way to call a function that expects three arguments
202+js3 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2)
203+ => name -> a0 -> a1 -> a2 -> JSF
204+js3 name a0 a1 a2 = jsf name (a0, a1, a2)
206+-- | Handy way to call a function that expects four arguments
207+js4 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2,
209+ => name -> a0 -> a1 -> a2 -> a3 -> JSF
210+js4 name a0 a1 a2 a3 = jsf name (a0, a1, a2, a3)
212+-- | Handy way to call a function that expects five arguments
213+js5 :: (MakeStringRef name, MakeValueRef a0, MakeValueRef a1, MakeValueRef a2,
214+ MakeValueRef a3, MakeValueRef a4)
215+ => name -> a0 -> a1 -> a2 -> a3 -> a4 -> JSF
216+js5 name a0 a1 a2 a3 a4 = jsf name (a0, a1, a2, a3, a4)
155-- | Handy way to get and hold onto a reference top level javascript
219-- | Handy way to get and hold onto a reference top level javascript
157-- >>> testJSC $ eval "w = console; w.log('Hello World')"
221-- >>> testJSC $ eval "w = console; w.log('Hello World')"
@@ -212,6 +276,16 @@ call function this args = do
212 rthis <- makeObjectRef this
276 rthis <- makeObjectRef this
213 rethrow $ objCallAsFunction rfunction rthis args
277 rethrow $ objCallAsFunction rfunction rthis args
279+-- | Make an empty object using the default constuctor
281+-- >>> testJSC $ eval "var a = {}; a.x = 'Hello'; a.x"
282+-- >>> testJSC $ do { a <- obj; a ^. js "x" <# "Hello"; a ^. js "x" }
284+obj :: JSC JSObjectRef
287+ liftIO $ jsobjectmake gctxt nullPtr nullPtr
215type JSObjectCallAsFunctionCallback' =
289type JSObjectCallAsFunctionCallback' =
216 JSContextRef
290 JSContextRef
217 -> JSValueRef
291 -> JSValueRef