This post will demonstrate a couple of techniques for using the GHCJS helper libraries: jsaddle and jsaddle-dom to:

Create an ArrayBuffer for use with WebGL

Leverage the Notification API available in some browsers.

ArrayBuffers

During some experiments with reflex-dom-canvas, a library for interacting with the Canvas API, I started having runtime exceptions regarding a vertex index being out of bounds. Initially I thought this was due to an incorrect memory plan:

let = 2 -- 2 components per iteration size = Gl.FLOAT -- the data is 32bit floats dataType = False -- don't normalize the data normalise = 0 -- 0 for tightly packed array, or (size * sizeof(type)) to get the next position/per iteration stride = 0 -- start at the beginning of the buffer offset -- Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER) fromIntegral _rPosAttrLoc) size dataType normalise stride offset Gl.vertexAttribPointerF (_rPosAttrLoc) size dataType normalise stride offset

After trying a few iterations of different stride values I inspected the list of vertices, in case something hadn’t survived the trip from Haskell -> JavaScript. It wasn’t the first point of investigation because it seemed so innocuous:

positions :: [ Double ] = positions [ 0.0 , 0.0 , 0.0 , 0.5 , 0.7 , 0.0 ]

Using the toJSVal function from jsaddle to take our [Double] and turn it into a JSVal for the API to consume:

toJSVal positions

The problem was that the list of vertices were being inlined as separate arguments to the bufferData function, instead of the list itself being treated as a single argument.

What I expected:

bufferData (ARRAY_BUFFER , vertexList , STATIC_DRAW) ; (ARRAY_BUFFERvertexListSTATIC_DRAW)

What was appearing in JavaScript:

bufferData (ARRAY_BUFFER , 0.0 , 0.0 , 0.0 , 0.5 , 0.7 , 0.0 , STATIC_DRAW) ; (ARRAY_BUFFERSTATIC_DRAW)

This is because jsaddle provides the MakeArgs typeclass for turning a list into the arguments of a function. So my list of vertices was being inlined as individual function inputs, and I didn’t understand it well enough to be able to solve my problem - yet.

From [Double] to ArrayBuffer

In the first attempt, the ArrayBuffer was handled directly. Creating a Float32Array that was backed by that buffer, populating it with data, before accessing the underlying buffer again to use in the WebGL code. Fuelled by a need to make something appear on the screen, and my general ignorance of the workings of the jsaddle library, this attempt ended up being very low level and manual.

The second attempt more closely mirrors the approach used in many JavaScript WebGL tutorials. It is also a marked improvement over the first attempt, courtesy of mistakes made, lessons learned, and questions asked.

First Attempt

Step 1) Instantiate a fixed size ArrayBuffer object and create a Float32Array using that buffer.

// JavaScript var buff = new ArrayBuffer ( positions . length * 4 ) ; buff var f32Arr = new Float32Array (buff) ; f32Arr(buff)

-- Haskell -- The 'length' function returns an 'Int', but we need to give jsaddle a 'Double', so we use -- 'fromIntegral' to handle that transformation for us. This needed a type annotation to -- help Haskell along as these types end up very general. You can't pick a typeclass when -- all you know about a type is that it is an instance of 'Num'. let buffSize :: Double = fromIntegral ( length positions * 4 ) buffSizepositions <- new (jsg "ArrayBuffer" ) buffSize buffnew (jsg) buffSize <- new (jsg "Float32Array" ) buff f32Arrnew (jsg) buff

The new function from jsaddle works similarly to its JavaScript counterpart, requiring a constructor object and a list of arguments to pass to the constructor function. The jsg function acquires a top level JavaScript reference, a simple intuition for it may be anything it exists on the window object may be accessed using jsg . You can read more about these functions here ( new ), and here ( jsg ).

Step 2) Populate the Float32Array with the position information.

// JavaScript for ( var i = 0 ; i < positions . length ; i ++ ) { = positions[i] ; f32Arr[i]positions[i] }

-- Haskell -> (f32Arr <## ix) pos ) positions itraverse_ (\ix pos(f32Arrix) pos ) positions

The itraverse_ function is an indexed traversal, providing the index of the current element as input to the traversal function:

itraverse_ :: ( Applicative f, FoldableWithIndex i t) => (i -> a -> f b) -> t a -> f () f,i t)(if b)t af ()

