The JavaScript engine SpiderMonkey used in Firefox for utilizing an updated implementation of regular expressions, based on current code from the V8 JavaScript engine used in Chromium-based browsers. The new RegExp implementation will be proposed in Firefox version 78, scheduled for June 30, and will enable the browser to implement all the missing ECMAScript features related to regular expressions.
It is noted that the RegExp engine in SpiderMonkey is designed as a separate component, making it relatively independent and suitable for replacement without the need for significant changes to the codebase. Modularity allowed for the 2014 replacement of the originally used RegExp engine YARR in Firefox with a fork of the Irregexp engine from V8. Irregexp is tied to the V8 API, linked to the garbage collector, and utilizes V8-specific string representation and object model. During the adaptation to SpiderMonkey's internal API in 2014, the Irregexp engine was partially rewritten, and emerging changes, such as the '\u' flag, were carried over whenever possible to a branch supported by Mozilla.
Unfortunately, maintaining a synchronized fork is a challenging task and requires substantial resources. With the introduction of new features related to regular expressions in the ECMAScript 2018 standard, Mozilla developers considered how to simplify the transfer of changes from Irregexp. As a solution, the concept of wrapping was proposed, allowing SpiderMonkey to use the Irregexp engine almost unchanged (the changes are limited to automatic replacement of ‘#include’ blocks).
The wrapper provides Irregexp with necessary V8-specific capabilities, including memory management and code generation functions, as well as the original data structures implemented based on SpiderMonkey's memory management, code generators, and structures.
The update to the RegExp engine will ensure support in Firefox for features such as named captures, escaping of Unicode character classes, the dotAll flag, and Lookbehind mode:
- Escaping classes
- Unicode characters are added using the constructs \p{…} and \P{…}, for instance, \p{Number} defines all possible characters representing digits (including symbols like ①), \p{Alphabetic} — letters (including hieroglyphs), \p{Math} — mathematical symbols, and so on.
- Flag causes the `.` mask to match newline characters as well.
- Mode allows you to specify in a regular expression that one pattern precedes another (for example, to match a dollar amount without capturing the dollar sign).
The project was carried out with the participation of V8 developers, who worked on reducing Irregexp's dependency on V8 and moved some features that cannot be implemented on the SpiderMonkey base into optional blocks '#ifdef'. The cooperation turned out to be mutually beneficial. On their side, Mozilla developers contributed changes to Irregexp that eliminated some with the JavaScript standard and the code quality. Also, during fuzz testing of Firefox, previously unnoticed bugs in the Irregexp code that led to crashes were identified and fixed.
Source: opennet.ru
