we (web engine): Experimental web browser project to understand the limits of Claude

Implement script element loading and execution #285

Open pierrelf.com
targeting main
Open opened by and assigned to pierrelf.com
targeting main

Implements issue 3mhyfp5om4p26 — Script element loading and execution (defer, async).

Changes #

New: script_loader module (crates/browser/src/script_loader.rs) #

  • Walks the DOM in document order for <script> elements after HTML parsing
  • Executes inline scripts and fetches external scripts via ResourceLoader
  • All scripts share a single JS VM with full DOM access (attach_document)
  • Respects type attribute: only executes text/javascript or no-type scripts
  • defer external scripts are queued and executed after all synchronous scripts
  • async scripts execute immediately (no ordering guarantee)
  • Script errors are caught and logged without crashing the page
  • After execution, document is detached from VM and returned for rendering

JS compiler: top-level var stores to global scope #

  • Top-level var declarations now also emit StoreGlobal, matching function declaration behavior. This ensures variables persist across separate execute() calls when multiple scripts share a VM.

VM: detach_document() method #

  • Reclaims the Document from the DOM bridge after script execution
  • Enables the pattern: parse → attach → execute scripts → detach → render

Resource loader: Script variant #

  • Added Resource::Script for JavaScript MIME types
  • Updated MIME classifier with text/javascript, application/javascript
  • Exhaustive match updates in css_loader and img_loader

Integration in main.rs #

  • load_page() now calls execute_page_scripts() between HTML parsing and CSS/image collection, so scripts can modify the DOM before styling.

Tests #

9 new tests covering:

  • Inline script execution with DOM modification
  • Multiple scripts sharing global scope
  • Unknown type attribute prevents execution
  • type="text/javascript" executes correctly
  • Script errors don't prevent subsequent scripts
  • Empty scripts don't crash
  • defer inline scripts (treated as sync per spec)
  • createElement/appendChild from scripts
  • Pages with no scripts pass through unchanged

This pull request has no versions submitted yet.