问题描述
我有一个 Data.Vector.Unboxed.Vector Word32
类型的元素。我想将其转换为本机JS TypedArray
(特别是 Uint32Array
)。我知道 toJsArray
和 toJsValListOf
,但这两个函数都处理列表,而不是向量,效率很低。如何将未装箱的 Vector
直接转换为JS TypedArray
?
I have an element of type Data.Vector.Unboxed.Vector Word32
. I want to convert that to a native JS TypedArray
(an Uint32Array
, specifically). I'm aware of toJsArray
and toJsValListOf
, but both functions deal with lists, not vectors, and are inefficient. How can I convert an unboxed Vector
directly to a JS TypedArray
?
推荐答案
我能够解决这个问题直到编组到 Int32Array
而不是 Uint32Array
;可能有人已经知道GHCJS的时间超过了我已经投入的一个小时就可以扩展这个以便你得到你的 Uint32Array
(或者你可以制作支持 getUint32Array
操作的 GHCJS.Buffer
的黑客版本。
I was able to solve this up to marshalling to an Int32Array
instead of an Uint32Array
; probably someone who has actually known GHCJS for more than the one hour I've put into this will be able to expand on this so that you get your Uint32Array
(or you can just make a hacked-up version of GHCJS.Buffer
that supports a getUint32Array
operation).
我们的想法是让 BycAcray
基础 Vector
表示,然后 slice
它只剩下相关部分:
The idea is to get the ByteArray
underlying the Vector
representation, and then slice
it so that only the relevant portion 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's some example code using it:
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'
如果你在浏览器中查看控制台,你可以检查 jsval v'
确实有类型 Int32Array
。
If you look at the console in a browser, you can check that jsval v'
does indeed have type Int32Array
.
这篇关于如何在GHCJS上从Unboxed Vector转换为JS TypedArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!