Scripts: script tests build with main's host classes and preset checks

main added HostClass initializers in host_tests.rs without the reflect
`info` field, a second test named
assigning_undefined_to_the_selection_throws_and_keeps_it, and type and
size checks on DocumentPreset settings.

- host_tests.rs: the six new HostClass initializers give `info`.
- The selection test for an index past the end of pageItems is renamed
  assigning_an_index_past_the_end_to_the_selection_throws_and_keeps_it.
- The preset heap tests used titles of 512 KiB and an array of 20,000
  booleans as a title, which the setter now refuses. They now reach the
  run's heap with 4,092-byte titles, and compare presets holding a
  four-number documentBleedOffsetRect with presets holding an eight-byte
  title, so each array element still counts.
This commit is contained in:
dotCooCoo committed 2026-11-27 23:29:12 -08:00
1 parent f56fcd99f6
commit 0a46b8e849
2 files changed
+42 -23

No files matched your search

+29 -20
View File
@@ -304,7 +304,7 @@ fn selection_assignment_is_atomic_across_invalid_items() {
/// An index past the end of `pageItems` gives `undefined`, which does not clear the selection;
/// `null` and `[]` do.
#[test]
fn assigning_undefined_to_the_selection_throws_and_keeps_it() {
fn assigning_an_index_past_the_end_to_the_selection_throws_and_keeps_it() {
let (mut s, a, _) = doc();
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
let caught = "var d = app.activeDocument, m = '';\
@@ -1064,16 +1064,17 @@ fn a_document_preset_is_an_instance_of_its_class() {
#[test]
fn document_preset_values_are_bounded_by_the_runs_heap() {
use crate::script::TEST_ENGINE_HEAP;
TEST_ENGINE_HEAP.with(|h| h.set(Some(8 << 20)));
TEST_ENGINE_HEAP.with(|h| h.set(Some(2 << 20)));
let mut s = Session::new();
let big = "var s = 'a'; while (s.length < (1 << 19)) s += s;";
// A title of 4,092 bytes, which with the loop counter stays within the 4,096 a title may have.
let big = "var s = 'a'; while (s.length < 4092) s += 'a';";
let again =
format!("#targetengine presets\n{big} var p = new DocumentPreset(); for (var k = 0; k < 40; k++) p.title = s + k; vc.log(p.title.length);");
assert_eq!(log(&mut s, &again), vec![json!(524_290)]);
format!("#targetengine presets\n{big} var p = new DocumentPreset(); for (var k = 0; k < 1000; k++) p.title = s + k; vc.log(p.title.length);");
assert_eq!(log(&mut s, &again), vec![json!(4095)]);
let many = format!(
"#targetengine presets\n{big} var m = '';\
try {{ for (var k = 0; k < 40; k++) {{ var p = new DocumentPreset(); p.title = s; }} }} catch (e) {{ m = e.name + ': ' + e.message; }}\
vc.log(k < 40); vc.log(m);"
try {{ for (var k = 0; k < 2000; k++) {{ var p = new DocumentPreset(); p.title = s; }} }} catch (e) {{ m = e.name + ': ' + e.message; }}\
vc.log(k < 2000); vc.log(m);"
);
let out = log(&mut s, &many);
TEST_ENGINE_HEAP.with(|h| h.set(None));
@@ -1081,22 +1082,30 @@ 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.
/// An array a preset holds counts a whole value for each element: presets that each hold a
/// `documentBleedOffsetRect` of four numbers reach the run's heap after fewer presets than ones
/// that each hold an eight-byte `title`, which counts one value and its text.
#[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:?}");
let made = |assign: &str| {
TEST_ENGINE_HEAP.with(|h| h.set(Some(1 << 20)));
let mut s = Session::new();
let out = log(
&mut s,
&format!(
"#targetengine presets\nvar m = '';\
try {{ for (var k = 0; k < 10000; k++) {{ var p = new DocumentPreset(); {assign} }} }} catch (e) {{ m = e.name + ': ' + e.message; }}\
vc.log(k); vc.log(m);"
),
);
TEST_ENGINE_HEAP.with(|h| h.set(None));
assert!(out[1].as_str().unwrap().starts_with("RangeError: the DocumentPreset settings"), "{assign}: {out:?}");
out[0].as_f64().unwrap()
};
let with_title = made("p.title = 'abcdefgh';");
let with_rect = made("p.documentBleedOffsetRect = [1, 2, 3, 4];");
assert!(with_rect < with_title, "{with_rect} presets with a rectangle, {with_title} with a title");
}
#[test]
+13 -3
View File
@@ -18,7 +18,7 @@ fn non_finite_indexer_keys_reach_the_host_unchanged() {
assert!(matches!(args.get(1), Some(Data::Number(n)) if !n.is_finite()), "{args:?}");
Ok(Data::Bool(true))
});
let class = HostClass { name: "Indexed".into(), parent: None, members: vec![], index: Some(index), construct: None };
let class = HostClass { name: "Indexed".into(), parent: None, members: vec![], info: Vec::new(), index: Some(index), construct: None };
r.define_host_classes(vec![class], vec![("it".into(), host_ref("Indexed", &[1.0]))]).unwrap();
let source = "[it[NaN], it[Infinity], it[-Infinity], NaN in it, Infinity in it, -Infinity in it].join('|')";
assert_eq!(r.run(source).unwrap(), JsValue::String("true|true|true|true|true|true".into()));
@@ -39,6 +39,7 @@ fn host_receivers_obey_limits_lowered_after_the_object_was_created() {
name: "Indexed".into(),
parent: None,
members: vec![("method".into(), HostMember::Method(function.clone())), ("take".into(), HostMember::Method(function.clone()))],
info: Vec::new(),
index: Some(function),
construct: None,
};
@@ -54,7 +55,14 @@ fn host_receivers_obey_limits_lowered_after_the_object_was_created() {
#[test]
fn index_keys_share_the_receivers_data_budget() {
let mut r = realm(1_000_000);
let class = HostClass { name: "Indexed".into(), parent: None, members: vec![], index: Some(Rc::new(|_| Ok(Data::Bool(true)))), construct: None };
let class = HostClass {
name: "Indexed".into(),
parent: None,
members: vec![],
info: Vec::new(),
index: Some(Rc::new(|_| Ok(Data::Bool(true)))),
construct: None,
};
r.define_host_classes(vec![class], vec![("it".into(), host_ref("Indexed", &[1.0]))]).unwrap();
r.set_data_limits(DataLimits { depth: 64, bytes: 256, entries: 100 });
assert_eq!(r.run("it['short']").unwrap(), JsValue::Bool(true));
@@ -908,6 +916,7 @@ fn a_member_a_class_declares_unavailable_is_not_its_own_when_its_parent_has_it()
name: "Base".into(),
parent: None,
members: vec![("size".to_string(), HostMember::Get(value.clone())), ("grow".to_string(), HostMember::Method(value))],
info: Vec::new(),
index: None,
construct: None,
};
@@ -915,6 +924,7 @@ fn a_member_a_class_declares_unavailable_is_not_its_own_when_its_parent_has_it()
name: "Leaf".into(),
parent: Some("Base".into()),
members: vec![("size".to_string(), HostMember::Unavailable(missing))],
info: Vec::new(),
index: None,
construct: None,
};
@@ -935,7 +945,7 @@ fn assigning_a_name_a_prototype_has_does_not_call_the_indexer() {
_ => Err(HostError::Error("no such member".into())),
}
});
let class = HostClass { name: "List".into(), parent: None, members: Vec::new(), index: Some(index), construct: None };
let class = HostClass { name: "List".into(), parent: None, members: Vec::new(), info: 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));