Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create Arrays? #2

Open
schell opened this issue May 4, 2017 · 10 comments
Open

How to create Arrays? #2

schell opened this issue May 4, 2017 · 10 comments

Comments

@schell
Copy link

schell commented May 4, 2017

I see there are many types of arrays (Float32Array, ArrayBuffer, Int32Array, ...), and many type classes that these array objects belong to (IsFloat32Array, IsGObject, IsJSVal, PToJSVal, ...) but it isn't immediately clear how to construct one of these arrays (perhaps with 1, 2, 3, etc, values) without using FFI - which seems to break the goal of jsaddle. Any tips?

@hamishmack
Copy link
Member

This is an area that probably needs some work. We have some modules in JSaddle that try to provide the same interface as ghcjs-base. It sounds like you probably want GHCJS.Buffer and JavaScript.TypedArray. One problem with these is that you currently have to cast them to the ghcjs-dom types. Where possible we should try to add instances of the ghcjs-dom type classes for the ghcjs-base types. For example we could probably add the following to GHCJS.DOM.Types:

instance IsFloat32Array JavaScript.TypedArray.Float32Array

@schell
Copy link
Author

schell commented May 5, 2017

It also seems we need to add instance TypedArray Javascript.TypedArray.Int32Array as currently all I see are instances for the IO* variants of arrays. I can't seem to create an Int32Array that is TypedArray. After that I have to setIndex and then cast.

@schell
Copy link
Author

schell commented May 5, 2017

So far I've come to this:

singleton :: Int -> JSM Int32Array
singleton int = do
  array :: IOInt32Array <- create 1
  setIndex 0 int array
  _ array

I'm still trying to figure out how to cast IOInt32Array to Int32Array.

@schell
Copy link
Author

schell commented May 8, 2017

Okay, so I simply stuck undefined in the hole above to get past this error. It turns out that Javascript.TypedArray.Internal.Types (which is where IOInt32Array is defined) isn't exported in ghcjs-base, so compilation on ghcjs fails with this. There must be another way. I guess I could just go back to CPP + FFI.

@schell
Copy link
Author

schell commented Jun 19, 2017

I think that the way to do this may be through the ArrayBufferView interface.
on mdn

@oliverbunting
Copy link

@hamishmack Is the casting something which is likely to be fixed at some point, or will coercions be here to stay?

@mankyKitty
Copy link

Apologies if this isn't quite the right place for this but, this is how I currently create a new "typed" array via jsaddle-dom (using ghcjs-dom):

toFloat32Array  :: MonadJSM m  => [Double]  -> m Float32Array
toFloat32Array ds = liftJSM $ do
  a <- new (jsg ("Float32Array" :: Text)) [ds]
  unsafeCastTo Float32Array a

This feels pretty horrid to me but thus far it works. I've a function for 'Uint8Array' that works the same way.

I'll be trying to add a fix for the ArrayBufferView because that's not actually a real thing in JS land, based on what I've read. It is meant to indicate any of the typed array variants. Float32Array Float64Array Uint8Array etc, you can't actually construct that type.

@nomeata
Copy link

nomeata commented Aug 8, 2018

I am also stuck here, while trying to port this file from using just jsaddle to ghcjs-dom: https://github.com/nomeata/reflex-dom-fragment-shader-canvas/blob/02c191b0c0c7aac1da3fe9f5dd80db295590eea1/Reflex/Dom/FragmentShaderCanvas.hs

@schell
Copy link
Author

schell commented Aug 8, 2018

I have never solved this without coercion, but I also haven't worked on it in a long time.

@nomeata
Copy link

nomeata commented Aug 8, 2018

This hack seems to work for me :-(

  array <- liftDOM (new (jsg ("Float32Array"::Text))
        [[ -1.0, -1.0,
            1.0, -1.0,
           -1.0,  1.0,
           -1.0,  1.0,
            1.0, -1.0,
            1.0,  1.0 :: Double]])
    >>= unsafeCastTo Float32Array
  let array' = uncheckedCastTo ArrayBuffer array
  bufferData gl ARRAY_BUFFER (Just array') STATIC_DRAW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants