tunnelvisionlabs / antlr4ts

Optimized TypeScript target for ANTLR 4
Other
624 stars 106 forks source link

Unexpected exception in CodePointCharStream #501

Closed mike-lischke closed 2 years ago

mike-lischke commented 3 years ago

Apparently there's a size limit what String.fromCharCode can handle and that can lead to an exception which is totally unexpected, because the given range is correct.

When calling CharStream.getText(range) the call reaches through to CodePointCharStream.getText(). With a large enough range the following code crashes:

Bildschirmfoto 2021-04-03 um 13 53 06

The numbers below show the current values passed in and the maximum number of values that String.fromCharCode can handle without problems (119886). One more and you get:

String.fromCharCode(...Array.from(this._array.subarray(0, 119887))) Uncaught RangeError: Maximum call stack size exceeded

The stream size is 140697.

I didn't see any mentioning of this problem on MDN, so I'm not sure if that is an inherent problem with JS String or has other reasons. I can certainly handle the range error, but since the given range is valid, this makes me wonder, if there shouldn't be a central solution in the code point char stream.

mike-lischke commented 3 years ago

Apparently this is a known problem, but unfortunately not mentioned on MDN, which is why I didn't saw possible solutions until now. Here's one: https://stackoverflow.com/questions/38432611/converting-arraybuffer-to-string-maximum-call-stack-size-exceeded