//! `ExternalObject` and the XMP classes it loads, as the help pages
//! ,
//! and
//! describe them,
//! as built-ins ([`crate::Realm::define_external_object`]). The metadata itself is
//! [`crate::xmp`].
//!
//! `new ExternalObject("lib:AdobeXMPScript")` defines the globals `XMPMeta`, `XMPFile` and
//! `XMPConst` and gives a handle whose `terminate()` releases it; the globals and the objects made
//! with them stay. Any other library name throws an `Error`, and no native code is loaded.
//! `ExternalObject.search(name)` is true for that name alone.
//!
//! `XMPMeta` holds its metadata in its object. Its namespace prefixes are the realm's: those a
//! packet declares are registered when it is read, and `XMPMeta.registerNamespace` adds others.
//! `getNamespacePrefix` and `registerNamespace` give a prefix with its colon. Property paths are a
//! name, `prefix:name`, and either with `[n]` (from 1) or `[last()]` for an array item. An array
//! property holds simple items; any other value a packet holds is kept as it was written and
//! `getProperty` gives its text, when it has one, and its options.
//! `insertArrayItem` inserts before the last existing item when its index is `ARRAY_LAST_ITEM`.
//! `insertArrayItem` and `setArrayItem` store `null` as an empty item value.
//! Metadata edits charge fuel for size accounting.
//!
//! `XMPFile` reads a file through the host functions `xmpOpen` and `xmpWrite` of `File` and
//! `Folder` ([`crate::Realm::define_file_classes`]), so it reads and writes only where they may. A
//! file named `.xmp` is read whole as a packet, and any other file, or any file with
//! `OPEN_USE_PACKET_SCANNING`, is scanned for its first packet ([`crate::xmp::find_packet`]).
//! `putXMP` keeps the packet and `closeFile` writes it.
//!
//! VectorCraft's own rules: only a `.xmp` file opened with `OPEN_FOR_UPDATE` takes `putXMP`;
//! `closeFile` always replaces the file through a temporary one when the folder may hold one, and
//! with `CLOSE_UPDATE_SAFELY` it writes no other way, throwing and keeping the packet instead; an
//! `XMPFile` is open only in the file run that opened it. Closing it successfully or beginning
//! another file run with `Realm::reset_folder_current` releases its packet buffers. A member the
//! reference lists that is not built yet, and a flag, format or option not handled yet, throws an
//! error that says so.
use crate::builtins::arg;
use crate::heap::{Class, ENUMERABLE, HIDDEN, NativeKind, OBJECT_BYTES, ObjId, Object, PROP_BYTES, Prop, PropMap, key, string_bytes};
use crate::interp::{Abrupt, ErrorType, Interp, JsResult};
use crate::value::Value;
use crate::xmp::{self, Form, Item, Meta, Registry, Step, XmpError};
/// The one library `ExternalObject` loads.
const LIBRARY: &str = "lib:AdobeXMPScript"; // brand-ok: the library name scripts load
const FILE_UNKNOWN: u32 = 0x2020_2020;
const FILE_TEXT: u32 = 0x7465_7874;
const FILE_XML: u32 = 0x584D_4C20;
const OPEN_FOR_READ: u32 = 0x1;
const OPEN_FOR_UPDATE: u32 = 0x2;
const OPEN_ONLY_XMP: u32 = 0x4;
const OPEN_USE_PACKET_SCANNING: u32 = 0x40;
const CLOSE_UPDATE_SAFELY: u32 = 0x1;
const SERIALIZE_OMIT_PACKET_WRAPPER: u32 = 0x10;
const LIBRARY_SLOT: &str = "__vcLibrary";
const PATH: &str = "__vcXmpPath";
const FORMAT: &str = "__vcXmpFormat";
const FLAGS: &str = "__vcXmpFlags";
const RUN: &str = "__vcXmpRun";
const OPEN: &str = "__vcXmpOpen";
const PACKET: &str = "__vcXmpPacket";
const OFFSET: &str = "__vcXmpOffset";
const LENGTH: &str = "__vcXmpLength";
const WRITEABLE: &str = "__vcXmpWriteable";
const SIDECAR: &str = "__vcXmpSidecar";
const CHAR_FORM: &str = "__vcXmpCharForm";
const PENDING: &str = "__vcXmpPending";
/// The objects of `ExternalObject` and the XMP classes, kept by the realm, and its namespaces.
#[derive(Debug)]
pub(crate) struct Classes {
ids: Ids,
registry: Registry,
}
#[derive(Clone, Copy, Debug)]
struct Ids {
external_proto: ObjId,
meta: ObjId,
meta_proto: ObjId,
file: ObjId,
file_proto: ObjId,
constants: ObjId,
property_proto: ObjId,
}
impl Classes {
pub(crate) fn bytes(&self) -> usize {
self.registry.bytes()
}
/// The objects a collection keeps.
pub(crate) fn objects(&self) -> [ObjId; 7] {
let i = self.ids;
[i.external_proto, i.meta, i.meta_proto, i.file, i.file_proto, i.constants, i.property_proto]
}
}
fn ids(i: &Interp) -> JsResult {
i.xmp.as_ref().map(|c| c.ids).ok_or_else(|| Abrupt::Internal("ExternalObject is not installed".into()))
}
fn registry(i: &mut Interp) -> JsResult<&mut Registry> {
i.xmp.as_mut().map(|c| &mut c.registry).ok_or_else(|| Abrupt::Internal("ExternalObject is not installed".into()))
}
// ---- errors ----
fn not_yet(i: &mut Interp, what: &str) -> Abrupt {
i.throw(ErrorType::Error, &format!("{what} is not available yet"))
}
fn not_handled(i: &mut Interp, what: &str, value: &str) -> Abrupt {
let value = shown(value);
i.throw(ErrorType::Range, &format!("{what}: the value {value} is not available yet"))
}
fn wrong_object(i: &mut Interp, what: &str, class: &str) -> Abrupt {
i.type_error(&format!("{what}: the object is not an {class} object"))
}
/// The first 200 characters of `s`, the most of a script's text an error message holds.
fn shown(s: &str) -> String {
s.chars().take(200).collect()
}
/// The exception for `e`, which `what` met with the name or path `name`.
fn xmp_error(i: &mut Interp, what: &str, e: XmpError, name: &str) -> Abrupt {
let name = shown(name);
match e {
XmpError::Malformed => i.throw(ErrorType::Error, &format!("{what}: the XMP packet is not well-formed XMP")),
XmpError::TooLarge | XmpError::Namespaces => i.throw(ErrorType::Range, &format!("{what}: the XMP data is too large")),
XmpError::BadName => i.throw(ErrorType::Error, &format!("{what}: {name} is not a valid property name")),
XmpError::BadText => i.throw(ErrorType::Error, &format!("{what}: the text holds a character XML does not allow")),
XmpError::NotYet => not_handled(i, what, &name),
XmpError::Heap => Abrupt::Limit(crate::LimitKind::Heap),
}
}
fn not_array(i: &mut Interp, what: &str, name: &str) -> Abrupt {
let name = shown(name);
i.throw(ErrorType::Error, &format!("{what}: {name} is not an array"))
}
fn not_simple(i: &mut Interp, what: &str, name: &str) -> Abrupt {
let name = shown(name);
i.throw(ErrorType::Error, &format!("{what}: {name} is not a simple property"))
}
fn not_alt_text(i: &mut Interp, what: &str, name: &str) -> Abrupt {
let name = shown(name);
i.throw(ErrorType::Error, &format!("{what}: {name} is not a language alternative array"))
}
fn out_of_range(i: &mut Interp, what: &str, index: i64) -> Abrupt {
i.throw(ErrorType::Range, &format!("{what}: the array index {index} is out of range"))
}
fn unregistered(i: &mut Interp, what: &str, uri: &str) -> Abrupt {
let uri = shown(uri);
i.throw(ErrorType::Error, &format!("{what}: the namespace {uri} is not registered"))
}
// ---- small helpers ----
fn text(i: &mut Interp, v: &Value) -> JsResult {
Ok(i.to_str(v)?.to_rust_string())
}
/// A string argument's value as plain text, refused when it holds a character XML does not allow.
fn xml_text(i: &mut Interp, v: &Value, what: &str) -> JsResult {
let s = text(i, v)?;
if !xmp::is_text(&s) {
return Err(xmp_error(i, what, XmpError::BadText, ""));
}
Ok(s)
}
/// A flags argument: `undefined` and `null` are 0, any other value a whole number from 0 to
/// 2^32 - 1, refused with a `RangeError` otherwise.
fn flags(i: &mut Interp, v: &Value, what: &str) -> JsResult {
if matches!(v, Value::Undefined | Value::Null) {
return Ok(0);
}
let n = i.to_number(v)?;
if n.fract() != 0.0 || !(0.0..=f64::from(u32::MAX)).contains(&n) {
let shown = crate::number::to_string(n);
return Err(not_handled(i, what, &shown));
}
Ok(n as u32)
}
/// Refuses the bits of `value` outside `allowed`, naming the first.
fn only(i: &mut Interp, value: u32, allowed: u32, what: &str) -> JsResult<()> {
let extra = value & !allowed;
if extra != 0 {
let bit = 1u32 << extra.trailing_zeros();
return Err(not_handled(i, what, &format!("0x{bit:X}")));
}
Ok(())
}
/// Takes fuel for reading or writing `bytes` bytes of XMP.
fn spend(i: &mut Interp, bytes: usize) -> JsResult<()> {
crate::regexp_vm::use_fuel_n(i, 1 + bytes as u64 / 256)
}
fn slot(i: &Interp, o: ObjId, name: &str) -> JsResult {
if let Some(Class::XmpFile(fields)) = i.heap.object(o).map(|object| &object.class) {
return Ok(fields.get(&key(name)).cloned().unwrap_or(Value::Undefined));
}
Ok(match i.get_own_property(o, &key(name))? {
Some(Prop::Data { value, .. }) => value,
_ => Value::Undefined,
})
}
fn set_slot(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> {
set_slot_with_charge(i, o, name, value, true)
}
fn set_slot_with_charge(i: &mut Interp, o: ObjId, name: &str, value: Value, charged: bool) -> JsResult<()> {
if let Some(Class::XmpFile(fields)) = i.heap.object(o).map(|object| &object.class) {
let name = key(name);
let added = if fields.contains(&name) { 0 } else { PROP_BYTES + string_bytes(name.len()) };
let strings = if charged {
0
} else if let Value::String(s) = &value {
string_bytes(s.len())
} else {
0
};
return i.rooted(&[Value::Object(o), value.clone()], |i| {
i.reserve_heap(added + strings)?;
if let Some(Class::XmpFile(fields)) = i.heap.object_mut(o).map(|object| &mut object.class) {
fields.insert(name, value);
i.heap.bytes = i.heap.bytes.saturating_add(added);
}
Ok(())
});
}
if let Value::String(s) = &value {
i.rooted(std::slice::from_ref(&value), |i| i.reserve_heap(PROP_BYTES + string_bytes(s.len())))?;
}
i.define(o, key(name), Prop::Data { value, flags: 0 })
}
pub(crate) fn reset_file_state(fields: &mut PropMap) {
if let Some(packet) = fields.get_mut(&key(PACKET)) {
*packet = Value::Null;
}
if let Some(pending) = fields.get_mut(&key(PENDING)) {
*pending = Value::Undefined;
}
}
/// A string value, charged to the heap.
fn string(i: &mut Interp, s: &str) -> JsResult {
let bytes = string_bytes(s.encode_utf16().count());
i.reserve_heap(bytes)?;
i.heap.bytes = i.heap.bytes.saturating_add(bytes);
Ok(Value::str(s))
}
/// A new ordinary object with the prototype `proto` and the read-only data properties `fields`.
fn record(i: &mut Interp, proto: Option, fields: Vec<(&'static str, Value)>) -> JsResult {
i.rooted(&fields.iter().map(|(_, v)| v.clone()).collect::>(), |i| {
let keys: usize = fields.iter().map(|(name, _)| string_bytes(name.encode_utf16().count())).sum();
i.reserve_heap(OBJECT_BYTES + PROP_BYTES * fields.len() + keys)?;
let o = i.heap.alloc_object(Object::new(Class::Ordinary, proto.or(Some(i.intrinsics.object_proto))));
for (name, value) in &fields {
i.define(o, key(name), Prop::Data { value: value.clone(), flags: ENUMERABLE })?;
}
Ok(Value::Object(o))
})
}
// ---- ExternalObject ----
fn external_ctor(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult {
let name = text(i, &arg(args, 0))?;
if name != LIBRARY {
let name = shown(&name);
let what = "ExternalObject";
return Err(i.throw(ErrorType::Error, &format!("{what}: the library {name} is not available")));
}
let ids = ids(i)?;
let global = i.global;
i.reserve_heap(OBJECT_BYTES + PROP_BYTES)?;
let o = i.heap.alloc_object(Object::new(Class::Ordinary, Some(ids.external_proto)));
i.rooted(&[Value::Object(o)], |i| {
let library = string(i, LIBRARY)?;
data(i, o, LIBRARY_SLOT, library, 0)?;
let members = [("XMPMeta", ids.meta), ("XMPFile", ids.file), ("XMPConst", ids.constants)];
let mut added = 0;
for (name, _) in members {
if i.get_own_property(global, &key(name))?.is_none() {
added += PROP_BYTES + string_bytes(name.len());
}
}
i.reserve_heap(added)?;
for (name, value) in members {
crate::builtins::data(i, global, name, Value::Object(value), HIDDEN)?;
}
Ok(())
})?;
Ok(Value::Object(o))
}
fn external_search(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult {
Ok(Value::Bool(text(i, &arg(args, 0))? == LIBRARY))
}
fn external_terminate(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult {
let handle = this.as_object().filter(|&o| matches!(slot(i, o, LIBRARY_SLOT), Ok(Value::String(_))));
let Some(o) = handle else { return Err(wrong_object(i, "ExternalObject.terminate", "ExternalObject")) };
set_slot(i, o, LIBRARY_SLOT, Value::Bool(false))?;
Ok(Value::Undefined)
}
// ---- XMPMeta ----
fn is_meta(i: &Interp, v: &Value) -> Option {
v.as_object().filter(|&o| matches!(i.heap.object(o).map(|ob| &ob.class), Some(Class::Xmp(_))))
}
fn this_meta(i: &mut Interp, this: &Value, what: &str) -> JsResult {
match is_meta(i, this) {
Some(o) => Ok(o),
None => Err(wrong_object(i, what, "XMPMeta")),
}
}
fn meta_of(i: &Interp, o: ObjId) -> JsResult<&Meta> {
match i.heap.object(o).map(|ob| &ob.class) {
Some(Class::Xmp(m)) => Ok(m),
_ => Err(Abrupt::Internal("an XMPMeta lost its metadata".into())),
}
}
/// Changes the metadata of `o` with `f`, given `grow`, the most bytes the change adds, and charges
/// the heap for what it added or frees what it took out.
fn change(i: &mut Interp, o: ObjId, grow: usize, f: impl FnOnce(&mut Meta, &Registry) -> T) -> JsResult {
let accounting = meta_of(i, o)?.heap_bytes().saturating_mul(3).saturating_add(grow);
spend(i, accounting)?;
change_counted(i, o, grow, None, f)
}
fn change_counted(i: &mut Interp, o: ObjId, grow: usize, delta: Option<(usize, usize)>, f: impl FnOnce(&mut Meta, &Registry) -> T) -> JsResult {
i.reserve_heap(grow)?;
let Some(c) = i.xmp.as_ref() else { return Err(Abrupt::Internal("ExternalObject is not installed".into())) };
let Some(Class::Xmp(m)) = i.heap.object_mut(o).map(|ob| &mut ob.class) else {
return Err(Abrupt::Internal("an XMPMeta lost its metadata".into()));
};
let before = delta.map_or_else(|| m.heap_bytes(), |(removed, _)| removed);
let r = f(m, &c.registry);
let after = delta.map_or_else(|| m.heap_bytes(), |(_, added)| added);
i.heap.bytes = if after >= before { i.heap.bytes.saturating_add(after - before) } else { i.heap.bytes.saturating_sub(before - after) };
Ok(r)
}
/// A new `XMPMeta` holding `meta`.
fn new_meta(i: &mut Interp, meta: Meta) -> JsResult {
let ids = ids(i)?;
let bytes = meta.heap_bytes();
i.reserve_heap(OBJECT_BYTES.saturating_add(bytes))?;
let o = i.heap.alloc_object(Object::new(Class::Xmp(Box::new(meta)), Some(ids.meta_proto)));
i.heap.bytes = i.heap.bytes.saturating_add(bytes);
Ok(Value::Object(o))
}
/// The metadata of the packet `text`, read for `what`.
fn parse_packet(i: &mut Interp, what: &str, text: &str) -> JsResult {
let mut staged = registry(i)?.clone();
let copied = staged.bytes();
spend(i, text.len().saturating_add(2 * copied))?;
i.collect_garbage();
let budget = i.limits.heap_bytes.saturating_sub(i.heap.bytes).saturating_sub(OBJECT_BYTES);
let meta = xmp::parse_within(text, &mut staged, budget).map_err(|e| xmp_error(i, what, e, ""))?;
let growth = staged.bytes().saturating_sub(copied);
i.reserve_heap(OBJECT_BYTES.saturating_add(meta.heap_bytes()).saturating_add(growth))?;
*registry(i)? = staged;
i.heap.bytes = i.heap.bytes.saturating_add(growth);
Ok(meta)
}
fn meta_ctor(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult {
let meta = match arg(args, 0) {
Value::Undefined => Meta::default(),
Value::Object(o) if matches!(i.heap.object(o).map(|ob| &ob.class), Some(Class::Array { .. })) => {
return Err(not_yet(i, "new XMPMeta(buffer)"));
}
v => {
let packet = text(i, &v)?;
parse_packet(i, "XMPMeta", &packet)?
}
};
new_meta(i, meta)
}
/// The namespace argument `v`, refused when it is not registered.
fn namespace(i: &mut Interp, v: &Value, what: &str) -> JsResult {
let uri = text(i, v)?;
if registry(i)?.prefix_of(&uri).is_none() {
return Err(unregistered(i, what, &uri));
}
Ok(uri)
}
/// The namespace and path arguments at `at`, each converted once.
struct Target {
ns: String,
/// The property name and its step.
name: String,
step: Step,
/// The path as given.
path: String,
}
/// The namespace and path arguments at `at`.
fn target(i: &mut Interp, args: &[Value], at: usize, what: &str) -> JsResult {
let ns = namespace(i, &arg(args, at), what)?;
let path = text(i, &arg(args, at + 1))?;
// A packet holds no property in the RDF or XML namespace.
if ns == xmp::RDF || ns == xmp::XML {
return Err(xmp_error(i, what, XmpError::BadName, &path));
}
let r = xmp::parse_path(&path, &ns, registry(i)?);
match r {
Ok((name, step)) => Ok(Target { ns, name, step, path }),
Err(e) => Err(xmp_error(i, what, e, &path)),
}
}
/// The index an item position names in an array of `len` items: -1 is the last.
fn position(index: i64, len: usize) -> Option {
let i = if index == -1 { len } else { usize::try_from(index).ok()? };
(1..=len).contains(&i).then(|| i - 1)
}
/// An item position argument: a whole number from 1, or -1 for the last.
fn item_index(i: &mut Interp, v: &Value, what: &str) -> JsResult {
let n = i.to_number(v)?;
if n.fract() != 0.0 || !(n == -1.0 || (1.0..=1e15).contains(&n)) {
return Err(out_of_range(i, what, if n.is_finite() { n as i64 } else { 0 }));
}
Ok(n as i64)
}
/// An `XMPProperty` for `path` in `ns`.
fn property(i: &mut Interp, ns: &str, path: &str, value: Option<&str>, options: u32, locale: &str) -> JsResult {
let proto = ids(i)?.property_proto;
let ns = string(i, ns)?;
let path = i.rooted(std::slice::from_ref(&ns), |i| string(i, path))?;
let value = match value {
Some(v) => i.rooted(&[ns.clone(), path.clone()], |i| string(i, v))?,
None => Value::Undefined,
};
let locale = i.rooted(&[ns.clone(), path.clone(), value.clone()], |i| string(i, locale))?;
let fields = vec![("namespace", ns), ("path", path), ("value", value), ("options", Value::Number(f64::from(options))), ("locale", locale)];
record(i, Some(proto), fields)
}
fn property_to_string(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult {
let v = match this.as_object() {
Some(o) => slot(i, o, "value")?,
None => Value::Undefined,
};
if matches!(v, Value::Undefined) { Ok(Value::str("")) } else { Ok(Value::String(i.to_str(&v)?)) }
}
fn get_property(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult {
let what = "XMPMeta.getProperty";
let o = this_meta(i, &this, what)?;
let Target { ns, name, step, path } = target(i, args, 0, what)?;
if !matches!(arg(args, 2), Value::Undefined) {
let v = text(i, &arg(args, 2))?;
return Err(not_handled(i, what, &v));
}
let found = {
let m = meta_of(i, o)?;
match m.find(&ns, &name).map(|at| &m.props[at].value) {
None => Ok(None),
Some(value) => match step {
Step::Whole => Ok(Some((value.text().map(str::to_string), value.options(), String::new()))),
Step::Item(index) => match value {
xmp::Value::Array { items, .. } => Ok(position(index, items.len())
.and_then(|p| items.get(p))
.map(|item| (Some(item.text.clone()), item.options(), item.lang.clone().unwrap_or_default()))),
_ => Err(()),
},
},
}
};
let found = found.map_err(|()| not_array(i, what, &name))?;
let Some((text, options, locale)) = found else { return Ok(Value::Undefined) };
property(i, &ns, &path, text.as_deref(), options, &locale)
}
fn set_property(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult {
let what = "XMPMeta.setProperty";
let o = this_meta(i, &this, what)?;
let Target { ns, name, step, .. } = target(i, args, 0, what)?;
let options = flags(i, &arg(args, 3), what)?;
only(i, options, xmp::PROP_IS_ARRAY | xmp::ARRAY_IS_ORDERED | xmp::ARRAY_IS_ALTERNATIVE, what)?;
if !matches!(arg(args, 4), Value::Undefined) {
let v = text(i, &arg(args, 4))?;
return Err(not_handled(i, what, &v));
}
let value = arg(args, 2);
if let Step::Item(index) = step {
// An array item is a simple value, which takes no options.
only(i, options, 0, what)?;
let v = xml_text(i, &value, what)?;
return set_item(i, o, &ns, &name, index, v, false, what);
}
if options != 0 {
// An array is made empty; one that exists is left as it is.
if !matches!(value, Value::Undefined | Value::Null) {
let v = text(i, &value)?;
return Err(not_handled(i, what, &v));
}
let kind = {
let m = meta_of(i, o)?;
m.find(&ns, &name).map(|at| matches!(m.props[at].value, xmp::Value::Array { .. }))
};
return match kind {
Some(true) => Ok(Value::Undefined),
Some(false) => Err(not_array(i, what, &name)),
None => {
let prop = xmp::Prop { ns: ns.into(), name, value: xmp::Value::Array { form: Form::from_options(options), items: Vec::new() } };
change(i, o, prop.heap_bytes(), |m, _| m.props.push(prop)).map(|()| Value::Undefined)
}
};
}
let v = xml_text(i, &value, what)?;
let simple = {
let m = meta_of(i, o)?;
m.find(&ns, &name).map(|at| (at, matches!(m.props[at].value, xmp::Value::Simple { .. })))
};
let new = xmp::Value::Simple { text: v, uri: false };
match simple {
Some((_, false)) => Err(not_simple(i, what, &name)),
Some((at, true)) => {
let grow = 96 + new.text().map_or(0, str::len);
change(i, o, grow, |m, _| {
if let Some(p) = m.props.get_mut(at) {
p.value = new;
}
})
}
None => {
let prop = xmp::Prop { ns: ns.into(), name, value: new };
change(i, o, prop.heap_bytes(), |m, _| m.props.push(prop))
}
}
.map(|()| Value::Undefined)
}
fn delete_property(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult {
let what = "XMPMeta.deleteProperty";
let o = this_meta(i, &this, what)?;
let Target { ns, name, step, .. } = target(i, args, 0, what)?;
match step {
Step::Whole => {
let at = meta_of(i, o)?.find(&ns, &name);
if let Some(at) = at {
change(i, o, 0, |m, _| m.props.remove(at))?;
}
Ok(Value::Undefined)
}
Step::Item(index) => delete_item(i, o, &ns, &name, index, what, true),
}
}
fn does_property_exist(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult {
let what = "XMPMeta.doesPropertyExist";
let o = this_meta(i, &this, what)?;
let Target { ns, name, step, .. } = target(i, args, 0, what)?;
let m = meta_of(i, o)?;
let exists = match (m.find(&ns, &name).map(|at| &m.props[at].value), step) {
(None, _) => false,
(Some(_), Step::Whole) => true,
(Some(xmp::Value::Array { items, .. }), Step::Item(index)) => position(index, items.len()).is_some(),
(Some(_), Step::Item(_)) => false,
};
Ok(Value::Bool(exists))
}
/// The items of the array `name` in `ns` of `o`, `None` when it has no such property, refused when
/// the property is not an array.
fn array_items(i: &mut Interp, o: ObjId, ns: &str, name: &str, what: &str) -> JsResult