Skip to content

Replace embedded source code with global lookup table#22

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-dont-embed-source-code
Draft

Replace embedded source code with global lookup table#22
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-dont-embed-source-code

Conversation

Copy link

Copilot AI commented Nov 18, 2025

The compiler was embedding Python source code directly into generated JavaScript via $currSource variable assignments, bloating output size linearly with program length.

Changes

Source storage (src/misceval.js):

  • Added Sk.sourceMaps global registry indexed by filename
  • Added Sk.registerSourceMap(filename, source) to store source during compilation
  • Added Sk.getSourceLine(filename, lineno) for runtime lookup

Code generation (src/compile.js):

  • Removed $currSource variable declarations and assignments from all generated code
  • Updated annotateSource() to emit only position info (line/column)
  • Simplified $saveSuspension signature to remove source parameter
  • Modified Sk.compile() to register source in global map instead of passing to compiler

Error handling (src/misceval.js):

  • Updated handleTraceback() to lookup source using filename + line number
  • Removed source storage from injectSusp()

Before/After

Before:

$currLineNo = 1; $currColNo = 0;
Sk.currFilename = $fname;
$currSource = "print(\"Hello, World!\")";  // Embedded on every line

After:

$currLineNo = 1; $currColNo = 0;
Sk.currFilename = $fname;  // Source looked up from Sk.sourceMaps[fname][1]

Error tracebacks continue to display source correctly via runtime lookup.

Original prompt

This section details on the original issue you should resolve

<issue_title>Don't embed source code</issue_title>
<issue_description>Currently the source code is annotated using a clunky approach that embeds the source of the python directly into the JavaScript. Instead, the compiler should use fast static lookup tables (assuming they actually make the strings smaller) by storing a copy of the python code that is suitably compressed in a file of the same name but with the py extension in addition to the js file that we create. We will need to keep track of the position instead of the entire source inline, and look up what the corresponding line of python code is. Look at the annotateSource function in compile.js, along with the parser information.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Add global source map storage (Sk.sourceMaps)
- Register source code during compilation instead of embedding
- Update annotateSource to not embed source in generated JS
- Modify handleTraceback to lookup source from source map
- Remove $currSource variable from generated code
- Update all suspension handling to not pass source
- Tests still pass (553/563)

Co-authored-by: acbart <897227+acbart@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix embedding of source code in JavaScript Replace embedded source code with global lookup table Nov 18, 2025
Copilot AI requested a review from acbart November 18, 2025 05:54
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.

Don't embed source code

2 participants