Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 27, 2025

This PR implements document-order script execution for HTMLScriptElement to match classic browser behavior, where scripts execute in the order they appear in the DOM regardless of download completion order.

Problem

Previously, JSAR executed scripts immediately upon compilation completion, which could result in out-of-order execution if later scripts downloaded faster than earlier ones. This behavior differs from standard browser implementations and can break web content that relies on strict script execution order.

Solution

Introduced a script execution queue in BrowsingContext that ensures classic scripts (those without async, defer, or type="module") execute in document order:

<!-- These scripts will now execute in order a → b → c -->
<!-- regardless of which downloads first -->
<script src="script-a.js"></script>
<script src="script-b.js"></script> 
<script src="script-c.js"></script>

<!-- These remain unaffected by the queue -->
<script src="async.js" async></script>        <!-- Executes when ready -->
<script src="defer.js" defer></script>        <!-- Executes after parsing -->
<script type="module" src="module.js"></script> <!-- Module order rules -->

Implementation Details

  • Decoupled Architecture: Uses ScriptExecutionHandle with script IDs and weak pointers to avoid circular dependencies between BrowsingContext and HTMLScriptElement
  • Queue Management: Added std::deque<ScriptExecutionHandle> to track scripts in document order
  • Registration: Classic scripts register with the queue in connectedCallback()
  • Execution Control: Scripts wait for their turn instead of executing immediately after compilation
  • Minimal Impact: Only affects classic scripts; async, defer, and module scripts retain their existing behavior

Key Features

  • Document Order Preservation: Scripts execute in DOM order regardless of download speed
  • Asynchronous Loading: Network requests remain parallel for performance
  • Selective Application: Only classic scripts use the queue; other script types unchanged
  • Browser Compatibility: Matches standard web browser execution behavior
  • Clean Architecture: Avoids circular dependencies through ID-based handles

Files Changed

  • src/client/dom/browsing_context.hpp - Added execution queue interface with decoupled design
  • src/client/dom/browsing_context.cpp - Implemented queue management logic using script handles
  • src/client/html/html_script_element.hpp - Added queue integration methods
  • src/client/html/html_script_element.cpp - Modified execution flow to use queue

This change improves web compatibility by ensuring JSAR's script execution behavior aligns with browser standards while maintaining a clean, decoupled architecture.

Fixes #250.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 27, 2025 12:13
Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
…tation

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copilot AI changed the title [WIP] Support blocking execution order for HTMLScriptElement (document-order script execution) Implement blocking script execution order for HTMLScriptElement Aug 27, 2025
Copilot AI requested a review from yorkie August 27, 2025 12:18
- Use ScriptExecutionHandle with IDs and weak pointers instead of direct HTMLScriptElement references
- Remove test file and documentation as requested
- Maintain same functionality with better decoupling

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 27, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: npm ci (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from yorkie August 27, 2025 12:43
@EndlessJour9527 EndlessJour9527 marked this pull request as ready for review August 28, 2025 06:51
@yorkie yorkie merged commit d88d7ae into main Aug 28, 2025
2 checks passed
@yorkie yorkie deleted the copilot/fix-250 branch August 28, 2025 17:06
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.

Support blocking execution order for HTMLScriptElement (document-order script execution)

3 participants