mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Scripts: DocumentPreset arrays count each element against the heap
The heap limit on DocumentPreset values counted a boolean as 4 bytes, while the host keeps a whole value for each array element, so arrays kept by many presets could hold several times the run's heap. The limit now counts the host's copy: a value for the field and for each element or property, plus the text of its strings and property names.
This commit is contained in:
1 parent
5cb70ea010
commit
99e07b0f6b
2 files changed
+32
-2
No files matched your search
@@ -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::<String>() + k.len()) as u64 + held_bytes(v)).sum(),
|
||||
Data::Undefined | Data::Null | Data::Bool(_) | Data::Number(_) => 0,
|
||||
};
|
||||
size_of::<Data>() 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)),
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user