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

JavaScript.Web.WebSocket: ReadyState does not map to readyState constants #94

Closed eamsden closed 7 years ago

eamsden commented 7 years ago

Synopsis

The ReadyState type exported by JavaScript.Web.WebSocket has an enum instance which is used to marshall it from the readyState property of a websocket. This enum instance does not properly map the integer constants to ReadyState values. The possible values of the readyState property are given at https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Ready_state_constants

Expected behavior

getReadyState myWebSocket

returns the ready state of the websocket.

Actual behavior

getReadyState myWebSocket

throws the following exception to the console:

toEnum{ReadyState}: tag (3) is outside of enumeration's range (0,2)
CallStack (from HasCallStack):
  error, called at ./JavaScript/Web/WebSocket.hs:71:29 in ghcjs-base-0.2.0.0-GiGsdYOoO5CISyJ2m7ax9F:JavaScript.Web.WebSocket

Cause

The ReadyState type is written

data ReadyState = Closed | Connecting | Connected
  deriving (Data, Typeable, Enum, Eq, Ord, Show)

and decoded by

getReadyState ws = fmap toEnum (js_getReadyState ws)

The Enum instance derived by the deriving clause does not map to the ready states given in the WebSocket API documentation.

eamsden commented 7 years ago

I've made a pull request #95 which fixes this issue.

luigy commented 7 years ago

Good catch! I wonder if functionality that is available from ghcjs-dom should be eventually removed from ghcjs-base. ghcjs-dom has the advantage that is auto generated from the IDL files and this kind of bugs are unlikely to come up

eamsden commented 7 years ago

Is ghcjs-dom now the preferred way to access Javascript API functionality? If so, this should be stated somewhere more clearly than it is right now.