diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 0753cc5263..8054a1b174 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -996,7 +996,7 @@ fn new_preset(h: &Host) -> Answer { item("numArtboards", num(1.0)), item("width", num(612.0)), ]; - hold_preset_bytes(h, 0, fields.iter().map(|(_, v)| data_size(v)).sum())?; + hold_preset_bytes(h, 0, fields.iter().map(|(_, v)| held_bytes(v)).sum())?; presets.push(fields); Ok(host_ref("DocumentPreset", &[float(h.run), float(presets.len() as u64 - 1)])) } @@ -1012,6 +1012,18 @@ fn hold_preset_bytes(h: &Host, replaced: u64, added: u64) -> std::result::Result Ok(()) } +/// The bytes the host's copy of `d` takes: a `Data` for the value and each element or property +/// in it, plus the text of its strings and property names. +fn held_bytes(d: &Data) -> u64 { + let owned = match d { + Data::String(s) => s.len() as u64, + Data::Array(a) => a.iter().map(held_bytes).sum(), + Data::Object(o) => o.iter().map(|(k, v)| (size_of::() + k.len()) as u64 + held_bytes(v)).sum(), + Data::Undefined | Data::Null | Data::Bool(_) | Data::Number(_) => 0, + }; + size_of::() as u64 + owned +} + /// The most `DocumentPreset` objects one run can make. const MAX_PRESETS: usize = 10_000; @@ -1067,7 +1079,7 @@ fn preset_field(link: &Link, key: &'static str) -> HostMember { // The values the presets hold now count against the run's heap: a value assigned again // replaces the one before it. let at = fields.iter().position(|(k, _)| k == key); - hold_preset_bytes(&h, at.map_or(0, |n| data_size(&fields[n].1)), data_size(&value))?; + hold_preset_bytes(&h, at.map_or(0, |n| held_bytes(&fields[n].1)), held_bytes(&value))?; match at { Some(n) => fields[n].1 = value, None => fields.push((key.to_string(), value)), diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index d0cb4e7ecd..039d5d17e8 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -829,6 +829,24 @@ fn document_preset_values_are_bounded_by_the_runs_heap() { assert!(out[1].as_str().unwrap().starts_with("RangeError: the DocumentPreset settings"), "{out:?}"); } +/// An array a preset holds counts a whole value for each element, so arrays of booleans kept by +/// many presets stop at the run's heap too. +#[test] +fn document_preset_arrays_count_each_element_they_hold() { + use crate::script::TEST_ENGINE_HEAP; + TEST_ENGINE_HEAP.with(|h| h.set(Some(8 << 20))); + let mut s = Session::new(); + let out = log( + &mut s, + "#targetengine presets\nvar a = []; for (var i = 0; i < 20000; i++) a.push(true); var m = '';\ + try { for (var k = 0; k < 40; k++) { var p = new DocumentPreset(); p.title = a; } } catch (e) { m = e.name + ': ' + e.message; }\ + vc.log(k < 40); vc.log(m);", + ); + TEST_ENGINE_HEAP.with(|h| h.set(None)); + assert_eq!(out[0], json!(true), "{out:?}"); + assert!(out[1].as_str().unwrap().starts_with("RangeError: the DocumentPreset settings"), "{out:?}"); +} + #[test] fn the_same_document_or_item_is_the_same_object() { let (mut s, a, _) = doc();