Skip to content

Commit

Permalink
test(purescript#309): Failing test
Browse files Browse the repository at this point in the history
Demonstrating that the current implementation of `Array`'s `Bind` instance
causes `RangeError: Maximum call stack size exceeded` when the output of `f` in
`ma >>= f` is sufficiently large.

This is due to usage of `Function.prototype.apply`. From
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply#using_apply_and_built-in_functions):

> But beware: by using apply() (or the spread syntax) with an arbitrarily long
arguments list, you run the risk of exceeding the JavaScript engine's argument
length limit.

> The consequences of calling a function with too many arguments (that is, more
than tens of thousands of arguments) is unspecified and varies across engines.
(The JavaScriptCore engine has a hard-coded [argument limit of
65536](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply#using_apply_and_built-in_functions).)

Node v20.18.1 seems to have a higher limit around 106,000.
  • Loading branch information
pete-murphy committed Feb 10, 2025
1 parent f4cad0a commit 3843187
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions test/Test/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ export function testNumberShow(showNumber) {
]);
};
}

export function makeArray(length) {
var arr = [];
for (var i = 0; i < length; i++) {
arr.push(i);
}
return arr;
}
12 changes: 12 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ main = do
testReflectType
testReifyType
testSignum
testArrayBind

foreign import testNumberShow :: (Number -> String) -> AlmostEff

Expand Down Expand Up @@ -189,3 +190,14 @@ testSignum = do
assert "signum positive zero" $ show (1.0/(signum 0.0)) == "Infinity"
assert "Clarifies what 'signum negative zero' test is doing" $ show (1.0/(-0.0)) == "-Infinity"
assert "signum negative zero" $ show (1.0/(signum (-0.0))) == "-Infinity"

foreign import makeArray :: Int -> Array Int

testArrayBind :: AlmostEff
testArrayBind = do
assert "Array bind does not cause RangeError" do
let
_ = do
_ <- [unit]
makeArray 106_000
true

0 comments on commit 3843187

Please sign in to comment.