Skip to content
New issue

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

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

Already on GitHub? # to your account

fromByteString produces a null Buffer when the ByteString is empty #49

Open
ryantrinkle opened this issue Nov 21, 2015 · 3 comments
Open
Labels

Comments

@ryantrinkle
Copy link

This program:

{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}

import qualified Data.ByteString as BS
import GHCJS.Types
import GHCJS.Buffer

main :: IO ()
main = do
  let (b, _, _) = fromByteString BS.empty
  consoleLog $ jsval $ getArrayBuffer b

foreign import javascript safe "console.log($1)" consoleLog :: JSVal -> IO ()

produces the following error:

uncaught exception in Haskell main thread: TypeError: Cannot read property 'buf' of null
TypeError: Cannot read property 'buf' of null
    at h$$k4 (/home/ryan/try-reflex22/fromByteStringIssue.jsexe/all.js:37401:57)
    at h$runThreadSlice (/home/ryan/try-reflex22/fromByteStringIssue.jsexe/all.js:9790:11)
    at h$runThreadSliceCatch (/home/ryan/try-reflex22/fromByteStringIssue.jsexe/all.js:9741:12)
    at Immediate.h$mainLoop (/home/ryan/try-reflex22/fromByteStringIssue.jsexe/all.js:9736:9)
    at processImmediate [as _immediateCallback] (timers.js:374:17)

But, when the BS.empty is replaced with a non-empty ByteString, it outputs

ArrayBuffer {}

as expected.

@luite
Copy link
Member

luite commented Nov 22, 2015

Hmm, good catch. GHCJS does not allocate a buffer of size 0 for empty buffers.

GHC HEAD also adds some resizable ByteArray# operations, which may make it necessary to move getArrayBuffer into IO. Should it throw an exception when there's no buffer? should it return a Maybe or Nullable type instead? or perhaps return a dummy buffer?

@ryantrinkle
Copy link
Author

in my use case, I need to send the buffer along to WebSocket's send method, so I need a dummy, ultimately. However, I don't mind converting from Maybe/Nullable as necessary - whatever makes the most sense to you (or has the best performance) is fine with me.

@ryantrinkle
Copy link
Author

@hamishmack hamishmack added the bug label Jul 7, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants