Skip to content

[jsifier] Share (and fix) function-matching regexes - #28288

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:jsifier-arrow-re
Nov 18, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:jsifier-arrow-re

Conversation

@sbc100

@sbc100 sbc100 commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Extract shared module-level arrowFuncRe and regularFuncRe regexes in
src/jsifier.mjs and use both for stringified function detection and
mangled name emission.

Previously:

  • Both arrow regexes used [^}]*, failing on destructured parameters
    (e.g. ({a}) => ...), omitting single-arg (including $ and async
    as a parameter) and async arrow forms (including async(x) => ...),
    and potentially misclassifying regular functions with default arrow
    parameters.
  • Stringified function detection (/^function\b/) did not allow
    leading whitespace or async, and did not share the regular-function
    name-replacement regex (regularFuncRe), which now anchors at ^,
    requires (, and preserves leading whitespace and async.
  • Stringified function detection lacked a !isConst guard, risking
    treating __const string literals like x => ... or function(...)
    as functions.

Also assert that regularFuncRe matches in the regular-function
fallback, and convert $find_closing_parens_index in
src/lib/libc_preprocessor.js from an arrow function to a regular
function so its closing=')' default parameter doesn't trip
arrowFuncRe's [^)]* parameter match.

Add test_jslib_function_forms in test/test_jslib.py covering these
cases.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a centralized regular expression, "arrowFuncRe", to identify arrow function signatures in JS library snippets and replaces inline regex matches with this centralized pattern. It also adds a test suite to verify various arrow function patterns. The review feedback recommends improving the "arrowFuncRe" regex to support identifiers containing "$" and to handle "async" arrow functions without spaces before the parameter list. Additionally, it is suggested to allow leading whitespace in the "function" keyword check to prevent false negatives.

Comment thread src/jsifier.mjs Resolved
Comment thread src/jsifier.mjs Resolved
@sbc100
sbc100 requested a review from kripken November 17, 2026 16:51
@sbc100
sbc100 force-pushed the jsifier-arrow-re branch 2 times, most recently from 6d8271a to 35445e5 Compare November 17, 2026 17:42
@sbc100 sbc100 changed the title [jsifier] Share and fix arrow function regex [jsifier] Share regex for regular (non-arrow) functions Nov 17, 2026
@sbc100 sbc100 changed the title [jsifier] Share regex for regular (non-arrow) functions [jsifier] Share and fix arrow and regular function regexes Nov 17, 2026
@sbc100 sbc100 changed the title [jsifier] Share and fix arrow and regular function regexes [jsifier] Share (and fix) function-matching regexes Nov 17, 2026
// Finds the index of closing parens from the opening parens at arr[i].
// Used polymorphically for strings ('foo') and token arrays (['(', 'foo', ')']) as input.
$find_closing_parens_index: (arr, i, opening='(', closing=')') => {
$find_closing_parens_index: function(arr, i, opening='(', closing=')') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this had to change, or was this just a drive-by of some kind?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, if we left this as an arrow function it would be beyond the power of the regex to solve (since there is an embedded closing brace inside a string, which confused the regex).

Changing it to use function works around it, and also matches the function just below.

If anyone tries to change it back we now have an assert that would catch it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a paragraph about this in the PR description.

I asked Gemini to find a way to make it work, and it restored to using acorn to actually parse the JS.

Extract shared module-level `arrowFuncRe` and `regularFuncRe` regexes in
`src/jsifier.mjs` and use both for stringified function detection and
mangled name emission.

Previously:
- Both arrow regexes used `[^}]*`, failing on destructured parameters
  (e.g. `({a}) => ...`), omitting single-arg (including `$` and `async`
  as a parameter) and `async` arrow forms (including `async(x) => ...`),
  and potentially misclassifying regular functions with default arrow
  parameters.
- Stringified `function` detection (`/^function\b/`) did not allow
  leading whitespace or `async`, and did not share the regular-function
  name-replacement regex (`regularFuncRe`), which now anchors at `^`,
  requires `(`, and preserves leading whitespace and `async`.
- Stringified function detection lacked a `!isConst` guard, risking
  treating `__const` string literals like `x => ...` or `function(...)`
  as functions.

Also assert that `regularFuncRe` matches in the regular-function
fallback, and convert `$find_closing_parens_index` in
`src/lib/libc_preprocessor.js` from an arrow function to a regular
`function` so its `closing=')'` default parameter doesn't trip
`arrowFuncRe`'s `[^)]*` parameter match.

Add `test_jslib_function_forms` in `test/test_jslib.py` covering these
cases.
@sbc100
sbc100 merged commit fa6110f into emscripten-core:main Nov 18, 2026
19 of 25 checks passed
@sbc100
sbc100 deleted the jsifier-arrow-re branch November 18, 2026 18:04
sbc100 added a commit that referenced this pull request Nov 23, 2026
Followup to #28286
Depends on #28288

Use object and parameter destructuring for multi-property reads and
unused event parameters across `src/lib/` and
`src/pthread_esm_startup.mjs`, using `const` for declarations so
Closure Compiler lowers them cleanly without extra bindings.

Update affected `test/codesize/` expectations.

See: #28272
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.

2 participants