mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Merge main into feat/scripts-export-for-screens
This commit is contained in:
commit
0a2d25e334
26 files changed
+1190
-100
No files matched your search
@@ -56,6 +56,9 @@ pub(crate) struct Known {
|
||||
/// A file gives names values the analysis doesn't read ([`Declared::unsure`]): no object counts
|
||||
/// as a Folder object whatever the path names.
|
||||
pub unsure: bool,
|
||||
/// The script includes another file ([`Declared::includes`]), whose code may give a file's
|
||||
/// variables values the scan of that file does not read.
|
||||
pub others: bool,
|
||||
}
|
||||
|
||||
impl Known {
|
||||
@@ -75,6 +78,7 @@ impl Known {
|
||||
/// object only when both sides say so; otherwise it may hold a File object.
|
||||
pub fn add(&mut self, other: Known, entries: usize) {
|
||||
self.unsure |= other.unsure;
|
||||
self.others |= other.others;
|
||||
for (name, chain) in other.bound {
|
||||
if self.bound.len() < entries || self.bound.contains_key(&name) {
|
||||
self.bound.entry(name).or_insert(chain);
|
||||
@@ -286,8 +290,8 @@ struct ObjectValue {
|
||||
/// key's whole value, a bit each ([`Analyzer::writers`]).
|
||||
bits: u8,
|
||||
/// It is the value of the first name a `var` statement declares, and the statement is directly
|
||||
/// in a function body or the global code, not in a block and not after `if (…)`, `else`, `do` or
|
||||
/// a loop's parentheses: only then does the variable hold `bits` after it.
|
||||
/// in a function body or the global code, not in a block and not after `if (…)`, `else`, `do`, a
|
||||
/// loop's parentheses or a label: only then does the variable hold `bits` after it.
|
||||
whole: bool,
|
||||
/// The bit of the method whose value is being read, and how far that value has been read.
|
||||
value: Option<(u8, Value)>,
|
||||
@@ -721,6 +725,8 @@ struct Analyzer<'s, 'a, 'l> {
|
||||
/// A `with` statement had no room in [`Analyzer::withs`]: no call of a literal's own method is
|
||||
/// known from then on.
|
||||
withs_full: bool,
|
||||
/// The script includes another file ([`Known::others`]): no variable joins [`Analyzer::writers`].
|
||||
others: bool,
|
||||
/// The variables that may get another value when code read elsewhere runs: assigned inside a
|
||||
/// function body that does not declare them, or as a property of `this` ([`Analyzer::reassigned_later`]).
|
||||
/// None of them joins [`Analyzer::writers`].
|
||||
@@ -816,6 +822,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
with_head: None,
|
||||
withs: vec![],
|
||||
withs_full: false,
|
||||
others: known.others || declared.includes,
|
||||
later: HashSet::new(),
|
||||
later_full: false,
|
||||
unsure: known.unsure || declared.unsure,
|
||||
@@ -1555,12 +1562,20 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
/// The variable of the [`ObjectValue::whole`] literal whose `}` the token before `t` was joins
|
||||
/// [`Analyzer::writers`] when `t` ends the value (`;`, `,` or a line break). It is read before `t`
|
||||
/// closes a function body, so a literal given to the body's own variable stays inside the body.
|
||||
/// Calls inside a loop that assigns the variable use [`Declared::looped`] to classify its methods.
|
||||
/// No variable joins when code the scan does not follow at that point may give it another value:
|
||||
/// a function body that assigns its name ([`Declared::body_assigned`], [`Analyzer::later`]), a
|
||||
/// function body read before the literal that changes or shares the variable, or assigns a property
|
||||
/// of that name, which may be the global object's ([`Analyzer::shared_early`]), code given as text
|
||||
/// or a `with` statement ([`Analyzer::unsure`]), and an included file ([`Analyzer::others`]). Calls inside a loop that
|
||||
/// assigns the variable use [`Declared::looped`] to classify its methods.
|
||||
fn settle_literal(&mut self, t: &Sig<'s>) {
|
||||
if let Some((name, bits)) = self.closed.take()
|
||||
&& (t.is(";") || t.is(",") || (t.nl && !goes_on_with(t)))
|
||||
&& !self.shared_full
|
||||
&& self.shared_early.get(&name).is_none_or(|&open| open <= self.scopes.len())
|
||||
&& !self.unsure
|
||||
&& !self.others
|
||||
&& !self.declared.body_assigned.contains(name.as_str())
|
||||
&& !self.later_full
|
||||
&& !self.later.contains(&name)
|
||||
&& !self.declared.looped_full
|
||||
@@ -1905,7 +1920,13 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
}
|
||||
// A variable that any assignment gave a value that may be a File object leaves as one.
|
||||
let folders = self.folders.iter().filter(|name| self.folder(name)).cloned().collect();
|
||||
let known = Known { bound: std::mem::take(&mut self.bound), files: std::mem::take(&mut self.files), folders, unsure: self.unsure };
|
||||
let known = Known {
|
||||
bound: std::mem::take(&mut self.bound),
|
||||
files: std::mem::take(&mut self.files),
|
||||
folders,
|
||||
unsure: self.unsure,
|
||||
others: self.others,
|
||||
};
|
||||
(std::mem::take(&mut self.includes), module, known)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,10 @@
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use super::{Acc, Brackets, Lexer, LineBreaks, Read, Sig, T, catalog, for_in, goes_on_after, goes_on_with, is, member_mark, unquote, word};
|
||||
use super::{
|
||||
Acc, Brackets, COMPOUND, Lexer, LineBreaks, Read, Sig, T, Token, catalog, directives, for_in, goes_on_after, goes_on_with, is, member_mark,
|
||||
unquote, word,
|
||||
};
|
||||
|
||||
/// The names that give variables values the analysis doesn't read: a `with` statement makes an
|
||||
/// object's properties stand for names in its body (ECMA-262 3rd edition, section 12.10), and `eval`,
|
||||
@@ -67,6 +70,14 @@ pub(crate) struct Declared<'s> {
|
||||
/// The property names a member assignment writes (`o.d = …`, `o['d'] = …`). The object may be
|
||||
/// the global object, so a variable of that name may get a value the analysis doesn't read.
|
||||
pub(super) rebound: HashSet<String>,
|
||||
/// The names a function body assigns by name (`d = …`, `d += …`, `d++`, `for (d in …)`) other than
|
||||
/// in a `var` statement's declarations, unless a body around the assignment declared the name with
|
||||
/// `var` before it. The function may run at any time after it is defined,
|
||||
/// also before its body in the text when it is a declaration, so a variable of that name may lose
|
||||
/// the value an assignment the analysis read gave it.
|
||||
pub(super) body_assigned: HashSet<&'s str>,
|
||||
/// The file has an `#include` line: the included file's code may give its variables values.
|
||||
pub(super) includes: bool,
|
||||
/// The variables assigned inside a loop, its parentheses included (`name = …`), each by the
|
||||
/// offset of the `{` of the function body that declares it (`None`: the global code's), with the
|
||||
/// outermost loops that assign it, entries of `loop_spans`.
|
||||
@@ -197,7 +208,7 @@ pub(crate) fn declarations<'s>(src: &'s str, acc: &mut Acc) -> Declared<'s> {
|
||||
for t in lexer.by_ref() {
|
||||
match breaks.read(&t) {
|
||||
Read::Skip => {}
|
||||
Read::Directive => d.directive(),
|
||||
Read::Directive => d.directive(&t),
|
||||
Read::Token(sig) => d.token(sig),
|
||||
}
|
||||
}
|
||||
@@ -270,9 +281,43 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
if t.word("function") && !member_mark(h0) && !word(h0, "function") {
|
||||
self.function = Some(depth);
|
||||
}
|
||||
if !self.bodies.is_empty() || self.untracked.is_some() {
|
||||
self.body_assignment(&t, depth);
|
||||
}
|
||||
self.h = [Some(t), h0, h1];
|
||||
}
|
||||
|
||||
/// `t` inside a function body at bracket depth `depth`: the name it assigns, when it assigns one
|
||||
/// by name outside a `var` statement's declarations, joins [`Declared::body_assigned`].
|
||||
fn body_assignment(&mut self, t: &Sig<'s>, depth: usize) {
|
||||
let [h0, h1, _] = self.h;
|
||||
let name = |s: Option<Sig<'s>>| s.filter(|s| s.kind == T::Ident).map(|s| s.text);
|
||||
let declares = word(h1, "var") || (is(h1, ",") && self.var_depth == Some(depth));
|
||||
let assigned = if t.kind == T::Punct && (t.text == "=" || COMPOUND.contains(&t.text) || matches!(t.text, "++" | "--")) {
|
||||
// `d = …`, `d += …` and `d++`, not `o.d = …` (a member: [`Declared::rebound`]) or `var d = …`.
|
||||
name(h0).filter(|_| !member_mark(h1) && !(t.text == "=" && declares))
|
||||
} else if t.kind == T::Ident && (is(h0, "++") || is(h0, "--")) {
|
||||
// `++d`.
|
||||
Some(t.text)
|
||||
} else if t.word("in") && (is(h1, "(") || word(h1, "var")) {
|
||||
// `for (d in …)`.
|
||||
name(h0)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// A name the innermost body around `t` has declared with `var` before it is that body's own. A
|
||||
// name only an outer body declared is that body's variable, which a nested function may assign
|
||||
// before a call in the outer body runs.
|
||||
let own = |name: &str| self.untracked.is_none() && self.bodies.last().is_some_and(|b| b.names.contains(name));
|
||||
if let Some(name) = assigned.filter(|n| !own(n)) {
|
||||
if self.out.body_assigned.contains(name) || self.acc.room(self.out.body_assigned.len()) {
|
||||
self.out.body_assigned.insert(name);
|
||||
} else {
|
||||
self.out.unsure = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `t`, a token before the brackets are followed past it: the body of the innermost loop starts
|
||||
/// at it, and `for`, `while` and `do` start a loop. Past [`crate::Limits::nesting`] loops, any
|
||||
/// variable may be assigned in one ([`Declared::looped_full`]).
|
||||
@@ -613,7 +658,10 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
/// A preprocessor line ends the statement before it, as in [`Analyzer::directive`]. Inside a
|
||||
/// property's name in brackets it hides the tokens before the name (`this[`, a line, `'eval']`),
|
||||
/// so the name may be any.
|
||||
fn directive(&mut self) {
|
||||
fn directive(&mut self, t: &Token<'s>) {
|
||||
if directives::parse(t.text).known == Some("include") {
|
||||
self.out.includes = true;
|
||||
}
|
||||
if self.keys.iter().any(|k| k.member) {
|
||||
self.out.unsure = true;
|
||||
}
|
||||
|
||||
@@ -642,17 +642,25 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
if let Some(name) = early {
|
||||
self.share_early(name);
|
||||
}
|
||||
// A name read in a function body that the body does not declare, as a receiver or otherwise.
|
||||
if let Some(r) = h0.filter(|r| r.kind == T::Ident && !member_mark(h1) && !RESERVED.contains(&r.text)) {
|
||||
// A name read in a function body that the body does not declare, as a receiver or otherwise. A
|
||||
// receiver is settled once its member is read: assigning a property that is not one of the
|
||||
// document methods that write a file (`d.n = 1`) leaves its methods as they are.
|
||||
if let Some(r) = h0.filter(|r| r.kind == T::Ident && !member_mark(h1) && !RESERVED.contains(&r.text) && !t.is(".")) {
|
||||
self.share_early(r.text);
|
||||
}
|
||||
if let Some(r) = h2.filter(|r| is(h1, ".") && r.kind == T::Ident && !member_mark(h3) && !RESERVED.contains(&r.text)) {
|
||||
let own_property = t.is("=") && h0.is_some_and(|m| m.kind == T::Ident && !catalog::DOM_FILE_WRITES.contains(&m.text));
|
||||
if !own_property {
|
||||
self.share_early(r.text);
|
||||
}
|
||||
}
|
||||
if changes {
|
||||
// A property of the global object: `this.local`, `globalThis.local` or `$.global.local`, not
|
||||
// `holder.this.local`; `$.global['local']` and the other indexes of the global object.
|
||||
let global = ((word(h2, "this") || word(h2, "globalThis")) && !member_mark(h3)) || (word(h2, "global") && is(h3, ".") && word(h4, "$"));
|
||||
let rebound = if is(h1, ".") && global {
|
||||
// A member assignment of a property `local` (`o.local = …`, `o['local'] = …`): the object may
|
||||
// be the global object (`this`, `globalThis`, `$.global`, or any object holding it, as
|
||||
// `holder.this`), so the variable of that name may get another value.
|
||||
let rebound = if is(h1, ".") {
|
||||
h0.filter(|s| s.kind == T::Ident).map(|s| s.text.to_string())
|
||||
} else if global_index_closed && is(h0, "]") && is(h2, "[") {
|
||||
} else if is(h0, "]") && is(h2, "[") && h1.is_some_and(|s| s.kind == T::Str) {
|
||||
h1.filter(|s| s.kind == T::Str).map(|s| unquote(s.text, 256))
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -897,7 +897,8 @@ fn try_clauses_and_else_after_a_nested_do_loop_are_in_the_loop() {
|
||||
|
||||
/// A literal after a label's line break may not run, a `this` assignment in a function whose own
|
||||
/// variable hides the global one changes the global, and a property `d` of another object is not
|
||||
/// the variable `d`.
|
||||
/// the variable `d`. A member assignment of a property `g` (`holder.this.g = …`) may be the global
|
||||
/// object's, so `g.saveAs` writes a file.
|
||||
#[test]
|
||||
fn labels_hidden_globals_and_other_objects_keep_writes_known() {
|
||||
let r = report(concat!(
|
||||
@@ -922,7 +923,7 @@ fn labels_hidden_globals_and_other_objects_keep_writes_known() {
|
||||
"function other() { var g; holder.this.g = app.activeDocument; holder.this['g'] = 1; }\n",
|
||||
"other(); g.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 8, 17][..]));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 8, 17, 20][..]));
|
||||
}
|
||||
|
||||
/// A call of another method of such an object may replace its `saveAs` through `this`, and a property
|
||||
@@ -1203,6 +1204,61 @@ fn only_a_whole_var_value_makes_save_as_the_scripts_own() {
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 9, 11, 13, 14, 17][..]));
|
||||
}
|
||||
|
||||
/// A literal's `saveAs` stays the script's own only while no code the check does not follow may give
|
||||
/// the variable another value: a declaration after a label, which `if` may skip, a function body that
|
||||
/// assigns the variable (its own method's, or a declaration's that runs before its text), a member
|
||||
/// assignment of a property of that name, which may be the global object's, a function body that
|
||||
/// assigns a `saveAs` or `exportFile` property of any object, code given as text, a `with` statement
|
||||
/// and an included file each leave the write reported. A function that declares its own variable of the
|
||||
/// name, or assigns another property of it, leaves it the script's own. A nested function that assigns
|
||||
/// a variable its enclosing body declared leaves the write reported too.
|
||||
#[test]
|
||||
fn a_save_as_other_code_may_replace_writes_a_file() {
|
||||
let cases: [(&str, &[usize]); 16] = [
|
||||
("var f = new File(p);\nvar d = app.activeDocument;\nif (flag) label:\nvar d = {saveAs: function () {}};\nd.saveAs(f);\n", &[5]),
|
||||
(
|
||||
"var f = new File(p);\nvar d = {\n saveAs: function () {},\n useDocument: function () {\n d = null;\n d = app.activeDocument;\n }\n};\nd.useDocument();\nd.saveAs(f);\n",
|
||||
&[10],
|
||||
),
|
||||
("var f = new File(p);\nvar d = {saveAs: function () {}};\nswap();\nd.saveAs(f);\nfunction swap() { d = app.activeDocument; }\n", &[4]),
|
||||
("var f = new File(p);\nvar d = {saveAs: function () {}};\nthis.d = app.activeDocument;\nd.saveAs(f);\n", &[4]),
|
||||
("var f = new File(p);\nvar d = {saveAs: function () {}};\neval(s);\nd.saveAs(f);\n", &[4]),
|
||||
("#include \"other.jsx\"\nvar f = new File(p);\nvar d = {saveAs: function () {}};\nd.saveAs(f);\n", &[4]),
|
||||
("var f = new File(p);\nvar d = {saveAs: function () {}};\n#include \"other.jsx\"\nd.saveAs(f);\n", &[4]),
|
||||
("var f = new File(p);\nvar d = {saveAs: function () {}};\nwith ({d: app.activeDocument}) {\n d.saveAs(f);\n}\n", &[4]),
|
||||
(
|
||||
"var f = new File(p);\nfunction replace() { d.saveAs = app.activeDocument.saveAs; }\nvar d = {saveAs: function () {}};\nreplace();\nd.saveAs(f);\n",
|
||||
&[5],
|
||||
),
|
||||
(
|
||||
"var f = new File(p);\nfunction replace(o) { o['exportFile'] = w; }\nvar d = {exportFile: function () {}};\nreplace(d);\nd.exportFile(f);\n",
|
||||
&[5],
|
||||
),
|
||||
("var f = new File(p);\nfunction note() { d.n = 1; }\nvar d = {saveAs: function () {}};\nnote();\nd.saveAs(f);\n", &[]),
|
||||
("var f = new File(p);\nvar d = {saveAs: function () {}};\nfunction g() { var d = app.activeDocument; d = 1; }\nd.saveAs(f);\n", &[]),
|
||||
(
|
||||
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { d = app.activeDocument; }\n}\nrun();\n",
|
||||
&[5],
|
||||
),
|
||||
(
|
||||
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { d += 1; }\n}\n",
|
||||
&[5],
|
||||
),
|
||||
(
|
||||
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { d++; }\n}\n",
|
||||
&[5],
|
||||
),
|
||||
(
|
||||
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { for (d in o) {} }\n}\n",
|
||||
&[5],
|
||||
),
|
||||
];
|
||||
for (src, lines) in cases {
|
||||
let r = report(src);
|
||||
assert_eq!(feature(&r, "file.write").map_or(&[][..], |f| f.lines.as_slice()), lines, "{src}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writer_calls_after_many_assignment_loops_have_bounded_work() {
|
||||
for reinitialize in [false, true] {
|
||||
|
||||
Reference in new issue
Block a user