Skip to content

Implement source map support#2387

Merged
gbrail merged 6 commits into
mozilla:masterfrom
andreabergia:scratch/ab/source-maps-1
May 21, 2026
Merged

Implement source map support#2387
gbrail merged 6 commits into
mozilla:masterfrom
andreabergia:scratch/ab/source-maps-1

Conversation

@andreabergia
Copy link
Copy Markdown
Contributor

@andreabergia andreabergia commented Apr 28, 2026

Stacked on top of #2388. This is basically the internal implementation that we have had in ServiceNow for some years now, slightly cleaned up.


Adds an abstract SourceMapper interface. Callers attach a mapper via
ScriptCompileSpec/FunctionCompileSpec builders; the mapper is held
on CompilerEnvirons.

It is consulted during code generation in both backends (BodyCodegen
and CodeGenerator) so emitted line numbers refer to the original
source - i.e. this means that features such as debugger breakpoints or
stack traces come for free.

Parser error reporting and the debugger source handoff also use the
mapper. Null mappings cause the corresponding line entry to be skipped
in both LineNumberTable and Icode_LINE.

Tests cover both backends where applicable (via
Utils.runWithAllModes), although there are some interpreter-only
tests. InterpreterIcodeCapture was added to reduces duplication with
InterpreterBytecodeDumpTest.


The next step will be implementing a fully spec-compliant source mapper parser that uses the official test suite.

Copy link
Copy Markdown
Contributor

@aardvark179 aardvark179 left a comment

Choose a reason for hiding this comment

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

I think the code looks good, but could we get a couple of test cases to represent the most common sorts of map?

  1. A source map which represents minification, i.e. it has collapsed all source lines to one minified line.
  2. A source map which represents transpiration, i.e. it has expanded each line to potentially multiple lines, but not collapsed any lines.

I know we are currently limited by our line maps, and that's a good reason for source maps not being applied lazily, but I would be interested if you think there is a path to not evaluating them until they are really needs for either error reporting or debugging

* @param sourceLine 1-indexed line number in the original source
* @return the line text, or {@code null} if the line is out of range or unavailable
*/
String getSourceLineText(int sourceLine);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So, I'm trying to work out if this really makes sense. If you have an error in your original source then that should have been caught during whatever transformation process is being done, and if that transformation process introduced the error then that's the source you probably want to see.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, realistically I doubt that you'd get an error in Rhino if your code has gone through the transpiler already. But, this is the logic we have in our fork and I thought it was simple enough to be worth keeping.

@andreabergia andreabergia marked this pull request as ready for review April 29, 2026 14:12
Copy link
Copy Markdown
Contributor

@0xe 0xe left a comment

Choose a reason for hiding this comment

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

looks good!

@andreabergia andreabergia force-pushed the scratch/ab/source-maps-1 branch 2 times, most recently from cdf60e4 to e26ccce Compare May 7, 2026 11:50
@andreabergia
Copy link
Copy Markdown
Contributor Author

  1. A source map which represents minification, i.e. it has collapsed all source lines to one minified line.
  2. A source map which represents transpiration, i.e. it has expanded each line to potentially multiple lines, but not collapsed any lines.

Added

@andreabergia andreabergia force-pushed the scratch/ab/source-maps-1 branch from e26ccce to 26429dd Compare May 11, 2026 08:24
@andreabergia
Copy link
Copy Markdown
Contributor Author

The CI failure seems to be just an environmental transitory problem:

  • What went wrong:
    Execution failed for task ':rhino-kotlin:decycleMain'.
    A failure occurred while executing de.obqo.decycle.gradle.DecycleWorker
    Stream closed

@andreabergia andreabergia force-pushed the scratch/ab/source-maps-1 branch from 26429dd to d37b9a1 Compare May 12, 2026 09:38
@andreabergia andreabergia requested a review from aardvark179 May 12, 2026 10:29
Comment thread rhino/build.gradle
'org.mozilla.javascript.lc.type',
'org.mozilla.javascript.optimizer',
'org.mozilla.javascript.serialize',
'org.mozilla.javascript.sourcemap',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I realize I'm the one who did NOT want BND to manage both OSGi modules and Java modules, but I believe that if you're going to export this new package in OSGi, you should probably export it in module-info.java as well unless you're sure it'll never be used outside this module.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed that (and rebased)

Adds an abstract `SourceMapper` interface. Callers attach a mapper via
`ScriptCompileSpec`/`FunctionCompileSpec` builders; the mapper is held
on `CompilerEnvirons`.

It is consulted during code generation in both backends (`BodyCodegen`
and `CodeGenerator`) so emitted line numbers refer to the original
source - i.e. this means that features such as debugger breakpoints or
stack traces come for free.

Parser error reporting and the debugger source handoff also use the
mapper. Null mappings cause the corresponding line entry to be skipped
in both LineNumberTable and Icode_LINE.

Tests cover both backends where applicable (via
`Utils.runWithAllModes`), although there are some interpreter-only
tests. `InterpreterIcodeCapture` was added to reduces duplication with
`InterpreterBytecodeDumpTest`.
The test represents a minification of:

```js
var x = 1;
throw new Error("oops");
```

So it squeezes two lines into one.
@andreabergia andreabergia force-pushed the scratch/ab/source-maps-1 branch from d37b9a1 to 050e195 Compare May 20, 2026 07:12
@gbrail
Copy link
Copy Markdown
Collaborator

gbrail commented May 21, 2026

OK, thanks!

@gbrail gbrail merged commit 6a654c0 into mozilla:master May 21, 2026
11 checks passed
@andreabergia andreabergia deleted the scratch/ab/source-maps-1 branch May 21, 2026 09:31
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.

4 participants