Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:A9T9 RPA JS Interpreter Step Loop

From Leeroopedia
Knowledge Sources
Domains Browser_Automation, Script_Execution
Last Updated 2026-10-22 17:48 GMT

Overview

JS-Interpreter sandbox runs user macros one synchronous step at a time, yielding to the browser event loop between steps to keep the UI responsive and enable pause/breakpoint support.

Description

The Ui.Vision JS macro engine wraps user code in the JS-Interpreter sandbox, which interprets JavaScript step-by-step. The run loop calls interpreter.step() repeatedly, but between steps it yields to the browser's event loop (via setTimeout or microtask scheduling). This prevents the content script or service worker from becoming unresponsive during long-running macros. The async bridge (__uiv_bridge) suspends the interpreter at await points — the callback MUST always fire, even on rejection, or the interpreter stays suspended forever with no error. The __uiv_pause function handles uiv.pause() / uiv.sleep() the same way.

Usage

Use this heuristic when running JS script macros (uiv.* API) in the content script or background service worker. The step loop is the core execution model — it enables pause, breakpoints, and cancellation by simply stopping the step() calls. Any async operation inside a macro must go through the bridge to avoid deadlocking the interpreter.

The Insight (Rule of Thumb)

  • Action: Run interpreter.step() in a loop with event-loop yields between steps; route all async operations through __uiv_bridge (async function in the interpreter).
  • Value: The bridge callback must always fire, even on rejection — return { ok: false, error: ... } instead of throwing natively.
  • Trade-off: Step-by-step interpretation is slower than native execution, but enables pause/breakpoints and keeps the UI responsive.
  • Compatibility: Works in both content script and background SW contexts. The interpreter is initialized with PARSE_OPTIONS.locations = true for line-number highlighting in error messages.
  • Never throw natively: A native throw escapes step() and kills the run uncatchably. Always return { ok: false, error: ... } via the bridge.

Reasoning

The JS-Interpreter sandbox cannot run native await — it suspends at async function callbacks. If the callback never fires (e.g., a rejected bridge promise), the interpreter hangs silently with no error anywhere. The bridge wrapper catches all errors and converts them to { ok: false, error: ... } objects that the polyfill turns into script errors the user can see and handle.

The __uiv_get function follows the same pattern: it returns { ok: false, error: ... } for unknown variables instead of throwing, because a native throw escapes step() and kills the run uncatchably. The previous behavior (returning undefined for every failure) made a typo'd !TIMEOOUT_WAIT indistinguishable from a genuinely unset !STATUSOK, surfacing as NaN further down.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment