diff --git a/crates/engine/src/tests/script_xmp.rs b/crates/engine/src/tests/script_xmp.rs index b839da8893..6c68cadcff 100644 --- a/crates/engine/src/tests/script_xmp.rs +++ b/crates/engine/src/tests/script_xmp.rs @@ -163,7 +163,8 @@ fn a_scanned_sidecar_is_read_only() { /// The state an `XMPFile` keeps cannot be changed by the script: a sidecar opened for reading is /// not written by `closeFile` after the script assigns, defines or deletes the properties that -/// hold the packet, the path and the open flags. +/// hold the packet, the path and the open flags, or calls `closeFile` on another object that holds +/// properties of the same names, plain or made from `XMPFile.prototype`. #[test] fn a_script_cannot_change_the_state_of_an_xmp_file() { let base = folder("state"); @@ -177,10 +178,14 @@ fn a_script_cannot_change_the_state_of_an_xmp_file() { x.__vcXmpPending = 'not a packet'; x.__vcXmpFlags = XMPConst.OPEN_FOR_UPDATE;\n\ try {{ Object.defineProperty(x, '__vcXmpPending', {{ value: 'not a packet' }}); out.push('defined'); }} catch (e) {{ out.push(e.name); }}\n\ out.push(typeof x.__vcXmpPending, x.getFileInfo().openFlags == XMPConst.OPEN_FOR_READ);\n\ + [{{}}, Object.create(XMPFile.prototype)].forEach(function (fake) {{\n\ + fake.__vcXmpPath = p; fake.__vcXmpRun = x.__vcXmpRun; fake.__vcXmpOpen = true; fake.__vcXmpPending = 'not a packet';\n\ + try {{ XMPFile.prototype.closeFile.call(fake); out.push('closed'); }} catch (e) {{ out.push(e.name); }}\n\ + }});\n\ x.closeFile(); out.push(delete x.__vcXmpPath); vc.log(out); vc.log(p);" ), ); - assert_eq!(log[0], json!(["TypeError", "undefined", true, false])); + assert_eq!(log[0], json!(["TypeError", "undefined", true, "TypeError", "TypeError", false])); let text = std::fs::read_to_string(log[1].as_str().unwrap()).unwrap(); assert_eq!(text, PACKET); let _ = std::fs::remove_dir_all(base); diff --git a/crates/js/src/heap.rs b/crates/js/src/heap.rs index fc665342dc..4e3cf3dd87 100644 --- a/crates/js/src/heap.rs +++ b/crates/js/src/heap.rs @@ -248,6 +248,8 @@ pub(crate) enum Class { XmlFilter(Box), /// An `XMPMeta` object and its metadata ([`crate::xmp_classes`]). Xmp(Box), + /// An object the `XMPFile` constructor made ([`crate::xmp_classes`]). + XmpFile, } /// What an XML value is. @@ -311,7 +313,7 @@ impl Class { Class::XmlList(_) => "XMLList", Class::Namespace(_) => "Namespace", Class::QName { .. } => "QName", - Class::XmlFilter(_) | Class::Xmp(_) => "Object", + Class::XmlFilter(_) | Class::Xmp(_) | Class::XmpFile => "Object", Class::Math => "Math", Class::Json => "JSON", Class::Ordinary | Class::ForIn(_) => "Object", diff --git a/crates/js/src/xmp_classes.rs b/crates/js/src/xmp_classes.rs index 5733d28e9a..3357336a5f 100644 --- a/crates/js/src/xmp_classes.rs +++ b/crates/js/src/xmp_classes.rs @@ -770,7 +770,15 @@ fn set_localized_text(i: &mut Interp, this: Value, args: &[Value], _: bool) -> J meta_of(i, o)?.find(&ns, &name).unwrap_or(0) } }; - let grow = 2 * (64 + value.len() + specific.len()); + // An item and the `x-default` item change, or every item with the text of the `x-default` one. + let changed = match meta_of(i, o)?.props.get(at).map(|p| &p.value) { + Some(xmp::Value::Array { items, .. }) if specific.eq_ignore_ascii_case("x-default") => { + let old = items.iter().find(|i| same_lang(i.lang.as_deref(), "x-default")).map(|i| &i.text); + items.iter().filter(|i| Some(&i.text) == old).count().max(2) + } + _ => 2, + }; + let grow = changed.saturating_mul(64 + value.len() + specific.len()); change(i, o, grow, |m, _| { let Some(xmp::Value::Array { items, .. }) = m.props.get_mut(at).map(|p| &mut p.value) else { return }; set_lang(items, &generic, &specific, value); @@ -781,14 +789,14 @@ fn set_localized_text(i: &mut Interp, this: Value, args: &[Value], _: bool) -> J /// Sets the item of `items` in `specific` to `value`, or, when there is none, the one item in the /// language `generic` or a variant of it. VectorCraft's own rule: the `x-default` item is first; it /// is added with the value when there is none, and follows a change to an item that had the same -/// text. +/// text; a change to the `x-default` item also changes the items that had its text. fn set_lang(items: &mut Vec, generic: &str, specific: &str, value: String) { let default = items.iter().position(|i| same_lang(i.lang.as_deref(), "x-default")); if specific.eq_ignore_ascii_case("x-default") { - match default { - Some(d) => { - if let Some(item) = items.get_mut(d) { - item.text = value; + match default.and_then(|d| items.get(d)).map(|i| i.text.clone()) { + Some(old) => { + for item in items.iter_mut().filter(|i| i.text == old) { + item.text.clone_from(&value); } } None => items.insert(0, Item { text: value, lang: Some("x-default".into()) }), @@ -917,9 +925,10 @@ not_yet_fns! { // ---- XMPFile ---- -/// The `XMPFile` `this`, refused when it is not one. +/// The `XMPFile` `this`, refused when the `XMPFile` constructor did not make it. fn this_file(i: &mut Interp, this: &Value, what: &str) -> JsResult { if let Some(o) = this.as_object() + && matches!(i.heap.object(o).map(|ob| &ob.class), Some(Class::XmpFile)) && matches!(slot(i, o, PATH)?, Value::String(_)) { return Ok(o); @@ -970,7 +979,7 @@ fn file_ctor(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult