Merge pull request #9406 from dotCooCoo/fix/realm-replaces-paused-run

End paused JavaScript runs before compiling replacement source
This commit is contained in:
BFlat
2026-11-27 00:51:46 +07:00
committed by GitHub
4 changed files with 97 additions and 0 deletions
+90
View File
@@ -75,6 +75,96 @@ fn an_ended_run_leaves_the_realm_ready_for_the_next() {
r.end();
}
fn paused_for_replacement(promise: bool) -> Realm {
let mut r = Realm::with_options(Options { es2015: promise, ..Options::default() }, Limits::default());
let pause = r.pause_flag();
r.define_function("pauseNow", move |_| {
pause.set(true);
Ok(JsValue::Undefined)
});
let source = if promise {
"var kept = 'yes', after = false, queued = false;\n\
Promise.resolve().then(function () { pauseNow(); after = true; });\n\
Promise.resolve().then(function () { queued = true; }); 'old'"
} else {
"var kept = 'yes', after = false, queued = false; pauseNow(); after = true; 'old'"
};
r.start(source).unwrap();
assert_eq!(r.advance(1_000_000).unwrap(), Progress::Paused);
assert!(r.is_running());
if promise {
assert!(r.interp().promises.draining);
}
r
}
fn replacement(r: &mut Realm, sliced: bool, source: &str) -> Result<JsValue, RunError> {
if sliced {
r.start(source)?;
finish(r, 1_000).0
} else {
r.run(source)
}
}
#[test]
fn replacing_a_paused_promise_run_discards_its_reaction_and_queued_jobs() {
for sliced in [false, true] {
let mut r = paused_for_replacement(true);
assert_eq!(replacement(&mut r, sliced, "'new'").unwrap(), JsValue::String("new".into()));
assert!(!r.is_running());
assert_eq!(r.run("[kept, after, queued].join('|')").unwrap(), JsValue::String("yes|false|false".into()));
}
}
#[test]
fn replacing_a_paused_script_discards_its_remaining_code() {
for sliced in [false, true] {
let mut r = paused_for_replacement(false);
assert_eq!(replacement(&mut r, sliced, "'new'").unwrap(), JsValue::String("new".into()));
assert!(!r.is_running());
assert_eq!(r.run("[kept, after, queued].join('|')").unwrap(), JsValue::String("yes|false|false".into()));
}
}
#[test]
fn replacing_a_waiting_run_discards_its_host_call() {
for sliced in [false, true] {
let mut r = realm(1_000_000);
let calls = with_ask(&mut r);
r.start("var after = false; ask('old'); after = true").unwrap();
assert_eq!(r.advance(1_000).unwrap(), Progress::Paused);
assert!(r.waits());
assert_eq!(replacement(&mut r, sliced, "'new'").unwrap(), JsValue::String("new".into()));
assert!(!r.waits());
assert_eq!(r.run("after").unwrap(), JsValue::Bool(false));
assert_eq!(calls.get(), 1);
assert!(r.answer(Data::Null).is_err());
}
}
#[test]
fn replacing_a_paused_run_with_invalid_source_still_ends_it() {
for promise in [false, true] {
for sliced in [false, true] {
let mut r = paused_for_replacement(promise);
assert_eq!(replacement(&mut r, sliced, "(").unwrap_err().kind, RunErrorKind::Syntax);
assert!(!r.is_running());
assert_eq!(r.run("[kept, after, queued].join('|')").unwrap(), JsValue::String("yes|false|false".into()));
}
}
}
#[test]
fn replacing_a_paused_run_applies_its_pending_language_setting() {
for sliced in [false, true] {
let mut r = paused_for_replacement(false);
r.set_es2015(true);
assert_eq!(replacement(&mut r, sliced, "let fresh = 7; fresh").unwrap(), JsValue::Number(7.0));
assert_eq!(r.run("after").unwrap(), JsValue::Bool(false));
}
}
/// Setting `Folder.current` back for the next run drops what only the last run's uncaught
/// exception held, so a full heap from that exception does not stop the next run.
#[test]
+3
View File
@@ -277,6 +277,7 @@ impl Realm {
/// Runs `source` as a script in the global environment, then the promise jobs it queued, and
/// returns its completion value (the value of the last expression statement that ran). A realm
/// whose built-ins could not be installed runs nothing and gives an internal error.
/// A paused or waiting run ends first; its globals remain available.
pub fn run(&mut self, source: &str) -> Result<JsValue, RunError> {
let code = self.compile(source)?;
let result = self.interp.run_global(code, false).and_then(|v| self.interp.drain_jobs(v));
@@ -285,6 +286,7 @@ impl Realm {
/// Compiles `source` and readies the realm for a run.
fn compile(&mut self, source: &str) -> Result<std::rc::Rc<crate::bytecode::Code>, RunError> {
self.end();
self.script_tag = UNKNOWN_SOURCE;
self.rejections = (Vec::new(), 0);
let interp = &mut self.interp;
@@ -391,6 +393,7 @@ impl Realm {
}
/// Begins a run that [`Realm::advance`] carries on: compiles `source` and enters it.
/// A paused or waiting run ends first; its globals remain available.
pub fn start(&mut self, source: &str) -> Result<(), RunError> {
let code = self.compile(source)?;
match self.interp.enter_global(code, false) {
+2
View File
@@ -2901,6 +2901,8 @@ own rules:
- The callbacks `then`, `catch` and `finally` register run as jobs, one at a time, in the order they were queued, after
the script's own code ends and before the run ends; a job a callback queues runs after the jobs already waiting.
A promise still pending when no job is left stays pending, and the run ends.
- In the host API, `Realm::run` and `Realm::start` end a paused or waiting run before compiling the new source.
The earlier run's globals remain available; its remaining code and queued jobs do not run.
- The jobs run on the run's fuel and limits, and each job takes 1 unit of fuel. A chain of callbacks that never ends
stops at the fuel limit. More than 1,048,576 jobs waiting at once end the run with the limit error
`limit reached: Jobs (more than 1048576 promise jobs were waiting to run)`. A run that ends with an error, a limit or
+2
View File
@@ -32,6 +32,8 @@
## Feature submission evidence
`vectorcraft-js` ends a paused or waiting run before `Realm::run` or `Realm::start` compiles new source. Its globals remain available. The earlier run's remaining code and queued jobs do not run. Regressions in `crates/js/src/host_tests.rs` cover paused scripts, Promise reactions, waiting host calls, invalid replacement source, and deferred language settings.
History panel row context menus (2026-11-20, G12): Right-clicking any state offers New Document from State, Step to This State, and Clear History. Creating a document uses the clicked snapshot without stepping the source document; Step to This State uses undo/redo and is disabled on the current row; Clear History removes undo and redo steps without changing artwork and is disabled when history is empty. Headless tests in `crates/ui-egui/src/panels/history.rs` cover opening an earlier row menu, snapshot creation, backward/forward jumps, and clearing history. The snapshot chosen when the menu opens is retained while it stays open; pending tool edits are settled before resolving that snapshot to its new row; a discarded snapshot reports that it is unavailable instead of acting on a neighboring row. Jumps settle pending typing before counting undo/redo steps. Shift+F10 opens the focused row menu; Escape dismisses it and restores row focus. Switching documents dismisses the menu before it can act on another document. Headless tests cover these keyboard and document-switch paths. The list and menu use shared Craft UI controls. Scores and estimates unchanged.
Script appearance and geometry setters reject objects on locked artboards. Assigning the current opacity remains allowed when it changes no graph styling. Position, size and stacking-order setters also reject objects excluded by selection filtering, including locked guide paths. `crates/engine/src/tests/script_dom.rs` covers refused edits, unchanged documents and undo history, and editing a guide after unlocking it.