mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
Setting `closed` on a path with no points gives it an empty closed subpath, so the path's own data keeps the setting. The separate set of empty paths, its key, its cleanup after commands and its copy in the command trail were no longer written to by anything. `closed` and `setEntirePath` now read the closed state from the subpaths only.
3264 lines
147 KiB
Rust
3264 lines
147 KiB
Rust
//! Running a script: `script.run`, `script.stop` and the job behind them.
|
|
//!
|
|
//! A run works on its own copy of the open documents and the session state commands change (the
|
|
//! working copy). Between two slices of a run, the session holds the state as it was before the
|
|
//! run: what is drawn, what a read-only command returns and what Data Recovery copies never show a
|
|
//! script's unfinished work. Each slice swaps the working copy in, advances the interpreter by one
|
|
//! measured slice of fuel and swaps it out. A commit makes the working copy the session's state,
|
|
//! with one undo step for each document whose art changed. A rollback drops the working copy.
|
|
//!
|
|
//! While a job exists, [`Session::execute`] refuses any command that can edit, select, save or give
|
|
//! a document a new revision, and the tool events are refused the same way (the mutation lease).
|
|
//! The script's own commands run through [`Session::execute`] inside a slice, each with its own
|
|
//! parameter checks, enabled rule, undo history and journal entry, and a command that fails leaves
|
|
//! no trace of itself. Inside a run, `edit.undo` reaches only steps the script made.
|
|
//!
|
|
//! Files a script's commands saved or exported before it failed stay written, and the failure names
|
|
//! them. Ids given out in a run are never given out again for a document, also after an in-run
|
|
//! undo or a rollback.
|
|
//!
|
|
//! The values here are VectorCraft's own limits: [`MAX_SOURCE_BYTES`], [`DEFAULT_FUEL`],
|
|
//! [`MAX_FUEL`], [`SLICE_FUEL`], [`HEAP_BYTES`], [`MAX_HOST_CALLS`], [`GROWTH_OBJECTS`],
|
|
//! [`GROWTH_BYTES`], [`MAX_CONTINUES`], [`MAX_LOG_LINES`], [`MAX_LOG_BYTES`], [`MAX_ARGS_BYTES`],
|
|
//! and the 64 levels, 16 MiB and 1,048,576 entries of [`vectorcraft_js::DataLimits`]'s default,
|
|
//! and the console's ([`console`]).
|
|
//!
|
|
//! Before a run starts, its preprocessor lines are read ([`include`]): included files take the
|
|
//! place of their lines, and an error in one is reported at its own file, line and column.
|
|
//! `$.evalFile(path)` runs another file in the same global environment and gives its last value,
|
|
//! `$.fileName` is the file of the code that reads it (the file being run for code that `eval` or
|
|
//! `Function` compiled) and `$.includePath` the `#includepath` list.
|
|
//!
|
|
//! A run writes to the Script Console with `$.write` and `$.writeln`, and `vc.log` adds a line
|
|
//! there too. The session's console gets a start line, the run's output, its uncaught error, an end
|
|
//! line and a report line; the run's report holds its own output and error, its time, its host
|
|
//! calls by kind and its slowest commands. The rest of `$`, and the global `localize` and
|
|
//! `toSource`, are in [`dollar`].
|
|
//!
|
|
//! `alert`, `confirm` and `prompt` open dialogs ([`dialogs`]): in the app the run waits for the
|
|
//! person's answer, and a run with nobody to ask takes its answers from `script.run`'s `answers`.
|
|
//!
|
|
//! `File` and `Folder` read files inside the script's own folder, and write in the run's temporary
|
|
//! folder, the script's data folder and what a person allowed it in a file dialog ([`files`]).
|
|
//!
|
|
//! A script with `#targetengine name` runs in that named engine, which the session keeps with the
|
|
//! globals its runs leave, at most [`MAX_ENGINES`] of them with [`ENGINE_HEAP_BYTES`] of heap each
|
|
//! (VectorCraft's own limits). A script without the line, or with `main`, gets a new realm for each
|
|
//! run. An engine keeps no document and no run: its host functions reach the run through a link
|
|
//! that holds the run's host only while the run goes on. An error, a limit or a stop puts the
|
|
//! documents back and keeps the engine's globals; an internal error drops the engine.
|
|
//!
|
|
//! A run in a named engine can show ScriptUI palettes ([`palettes`]), which stay open after it and
|
|
//! whose events run as runs of their own in that engine, one at a time once no run is going. A run
|
|
//! of that engine whose own code calls `update()` handles the waiting events inside the run
|
|
//! instead. A palette closes with its engine.
|
|
//!
|
|
//! `Document.print` to a printer prepares its job in memory; the session holds the jobs of a run
|
|
//! only after the run's changes are kept, for the frontend to send once the person approves them
|
|
//! ([`printing`]).
|
|
|
|
pub mod actions;
|
|
pub mod console;
|
|
pub mod dialogs;
|
|
pub mod dollar;
|
|
pub mod dom;
|
|
pub mod include;
|
|
pub mod palettes;
|
|
pub mod printing;
|
|
pub mod scriptui;
|
|
|
|
pub use dialogs::{DialogAnswer, DialogKind, ScriptDialog};
|
|
pub use palettes::{ScriptPalette, Waiting};
|
|
pub use scriptui::{ScriptUiDialog, UiEvent, UiKind, UiValue};
|
|
|
|
use std::cell::{Cell, RefCell};
|
|
use std::collections::HashMap;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use serde_json::{Map, Value, json};
|
|
use vectorcraft_js::{
|
|
Data, DataLimits, HostError, HostFunction, HostItem, Interrupt, LimitKind, Limits, LoadError, LoadedModule, ModuleLimits, Options, Progress,
|
|
Realm, RunError, RunErrorKind,
|
|
};
|
|
|
|
use crate::{
|
|
Clipboard, DocState, DrawMode, EngineError, Journal, NodeId, PaintDefaults, Result, Session, cmd, command_specs, guard, waits_for_tool_edit,
|
|
};
|
|
|
|
pub mod files;
|
|
pub mod folder;
|
|
pub mod startup;
|
|
|
|
/// The most bytes of source a run takes, as a file or as `source`.
|
|
pub const MAX_SOURCE_BYTES: usize = 8 << 20;
|
|
/// The instructions a run may execute when `maxFuel` is left out.
|
|
pub const DEFAULT_FUEL: u64 = 1_000_000_000;
|
|
/// The largest `maxFuel`.
|
|
pub const MAX_FUEL: u64 = 100_000_000_000;
|
|
/// The instructions one slice of a run executes before the interface draws again.
|
|
pub const SLICE_FUEL: u64 = 500_000;
|
|
/// The heap one run may use.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub const HEAP_BYTES: usize = 256 << 20;
|
|
/// The heap one run may use in a wasm build.
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub const HEAP_BYTES: usize = 128 << 20;
|
|
/// The most `vc.run` calls and getter reads of one run.
|
|
pub const MAX_HOST_CALLS: u64 = 2_000_000;
|
|
/// The objects a run may make before it is asked to continue or stop.
|
|
pub const GROWTH_OBJECTS: u64 = 100_000;
|
|
/// The bytes of new document data a run may keep before it is asked to continue or stop.
|
|
pub const GROWTH_BYTES: u64 = 512 << 20;
|
|
/// How many times a person may let a run go past its growth allowance; each time adds one more.
|
|
pub const MAX_CONTINUES: u32 = 8;
|
|
/// The most `vc.log` entries of one run.
|
|
pub const MAX_LOG_LINES: usize = 10_000;
|
|
/// The most bytes of `vc.log` entries of one run.
|
|
pub const MAX_LOG_BYTES: usize = 4 << 20;
|
|
/// The most bytes of `args` given to one run, as JSON.
|
|
pub const MAX_ARGS_BYTES: usize = 1 << 20;
|
|
/// The shortest run time between two ScriptUI `update()` calls that each end the slice, so the app
|
|
/// draws the window (VectorCraft's own value, about one frame).
|
|
const UPDATE_INTERVAL: std::time::Duration = std::time::Duration::from_millis(16);
|
|
/// The `vc.run` calls one slice makes before the interface draws again.
|
|
const SLICE_HOST_CALLS: u32 = 256;
|
|
/// The bytes that may cross between a script and the host in one run.
|
|
const MAX_BRIDGE_BYTES: u64 = 4 << 30;
|
|
/// The most files a run's report names in `written`; past them the report has `writtenMore`.
|
|
pub const MAX_WRITTEN: usize = 4096;
|
|
|
|
/// The files a run wrote, as its report names them: at most [`MAX_WRITTEN`], each once, in the
|
|
/// order they were first written, and whether it wrote more.
|
|
#[derive(Default)]
|
|
struct Written {
|
|
paths: Vec<String>,
|
|
seen: std::collections::HashSet<String>,
|
|
more: bool,
|
|
}
|
|
|
|
impl Written {
|
|
fn add(&mut self, path: String) {
|
|
if self.seen.contains(&path) {
|
|
return;
|
|
}
|
|
if self.paths.len() >= MAX_WRITTEN {
|
|
self.more = true;
|
|
return;
|
|
}
|
|
self.seen.insert(path.clone());
|
|
self.paths.push(path);
|
|
}
|
|
}
|
|
|
|
/// The version `vc.api.version` gives.
|
|
pub const API_VERSION: &str = "1.0";
|
|
/// The features `vc.api.has` knows.
|
|
pub const API_FEATURES: &[&str] = &["run", "document", "commands", "log", "args", "console", "dialogs", "dollar", "scriptui"];
|
|
/// The most commands a run's report lists in `slowestCommands`.
|
|
pub const SLOWEST_COMMANDS: usize = 5;
|
|
|
|
/// What the step of a run's one undo step is called.
|
|
pub const STEP_LABEL: &str = "Run Script";
|
|
|
|
/// `id`, as `vc.run` is given it, is one a script may not run: a command that would leave the run's
|
|
/// transaction, change something outside the documents that a rollback could not put back, or clear
|
|
/// the undo steps from before the run, which the run keeps.
|
|
pub fn refused_to_scripts(id: &str) -> bool {
|
|
const PREFIXES: &[&str] = &[
|
|
"script.",
|
|
"plugin.",
|
|
"prefs.",
|
|
"actionSet.",
|
|
"shortcutSet.",
|
|
"help.",
|
|
"print.",
|
|
"library.",
|
|
"importMatches.",
|
|
"edit.undoGroup.",
|
|
"file.recovery.",
|
|
"stroke.widthProfile.",
|
|
"proof.",
|
|
];
|
|
const IDS: &[&str] = &[
|
|
"text.addFontFiles",
|
|
"text.findFontFiles",
|
|
"text.rescanFonts",
|
|
"links.findFiles",
|
|
"file.place.queue",
|
|
"color.loadProfile",
|
|
"color.removeProfile",
|
|
"file.print",
|
|
"view.proofSetup",
|
|
"view.proofSetup.save",
|
|
"view.proofSetup.load",
|
|
"view.proofColors",
|
|
"view.overprintPreview",
|
|
"view.separationsPreview",
|
|
"edit.clearHistory",
|
|
];
|
|
let saved_preset = (id.contains("resets.") || id.contains(".preset.")) && !id.ends_with(".list") && !id.ends_with(".apply");
|
|
id != "script.check" && (saved_preset || PREFIXES.iter().any(|p| id.starts_with(p)) || IDS.contains(&id))
|
|
}
|
|
|
|
/// The error for a script's command that [`refused_to_scripts`] names.
|
|
pub(crate) fn refused_text(id: &str) -> String {
|
|
format!("a script cannot run `{id}`")
|
|
}
|
|
|
|
/// A request to run a script.
|
|
#[derive(Clone)]
|
|
pub(crate) struct Request {
|
|
/// The run's name; unless `name_given`, a `#script` line in the script replaces it.
|
|
pub name: String,
|
|
pub name_given: bool,
|
|
/// The file the script was read from, which its included files are read beside.
|
|
pub file: Option<String>,
|
|
/// The real path of `file` as the read of `source` found it, which the included files are then
|
|
/// read beside instead of looking `file` up again.
|
|
pub checked_real: Option<std::path::PathBuf>,
|
|
pub source: String,
|
|
pub args: Map<String, Value>,
|
|
pub fuel: u64,
|
|
/// The locale the run starts with (`$.locale`).
|
|
pub locale: String,
|
|
/// The answers to the dialogs of a run with nobody to ask, in order.
|
|
pub answers: Vec<DialogAnswer>,
|
|
/// The script runs as an ECMAScript 2015 module, as a `.mjs` file does ([`include::module_script`]).
|
|
pub module: bool,
|
|
/// The actions `app.doScript` can play.
|
|
pub actions: Vec<actions::Playable>,
|
|
}
|
|
|
|
thread_local! {
|
|
/// The running jobs of this thread's sessions. A job is held here, and not in its session,
|
|
/// because it holds the interpreter, which stays on the thread that started it.
|
|
static JOBS: RefCell<HashMap<u64, Job>> = RefCell::new(HashMap::new());
|
|
}
|
|
|
|
static NEXT_JOB: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
|
|
|
/// A session's hold on its running job: while one exists the lease is held. Dropping it ends the job.
|
|
/// It keeps the run's number and its name as console lines give it, so the run's console entry can
|
|
/// end when the job is gone.
|
|
#[derive(Debug)]
|
|
pub struct JobKey {
|
|
id: u64,
|
|
run: u64,
|
|
name: Arc<str>,
|
|
}
|
|
|
|
impl Drop for JobKey {
|
|
fn drop(&mut self) {
|
|
let _ = JOBS.try_with(|jobs| {
|
|
if let Ok(mut jobs) = jobs.try_borrow_mut() {
|
|
jobs.remove(&self.id);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
fn with_job<T>(key: &JobKey, f: impl FnOnce(&mut Job) -> T) -> Option<T> {
|
|
JOBS.with(|jobs| jobs.borrow_mut().get_mut(&key.id).map(f))
|
|
}
|
|
|
|
/// The most named engines (`#targetengine`) a session keeps.
|
|
pub const MAX_ENGINES: usize = 8;
|
|
/// The heap a named engine may use, with what it keeps between runs.
|
|
pub const ENGINE_HEAP_BYTES: usize = 64 << 20;
|
|
/// The engine of a script without `#targetengine`, which `#targetengine main` also names: a new
|
|
/// one for each run, never kept.
|
|
pub const MAIN_ENGINE: &str = "main";
|
|
|
|
/// The run a script's host functions act for. A named engine keeps its functions between runs, so
|
|
/// they find the run through this link, which holds the run's [`Host`] only while the run goes on.
|
|
type Link = Rc<RefCell<Option<Rc<Host>>>>;
|
|
|
|
/// The host of the run `link` holds, or an error when no run holds it.
|
|
fn current(link: &Link) -> std::result::Result<Rc<Host>, HostError> {
|
|
link.borrow().clone().ok_or_else(|| HostError::Error(NO_RUN.to_string()))
|
|
}
|
|
|
|
/// A named engine: its interpreter, with the globals its runs left (`None` while a run has it),
|
|
/// the link its host functions use, its heap after its last run, and the source maps of the code
|
|
/// its earlier runs compiled.
|
|
struct Engine {
|
|
name: String,
|
|
realm: Option<Realm>,
|
|
link: Link,
|
|
heap: usize,
|
|
maps: Rc<RefCell<KeptMaps>>,
|
|
}
|
|
|
|
/// A named engine taken for a run: its realm (`None` for an engine made now), its link and its
|
|
/// kept source maps.
|
|
type TakenEngine = (Option<Realm>, Link, Rc<RefCell<KeptMaps>>);
|
|
|
|
/// The most bytes of source maps ([`include::SourceMap::bytes`]) a named engine keeps from its
|
|
/// earlier runs.
|
|
pub const KEPT_MAP_BYTES: usize = 8 << 20;
|
|
|
|
/// A source map an engine kept from an earlier run: the realm's tag for the source
|
|
/// ([`Realm::error_tag`]), the map, and for the run's own script, that run's name, which stands for
|
|
/// a script given as text.
|
|
struct KeptMap {
|
|
tag: u32,
|
|
map: include::SourceMap,
|
|
script: Option<Arc<str>>,
|
|
bytes: usize,
|
|
}
|
|
|
|
/// The source maps a named engine kept from its earlier runs, by tag, oldest first, in at most
|
|
/// [`KEPT_MAP_BYTES`]: the oldest go first to make room, and a map larger than that is not kept.
|
|
#[derive(Default)]
|
|
struct KeptMaps {
|
|
maps: std::collections::VecDeque<KeptMap>,
|
|
bytes: usize,
|
|
/// The SHA-256 of each source the engine's runs compiled (their scripts and what they
|
|
/// evaluated), at most [`MAX_ENGINE_CODE`], and whether there were more: a later run's kept file
|
|
/// grants are in force only for code they were kept for ([`files::Disk`]).
|
|
code: std::collections::BTreeSet<String>,
|
|
code_full: bool,
|
|
}
|
|
|
|
/// The most sources whose SHA-256 a named engine keeps.
|
|
pub const MAX_ENGINE_CODE: usize = 1024;
|
|
|
|
impl KeptMaps {
|
|
/// Adds the sources `code` (and whether a run had more than it counted) to the engine's code.
|
|
fn keep_code(&mut self, (code, more): (Vec<String>, bool)) {
|
|
self.code_full |= more;
|
|
for hash in code {
|
|
if self.code.len() >= MAX_ENGINE_CODE && !self.code.contains(&hash) {
|
|
self.code_full = true;
|
|
} else {
|
|
self.code.insert(hash);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn keep(&mut self, tag: u32, map: include::SourceMap, script: Option<Arc<str>>) {
|
|
let bytes = map.bytes() + script.as_ref().map_or(0, |s| s.len());
|
|
let limit = kept_map_bytes();
|
|
if bytes > limit || self.maps.back().is_some_and(|m| m.tag >= tag) {
|
|
return;
|
|
}
|
|
while self.bytes + bytes > limit {
|
|
let Some(old) = self.maps.pop_front() else { break };
|
|
self.bytes -= old.bytes;
|
|
}
|
|
self.bytes += bytes;
|
|
self.maps.push_back(KeptMap { tag, map, script, bytes });
|
|
}
|
|
|
|
fn get(&self, tag: u32) -> Option<&KeptMap> {
|
|
self.maps.binary_search_by_key(&tag, |m| m.tag).ok().and_then(|i| self.maps.get(i))
|
|
}
|
|
}
|
|
|
|
/// The bytes of source maps a named engine keeps.
|
|
fn kept_map_bytes() -> usize {
|
|
#[cfg(test)]
|
|
if let Some(n) = TEST_KEPT_MAP_BYTES.with(Cell::get) {
|
|
return n;
|
|
}
|
|
KEPT_MAP_BYTES
|
|
}
|
|
|
|
/// The named engines of one thread's sessions, by their key, each in the order they were made.
|
|
struct ThreadEngines {
|
|
thread: std::thread::ThreadId,
|
|
by_key: HashMap<u64, Vec<Engine>>,
|
|
}
|
|
|
|
impl ThreadEngines {
|
|
fn new() -> Self {
|
|
let thread = std::thread::current().id();
|
|
released().insert(thread, Vec::new());
|
|
Self { thread, by_key: HashMap::new() }
|
|
}
|
|
}
|
|
|
|
impl Drop for ThreadEngines {
|
|
fn drop(&mut self) {
|
|
released().remove(&self.thread);
|
|
}
|
|
}
|
|
|
|
thread_local! {
|
|
/// The named engines of this thread's sessions. They are held here, and not in their session,
|
|
/// because they hold interpreters, which stay on this thread.
|
|
static ENGINES: RefCell<ThreadEngines> = RefCell::new(ThreadEngines::new());
|
|
}
|
|
|
|
static NEXT_ENGINES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
|
|
|
/// For each thread that holds engines, the keys of the ones sessions let go of on another thread.
|
|
/// The thread drops them the next time it uses its engines; a thread that has ended has no entry,
|
|
/// since its engines ended with it.
|
|
static RELEASED_ENGINES: std::sync::LazyLock<std::sync::Mutex<HashMap<std::thread::ThreadId, Vec<u64>>>> =
|
|
std::sync::LazyLock::new(|| std::sync::Mutex::new(HashMap::new()));
|
|
|
|
fn released() -> std::sync::MutexGuard<'static, HashMap<std::thread::ThreadId, Vec<u64>>> {
|
|
RELEASED_ENGINES.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
}
|
|
|
|
/// Lets the engines with key `id` go, for `thread`, which holds them, to drop.
|
|
fn release_engines(thread: std::thread::ThreadId, id: u64) {
|
|
if let Some(ids) = released().get_mut(&thread) {
|
|
ids.push(id);
|
|
}
|
|
}
|
|
|
|
/// A session's hold on its named engines: their key, and the thread that holds them once a run
|
|
/// made one. Dropping it drops them. The engines stay on that thread, so a session used on another
|
|
/// thread lets them go and starts there with none.
|
|
#[derive(Debug)]
|
|
pub struct EnginesKey(std::sync::Mutex<(u64, Option<std::thread::ThreadId>)>);
|
|
|
|
impl EnginesKey {
|
|
pub(crate) fn new() -> Self {
|
|
Self(std::sync::Mutex::new((NEXT_ENGINES.fetch_add(1, std::sync::atomic::Ordering::Relaxed), None)))
|
|
}
|
|
|
|
/// The key of the session's engines on this thread.
|
|
fn here(&self) -> u64 {
|
|
let mut key = self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
|
let thread = std::thread::current().id();
|
|
match key.1 {
|
|
Some(t) if t != thread => {
|
|
release_engines(t, key.0);
|
|
*key = (NEXT_ENGINES.fetch_add(1, std::sync::atomic::Ordering::Relaxed), Some(thread));
|
|
}
|
|
Some(_) => {}
|
|
None => key.1 = Some(thread),
|
|
}
|
|
key.0
|
|
}
|
|
}
|
|
|
|
impl Drop for EnginesKey {
|
|
fn drop(&mut self) {
|
|
let (id, thread) = *self.0.get_mut().unwrap_or_else(std::sync::PoisonError::into_inner);
|
|
if let Some(t) = thread.filter(|t| *t != std::thread::current().id()) {
|
|
release_engines(t, id);
|
|
return;
|
|
}
|
|
let gone = ENGINES.try_with(|engines| engines.try_borrow_mut().ok().and_then(|mut e| e.by_key.remove(&id)));
|
|
drop(gone);
|
|
}
|
|
}
|
|
|
|
fn with_engines<T>(key: &EnginesKey, f: impl FnOnce(&mut Vec<Engine>) -> T) -> T {
|
|
let id = key.here();
|
|
ENGINES.with(|engines| {
|
|
let gone: Vec<Vec<Engine>> = {
|
|
let mut here = engines.borrow_mut();
|
|
let ids = released().get_mut(&here.thread).map(std::mem::take).unwrap_or_default();
|
|
ids.iter().filter_map(|id| here.by_key.remove(id)).collect()
|
|
};
|
|
drop(gone);
|
|
f(engines.borrow_mut().by_key.entry(id).or_default())
|
|
})
|
|
}
|
|
|
|
/// Tests: the named engines this thread holds, for every session.
|
|
#[cfg(test)]
|
|
pub(crate) fn engines_on_this_thread() -> usize {
|
|
ENGINES.with(|engines| engines.borrow().by_key.values().map(Vec::len).sum())
|
|
}
|
|
|
|
/// Tests: whether engines can still be let go for `thread`, which holds an entry while it lives.
|
|
#[cfg(test)]
|
|
pub(crate) fn releases_kept_for(thread: std::thread::ThreadId) -> bool {
|
|
released().contains_key(&thread)
|
|
}
|
|
|
|
/// The heap of a named engine.
|
|
fn engine_heap() -> usize {
|
|
#[cfg(test)]
|
|
if let Some(n) = TEST_ENGINE_HEAP.with(Cell::get) {
|
|
return n;
|
|
}
|
|
ENGINE_HEAP_BYTES
|
|
}
|
|
|
|
/// The state a script changes that a rollback puts back.
|
|
struct World {
|
|
docs: Vec<DocState>,
|
|
active: Option<usize>,
|
|
paint: PaintDefaults,
|
|
fill_active: bool,
|
|
draw_mode: DrawMode,
|
|
draw_inside: Option<NodeId>,
|
|
clipboard: Clipboard,
|
|
clipboard_copied: bool,
|
|
untitled_counter: u32,
|
|
menu: cmd::menucmds::MenuState,
|
|
last_solid: crate::Color,
|
|
last_gradient: crate::GradientPaint,
|
|
recent_colors: Vec<crate::Color>,
|
|
pending_paint: Option<crate::Paint>,
|
|
choices: Choices,
|
|
journal: Journal,
|
|
/// The preferences when the run started.
|
|
start_prefs: crate::Prefs,
|
|
/// The Caps Lock state when the run started.
|
|
start_caps_lock: bool,
|
|
}
|
|
|
|
/// The choices outside the documents that a script's commands can change: the preferences, the
|
|
/// global edit options, the Select Similar Objects attribute, the recent URLs, the chosen gradient stop, freeform point and appearance
|
|
/// item, the Eyedropper's sample, the envelope defaults, the Caps Lock state and whether Overprint
|
|
/// Preview turns on when the run ends.
|
|
#[derive(Clone)]
|
|
struct Choices {
|
|
prefs: crate::Prefs,
|
|
caps_lock: bool,
|
|
overprint_after_run: bool,
|
|
eyedropper_sample: Option<Arc<vectorcraft_doc::Node>>,
|
|
global_edit_options: cmd::globaledit::Options,
|
|
select_similar: &'static str,
|
|
recent_urls: Vec<String>,
|
|
gradient_stop: Option<(usize, cmd::gradient::StopOwner)>,
|
|
freeform_point: Option<(usize, cmd::gradient::StopOwner)>,
|
|
active_appearance_item: Option<cmd::appearance::ActiveItem>,
|
|
envelope_defaults: Option<(vectorcraft_doc::live::EnvelopeOptions, f64)>,
|
|
}
|
|
|
|
impl Choices {
|
|
fn of(s: &Session) -> Self {
|
|
Self {
|
|
prefs: s.prefs.clone(),
|
|
caps_lock: s.caps_lock,
|
|
overprint_after_run: s.overprint_after_run,
|
|
eyedropper_sample: s.eyedropper_sample.clone(),
|
|
global_edit_options: s.global_edit_options.clone(),
|
|
select_similar: s.select_similar,
|
|
recent_urls: s.recent_urls.clone(),
|
|
gradient_stop: s.gradient_stop,
|
|
freeform_point: s.freeform_point,
|
|
active_appearance_item: s.active_appearance_item.clone(),
|
|
envelope_defaults: s.envelope_defaults,
|
|
}
|
|
}
|
|
|
|
fn put(self, s: &mut Session) {
|
|
s.prefs = self.prefs;
|
|
s.caps_lock = self.caps_lock;
|
|
s.overprint_after_run = self.overprint_after_run;
|
|
s.eyedropper_sample = self.eyedropper_sample;
|
|
s.global_edit_options = self.global_edit_options;
|
|
s.select_similar = self.select_similar;
|
|
s.recent_urls = self.recent_urls;
|
|
s.gradient_stop = self.gradient_stop;
|
|
s.freeform_point = self.freeform_point;
|
|
s.active_appearance_item = self.active_appearance_item;
|
|
s.envelope_defaults = self.envelope_defaults;
|
|
}
|
|
|
|
fn swap(&mut self, s: &mut Session) {
|
|
use std::mem::swap;
|
|
swap(&mut self.prefs, &mut s.prefs);
|
|
swap(&mut self.caps_lock, &mut s.caps_lock);
|
|
swap(&mut self.overprint_after_run, &mut s.overprint_after_run);
|
|
swap(&mut self.eyedropper_sample, &mut s.eyedropper_sample);
|
|
swap(&mut self.global_edit_options, &mut s.global_edit_options);
|
|
swap(&mut self.select_similar, &mut s.select_similar);
|
|
swap(&mut self.recent_urls, &mut s.recent_urls);
|
|
swap(&mut self.gradient_stop, &mut s.gradient_stop);
|
|
swap(&mut self.freeform_point, &mut s.freeform_point);
|
|
swap(&mut self.active_appearance_item, &mut s.active_appearance_item);
|
|
swap(&mut self.envelope_defaults, &mut s.envelope_defaults);
|
|
}
|
|
}
|
|
|
|
/// The preferences after a run: `current`, the session's, which the app may have changed while the
|
|
/// run went on, with each preference the run changed from `start` set to the run's value. A
|
|
/// preference the run emptied, which is then left out of the saved form, is left out of the result.
|
|
/// The values scripts store with `app.preferences` are merged the same way one key at a time.
|
|
fn merged_prefs(start: &crate::Prefs, run: &crate::Prefs, current: &crate::Prefs) -> crate::Prefs {
|
|
const SCRIPT_KEYS: &str = "scriptPreferences";
|
|
let (Ok(Value::Object(start_json)), Ok(Value::Object(run_json)), Ok(Value::Object(mut out))) =
|
|
(serde_json::to_value(start), serde_json::to_value(run), serde_json::to_value(current))
|
|
else {
|
|
return current.clone();
|
|
};
|
|
for key in start_json.keys().filter(|k| *k != SCRIPT_KEYS && !run_json.contains_key(*k)) {
|
|
out.remove(key);
|
|
}
|
|
for (key, value) in run_json {
|
|
if key != SCRIPT_KEYS && start_json.get(&key) != Some(&value) {
|
|
out.insert(key, value);
|
|
}
|
|
}
|
|
let mut merged: crate::Prefs = serde_json::from_value(Value::Object(out)).unwrap_or_else(|_| current.clone());
|
|
let script = &mut merged.script_preferences;
|
|
for key in start.script_preferences.keys().filter(|k| !run.script_preferences.contains_key(*k)) {
|
|
script.remove(key);
|
|
}
|
|
for (key, value) in &run.script_preferences {
|
|
if start.script_preferences.get(key) != Some(value) {
|
|
script.insert(key.clone(), value.clone());
|
|
}
|
|
}
|
|
merged
|
|
}
|
|
|
|
impl World {
|
|
/// A working copy of the session's state: its documents, each with an undo history of its own
|
|
/// that starts empty, and an empty journal.
|
|
fn working(s: &Session) -> Self {
|
|
let mut docs = s.docs.clone();
|
|
for st in &mut docs {
|
|
st.history.undo.clear();
|
|
st.history.redo.clear();
|
|
}
|
|
Self {
|
|
docs,
|
|
active: s.active,
|
|
paint: s.paint.clone(),
|
|
fill_active: s.fill_active,
|
|
draw_mode: s.draw_mode,
|
|
draw_inside: s.draw_inside,
|
|
clipboard: s.clipboard.clone(),
|
|
clipboard_copied: s.clipboard_copied,
|
|
untitled_counter: s.untitled_counter,
|
|
menu: s.menu.clone(),
|
|
last_solid: s.last_solid,
|
|
last_gradient: s.last_gradient.clone(),
|
|
recent_colors: s.recent_colors.clone(),
|
|
pending_paint: s.pending_paint.clone(),
|
|
choices: Choices::of(s),
|
|
journal: Journal::default(),
|
|
start_prefs: s.prefs.clone(),
|
|
start_caps_lock: s.caps_lock,
|
|
}
|
|
}
|
|
|
|
/// Exchanges this state with the session's.
|
|
fn swap(&mut self, s: &mut Session) {
|
|
use std::mem::swap;
|
|
swap(&mut self.docs, &mut s.docs);
|
|
swap(&mut self.active, &mut s.active);
|
|
swap(&mut self.paint, &mut s.paint);
|
|
swap(&mut self.fill_active, &mut s.fill_active);
|
|
swap(&mut self.draw_mode, &mut s.draw_mode);
|
|
swap(&mut self.draw_inside, &mut s.draw_inside);
|
|
swap(&mut self.clipboard, &mut s.clipboard);
|
|
swap(&mut self.clipboard_copied, &mut s.clipboard_copied);
|
|
swap(&mut self.untitled_counter, &mut s.untitled_counter);
|
|
swap(&mut self.menu, &mut s.menu);
|
|
swap(&mut self.last_solid, &mut s.last_solid);
|
|
swap(&mut self.last_gradient, &mut s.last_gradient);
|
|
swap(&mut self.recent_colors, &mut s.recent_colors);
|
|
swap(&mut self.pending_paint, &mut s.pending_paint);
|
|
self.choices.swap(s);
|
|
swap(&mut self.journal, &mut s.journal);
|
|
}
|
|
}
|
|
|
|
/// The commands that can change the clipboard and still fail, or still be put back because their
|
|
/// result cannot reach the script, and that do not put it back themselves. The clipboard can hold
|
|
/// a great deal, so a savepoint copies it only for these.
|
|
const CAN_FAIL_AFTER_COPYING: &[&str] =
|
|
&["action.batch", "artboard.copy", "artboard.cut", "clipboard.importSvg", "command.batch", "edit.copy", "edit.cut"];
|
|
|
|
/// The commands that remove a document's undo history, or the document: a savepoint keeps a whole
|
|
/// copy of the histories for these. Any other command that removes or changes a step under the
|
|
/// newest undo or redo step and then fails ends the run ([`HISTORY_LOST`]).
|
|
const DROP_HISTORY: &[&str] = &["file.close", "file.closeAll", "document.close", "file.revert"];
|
|
|
|
/// A document's state before a command: everything but its undo history, which `mark` gives back,
|
|
/// or `whole` for the commands in [`DROP_HISTORY`].
|
|
struct SavedDoc {
|
|
state: DocState,
|
|
mark: crate::history::HistoryMark,
|
|
whole: Option<crate::History>,
|
|
}
|
|
|
|
/// What one command may need to put back when it fails inside a run: the state of [`World`] the
|
|
/// command can reach.
|
|
pub(crate) struct Savepoint {
|
|
docs: Vec<SavedDoc>,
|
|
active: Option<usize>,
|
|
paint: PaintDefaults,
|
|
fill_active: bool,
|
|
draw_mode: DrawMode,
|
|
draw_inside: Option<NodeId>,
|
|
/// The clipboard, [`Session::clipboard_copied`] and [`Session::copies`].
|
|
clipboard: Option<(Clipboard, bool, u64)>,
|
|
untitled_counter: u32,
|
|
menu: cmd::menucmds::MenuState,
|
|
last_solid: crate::Color,
|
|
last_gradient: crate::GradientPaint,
|
|
recent_colors: Vec<crate::Color>,
|
|
pending_paint: Option<crate::Paint>,
|
|
choices: Choices,
|
|
journal: usize,
|
|
/// The plug-in events queued before the command ([`Session::plugin_events_mark`]).
|
|
plugin_events: u64,
|
|
}
|
|
|
|
impl Savepoint {
|
|
/// The state before command `id`. Until [`Savepoint::keep`] or [`Savepoint::restore`], the
|
|
/// documents' undo histories let no step go.
|
|
pub(crate) fn take(s: &mut Session, id: &str) -> Self {
|
|
let whole = DROP_HISTORY.contains(&id);
|
|
let docs = s
|
|
.docs
|
|
.iter_mut()
|
|
.map(|st| {
|
|
let history = std::mem::take(&mut st.history);
|
|
let state = st.clone();
|
|
st.history = history;
|
|
let whole = whole.then(|| st.history.clone());
|
|
SavedDoc { state, mark: st.history.mark(), whole }
|
|
})
|
|
.collect();
|
|
Self {
|
|
docs,
|
|
active: s.active,
|
|
paint: s.paint.clone(),
|
|
fill_active: s.fill_active,
|
|
draw_mode: s.draw_mode,
|
|
draw_inside: s.draw_inside,
|
|
clipboard: CAN_FAIL_AFTER_COPYING.contains(&id).then(|| (s.clipboard.clone(), s.clipboard_copied, s.copies)),
|
|
untitled_counter: s.untitled_counter,
|
|
menu: s.menu.clone(),
|
|
last_solid: s.last_solid,
|
|
last_gradient: s.last_gradient.clone(),
|
|
recent_colors: s.recent_colors.clone(),
|
|
pending_paint: s.pending_paint.clone(),
|
|
choices: Choices::of(s),
|
|
journal: s.journal.len(),
|
|
plugin_events: s.plugin_events_mark(),
|
|
}
|
|
}
|
|
|
|
/// Keeps what the command did: the histories' limits apply again.
|
|
pub(crate) fn keep(self, s: &mut Session) {
|
|
for saved in &self.docs {
|
|
if let Some(st) = s.docs.iter_mut().find(|d| d.uid == saved.state.uid) {
|
|
st.history.release(&saved.mark);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Puts back the state before the command. → `false` when an undo history could not be given
|
|
/// back as it was (a step under the newest one was removed or changed in place, or its
|
|
/// document was closed); the rest is put back all the same.
|
|
pub(crate) fn restore(self, s: &mut Session) -> bool {
|
|
let mut exact = true;
|
|
let mut docs = Vec::with_capacity(self.docs.len());
|
|
for SavedDoc { mut state, mark, whole } in self.docs {
|
|
let now = s.docs.iter_mut().find(|d| d.uid == state.uid).map(|d| std::mem::take(&mut d.history));
|
|
state.history = match (whole, now) {
|
|
(Some(history), _) => history,
|
|
(None, Some(mut history)) => {
|
|
exact &= history.put_back(mark);
|
|
history
|
|
}
|
|
(None, None) => {
|
|
exact = false;
|
|
let mut history = crate::History::default();
|
|
history.put_back(mark);
|
|
history
|
|
}
|
|
};
|
|
docs.push(state);
|
|
}
|
|
s.docs = docs;
|
|
s.active = self.active;
|
|
s.paint = self.paint;
|
|
s.fill_active = self.fill_active;
|
|
s.draw_mode = self.draw_mode;
|
|
s.draw_inside = self.draw_inside;
|
|
if let Some((clipboard, copied, copies)) = self.clipboard {
|
|
s.clipboard = clipboard;
|
|
s.clipboard_copied = copied;
|
|
s.copies = copies;
|
|
}
|
|
s.untitled_counter = self.untitled_counter;
|
|
s.menu = self.menu;
|
|
s.last_solid = self.last_solid;
|
|
s.last_gradient = self.last_gradient;
|
|
s.recent_colors = self.recent_colors;
|
|
s.pending_paint = self.pending_paint;
|
|
self.choices.put(s);
|
|
s.journal.truncate(self.journal);
|
|
// The documents the command opened are closed again: plug-ins are not told of them.
|
|
s.forget_plugin_opens_since(self.plugin_events);
|
|
s.sync_front_profiles();
|
|
exact
|
|
}
|
|
}
|
|
|
|
/// The counters and the session a script's host functions share. Between slices `session` holds a
|
|
/// spare session; during a slice it holds the real one, because the interpreter cannot be given a
|
|
/// borrow of it.
|
|
struct Host {
|
|
session: Rc<RefCell<Session>>,
|
|
pause: Rc<Cell<bool>>,
|
|
/// The run is sliced by frames, so the person at the computer can be asked to continue.
|
|
framed: bool,
|
|
calls: Cell<u64>,
|
|
calls_this_slice: Cell<u32>,
|
|
objects: Cell<u64>,
|
|
bytes_kept: Cell<u64>,
|
|
bridge: Cell<u64>,
|
|
allowance_objects: Cell<u64>,
|
|
allowance_bytes: Cell<u64>,
|
|
continues: Cell<u32>,
|
|
/// Past the growth allowance, waiting for the person's answer.
|
|
waiting: Cell<bool>,
|
|
log: RefCell<Vec<Value>>,
|
|
log_bytes: Cell<usize>,
|
|
written: RefCell<Written>,
|
|
/// A command ran a Copy or Cut ([`Session::copies`]), also inside a batch or an action, and
|
|
/// the run kept it.
|
|
copied: Cell<bool>,
|
|
/// The run's number in its session, and the script's name, which its console lines carry.
|
|
run: u64,
|
|
name: Arc<str>,
|
|
/// The run's own output and error, for its report.
|
|
console: RefCell<console::Console>,
|
|
/// The line `$.write` has not ended yet.
|
|
pending: RefCell<console::Pending>,
|
|
counts: Counts,
|
|
/// Each command `vc.run` ran: its calls, its total time and its longest call, in nanoseconds.
|
|
commands: RefCell<HashMap<&'static str, (u64, u128, u128)>>,
|
|
/// The time the run's slices took, in nanoseconds.
|
|
run_nanos: Cell<u128>,
|
|
/// `alert`, `confirm` and `prompt`: their answers and the list the report gives.
|
|
dialogs: dialogs::Dialogs,
|
|
/// The files the run reads: its includes and `$.evalFile`'s.
|
|
files: RefCell<include::Files>,
|
|
/// What `File` and `Folder` have open and have read.
|
|
disk: RefCell<files::Disk>,
|
|
/// Where the lines of the script's expanded source came from.
|
|
script_map: RefCell<include::SourceMap>,
|
|
/// The same for each source `$.evalFile` gave, in order.
|
|
eval_maps: RefCell<Vec<include::SourceMap>>,
|
|
/// The module key ([`include::Files::module`]) of each of [`Host::eval_maps`] that is a
|
|
/// module's, by its index there.
|
|
module_maps: RefCell<HashMap<usize, String>>,
|
|
/// The file, line and column of each error `$.evalFile` raised for a file it could not run, in
|
|
/// order: the error's mark ([`HostError::Marked`]) is its index.
|
|
eval_failures: RefCell<Vec<(Arc<str>, u32, u32)>>,
|
|
/// The realm's tag for the script ([`Realm::script_tag`]) once the run has started it, and
|
|
/// [`vectorcraft_js::UNKNOWN_SOURCE`] before or when the script did not compile.
|
|
script_tag: Cell<u32>,
|
|
/// The run's name as its report gives it, which [`Host::name`] shortens for console lines.
|
|
report_name: Arc<str>,
|
|
/// The source maps the named engine of the run kept from its earlier runs; `None` for a run
|
|
/// without one.
|
|
kept_maps: Option<Rc<RefCell<KeptMaps>>>,
|
|
/// When the run started, where the platform has a clock.
|
|
started: Option<std::time::Instant>,
|
|
/// The time since [`Host::started`] at which a `$.sleep` ends.
|
|
wake_at: Cell<Option<std::time::Duration>>,
|
|
/// The time since [`Host::started`] at which a ScriptUI `update()` last ended the slice.
|
|
drawn_at: Cell<Option<std::time::Duration>>,
|
|
/// Set while a host function runs where the run can stop at the call ([`Realm::wait_flag`]).
|
|
can_wait: Rc<Cell<bool>>,
|
|
/// What the last `#strict` line of the file `$.evalFile` last gave set (`None` without one).
|
|
eval_strict: Cell<Option<bool>>,
|
|
/// The values of `$` that belong to the run.
|
|
dollar: dollar::RunState,
|
|
/// The run's `args`, which `vc.args` gives, and their size as `vc.args` charges it.
|
|
args: RefCell<(Data, u64)>,
|
|
/// The named engine the run is in; `None` for a new realm.
|
|
engine: Option<String>,
|
|
/// The script file the run was read from; `None` for a script given as text.
|
|
file: Option<String>,
|
|
/// For the run of a palette's handler, the palette's id and the event, until the palette takes
|
|
/// it ([`palettes`]).
|
|
palette_event: RefCell<Option<palettes::Queued>>,
|
|
/// The run's state of the script object model (`app`).
|
|
dom: dom::State,
|
|
/// While a slice runs: the documents the interface has open ([`World::docs`] of the run's
|
|
/// job), which a capture must not write over either. Empty between slices.
|
|
interface_docs: RefCell<Vec<DocState>>,
|
|
/// The actions `app.doScript` can play.
|
|
actions: Vec<actions::Playable>,
|
|
/// While a palette's `update()` gives a waiting event to its palette, the actions of the run
|
|
/// that showed that palette, which `app.doScript` plays in place of [`Host::actions`]. A
|
|
/// palette's `paletteTake` sets them and its `palettePending` clears them.
|
|
palette_actions: RefCell<Option<Vec<actions::Playable>>>,
|
|
/// The index of each open document's active artboard as the run starts, by document uid. The
|
|
/// view keeps that index while the run goes on, so the steps `app.doScript` plays act on it.
|
|
active_artboards: Vec<(u64, usize)>,
|
|
/// The artboards the Artboard tool had selected in the active document as the run starts (two or
|
|
/// more), with that document's uid. The run gives the tool fresh state, which drops them, so the
|
|
/// steps `app.doScript` plays read them here.
|
|
artboard_picks: Option<(u64, Vec<usize>)>,
|
|
/// The documents open when the run began as the person last left them, each with its own file
|
|
/// and the file it was read from ([`crate::DocState::as_person_left`],
|
|
/// [`crate::cmd::links::LinkBase::of_files`]), until the run's first command takes the files
|
|
/// they link to from them ([`Host::linked`]).
|
|
run_docs: RefCell<Vec<RunDoc>>,
|
|
/// The files the documents open when the run began link to as the person left them
|
|
/// ([`crate::file_access::ScriptGate::linked`]).
|
|
linked: std::cell::OnceCell<crate::file_access::LinkedFiles>,
|
|
}
|
|
|
|
/// A document open when a run began, with its own file and the file it was read from.
|
|
type RunDoc = (Arc<crate::Document>, Option<String>, Option<String>);
|
|
|
|
impl Host {
|
|
/// The files the commands the run runs may read because the documents open when the run began
|
|
/// link to them, found once, from those documents as the person left them: a link a script's
|
|
/// command added or changed, in this run or an earlier one, and a document it opened, give it
|
|
/// no file to read.
|
|
fn linked(&self) -> crate::file_access::LinkedFiles {
|
|
self.linked
|
|
.get_or_init(|| {
|
|
let docs = std::mem::take(&mut *self.run_docs.borrow_mut());
|
|
let files = docs.iter().flat_map(|(d, path, from)| {
|
|
crate::cmd::links::files_linked(d, crate::cmd::links::LinkBase::of_files(path.as_deref(), from.as_deref()))
|
|
});
|
|
crate::file_access::ScriptGate::real_files(files.collect::<Vec<_>>())
|
|
})
|
|
.clone()
|
|
}
|
|
}
|
|
|
|
/// A run's host calls by kind, as its report gives them in `hostCallCounts`.
|
|
#[derive(Default)]
|
|
struct Counts {
|
|
vc_run: Cell<u64>,
|
|
document_reads: Cell<u64>,
|
|
command_list_reads: Cell<u64>,
|
|
vc_log: Cell<u64>,
|
|
console_writes: Cell<u64>,
|
|
/// Calls of `File` and `Folder` that reach the host ([`files`]).
|
|
file_calls: Cell<u64>,
|
|
/// `execute` calls, which open nothing.
|
|
file_executes: Cell<u64>,
|
|
}
|
|
|
|
impl Counts {
|
|
fn json(&self) -> Value {
|
|
json!({
|
|
"vcRun": self.vc_run.get(),
|
|
"documentReads": self.document_reads.get(),
|
|
"commandListReads": self.command_list_reads.get(),
|
|
"vcLog": self.vc_log.get(),
|
|
"consoleWrites": self.console_writes.get(),
|
|
"fileCalls": self.file_calls.get(),
|
|
"fileExecutes": self.file_executes.get(),
|
|
})
|
|
}
|
|
}
|
|
|
|
fn bump(c: &Cell<u64>) {
|
|
c.set(c.get().saturating_add(1));
|
|
}
|
|
|
|
/// Now, where the platform has a clock (not in a wasm build).
|
|
fn clock() -> Option<std::time::Instant> {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
Some(std::time::Instant::now())
|
|
}
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
None
|
|
}
|
|
}
|
|
|
|
/// Whole milliseconds, rounded down.
|
|
fn millis(nanos: u128) -> u64 {
|
|
u64::try_from(nanos / 1_000_000).unwrap_or(u64::MAX)
|
|
}
|
|
|
|
/// The [`SLOWEST_COMMANDS`] commands of `times` that took longest in all, longest first and then by
|
|
/// id.
|
|
pub(crate) fn slowest(times: &HashMap<&'static str, (u64, u128, u128)>) -> Vec<console::Slow> {
|
|
let mut all: Vec<(&&str, &(u64, u128, u128))> = times.iter().collect();
|
|
all.sort_by(|a, b| b.1.1.cmp(&a.1.1).then_with(|| a.0.cmp(b.0)));
|
|
all.into_iter()
|
|
.take(SLOWEST_COMMANDS)
|
|
.map(|(id, &(calls, total, max))| console::Slow { id: (*id).to_string(), calls, total_ms: millis(total), max_ms: millis(max) })
|
|
.collect()
|
|
}
|
|
|
|
impl Host {
|
|
/// Gives the named engine of the run the source maps of the code the run compiled: its script
|
|
/// and each file `$.evalFile` gave. A run that never started its script compiled nothing.
|
|
fn keep_maps(&self) {
|
|
let (Some(kept), tag) = (&self.kept_maps, self.script_tag.get()) else { return };
|
|
if tag == vectorcraft_js::UNKNOWN_SOURCE {
|
|
return;
|
|
}
|
|
let mut kept = kept.borrow_mut();
|
|
kept.keep_code(self.disk.borrow().code());
|
|
kept.keep(tag, std::mem::take(&mut *self.script_map.borrow_mut()), Some(self.report_name.clone()));
|
|
for (n, map) in std::mem::take(&mut *self.eval_maps.borrow_mut()).into_iter().enumerate() {
|
|
match u32::try_from(n + 1).ok().and_then(|n| tag.checked_add(n)).filter(|t| *t != vectorcraft_js::UNKNOWN_SOURCE) {
|
|
Some(t) => kept.keep(t, map, None),
|
|
None => break,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The file and line that line `line` of the source with tag `tag` came from, as `$.line` and
|
|
/// `$.stack` give them: the run's script, a file it included or a file `$.evalFile` gave, in
|
|
/// this run or in an earlier run of its named engine, with the run's name standing for a script
|
|
/// given as text. Code that `eval` or `Function` compiled has no map and keeps its own line,
|
|
/// named `[eval]`; code whose map is gone, such as one the named engine let go to stay within
|
|
/// [`KEPT_MAP_BYTES`], is at line 0 of `[unknown]`.
|
|
fn source_place(&self, tag: u32, line: u32) -> (String, u32) {
|
|
if tag == vectorcraft_js::UNKNOWN_SOURCE {
|
|
return ("[eval]".to_string(), line);
|
|
}
|
|
let named = |map: &include::SourceMap, script: Option<&Arc<str>>| {
|
|
let (index, file, line, _) = map.locate(line, 1);
|
|
match script {
|
|
Some(name) if index == 0 && file.is_empty() => (name.to_string(), line),
|
|
_ => (file.to_string(), line),
|
|
}
|
|
};
|
|
let script = self.script_tag.get();
|
|
let placed = match tag.checked_sub(script).filter(|_| script != vectorcraft_js::UNKNOWN_SOURCE) {
|
|
Some(0) => self.script_map.try_borrow().ok().map(|m| named(&m, Some(&self.report_name))),
|
|
Some(n) => self.eval_maps.try_borrow().ok().and_then(|maps| maps.get(n as usize - 1).map(|m| named(m, None))),
|
|
None => {
|
|
self.kept_maps.as_ref().and_then(|k| k.try_borrow().ok()).and_then(|k| k.get(tag).map(|kept| named(&kept.map, kept.script.as_ref())))
|
|
}
|
|
};
|
|
placed.unwrap_or_else(|| ("[unknown]".to_string(), 0))
|
|
}
|
|
|
|
/// `$.fileName` read by the code at `site`: the file that code is in, by the source map of the
|
|
/// source it came from (this run's script, a file `$.evalFile` gave, or code an earlier run of
|
|
/// the named engine compiled), so a function keeps the file it was defined in. Code from a file
|
|
/// `#include` inserted is in that file. A script given as text has no name, and neither has
|
|
/// code whose map the named engine no longer keeps. Code that `eval` or `Function` compiled has
|
|
/// no map, and is given the file being run now.
|
|
fn file_name_at(&self, site: vectorcraft_js::CallSite) -> String {
|
|
let name = |m: &include::SourceMap| m.locate(site.line, site.column).1.to_string();
|
|
let tag = site.source;
|
|
if tag == vectorcraft_js::UNKNOWN_SOURCE {
|
|
return self.files.borrow().file_name();
|
|
}
|
|
let script = self.script_tag.get();
|
|
match tag.checked_sub(script).filter(|_| script != vectorcraft_js::UNKNOWN_SOURCE) {
|
|
Some(0) => name(&self.script_map.borrow()),
|
|
Some(n) => self.eval_maps.borrow().get(n as usize - 1).map(name).unwrap_or_default(),
|
|
None => {
|
|
let kept = self.kept_maps.as_ref().map(|k| k.borrow());
|
|
kept.as_ref().and_then(|k| k.get(tag)).map(|k| name(&k.map)).unwrap_or_default()
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The folder of the module whose source has `tag`, when it is a module of a module run: the
|
|
/// script itself or a module it imported (not a file `$.evalFile` gave, or code from another
|
|
/// run).
|
|
fn module_dir_at(&self, tag: u32) -> Option<std::path::PathBuf> {
|
|
let script = self.script_tag.get();
|
|
if tag == vectorcraft_js::UNKNOWN_SOURCE || script == vectorcraft_js::UNKNOWN_SOURCE {
|
|
return None;
|
|
}
|
|
let n = tag.checked_sub(script)?;
|
|
if n == 0 {
|
|
return self.files.borrow().entry_module_dir();
|
|
}
|
|
let key = self.module_maps.borrow().get(&(n as usize - 1)).cloned()?;
|
|
self.files.borrow().module_dir(&key)
|
|
}
|
|
|
|
/// Adds a line to the run's console and, during a slice, to the session's.
|
|
fn line(&self, entry: console::Entry, cut: bool) {
|
|
let line = console::ConsoleLine { run: self.run, name: self.name.clone(), entry };
|
|
if let Ok(mut s) = self.session.try_borrow_mut() {
|
|
s.script_console.push(line.clone(), cut);
|
|
}
|
|
self.console.borrow_mut().push(line, cut);
|
|
}
|
|
|
|
/// `$.write` and `$.writeln`: `text`, then with `end` a line break.
|
|
fn write(&self, text: &str, end: bool) {
|
|
let mut pending = self.pending.borrow_mut();
|
|
pending.write(text, |line, cut| self.line(console::Entry::Output(line), cut));
|
|
if end {
|
|
pending.end_line(|line, cut| self.line(console::Entry::Output(line), cut));
|
|
}
|
|
}
|
|
|
|
/// The time since the run started; none without a clock.
|
|
fn elapsed(&self) -> Option<std::time::Duration> {
|
|
#[cfg(test)]
|
|
if TEST_NO_CLOCK.with(Cell::get) {
|
|
return None;
|
|
}
|
|
#[cfg(test)]
|
|
if let Some(d) = TEST_ELAPSED.with(Cell::get) {
|
|
return Some(d);
|
|
}
|
|
self.started.map(|s| s.elapsed())
|
|
}
|
|
|
|
/// `$.sleep`: ends the slice, and the run does not go on until `d` has passed. Without a clock
|
|
/// it does not wait. A getter, a conversion, a built-in's callback or a file `$.evalFile` runs
|
|
/// cannot stop at the call, so a wait there is an error, with or without a clock.
|
|
fn sleep(&self, d: std::time::Duration) -> std::result::Result<(), HostError> {
|
|
if d.is_zero() {
|
|
return Ok(());
|
|
}
|
|
if !self.can_wait.get() {
|
|
return Err(HostError::Error(
|
|
"$.sleep: a run cannot wait inside a callback of a built-in function, a getter, a conversion or a file $.evalFile runs; call it from the script's own code"
|
|
.into(),
|
|
));
|
|
}
|
|
if let Some(now) = self.elapsed() {
|
|
self.wake_at.set(Some(now.saturating_add(d)));
|
|
self.pause.set(true);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// A palette's `update()` in the script's own code: in the app, ends the slice so the palette is
|
|
/// drawn as the script left it, at most once per [`UPDATE_INTERVAL`] of the run's time. →
|
|
/// whether it does, and the palette's controls are to be given now. Without a clock (the web
|
|
/// build), and in a run with nobody to draw for, the palette is drawn when the slice or the run
|
|
/// ends for another reason.
|
|
fn update_drawn(&self) -> bool {
|
|
if !self.framed {
|
|
return false;
|
|
}
|
|
let Some(now) = self.elapsed() else { return false };
|
|
if self.drawn_at.get().is_some_and(|t| now.saturating_sub(t) < UPDATE_INTERVAL) {
|
|
return false;
|
|
}
|
|
self.drawn_at.set(Some(now));
|
|
self.pause.set(true);
|
|
true
|
|
}
|
|
|
|
/// The time left of a `$.sleep`, or none when the run is not asleep.
|
|
fn asleep(&self) -> Option<std::time::Duration> {
|
|
let wake = self.wake_at.get()?;
|
|
let left = self.elapsed().map_or(std::time::Duration::ZERO, |now| wake.saturating_sub(now));
|
|
if left.is_zero() {
|
|
self.wake_at.set(None);
|
|
return None;
|
|
}
|
|
Some(left)
|
|
}
|
|
|
|
/// Adds `paths`, files the script's `File` and `Folder` objects wrote, to the report's `written`.
|
|
fn note_written(&self, paths: Vec<String>) {
|
|
let mut written = self.written.borrow_mut();
|
|
for path in paths {
|
|
written.add(path);
|
|
}
|
|
}
|
|
|
|
/// Adds `writtenMore: true` to `report` when the run wrote more files than its `written` names.
|
|
fn more_written(&self, report: &mut Value) {
|
|
if self.written.borrow().more
|
|
&& let Some(o) = report.as_object_mut()
|
|
{
|
|
o.insert("writtenMore".into(), json!(true));
|
|
}
|
|
}
|
|
|
|
/// Writes the files the script left open and adds every file written to the report's `written`.
|
|
/// → the files it could not write.
|
|
fn flush_files(&self) -> Vec<String> {
|
|
let mut disk = self.disk.borrow_mut();
|
|
let failed = disk.flush_all();
|
|
disk.record_code();
|
|
let paths = disk.take_written();
|
|
drop(disk);
|
|
self.note_written(paths);
|
|
failed
|
|
}
|
|
|
|
fn over(&self, limit: &str) -> HostError {
|
|
HostError::Abort(limit.to_string())
|
|
}
|
|
|
|
/// `error`, marked as an error at `file`, `line` and `column`, the place [`located`] gives when
|
|
/// the run ends with it uncaught.
|
|
fn placed(&self, error: HostError, file: Arc<str>, line: u32, column: u32) -> HostError {
|
|
let mut places = self.eval_failures.borrow_mut();
|
|
let Ok(mark) = u32::try_from(places.len()) else { return error };
|
|
places.push((file, line, column));
|
|
HostError::Marked { error: Box::new(error), mark }
|
|
}
|
|
|
|
fn charge_call(&self, bytes: u64) -> std::result::Result<(), HostError> {
|
|
let calls = self.calls.get() + 1;
|
|
self.calls.set(calls);
|
|
if calls > MAX_HOST_CALLS {
|
|
return Err(self.over(OVER_CALLS));
|
|
}
|
|
let bridge = self.bridge.get().saturating_add(bytes);
|
|
self.bridge.set(bridge);
|
|
if bridge > bridge_bytes() {
|
|
return Err(self.over(OVER_BRIDGE));
|
|
}
|
|
let n = self.calls_this_slice.get() + 1;
|
|
self.calls_this_slice.set(n);
|
|
if n >= SLICE_HOST_CALLS {
|
|
self.pause.set(true);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Counts what a command added to the documents, past which the person is asked or, without
|
|
/// anyone to ask, the run ends. A run that goes on past twice its allowance within one slice
|
|
/// ends too.
|
|
fn charge_growth(&self, objects: u64, bytes: u64) -> std::result::Result<(), HostError> {
|
|
self.objects.set(self.objects.get().saturating_add(objects));
|
|
self.bytes_kept.set(self.bytes_kept.get().saturating_add(bytes));
|
|
let (o, b) = (self.objects.get(), self.bytes_kept.get());
|
|
let (limit_o, limit_b) = (self.allowance_objects.get(), self.allowance_bytes.get());
|
|
if o <= limit_o && b <= limit_b {
|
|
return Ok(());
|
|
}
|
|
let may_ask = self.framed && self.continues.get() < MAX_CONTINUES;
|
|
if !may_ask || o > limit_o.saturating_mul(2) || b > limit_b.saturating_mul(2) {
|
|
return Err(self.over(OVER_GROWTH));
|
|
}
|
|
self.waiting.set(true);
|
|
self.pause.set(true);
|
|
Ok(())
|
|
}
|
|
|
|
/// The growth [`Host::charge_growth`] has counted, and whether the run waits for an answer about
|
|
/// it, for [`Host::put_growth_back`].
|
|
fn growth_mark(&self) -> (u64, u64, bool) {
|
|
(self.objects.get(), self.bytes_kept.get(), self.waiting.get())
|
|
}
|
|
|
|
/// Puts back the growth counted after [`Host::growth_mark`] gave `mark`: the commands that added
|
|
/// it were put back.
|
|
fn put_growth_back(&self, (objects, bytes, waiting): (u64, u64, bool)) {
|
|
self.objects.set(objects);
|
|
self.bytes_kept.set(bytes);
|
|
self.waiting.set(waiting);
|
|
}
|
|
}
|
|
|
|
/// A run in progress.
|
|
struct Job {
|
|
realm: Realm,
|
|
host: Rc<Host>,
|
|
/// The link the realm's host functions find `host` through.
|
|
link: Link,
|
|
/// The named engine the realm belongs to, which gets it back when the run ends.
|
|
engine: Option<String>,
|
|
/// Between slices the working copy; during the last slice's commit the state before the run.
|
|
world: World,
|
|
/// The documents open when the run began, by [`DocState::uid`].
|
|
start_uids: Vec<u64>,
|
|
name: String,
|
|
/// The file-access scope `script.run` came in, which every slice runs in.
|
|
scope: crate::file_access::ScopeHandle,
|
|
/// The script has ended, and the run waits for the person's growth answer before it is kept.
|
|
finished: bool,
|
|
}
|
|
|
|
/// The `Data` for a JSON value, within `limits`. When it does not fit, the error is the place in
|
|
/// `v` where it stopped fitting, such as `.layers[2].name`, and empty for `v` itself.
|
|
pub(crate) fn to_data(v: &Value, limits: &DataLimits, depth: usize, used: &mut (usize, usize)) -> std::result::Result<Data, String> {
|
|
if depth > limits.depth {
|
|
return Err(String::new());
|
|
}
|
|
used.0 = used.0.saturating_add(16);
|
|
used.1 = used.1.saturating_add(1);
|
|
if used.0 > limits.bytes || used.1 > limits.entries {
|
|
return Err(String::new());
|
|
}
|
|
Ok(match v {
|
|
Value::Null => Data::Null,
|
|
Value::Bool(b) => Data::Bool(*b),
|
|
Value::Number(n) => Data::Number(n.as_f64().unwrap_or(f64::NAN)),
|
|
Value::String(s) => {
|
|
used.0 = used.0.saturating_add(s.len());
|
|
if used.0 > limits.bytes {
|
|
return Err(String::new());
|
|
}
|
|
Data::String(s.clone())
|
|
}
|
|
Value::Array(a) => Data::Array(
|
|
a.iter()
|
|
.enumerate()
|
|
.map(|(i, x)| to_data(x, limits, depth + 1, used).map_err(|place| format!("[{i}]{place}")))
|
|
.collect::<std::result::Result<_, _>>()?,
|
|
),
|
|
Value::Object(o) => Data::Object(
|
|
o.iter()
|
|
.map(|(k, x)| {
|
|
used.0 = used.0.saturating_add(k.len());
|
|
to_data(x, limits, depth + 1, used).map(|d| (k.clone(), d)).map_err(|place| format!(".{k}{place}"))
|
|
})
|
|
.collect::<std::result::Result<_, _>>()?,
|
|
),
|
|
})
|
|
}
|
|
|
|
/// The JSON for a value a script gave; a whole number within a double's exact range is an integer,
|
|
/// which is how commands read ids and counts.
|
|
fn to_json(d: &Data) -> Value {
|
|
match d {
|
|
Data::Undefined | Data::Null => Value::Null,
|
|
Data::Bool(b) => Value::Bool(*b),
|
|
Data::Number(n) =>
|
|
{
|
|
#[allow(clippy::cast_possible_truncation)]
|
|
if n.fract() == 0.0 && n.abs() <= 9_007_199_254_740_992.0 {
|
|
json!(*n as i64)
|
|
} else {
|
|
serde_json::Number::from_f64(*n).map_or(Value::Null, Value::Number)
|
|
}
|
|
}
|
|
Data::String(s) => Value::String(s.clone()),
|
|
Data::Array(a) => Value::Array(a.iter().map(to_json).collect()),
|
|
Data::Object(o) => Value::Object(o.iter().map(|(k, v)| (k.clone(), to_json(v))).collect()),
|
|
}
|
|
}
|
|
|
|
/// A step into a JSON value: a key or an index.
|
|
enum Step<'a> {
|
|
Name(&'a str),
|
|
Index(usize),
|
|
}
|
|
|
|
/// The place in `v` that is more than [`DataLimits`]' depth levels deep, such as `.layers[2].name`,
|
|
/// or `None`. It walks `v` with a list of its own, so any depth fits on the stack.
|
|
pub(crate) fn too_deep(v: &Value) -> Option<String> {
|
|
let limit = DataLimits::default().depth;
|
|
// `steps[i - 1]` is the entry that holds entry `i` and the step into it; entry 0 is `v`.
|
|
let mut steps: Vec<(usize, Step<'_>)> = Vec::new();
|
|
let mut todo = vec![(v, 0usize, 0usize)];
|
|
while let Some((v, depth, entry)) = todo.pop() {
|
|
if depth > limit {
|
|
let mut place = Vec::new();
|
|
let mut at = entry;
|
|
while let Some((parent, step)) = at.checked_sub(1).and_then(|i| steps.get(i)) {
|
|
place.push(match step {
|
|
Step::Name(k) => format!(".{k}"),
|
|
Step::Index(i) => format!("[{i}]"),
|
|
});
|
|
at = *parent;
|
|
}
|
|
place.reverse();
|
|
return Some(place.concat());
|
|
}
|
|
let children: Box<dyn Iterator<Item = (Step<'_>, &Value)>> = match v {
|
|
Value::Array(a) => Box::new(a.iter().enumerate().map(|(i, x)| (Step::Index(i), x))),
|
|
Value::Object(o) => Box::new(o.iter().map(|(k, x)| (Step::Name(k.as_str()), x))),
|
|
_ => continue,
|
|
};
|
|
for (step, child) in children {
|
|
steps.push((entry, step));
|
|
todo.push((child, depth + 1, steps.len()));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
/// Estimated bytes of `d`.
|
|
fn data_size(d: &Data) -> u64 {
|
|
match d {
|
|
Data::Undefined | Data::Null | Data::Bool(_) => 4,
|
|
Data::Number(_) => 8,
|
|
Data::String(s) => s.len() as u64 + 2,
|
|
Data::Array(a) => 8 + a.iter().map(data_size).sum::<u64>(),
|
|
Data::Object(o) => 8 + o.iter().map(|(k, v)| k.len() as u64 + 2 + data_size(v)).sum::<u64>(),
|
|
}
|
|
}
|
|
|
|
/// Brings each open document's next id up to the highest one the session has given out for it.
|
|
fn raise_ids(s: &mut Session) {
|
|
let marks = std::mem::take(&mut s.script_high_water);
|
|
for st in &mut s.docs {
|
|
if let Some(&mark) = marks.get(&st.uid)
|
|
&& mark > st.doc.peek_next_id()
|
|
{
|
|
st.change_unsaved(|d| Arc::make_mut(d).raise_next_id(mark));
|
|
}
|
|
}
|
|
s.script_high_water = marks;
|
|
}
|
|
|
|
/// Every object in `d`: all its art ([`crate::Document::art_roots`]), opacity masks included.
|
|
fn all_objects(d: &crate::Document) -> u64 {
|
|
d.art_roots().map(|n| n.count_with_masks() as u64).sum()
|
|
}
|
|
|
|
fn note_ids(s: &mut Session) {
|
|
for st in &s.docs {
|
|
let mark = s.script_high_water.entry(st.uid).or_insert(0);
|
|
*mark = (*mark).max(st.doc.peek_next_id());
|
|
}
|
|
}
|
|
|
|
/// Runs command `id` inside a slice. A command that fails puts everything back it had done.
|
|
/// → its result, the objects it added, the bytes of document data it added and the state before
|
|
/// it, for a result that cannot cross to the script.
|
|
fn step(s: &mut Session, host: &Host, id: &str, params: &Value) -> Result<(Value, u64, u64, Option<Savepoint>)> {
|
|
raise_ids(s);
|
|
s.script_style_trail = StyleTrail::start(s, dom::holds_resources(host));
|
|
let mutating = waits_for_tool_edit(id);
|
|
// Inside an object-model call's own savepoint ([`dom`]), that savepoint puts a failure back. A
|
|
// query that fills the clipboard (`clipboard.importSvg`) takes one too, for the clipboard.
|
|
let save = ((mutating || CAN_FAIL_AFTER_COPYING.contains(&id)) && !host.dom.saving()).then(|| Savepoint::take(s, id));
|
|
let before: Vec<(u64, Arc<crate::Document>, u64)> =
|
|
if mutating { s.docs.iter().map(|d| (d.uid, d.doc.clone(), d.doc.peek_next_id())).collect() } else { Vec::new() };
|
|
crate::file_access::start_recording_writes();
|
|
// The command reads and writes only where the script's own files may, and the files the person gave
|
|
// the open documents; it also reads the files the documents open when the run began link to, as the
|
|
// person left them. A document a script's command opened gives it no file.
|
|
let documents = s.docs.iter().filter_map(|d| d.as_person_left().and_then(|(_, path, _)| path.map(str::to_string))).collect();
|
|
// The linked files are fixed at the run's first step, also when that step is an exact open that gets none.
|
|
let linked = host.linked();
|
|
let linked = if id == "document.open" && host.disk.borrow().exact { Default::default() } else { linked };
|
|
let gate = host.disk.borrow().gate(documents, linked);
|
|
let was: Vec<(u64, RunDoc)> = s.docs.iter().map(|d| (d.uid, (d.doc.clone(), d.path.clone(), d.links_from.clone()))).collect();
|
|
let r = crate::file_access::with_script_gate(gate, || s.execute(id, params));
|
|
// What the command opened, changed or gave another file is not as the person left it.
|
|
for st in &mut s.docs {
|
|
let prior = was.iter().find(|(uid, _)| *uid == st.uid);
|
|
st.note_script_command(prior.map(|(_, (doc, path, from))| (doc, path, from)));
|
|
}
|
|
drop(was);
|
|
// A file a command wrote before it failed is on disk all the same.
|
|
let mut written = host.written.borrow_mut();
|
|
for path in crate::file_access::finish_recording_writes(r.is_ok()) {
|
|
written.add(path);
|
|
}
|
|
drop(written);
|
|
match r {
|
|
Ok(v) => {
|
|
note_ids(s);
|
|
// A command that is not a batch, such as `edit.undo`, notes the resources it removed too.
|
|
note_resources(s);
|
|
let (mut objects, mut bytes) = (0, 0);
|
|
for st in &s.docs {
|
|
match before.iter().find(|(uid, ..)| *uid == st.uid) {
|
|
Some((_, d, ids)) if !Arc::ptr_eq(d, &st.doc) => {
|
|
objects += st.doc.peek_next_id().saturating_sub(*ids);
|
|
bytes += d.retained_bytes_each(&st.doc).1 as u64;
|
|
}
|
|
Some(_) => {}
|
|
// A document the command opened or made counts whole.
|
|
None if mutating => {
|
|
objects += all_objects(&st.doc);
|
|
bytes += crate::Document::new(1.0, 1.0).retained_bytes_each(&st.doc).1 as u64;
|
|
}
|
|
None => {}
|
|
}
|
|
}
|
|
Ok((v, objects, bytes, save))
|
|
}
|
|
Err(e) => {
|
|
s.script_style_trail = StyleTrail { resources_scanned: s.script_style_trail.resources_scanned, ..Default::default() };
|
|
if let Some(save) = save
|
|
&& !save.restore(s)
|
|
{
|
|
return Err(EngineError::Internal { cmd: id.to_string(), msg: HISTORY_LOST.to_string() });
|
|
}
|
|
Err(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn vc_run(host: &Rc<Host>, args: Vec<Data>) -> std::result::Result<Data, HostError> {
|
|
let Some(Data::String(id)) = args.first() else { return Err(HostError::Type(NO_COMMAND_ID.to_string())) };
|
|
if args.len() > 2 {
|
|
return Err(HostError::Type(TOO_MANY_ARGUMENTS.to_string()));
|
|
}
|
|
let id = id.clone();
|
|
let params = match args.get(1) {
|
|
None | Some(Data::Undefined) => json!({}),
|
|
Some(d @ Data::Object(_)) => to_json(d),
|
|
Some(_) => return Err(HostError::Type(PARAMS_NOT_OBJECT.to_string())),
|
|
};
|
|
let bytes = args.iter().map(data_size).sum();
|
|
host.charge_call(bytes)?;
|
|
bump(&host.counts.vc_run);
|
|
let r = command(host, &id, ¶ms);
|
|
dom::forget_removed(host)?;
|
|
r
|
|
}
|
|
|
|
/// Runs command `id` for the script as `vc.run` does, its call already charged: it joins the run's
|
|
/// transaction, its growth is counted, and a command that fails, or whose result cannot cross to
|
|
/// the script, is put back. → its result.
|
|
fn command(host: &Host, id: &str, params: &Value) -> std::result::Result<Data, HostError> {
|
|
command_value(host, id, params).map(|(data, _)| data)
|
|
}
|
|
|
|
/// [`command`], with the command's result as it gave it as well as the script's copy of it.
|
|
fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(Data, Value), HostError> {
|
|
command_crossing(host, id, params, true).map(|(data, value)| (data.unwrap_or(Data::Undefined), value))
|
|
}
|
|
|
|
/// [`command`] for a member that keeps the command's result itself: the result does not cross to
|
|
/// the script, so it is not held to the limits of a crossing. → the result as the command gave it.
|
|
fn command_kept(host: &Host, id: &str, params: &Value) -> std::result::Result<Value, HostError> {
|
|
command_crossing(host, id, params, false).map(|(_, value)| value)
|
|
}
|
|
|
|
/// [`command_value`], with the script's copy of the result made only when `cross` is true.
|
|
fn command_crossing(host: &Host, id: &str, params: &Value, cross: bool) -> std::result::Result<(Option<Data>, Value), HostError> {
|
|
// Point changes that wait in the object model are written before any command runs.
|
|
dom::flush(host)?;
|
|
let session = host.session.clone();
|
|
let started = clock();
|
|
let before = dom::before_command(host, id);
|
|
let (copies, r) = {
|
|
let mut s = session.borrow_mut();
|
|
(s.copies, step(&mut s, host, id, params))
|
|
};
|
|
// The resource names a failed command's steps compared are still charged, before its error is
|
|
// given.
|
|
if r.is_err() {
|
|
dom::keep_scanned(host);
|
|
dom::charge_scanned(host)?;
|
|
}
|
|
// Only commands that exist are timed, so the table holds at most one row per command.
|
|
if let (Some(started), Some(spec)) = (started, crate::find_command(id)) {
|
|
let nanos = started.elapsed().as_nanos();
|
|
let mut times = host.commands.borrow_mut();
|
|
let row = times.entry(spec.id).or_insert((0, 0, 0));
|
|
*row = (row.0 + 1, row.1.saturating_add(nanos), row.2.max(nanos));
|
|
}
|
|
let (value, objects, added, save) = match r {
|
|
Ok(done) => done,
|
|
// A bug is not something a script can handle, and neither is a request to stop.
|
|
Err(e @ EngineError::Internal { .. }) => return Err(HostError::Abort(e.to_string())),
|
|
Err(EngineError::Cancelled) => return Err(HostError::Abort("canceled".into())),
|
|
Err(e) => return Err(HostError::Error(e.to_string())),
|
|
};
|
|
let mark = host.growth_mark();
|
|
let crossed = host.charge_growth(objects, added).and_then(|()| {
|
|
if !cross {
|
|
return Ok(None);
|
|
}
|
|
let data = to_data(&value, &result_limits(), 0, &mut (0, 0))
|
|
.map_err(|place| HostError::Range(format!("vc.run: the result{place} is too large for a script")))?;
|
|
host.charge_call(data_size(&data))?;
|
|
Ok(Some(data))
|
|
});
|
|
// A command whose result does not reach the script is put back, like one that failed, and so is
|
|
// the growth it was charged.
|
|
if crossed.is_err() {
|
|
host.put_growth_back(mark);
|
|
}
|
|
let mut s = session.borrow_mut();
|
|
if let Some(save) = save {
|
|
if crossed.is_ok() {
|
|
save.keep(&mut s);
|
|
} else if !save.restore(&mut s) {
|
|
return Err(HostError::Abort(HISTORY_LOST.to_string()));
|
|
}
|
|
}
|
|
// A Copy or Cut counts for the run only once what it put on the clipboard is kept.
|
|
if s.copies != copies {
|
|
host.copied.set(true);
|
|
}
|
|
drop(s);
|
|
dom::after_command(host, before);
|
|
// Style objects follow only a command that is kept.
|
|
if crossed.is_ok() {
|
|
// The command stays kept when the look at its changes passes a limit, which ends the run.
|
|
let seen = dom::see_points(host);
|
|
dom::after_kept_command(host);
|
|
seen?;
|
|
} else {
|
|
dom::keep_scanned(host);
|
|
session.borrow_mut().script_style_trail = StyleTrail::default();
|
|
}
|
|
// The resource names the command's steps compared are charged whoever ran it.
|
|
dom::charge_scanned(host)?;
|
|
crossed.map(|data| (data, value))
|
|
}
|
|
|
|
/// A document's uid, revert count, and character and paragraph style lists.
|
|
pub(crate) type StyleLists =
|
|
(u64, u64, vectorcraft_doc::Shared<Vec<vectorcraft_doc::TextStyleDef>>, vectorcraft_doc::Shared<Vec<vectorcraft_doc::TextStyleDef>>);
|
|
|
|
/// What the steps of a command a script runs did to the documents' style lists
|
|
/// ([`note_style_lists`]): each open document's lists as the last step left them, the styles a step
|
|
/// removed or renamed (a document's uid, whether it is a paragraph style, and the name), and the
|
|
/// documents a step reverted. It also holds each open document as the last step left it, and the
|
|
/// swatches, symbols, graphic styles and brushes a step removed or renamed.
|
|
#[derive(Clone, Default)]
|
|
pub(crate) struct StyleTrail {
|
|
lists: Vec<StyleLists>,
|
|
pub(crate) gone: std::collections::HashSet<(u64, bool, String)>,
|
|
pub(crate) reverted: Vec<u64>,
|
|
/// Whether the run holds resource objects, so that the steps note what they remove.
|
|
watch_resources: bool,
|
|
docs: Vec<(u64, Arc<crate::Document>)>,
|
|
pub(in crate::script) resources_gone: std::collections::HashSet<(u64, dom::ResourceKind, String)>,
|
|
/// The resource names the steps compared, charged to the run with the check that follows.
|
|
pub(crate) resources_scanned: u64,
|
|
}
|
|
|
|
impl StyleTrail {
|
|
/// A trail that starts from the documents' style lists now, and from the documents themselves
|
|
/// when `watch_resources`.
|
|
fn start(s: &Session, watch_resources: bool) -> Self {
|
|
let lists = s.docs.iter().map(|st| (st.uid, st.reverts, st.doc.char_styles.clone(), st.doc.para_styles.clone())).collect();
|
|
let docs = if watch_resources { s.docs.iter().map(|st| (st.uid, st.doc.clone())).collect() } else { Vec::new() };
|
|
StyleTrail { lists, docs, watch_resources, ..Default::default() }
|
|
}
|
|
}
|
|
|
|
/// After a step of a batch a script runs: notes the styles the step removed or renamed and the
|
|
/// documents it reverted, so the style objects see a style the batch removed and made again under
|
|
/// its name as another style ([`dom::after_kept_command`]). The trail keeps one copy of each open
|
|
/// document's lists, however many steps change them.
|
|
pub(crate) fn note_style_lists(s: &mut Session) {
|
|
let trail = &mut s.script_style_trail;
|
|
trail.lists.retain(|(uid, ..)| s.docs.iter().any(|d| d.uid == *uid));
|
|
for st in &s.docs {
|
|
let (c, p) = (&st.doc.char_styles, &st.doc.para_styles);
|
|
let Some(last) = trail.lists.iter_mut().find(|(uid, ..)| *uid == st.uid) else {
|
|
trail.lists.push((st.uid, st.reverts, c.clone(), p.clone()));
|
|
continue;
|
|
};
|
|
if last.1 != st.reverts {
|
|
if !trail.reverted.contains(&st.uid) {
|
|
trail.reverted.push(st.uid);
|
|
}
|
|
} else {
|
|
for (para, old, new) in [(false, &last.2, c), (true, &last.3, p)] {
|
|
if vectorcraft_doc::Shared::ptr_eq(old, new) {
|
|
continue;
|
|
}
|
|
let names: std::collections::HashSet<&str> = new.iter().map(|d| d.name.as_str()).collect();
|
|
for d in old.iter().filter(|d| !names.contains(d.name.as_str())) {
|
|
trail.gone.insert((st.uid, para, d.name.clone()));
|
|
}
|
|
}
|
|
}
|
|
*last = (st.uid, st.reverts, c.clone(), p.clone());
|
|
}
|
|
note_resources(s);
|
|
}
|
|
|
|
/// After a step of a batch a script runs, and after each command it runs: notes the swatches,
|
|
/// symbols, styles and brushes the documents no longer have, when the run holds objects for them
|
|
/// ([`dom::after_kept_command`]). A document the trail already compared is not compared again.
|
|
fn note_resources(s: &mut Session) {
|
|
let trail = &mut s.script_style_trail;
|
|
if !trail.watch_resources {
|
|
return;
|
|
}
|
|
trail.docs.retain(|(uid, _)| s.docs.iter().any(|d| d.uid == *uid));
|
|
for st in &s.docs {
|
|
let Some(last) = trail.docs.iter_mut().find(|(uid, _)| *uid == st.uid) else {
|
|
trail.docs.push((st.uid, st.doc.clone()));
|
|
continue;
|
|
};
|
|
if !Arc::ptr_eq(&last.1, &st.doc) {
|
|
let (gone, scanned) = dom::resources_gone(&last.1, &st.doc);
|
|
trail.resources_scanned = trail.resources_scanned.saturating_add(scanned);
|
|
for (kind, name) in gone {
|
|
trail.resources_gone.insert((st.uid, kind, name));
|
|
}
|
|
last.1 = st.doc.clone();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// How many sets of style lists the steps of a batch keep ([`note_style_lists`]).
|
|
#[cfg(test)]
|
|
pub(crate) fn retained_style_lists(s: &Session) -> usize {
|
|
s.script_style_trail.lists.len()
|
|
}
|
|
|
|
/// A bounded snapshot of the active document, or `null` with none open. A snapshot past the limits
|
|
/// of a crossing throws a `RangeError`.
|
|
fn vc_document(host: &Host) -> std::result::Result<Data, HostError> {
|
|
dom::flush(host)?;
|
|
let s = host.session.borrow();
|
|
let Some(st) = s.active() else { return Ok(Data::Null) };
|
|
let ids: Vec<u64> = st.selection.objects.iter().take(1_000).map(|i| i.0).collect();
|
|
let v = json!({
|
|
"name": st.title(),
|
|
"path": st.path,
|
|
"modified": st.is_dirty(),
|
|
"artboards": st.doc.artboards.len(),
|
|
"layers": st.doc.layers.len(),
|
|
"selection": ids,
|
|
"selectionCount": st.selection.objects.len(),
|
|
});
|
|
to_data(&v, &DataLimits::default(), 0, &mut (0, 0)).map_err(|place| HostError::Range(format!("vc.document{place} is too large for a script")))
|
|
}
|
|
|
|
/// Every command a script may run: its id, label and parameters.
|
|
fn vc_commands() -> Data {
|
|
Data::Array(
|
|
command_specs()
|
|
.iter()
|
|
.filter(|c| !refused_to_scripts(c.id))
|
|
.map(|c| {
|
|
Data::Object(vec![
|
|
("id".into(), Data::String(c.id.into())),
|
|
("label".into(), Data::String(c.label.into())),
|
|
("params".into(), Data::String(c.params.into())),
|
|
])
|
|
})
|
|
.collect(),
|
|
)
|
|
}
|
|
|
|
fn vc_log(host: &Host, args: Vec<Data>) -> std::result::Result<Data, HostError> {
|
|
let entry = args.first().unwrap_or(&Data::Undefined);
|
|
let bytes = data_size(entry) as usize;
|
|
if host.log.borrow().len() >= MAX_LOG_LINES || host.log_bytes.get().saturating_add(bytes) > MAX_LOG_BYTES {
|
|
return Err(HostError::Range(LOG_FULL.to_string()));
|
|
}
|
|
host.log_bytes.set(host.log_bytes.get() + bytes);
|
|
let value = to_json(entry);
|
|
let text = match &value {
|
|
Value::String(s) => s.clone(),
|
|
v => v.to_string(),
|
|
};
|
|
host.log.borrow_mut().push(value);
|
|
each_line(&text, |line, cut| host.line(console::Entry::Log(line), cut));
|
|
Ok(Data::Undefined)
|
|
}
|
|
|
|
/// Calls `done` with each console line of `text` and whether that line was cut, one at a time.
|
|
fn each_line(text: &str, mut done: impl FnMut(String, bool)) {
|
|
let mut pending = console::Pending::default();
|
|
pending.write(text, &mut done);
|
|
pending.end_line(done);
|
|
}
|
|
|
|
/// The `$` object: `$.write(...values)` writes the values to the Script Console as `String(value)`
|
|
/// gives them, one after another, and `$.writeln(...values)` does the same and ends the line.
|
|
/// `$.evalFile(path[, timeout])` runs a file and gives its last value, `$.fileName` is the file the
|
|
/// code reading it is in ([`Host::file_name_at`]) and `$.includePath` the `#includepath` list. Its
|
|
/// other members are [`dollar::members`].
|
|
fn dollar_object(link: &Link, info: dollar::RunInfo, localization: &Rc<vectorcraft_js::Localization>) -> Vec<(String, HostItem)> {
|
|
let writer = |end: bool| {
|
|
let l = link.clone();
|
|
HostItem::TextFunction(function(move |a| {
|
|
let h = current(&l)?;
|
|
let text: String = a.iter().map(|d| if let Data::String(s) = d { s.as_str() } else { "" }).collect();
|
|
h.charge_call(text.len() as u64 + 2)?;
|
|
bump(&h.counts.console_writes);
|
|
h.write(&text, end);
|
|
Ok(Data::Undefined)
|
|
}))
|
|
};
|
|
let getter = |read: fn(&include::Files) -> String| {
|
|
let l = link.clone();
|
|
HostItem::Getter(function(move |_| {
|
|
let h = current(&l)?;
|
|
let text = read(&h.files.borrow());
|
|
h.charge_call(text.len() as u64 + 2)?;
|
|
Ok(Data::String(text))
|
|
}))
|
|
};
|
|
let file_name = {
|
|
let l = link.clone();
|
|
HostItem::SiteGetter(Rc::new(move |site| {
|
|
let h = current(&l)?;
|
|
let text = h.file_name_at(site);
|
|
h.charge_call(text.len() as u64 + 2)?;
|
|
Ok(Data::String(text))
|
|
}))
|
|
};
|
|
let eval_file = {
|
|
let (l, end, strict) = (link.clone(), link.clone(), link.clone());
|
|
HostItem::Evaluator {
|
|
source: function(move |a| eval_file(&*current(&l)?, a)),
|
|
end: Rc::new(move || {
|
|
if let Ok(h) = current(&end) {
|
|
h.files.borrow_mut().end_eval();
|
|
}
|
|
}),
|
|
strict_writes: Rc::new(move || current(&strict).ok().and_then(|h| h.eval_strict.get())),
|
|
site: true,
|
|
}
|
|
};
|
|
let mut items = vec![
|
|
("write".into(), writer(false)),
|
|
("writeln".into(), writer(true)),
|
|
("evalFile".into(), eval_file),
|
|
("fileName".into(), file_name),
|
|
("includePath".into(), getter(include::Files::include_path)),
|
|
];
|
|
items.extend(dollar::members(link, info, localization));
|
|
items
|
|
}
|
|
|
|
/// `$.evalFile(path[, timeout])`: the source of the file `path` names, relative to the file being
|
|
/// run (for a call in a module the script imported, that module's file), with its preprocessor
|
|
/// lines read, which the interpreter then runs. `timeout`, in milliseconds, is taken and not
|
|
/// waited on: the file is read at once. `args` starts with the call's place, as
|
|
/// [`HostItem::Evaluator`] gives it with `site`.
|
|
fn eval_file(host: &Host, mut args: Vec<Data>) -> std::result::Result<Data, HostError> {
|
|
let tag = match args.first() {
|
|
Some(Data::Number(n)) if args.len() >= 3 => *n as u32,
|
|
_ => vectorcraft_js::UNKNOWN_SOURCE,
|
|
};
|
|
args.drain(..3.min(args.len()));
|
|
let module_dir = host.module_dir_at(tag);
|
|
let Some(Data::String(path)) = args.first().filter(|p| !matches!(p, Data::String(s) if s.is_empty())) else {
|
|
return Err(HostError::Type(EVAL_NO_PATH.to_string()));
|
|
};
|
|
match args.get(1) {
|
|
None | Some(Data::Undefined) => {}
|
|
Some(Data::Number(ms)) if ms.is_finite() && *ms >= 0.0 => {}
|
|
Some(Data::Number(_)) => return Err(HostError::Range(EVAL_TIMEOUT.to_string())),
|
|
Some(_) => return Err(HostError::Type(EVAL_TIMEOUT.to_string())),
|
|
}
|
|
if args.len() > 2 {
|
|
return Err(HostError::Type(EVAL_ARGUMENTS.to_string()));
|
|
}
|
|
host.charge_call(path.len() as u64 + 2)?;
|
|
let expanded = host.files.borrow_mut().eval_file(path, module_dir).map_err(|f| {
|
|
let message = if f.line > 0 { format!("{}:{}:{}: {}", f.file, f.line, f.column, f.message) } else { f.message };
|
|
let error = match f.kind {
|
|
include::FailKind::Limit => HostError::Abort(message),
|
|
include::FailKind::Syntax => HostError::Syntax(message),
|
|
include::FailKind::Error => HostError::Error(message),
|
|
};
|
|
if f.line > 0 { host.placed(error, f.file, f.line, f.column) } else { error }
|
|
})?;
|
|
// A file without preprocessor lines is parsed here first, so its syntax error names its place.
|
|
let options = host.files.borrow().options();
|
|
if let Err(e) = vectorcraft_js::parse(&expanded.source, &options) {
|
|
host.files.borrow_mut().end_eval();
|
|
let (_, file, line, column) = expanded.map.locate(e.line, e.column);
|
|
let message = format!("{file}:{line}:{column}: {}", e.message);
|
|
return Err(host.placed(HostError::Syntax(message), file, line, column));
|
|
}
|
|
host.eval_maps.borrow_mut().push(expanded.map);
|
|
host.eval_strict.set(expanded.strict);
|
|
Ok(Data::String(expanded.source))
|
|
}
|
|
|
|
fn function(f: impl Fn(Vec<Data>) -> std::result::Result<Data, HostError> + 'static) -> HostFunction {
|
|
Rc::new(f)
|
|
}
|
|
|
|
/// The `vc` object, made once for each realm: its members act for the run `link` holds when they
|
|
/// are called.
|
|
fn vc_object(link: &Link) -> Vec<(String, HostItem)> {
|
|
let args = {
|
|
let l = link.clone();
|
|
function(move |_| {
|
|
let h = current(&l)?;
|
|
let (args, size) = &*h.args.borrow();
|
|
h.charge_call(*size)?;
|
|
Ok(args.clone())
|
|
})
|
|
};
|
|
let run = {
|
|
let l = link.clone();
|
|
function(move |a| vc_run(¤t(&l)?, a))
|
|
};
|
|
let document = {
|
|
let l = link.clone();
|
|
function(move |_| {
|
|
let h = current(&l)?;
|
|
h.charge_call(64)?;
|
|
bump(&h.counts.document_reads);
|
|
vc_document(&h)
|
|
})
|
|
};
|
|
let commands = {
|
|
let l = link.clone();
|
|
let list = Rc::new(vc_commands());
|
|
let size = data_size(&list);
|
|
function(move |_| {
|
|
let h = current(&l)?;
|
|
h.charge_call(size)?;
|
|
bump(&h.counts.command_list_reads);
|
|
Ok((*list).clone())
|
|
})
|
|
};
|
|
let log = {
|
|
let l = link.clone();
|
|
function(move |a| {
|
|
let h = current(&l)?;
|
|
h.charge_call(data_size(a.first().unwrap_or(&Data::Undefined)))?;
|
|
bump(&h.counts.vc_log);
|
|
vc_log(&h, a)
|
|
})
|
|
};
|
|
let has = function(|a| Ok(Data::Bool(matches!(a.first(), Some(Data::String(f)) if API_FEATURES.contains(&f.as_str())))));
|
|
vec![
|
|
("run".into(), HostItem::Function(run)),
|
|
("document".into(), HostItem::Getter(document)),
|
|
("commands".into(), HostItem::Getter(commands)),
|
|
("log".into(), HostItem::Function(log)),
|
|
("args".into(), HostItem::FrozenGetter(args)),
|
|
(
|
|
"api".into(),
|
|
HostItem::Object(vec![("version".into(), HostItem::Value(Data::String(API_VERSION.into()))), ("has".into(), HostItem::Function(has))]),
|
|
),
|
|
]
|
|
}
|
|
|
|
/// A plain-words kind of a run's failure, as the report gives it.
|
|
fn kind_id(kind: &RunErrorKind) -> &'static str {
|
|
match kind {
|
|
RunErrorKind::Syntax => "syntax",
|
|
RunErrorKind::Thrown => "thrown",
|
|
RunErrorKind::Limit(LimitKind::Interrupted) => "stopped",
|
|
RunErrorKind::Limit(_) => "limit",
|
|
RunErrorKind::Internal => "internal",
|
|
}
|
|
}
|
|
|
|
/// `e`'s message, with what to do about a failure that comes from a script written for another
|
|
/// program. A message the script supplied itself ([`RunError::authored`]) is given as it is.
|
|
fn explained(e: &RunError) -> String {
|
|
const HOST_OBJECTS: &[&str] = &["BridgeTalk"];
|
|
let message = &e.message;
|
|
let first = message.strip_prefix("ReferenceError: ").and_then(|m| m.split_whitespace().next());
|
|
let names_host_object = first.is_some_and(|f| HOST_OBJECTS.contains(&f));
|
|
if e.kind == RunErrorKind::Thrown && !e.authored && names_host_object {
|
|
format!("{message} (scripts here use the `vc` object; objects other applications give their scripts are not available)")
|
|
} else {
|
|
message.to_string()
|
|
}
|
|
}
|
|
|
|
/// The module `specifier` names for the module `referrer` of the run of `host`, read through the
|
|
/// run's files. Its source map is kept with the maps of the sources the run evaluates, in the order
|
|
/// the modules are read, which is the order of their sources' tags.
|
|
fn load_module(host: &Host, referrer: &str, specifier: &str) -> std::result::Result<LoadedModule, LoadError> {
|
|
let (key, read) = host.files.borrow_mut().module(referrer, specifier)?;
|
|
let source = read.map(|(text, map)| {
|
|
let mut maps = host.eval_maps.borrow_mut();
|
|
host.module_maps.borrow_mut().insert(maps.len(), key.clone());
|
|
maps.push(map);
|
|
drop(maps);
|
|
host.disk.borrow_mut().saw_code(&text.encode_utf16().collect::<Vec<u16>>());
|
|
text
|
|
});
|
|
Ok(LoadedModule { key, source })
|
|
}
|
|
|
|
/// The error of a run that stopped in its preprocessor lines (`f`), as the realm gives its own.
|
|
fn preprocessor_error(f: include::Failure) -> (RunError, Option<Arc<str>>) {
|
|
let (kind, message) = match f.kind {
|
|
include::FailKind::Syntax if f.line > 0 => {
|
|
(RunErrorKind::Syntax, format!("SyntaxError: {} (line {}, column {})", f.message, f.line, f.column))
|
|
}
|
|
include::FailKind::Syntax => (RunErrorKind::Syntax, format!("SyntaxError: {}", f.message)),
|
|
include::FailKind::Error => (RunErrorKind::Thrown, format!("Error: {}", f.message)),
|
|
include::FailKind::Limit => (RunErrorKind::Limit(LimitKind::Host), f.message),
|
|
};
|
|
let file = (f.index > 0).then_some(f.file);
|
|
(RunError { kind, message, line: f.line, column: f.column, authored: false }, file)
|
|
}
|
|
|
|
/// `e`, the error `realm` raised in the run of `host`, at the line and column of the file it is in,
|
|
/// with that file's name unless it is the run's own script.
|
|
fn located(host: &Host, realm: &Realm, e: &RunError) -> (RunError, Option<Arc<str>>) {
|
|
// An error `$.evalFile` raised for a file it could not run, and no script caught, is at that
|
|
// file's place.
|
|
if let Some((file, line, column)) = realm.error_mark().and_then(|m| host.eval_failures.borrow().get(m as usize).cloned()) {
|
|
let message = if e.kind == RunErrorKind::Syntax { relocated(&e.message, e.line, e.column, line, column) } else { e.message.clone() };
|
|
return (RunError { kind: e.kind.clone(), message, line, column, authored: e.authored }, Some(file));
|
|
}
|
|
placed(host, e, realm.error_source(), realm.error_tag())
|
|
}
|
|
|
|
/// `e`, raised at its line and column of the source `source` with tag `tag` ([`Realm::source_of`]
|
|
/// and [`Realm::error_tag`]), at the line and column of the file it is in, with that file's name
|
|
/// unless it is the run's own script.
|
|
fn placed(host: &Host, e: &RunError, source: u32, tag: u32) -> (RunError, Option<Arc<str>>) {
|
|
let map = |m: &include::SourceMap| {
|
|
let (index, file, line, column) = m.locate(e.line, e.column);
|
|
let message = if e.kind == RunErrorKind::Syntax { relocated(&e.message, e.line, e.column, line, column) } else { e.message.clone() };
|
|
(RunError { kind: e.kind.clone(), message, line, column, authored: e.authored }, index, file)
|
|
};
|
|
if e.line == 0 {
|
|
return (e.clone(), None);
|
|
}
|
|
// An error in code from a source whose map is not at hand, such as code that `eval` compiled,
|
|
// is given without a place.
|
|
let unplaced = || (RunError { line: 0, column: 0, ..e.clone() }, None);
|
|
match source {
|
|
0 => {
|
|
let (e, index, file) = map(&host.script_map.borrow());
|
|
(e, (index > 0).then_some(file))
|
|
}
|
|
n if n != vectorcraft_js::UNKNOWN_SOURCE => match host.eval_maps.borrow().get(n as usize - 1) {
|
|
Some(m) => {
|
|
let (e, _, file) = map(m);
|
|
(e, Some(file))
|
|
}
|
|
None => unplaced(),
|
|
},
|
|
// Code an earlier run of the named engine compiled, such as a function it defined, is at
|
|
// its place in that run's files: the run's script, a file it included, or a file
|
|
// `$.evalFile` gave. A script given as text is named by that run's name.
|
|
_ => {
|
|
let kept = host.kept_maps.as_ref().map(|k| k.borrow());
|
|
match kept.as_ref().and_then(|k| k.get(tag)) {
|
|
Some(kept) => {
|
|
let (e, index, file) = map(&kept.map);
|
|
let file = match &kept.script {
|
|
Some(name) if index == 0 && file.is_empty() => name.clone(),
|
|
_ => file,
|
|
};
|
|
(e, Some(file))
|
|
}
|
|
None => unplaced(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A syntax error's `message`, which ends with its place `(line L, column C)`, with the place
|
|
/// given as `line`, `column`.
|
|
fn relocated(message: &str, old_line: u32, old_column: u32, line: u32, column: u32) -> String {
|
|
let old = format!(" (line {old_line}, column {old_column})");
|
|
match message.strip_suffix(&old) {
|
|
Some(m) => format!("{m} (line {line}, column {column})"),
|
|
None => message.to_string(),
|
|
}
|
|
}
|
|
|
|
impl Session {
|
|
/// Is a script running, or a run `script.startup.run` queued waiting to start at the next
|
|
/// [`Self::script_pump`]? While one is, the commands that change anything are refused.
|
|
pub fn script_running(&self) -> bool {
|
|
self.script_key.is_some() || !self.script_queue.is_empty()
|
|
}
|
|
|
|
/// The named engine of the running script.
|
|
fn running_engine(&self) -> Option<String> {
|
|
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.engine.clone())).flatten()
|
|
}
|
|
|
|
/// The named engines (`#targetengine`) this session keeps, in the order they were made: each
|
|
/// one's name, its heap after its last run in bytes, and whether a run has it now.
|
|
pub fn script_engines(&self) -> Vec<(String, usize, bool)> {
|
|
with_engines(&self.script_engines, |engines| engines.iter().map(|e| (e.name.clone(), e.heap, e.realm.is_none())).collect())
|
|
}
|
|
|
|
/// Drops the named engine `name` and the globals its runs left; a later run that names it gets a
|
|
/// new one.
|
|
pub fn drop_script_engine(&mut self, name: &str) -> Result<()> {
|
|
if name == MAIN_ENGINE {
|
|
return Err(EngineError::Other(DROP_MAIN.to_string()));
|
|
}
|
|
if self.running_engine().as_deref() == Some(name) {
|
|
return Err(EngineError::Busy);
|
|
}
|
|
let gone = with_engines(&self.script_engines, |engines| {
|
|
let at = engines.iter().position(|e| e.name == name)?;
|
|
Some(engines.remove(at))
|
|
});
|
|
match gone {
|
|
Some(engine) => {
|
|
drop(engine);
|
|
self.keep_engines_palettes();
|
|
Ok(())
|
|
}
|
|
None => Err(EngineError::Other(format!("there is no script engine named {name:?}"))),
|
|
}
|
|
}
|
|
|
|
/// The realm of the named engine `name` for a run, with the link its host functions use and the
|
|
/// source maps it kept: the realm its last run left, or `None` for an engine made now. → the
|
|
/// kind and text of the failure when the engine cannot be had.
|
|
fn take_engine(&mut self, name: &str) -> std::result::Result<TakenEngine, (include::FailKind, String)> {
|
|
if cfg!(target_arch = "wasm32") {
|
|
return Err((include::FailKind::Error, NO_ENGINES_ON_WEB.to_string()));
|
|
}
|
|
with_engines(&self.script_engines, |engines| {
|
|
if let Some(e) = engines.iter_mut().find(|e| e.name == name) {
|
|
let realm = e.realm.take().ok_or((include::FailKind::Error, ENGINE_BUSY.to_string()))?;
|
|
return Ok((Some(realm), e.link.clone(), e.maps.clone()));
|
|
}
|
|
if engines.len() >= MAX_ENGINES {
|
|
return Err((
|
|
include::FailKind::Limit,
|
|
format!("a session keeps at most {MAX_ENGINES} script engines; drop one with script.engine.drop"),
|
|
));
|
|
}
|
|
let link: Link = Rc::new(RefCell::new(None));
|
|
let maps = Rc::new(RefCell::new(KeptMaps::default()));
|
|
engines.push(Engine { name: name.to_string(), realm: None, link: link.clone(), heap: 0, maps: maps.clone() });
|
|
Ok((None, link, maps))
|
|
})
|
|
}
|
|
|
|
/// The end of a run in engine `engine` (`None` for a realm that is not kept): the link no longer
|
|
/// holds the run, and the engine keeps `realm`, with the source maps of the code the run
|
|
/// compiled, or is dropped without one.
|
|
fn return_engine(&self, engine: Option<&str>, link: &Link, realm: Option<Realm>) {
|
|
let host = link.borrow_mut().take();
|
|
if let (Some(h), Some(_)) = (&host, &realm) {
|
|
h.keep_maps();
|
|
}
|
|
drop(host);
|
|
let Some(name) = engine else { return };
|
|
let mut realm = realm;
|
|
let heap = realm.as_mut().map(Realm::collect_garbage);
|
|
let gone = with_engines(&self.script_engines, |engines| {
|
|
let at = engines.iter().position(|e| e.name == name)?;
|
|
match (realm.take(), engines.get_mut(at)) {
|
|
(Some(r), Some(e)) => {
|
|
e.realm = Some(r);
|
|
e.heap = heap.unwrap_or(0);
|
|
None
|
|
}
|
|
_ => Some(engines.remove(at)),
|
|
}
|
|
});
|
|
drop(gone);
|
|
drop(realm);
|
|
}
|
|
|
|
/// The end of `job`: its engine gets its realm back, with a run that was stopped where it was
|
|
/// ended, unless the run ended with an internal error, which drops the engine.
|
|
fn end_job(&self, job: Job, internal: bool) {
|
|
let Job { mut realm, link, engine, host, .. } = job;
|
|
drop(host);
|
|
realm.end();
|
|
self.return_engine(engine.as_deref(), &link, (!internal).then_some(realm));
|
|
}
|
|
|
|
/// The flag that stops the script running in this session from any thread, at its next
|
|
/// instruction.
|
|
pub fn script_interrupt(&self) -> Interrupt {
|
|
self.script_stop.clone()
|
|
}
|
|
|
|
/// The report of the run that ended last (`script.run`'s result, or its failure).
|
|
pub fn script_report(&self) -> Option<&Value> {
|
|
self.script_report.as_ref()
|
|
}
|
|
|
|
/// Whether jobs a run prepared for a printer wait to be sent or dropped ([`printing`]).
|
|
pub fn script_prints_waiting(&self) -> bool {
|
|
!self.script_prints.is_empty()
|
|
}
|
|
|
|
/// The jobs that wait, as [`Self::take_script_prints`] would give them.
|
|
pub fn script_prints(&self) -> &[printing::PreparedPrint] {
|
|
&self.script_prints
|
|
}
|
|
|
|
/// The jobs the last run that printed to a printer prepared, which the session no longer holds:
|
|
/// the frontend sends them or drops them ([`printing`]).
|
|
pub fn take_script_prints(&mut self) -> Vec<printing::PreparedPrint> {
|
|
std::mem::take(&mut self.script_prints)
|
|
}
|
|
|
|
/// The Script Console: the lines of this session's runs, oldest first.
|
|
pub fn script_console(&self) -> &console::Console {
|
|
&self.script_console
|
|
}
|
|
|
|
/// Empties the Script Console and sets its counts to zero. The last run's report, and the
|
|
/// report of a running script, keep their lines. → the lines removed.
|
|
pub fn clear_script_console(&mut self) -> usize {
|
|
let n = self.script_console.lines().len();
|
|
self.script_console.clear();
|
|
n
|
|
}
|
|
|
|
/// The error text of the last run when it failed: its name, place, message and kind, and the
|
|
/// files it left written.
|
|
pub fn script_failure_text(&self) -> Option<String> {
|
|
self.script_report.as_ref().filter(|r| r["ok"] == false).map(failure_text)
|
|
}
|
|
|
|
/// [`Self::script_failure_text`] split around the message when that message is text the script
|
|
/// supplied itself (`messageOrigin` `script`): the text before it, the message and the text after
|
|
/// it. `None` when the last run did not fail, or failed with one of the app's own errors.
|
|
pub fn script_authored_failure(&self) -> Option<(String, &str, String)> {
|
|
let r = self.script_report.as_ref().filter(|r| r["ok"] == false && r["messageOrigin"] == "script")?;
|
|
let (before, after) = failure_frame(r);
|
|
Some((before, r["message"].as_str().unwrap_or_default(), after))
|
|
}
|
|
|
|
/// Has the running script used up its growth allowance, so a person must answer? A run asked
|
|
/// to stop no longer waits: the next [`Session::script_pump`] ends it.
|
|
pub fn script_waiting(&self) -> bool {
|
|
!self.script_stop.is_set() && self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.waiting.get())).unwrap_or(false)
|
|
}
|
|
|
|
/// The time left of the running script's `$.sleep`, or none when it is not asleep. A run asked
|
|
/// to stop is not asleep: the next [`Session::script_pump`] ends it.
|
|
pub fn script_sleep_left(&self) -> Option<std::time::Duration> {
|
|
if self.script_stop.is_set() {
|
|
return None;
|
|
}
|
|
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.asleep())).flatten()
|
|
}
|
|
|
|
/// The dialog (`alert`, `confirm` or `prompt`) the running script waits at for the person's
|
|
/// answer. A run asked to stop no longer waits: the next [`Session::script_pump`] ends it.
|
|
pub fn script_dialog(&self) -> Option<ScriptDialog> {
|
|
if self.script_stop.is_set() {
|
|
return None;
|
|
}
|
|
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.dialogs.waiting())).flatten()
|
|
}
|
|
|
|
/// Whether [`Session::script_dialog`] is the confirm the app opened to turn off the external
|
|
/// script warning, which only a person answers Yes to. A script's own confirm with the same
|
|
/// text is not.
|
|
pub fn script_dialog_turns_warning_off(&self) -> bool {
|
|
self.script_dialog().is_some() && self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.dialogs.asks_warning_off())).unwrap_or(false)
|
|
}
|
|
|
|
/// Gives the dialog the running script waits at its answer; the next [`Session::script_pump`]
|
|
/// goes on from the call. → false when nothing waits, the answer is for another kind of
|
|
/// dialog, or a prompt's text is longer than [`dialogs::MAX_PROMPT_BYTES`]. Yes to a confirm the
|
|
/// app opened for the script ([`dialogs::Dialogs::ask`]) also does what it asked.
|
|
pub fn script_answer(&mut self, answer: DialogAnswer) -> bool {
|
|
let Some(k) = self.script_key.as_ref().filter(|_| !self.script_stop.is_set()) else { return false };
|
|
with_job(k, |j| {
|
|
let given = j.host.dialogs.answer(&mut j.realm, answer);
|
|
// Between slices the run's preferences are in its working copy.
|
|
if let Some(dialogs::OnYes::ExternalScriptWarningOff) = j.host.dialogs.take_approved() {
|
|
j.world.choices.prefs.show_external_script_warning = false;
|
|
}
|
|
given
|
|
})
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
/// The ScriptUI dialog the running script's `show()` waits at for what the person does. A run
|
|
/// asked to stop no longer waits: the next [`Session::script_pump`] ends it.
|
|
pub fn script_window(&self) -> Option<ScriptUiDialog> {
|
|
if self.script_stop.is_set() {
|
|
return None;
|
|
}
|
|
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.dialogs.waiting_window())).flatten()
|
|
}
|
|
|
|
/// The ScriptUI dialog whose `show()` the running script is in, whether it waits at it or runs
|
|
/// one of its handlers. A run asked to stop is in none.
|
|
pub fn script_window_showing(&self) -> Option<ScriptUiDialog> {
|
|
if self.script_stop.is_set() {
|
|
return None;
|
|
}
|
|
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.dialogs.showing_window())).flatten()
|
|
}
|
|
|
|
/// Gives the ScriptUI dialog the running script waits at `event`; the next
|
|
/// [`Session::script_pump`] runs the control's handler. → an error, and the dialog still
|
|
/// waits, when nothing waits or the dialog cannot take the event ([`scriptui::check_event`]).
|
|
pub fn script_window_answer(&mut self, event: UiEvent) -> std::result::Result<(), String> {
|
|
let Some(k) = self.script_key.as_ref().filter(|_| !self.script_stop.is_set()) else { return Err("no script dialog waits".into()) };
|
|
with_job(k, |j| j.host.dialogs.window_answer(&mut j.realm, event)).unwrap_or_else(|| Err("no script dialog waits".into()))
|
|
}
|
|
|
|
/// The file dialog (`File.openDialog`, `File.saveDialog`, `Folder.selectDialog` and the objects'
|
|
/// `openDlg`, `saveDlg` and `selectDlg`) the running script waits at. A run asked to stop no
|
|
/// longer waits.
|
|
pub fn script_picker(&self) -> Option<files::ScriptPicker> {
|
|
if self.script_stop.is_set() {
|
|
return None;
|
|
}
|
|
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.disk.borrow().picker.clone())).flatten()
|
|
}
|
|
|
|
/// Gives the file dialog the running script waits at its answer: the paths the person picked
|
|
/// and allowed, or a denial. The next [`Session::script_pump`] goes on from the call. → false
|
|
/// when no file dialog waits.
|
|
pub fn script_pick(&mut self, answer: files::PickAnswer) -> bool {
|
|
let Some(k) = self.script_key.as_ref().filter(|_| !self.script_stop.is_set()) else { return false };
|
|
with_job(k, |j| {
|
|
let Some(data) = j.host.disk.borrow_mut().answer(answer) else { return false };
|
|
j.host.note_written(j.host.disk.borrow_mut().take_written());
|
|
if let Err(e) = j.realm.answer(data) {
|
|
j.host.disk.borrow_mut().failed = Some(e);
|
|
}
|
|
true
|
|
})
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
/// The name of the running script.
|
|
pub fn script_name(&self) -> Option<String> {
|
|
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.name.clone()))
|
|
}
|
|
|
|
/// Lets the run go past its growth allowance by one more allowance.
|
|
pub fn script_continue(&mut self) {
|
|
if let Some(k) = &self.script_key {
|
|
with_job(k, |j| {
|
|
let h = &j.host;
|
|
if h.waiting.replace(false) {
|
|
h.continues.set(h.continues.get() + 1);
|
|
h.allowance_objects.set(h.allowance_objects.get().saturating_add(growth_objects()));
|
|
h.allowance_bytes.set(h.allowance_bytes.get().saturating_add(growth_bytes()));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/// Asks the running script to stop; it is rolled back at the next slice. Between two runs
|
|
/// `script.startup.run` queued, the next one stops as it starts. False with none running.
|
|
pub fn script_request_stop(&mut self) -> bool {
|
|
if !self.script_running() {
|
|
return false;
|
|
}
|
|
self.script_stop.set();
|
|
true
|
|
}
|
|
|
|
/// Stops the running script at once and puts everything back, with the report of a stopped run
|
|
/// ([`Self::script_report`]). Between two runs `script.startup.run` queued, the queued runs are
|
|
/// not started, and the report stays the last run's. False with none running or queued.
|
|
pub fn script_abandon(&mut self) -> bool {
|
|
// The runs queued after it are not started.
|
|
let queued = !self.script_queue.is_empty();
|
|
self.script_queue.clear();
|
|
self.script_queue_scope = None;
|
|
self.script_queue_then = None;
|
|
self.startup_ended();
|
|
let Some(key) = self.script_key.take() else { return queued };
|
|
let Some(job) = JOBS.with(|jobs| jobs.borrow_mut().remove(&key.id)) else {
|
|
self.script_gone(key, console::Ending::Stopped);
|
|
return true;
|
|
};
|
|
drop(key);
|
|
let kind = LimitKind::Interrupted;
|
|
let e = RunError { kind: RunErrorKind::Limit(kind), message: format!("limit reached: {kind:?}"), line: 0, column: 0, authored: false };
|
|
let _ = self.script_failed(job, &e);
|
|
true
|
|
}
|
|
|
|
/// Ends the run whose job is no longer held: its console entry ends with `ending`, with no
|
|
/// report line, since its time and host calls went with the job, and the session's state is
|
|
/// put back as at the end of any run.
|
|
fn script_gone(&mut self, key: JobKey, ending: console::Ending) {
|
|
let line = console::ConsoleLine { run: key.run, name: key.name.clone(), entry: console::Entry::End(ending) };
|
|
self.script_console.push(line, false);
|
|
drop(key);
|
|
// The realm of the job's named engine went with the job, so the engine is dropped.
|
|
let lost: Vec<Engine> = with_engines(&self.script_engines, |engines| {
|
|
let (lost, kept) = std::mem::take(engines).into_iter().partition(|e| e.realm.is_none());
|
|
*engines = kept;
|
|
lost
|
|
});
|
|
drop(lost);
|
|
self.keep_engines_palettes();
|
|
// The job's working copy went with it, with the documents the run opened.
|
|
self.plugin_events_script_ended(false);
|
|
raise_ids(self);
|
|
self.script_stop.clear();
|
|
self.reset_tool();
|
|
cmd::compositefonts::sync(self);
|
|
}
|
|
|
|
/// A script named `name` that did not start because its file could not be read as a script:
|
|
/// the Script Console gets a run with a start line, `message` as its error and an end line.
|
|
pub(crate) fn note_unread(&mut self, name: &str, message: &str) {
|
|
self.script_runs += 1;
|
|
let name = console::line_name(name);
|
|
for entry in [console::Entry::Start, console::Entry::Error(format!("{name}: {message}")), console::Entry::End(console::Ending::Error)] {
|
|
self.script_console.push(console::ConsoleLine { run: self.script_runs, name: name.clone(), entry }, false);
|
|
}
|
|
}
|
|
|
|
/// A script can't start while a document holds a drag in progress or an undo group, which its
|
|
/// one undo step could not take in.
|
|
fn settle_for_script(&mut self) -> Result<()> {
|
|
#[cfg(test)]
|
|
if SETTLE_FAILS.with(Cell::get) {
|
|
return Err(EngineError::Other("the open edit could not be settled".into()));
|
|
}
|
|
self.finish_tool_edit_later()?;
|
|
if self.active().is_some() {
|
|
cmd::settle_interaction(self)?;
|
|
}
|
|
if self.explicit_undo_group.is_some() || self.docs.iter().any(|d| d.undo_group.is_some() || d.interaction.is_some()) {
|
|
return Err(EngineError::Other(OPEN_EDIT.to_string()));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Begins a run of `request`. → the run's name.
|
|
pub(crate) fn script_start(&mut self, request: Request) -> Result<String> {
|
|
let palette_event = self.script_palettes.starting.take();
|
|
self.settle_for_script()?;
|
|
// In a build where a fault ends the program, every unsaved document has a copy first.
|
|
if self.script_copy_first && self.docs.iter().any(DocState::is_dirty) && cmd::recovery::save_copies(self).is_err() {
|
|
return Err(EngineError::Other(NO_RECOVERY_COPY.to_string()));
|
|
}
|
|
// The Artboard tool's selection, which the fresh tool state below drops.
|
|
let picks = cmd::artboard_tool_picks(self);
|
|
let artboard_picks = self.active().filter(|_| !picks.is_empty()).map(|d| (d.uid, picks));
|
|
self.reset_tool();
|
|
let open: Vec<u64> = self.docs.iter().map(|d| d.uid).collect();
|
|
self.script_high_water.retain(|uid, _| open.contains(uid));
|
|
self.script_stop.clear();
|
|
self.script_report = None;
|
|
let mut files = include::Files::new(request.file.as_deref(), request.checked_real, include::Files::jsinclude_env());
|
|
if request.module {
|
|
files.read_es2015();
|
|
}
|
|
// A module (a `.mjs` file, or a script run with `module`) has no preprocessor lines;
|
|
// `module_key` names it among the modules it imports.
|
|
let (expanded, module_key) = if request.module {
|
|
match files.module_entry(&request.source) {
|
|
Ok((key, expanded)) => (Ok(expanded), Some(key)),
|
|
Err(f) => (Err(f), None),
|
|
}
|
|
} else {
|
|
(files.expand_script(&request.source), None)
|
|
};
|
|
let name = match &expanded {
|
|
Ok(include::Expanded { name: Some(name), .. }) if !request.name_given => name.clone(),
|
|
_ => request.name,
|
|
};
|
|
// The engine named by `#targetengine`, other than `main`: its realm, kept from its last run,
|
|
// or none for an engine this run makes.
|
|
let named = match &expanded {
|
|
Ok(include::Expanded { engine: Some((engine, line, column)), .. }) if engine != MAIN_ENGINE => Some((engine.clone(), *line, *column)),
|
|
_ => None,
|
|
};
|
|
let (expanded, kept) = match named {
|
|
None => (expanded, None),
|
|
Some((engine, line, column)) => match self.take_engine(&engine) {
|
|
Ok(kept) => (expanded, Some((engine, kept))),
|
|
Err((kind, message)) => {
|
|
let file = Arc::from(files.file_name());
|
|
(Err(include::Failure { kind, message, index: 0, file, line, column }), None)
|
|
}
|
|
},
|
|
};
|
|
let heap_bytes = if kept.is_some() { engine_heap() } else { HEAP_BYTES };
|
|
let limits = Limits { fuel: request.fuel, heap_bytes, ..Limits::default() };
|
|
let (engine, realm, link, kept_maps) = match kept {
|
|
None => (None, None, Rc::new(RefCell::new(None)), None),
|
|
Some((engine, (realm, link, maps))) => (Some(engine), realm, link, Some(maps)),
|
|
};
|
|
// A realm made here gets the script extensions, `$` and the dialogs; a kept one has them
|
|
// from its first run, and they act for the run that calls them.
|
|
let fresh = realm.is_none();
|
|
// A `.js` script reads ECMAScript 2015; `eval`, `Function`, includes and `$.evalFile` read
|
|
// code the way the script does. Its realm has `Promise`; a kept realm retains it.
|
|
let es2015 = request.module || include::es2015_script(request.file.as_deref());
|
|
// It also reads async functions and `await` (ECMAScript 2017).
|
|
let options =
|
|
Options { max_source_units: include::MAX_TOTAL_BYTES.saturating_mul(2), es2015, es2017: es2015, e4x: true, ..Options::default() };
|
|
let strict = options.strict;
|
|
// A kept engine reads each run's code the way that run's script does.
|
|
let mut realm = match realm {
|
|
Some(mut realm) => {
|
|
realm.set_limits(limits);
|
|
realm.set_es2015(es2015);
|
|
realm.set_es2017(es2015);
|
|
if es2015 {
|
|
realm.add_es2015();
|
|
}
|
|
realm
|
|
}
|
|
None => {
|
|
let mut realm = Realm::with_options(options, limits);
|
|
// Code the run evaluates withdraws the kept file grants not kept for it, before it runs.
|
|
let l = link.clone();
|
|
realm.observe_code(move |units| {
|
|
if let Ok(h) = current(&l) {
|
|
h.disk.borrow_mut().saw_code(units);
|
|
}
|
|
});
|
|
realm
|
|
}
|
|
};
|
|
realm.share_interrupt(&self.script_stop);
|
|
realm.set_data_limits(DataLimits::default());
|
|
self.script_runs += 1;
|
|
let host = Rc::new(Host {
|
|
session: Rc::new(RefCell::new(Session::new())),
|
|
pause: realm.pause_flag(),
|
|
framed: self.script_frames,
|
|
calls: Cell::new(0),
|
|
calls_this_slice: Cell::new(0),
|
|
objects: Cell::new(0),
|
|
bytes_kept: Cell::new(0),
|
|
bridge: Cell::new(0),
|
|
allowance_objects: Cell::new(growth_objects()),
|
|
allowance_bytes: Cell::new(growth_bytes()),
|
|
continues: Cell::new(0),
|
|
waiting: Cell::new(false),
|
|
log: RefCell::new(Vec::new()),
|
|
log_bytes: Cell::new(0),
|
|
written: RefCell::new(Written::default()),
|
|
copied: Cell::new(false),
|
|
run: self.script_runs,
|
|
name: console::line_name(&name),
|
|
console: RefCell::default(),
|
|
pending: RefCell::default(),
|
|
counts: Counts::default(),
|
|
commands: RefCell::default(),
|
|
run_nanos: Cell::new(0),
|
|
dialogs: dialogs::Dialogs::new(self.script_frames, &realm, request.answers),
|
|
disk: RefCell::new(files::Disk::new(
|
|
files.scope(),
|
|
files::Setup {
|
|
framed: self.script_frames,
|
|
script: request.file.clone(),
|
|
// The text with its included files, so a changed included file asks again.
|
|
source: expanded.as_ref().map_or_else(|_| request.source.clone(), |e| e.source.clone()),
|
|
own: palette_event.as_ref().and_then(|q| q.grants.clone()),
|
|
data_dir: self.script_data_dir.clone(),
|
|
engine_code: kept_maps.as_ref().map_or_else(Default::default, |m| {
|
|
let m = m.borrow();
|
|
(m.code.iter().cloned().collect(), m.code_full)
|
|
}),
|
|
},
|
|
)),
|
|
files: RefCell::new(files),
|
|
script_map: RefCell::default(),
|
|
eval_maps: RefCell::default(),
|
|
module_maps: RefCell::default(),
|
|
eval_failures: RefCell::default(),
|
|
script_tag: Cell::new(vectorcraft_js::UNKNOWN_SOURCE),
|
|
report_name: Arc::from(name.as_str()),
|
|
kept_maps,
|
|
started: clock(),
|
|
wake_at: Cell::new(None),
|
|
drawn_at: Cell::new(None),
|
|
can_wait: realm.wait_flag(),
|
|
eval_strict: Cell::new(None),
|
|
dollar: dollar::RunState::new(&request.locale),
|
|
args: RefCell::new((Data::Undefined, 0)),
|
|
engine: engine.clone(),
|
|
file: request.file.clone(),
|
|
palette_event: RefCell::new(palette_event),
|
|
dom: dom::State::new(self.documents(), heap_bytes),
|
|
interface_docs: RefCell::new(Vec::new()),
|
|
actions: request.actions,
|
|
palette_actions: RefCell::new(None),
|
|
active_artboards: self.docs.iter().map(|d| (d.uid, dom::active_artboard(d))).collect(),
|
|
artboard_picks,
|
|
run_docs: RefCell::new(
|
|
self.docs
|
|
.iter()
|
|
.filter_map(DocState::as_person_left)
|
|
.map(|(doc, path, from)| (doc.clone(), path.map(str::to_string), from.map(str::to_string)))
|
|
.collect(),
|
|
),
|
|
linked: std::cell::OnceCell::new(),
|
|
});
|
|
// The printers the frontend listed are this run's alone, and a run may not print while
|
|
// jobs an earlier run prepared still wait for the person.
|
|
host.dom.start_printing(self.script_printers.take(), !self.script_prints.is_empty());
|
|
*link.borrow_mut() = Some(host.clone());
|
|
let args = match to_data(&Value::Object(request.args), &DataLimits::default(), 0, &mut (0, 0)) {
|
|
Ok(args) => args,
|
|
Err(place) => {
|
|
self.return_engine(engine.as_deref(), &link, (!fresh).then_some(realm));
|
|
return Err(EngineError::BadParams { cmd: "script.run".into(), msg: format!("`args{place}` is nested more than 64 levels deep") });
|
|
}
|
|
};
|
|
let size = data_size(&args);
|
|
*host.args.borrow_mut() = (args, size);
|
|
let line = console::ConsoleLine { run: host.run, name: host.name.clone(), entry: console::Entry::Start };
|
|
self.script_console.push(line, false);
|
|
let engine_name = match &expanded {
|
|
Ok(include::Expanded { engine: Some((name, ..)), .. }) => Some(name.clone()),
|
|
_ => None,
|
|
};
|
|
let info = dollar::RunInfo { strict, engine: engine_name };
|
|
let localization = realm.localization();
|
|
dollar::start_run(&localization, &request.locale);
|
|
let started = match expanded {
|
|
Err(f) => Err(preprocessor_error(f)),
|
|
Ok(expanded) => {
|
|
*host.script_map.borrow_mut() = expanded.map;
|
|
realm.set_strict_writes(expanded.strict == Some(true));
|
|
realm.set_script_name(Some(host.name.to_string()));
|
|
if fresh {
|
|
let l = link.clone();
|
|
realm.set_source_locator(Rc::new(move |tag, line| match current(&l) {
|
|
Ok(h) => h.source_place(tag, line),
|
|
Err(_) => ("[unknown]".to_string(), 0),
|
|
}));
|
|
}
|
|
(if fresh { realm.add_script_extensions() } else { Ok(()) })
|
|
.and_then(|()| if fresh { realm.add_unit_values() } else { Ok(()) })
|
|
.and_then(|()| if fresh { realm.define_host_object("vc", vc_object(&link)) } else { Ok(()) })
|
|
.and_then(|()| if fresh { realm.define_host_object("$", dollar_object(&link, info, &localization)) } else { Ok(()) })
|
|
.and_then(|()| if fresh { dialogs::install(&mut realm, &link) } else { Ok(()) })
|
|
.and_then(|()| if fresh { scriptui::install(&mut realm, &link) } else { Ok(()) })
|
|
.and_then(|()| if fresh { dom::install(&mut realm, &link) } else { Ok(()) })
|
|
.and_then(|()| if fresh { files::install(&mut realm, &link) } else { realm.reset_folder_current() })
|
|
.and_then(|()| {
|
|
let r = match &module_key {
|
|
None => realm.start(&expanded.source),
|
|
Some(key) => {
|
|
let limits = ModuleLimits { modules: include::MAX_FILES + 1, depth: include::MAX_DEPTH };
|
|
realm.start_module(key, &expanded.source, limits, &mut |referrer, specifier| load_module(&host, referrer, specifier))
|
|
}
|
|
};
|
|
// Source that does not compile defines no code, so its map is not kept.
|
|
if !matches!(&r, Err(e) if e.kind == RunErrorKind::Syntax) {
|
|
host.script_tag.set(realm.script_tag());
|
|
}
|
|
r
|
|
})
|
|
.map_err(|e| located(&host, &realm, &e))
|
|
}
|
|
};
|
|
if let Err((e, file)) = started {
|
|
// An engine this run made is not kept: only a run that starts makes one. An internal
|
|
// error drops a kept one.
|
|
let keep = !fresh && e.kind != RunErrorKind::Internal;
|
|
self.return_engine(engine.as_deref(), &link, keep.then_some(realm));
|
|
self.keep_engines_palettes();
|
|
let mut report = failure_report(&name, &e, file.as_deref(), &host, 0, 0);
|
|
close_run(self, &host, &mut report, ending(&e.kind));
|
|
let message = failure_text(&report);
|
|
self.script_report = Some(report);
|
|
return Err(EngineError::Other(message));
|
|
}
|
|
let scope = crate::file_access::ScopeHandle::capture();
|
|
let (run, line_name) = (host.run, host.name.clone());
|
|
let job = Job { realm, host, link, engine, world: World::working(self), start_uids: open, name: name.clone(), scope, finished: false };
|
|
let id = NEXT_JOB.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
|
JOBS.with(|jobs| jobs.borrow_mut().insert(id, job));
|
|
self.script_key = Some(JobKey { id, run, name: line_name });
|
|
Ok(name)
|
|
}
|
|
|
|
/// Advances the running script by one slice. `None` while it is still running; otherwise the
|
|
/// run has ended: its report, or the error that ended it, with the documents put back. When
|
|
/// `script.startup.run` queued more runs, the next one starts at the pump after that, so the
|
|
/// ended run's report ([`Self::script_report`]) stays until then; a queued run that cannot start
|
|
/// is kept for [`Self::take_startup_failures`].
|
|
pub fn script_pump(&mut self) -> Option<Result<Value>> {
|
|
if self.script_key.is_none() {
|
|
// A stop asked for between two runs stops the run that starts now, which clears it.
|
|
let stop = self.script_stop.is_set();
|
|
self.start_queued();
|
|
if stop && (self.script_key.is_some() || !self.script_queue.is_empty()) {
|
|
self.script_stop.set();
|
|
} else {
|
|
self.script_stop.clear();
|
|
}
|
|
return None;
|
|
}
|
|
// Held aside while the run ends, so that its commit does not see the queue as a running script.
|
|
let queue = std::mem::take(&mut self.script_queue);
|
|
let result = self.pump_one();
|
|
self.script_queue = queue;
|
|
let result = result?;
|
|
self.startup_ended();
|
|
Some(result)
|
|
}
|
|
|
|
/// [`Self::script_pump`] for the running script alone.
|
|
fn pump_one(&mut self) -> Option<Result<Value>> {
|
|
let key = self.script_key.take()?;
|
|
let Some(mut job) = JOBS.with(|jobs| jobs.borrow_mut().remove(&key.id)) else {
|
|
self.script_gone(key, console::Ending::Internal);
|
|
return Some(Err(EngineError::Other(JOB_GONE.to_string())));
|
|
};
|
|
let picker_failed = job.host.disk.borrow_mut().failed.take();
|
|
if let Some(e) = job.host.dialogs.take_failure().or(picker_failed) {
|
|
return Some(self.script_failed(job, &e));
|
|
}
|
|
// A canceled request ends a sleep or a wait at this pump.
|
|
if vectorcraft_doc::cancel::check().is_err() {
|
|
self.script_stop.set();
|
|
}
|
|
let picking = job.host.disk.borrow().picker.is_some();
|
|
if (job.host.waiting.get() || job.host.asleep().is_some() || job.host.dialogs.is_waiting() || picking) && !self.script_stop.is_set() {
|
|
JOBS.with(|jobs| jobs.borrow_mut().insert(key.id, job));
|
|
self.script_key = Some(key);
|
|
return None;
|
|
}
|
|
// A run whose script ended past its growth allowance is kept once the person lets it go on,
|
|
// and put back when they stop it.
|
|
if job.finished {
|
|
if self.script_stop.is_set() {
|
|
let kind = LimitKind::Interrupted;
|
|
let e =
|
|
RunError { kind: RunErrorKind::Limit(kind), message: format!("limit reached: {kind:?}"), line: 0, column: 0, authored: false };
|
|
return Some(self.script_failed(job, &e));
|
|
}
|
|
// What the script added must fit the allowance the last answer gave: past it, the
|
|
// person is asked again, as a running script is at its next charge.
|
|
if let Err(e) = job.host.charge_growth(0, 0) {
|
|
return Some(self.script_failed(job, &end_flush_error(e)));
|
|
}
|
|
if job.host.waiting.get() {
|
|
JOBS.with(|jobs| jobs.borrow_mut().insert(key.id, job));
|
|
self.script_key = Some(key);
|
|
return None;
|
|
}
|
|
return Some(self.script_ended(job));
|
|
}
|
|
// A stop while a dialog waits ends the run there; the interpreter cannot go on from the call.
|
|
if job.realm.waits() {
|
|
let kind = LimitKind::Interrupted;
|
|
let e = RunError { kind: RunErrorKind::Limit(kind), message: format!("limit reached: {kind:?}"), line: 0, column: 0, authored: false };
|
|
return Some(self.script_failed(job, &e));
|
|
}
|
|
job.host.calls_this_slice.set(0);
|
|
let outcome = run_slice(self, &mut job);
|
|
// A failure a dialog recorded in the slice (steps a show left unused) ends the run, even
|
|
// when the script went on.
|
|
let outcome = match (outcome, job.host.dialogs.take_failure()) {
|
|
(Ok(Ok(_)), Some(e)) => Ok(Err(e)),
|
|
(outcome, _) => outcome,
|
|
};
|
|
match outcome {
|
|
Err(panic) => {
|
|
let e = RunError { kind: RunErrorKind::Internal, message: panic, line: 0, column: 0, authored: false };
|
|
Some(self.script_failed(job, &e))
|
|
}
|
|
Ok(Err(e)) => Some(self.script_failed(job, &e)),
|
|
Ok(Ok(Progress::Paused)) => {
|
|
JOBS.with(|jobs| jobs.borrow_mut().insert(key.id, job));
|
|
self.script_key = Some(key);
|
|
None
|
|
}
|
|
Ok(Ok(Progress::Done(_))) => {
|
|
// What the script added must fit the allowance the last answer gave, which one
|
|
// answer can leave short of what the script had already added.
|
|
if !job.host.waiting.get()
|
|
&& let Err(e) = job.host.charge_growth(0, 0)
|
|
{
|
|
return Some(self.script_failed(job, &end_flush_error(e)));
|
|
}
|
|
// The point changes written as the script ended, or what it added before, went past
|
|
// the growth allowance.
|
|
if job.host.waiting.get() {
|
|
job.finished = true;
|
|
JOBS.with(|jobs| jobs.borrow_mut().insert(key.id, job));
|
|
self.script_key = Some(key);
|
|
return None;
|
|
}
|
|
Some(self.script_ended(job))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Ends the run whose script has ended: it fails when answers it was given went unused, and is
|
|
/// kept otherwise.
|
|
fn script_ended(&mut self, job: Job) -> Result<Value> {
|
|
let n = job.host.dialogs.unused();
|
|
if n > 0 {
|
|
let kind = LimitKind::Host;
|
|
let message = if n == 1 {
|
|
format!("`answers` has {n} answer the script did not use")
|
|
} else {
|
|
format!("`answers` has {n} answers the script did not use")
|
|
};
|
|
return self.script_failed(job, &RunError { kind: RunErrorKind::Limit(kind), message, line: 0, column: 0, authored: false });
|
|
}
|
|
let r = self.script_commit(job);
|
|
self.trim_histories();
|
|
r
|
|
}
|
|
|
|
fn script_failed(&mut self, job: Job, e: &RunError) -> Result<Value> {
|
|
job.host.dialogs.stopped();
|
|
job.host.disk.borrow_mut().picker = None;
|
|
// The run fails already; a file it could not write is left out of `written`.
|
|
let _ = job.host.flush_files();
|
|
let fuel_left = job.realm.fuel_left();
|
|
let (e, file) = located(&job.host, &job.realm, e);
|
|
let mut report = failure_report(&job.name, &e, file.as_deref(), &job.host, fuel_left, job.realm.limits().fuel.saturating_sub(fuel_left));
|
|
close_run(self, &job.host, &mut report, ending(&e.kind));
|
|
self.end_job(job, e.kind == RunErrorKind::Internal);
|
|
self.keep_engines_palettes();
|
|
self.plugin_events_script_ended(false);
|
|
raise_ids(self);
|
|
self.script_stop.clear();
|
|
self.reset_tool();
|
|
cmd::compositefonts::sync(self);
|
|
let message = failure_text(&report);
|
|
self.script_report = Some(report);
|
|
Err(EngineError::Other(message))
|
|
}
|
|
|
|
/// Makes the working copy the session's state: one undo step for each document whose art
|
|
/// changed, nothing for the others.
|
|
fn script_commit(&mut self, mut job: Job) -> Result<Value> {
|
|
let unwritten = job.host.flush_files();
|
|
if !unwritten.is_empty() {
|
|
let message = format!("{UNWRITTEN_FILES}: {}", unwritten.join(", "));
|
|
let e = RunError { kind: RunErrorKind::Thrown, message, line: 0, column: 0, authored: false };
|
|
return self.script_failed(job, &e);
|
|
}
|
|
// The working copy goes into the session; `job.world` holds the state before the run.
|
|
job.world.swap(self);
|
|
self.prefs = merged_prefs(&job.world.start_prefs, &self.prefs, &job.world.choices.prefs);
|
|
// The Caps Lock state takes the run's value only when the run changed it.
|
|
if self.caps_lock == job.world.start_caps_lock {
|
|
self.caps_lock = job.world.choices.caps_lock;
|
|
}
|
|
note_ids(self);
|
|
raise_ids(self);
|
|
let mut start = std::mem::take(&mut job.world.docs);
|
|
for st in &self.docs {
|
|
let changed = start.iter().find(|b| b.uid == st.uid).is_none_or(|b| !st.same_content(&b.doc));
|
|
if changed && (!crate::sanity::doc_sane(&st.doc, &st.selection) || st.doc.too_deep(None)) {
|
|
job.world.docs = start;
|
|
job.world.swap(self);
|
|
let e = RunError { kind: RunErrorKind::Internal, message: INVALID_RESULT.to_string(), line: 0, column: 0, authored: false };
|
|
return self.script_failed(job, &e);
|
|
}
|
|
}
|
|
let mut changed_names = Vec::new();
|
|
for st in &mut self.docs {
|
|
let Some(b) = start.iter_mut().find(|b| b.uid == st.uid) else {
|
|
// A document the script opened or made starts with no undo steps.
|
|
st.history = crate::History { limit: st.history.limit, ..crate::History::default() };
|
|
st.revision += 1;
|
|
changed_names.push(st.title());
|
|
continue;
|
|
};
|
|
let changed = !st.same_content(&b.doc);
|
|
st.history = std::mem::take(&mut b.history);
|
|
st.recovery.clone_from(&b.recovery);
|
|
if changed {
|
|
st.push_undo(STEP_LABEL.to_string(), b.doc.clone(), Arc::new(b.selection.clone()));
|
|
changed_names.push(st.title());
|
|
}
|
|
if changed || st.revision != b.revision {
|
|
st.revision = st.revision.max(b.revision) + 1;
|
|
}
|
|
st.sync_new_art();
|
|
}
|
|
// A document a frontend opened while the run went on stays open.
|
|
for st in start.iter().filter(|b| !job.start_uids.contains(&b.uid)) {
|
|
self.docs.push(st.clone());
|
|
}
|
|
// A document the script closed is gone now, with its recovery copy.
|
|
for b in start.iter().filter(|b| job.start_uids.contains(&b.uid) && !self.docs.iter().any(|d| d.uid == b.uid)) {
|
|
cmd::recovery::forget_closed(self, b);
|
|
}
|
|
// A document the script saved has nothing left to recover.
|
|
cmd::recovery::forget_clean(self);
|
|
let entries: Vec<_> = self.journal.iter().cloned().collect();
|
|
self.journal = std::mem::take(&mut job.world.journal);
|
|
for (id, p) in entries {
|
|
self.journal.push((id, p));
|
|
}
|
|
self.script_stop.clear();
|
|
self.reset_tool();
|
|
cmd::compositefonts::sync(self);
|
|
if std::mem::take(&mut self.overprint_after_run) {
|
|
cmd::colormgmt::turn_on_overprint_preview(self);
|
|
}
|
|
let host = &job.host;
|
|
let mut report = json!({
|
|
"ok": true,
|
|
"name": job.name,
|
|
"changed": changed_names,
|
|
"written": host.written.borrow().paths,
|
|
"log": *host.log.borrow(),
|
|
"fuelUsed": job.realm.limits().fuel - job.realm.fuel_left(),
|
|
"hostCalls": host.calls.get(),
|
|
});
|
|
host.more_written(&mut report);
|
|
let start: Vec<(u64, usize)> = host.dom.boards_at_start.iter().chain(host.dom.boards_chosen_in_new.borrow().iter()).copied().collect();
|
|
if let Some(boards) = dom::layers::active_artboards(&host.dom.active_artboards.borrow(), &start, self.documents()) {
|
|
report["activeArtboards"] = boards;
|
|
}
|
|
// The run ran a Copy or Cut, and the clipboard still holds what one put there.
|
|
if host.copied.get()
|
|
&& self.clipboard_copied
|
|
&& let Some(o) = report.as_object_mut()
|
|
{
|
|
o.insert("copied".into(), json!(true));
|
|
}
|
|
// The jobs it prepared for a printer wait for the frontend now that its changes are kept.
|
|
let prints = host.dom.take_prints();
|
|
if !prints.is_empty() {
|
|
report["prints"] = json!(prints.len());
|
|
self.script_prints = prints;
|
|
}
|
|
let rejections = rejections_report(host, &job.realm);
|
|
if let (false, Some(o)) = (rejections.is_empty(), report.as_object_mut()) {
|
|
o.insert("unhandledRejections".into(), Value::Array(rejections));
|
|
o.insert("unhandledRejectionCount".into(), json!(job.realm.unhandled_rejection_count()));
|
|
}
|
|
close_run(self, host, &mut report, console::Ending::Finished);
|
|
let tool = host.dom.chosen_tool();
|
|
self.end_job(job, false);
|
|
self.plugin_events_script_ended(true);
|
|
// The tool `app.selectTool` chose last becomes the current tool, as a choice on the
|
|
// toolbar makes it; the run's changes are kept when the tool cannot be chosen.
|
|
if let Some(id) = tool
|
|
&& let Err(e) = self.select_tool(id, self.last_view)
|
|
{
|
|
report["toolError"] = json!(e.to_string());
|
|
}
|
|
self.script_report = Some(report.clone());
|
|
Ok(report)
|
|
}
|
|
|
|
/// Runs the running script until it ends.
|
|
pub(crate) fn script_run_to_end(&mut self) -> Result<Value> {
|
|
loop {
|
|
if let Some(result) = self.script_pump() {
|
|
return result;
|
|
}
|
|
if self.script_waiting() || self.script_dialog().is_some() {
|
|
self.script_stop.set();
|
|
}
|
|
// A `$.sleep` waits here, where nothing is drawn, instead of turning the loop over.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
if let Some(left) = self.script_sleep_left() {
|
|
std::thread::sleep(left.min(std::time::Duration::from_millis(100)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) const OPEN_EDIT: &str = "a drag or an undo group is open; finish it before running a script";
|
|
const INVALID_RESULT: &str = "the script left a document that cannot be kept";
|
|
const UNWRITTEN_FILES: &str = "Error: the script left open files that could not be written";
|
|
const JOB_GONE: &str = "the script's run is no longer available";
|
|
const HISTORY_LOST: &str = "a failed command changed undo steps that could not be put back, so the script was stopped";
|
|
// Text a script reads from the errors it catches, or that ends its run with its name and place.
|
|
const OVER_CALLS: &str = "the script made more host calls than one run may make";
|
|
const OVER_BRIDGE: &str = "the script passed more data to and from commands than one run may pass";
|
|
const OVER_GROWTH: &str = "the script added more objects and data to the documents than one run may add";
|
|
const NO_COMMAND_ID: &str = "vc.run: give a command id, as a string";
|
|
const TOO_MANY_ARGUMENTS: &str = "vc.run: takes a command id and an object of parameters";
|
|
const PARAMS_NOT_OBJECT: &str = "vc.run: the parameters must be an object";
|
|
const LOG_FULL: &str = "vc.log: the log of this run is full";
|
|
const NO_RECOVERY_COPY: &str = "a recovery copy could not be written, so the script was not run";
|
|
const EVAL_NO_PATH: &str = "$.evalFile requires a file path";
|
|
const EVAL_TIMEOUT: &str = "$.evalFile: the timeout must be a finite number of milliseconds, 0 or more";
|
|
const EVAL_ARGUMENTS: &str = "$.evalFile: takes a file path and a timeout";
|
|
const NO_RUN: &str = "this function's script run has ended";
|
|
const DROP_MAIN: &str = "the main script engine is new for each run and is never kept, so it cannot be dropped";
|
|
const NO_ENGINES_ON_WEB: &str = "named script engines (#targetengine) are not available on the web";
|
|
const ENGINE_BUSY: &str = "the script engine is in use by another run";
|
|
const UNHANDLED_REJECTION: &str = "unhandled promise rejection";
|
|
const UNHANDLED_REJECTIONS: &str = "unhandled promise rejections";
|
|
|
|
/// The promises `realm`'s run rejected and left without a handler, each with its reason and, when
|
|
/// known, the line, column and file it was rejected at.
|
|
fn rejections_report(host: &Host, realm: &Realm) -> Vec<Value> {
|
|
let mut out = Vec::new();
|
|
for r in realm.unhandled_rejections() {
|
|
let e = RunError { kind: RunErrorKind::Thrown, message: r.message.clone(), line: r.line, column: r.column, authored: r.authored };
|
|
let (e, file) = placed(host, &e, realm.source_of(r.tag), r.tag);
|
|
let mut o = json!({
|
|
"message": e.message,
|
|
"messageOrigin": if e.authored { "script" } else { "built-in" },
|
|
"line": e.line,
|
|
"column": e.column,
|
|
});
|
|
if let (Some(file), Some(map)) = (file, o.as_object_mut()) {
|
|
map.insert("file".into(), json!(&*file));
|
|
}
|
|
out.push(o);
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The report of a run that failed; `file` is the included file the error is in.
|
|
fn failure_report(name: &str, e: &RunError, file: Option<&str>, host: &Host, fuel_left: u64, fuel_used: u64) -> Value {
|
|
let mut report = json!({
|
|
"ok": false,
|
|
"kind": kind_id(&e.kind),
|
|
"message": explained(e),
|
|
"messageOrigin": if e.authored { "script" } else { "built-in" },
|
|
"name": name,
|
|
"line": e.line,
|
|
"column": e.column,
|
|
"written": host.written.borrow().paths,
|
|
"log": *host.log.borrow(),
|
|
"fuelLeft": fuel_left,
|
|
"fuelUsed": fuel_used,
|
|
"hostCalls": host.calls.get(),
|
|
});
|
|
host.more_written(&mut report);
|
|
if let (Some(file), Some(o)) = (file, report.as_object_mut()) {
|
|
o.insert("file".into(), json!(file));
|
|
}
|
|
report
|
|
}
|
|
|
|
/// How the run that failed with `kind` ended, as its console end line gives it.
|
|
fn ending(kind: &RunErrorKind) -> console::Ending {
|
|
match kind {
|
|
RunErrorKind::Syntax | RunErrorKind::Thrown => console::Ending::Error,
|
|
RunErrorKind::Limit(LimitKind::Fuel) => console::Ending::Fuel,
|
|
RunErrorKind::Limit(LimitKind::Host) => console::Ending::HostLimit,
|
|
RunErrorKind::Limit(LimitKind::Interrupted) => console::Ending::Stopped,
|
|
RunErrorKind::Limit(_) => console::Ending::Limit,
|
|
RunErrorKind::Internal => console::Ending::Internal,
|
|
}
|
|
}
|
|
|
|
/// Adds a line of `host`'s run to the session's console and to the run's own.
|
|
fn line_to(s: &mut Session, host: &Host, entry: console::Entry, cut: bool) {
|
|
let line = console::ConsoleLine { run: host.run, name: host.name.clone(), entry };
|
|
s.script_console.push(line.clone(), cut);
|
|
host.console.borrow_mut().push(line, cut);
|
|
}
|
|
|
|
/// The error of the failed run `report` describes, as its console lines give it; `name` is the
|
|
/// run's name in those lines ([`console::line_name`]).
|
|
fn error_text(name: &str, report: &Value) -> String {
|
|
let (line, column) = (report["line"].as_u64().unwrap_or(0), report["column"].as_u64().unwrap_or(0));
|
|
let message = report["message"].as_str().unwrap_or_default();
|
|
match report["file"].as_str() {
|
|
Some(file) if line > 0 => format!("{name}: {file}:{line}:{column}: {message}"),
|
|
_ if line > 0 => format!("{name}:{line}:{column}: {message}"),
|
|
_ => format!("{name}: location unavailable: {message}"),
|
|
}
|
|
}
|
|
|
|
/// The console lines of the error of the failed run `report` describes, each as the console keeps
|
|
/// it, cut to its limit.
|
|
pub(crate) fn error_lines(report: &Value) -> Vec<String> {
|
|
let name = console::line_name(report["name"].as_str().unwrap_or_default());
|
|
let mut lines = vec![];
|
|
each_line(&error_text(&name, report), |line, _| lines.push(line));
|
|
lines
|
|
}
|
|
|
|
/// Ends the console lines of `host`'s run: the line `$.write` left unended, then for a failed run
|
|
/// (`report["ok"]` false) its error as `name:line:column: message`, then an end line and a report
|
|
/// line in the session's console. Adds the run's time, host calls by kind, slowest commands,
|
|
/// console and dialogs to `report`.
|
|
fn close_run(s: &mut Session, host: &Host, report: &mut Value, ending: console::Ending) {
|
|
let mut pending = std::mem::take(&mut *host.pending.borrow_mut());
|
|
if !pending.is_empty() {
|
|
let mut last = None;
|
|
pending.end_line(|line, cut| last = Some((line, cut)));
|
|
if let Some((line, cut)) = last {
|
|
line_to(s, host, console::Entry::Output(line), cut);
|
|
}
|
|
}
|
|
if report["ok"] == false {
|
|
each_line(&error_text(&host.name, report), |line, cut| line_to(s, host, console::Entry::Error(line), cut));
|
|
}
|
|
if let Some(rejections) = report["unhandledRejections"].as_array() {
|
|
for r in rejections {
|
|
let mut placed = r.clone();
|
|
placed["message"] = json!(format!("{UNHANDLED_REJECTION}: {}", r["message"].as_str().unwrap_or_default()));
|
|
each_line(&error_text(&host.name, &placed), |line, cut| line_to(s, host, console::Entry::Error(line), cut));
|
|
}
|
|
let more = report["unhandledRejectionCount"].as_u64().unwrap_or(0).saturating_sub(rejections.len() as u64);
|
|
if more > 0 {
|
|
let text = format!("{}: {more} more {UNHANDLED_REJECTIONS}", host.name);
|
|
each_line(&text, |line, cut| line_to(s, host, console::Entry::Error(line), cut));
|
|
}
|
|
}
|
|
let timing = clock().is_some();
|
|
let ms = millis(host.run_nanos.get());
|
|
let slow = slowest(&host.commands.borrow());
|
|
let slow_json: Vec<Value> = slow.iter().map(|c| json!({"id": c.id, "calls": c.calls, "totalMs": c.total_ms, "maxMs": c.max_ms})).collect();
|
|
let line = |entry| console::ConsoleLine { run: host.run, name: host.name.clone(), entry };
|
|
s.script_console.push(line(console::Entry::End(ending)), false);
|
|
let summary = console::Entry::Report { ms: timing.then_some(ms), host_calls: host.calls.get(), slowest: slow };
|
|
s.script_console.push(line(summary), false);
|
|
if let Some(o) = report.as_object_mut() {
|
|
o.insert("runTimeMs".into(), json!(if timing { ms } else { 0 }));
|
|
o.insert("timingAvailable".into(), json!(timing));
|
|
o.insert("hostCallCounts".into(), host.counts.json());
|
|
o.insert("slowestCommands".into(), Value::Array(slow_json));
|
|
o.insert("console".into(), host.console.borrow().report());
|
|
host.dialogs.report(o);
|
|
}
|
|
}
|
|
|
|
/// The text of a failed run's error.
|
|
fn failure_text(report: &Value) -> String {
|
|
let (before, after) = failure_frame(report);
|
|
format!("{before}{}{after}", report["message"].as_str().unwrap_or_default())
|
|
}
|
|
|
|
/// The text of a failed run's error before its message and after it.
|
|
fn failure_frame(report: &Value) -> (String, String) {
|
|
let s = |k: &str| report[k].as_str().unwrap_or_default();
|
|
let (line, column) = (report["line"].as_u64().unwrap_or(0), report["column"].as_u64().unwrap_or(0));
|
|
let at = match report["file"].as_str() {
|
|
Some(file) if line > 0 => format!(": line {line}, column {column} of {file}"),
|
|
_ if line > 0 => format!(": line {line}, column {column}"),
|
|
_ => String::new(),
|
|
};
|
|
let written = report["written"].as_array().map_or(0, Vec::len);
|
|
let kept = if written > 0 {
|
|
let paths: Vec<&str> = report["written"].as_array().into_iter().flatten().filter_map(Value::as_str).collect();
|
|
format!(" (these files stay written: {})", paths.join(", "))
|
|
} else {
|
|
String::new()
|
|
};
|
|
(format!("{}{at}: ", s("name")), format!(" ({}){kept}", s("kind")))
|
|
}
|
|
|
|
/// A run's slice: the working copy in, the real session lent to the host functions, one measured
|
|
/// slice run in the file-access scope the run began in, and everything back. A panic in the
|
|
/// interpreter is the outer `Err`.
|
|
fn run_slice(s: &mut Session, job: &mut Job) -> std::result::Result<std::result::Result<Progress, RunError>, String> {
|
|
let depth = std::mem::replace(&mut s.depth, 0);
|
|
job.world.swap(s);
|
|
// The interface's documents are the host's to read during the slice, and the job's again after.
|
|
std::mem::swap(&mut job.world.docs, &mut *job.host.interface_docs.borrow_mut());
|
|
// The run's front document draws and converts through its own profiles, and the interface's
|
|
// again after the slice.
|
|
s.sync_front_profiles();
|
|
s.script_in_run = true;
|
|
let cell = job.host.session.clone();
|
|
std::mem::swap(s, &mut *cell.borrow_mut());
|
|
let scope = job.scope.clone();
|
|
let realm = &mut job.realm;
|
|
let named = job.engine.is_some();
|
|
let host = &job.host;
|
|
let started = clock();
|
|
let r = scope.run(|| {
|
|
guard::catch_panic(|| {
|
|
#[cfg(test)]
|
|
if PANIC_IN_SLICE.with(Cell::get) {
|
|
panic!("a slice failed");
|
|
}
|
|
// Point changes that still wait in the object model are written when the script ends. Between
|
|
// slices the run's documents are put aside, so they can wait across slices.
|
|
let progress = realm.advance(SLICE_FUEL).and_then(|progress| {
|
|
if matches!(progress, Progress::Done(_)) {
|
|
dom::flush(host).map_err(end_flush_error)?;
|
|
}
|
|
Ok(progress)
|
|
});
|
|
// A limit that ends a palette's handler which `update()` took leaves the handler's
|
|
// actions in place of the run's: once the run ends, the refresh's getters play the
|
|
// run's own.
|
|
if !matches!(progress, Ok(Progress::Paused)) {
|
|
*host.palette_actions.borrow_mut() = None;
|
|
}
|
|
match progress {
|
|
// The palettes of a named engine show the controls as the run left them. A getter
|
|
// the refresh reads may change points, which are written before the run ends.
|
|
Ok(Progress::Done(v)) if named => realm.refresh_script_ui().and_then(|()| {
|
|
dom::flush(host).map_err(end_flush_error)?;
|
|
Ok(Progress::Done(v))
|
|
}),
|
|
// A failed run keeps its globals, so its palettes show what it left too; a refresh
|
|
// that fails as well (the fuel is spent, a stop) leaves them as they were, and the
|
|
// run's error keeps its place.
|
|
Err(e) if named && e.kind != RunErrorKind::Internal => {
|
|
realm.refresh_script_ui_after_error();
|
|
Err(e)
|
|
}
|
|
r => r,
|
|
}
|
|
})
|
|
});
|
|
if let Some(started) = started {
|
|
job.host.run_nanos.set(job.host.run_nanos.get().saturating_add(started.elapsed().as_nanos()));
|
|
}
|
|
std::mem::swap(s, &mut *cell.borrow_mut());
|
|
s.script_in_run = false;
|
|
// A panic in a member that opened the fence for one command skips its own reset.
|
|
s.script_unfenced = None;
|
|
std::mem::swap(&mut job.world.docs, &mut *job.host.interface_docs.borrow_mut());
|
|
job.world.swap(s);
|
|
s.depth = depth;
|
|
s.sync_front_profiles();
|
|
r
|
|
}
|
|
|
|
/// The error that ends a run when the point changes waiting at the end of its script cannot be
|
|
/// written.
|
|
fn end_flush_error(e: HostError) -> RunError {
|
|
let message = match e {
|
|
HostError::Error(m) | HostError::Type(m) | HostError::Range(m) | HostError::Syntax(m) | HostError::Abort(m) => m,
|
|
HostError::Marked { error, .. } => return end_flush_error(*error),
|
|
HostError::Wait => "a host function waited for its result where the run cannot pause".into(),
|
|
};
|
|
RunError { kind: RunErrorKind::Limit(LimitKind::Host), message, line: 0, column: 0, authored: false }
|
|
}
|
|
|
|
/// Tests: removes the session's running job while the session keeps its hold on it. → whether a
|
|
/// job was removed.
|
|
#[cfg(test)]
|
|
pub(crate) fn forget_job(s: &Session) -> bool {
|
|
s.script_key.as_ref().is_some_and(|k| JOBS.with(|jobs| jobs.borrow_mut().remove(&k.id)).is_some())
|
|
}
|
|
|
|
/// The objects a run may add before it is asked to continue or stop.
|
|
fn growth_objects() -> u64 {
|
|
#[cfg(test)]
|
|
if let Some(n) = TEST_GROWTH.with(Cell::get) {
|
|
return n;
|
|
}
|
|
GROWTH_OBJECTS
|
|
}
|
|
|
|
/// The bytes of new document data a run may keep before it is asked to continue or stop.
|
|
fn growth_bytes() -> u64 {
|
|
#[cfg(test)]
|
|
if let Some(n) = TEST_GROWTH_BYTES.with(Cell::get) {
|
|
return n;
|
|
}
|
|
GROWTH_BYTES
|
|
}
|
|
|
|
/// The data a run may pass to and from commands.
|
|
fn bridge_bytes() -> u64 {
|
|
#[cfg(test)]
|
|
if let Some(n) = TEST_BRIDGE_BYTES.with(Cell::get) {
|
|
return n;
|
|
}
|
|
MAX_BRIDGE_BYTES
|
|
}
|
|
|
|
/// The limits of a command's result on its way to the script.
|
|
fn result_limits() -> DataLimits {
|
|
#[cfg(test)]
|
|
if let Some(bytes) = TEST_RESULT_BYTES.with(Cell::get) {
|
|
return DataLimits { bytes, ..DataLimits::default() };
|
|
}
|
|
DataLimits::default()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
thread_local! {
|
|
/// Tests: the bytes a command's result may hold on its way to the script.
|
|
pub(crate) static TEST_RESULT_BYTES: Cell<Option<usize>> = const { Cell::new(None) };
|
|
/// Tests: makes the next slice panic.
|
|
pub(crate) static PANIC_IN_SLICE: Cell<bool> = const { Cell::new(false) };
|
|
/// Tests: makes settling the open edits before a run fail.
|
|
pub(crate) static SETTLE_FAILS: Cell<bool> = const { Cell::new(false) };
|
|
/// Tests: makes the next command a member runs past the script fence panic before it runs.
|
|
pub(crate) static PANIC_WHILE_UNFENCED: Cell<bool> = const { Cell::new(false) };
|
|
/// Tests: the objects a run may add in place of [`GROWTH_OBJECTS`].
|
|
pub(crate) static TEST_GROWTH: Cell<Option<u64>> = const { Cell::new(None) };
|
|
/// Tests: the bytes a run may keep in place of [`GROWTH_BYTES`].
|
|
pub(crate) static TEST_GROWTH_BYTES: Cell<Option<u64>> = const { Cell::new(None) };
|
|
/// Tests: the data a run may pass to and from commands in place of [`MAX_BRIDGE_BYTES`].
|
|
pub(crate) static TEST_BRIDGE_BYTES: Cell<Option<u64>> = const { Cell::new(None) };
|
|
/// Tests: the swatch, symbol, style and brush objects a run may hold, in place of 100,000.
|
|
pub(crate) static TEST_MAX_SLOTS: Cell<Option<usize>> = const { Cell::new(None) };
|
|
/// Tests: how many times the object model worked out where the points of each subpath of a
|
|
/// path start.
|
|
pub(crate) static LAYOUTS_BUILT: Cell<u64> = const { Cell::new(0) };
|
|
/// Tests: the heap of a named engine in place of [`ENGINE_HEAP_BYTES`].
|
|
pub(crate) static TEST_ENGINE_HEAP: Cell<Option<usize>> = const { Cell::new(None) };
|
|
/// Tests: the bytes of source maps a named engine keeps, in place of [`KEPT_MAP_BYTES`].
|
|
pub(crate) static TEST_KEPT_MAP_BYTES: Cell<Option<usize>> = const { Cell::new(None) };
|
|
/// Tests: the `JSINCLUDE` folders in place of the environment's.
|
|
pub(crate) static TEST_JSINCLUDE: RefCell<Option<Vec<std::path::PathBuf>>> = const { RefCell::new(None) };
|
|
/// Tests: the time since a run started, in place of its clock's.
|
|
pub(crate) static TEST_ELAPSED: Cell<Option<std::time::Duration>> = const { Cell::new(None) };
|
|
/// Tests: runs have no clock, as in a wasm build.
|
|
pub(crate) static TEST_NO_CLOCK: Cell<bool> = const { Cell::new(false) };
|
|
}
|