mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Scripts: queued startup runs keep the file access scope of the request that queued them
In the app, script.startup.run starts only the first queued run inside the request; the later startup scripts and the chosen script start from the frame loop. Each of them now starts, and its file is read, in the scope captured when the queue was built, so a confined request's automation roots apply to every run it queued.
This commit is contained in:
1 parent
81dfbf4078
commit
909c169afe
5 files changed
+41
-2
No files matched your search
@@ -449,17 +449,20 @@ impl Session {
|
||||
pub(crate) fn start_queued(&mut self) -> Option<Value> {
|
||||
// Held aside while a run starts, so that the queue does not count as a running script then.
|
||||
let mut queue = std::mem::take(&mut self.script_queue);
|
||||
// Each run starts in the file access scope of the request that queued it, which the run
|
||||
// keeps, and not in the scope of the frame that starts it.
|
||||
let scope = self.script_queue_scope.clone().unwrap_or_else(crate::file_access::ScopeHandle::capture);
|
||||
let mut reply = None;
|
||||
while self.script_key.is_none() {
|
||||
let Some(next) = queue.pop_front() else { break };
|
||||
match next {
|
||||
script::startup::Queued::Startup { path, fuel } => {
|
||||
if let Err(e) = self.start_startup(&path, fuel) {
|
||||
if let Err(e) = scope.clone().run(|| self.start_startup(&path, fuel)) {
|
||||
self.startup_failures.push((file_name_of(&path), e));
|
||||
}
|
||||
}
|
||||
script::startup::Queued::Then(params) => {
|
||||
match run(self, ¶ms) {
|
||||
match scope.clone().run(|| run(self, ¶ms)) {
|
||||
Ok(_) if self.script_key.is_some() => {}
|
||||
Ok(v) => reply = Some(v),
|
||||
Err(e) => self.startup_failures.push((then_name(¶ms), e.to_string())),
|
||||
@@ -468,6 +471,9 @@ impl Session {
|
||||
}
|
||||
}
|
||||
}
|
||||
if queue.is_empty() {
|
||||
self.script_queue_scope = None;
|
||||
}
|
||||
self.script_queue = queue;
|
||||
reply
|
||||
}
|
||||
@@ -553,6 +559,7 @@ fn startup_run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if s.script_frames {
|
||||
s.script_queue =
|
||||
scripts.into_iter().map(|path| script::startup::Queued::Startup { path, fuel }).chain(then.map(script::startup::Queued::Then)).collect();
|
||||
s.script_queue_scope = Some(crate::file_access::ScopeHandle::capture());
|
||||
let queued = s.script_queue.len();
|
||||
s.startup_failures.clear();
|
||||
let then = s.start_queued();
|
||||
|
||||
@@ -314,6 +314,9 @@ pub struct Session {
|
||||
/// The runs `script.startup.run` queued in the app, each started when the run before it ends
|
||||
/// ([`Session::script_pump`]).
|
||||
pub(crate) script_queue: std::collections::VecDeque<script::startup::Queued>,
|
||||
/// The file access scope of the request that queued [`Self::script_queue`], in which each queued
|
||||
/// run starts and is read.
|
||||
pub(crate) script_queue_scope: Option<file_access::ScopeHandle>,
|
||||
/// While a startup script runs: the launch marker's entry naming it
|
||||
/// ([`vectorcraft_plugins::safe_mode::loading`]).
|
||||
pub(crate) startup_loading: Option<vectorcraft_plugins::safe_mode::Loading>,
|
||||
|
||||
@@ -1884,6 +1884,7 @@ impl Session {
|
||||
pub fn script_abandon(&mut self) -> bool {
|
||||
// The runs queued after it are not started.
|
||||
self.script_queue.clear();
|
||||
self.script_queue_scope = None;
|
||||
self.startup_ended();
|
||||
let Some(key) = self.script_key.take() else { return false };
|
||||
let Some(job) = JOBS.with(|jobs| jobs.borrow_mut().remove(&key.id)) else {
|
||||
|
||||
@@ -170,6 +170,7 @@ impl Session {
|
||||
script_runs: 0,
|
||||
script_frames: false,
|
||||
script_queue: Default::default(),
|
||||
script_queue_scope: None,
|
||||
startup_loading: None,
|
||||
startup_failures: vec![],
|
||||
script_high_water: Default::default(),
|
||||
|
||||
@@ -353,3 +353,30 @@ fn in_the_app_startup_scripts_that_cannot_start_are_in_the_reply() {
|
||||
assert_eq!(r["failed"], json!([{"name": "big.jsx", "error": vectorcraft_engine::cmd::scripts::TOO_LARGE}]));
|
||||
assert!(s.take_startup_failures().is_empty(), "the reply names them, so the status bar does not again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_the_app_every_queued_run_keeps_the_automation_roots_of_the_request_that_queued_it() {
|
||||
let _f = fresh();
|
||||
let dir = tmp_dir("app-roots");
|
||||
// Writes a file in the run's temporary folder, which is outside the write roots below.
|
||||
let source = "var ok; try { var t = new File(Folder.temp.fsName + '/t.txt'); ok = t.open('w'); t.close(); } catch (e) { ok = false; }\n\
|
||||
vc.log(ok ? 'wrote' : 'refused');";
|
||||
write(&dir, "a.jsx", source);
|
||||
write(&dir, "b.jsx", source);
|
||||
let roots = vectorcraft_engine::file_access::AutomationRoots::new(&[dir.as_path()], &[]).unwrap().unwrap();
|
||||
for confined in [false, true] {
|
||||
let mut s = session(&dir);
|
||||
s.script_frames = true;
|
||||
let params = json!({"reason": "menu", "then": {"source": source, "name": "chosen"}});
|
||||
let mut request = || s.execute("script.startup.run", ¶ms).unwrap();
|
||||
let r = if confined { vectorcraft_engine::file_access::confine(Some(&roots), request) } else { request() };
|
||||
assert_eq!(r["queued"], json!(3));
|
||||
// The frames that start the later runs are outside the request.
|
||||
while s.script_running() {
|
||||
s.script_pump();
|
||||
}
|
||||
assert_eq!(started(&s), ["a.jsx", "b.jsx", "chosen"]);
|
||||
let want = if confined { "refused" } else { "wrote" };
|
||||
assert_eq!(console(&s, "log"), [want, want, want], "confined {confined}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user