Scripts: charge resource scans after every command, and read a placed instance's id exactly

- The resource names a command's steps compare are charged when the command
  ends, whether vc.run or the object model ran it, so repeated Swatch.color
  edits over many swatches use up the run's data budget.
- symbolItems.add() reads the new instance's id from the command's result
  as an integer, so an id above 2^53 names the placed instance.
This commit is contained in:
dotCooCoo committed 2026-11-30 15:35:34 -08:00
1 parent f3df0a6478
commit 25d7b3d3ab
4 files changed
+53 -17

No files matched your search

+14 -7
View File
@@ -117,13 +117,21 @@ type GoneResources = (HashSet<(u64, ResourceKind, String)>, u64);
/// What [`after_command`] compares with, taken before each command a script runs
/// ([`variables::before_command`]). The resources earlier commands removed stay noted until the
/// run next looks up a resource object or [`forget_removed`] runs; only the count of the names
/// their steps compared starts again.
/// run next looks up a resource object or [`forget_removed`] runs.
pub(super) fn before_command(h: &Host, id: &str) -> Option<(u64, Vec<crate::doc::DataSet>)> {
h.dom.resources_gone.borrow_mut().1 = 0;
variables::before_command(h, id)
}
/// Charges the run for the resource names the last command's steps compared, whether it was kept
/// or not, and whether `vc.run` or the object model ran it.
pub(super) fn charge_scanned(h: &Host) -> std::result::Result<(), HostError> {
let scanned = std::mem::take(&mut h.dom.resources_gone.borrow_mut().1);
if scanned > 0 {
h.charge_call(scanned)?;
}
Ok(())
}
/// After each command a script runs ([`variables::after_command`]).
pub(super) fn after_command(h: &Host, before: Option<(u64, Vec<crate::doc::DataSet>)>) {
variables::after_command(h, before);
@@ -218,8 +226,7 @@ pub(super) struct State {
locked: RefCell<Vec<LockedList>>,
/// The resources the steps of kept commands removed or renamed, which the next lookup of a
/// resource object or [`forget_removed`] marks stale, and how many names the last command's
/// steps compared, which [`forget_removed`] charges. Each command starts the count again
/// ([`before_command`]).
/// steps compared, which [`charge_scanned`] charges when the command ends.
resources_gone: RefCell<GoneResources>,
/// The swatches, symbols, styles and brushes the run's objects name ([`resources`]).
resources: RefCell<resources::Table>,
@@ -965,8 +972,8 @@ pub(super) fn after_kept_command(h: &Host) {
text::styles::after_command(h);
}
/// After a command that is not kept: the resource names its steps compared are charged with the
/// next [`forget_removed`], and the resources they removed are not marked stale.
/// After a command that is not kept: the resource names its steps compared are charged
/// ([`charge_scanned`]), and the resources they removed are not marked stale.
pub(super) fn keep_scanned(h: &Host) {
let scanned = std::mem::take(&mut h.session.borrow_mut().script_style_trail.resources_scanned);
let mut gone = h.dom.resources_gone.borrow_mut();
+6 -9
View File
@@ -70,9 +70,9 @@ use vectorcraft_doc::Shared;
use vectorcraft_js::{Data, HostClass, HostError, HostFunction, HostItem, HostMember, MemberInfo, Realm, RunError, host_ref};
use super::{
Answer, CROSS_DOCUMENT, Described, Doc, Host, Item, Link, NOT_FOUND, arg, class, colors, command, current, data_size, doc_of, doc_ref, editable,
find_doc, float, given, handle_of, item_of, item_ref, later, named, num, on_doc, page_items, returns, text, this_doc, typed, typename_of, whole,
with_doc,
Answer, CROSS_DOCUMENT, Described, Doc, Host, Item, Link, NOT_FOUND, arg, class, colors, command, command_value, current, data_size, doc_of,
doc_ref, editable, find_doc, float, given, handle_of, item_of, item_ref, later, named, num, on_doc, page_items, returns, text, this_doc, typed,
typename_of, whole, with_doc,
};
use crate::{DocState, Document, NodeId, NodeKind};
@@ -1095,12 +1095,9 @@ fn sitems_add(h: &Host, _: Kind, a: &[Data]) -> Answer {
if sym.doc.uid != doc.uid {
return Err(HostError::Error(CROSS_DOCUMENT.into()));
}
let out = run_on(h, doc, "symbol.place", &json!({"name": sym.name}))?;
let id = match &out {
Data::Object(o) => o.iter().find(|(k, _)| k == "id").and_then(|(_, v)| if let Data::Number(n) = v { whole(*n) } else { None }),
_ => None,
}
.ok_or_else(|| HostError::Error("the instance was not made".into()))?;
// The id is read from the command's own result, which holds every id exactly.
let (_, out) = on_doc(h, doc.uid, true, |h| command_value(h, "symbol.place", &json!({"name": sym.name})))?;
let id = out["id"].as_u64().ok_or_else(|| HostError::Error("the instance was not made".into()))?;
Ok(with_doc(h, doc, |_, st| item_ref(h, st, NodeId(id))))
}
+5 -1
View File
@@ -1418,9 +1418,11 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
let mut s = session.borrow_mut();
step(&mut s, host, id, params)
};
// The resource names a failed command's steps compared are still charged ([`dom::forget_removed`]).
// 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)) {
@@ -1465,6 +1467,8 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
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))
}
@@ -783,6 +783,34 @@ fn a_new_resource_past_the_object_limit_is_not_kept() {
assert_eq!(out.unwrap()["log"], json!([true, 1]), "the duplicate is not kept");
}
#[test]
fn a_placed_symbol_instance_with_a_large_id_keeps_its_id() {
let (mut s, a) = doc();
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
s.execute("symbol.new", &json!({"name": "Mark", "keepArt": true})).unwrap();
s.execute("select.none", &json!({})).unwrap();
let big = (1u64 << 53) + 1;
s.active_mut().unwrap().change_unsaved(|d| std::sync::Arc::make_mut(d).raise_next_id(big));
let out = log(&mut s, "var d = app.activeDocument, i = d.symbolItems.add(d.symbols.getByName('Mark')); i.name = 'big'; vc.log(i.name);");
assert_eq!(out, vec![json!("big")]);
assert_eq!(s.active().unwrap().doc.node(NodeId(big)).and_then(|n| n.name.clone()).as_deref(), Some("big"));
}
#[test]
fn editing_swatch_colors_is_charged_for_the_swatches_each_change_compares() {
let (mut s, _) = doc();
for i in 0..1000 {
s.execute("swatch.new", &json!({"name": format!("P{i}"), "color": "#336699"})).unwrap();
}
let source = "var w = app.activeDocument.swatches.getByName('P0'), a = new RGBColor(), b = new RGBColor(); b.red = 200;\
for (var i = 0; i < 30; i++) w.color = i % 2 ? a : b;";
crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(20_000)));
let r = run(&mut s, source);
crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None));
let e = r.map(|r| panic!("30 swatch color edits over 1000 swatches ran: {r}")).unwrap_err().to_string();
assert!(e.contains("passed more data"), "{e}");
}
#[test]
fn a_swatch_color_converts_through_the_profiles_of_its_own_document() {
use vectorcraft_color::{Color, cms};