Scripts: null exportForScreens arguments throw, and a read before a closing brace is shared

- Document.exportForScreens throws a TypeError for null in place of the
  options, itemToExport or fileNamePrefix, as exportFile does for null
  options, instead of taking the defaults.
- The script checker counts a variable read just before the `}` that
  closes a function body (`function get() { return $.global['local'] }`)
  as read in that body, so a later literal's saveAs is no longer exempt.
This commit is contained in:
dotCooCoo committed 2026-12-04 16:04:49 -08:00
1 parent ddfc1c4c7a
commit 8b82ccf327
6 files changed
+50 -7

No files matched your search

@@ -38,6 +38,7 @@
//! accepted at its default and throws at any other value, and a property the pages do not list
//! throws an error that names it, unless it is `undefined`. The options object's class must be
//! the one of `exportFormat`, and an omitted options object takes the defaults a new one holds.
//! `null` in place of the options, `itemToExport` or `fileNamePrefix` throws a TypeError.
use std::rc::Rc;
@@ -442,7 +443,10 @@ fn doc_export_for_screens(h: &Host, a: &[Data]) -> Answer {
let doc = this_doc(h, a)?;
let folder = folder_path(arg(a, 1))?;
let (mut row, class) = format(arg(a, 2))?;
let options = given(arg(a, 3)).filter(|d| !matches!(d, Data::Null));
let options = given(arg(a, 3));
if matches!(options, Some(Data::Null)) {
return Err(HostError::Type("the options must be an object".into()));
}
let mut q = params(class, options)?;
if class.name == PNG24.name {
background(&mut q);
@@ -453,8 +457,8 @@ fn doc_export_for_screens(h: &Host, a: &[Data]) -> Answer {
size(&mut q)?;
}
row.extend(q);
let exports = exports(given(arg(a, 4)).filter(|d| !matches!(d, Data::Null)))?;
let prefix = match given(arg(a, 5)).filter(|d| !matches!(d, Data::Null)) {
let exports = exports(given(arg(a, 4)))?;
let prefix = match given(arg(a, 5)) {
None => None,
Some(Data::String(s)) if s.len() > MAX_TEXT_BYTES => return Err(HostError::Range("fileNamePrefix is too long".into())),
Some(Data::String(s)) => Some(s.clone()),
@@ -466,6 +466,9 @@ fn bad_arguments_are_refused_with_an_error() {
"has no option artboard",
),
(format!("{e}({f}, ExportForScreensType.SE_PNG24, undefined, undefined, 5);"), "fileNamePrefix must be a string"),
(format!("{e}({f}, ExportForScreensType.SE_PNG24, null);"), "the options must be an object"),
(format!("{e}({f}, ExportForScreensType.SE_PNG24, undefined, null);"), "ExportForScreensItemToExport"),
(format!("{e}({f}, ExportForScreensType.SE_PNG24, undefined, undefined, null);"), "fileNamePrefix must be a string"),
(format!("var i = new ExportForScreensItemToExport(); i.artboards = '3'; {e}({f}, ExportForScreensType.SE_PNG24, undefined, i);"), "range"),
];
for (case, wanted) in &cases {
+9
View File
@@ -703,6 +703,9 @@ struct Analyzer<'s, 'a, 'l> {
/// [`Analyzer::shared_early`] had no room for a name: no literal gives methods that are the
/// script's own from then on.
shared_full: bool,
/// The function body that the token being read closed (its `}`): how many bodies were open
/// in it and the names it declared. A value read just before the `}` is read in that body.
closed_body: Option<(usize, Vec<String>)>,
/// The object literals being assigned to a variable, the innermost last, up to
/// [`crate::Limits::nesting`] of them.
objects: Vec<ObjectValue>,
@@ -823,6 +826,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
writers: HashMap::new(),
shared_early: HashMap::new(),
shared_full: false,
closed_body: None,
objects: vec![],
closed: None,
index_receiver: 0,
@@ -917,6 +921,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
fn token(&mut self, mut t: Sig<'s>) {
self.acc.steps = self.acc.steps.saturating_add(1);
self.closed_body = None;
// The name of a grouped value whose last `)` was read, or of a value without parentheses that
// `t` ends, holds the value, or nothing the check follows, before `t` starts a member chain or
// closes a function body.
@@ -1240,7 +1245,11 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
/// in them gets back what it held outside them, and the `var` statement a body was in goes on.
fn close_scopes(&mut self) {
while self.scopes.last().is_some_and(|s| self.depth() < s.depth) {
let open = self.scopes.len();
let Some(scope) = self.scopes.pop() else { return };
if self.closed_body.is_none() {
self.closed_body = Some((open, scope.saved.keys().cloned().collect()));
}
self.var_depth = scope.var_depth;
for (name, saved) in scope.saved {
self.restore(name, saved);
+9 -3
View File
@@ -536,12 +536,18 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
/// [`Analyzer::share_early`], and with `global` set, for the global variable `name` read through
/// the global object (`$.global.local`) even when the body declares a variable of that name.
fn share_early_as(&mut self, name: &str, global: bool) {
let Some(body) = self.scopes.last() else { return };
if (body.saved.contains_key(name) && !global) || self.writers.contains_key(name) {
// A value read just before the `}` that closes a function body is read in that body.
let (open, declared) = match &self.closed_body {
Some((open, names)) => (*open, names.iter().any(|n| n == name)),
None => match self.scopes.last() {
Some(body) => (self.scopes.len(), body.saved.contains_key(name)),
None => return,
},
};
if (declared && !global) || self.writers.contains_key(name) {
return;
}
if self.shared_early.contains_key(name) || self.acc.room(self.shared_early.len()) {
let open = self.scopes.len();
let seen = self.shared_early.entry(name.to_string()).or_insert(open);
*seen = (*seen).max(open);
} else {
+21
View File
@@ -1515,6 +1515,27 @@ fn a_bracketed_global_member_used_as_a_value_ends_the_own_save_as() {
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 10][..]));
}
/// A hoisted function body whose last value, read without a `;` before its closing `}`, is the
/// variable shares it with its caller, as one that ends with `;` does.
#[test]
fn a_variable_read_just_before_a_function_body_closes_ends_the_own_save_as() {
for read in ["$.global['local']", "$.global.local", "this.local", "local"] {
let r = report(&format!(
concat!(
"var f = new File(p);\n",
"function get() {{ return {} }}\n",
"var local = {{saveAs: function () {{}}}};\n",
"get().saveAs = function (f) {{\n",
" app.activeDocument.saveAs(f);\n",
"}};\n",
"local.saveAs(f);\n",
),
read
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 7][..]), "{read}");
}
}
/// `$.global.local.saveAs = …` in a function whose parameter is named `local` replaces the global
/// variable's method; the parameter alone leaves the outer literal's `saveAs` the script's own.
#[test]
+1 -1
View File
@@ -3562,7 +3562,7 @@ through `$`:
`SVGFontType.SVGFONT`, the default, writes text as SVG text elements. A listed option that is not mapped
(`matteColor`) is accepted at its default and throws at any other value, a property the pages do not list throws an
error that names it, an omitted options object takes the defaults a new one holds, and an options object of
another class throws. A bad option value throws an error before anything is written. Each export checks the scale of
another class throws. `null` in place of the options, `itemToExport` or `fileNamePrefix` throws a TypeError. A bad option value throws an error before anything is written. Each export checks the scale of
each of its items before it writes, and an item too large to draw throws after the files before it are written. A
size gives each region its own scale, so a size that only a later export's items make out of range throws after the
earlier exports' files are written. The warnings of each export go to the console after `exportForScreens: `.