tolmasky / language

A fast PEG parser written in JavaScript with first class errors
languagejs.com
MIT License
411 stars 48 forks source link

Fixed RegExp clone #9

Closed aparajita closed 13 years ago

aparajita commented 13 years ago

Safari doesn't like specifying flags when cloning a RegExp.

tolmasky commented 13 years ago

The problem actually seems to be that typeof RegExp === "object" in Safari but === "function" in v8 (how can they disagree on this???). So we should instead be doing if (typeof rule[1].valueOf() === "string")...

aparajita commented 13 years ago

You can test the type more precisely by doing this:

if (rule[1].constructor === RegExp)

I'm pretty sure this works in all browsers. Not sure about CommonJS though.

tolmasky commented 13 years ago

I pushed this for now, should suffice for the time being (there's a major parser change coming soon anyways): https://github.com/tolmasky/language/commit/d630c078b3aeaa5e5d4da92bb5e6c68d6c733749

michaelficarra commented 13 years ago

how can they disagree on this

It's actually not specified. Since v8 implements [[Call]] for RegExp instances, typeof should produce "function", but Safari doesn't have this internal value for RegExp instances (which is perfectly okay, that's the unspecified part), so the spec for typeof says that is should produce "object". See 11.4.3. It should also be noted that v8 has recently removed that functionality for RegExps.