HeinrichApfelmus / threepenny-gui

GUI framework that uses the web browser as a display.
https://heinrichapfelmus.github.io/threepenny-gui/
Other
437 stars 77 forks source link

Cannot convert undefined or null to object #264

Open polymorphicengine opened 2 years ago

polymorphicengine commented 2 years ago

Hello, I'm using the threepenny library for a code editor based on the codemirror library.

I have the following function that expects a CodeMirror editor object as input and get's triggered by pressing STR+ENTER while the editor is selected.

getValue :: JSObject -> UI String
getValue cm = callFunction $ ffi "(%1).getValue()" cm

it can apperently happen, that sometimes the argument is 'undefined' so I tried something like this:

getValue :: JSObject -> UI String
getValue cm = callFunction $ ffi "(function (a) { if (typeof a === 'undefined' || a === null) {console.log(\"hm1\"); return \"\";} else {console.log(\"okk\");  return a.getValue(); } })(%1)" cm

But this will sometimes throw

haskell.js:359 Uncaught TypeError: Cannot convert undefined or null to object
    at Object.Haskell.freeStablePtr (haskell.js:359:22)
    at eval (eval at receive (haskell.js:266:11), <anonymous>:1:9)
    at receive (haskell.js:266:11)
    at WebSocket.ws.onmessage (haskell.js:108:9)

The annoying thing is that it is not very easy to reproduce this, as it doesn't occur very often. But when it occurs it's very annoying because it crashes the editor. I unfortunatley don't have a lot of experience with JavaScript, so I'm hoping that someone here can help me out.