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

Dereferencing an exported (0 :: Int) fails #44

Closed wereHamster closed 8 years ago

wereHamster commented 8 years ago

Int prim type is represented as JavaScript integer, so this check in h$derefExport wrongfully fails:

if (!e.root) return null;

Same problem with anything that is represented in JavaScript as a falsy value (false, undefined, empty string).

luite commented 8 years ago

good point. keep in mind that you can only export lifted values though, these are always either a number, a boolean or a heap object. but this is indeed wrong, and a bit tricky to reproduce even.

wereHamster commented 8 years ago

I didn't have any trouble reproduce it ;)

main = withExport (0 :: Int) $ \r -> do
        v <- derefExport r
        print (v :: Maybe Int)

prints "Nothing".

luite commented 8 years ago

It's easy to export the unevaluated version instead, and if GHC floats it to the top level it also becomes boxed again, and therefore truthy.

I've fixed it now and added it to the test case.