In my library (Scheme in JavaScript) I have a test that should fail because it calculates (! 1000) and it should pass when TCO (tail call optimization) is implemented. But recently NodeJS probably increased the stack size limit because the test passes which means it failed to fail.
Is there a way to make stack size smaller in NodeJS? So I don't need to increase the value of the factorial function to overflow the stack.
EDIT: note that --stack-size
option discussed on GitHub may crash node. I would like to have a stack overflow error but for a smaller value than 1000.
CodePudding user response:
You can use the --stack-size
V8 option (see also here).
To see the default size, you can check --v8-options
:
$ node --v8-options | grep -B0 -A1 stack-size
--stack-size (default size of stack region v8 is allowed to use (in kBytes))
type: int default: 984
(I don't know of any way to read the default or current value programmatically other than invoking and parsing node --v8-options
.)
You can then specify a new stack size with that option:
$ node --stack-size=512 index.js
You can also set this from the program itself using v8.setFlagsFromString
:
const v8 = require('v8');
v8.setFlagsFromString('--stack-size=512');
Note that the available V8 options may change in the future in newer versions of V8 used in node.js. (For example I believe this option was called stack_size
with an underscore before, but now it's stack-size
with a dash.) You could use the v8flags
package package or programmatically invoke node --v8-options
to raise an error if the option no longer exists so you can adjust the test.