regexp literal after for/if/while header causes SyntaxError in js_parse_skip_parens_token

Open
#620 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 5 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c, javascript
Domain
compilers

Research direction

Start by reading js_parse_skip_parens_token(), is_regexp_allowed(), and js_parse_regexp() in the parser; the issue identifies these as the paths involved in the incorrect lookahead and pending exception. Reproduce with the supplied snippets and trace how parsing handles / after each listed header. Done means all snippets parse and the IIFE leaves no pending syntax error.

Written by the indexing model from the issue text.

Description

var f1 = (fn = function(e){ for (var r in e) /data-/gi.test(r); var s = "#"; }) => fn;
var f2 = (fn = function(){ for (var x in {a:1}) /[)]/.test(x); }) => fn;
var f3 = (fn = function(){ if (1) /[)]/.test(")"); }) => fn;
var f4 = (fn = function(){ while (0) /[)]/.test(")"); }) => fn;
var f5 = (fn = function(){ with ({}) /[)]/.test(")"); }) => fn;
var f6 = (fn = async function(){ for await (var x of [1]) /[)]/.test(")"); }) => fn;

All the valid snippets above fail to parse with SyntaxError: expecting ';'. In addition, for an IIFE like:

(function(e){ for (var r in e) /data-/gi.test(r); var s = "#"; })({a:1});

JS_Eval() succeeds, but JS_GetException(ctx) still returns a pending SyntaxError: unexpected end of regexp.

The root cause is that is_regexp_allowed(')') always returns FALSE inside js_parse_skip_parens_token(). However, after the closing ) of a for (...), for await (...), if (...), while (...), or with (...) header, a statement follows and / starts a regexp literal rather than division. Treating / as division breaks paren matching during lookahead and also leaves an uncleared exception in rt->current_exception when js_parse_regexp() fails. Could you help look into fixing this?

Dominant language
C
Stars
11.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bellard/quickjs

All issues in bellard/quickjs

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.