End paused JavaScript runs before compiling replacement source

This commit is contained in:
dotCooCoo committed 2026-11-26 06:57:29 -08:00
1 parent 5d7e2af8a0
commit 0983b6dfd5
4 files changed
+97

No files matched your search

+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
@@ -2883,6 +2883,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.
Variables panel row context menus (2026-11-20, G12): Right-click highlights the clicked variable without changing the canvas selection and offers Select Bound Object, Variable Options…, Unbind Variable, the four Make Dynamic actions, and Delete Variable. Shift+F10 opens the same menu for the focused row; Escape dismisses it. Row actions target that variable; dynamic actions use the canvas selection. Lock and selection checks disable unavailable actions. Headless pointer/accessibility tests cover opening the menu, highlighting, bound-object selection, deletion in one undo step and restoration, and disabled actions. Scores and estimates unchanged.
Actions panel item context menus (2026-11-20): Right-clicking an Action Set row selects it and presents a context menu with New Action…, New Set…, Duplicate Set, Delete Set, and Set Options…. Right-clicking an Action row selects it and presents a context menu with Play, Record, Duplicate, Delete, Action Options…, and Batch…. Duplicate on an action duplicates that action and selects the duplicate; Delete removes it. Headless tests in `crates/ui-egui/src/panels/actions.rs`: `right_clicking_an_action_set_selects_it_and_presents_set_context_options`, `right_clicking_an_action_selects_it_and_presents_action_options`, and `clicking_duplicate_on_an_action_duplicates_that_action_in_the_set`. Scores and estimates unchanged.