Scripts: a path point read before its path's points are removed is stale

A PathPoint kept its index, so after `remove()` of an earlier point it
read and changed the point that moved into its place. A point read
before the script removed a point of its path (`remove()`,
`removeAll()`) or replaced its points (`setEntirePath`) now throws
`Stale path point`.
This commit is contained in:
dotCooCoo committed 2026-11-21 15:14:52 -08:00
1 parent 1548547fc1
commit 07d673898f
4 files changed
+62 -7

No files matched your search

+5 -1
View File
@@ -89,7 +89,8 @@ const ARTBOARD_COORDS: &str = "CoordinateSystem.ARTBOARDCOORDINATESYSTEM";
/// What the object model keeps for one run: its coordinate system, the last list of a document's
/// page items, kept while that document is unchanged, the point changes of a path not written yet,
/// and whether a call's own savepoint ([`atomically`]) is open.
/// how many times it removed or replaced each path's points, and whether a call's own savepoint
/// ([`atomically`]) is open.
#[derive(Default)]
pub(super) struct State {
coords: Cell<Coords>,
@@ -99,6 +100,9 @@ pub(super) struct State {
/// A path and its subpaths with the point changes the run has made to it and not written yet
/// ([`flush`]).
pending: RefCell<Option<(Item, Vec<vectorcraft_geom::path::SubPath>)>>,
/// How many times the run has removed or replaced points of each path, by document uid and
/// path id ([`paths`]).
point_cuts: RefCell<std::collections::HashMap<(u64, u64), u64>>,
saving: Cell<bool>,
}
+28 -5
View File
@@ -28,7 +28,9 @@
//! stroke (solid, gradient or pattern), or black when new art has none. `PathPoints.add()` adds
//! a corner point with no handles at the last point, or at `[0, 0]` on a path with no points.
//! Removing the last points leaves the path, with no points.
//! - A point whose index is past the end of its path throws `Stale path point`.
//! - A point whose index is past the end of its path throws `Stale path point`, and so does a
//! point read before the run removed a point of its path or replaced its points
//! (`PathPoint.remove()`, `PathPoints.removeAll()`, `setEntirePath`).
//! - Shape creators take sizes and radii that are finite and not negative; `sides` is a whole
//! number from 3 to 1000 and `points` one from 2 to 1000. `inscribed` true throws an error that
//! says it is not available yet. `rotateAbout` and `scaleAbout` default to
@@ -56,7 +58,8 @@ use super::{
};
use crate::{DocState, NodeId, NodeKind};
/// The error of a point whose index is past the end of its path.
/// The error of a point whose index is past the end of its path, or that was read before the run
/// removed or replaced points of its path.
pub const STALE_POINT: &str = "Stale path point";
/// The most points `setEntirePath` takes.
pub const MAX_POINTS: usize = 100_000;
@@ -598,6 +601,7 @@ fn path_set_entire(h: &super::Host, a: &[Data]) -> Answer {
let anchors: Vec<Anchor> = script.iter().map(|p| Anchor::corner(f.to_document(c, *p))).collect();
let subs = if anchors.is_empty() { Vec::new() } else { vec![SubPath::new(anchors, closed)] };
write_path(h, item, subs)?;
cut_points(h, item);
Ok(Data::Undefined)
}
@@ -1064,7 +1068,20 @@ fn points_parent(h: &super::Host, a: &[Data]) -> Answer {
fn point_ref(h: &super::Host, item: Item, i: usize) -> Data {
let [high, low] = id_parts(item.id);
host_ref("PathPoint", &[float(h.run), float(item.doc.uid), high, low, float(i as u64)])
host_ref("PathPoint", &[float(h.run), float(item.doc.uid), high, low, float(i as u64), float(point_cuts(h, item))])
}
/// How many times the run has removed or replaced points of `item`.
fn point_cuts(h: &super::Host, item: Item) -> u64 {
h.dom.point_cuts.borrow().get(&(item.doc.uid, item.id.0)).copied().unwrap_or(0)
}
/// Notes that the run removed or replaced points of `item`, which makes the points read before
/// stale.
fn cut_points(h: &super::Host, item: Item) {
let mut cuts = h.dom.point_cuts.borrow_mut();
let n = cuts.entry((item.doc.uid, item.id.0)).or_insert(0);
*n = n.saturating_add(1);
}
fn points_at(h: &super::Host, a: &[Data]) -> Answer {
@@ -1107,16 +1124,21 @@ fn points_add(h: &super::Host, a: &[Data]) -> Answer {
fn points_remove_all(h: &super::Host, a: &[Data]) -> Answer {
let item = points_path(h, a)?;
write_path(h, item, Vec::new())?;
cut_points(h, item);
Ok(Data::Undefined)
}
// ---- PathPoint ----
/// The path and the index of the point `this` names, which must still be in the path.
/// The path and the index of the point `this` names, which must still be in the path, with no
/// point of the path removed or replaced since `this` was read.
pub(super) fn this_point(h: &super::Host, a: &[Data]) -> std::result::Result<(Item, usize), HostError> {
let Some(handle) = handle_of(arg(a, 0), "PathPoint") else { return Err(HostError::Type("this is not a PathPoint".into())) };
let [run, uid, high, low, i] = handle.as_slice() else { return Err(HostError::Type("this is not a PathPoint".into())) };
let [run, uid, high, low, i, cuts] = handle.as_slice() else { return Err(HostError::Type("this is not a PathPoint".into())) };
let item = path_at(h, *run, *uid, *high, *low)?;
if whole(*cuts) != Some(point_cuts(h, item)) {
return Err(HostError::Error(STALE_POINT.into()));
}
let i = whole(*i).and_then(|i| usize::try_from(i).ok()).ok_or_else(|| HostError::Error(STALE_POINT.into()))?;
let n = read_points(h, item, point_count)?;
if i >= n {
@@ -1216,6 +1238,7 @@ fn pt_remove(h: &super::Host, a: &[Data]) -> Answer {
}
}
})?;
cut_points(h, item);
Ok(Data::Undefined)
}
@@ -258,6 +258,32 @@ fn path_point_wrapper_goes_stale_after_remove() {
);
}
#[test]
fn points_read_before_a_removal_or_replacement_go_stale() {
let (mut s, _, _) = doc();
let out = log(
&mut s,
"var p = app.activeDocument.pathItems[0], q = app.activeDocument.pathItems[1];\
var second = p.pathPoints[1], other = q.pathPoints[1]; p.pathPoints[0].remove();\
try { second.anchor = [0, 0]; } catch (e) { vc.log(e.message); } try { second.anchor; } catch (e) { vc.log(e.message); }\
vc.log(other.anchor); var first = p.pathPoints[0]; vc.log(first.anchor);\
p.setEntirePath([[1, 1], [2, 2], [3, 3]]); try { first.anchor; } catch (e) { vc.log(e.message); }\
var again = p.pathPoints[0]; vc.log(again.anchor); p.pathPoints.removeAll(); try { again.anchor; } catch (e) { vc.log(e.message); }",
);
assert_eq!(
out,
vec![
json!("Stale path point"),
json!("Stale path point"),
json!([150, 200]),
json!([40, 280]),
json!("Stale path point"),
json!([1, 1]),
json!("Stale path point")
]
);
}
#[test]
fn fill_color_round_trips_rgb_cmyk_gray_lab() {
let (mut s, a, _) = doc();
+3 -1
View File
@@ -2715,7 +2715,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot
closed, and assigning it opens or closes them all; `setEntirePath` takes at most 100,000 points and gives corner
points with no handles, keeping the path closed when it was; moving `anchor` leaves its handles where they are, and
`pointType` changes only the type; `PathPoints.add()` adds a corner point at the last point, or at `[0, 0]`; a point
whose index is past the end of its path throws `Stale path point`. Color components left out count as 0, `new
whose index is past the end of its path throws `Stale path point`, and so does a point read before the script removed
a point of its path (`remove()`, `removeAll()`) or replaced its points (`setEntirePath`). Color components left out
count as 0, `new
cmykColor()` makes a `CMYKColor`, `LabColor.l` is 0 to 100 and `a` and `b` are -128 to 128, and a component out of
range throws a `RangeError` when the color is assigned, with nothing changed. A component reads with the fewest
decimals, at least 4, that give the stored value back, so assigning a color read from a path leaves the path as it