mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 05:22:58 -05:00
Merge main into feat/scripts-export-autocad
This commit is contained in:
commit
270ffca6ef
126 files changed
+8418
-666
No files matched your search
@@ -573,6 +573,19 @@ fn export_variables_in_brackets_writes_a_file() {
|
||||
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[3, 4][..])]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evaluated_code_keeps_warnings_for_rebound_writer_receivers() {
|
||||
for call in ["local.exportVariables(f);", "local['exportVariables'](f);", "local.saveAs(f);", "local['exportFile'](f);"] {
|
||||
let r = report(&format!(
|
||||
"var local = {{exportVariables: function () {{}}, saveAs: function () {{}}, exportFile: function () {{}}}};\n\
|
||||
eval('local = app.activeDocument');\n{call}\n"
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]), "{call}");
|
||||
let written = r.outside.iter().find(|o| o.id == "filesWritten");
|
||||
assert_eq!(written.map(|o| o.lines.as_slice()), Some(&[3][..]), "{call}");
|
||||
}
|
||||
}
|
||||
|
||||
/// `SaveOptions.SAVECHANGES` makes a document's `close` save it to its own file, read directly, in
|
||||
/// brackets, as a string or through a variable; closing without it, and the other save options,
|
||||
/// write nothing.
|
||||
@@ -592,6 +605,24 @@ fn closing_with_save_changes_writes_a_file() {
|
||||
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[4, 5, 7][..])]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unrelated_save_changes_properties_and_strings_do_not_report_writes() {
|
||||
let r = report(concat!(
|
||||
"var a = other.SAVECHANGES;\n",
|
||||
"var b = other['SAVECHANGES'];\n",
|
||||
"app.activeDocument.close('SAVECHANGES');\n",
|
||||
"var c = holder.SaveOptions.SAVECHANGES;\n",
|
||||
"var d = holder.SaveOptions['SAVECHANGES'];\n",
|
||||
"var e = 'SAVECHANGES';\n",
|
||||
"var key = SaveOptions['SAVECHANGES' + suffix];\n",
|
||||
"var f = SaveOptions.SAVECHANGES;\n",
|
||||
"var g = SaveOptions['SAVECHANGES'];\n",
|
||||
"var h = 'SaveOptions.SAVECHANGES';\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[8, 9, 10][..]));
|
||||
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[8, 9, 10][..])]);
|
||||
}
|
||||
|
||||
/// `saveAs` and `exportFile` called with their names as string literals in brackets write the file
|
||||
/// too; reading the method without calling it does not.
|
||||
#[test]
|
||||
@@ -640,18 +671,16 @@ fn the_scripts_own_save_as_writes_no_file() {
|
||||
fn a_changed_or_shared_own_save_as_writes_a_file() {
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}}; var c = {saveAs: function () {}};\n",
|
||||
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}};\n",
|
||||
"var d = {saveAs: function () {}}; var e = {saveAs: function () {}}; var g = {saveAs: function () {}};\n",
|
||||
"a.saveAs(f); b['saveAs'](f); a.n = 1; a.n;\n",
|
||||
"a.saveAs = app.activeDocument.saveAs;\n",
|
||||
"b['saveAs'] = w;\n",
|
||||
"c = app.activeDocument;\n",
|
||||
"delete d.saveAs;\n",
|
||||
"var h = e;\n",
|
||||
"use(g);\n",
|
||||
"a.saveAs(f);\n",
|
||||
"b.saveAs(f);\n",
|
||||
"c.saveAs(f);\n",
|
||||
"d.saveAs(f);\n",
|
||||
"e.saveAs(f);\n",
|
||||
"g['saveAs'](f);\n",
|
||||
@@ -662,10 +691,125 @@ fn a_changed_or_shared_own_save_as_writes_a_file() {
|
||||
"q['saveAs'](f);\n",
|
||||
"s.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[11, 12, 13, 14, 15, 16, 18, 19, 21, 22][..]));
|
||||
// A computed key may name any property, a variable's on the global object included.
|
||||
let r = report("var f = new File(p);\nvar c = {saveAs: function () {}};\nc[k] = w;\nc.saveAs(f);\n");
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[10, 11, 12, 13, 14, 16, 17, 19, 20][..]));
|
||||
// A computed key may name any variable, so every `saveAs` in the script may write, the ones
|
||||
// before the computed key included.
|
||||
let r = report("var f = new File(p);\nvar c = {saveAs: function () {}};\nc.saveAs(f);\nc[k] = w;\nc.saveAs(f);\n");
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3, 5][..]));
|
||||
}
|
||||
|
||||
/// A variable assigned in a loop may hold a document when a later iteration calls its `saveAs`,
|
||||
/// before the assignment in the text: the call writes a file. One assigned only after a loop does
|
||||
/// not.
|
||||
#[test]
|
||||
fn an_own_save_as_reassigned_later_in_a_loop_writes_a_file() {
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}}; var c = {saveAs: function () {}};\n",
|
||||
"var d = {saveAs: function () {}};\n",
|
||||
"for (var i = 0; i < 2; i++) { a.saveAs(f); a = app.activeDocument; }\n",
|
||||
"while (n--) b.saveAs(f), b = app.activeDocument;\n",
|
||||
"do { c.saveAs(f); } while ((c = next()));\n",
|
||||
"for (;;) { d.saveAs(f); break; }\n",
|
||||
"d = app.activeDocument;\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 5, 6][..]));
|
||||
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[4, 5, 6][..])]);
|
||||
// The `else` of a body without braces is in the loop; a statement after a line break that ends
|
||||
// such a body is not, and neither is a variable of the same name that a function declares.
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var e = {saveAs: function () {}}; var g = {saveAs: function () {}}; var h = {saveAs: function () {}};\n",
|
||||
"while (n--) if (n) e.saveAs(f); else e = app.activeDocument;\n",
|
||||
"while (n--) g.saveAs(f), n\n",
|
||||
"g = app.activeDocument\n",
|
||||
"function k() { var h; for (;;) h = app.activeDocument; }\n",
|
||||
"h.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
|
||||
// An object literal's `}` does not end a body without braces, a call's `)` before a line break
|
||||
// does, and a literal given after the last loop that assigns its variable is the script's own.
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}};\n",
|
||||
"while (n--) a.saveAs(f), t = {}, a = app.activeDocument;\n",
|
||||
"b.saveAs(f);\n",
|
||||
"while (n--) tick()\n",
|
||||
"b = app.activeDocument\n",
|
||||
"for (;;) { c = app.activeDocument; }\n",
|
||||
"var c = {saveAs: function () {}};\n",
|
||||
"c.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var a = {saveAs: function () {}};\n",
|
||||
"while (n--) if (n)\n",
|
||||
" a.saveAs(f), a = app.activeDocument;\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4][..]));
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var a = {saveAs: function () {}}; var b = {saveAs: function () {}};\n",
|
||||
"a.saveAs(f); b['saveAs'](f);\n",
|
||||
"while (n--) { a.saveAs(f); a = app.activeDocument; }\n",
|
||||
"while (n--) { b['saveAs'](f); b = app.activeDocument; }\n",
|
||||
"var a = {saveAs: function () {}};\n",
|
||||
"a.saveAs(f);\n",
|
||||
"while (n--) { a.saveAs(f); a = app.activeDocument; }\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 5, 8][..]));
|
||||
}
|
||||
|
||||
/// The `catch` and `finally` of a `try` body, and an `else` after a `do` loop inside a body without
|
||||
/// braces, are in the loop; a statement after the whole loop is not.
|
||||
#[test]
|
||||
fn try_clauses_and_else_after_a_nested_do_loop_are_in_the_loop() {
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var d = {saveAs: function () {}}; var e = {saveAs: function () {}}; var g = {saveAs: function () {}};\n",
|
||||
"var h = {saveAs: function () {}};\n",
|
||||
"while (i++ < 2) try { d.saveAs(f); } finally { d = app.activeDocument; }\n",
|
||||
"while (i++ < 2)\n",
|
||||
" if (i === 2) do e.saveAs(f); while (false);\n",
|
||||
" else e = app.activeDocument;\n",
|
||||
"while (n--) do g.saveAs(f); while (false);\n",
|
||||
"g = app.activeDocument;\n",
|
||||
"while (n--) h.saveAs(f), holder.finally\n",
|
||||
"h = app.activeDocument;\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 6][..]));
|
||||
}
|
||||
|
||||
/// 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`. 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!(
|
||||
"var f = new File(p);\n",
|
||||
"var a = app.activeDocument;\n",
|
||||
"if (no) l:\n",
|
||||
"var a = {saveAs: function () {}};\n",
|
||||
"a.saveAs(f);\n",
|
||||
"var b = {saveAs: function () {}};\n",
|
||||
"function change() { var b; this.b = app.activeDocument; }\n",
|
||||
"change(); b.saveAs(f);\n",
|
||||
"var c = {saveAs: function () {}};\n",
|
||||
"function keep() { var c; c = 1; }\n",
|
||||
"keep(); c.saveAs(f);\n",
|
||||
"var d = {saveAs: function () {}};\n",
|
||||
"holder.d['saveAs'] = 1;\n",
|
||||
"d.saveAs(f);\n",
|
||||
"var e = {saveAs: function () {}};\n",
|
||||
"e['saveAs'] = app.activeDocument.saveAs;\n",
|
||||
"e.saveAs(f);\n",
|
||||
"var g = {saveAs: function () {}};\n",
|
||||
"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, 20][..]));
|
||||
}
|
||||
|
||||
/// Only a `var` statement directly in a function body or the global code that gives its first name
|
||||
@@ -752,6 +896,114 @@ fn a_save_as_other_code_may_replace_writes_a_file() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writer_calls_after_many_assignment_loops_have_bounded_work() {
|
||||
for reinitialize in [false, true] {
|
||||
let mut src = "var d = {saveAs: function () {}};\n".to_string();
|
||||
for _ in 0..512 {
|
||||
src.push_str("while (n--) { d = next(); }\n");
|
||||
}
|
||||
if reinitialize {
|
||||
src.push_str("var d = {saveAs: function () {}};\n");
|
||||
}
|
||||
src.push_str("var local = {saveAs: function () {}};\n");
|
||||
for _ in 0..2048 {
|
||||
src.push_str("d.saveAs(f); local.saveAs(f);\n");
|
||||
}
|
||||
let limits = Limits::default();
|
||||
let mut acc = report::Acc::new(&limits);
|
||||
let declared = analyze::declarations(&src, &mut acc);
|
||||
let before = declared.loop_span_reads();
|
||||
analyze::scan(&src, &declared, &[], None, std::rc::Rc::from([]), &analyze::Known::default(), &mut acc);
|
||||
let reads = declared.loop_span_reads() - before;
|
||||
if reinitialize {
|
||||
assert!(reads < 16 * 2048, "{reads} span accesses for 2048 calls on d");
|
||||
} else {
|
||||
assert_eq!(reads, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A function body read before the literal that assigns its variable without declaring it may run
|
||||
/// after the literal, so the literal's `saveAs` keeps reporting the write; a body that declares the
|
||||
/// name assigns its own variable.
|
||||
#[test]
|
||||
fn a_function_read_earlier_that_assigns_the_variable_keeps_save_as_writing() {
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"function choose() { d = app.activeDocument; }\n",
|
||||
"var d = {saveAs: function () {}};\n",
|
||||
"choose(); d.saveAs(f);\n",
|
||||
"function bump() { e++; }\n",
|
||||
"var e = {saveAs: function () {}};\n",
|
||||
"e.saveAs(f);\n",
|
||||
"function own() { var g; g = app.activeDocument; }\n",
|
||||
"var g = {saveAs: function () {}};\n",
|
||||
"g.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7][..]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_writer_receivers_can_change_before_each_call() {
|
||||
for method in ["saveAs", "exportFile"] {
|
||||
for call in [format!("d.{method}(f)"), format!("d['{method}'](f)")] {
|
||||
for (prefix, suffix) in [("", ""), ("function outer() {\n", "}\nouter();\n")] {
|
||||
let src = format!(
|
||||
"{prefix}var d = {{{method}: function () {{}}}};\n\
|
||||
function save(f) {{ {call}; }}\n\
|
||||
d = app.activeDocument; save(f);\n{suffix}"
|
||||
);
|
||||
let r = report(&src);
|
||||
let line = if prefix.is_empty() { 2 } else { 3 };
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[line][..]), "{src}");
|
||||
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[line][..])], "{src}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_reassignments_keep_save_as_writing_after_a_later_initializer() {
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"function outer() {\n",
|
||||
" function choose() { d = app.activeDocument; }\n",
|
||||
" var d = {saveAs: function () {}};\n",
|
||||
" choose(); d.saveAs(f);\n",
|
||||
" function bump() { e++; }\n",
|
||||
" var e = {saveAs: function () {}};\n",
|
||||
" bump(); e.saveAs(f);\n",
|
||||
" function own() { var g; g = app.activeDocument; }\n",
|
||||
" var g = {saveAs: function () {}};\n",
|
||||
" own(); g.saveAs(f);\n",
|
||||
"}\n",
|
||||
"outer();\n",
|
||||
));
|
||||
assert_eq!(r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect::<Vec<_>>(), [("filesWritten", &[5, 8][..])]);
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 8][..]));
|
||||
}
|
||||
|
||||
/// Assigning a property of `this`, the global object in the global code and in a function called
|
||||
/// without a receiver, may give the global variable of that name another value, before or after
|
||||
/// its literal.
|
||||
#[test]
|
||||
fn assigning_a_property_of_this_keeps_save_as_writing() {
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var d = {saveAs: function () {}};\n",
|
||||
"this.d = app.activeDocument;\n",
|
||||
"d.saveAs(f);\n",
|
||||
"function swap() { this['e'] = app.activeDocument; }\n",
|
||||
"var e = {saveAs: function () {}};\n",
|
||||
"swap(); e.saveAs(f);\n",
|
||||
"var g = {saveAs: function () {}};\n",
|
||||
"this.other = 1;\n",
|
||||
"g.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7][..]));
|
||||
}
|
||||
|
||||
/// A `saveAs` key's value is a method of the script's own only when the whole value is one function
|
||||
/// expression: a value that goes on after the function's body, and a literal inside another, leave
|
||||
/// `saveAs` writing the file. A literal given to a function body's own variable at the end of the body
|
||||
|
||||
Reference in new issue
Block a user