Permalink
Browse files

Add strict imports for writeArray and create a listToStrictJSArray

  • Loading branch information...
1 parent ac1368e commit 743acb12060c09176eb7b0aff3cb70daf6b0c2f5 @norm2782 norm2782 committed Mar 19, 2012
Showing with 11 additions and 0 deletions.
  1. +11 −0 uhc-js/src/Language/UHC/JScript/ECMA/Array.hs
View
11 uhc-js/src/Language/UHC/JScript/ECMA/Array.hs
@@ -11,6 +11,9 @@ type JSArray x = BoxArray x
instance JS (JSArray a)
+instance ToJS [a] (JSArray a) where
+ toJS = listToStrictJSArray
+
foreign import js "%1.length"
lengthJSArray :: JSArray x -> Int
@@ -185,11 +188,19 @@ foreign import prim
foreign import prim "primWriteArray"
primWriteArray :: BoxArray x -> Int -> x -> ()
+foreign import prim "primStrictWriteArray"
+ primStrictWriteArray :: BoxArray x -> Int -> x -> ()
+
listToJSArray :: [a] -> JSArray a
listToJSArray [] = error "Cannot convert empty list"
listToJSArray xs = snd $ foldr f (0, primNewArray (B.length xs) (head xs)) xs
where f x (n, arr) = (n+1, seq (primWriteArray arr n x) arr)
+listToStrictJSArray :: [a] -> JSArray a
+listToStrictJSArray [] = error "Cannot convert empty list"
+listToStrictJSArray xs = snd $ foldr f (0, primNewArray (B.length xs) (head xs)) xs
+ where f x (n, arr) = (n+1, seq (primStrictWriteArray arr n x) arr)
+
indexJSArray :: JSArray x -> Int -> x
indexJSArray = indexArray

0 comments on commit 743acb1

Please sign in to comment.