ghcjs / ghcjs-base

base library for GHCJS for JavaScript interaction and marshalling, used by higher level libraries like JSC
MIT License
45 stars 67 forks source link

Added Float32/64 Typed array types #38

Closed achirkin closed 8 years ago

achirkin commented 8 years ago

Seems like someone forgot to re-export Float32Array and Float64Array in JavaScript.TypedArray from JavaScript.TypedArray.Internal.Types. Also I would like to have (TypedArray a => JSRef -> a) interface for my WebGL things: some of the webGL methods return typed arrays of arbitrary type - https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView

achirkin commented 8 years ago

And last thing: do you consider exposing SomeTypedArray/SomeArrayBuffer? My concern is that WebGL does not care about mutability, and I do not want to implement two different interfaces for mutable/immutable arrays, but this might be handy for users.

luite commented 8 years ago

Oops I was typing a reply to this, but accidentally closed the window

luite commented 8 years ago

I was expecting libraries to typically use one of the two types, and users to (unsafe)freeze/thaw for conversion, but maybe it can indeed be exported, if it doesn't lead to new ways of circumventing safety. I'll have to think about this a little.

luite commented 8 years ago

What exactly should your (TypedArray a => JSRef -> a) do? Try to convert an arbitrary typed array in the JSRef to the requested one, and fail with an exception if it can't (due to improper alignment for example)? Can you give an example of where you'd use this?

achirkin commented 8 years ago

Yes, I see that my suggestion is not very nice in that sense. I have problems with porting my library to the new ghcjs-base, because in my version of typed arrays I use single type parameterized by an element type. Today I tried to make it as close as possible to yours, and here is it:

type TypedArray a = SomeTypedArray Immutable a
type STTypedArray s a = SomeTypedArray (STMutable s) a
type IOTypedArray a = SomeTypedArray Mutable a
newtype SomeTypedArray (m :: MutabilityType s) a
    = SomeTypedArray JSRef deriving Typeable
instance IsJSRef (SomeTypedArray m a)

Then e.g. WebGL operation readPixels looks like this:

readPixels' :: Mutability m ~ IsMutable
            => WebGLRenderingContext -> GLint -> GLint
            -> GLsizei -> GLsizei -> GLenum -> GLenum
            -> SomeTypedArray m a -> IO ()

Also I parameterize arrays over Num types directly, not by TypedArrayElem (the only problem here is ambiguity of Uint8clamped, which I solved by newtypeing Word8), and use two classes for immutable and mutable operations. This makes it possible to provide simple functions like following:

fromList :: [a] -> SomeTypedArray Immutable a
fromListIO :: [a] -> IO (SomeTypedArray Mutable a)
fromListST :: [a] -> ST s (SomeTypedArray (STMutable s) a)