We can use that with a jsaddle function, (<##) ,documented here. This function sets a property on an object at the given index. For a bit more flavour, we’re able to take advantage of the fact that Haskell functions only take one argument [1] [2], to simplify our traversal function:

-- Starting here with our original function, let's call it 'f' for now... f :: Int -> Double -> JSM () () = (f32Arr <## ix) pos f ix pos(f32Arrix) pos -- We don't need to explicitly include the 'pos' argument f :: Int -> Double -> JSM () () = (f32Arr <## ix) pos f ix pos(f32Arrix) pos -- is the same as f :: Int -> Double -> JSM () () = f32Arr <## ix f ixf32Arrix -- because... We can also drop the 'ix' argument, because this function: (<##) :: ( MakeObject this, ToJSVal val) => this -> Int -> val -> JSM () this,val)thisval() -- Specialised to our Float32Array (<##) :: Float32Array -> Int -> Double -> JSM () () -- Then partially applied to our Float32Array: f :: Int -> Double -> JSM () () f = (f32Arr <## ) (f32Arr -- Consequently our thoroughly "code golf'd" 'itraverse_' becomes <## ) positions itraverse_ (f32Arr) positions

Step 3) Retrieve the underlying buffer to be used in the rendering process.

The underlying buffer of a Float32Array is accessed as a property of the array object itself.

// JavaScript return f32Arr . buffer ;

Haskell needs a bit more information as the property access functions return a JSVal . Conversions are up to you, because this lets you choose where you want to be on the runtime safety scale:

-- Haskell -- Access the "buffer" property on our f32Arr object <- f32Arr ! "buffer" f32Bufff32Arr -- Convert our JSVal to the ArrayBuffer type we require by giving -- the constructor to the ``castTo`` function from 'jsaddle' ArrayBuffer f32Buff castTof32Buff

The castTo function from jsaddle will return a Maybe a of your desired cast, to avoid runtime exceptions wherever possible. There is another version unsafeCastTo that will simply crash if the cast was not possible.

First attempt results:

All in all, quite a bit of heavy lifting going on…

// JavaScript function buildBuffer (positions) { (positions) var buff = new ArrayBuffer ( positions . length * 4 ) ; buff var f32Arr = new Float32Array (buff) ; f32Arr(buff) for ( var i = 0 ; i < positions . length ; i ++ ) { = positions[i] ; f32Arr[i]positions[i] } return f32Arr . buffer ; }

-- Haskell buildBuffer :: [ Double ] -> JSM ( Maybe ArrayBuffer ) = do buildBuffer positions let buffSize :: Double = fromIntegral ( length positions * 4 ) buffSizepositions <- new (jsg "ArrayBuffer" ) buffSize buffnew (jsg) buffSize <- new (jsg "Float32Array" ) buff f32Arrnew (jsg) buff <## ) positions itraverse_ (f32Arr) positions <- f32Arr ! "buffer" buffValf32Arr ArrayBuffer buffVal castTobuffVal

The different steps translate quite easily to Haskell, and we have the added benefit of the types and all the various plumbing functions that Haskell provides.

Second Attempt

Many WebGL tutorials demonstrate the following technique for creating the Float32Array . Also, given that the array is backed by an ArrayBuffer by design, we can simply pull that off the newly minted array:

// JavaScript function buildBuffer (positions) { (positions) var f32Arr = new Float32Array (positions) ; f32Arr(positions) return f32Arr . buffer ; }

Where I was coming unstuck with my usage of the jsaddle functions was my understanding of the MakeArgs TypeClass. Its purpose is to allow you to construct the list of arguments for a function. I needed to pass a list of inputs as a single argument to the function. Perhaps obviously, the solution was to simply place my list in a list. Thus…

buildBuffer :: [ Double ] -> JSM ( Maybe ArrayBuffer ) = do buildBuffer positions <- new (jsg "Float32Array" ) [positions] f32Arrnew (jsg) [positions] <- f32Arr ! "buffer" buffValf32Arr ArrayBuffer buffVal castTobuffVal

Or if you don’t like intermediate variables:

buildBuffer :: [ Double ] -> JSM ( Maybe ArrayBuffer ) = new (jsg "Float32Array" ) [positions] buildBuffer positionsnew (jsg) [positions] >>= ( ! "buffer" ) >>= castTo ArrayBuffer castTo

This is a much more concise way of building an ArrayBuffer , and knowing more about the MakeArgs typeclass is extremely useful when it comes to creating things like callbacks and integrating with other JavaScript APIs.

Now that we know how to use JavaScript constructors from within GHCJS code, pass arguments to the constructors, and access properties on the objects themselves. We can do all these things, we will try something a bit more adventurous…

Now, let’s annoy, I mean communicate with, some users using desktop notifications! We’ll use some of the functions we introduced in the first section: new , jsg . To access the Notification object on the window, check our permissions, and try to send a simple notification to the user. We’ll work through the example from the MDN API documentation page.

Accessing the API Object

Earlier we used the jsg function to acquire a top level JavaScript reference, in that case it was a constructor: Float32Array . We’ll use that function again, but this time we need access to the object because we need to run some of its functions. You can think of the jsg function as a roughly equivalent to a property accessor for the window object.

<- jsg "Notification" notifyjsg

Now we can check what our permissions are with respect to the Notifications API and decide what to do. To do that we need to access a property on the Notification object.

<- notify ^. js "permission" permStrnotifyjs

This will give us a JSVal that is the current permission setting for the Notification API. But we have a JSVal and it could be anything! According to the documentation though, this property should be a stringly value from a list of three options:

“denied”

“default”

“granted”

We could use the valToText function from jsaddle to try to change this to a JSString , change that to a Text value with strToText , and finally unpack this to a String value and decide what to do:

<- valToText =<< notify ^. js "permission" permStrvalToTextnotifyjs case fromJSString permStr where fromJSString permStr "denied" -> ... "granted" -> ... "default" -> ... _ -> ...

But we would need to do that every time we checked the permissions, that’s not very nice! We’re using Haskell after all, so we will build a data type to represent our permission levels, then tell jsaddle how to translate from a JSVal . First, the new type:

data NotifyPerm = Default | Denied | Granted deriving ( Show , Eq )

Then we need to tell jsaddle how to translate a JSVal into our type. We do this by creating an instance of the FromJSVal class. In JavaScript land, the permission value is just a string, so we can use that to solve half the problem when trying to create an instance of our type:

instance FromJSVal NotifyPerm where fromJSVal :: JSVal -> JSM ( Maybe NotifyPerm ) = do fromJSVal v -- Will give us the JSString value of whatever this 'JSVal' is. <- valToStr v permStrvalToStr v -- Unpack the 'JSString' to a boring Haskell 'String' so we can use a 'case': pure $ case unpack $ strToText permStr of unpackstrToText permStr -- Pattern match on the string values "default" -> Just Default "denied" -> Just Denied "granted" -> Just Granted -- Ignore everything that doesn't meet our requirements. _ -> Nothing

We are able to use Generic to derive a FromJSVal instance automatically, using the DeriveGeneric and DeriveAnyClass extensions. But this technique won’t work the way you want. By just looking at the NotifyPerm type, can you see why?

Now that we’re able to use a more robust type, we can decide what to do when we know what permissions we have for creating notifications:

<- notify ^. js "permission" permValnotifyjs <- fromJSVal permVal notifyPermfromJSVal permVal -- Using 'traverse' here lets us write the 'handleNotify' function without -- worrying about the 'Nothing' case, which makes our life easier. traverse (handleNotify notify message) notifyPerm (handleNotify notify message) notifyPerm

Now we can write our handleNotify function in a let or where binding to keep things neat and tidy. Or not, referential transparency is lovely like that. We’ll need the reference to the Notification object, and the message, assumed to be a Text value. Because we don’t have to worry about the Maybe , we can pattern match on our permission, making everything even easier, again.

where -- No permission, just return '()' and do nothing. Denied = pure () handleNotify _ _() -- Permission already granted, create our Notification Granted = handleNotify nObj msg -- We use 'void' here because we don't need the result of this call to 'new' $ new nObj ( ValString msg) voidnew nObj (msg) -- The gnarly case, the API will ask the user for permission to show notifications -- We must provide a callback function to act on the answer Default = handleNotify nObj msg -- Call the 'requestPermission' function on the Notification object $ nObj ^. jsf "requestPermission" voidnObjjsf -- This is our callback function, we will go over this in more detail next $ \_ _ [newPerm] -> do [ fun\_ _ [newPerm] <- fromJSVal newPerm pVfromJSVal newPerm -> when ( p == Granted ) $ newNotify nObj ) pV traverse_ (\pwhen ( pnewNotify nObj ) pV ]

Lets go over building the callback function, which in jsaddle is defined as: JSCallAsFunction. Using the fun function as short hand for using a normal Haskell function.

type JSCallAsFunction = JSVal -- The function object -> JSVal -- "this" object -> [ JSVal ] -- The function arguments -> JSM () -- Must return unit as the function may run on a different thread () -- We can discard the first two arguments, using '_', we don't use them. Then we pattern match on the list of arguments $ \_ _ [newPerm] -> do [ fun\_ _ [newPerm] -- Take the 'JSVal' and convert it to a 'NotifyPerm' <- fromJSVal newPerm pVfromJSVal newPerm -- Use a traverse to run our function on the 'NotifyPerm' value, if we have one. -> when ( p == Granted ) $ newNotify nObj ) pV traverse_ (\pwhen ( pnewNotify nObj ) pV ]

The purpose of placing the entire call to fun inside a list is that we want the MakeArgs typeclass to pass in this function as a single input to the requestPermission function. Now we have all the moving parts in place to start using the Notification API in the Browser via GHCJS. Yay! A version of this integration is being used here. It won’t work on Android at the moment. Chrome version 62 or higher will require a https website before it will enable notifications, but Firefox should work.

Conclusion

Phew, that was a lot to get through. But by now you should know how to:

Call JavaScript constructors.

Pass arguments to JavaScript functions & constructors.

Access properties on objects and top level references.

Convert from simple JavaScript values to Haskell types.

Interact with JavaScript APIs.

Create callback functions for JavaScript code to use.

All from the nice type safe world of Haskell & GHCJS.