mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 19:36:16 +00:00
Merge pull request #9371 from dotCooCoo/fix/scripts-object-model-edges
Scripts: stale page item lists throw from toString; unavailable members stop hasOwnProperty
This commit is contained in:
@@ -1303,6 +1303,7 @@ fn selection_ids(h: &Host, doc: Doc, v: &Data) -> std::result::Result<Vec<NodeId
|
||||
d => vec![d],
|
||||
};
|
||||
let mut ids: Vec<NodeId> = Vec::with_capacity(list.len());
|
||||
let mut seen: HashSet<NodeId> = HashSet::with_capacity(list.len());
|
||||
// The document is read once for every object: nothing changes it while the list is checked.
|
||||
let (d, isolation, guides_locked) = with_doc(h, doc, |s, st| (st.doc.clone(), st.isolation, s.guides_locked()));
|
||||
let locks = d.locked_artboards();
|
||||
@@ -1317,7 +1318,7 @@ fn selection_ids(h: &Host, doc: Doc, v: &Data) -> std::result::Result<Vec<NodeId
|
||||
let name = with_doc(h, item.doc, |_, st| st.doc.node(item.id).and_then(typename).unwrap_or("page item"));
|
||||
return Err(HostError::Error(format!("Cannot select {name} {}", item.id.0)));
|
||||
}
|
||||
if !ids.contains(&item.id) {
|
||||
if seen.insert(item.id) {
|
||||
ids.push(item.id);
|
||||
}
|
||||
}
|
||||
@@ -1378,6 +1379,11 @@ fn items_typename(h: &Host, a: &[Data]) -> Answer {
|
||||
Ok(text("PageItems"))
|
||||
}
|
||||
|
||||
fn items_to_string(h: &Host, a: &[Data]) -> Answer {
|
||||
items_doc(h, a)?;
|
||||
Ok(text("[PageItems]"))
|
||||
}
|
||||
|
||||
fn items_parent(h: &Host, a: &[Data]) -> Answer {
|
||||
let doc = items_doc(h, a)?;
|
||||
Ok(doc_ref(h, doc.uid))
|
||||
@@ -2148,7 +2154,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
|
||||
("index", method(l, items_index)),
|
||||
("getByName", method(l, items_by_name)),
|
||||
("removeAll", method(l, items_remove_all)),
|
||||
("toString", class_text("PageItems")),
|
||||
("toString", method(l, items_to_string)),
|
||||
]),
|
||||
Some(function(l, items_at)),
|
||||
));
|
||||
|
||||
@@ -680,9 +680,13 @@ fn a_listed_preset_keeps_more_rows_than_artboards() {
|
||||
fn stale_documents_throw_on_every_member() {
|
||||
let (mut s, _, _) = doc();
|
||||
run(&mut s, "#targetengine keep\nvar d = app.activeDocument, p = d.pageItems, l = p[0].parent;").unwrap();
|
||||
for (member, message) in
|
||||
[("d.typename", "Stale document"), ("d.parent", "Stale document"), ("p.typename", "Stale document"), ("l.typename", "Stale")]
|
||||
{
|
||||
for (member, message) in [
|
||||
("d.typename", "Stale document"),
|
||||
("d.parent", "Stale document"),
|
||||
("p.typename", "Stale document"),
|
||||
("p.toString()", "Stale document"),
|
||||
("l.typename", "Stale"),
|
||||
] {
|
||||
let e = failure(&mut s, &format!("#targetengine keep\n{member};"));
|
||||
assert!(e.contains(message), "{member}: {e}");
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ fn failed_script_report_keeps_message_and_adds_origin() {
|
||||
("throw new Error('invalid array length');", "thrown", "Error: invalid array length", "script"),
|
||||
("throw new RangeError('invalid array length');", "thrown", "RangeError: invalid array length", "script"),
|
||||
("throw 'invalid array length';", "thrown", "invalid array length", "script"),
|
||||
("throw 'ReferenceError: app is not defined';", "thrown", "ReferenceError: app is not defined", "script"),
|
||||
("throw 'ReferenceError: Window is not defined';", "thrown", "ReferenceError: Window is not defined", "script"),
|
||||
("try { new Array(-1); } catch (e) { throw new Error(e.message); }", "thrown", "Error: invalid array length", "script"),
|
||||
("new Array(-1);", "thrown", "RangeError: invalid array length", "built-in"),
|
||||
("try { new Array(-1); } catch (e) { throw e; }", "thrown", "RangeError: invalid array length", "built-in"),
|
||||
|
||||
@@ -91,6 +91,8 @@ pub(crate) struct Entry {
|
||||
parent: Option<u32>,
|
||||
/// The names of the members `hasOwnProperty` reports: every member but the unavailable ones.
|
||||
own: Rc<HashSet<Key>>,
|
||||
/// The names of the members this class declares as unavailable.
|
||||
unavailable: Rc<HashSet<Key>>,
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
@@ -143,7 +145,9 @@ pub fn host_ref_parts(d: &Data) -> Option<(&str, Vec<f64>)> {
|
||||
|
||||
impl Interp {
|
||||
/// Whether `k` is a member that `hasOwnProperty` reports on `id`: `id` is an object of a host
|
||||
/// class, and `k` names an available member of that class or of a class it descends from.
|
||||
/// class, and `k` names an available member of that class or of a class it descends from. The
|
||||
/// nearest class that declares `k` decides, so a member a class declares unavailable is not
|
||||
/// reported even when a class it descends from has it.
|
||||
pub(crate) fn is_host_member(&self, id: ObjId, k: &[u16]) -> JsResult<bool> {
|
||||
let Class::Host(h) = &self.object(id)?.class else { return Ok(false) };
|
||||
let mut class = Some(h.class);
|
||||
@@ -152,6 +156,9 @@ impl Interp {
|
||||
if e.own.contains(k) {
|
||||
return Ok(true);
|
||||
}
|
||||
if e.unavailable.contains(k) {
|
||||
return Ok(false);
|
||||
}
|
||||
class = e.parent;
|
||||
}
|
||||
Ok(false)
|
||||
@@ -307,7 +314,8 @@ fn install_class(i: &mut Interp, class: HostClass) -> JsResult<()> {
|
||||
let r = fill_class(i, proto, &class);
|
||||
i.temp_roots.truncate(mark);
|
||||
let ctor = r?;
|
||||
let members = class.members.iter().filter(|(_, m)| !matches!(m, HostMember::Unavailable(_))).map(|(name, _)| key(name)).collect();
|
||||
let (unavailable, members): (Vec<_>, Vec<_>) = class.members.iter().partition(|(_, m)| matches!(m, HostMember::Unavailable(_)));
|
||||
let names = |list: Vec<&(String, HostMember)>| list.into_iter().map(|(name, _)| key(name)).collect::<HashSet<Key>>();
|
||||
i.host_classes.push(Entry {
|
||||
name: class.name.as_str().into(),
|
||||
proto,
|
||||
@@ -315,7 +323,8 @@ fn install_class(i: &mut Interp, class: HostClass) -> JsResult<()> {
|
||||
index: class.index,
|
||||
family,
|
||||
parent: parent_class,
|
||||
own: Rc::new(members),
|
||||
own: Rc::new(names(members)),
|
||||
unavailable: Rc::new(names(unavailable)),
|
||||
});
|
||||
let global = i.global;
|
||||
i.define(global, key(&class.name), Prop::Data { value: Value::Object(ctor), flags: crate::heap::HIDDEN })
|
||||
|
||||
@@ -756,6 +756,53 @@ fn objects_of_a_host_class_report_its_available_members_as_their_own() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_member_a_class_declares_unavailable_is_not_its_own_when_its_parent_has_it() {
|
||||
let mut r = realm(1_000_000);
|
||||
let value: HostFunction = Rc::new(|_| Ok(Data::Number(1.0)));
|
||||
let missing: HostFunction = Rc::new(|_| Err(HostError::Error("not here yet".into())));
|
||||
let base = HostClass {
|
||||
name: "Base".into(),
|
||||
parent: None,
|
||||
members: vec![("size".to_string(), HostMember::Get(value.clone())), ("grow".to_string(), HostMember::Method(value))],
|
||||
index: None,
|
||||
construct: None,
|
||||
};
|
||||
let leaf = HostClass {
|
||||
name: "Leaf".into(),
|
||||
parent: Some("Base".into()),
|
||||
members: vec![("size".to_string(), HostMember::Unavailable(missing))],
|
||||
index: None,
|
||||
construct: None,
|
||||
};
|
||||
r.define_host_classes(vec![base, leaf], vec![("it".into(), host_ref("Leaf", &[1.0]))]).unwrap();
|
||||
r.start("[it.hasOwnProperty('size'), it.propertyIsEnumerable('size'), it.hasOwnProperty('grow')].join()").unwrap();
|
||||
assert_eq!(finish(&mut r, 1_000_000).0.unwrap(), JsValue::String("false,false,true".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assigning_a_name_a_prototype_has_does_not_call_the_indexer() {
|
||||
let mut r = realm(1_000_000);
|
||||
let calls: Rc<Cell<u32>> = Rc::default();
|
||||
let seen = calls.clone();
|
||||
let index: HostFunction = Rc::new(move |a| {
|
||||
seen.set(seen.get() + 1);
|
||||
match a.get(1) {
|
||||
Some(Data::Number(_)) => Ok(Data::Undefined),
|
||||
_ => Err(HostError::Error("no such member".into())),
|
||||
}
|
||||
});
|
||||
let class = HostClass { name: "List".into(), parent: None, members: Vec::new(), index: Some(index), construct: None };
|
||||
r.define_host_classes(vec![class], vec![("it".into(), host_ref("List", &[1.0]))]).unwrap();
|
||||
r.start("Object.prototype.extra = 1; it.extra = 2; it.extra").unwrap();
|
||||
assert_eq!(finish(&mut r, 1_000_000).0.unwrap(), JsValue::Number(1.0));
|
||||
assert_eq!(calls.get(), 0);
|
||||
// A name nothing has still goes to the indexer, which throws.
|
||||
r.start("it.nope = 2").unwrap();
|
||||
assert!(finish(&mut r, 1_000_000).0.is_err());
|
||||
assert_eq!(calls.get(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_converted_function_reads_each_argument_as_its_list_says() {
|
||||
let mut r = realm(1_000_000);
|
||||
|
||||
@@ -1904,16 +1904,18 @@ impl Interp {
|
||||
None => {}
|
||||
}
|
||||
let proto = self.object(id)?.proto;
|
||||
match proto.map(|p| self.find_property(p, &k)).transpose()?.flatten() {
|
||||
let inherited = proto.map(|p| self.find_property(p, &k)).transpose()?.flatten();
|
||||
match inherited {
|
||||
Some(Prop::Accessor { set: Some(s), .. }) => return self.call(&Value::Object(s), &Value::Object(id), &[v]).map(drop),
|
||||
Some(Prop::Accessor { set: None, .. }) => return self.reject(throw, &k, "it has only a getter"),
|
||||
Some(Prop::Data { flags, .. }) if flags & WRITABLE == 0 => return self.reject(throw, &k, "it is read-only"),
|
||||
_ => {}
|
||||
}
|
||||
if !self.object(id)?.extensible {
|
||||
// A name that an object of a host class with an indexer lacks is read with the indexer
|
||||
// first, so assigning it throws what reading it throws.
|
||||
if array_index(&k).is_none()
|
||||
// A name that an object of a host class with an indexer lacks, and its prototypes lack
|
||||
// too, is read with the indexer first, so assigning it throws what reading it throws.
|
||||
if inherited.is_none()
|
||||
&& array_index(&k).is_none()
|
||||
&& let Some(f) = self.host_indexer(id)?
|
||||
{
|
||||
self.host_index(&f, id, Self::indexer_key(&k), false)?;
|
||||
|
||||
Reference in New Issue
Block a user