Fixing Native Stack Depth Error in Hyperformula Integration with React Native and HermesJS #1286
Replies: 3 comments 1 reply
-
That's an interesting case there, @vmalvaro The first question that arises is why the limit is so low? It seems to be blocking developers in many different occasions cause if not HyperFormula they may be blocked somewhere else. The best idea would be to raise the limit or let developers decide how deep they want to go. Nevertheless, I think that we'd need to get @sequba 's feedback here. At the first glance, I do not see an easy way to exclude Chevrotain from the equation. Would it be an issue to get back to you within ~3weeks? |
Beta Was this translation helpful? Give feedback.
-
Thanks for your quick response @AMBudnik, yes, am not sure why it's so low and it's a bit of a pain 😞 I've brought up the issue with Meta's dev team (see facebook/hermes#135 (comment)). They have suggested building a custom engine with a higher value, but it appears to be a complex task to integrate it with React Native and keep it up to date. While it might be challenging to bypass Chevrotain, I wonder if there are any alternative solutions or workarounds. Perhaps updating Chevrotain to the latest version could help, although I'm not entirely convinced. Another issue we are facing is the overuse of call/apply in Chevrotain, which exacerbates the problem of the low limit of native calls. Currently, we have had to limit the depth of nested parentheses to only 2 to avoid hitting the hard limit of 128. The only solution I have found so far is to monkey patch Chevrotain and reduce the usage of the call/apply function. This has allowed me to increase the limit to 8 nested parentheses, but I am still in the testing phase. I attempted to examine Chevrotain more closely, and it seems that the main issue lies within their recognize_engine.js file, specifically when calling defineRule. I believe this is related to Hyperformula's usage of Rule/SubRule options, although I'm not entirely sure. |
Beta Was this translation helpful? Give feedback.
-
Hello @vmalvaro
ChevrotainI'm not sure if Chevrotain can (or should) be changed for this edge case. In regards to the grammar:
It seems to be highly recursive as it seems to use rules hierarchy to define operator precedence. It may be possible to re-factor the grammar to parse the binary expressions as a flat list
However, such a re-factor may be a large undertaking, particularly considering that the grammar is using embedded actions and is not a "pure" grammar. Another benefit would likely be a large performance boost, but I do not know if hyperFormula needs better performance as the use case seems to be about very small inputs. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm currently working on implementing Hyperformula in React Native with the HermesJS engine.
Overall, it works ok, but I'm encountering an error with some formulas. I would greatly appreciate it if you could assist me or provide any ideas/suggestions for a workaround or fix for this issue.
In React Native, many of the standard JavaScript functions such as
map, apply, call, and others
are converted into native calls. With HermesJS, there is a limitation on the number of native stack calls that can be made at any given time, and this limit is relatively low (128) compared to engines like Chrome V8.The problem arises when attempting to parse a formula in Hyperformula using either the
calculatingFormulas
orsetSheetContent
functions, and the formula contains more than two levels of nested parentheses.For example, formulas like
=(((8)))
or=(A1*B2+((C2/10)/10)/10)
would result in aRangeError: Maximum call stack size exceeded (native stack depth)
.This issue occurs because Chevrotain parsing relies on recursive functions that use
apply
andcall
multiple times, leading to the exceeding of the native stack depth.You can see the code here:
https://github.com/Chevrotain/chevrotain/blob/d8593c78e919f8f03fce04c2c4860a3022af7e4f/packages/chevrotain/src/parse/parser/traits/recognizer_engine.ts#L245
and
https://github.com/Chevrotain/chevrotain/blob/d8593c78e919f8f03fce04c2c4860a3022af7e4f/packages/chevrotain/src/parse/parser/traits/recognizer_engine.ts#LL225C23-L225C23
There is a way to avoid Chevrotain doing this many recursive iterations, or to parse the formula in a different way so I can avoid hitting the limits?
Here is a repo with an example on when it fails:
https://github.com/vmalvaro/hermes-range-error
This is some HermesJS similar issues for more context:
facebook/hermes#135 (comment)
facebook/hermes#135 (comment)
Thanks for your help!!
Beta Was this translation helpful? Give feedback.
All reactions