Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 4, 2025

Fixed a NullReferenceException that occurred when using RegExp literal notation in getters parsed via Parser().ParseExpression(). The issue manifested in RegExpPrototype.RegExpBuiltinExec when the regex object was null.

Problem

When expressions containing regex literals are parsed individually (not as part of a full script preparation), the regex compilation step was skipped, leaving the Regex object uninitialized. This caused a null reference exception when the regex was later executed.

var engine = new Engine();
var obj = new JsObject(engine);
var getterFunction = (IFunction)new Parser().ParseExpression(
     "function() { return 'test'.match(/[a-z]/)?.[0]; }" // Previously threw NullReferenceException
);
var getter = new ScriptFunction(engine, getterFunction, true);
obj.FastSetProperty("name", new GetSetPropertyDescriptor(getter, null, true, false));

var result = engine.GetValue("customObj").AsObject().Get("name"); // Would fail here

Solution

Modified JintLiteralExpression.ResolveValue() to perform on-demand regex compilation when regExpParseResult.Regex is null. The fix uses the same Tokenizer.AdaptRegExp() method that's used in RegExpConstructor to ensure consistent behavior.

The solution:

  1. Checks if the regex object is null after parsing
  2. Compiles the regex using the engine's parser options if needed
  3. Maintains proper error handling for invalid regex patterns
  4. Preserves all existing functionality for prepared scripts and direct evaluation

Testing

  • Original failing case now works correctly
  • All existing regex functionality remains unaffected
  • Complex regex patterns (global, ignoreCase, multiline, named groups, unicode) work properly
  • Both literal notation (/pattern/flags) and constructor notation (new RegExp()) continue to work
  • Edge cases with invalid patterns are handled appropriately

Fixes #2161.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Using RegExp expressions in literal notation in getters results in NullReferenceException Fix RegExp literal NullReferenceException in getters parsed via ParseExpression Aug 4, 2025
Copilot finished work on behalf of sebastienros August 4, 2025 19:41
@Copilot Copilot AI requested a review from sebastienros August 4, 2025 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using RegExp expressions in literal notation in getters results in NullReferenceException
2 participants