mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 05:58:06 +00:00
Script checker: code the check can't read keeps a Folder's length, hidden and readonly setters
The check leaves out `length`, `hidden` and `readonly` set on `new
Folder(...)`, a `Folder` place, or a variable holding one, but a script
can give that name a File object in ways the check does not read. Such a
setter now counts as a file write in a check whose files:
- use `with`, or name `eval`, `Function`, `evalFile` or `constructor`,
as a word or as a string literal in brackets (`this['eval'](s)`,
`$['evalFile'](f)`, `(function () {}).constructor(s)`)
- read, call or assign a property in brackets when the brackets hold
anything but one string or number literal (`this[k](s)`,
`this['ev' + 'al'](s)`, `this[('eval')](s)`, `this[0, 'eval'](s)`,
`$[k](f)`, `g[k] = ...`), including a preprocessor line inside the
brackets
- assign a property in brackets whose name is a string literal longer
than 256 characters, or properties of more than 1,000 names
- assign or declare `Folder`
A variable whose name a member assignment writes (`$.global.d = ...`,
`this.d = ...`, `this['d'] = ...`) counts as one that may hold a File
object. Array literals, `o['plain']` and `a[0]` leave the Folder alone.
docs/mcp.md and the progress line in docs/target-app-parity.md state the
rule.
This commit is contained in:
@@ -53,19 +53,23 @@ pub(crate) struct Known {
|
||||
pub bound: HashMap<String, String>,
|
||||
pub files: HashSet<String>,
|
||||
pub folders: HashSet<String>,
|
||||
/// 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,
|
||||
}
|
||||
|
||||
impl Known {
|
||||
/// The names known, a name that may hold a File object counting twice: a Folder that becomes a
|
||||
/// possible File object ([`Known::add`]) changes it too.
|
||||
pub fn len(&self) -> usize {
|
||||
self.bound.len() + self.files.len().saturating_mul(2).saturating_sub(self.folders.len())
|
||||
self.bound.len() + self.files.len().saturating_mul(2).saturating_sub(self.folders.len()) + usize::from(self.unsure)
|
||||
}
|
||||
|
||||
/// What `other` adds, up to [`crate::Limits::entries`] names of each kind: a name already
|
||||
/// bound keeps its chain. A name that holds a File or Folder object on both sides holds a Folder
|
||||
/// 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;
|
||||
for (name, chain) in other.bound {
|
||||
if self.bound.len() < entries || self.bound.contains_key(&name) {
|
||||
self.bound.entry(name).or_insert(chain);
|
||||
@@ -566,6 +570,9 @@ struct Analyzer<'s, 'a, 'l> {
|
||||
maybe_files: HashSet<String>,
|
||||
/// `maybe_files` ran out of room: no variable counts as holding a Folder object.
|
||||
maybe_files_full: bool,
|
||||
/// This file or what the scan started with gives names values the analysis doesn't read
|
||||
/// ([`Declared::unsure`]): no object counts as a Folder object whatever the path names.
|
||||
unsure: bool,
|
||||
/// The platform names ([`platform_name`]) the script declares or assigns a meaning of its own
|
||||
/// for: they no longer stand for the platform's objects.
|
||||
own_names: HashSet<String>,
|
||||
@@ -635,8 +642,11 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
bound: known.bound.clone(),
|
||||
files: known.files.clone(),
|
||||
folders: known.folders.clone(),
|
||||
maybe_files: known.files.difference(&known.folders).cloned().collect(),
|
||||
// A variable of the same name as a property a member assignment writes may be that
|
||||
// property of the global object.
|
||||
maybe_files: known.files.difference(&known.folders).chain(declared.rebound.iter()).cloned().collect(),
|
||||
maybe_files_full: false,
|
||||
unsure: known.unsure || declared.unsure,
|
||||
own_names: HashSet::new(),
|
||||
scopes: vec![],
|
||||
declared,
|
||||
@@ -865,10 +875,10 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `name` holds a Folder object whatever the path names, and no assignment the check read gave it
|
||||
/// a value that may be a File object.
|
||||
/// `name` holds a Folder object whatever the path names, no assignment the check read gave it a
|
||||
/// value that may be a File object, and the file gives no name a value the analysis doesn't read.
|
||||
fn folder(&self, name: &str) -> bool {
|
||||
!self.maybe_files_full && self.files.contains(name) && self.folders.contains(name) && !self.maybe_files.contains(name)
|
||||
!self.maybe_files_full && !self.unsure && self.files.contains(name) && self.folders.contains(name) && !self.maybe_files.contains(name)
|
||||
}
|
||||
|
||||
/// `t`, when it names a parameter of the function whose body's `{` is still to come: kept, up to
|
||||
@@ -1568,7 +1578,7 @@ 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 };
|
||||
let known = Known { bound: std::mem::take(&mut self.bound), files: std::mem::take(&mut self.files), folders, unsure: self.unsure };
|
||||
(std::mem::take(&mut self.includes), module, known)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,21 @@
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use super::{Acc, Brackets, Lexer, LineBreaks, Read, Sig, T, for_in, goes_on_after, goes_on_with, is, member_mark, word};
|
||||
use super::{Acc, Brackets, Lexer, LineBreaks, Read, Sig, T, catalog, 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`,
|
||||
/// `Function` and `$.evalFile` run code given as text. A string literal in brackets that names one
|
||||
/// reaches it too (`this['eval']`), and so does a function's `constructor` property, which is
|
||||
/// `Function` (section 15.3.4.1).
|
||||
const UNREAD_BINDINGS: &[&str] = &["with", "eval", "Function", catalog::DOLLAR_EVAL_FILE];
|
||||
|
||||
/// The property a function reaches `Function` through.
|
||||
const CONSTRUCTOR: &str = "constructor";
|
||||
|
||||
/// The longest property name, in characters, that [`Declared::rebound`] keeps. A string literal in
|
||||
/// brackets that spells a longer one sets [`Declared::unsure`].
|
||||
const PROPERTY_CHARS: usize = 256;
|
||||
|
||||
/// The names a file's function bodies and its global code declare, read before the analysis. A
|
||||
/// function's `var` statements and the function declarations directly in its body make the names
|
||||
@@ -19,6 +33,17 @@ pub(crate) struct Declared<'s> {
|
||||
pub(super) bodies: Vec<(usize, Vec<&'s str>)>,
|
||||
/// The names the function declarations directly in the global code declare.
|
||||
pub(super) global: HashSet<&'s str>,
|
||||
/// 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 file gives names values the analysis doesn't read in other ways ([`UNREAD_BINDINGS`] or
|
||||
/// a `constructor` property used, a property read, called or assigned whose name in brackets is
|
||||
/// not one string or number literal, which may be a variable's name on the global object
|
||||
/// (ECMA-262 3rd edition, section 10.1.3) or one of [`UNREAD_BINDINGS`] whatever object the
|
||||
/// brackets follow, a property assigned whose name is a string literal longer than
|
||||
/// [`PROPERTY_CHARS`], or `Folder` assigned or declared), or `rebound` ran out of room: no
|
||||
/// object counts as a Folder object whatever the path names.
|
||||
pub(super) unsure: bool,
|
||||
}
|
||||
|
||||
/// A function body [`Declarations`] is reading: the bracket depth inside it, its entry in
|
||||
@@ -31,13 +56,23 @@ struct Body<'s> {
|
||||
var_depth: Option<usize>,
|
||||
}
|
||||
|
||||
/// A `[` [`Declarations`] is reading: the bracket depth inside it, whether a property's name in
|
||||
/// brackets starts at it (it comes after a value, where no array literal starts), and the tokens read
|
||||
/// inside it so far: how many, and the kind of the first.
|
||||
struct Key {
|
||||
depth: usize,
|
||||
member: bool,
|
||||
tokens: usize,
|
||||
first: Option<T>,
|
||||
}
|
||||
|
||||
/// The pass that finds [`Declared`]. It follows brackets, function bodies and `var` statements by
|
||||
/// the rules [`Analyzer`] follows them by, so that both passes find the same function bodies.
|
||||
struct Declarations<'s, 'a, 'l> {
|
||||
acc: &'a mut Acc<'l>,
|
||||
brackets: Brackets,
|
||||
/// The two significant tokens before this one, the latest first.
|
||||
h: [Option<Sig<'s>>; 2],
|
||||
/// The three significant tokens before this one, the latest first.
|
||||
h: [Option<Sig<'s>>; 3],
|
||||
/// `function` was read at this bracket depth, and its body's `{` is still to come.
|
||||
function: Option<usize>,
|
||||
/// The bracket depth of the `var` statement being read.
|
||||
@@ -50,6 +85,8 @@ struct Declarations<'s, 'a, 'l> {
|
||||
/// The bracket depth inside the outermost function body past that limit being read. The names
|
||||
/// declared inside it are not kept.
|
||||
untracked: Option<usize>,
|
||||
/// The `[` brackets being read, the innermost last, up to [`crate::Limits::nesting`].
|
||||
keys: Vec<Key>,
|
||||
out: Declared<'s>,
|
||||
}
|
||||
|
||||
@@ -59,12 +96,13 @@ pub(crate) fn declarations<'s>(src: &'s str, acc: &mut Acc) -> Declared<'s> {
|
||||
let mut d = Declarations {
|
||||
acc,
|
||||
brackets: Brackets::default(),
|
||||
h: [None; 2],
|
||||
h: [None; 3],
|
||||
function: None,
|
||||
var_depth: None,
|
||||
assign: None,
|
||||
bodies: vec![],
|
||||
untracked: None,
|
||||
keys: vec![],
|
||||
out: Declared::default(),
|
||||
};
|
||||
let mut breaks = LineBreaks::default();
|
||||
@@ -75,7 +113,8 @@ pub(crate) fn declarations<'s>(src: &'s str, acc: &mut Acc) -> Declared<'s> {
|
||||
Read::Token(sig) => d.token(sig),
|
||||
}
|
||||
}
|
||||
let out = d.out;
|
||||
let mut out = d.out;
|
||||
out.unsure |= out.rebound.contains(catalog::FOLDER);
|
||||
acc.steps = acc.steps.saturating_add(lexer.steps);
|
||||
out
|
||||
}
|
||||
@@ -86,7 +125,14 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
}
|
||||
|
||||
fn token(&mut self, t: Sig<'s>) {
|
||||
let [h0, h1] = self.h;
|
||||
let [h0, h1, _] = self.h;
|
||||
self.unread_binding(&t);
|
||||
if !t.is("]")
|
||||
&& let Some(key) = self.keys.last_mut()
|
||||
{
|
||||
key.tokens = key.tokens.saturating_add(1);
|
||||
key.first = key.first.or(Some(t.kind));
|
||||
}
|
||||
// As in [`Analyzer::token`], a line break ends a `var` statement when it is outside the
|
||||
// statement's brackets, and the tokens on either side of it don't go on with the expression.
|
||||
if t.nl && self.var_depth == Some(self.depth()) && !goes_on_after(h0) && !goes_on_with(&t) {
|
||||
@@ -118,7 +164,51 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
if t.word("function") && !member_mark(h0) && !word(h0, "function") {
|
||||
self.function = Some(depth);
|
||||
}
|
||||
self.h = [Some(t), h0];
|
||||
self.h = [Some(t), h0, h1];
|
||||
}
|
||||
|
||||
/// `t`, a token before the brackets are followed past it: notes in [`Declared::rebound`] and
|
||||
/// [`Declared::unsure`] the ways the file gives names values the analysis doesn't read.
|
||||
fn unread_binding(&mut self, t: &Sig<'s>) {
|
||||
let [h0, h1, h2] = self.h;
|
||||
if t.kind == T::Ident && (UNREAD_BINDINGS.contains(&t.text) || t.text == CONSTRUCTOR) {
|
||||
self.out.unsure = true;
|
||||
}
|
||||
// `this['eval']`, `$['evalFile']` and `o['constructor']`.
|
||||
if t.kind == T::Str && is(h0, "[") {
|
||||
let name = unquote(t.text, PROPERTY_CHARS.saturating_add(1));
|
||||
if UNREAD_BINDINGS.contains(&name.as_str()) || name == CONSTRUCTOR {
|
||||
self.out.unsure = true;
|
||||
}
|
||||
}
|
||||
// `var Folder`, `function Folder` and `Folder = …`: `Folder` may stop being the platform's.
|
||||
if (t.word(catalog::FOLDER) && (word(h0, "var") || word(h0, "function") || (is(h0, ",") && self.var_depth.is_some())))
|
||||
|| (t.is("=") && word(h0, catalog::FOLDER))
|
||||
{
|
||||
self.out.unsure = true;
|
||||
}
|
||||
if t.is("=") {
|
||||
let literal = |kind: T| h1.is_some_and(|s| s.kind == kind) && is(h2, "[");
|
||||
let property = if h0.is_some_and(|s| s.kind == T::Ident) && member_mark(h1) {
|
||||
h0.map(|s| s.text.to_string())
|
||||
} else if is(h0, "]") && literal(T::Str) {
|
||||
// The value spells a longer name than is kept: it may be any variable's.
|
||||
h1.map(|s| unquote(s.text, PROPERTY_CHARS.saturating_add(1))).filter(|name| name.chars().count() <= PROPERTY_CHARS)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(name) = property {
|
||||
if self.out.rebound.contains(&name) || self.acc.room(self.out.rebound.len()) {
|
||||
self.out.rebound.insert(name);
|
||||
} else {
|
||||
self.out.unsure = true;
|
||||
}
|
||||
} else if is(h0, "]") && !literal(T::Number) {
|
||||
// `o[k] = …` with a computed name, or a string literal too long to keep. A number
|
||||
// names no variable.
|
||||
self.out.unsure = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An open or close bracket, by the rules of [`Analyzer::bracket`]: the first `{` after
|
||||
@@ -128,6 +218,16 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
"(" | "[" | "{" => {
|
||||
let body = t.text == "{" && self.function.take() == Some(self.depth());
|
||||
self.brackets.open(t, self.acc.limits.nesting);
|
||||
if t.text == "[" {
|
||||
// After a value, `[` starts a property's name in brackets (ECMA-262 3rd
|
||||
// edition, section 11.2.1); elsewhere it starts an array literal.
|
||||
let member = !t.expr;
|
||||
if self.keys.len() < self.acc.limits.nesting {
|
||||
self.keys.push(Key { depth: self.depth(), member, tokens: 0, first: None });
|
||||
} else if member {
|
||||
self.out.unsure = true;
|
||||
}
|
||||
}
|
||||
if !body {
|
||||
return;
|
||||
}
|
||||
@@ -140,10 +240,22 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
}
|
||||
}
|
||||
")" | "]" | "}" => {
|
||||
let inside = self.depth();
|
||||
if !self.brackets.close(t) {
|
||||
return;
|
||||
}
|
||||
let depth = self.depth();
|
||||
// `o[k]`, `this['ev' + 'al']` and `o[('eval')]`, read, called or assigned: a name
|
||||
// in brackets that isn't one string or number literal may be any variable's on the
|
||||
// global object, or `eval`, `Function`, `evalFile` or `constructor`.
|
||||
if t.text == "]"
|
||||
&& let Some(key) = self.keys.pop_if(|k| k.depth == inside)
|
||||
&& key.member
|
||||
&& !(key.tokens == 1 && matches!(key.first, Some(T::Str | T::Number)))
|
||||
{
|
||||
self.out.unsure = true;
|
||||
}
|
||||
while self.keys.pop_if(|k| depth < k.depth).is_some() {}
|
||||
if self.var_depth.is_some_and(|d| depth < d) {
|
||||
self.var_depth = None;
|
||||
}
|
||||
@@ -163,7 +275,7 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
/// directly in it, and for the global code when the declaration is directly in the global code.
|
||||
/// Each body keeps up to [`crate::Limits::entries`] names, and so does the global code.
|
||||
fn declaration(&mut self, t: &Sig<'s>) {
|
||||
let [h0, _] = self.h;
|
||||
let [h0, ..] = self.h;
|
||||
let depth = self.depth();
|
||||
let var = word(h0, "var") || (is(h0, ",") && self.var_depth == Some(depth));
|
||||
let function = word(h0, "function") && h0.is_some_and(|f| f.stmt);
|
||||
@@ -182,9 +294,14 @@ impl<'s> Declarations<'s, '_, '_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A preprocessor line ends the statement before it, as in [`Analyzer::directive`].
|
||||
/// 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) {
|
||||
self.h = [None; 2];
|
||||
if self.keys.iter().any(|k| k.member) {
|
||||
self.out.unsure = true;
|
||||
}
|
||||
self.h = [None; 3];
|
||||
self.var_depth = None;
|
||||
self.assign = None;
|
||||
}
|
||||
|
||||
@@ -334,12 +334,13 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
}
|
||||
|
||||
/// What [`Self::file_receiver`] finds: `Some(true)` for a Folder object whatever the path names
|
||||
/// ([`Sig::folder`], [`Analyzer::folder`]), `Some(false)` for an object that may be a File object,
|
||||
/// and `None` for anything else.
|
||||
/// ([`Sig::folder`], [`Analyzer::folder`]), unless the file gives names values the analysis
|
||||
/// doesn't read ([`Analyzer::unsure`]), `Some(false)` for an object that may be a File object, and
|
||||
/// `None` for anything else.
|
||||
fn held_file(&self, receiver: Option<Sig<'s>>, before: Option<Sig<'s>>) -> Option<bool> {
|
||||
let r = receiver?;
|
||||
if r.file {
|
||||
Some(r.folder)
|
||||
Some(r.folder && !self.unsure)
|
||||
} else if r.kind == T::Ident && !member_mark(before) && self.alias(&r) {
|
||||
Some(self.folder(r.text))
|
||||
} else {
|
||||
|
||||
@@ -653,6 +653,154 @@ fn a_variable_that_may_hold_a_file_counts_as_one() {
|
||||
assert!(r.outside.is_empty(), "{:?}", r.outside);
|
||||
}
|
||||
|
||||
/// A `with` statement, `eval`, `Function`, `$.evalFile`, a member assignment that may write the
|
||||
/// global object's property of a variable's name, and a script's own `Folder` may give a name a
|
||||
/// File object the check doesn't see, before or after the setter in the text, so the setter counts.
|
||||
#[test]
|
||||
fn a_folder_that_other_code_may_rebind_counts_as_a_file() {
|
||||
let scripts: [(&str, usize); 11] = [
|
||||
("var d = new Folder(p);\nwith (o) { d.length = 0; }\n", 2),
|
||||
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\n$.global.d = new File(q);\n", 2),
|
||||
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\nthis.d = new File(q);\n", 2),
|
||||
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\nthis['d'] = new File(q);\n", 2),
|
||||
("var d = new Folder(p);\nfunction w() { d.hidden = true; }\n$.global[k] = new File(q);\n", 2),
|
||||
("eval(s);\nnew Folder(p).length = 0;\n", 2),
|
||||
("var d = new Folder(p);\nd.readonly = true;\n$.evalFile(f);\n", 2),
|
||||
("var d = new Folder(p);\nd.readonly = true;\nvar g = new Function(s);\n", 2),
|
||||
("$.global.Folder = File;\nnew Folder(p).length = 0;\n", 2),
|
||||
("function w() { new Folder(p).hidden = true; }\nFolder = File;\n", 1),
|
||||
("function w() { Folder.temp.hidden = true; }\nvar Folder = File;\n", 1),
|
||||
];
|
||||
let written = |r: &Report| r.outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone());
|
||||
for (script, line) in scripts {
|
||||
assert_eq!(written(&report(script)), Some(vec![line]), "{script}");
|
||||
}
|
||||
// Another variable's name, or a number as the index, leaves the Folder alone.
|
||||
let r = report("var d = new Folder(p);\no.e = new File(q);\na[0] = new File(q);\nd.length = 0;\n");
|
||||
assert_eq!(written(&r), None);
|
||||
// A computed index may name the variable on any object that may be the global object.
|
||||
let r = report("var d = new Folder(p);\na[k] = new File(q);\nd.length = 0;\n");
|
||||
assert_eq!(written(&r), Some(vec![3]));
|
||||
// A `with` statement in an included file counts in the script too.
|
||||
let mut m = mem(&[("lib.jsxinc", "with (o) { x = 1; }\n")]);
|
||||
let r = check("main.jsx", b"#include 'lib.jsxinc'\nvar d = new Folder(p);\nd.length = 0;\n", Some(&mut m), &Limits::default());
|
||||
let outside: Vec<(&str, &[usize])> = r.outside.iter().map(|o| (o.id, o.lines.as_slice())).collect();
|
||||
assert_eq!(outside, [("filesWritten", &[3][..])]);
|
||||
}
|
||||
|
||||
/// An assignment to a computed property of any object, a string literal in brackets that names
|
||||
/// `eval`, `Function` or `$.evalFile`, and any `constructor` property may give a name a File object
|
||||
/// the check doesn't see, so no object counts as a Folder object.
|
||||
#[test]
|
||||
fn a_folder_that_unnamed_code_may_rebind_counts_as_a_file() {
|
||||
let scripts: [(&str, usize); 13] = [
|
||||
("var d = new Folder(p);\nvar g = this;\ng[k] = new File(q);\nd.length = 0;\n", 4),
|
||||
("var d = new Folder(p);\nvar g = $.global;\ng[k] = File;\nd.length = 0;\n", 4),
|
||||
("var d = new Folder(p);\n$['global'][k] = new File(q);\nd.length = 0;\n", 3),
|
||||
("var d = new Folder(p);\n$[\"global\"][k] = new File(q);\nd.length = 0;\n", 3),
|
||||
("var d = new Folder(p);\no[f(k)] = new File(q);\nd.length = 0;\n", 3),
|
||||
("var d = new Folder(p);\nd.length = 0;\n$['evalFile'](f);\n", 2),
|
||||
("var d = new Folder(p);\nd.length = 0;\nthis['eval'](s);\n", 2),
|
||||
("var d = new Folder(p);\nd.length = 0;\nthis['\\u0065val'](s);\n", 2),
|
||||
("var d = new Folder(p);\nd.length = 0;\nvar g = $.global['Function'];\n", 2),
|
||||
("var d = new Folder(p);\nd.length = 0;\n(function () {}).constructor(s)();\n", 2),
|
||||
("var d = new Folder(p);\nd.length = 0;\nvar c = o['constructor'];\n", 2),
|
||||
("new Folder(p).hidden = true;\nvar c = o.constructor;\n", 1),
|
||||
("var d = new Folder(p);\nd.length = 0;\nvar c = constructor;\n", 2),
|
||||
];
|
||||
let written = |r: &Report| r.outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone());
|
||||
for (script, line) in scripts {
|
||||
assert_eq!(written(&report(script)), Some(vec![line]), "{script}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A string literal in brackets names the property its value spells, escapes undone, as a name
|
||||
/// written with escapes is read as the name it spells. A literal longer than the check reads may
|
||||
/// name any variable, so no object counts as a Folder object.
|
||||
#[test]
|
||||
fn a_bracketed_name_is_read_whole_or_counts_as_any_name() {
|
||||
let long = "a".repeat(300);
|
||||
let scripts = [
|
||||
format!("var {long} = new Folder(p);\nfunction w() {{ {long}.length = 0; }}\nthis['{long}'] = new File(q);\n"),
|
||||
format!("var d = new Folder(p);\nfunction w() {{ d.length = 0; }}\nthis['{long}'] = new File(q);\n"),
|
||||
"var d = new Folder(p);\nfunction w() { d.hidden = true; }\nthis['\\u0064'] = new File(q);\n".to_string(),
|
||||
"var \\u0064 = new Folder(p);\nfunction w() { d.hidden = true; }\nthis['d'] = new File(q);\n".to_string(),
|
||||
"var d = new Folder(p);\nfunction w() { \\u0064.hidden = true; }\nthis['\\x64'] = new File(q);\n".to_string(),
|
||||
];
|
||||
let written = |r: &Report| r.outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone());
|
||||
for script in &scripts {
|
||||
assert_eq!(written(&report(script)), Some(vec![2]), "{script}");
|
||||
}
|
||||
// A short literal that names another variable leaves the Folder alone.
|
||||
let r = report(&format!("var d = new Folder(p);\nfunction w() {{ d.length = 0; }}\nthis['{}'] = new File(q);\n", "a".repeat(200)));
|
||||
assert_eq!(written(&r), None);
|
||||
}
|
||||
|
||||
/// The lines of `filesWritten` in the report on `script`.
|
||||
fn files_written(script: &str) -> Option<Vec<usize>> {
|
||||
report(script).outside.iter().find(|o| o.id == "filesWritten").map(|o| o.lines.clone())
|
||||
}
|
||||
|
||||
/// A name in brackets held in a variable may be `eval`, so no object counts as a Folder object.
|
||||
#[test]
|
||||
fn a_bracketed_variable_read_counts_as_any_name() {
|
||||
let script = "var d = new Folder(p);\nd.length = 0;\nvar k = 'eval';\nthis[k]('d = new File(q)');\n";
|
||||
assert_eq!(files_written(script), Some(vec![2]));
|
||||
}
|
||||
|
||||
/// A name in brackets joined from strings may be `eval`, so no object counts as a Folder object.
|
||||
#[test]
|
||||
fn a_bracketed_joined_name_counts_as_any_name() {
|
||||
let script = "var d = new Folder(p);\nd.length = 0;\nthis['ev' + 'al']('d = new File(q)');\n";
|
||||
assert_eq!(files_written(script), Some(vec![2]));
|
||||
}
|
||||
|
||||
/// A string literal in grouping parentheses inside brackets is read as a computed name.
|
||||
#[test]
|
||||
fn a_bracketed_name_in_parentheses_counts_as_any_name() {
|
||||
let script = "var d = new Folder(p);\nd.length = 0;\nthis[('eval')]('d = new File(q)');\n";
|
||||
assert_eq!(files_written(script), Some(vec![2]));
|
||||
}
|
||||
|
||||
/// A comma expression in brackets names its last operand's value, which may be `eval`.
|
||||
#[test]
|
||||
fn a_bracketed_comma_expression_counts_as_any_name() {
|
||||
let script = "var d = new Folder(p);\nd.length = 0;\nthis[0, 'eval']('d = new File(q)');\n";
|
||||
assert_eq!(files_written(script), Some(vec![2]));
|
||||
}
|
||||
|
||||
/// A name in brackets held in a variable may be `evalFile`, called on `$`.
|
||||
#[test]
|
||||
fn a_bracketed_variable_call_on_dollar_counts_as_any_name() {
|
||||
let script = "var d = new Folder(p);\nd.length = 0;\nvar k = 'evalFile';\n$[k](f);\n";
|
||||
assert_eq!(files_written(script), Some(vec![2]));
|
||||
}
|
||||
|
||||
/// A name in brackets held in a variable may be `constructor`, which reaches `Function`.
|
||||
#[test]
|
||||
fn a_bracketed_variable_constructor_counts_as_any_name() {
|
||||
let script = "var d = new Folder(p);\nd.length = 0;\nvar k = 'constructor';\n(function () {})[k](s)();\n";
|
||||
assert_eq!(files_written(script), Some(vec![2]));
|
||||
}
|
||||
|
||||
/// One string or number literal in brackets names one property, and an array literal names none,
|
||||
/// also with brackets inside brackets, so the Folder is left alone. A computed name inside another
|
||||
/// name in brackets still counts as any name.
|
||||
#[test]
|
||||
fn a_literal_name_in_brackets_leaves_the_folder_alone() {
|
||||
let script = "var d = new Folder(p);\nvar x = o['plain'];\nvar y = a[0][1];\na[0] = 1;\nvar z = [k, o['x'], a[0]];\nd.length = 0;\n";
|
||||
assert_eq!(files_written(script), None);
|
||||
assert_eq!(files_written("var d = new Folder(p);\nd.length = 0;\nvar x = o[a[0]];\n"), Some(vec![2]));
|
||||
}
|
||||
|
||||
/// A preprocessor line inside a name in brackets hides the `[` before the name, so the name may be
|
||||
/// `eval` and no object counts as a Folder object.
|
||||
#[test]
|
||||
fn a_directive_in_a_bracketed_name_counts_as_any_name() {
|
||||
let script = "var d = new Folder(p);\nd.length = 0;\nthis[\n#target illustrator\n'eval']('d = new File(q)');\n";
|
||||
assert_eq!(files_written(script), Some(vec![2]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_function_after_a_line_that_ends_a_statement_is_a_declaration() {
|
||||
let r = report("var n = 1\nfunction File() {}\nFile('x');\nvar m = 2 /* a\n */ function Socket() {}\nnew Socket();\n");
|
||||
|
||||
Reference in New Issue
Block a user