Hello, and thanks for maintaining this library!
Curious, what is the purpose of the peg$subclass in the generated parser, and why does it modify the child's prototype?
function peg$subclass(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor();
}
I develop JavaScript apps on the Salesforce platform, and a recent security change enables Salesforce customers to freeze javascript prototypes. When customers enable that setting, then the peg$subclass function is blocked from modifying the child's prototype and the parser code won't load in the browser.
For my personal use, I've simply commented out those three lines so the peg$subclass function is empty and I'm able to use the parser on the Salesforce platform.
I've not noticed any negative side effects yet. The generated parser still seems to behave correctly, validating when good syntax is provided to it and flagging when bad syntax is provided to it. And that got me wondering why the function exists in the first place and if it's still necessary?
Thanks!
For reference, I'm indirectly using pegjs by using this other library, stomita/soql-parse.
It's needed when you want to confirm an instance of Error has been thrown, and before ES2015 (the parser is generated in ES5) this was the best way (using a function like peg$subclass that is) to make sure peg$SyntaxError (exported as parser.SyntaxError) is recognisable as an instance of Error.
It's probably going to stay around for some time, but it's probably a good idea to add an option to enable/disable this feature. I'll try to do this before releasing 0.11, and close this issue then.
Thanks for the quick reply and informative answer.
Most helpful comment
It's needed when you want to confirm an instance of
Errorhas been thrown, and before ES2015 (the parser is generated in ES5) this was the best way (using a function likepeg$subclassthat is) to make surepeg$SyntaxError(exported asparser.SyntaxError) is recognisable as an instance ofError.It's probably going to stay around for some time, but it's probably a good idea to add an option to enable/disable this feature. I'll try to do this before releasing
0.11, and close this issue then.