I was able to solve this before sorting up Int32Arrayinstead Uint32Array; probably the one who actually knew GHCJS for more than the one hour I invested in it will be able to expand it so that you get yours Uint32Array(or you can just make a hacked version GHCJS.Bufferthat supports the operation getUint32Array).
The idea is to get the ByteArrayunderlying view Vector, and then sliceso that only the relevant part remains:
import Data.Vector.Unboxed as V
import Data.Word
import qualified Data.Vector.Unboxed.Base as B
import qualified Data.Vector.Primitive as P
import GHCJS.Types
import qualified GHCJS.Buffer as Buffer
import JavaScript.TypedArray (Int32Array)
-- TODO: generalize this to all types that support unboxed vectors...
toI32Array :: Vector Word32 -> Int32Array
toI32Array (B.V_Word32 (P.Vector offset len bs)) =
js_slice offset (offset + len) $ Buffer.getInt32Array (Buffer.fromByteArray bs)
foreign import javascript unsafe "$3.slice($1, $2)" js_slice :: Int -> Int -> Int32Array -> Int32Array
-- should be
-- foreign import javascript unsafe "$3.slice($1, $2)" js_slice :: Int -> Int -> SomeTypedArray a m -> SomeTypedArray a m
-- but alas, JavaScript.TypedArray.Internal.Type is a hidden module
Here is a code usage example:
v :: Vector Word32
v = V.fromList [1, 2, 3]
foreign import javascript unsafe "console.debug($1);" js_debug :: JSVal -> IO ()
main = do
let v' = toI32Array v
js_debug $ jsval v'
If you look at the console in a browser, you can check what jsval v'really has a type Int32Array.
source
share