mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Merge feat/scripts-api-d1a-app-documents into feat/scripts-api-d4b-text-styles
This commit is contained in:
commit
771383bed8
1307 files changed
+194081
-268410
No files matched your search
@@ -802,6 +802,35 @@ struct Host {
|
||||
args: RefCell<(Data, u64)>,
|
||||
/// The run's state of the script object model (`app`).
|
||||
dom: dom::State,
|
||||
/// 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`.
|
||||
@@ -1046,7 +1075,7 @@ impl Host {
|
||||
}
|
||||
let bridge = self.bridge.get().saturating_add(bytes);
|
||||
self.bridge.set(bridge);
|
||||
if bridge > MAX_BRIDGE_BYTES {
|
||||
if bridge > bridge_bytes() {
|
||||
return Err(self.over(OVER_BRIDGE));
|
||||
}
|
||||
let n = self.calls_this_slice.get() + 1;
|
||||
@@ -1243,9 +1272,19 @@ fn step(s: &mut Session, host: &Host, id: &str, params: &Value) -> Result<(Value
|
||||
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 open documents' files.
|
||||
let gate = host.disk.borrow().gate(s.docs.iter().filter_map(|d| d.path.clone()).collect());
|
||||
// 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();
|
||||
let gate = host.disk.borrow().gate(documents, host.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()) {
|
||||
@@ -1562,7 +1601,7 @@ fn eval_file(host: &Host, args: Vec<Data>) -> std::result::Result<Data, HostErro
|
||||
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 = vectorcraft_js::Options { max_source_units: include::MAX_TOTAL_BYTES, ..vectorcraft_js::Options::default() };
|
||||
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);
|
||||
@@ -1652,16 +1691,10 @@ fn kind_id(kind: &RunErrorKind) -> &'static str {
|
||||
/// program. A message the script supplied itself ([`RunError::authored`]) is given as it is.
|
||||
fn explained(e: &RunError) -> String {
|
||||
const HOST_OBJECTS: &[&str] = &["Window", "BridgeTalk"];
|
||||
const APP_MEMBERS: &[&str] = &["activeDocument", "documents", "selection"];
|
||||
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
|
||||
&& let Some(member) = first.filter(|f| APP_MEMBERS.contains(f))
|
||||
{
|
||||
format!("{message} (use `app.{member}`)")
|
||||
} else if e.kind == RunErrorKind::Thrown && !e.authored && names_host_object {
|
||||
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()
|
||||
@@ -2060,11 +2093,16 @@ impl Session {
|
||||
// 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();
|
||||
let options = Options { max_source_units: include::MAX_TOTAL_BYTES.saturating_mul(2), ..Options::default() };
|
||||
// A `.js` script reads ECMAScript 2015; `eval`, `Function`, includes and `$.evalFile` read
|
||||
// code the way the script does.
|
||||
let es2015 = include::es2015_script(request.file.as_deref());
|
||||
let options = Options { max_source_units: include::MAX_TOTAL_BYTES.saturating_mul(2), es2015, ..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
|
||||
}
|
||||
None => {
|
||||
@@ -2134,6 +2172,14 @@ impl Session {
|
||||
dollar: dollar::RunState::new(&request.locale),
|
||||
args: RefCell::new((Data::Undefined, 0)),
|
||||
dom: dom::State::default(),
|
||||
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(),
|
||||
});
|
||||
*link.borrow_mut() = Some(host.clone());
|
||||
let args = match to_data(&Value::Object(request.args), &DataLimits::default(), 0, &mut (0, 0)) {
|
||||
@@ -2159,6 +2205,7 @@ impl Session {
|
||||
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) {
|
||||
@@ -2167,6 +2214,7 @@ impl Session {
|
||||
}));
|
||||
}
|
||||
(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(()) })
|
||||
@@ -2578,6 +2626,15 @@ fn growth_objects() -> u64 {
|
||||
GROWTH_OBJECTS
|
||||
}
|
||||
|
||||
/// 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)]
|
||||
@@ -2595,6 +2652,8 @@ thread_local! {
|
||||
pub(crate) static PANIC_IN_SLICE: 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 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 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`].
|
||||
|
||||
Reference in new issue
Block a user