antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.11k stars 3.28k forks source link

Javascript: Error.captureStackTrace is not a function in Firefox #2691

Open dhowe opened 4 years ago

dhowe commented 4 years ago

My antlr4 grammar/lexer/parser/visitor all work fine in the chrome browser and in the node runtime. When I use webpack to run my mocha tests in Firefox, however, I get the following error:

TypeError: Error.captureStackTrace is not a function
ParseCancellationException@webpack://module/./node_modules/antlr4/error/Errors.js?:157:8
LexerErrorHandler@webpack://module/./src/errors.js?:5:36
lex@webpack://module/./src/riscript.js?:51:33
lexParse@webpack://module/./src/riscript.js?:107:23
compile@webpack://module/./src/riscript.js?:32:33
evaluate@webpack://module/./src/riscript.js?:25:23
evaluate@webpack://module/./src/rita.js?:55:21
@http://localhost/tests/riscript-tests.js:81:19

My LexerErrorHandler extends ParseCancellationException for which (I think) the polyfill for Error.captureStackTrace is either missing or broken.

const Errors = require('antlr4/error/Errors');

function LexerErrorHandler() {
    Errors.ParseCancellationException.call(this);
    return this;
}

LexerErrorHandler.prototype = Object.create(Errors.ParseCancellationException.prototype);
LexerErrorHandler.prototype.constructor = LexerErrorHandler;
LexerErrorHandler.prototype.syntaxError = function(recognizer, offendingSymbol, line, column, msg, e) {
    throw Error("Lexer failed on line " + line + ":" + column + " " + msg);
};
ericvergnaud commented 4 years ago

Hi, could be that captureStackTrace is only available in Node.

dhowe commented 4 years ago

could be that captureStackTrace is only available in Node ?

I don't think so, as everything works fine in Chrome browser.

Plus antlr4/js is intended to run in the browser, so it should work (at least for modern browsers - and this is the current stable version Firefox, 70.0.1)

dhowe commented 4 years ago

Any status or update on this?

ericvergnaud commented 4 years ago

Can you try replicating for ParseCancellationException the RecognitionException constructor polyfill? i.e.: if (!!Error.captureStackTrace) { Error.captureStackTrace(this, RecognitionException); } else { var stack = new Error().stack; } (which relates to my November comment) (tbh not quite sure what to do with var stack)

dhowe commented 4 years ago

Sorry, not sure what you mean... you can see the file here. If there's a better way to do this, I'm happy to try it...

ericvergnaud commented 4 years ago

Yes, for now.

ericvergnaud commented 4 years ago

I mean change the antlr4 source code locally, until the fix is released

dhowe commented 4 years ago

that fixes the problem temporarily, but we are working in a node environment and every time we install dependencies, these changes are written over and need to be remade

isqua commented 1 year ago

could be that captureStackTrace is only available in Node ?

I don't think so, as everything works fine in Chrome browser.

It is available both in Node.js and Chrome(ium) Browser because it is the feature of the V8 engine.

Other JavaScript engines does not support this method. This method is not mentioned in the ECMAScript specs.

So I think it's better to remove usages of this method. Or check its existence before using it.

ericvergnaud commented 1 year ago

Please submit a PR?