purescript/purescript-arrays

Incompatibility with `purs bundle`

maximedenes opened this issue · 3 comments

The following line:

exports.replicate = typeof Array.prototype.fill === "function" ? replicate : replicatePolyfill;

causes runtime errors when compiled with purs bundle, because replicatePolyfill gets eliminated as dead code.

I'm not sure if it is a purs bundle bug, since I don't understand exactly the spec of the code elimination it performs (in particular, what shapes of exports it supports).

garyb commented

What version of purs are you using that you see this on? I just made a little test project so I could check that your fix in the PR was working, and when I build:

module Main where

import Prelude

import Data.Array as A
import Effect (Effect)
import Effect.Console (log)

main :: Effect Unit
main = do
  log "replicate should produce an array containg an item a specified number of times"
  log $ show $ A.replicate 3 true == [true, true, true]
  log $ show $ A.replicate 1 "foo" == ["foo"]
  log $ show $ A.replicate 0 "foo" == []
  log $ show $ A.replicate (-1) "foo" == []

The bundle produced includes both the implementaton and polyfill for replicate: https://gist.github.com/garyb/af8a44542209fd147ae95ccb70056956

zyla commented

I am experiencing it too.

An easy way to reproduce the issue is to run the test suite bundled and with Array.prototype.fill disabled:

$ pulp browserify -O --to output/test.js -I test --main Test.Main && node -e "delete Array.prototype.fill; require('./output/test.js').main();"
* Browserifying project in /home/zyla/w/o/purescript-arrays
* Building project in /home/zyla/w/o/purescript-arrays
           Src   Lib   All
Warnings   0     0     0  
Errors     0     0     0  
* Build successful.
* Browserifying...
* Browserified.
/home/zyla/w/o/purescript-arrays/output/test.js:873
  exports.replicate = typeof Array.prototype.fill === "function" ? replicate : replicatePolyfill;
                                                                               ^

ReferenceError: replicatePolyfill is not defined
    at PS.Data.Array (/home/zyla/w/o/purescript-arrays/output/test.js:873:80)
    at Object.1 (/home/zyla/w/o/purescript-arrays/output/test.js:1120:3)
    at o (/home/zyla/w/o/purescript-arrays/output/test.js:1:327)
    at r (/home/zyla/w/o/purescript-arrays/output/test.js:1:493)
    at Object.<anonymous> (/home/zyla/w/o/purescript-arrays/output/test.js:1:522)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
garyb commented

Thanks for the reproduction case!