Scripts: a failed UnitValue install leaves no constructor or operators behind

When defining the UnitValue members fails part way, the realm turns the
unit operators off again and restores the global UnitValue to what it was
before the call, so a second add_unit_values() installs everything.
This commit is contained in:
dotCooCoo
2026-11-22 00:30:42 -08:00
parent af91c28c64
commit e1e99023c2
3 changed files with 89 additions and 6 deletions
+2 -1
View File
@@ -532,7 +532,8 @@ impl Realm {
/// Adds the global constructor `UnitValue`, measurements with a magnitude and a unit, and has
/// the arithmetic, comparison and unary operators calculate with its objects
/// ([`crate::unit_value`]). A realm without it keeps the standard operators. A second call
/// changes nothing.
/// after one that succeeded changes nothing; after one that failed, it installs everything
/// again.
pub fn add_unit_values(&mut self) -> Result<(), RunError> {
match crate::unit_value::install(&mut self.interp) {
Ok(()) => Ok(()),
+37 -5
View File
@@ -452,15 +452,47 @@ fn accessor(i: &mut Interp, on: ObjId, name: &'static str, get: NativeFn, set: N
}
/// Adds the global constructor `UnitValue` ([`crate::Realm::add_unit_values`]). A second call
/// changes nothing.
/// after one that succeeded changes nothing; after one that failed, it installs everything again.
pub(crate) fn install(i: &mut Interp) -> JsResult<()> {
install_with(i, define_members)
}
/// [`install`] with `define` defining the members on the new prototype. When `define` fails, the
/// realm is left without the operators, and the global `UnitValue` as it was before the call.
pub(crate) fn install_with(i: &mut Interp, define: impl FnOnce(&mut Interp, ObjId) -> JsResult<()>) -> JsResult<()> {
if i.unit_values.is_some() {
return Ok(());
}
let object_proto = i.intrinsics.object_proto;
i.reserve_heap(OBJECT_BYTES)?;
let proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
i.unit_values = Some(Classes { proto, base: DEFAULT_BASE });
let global = i.global;
let name = key("UnitValue");
let previous = i.get_own_property(global, &name)?;
let held: Vec<Value> = match &previous {
Some(Prop::Data { value, .. }) => vec![value.clone()],
Some(Prop::Accessor { get, set, .. }) => get.iter().chain(set.iter()).map(|&o| Value::Object(o)).collect(),
None => Vec::new(),
};
i.rooted(&held, |i| {
let object_proto = i.intrinsics.object_proto;
i.reserve_heap(OBJECT_BYTES)?;
let proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
// `unit_values` roots the prototype while the members are defined, and turns the operators on.
i.unit_values = Some(Classes { proto, base: DEFAULT_BASE });
let defined = define(i, proto);
if defined.is_err() {
i.unit_values = None;
match previous {
Some(p) => i.define(global, name, p)?,
None => {
i.object_mut(global)?.props.remove(&name);
}
}
}
defined
})
}
/// Defines the constructor, its `baseUnit` and the prototype's members.
fn define_members(i: &mut Interp, proto: ObjId) -> JsResult<()> {
let ctor = native(i, "UnitValue", unit_value_ctor, 2, NativeKind::Plain, true)?;
constructor(i, "UnitValue", ctor, proto)?;
accessor(i, ctor, "baseUnit", get_class_base, set_class_base)?;
+50
View File
@@ -385,6 +385,56 @@ fn each_measurement_counts_toward_the_heap() {
assert_eq!(measures - plain, 100 * crate::heap::UNIT_VALUE_BYTES);
}
#[test]
fn an_install_that_hits_the_heap_limit_installs_everything_on_the_next_call() {
let cost = {
let mut r = Realm::new();
let before = r.collect_garbage();
r.add_unit_values().unwrap();
r.collect_garbage() - before
};
let mut failed = 0;
for step in 0..=16 {
let mut r = Realm::new();
let live = r.collect_garbage();
r.set_limits(Limits { heap_bytes: live + cost * step / 16, ..Limits::default() });
let first = r.add_unit_values();
r.set_limits(Limits::default());
if first.is_err() {
failed += 1;
r.add_unit_values().unwrap();
}
assert_eq!(number(&mut r, "UnitValue(1, 'in').as('pt')"), 72.0, "limit at {step}/16 of the install");
}
assert!(failed >= 1, "no limit stopped the install");
}
/// Installs with members that stop after the global `UnitValue` is defined.
fn stopped_install(r: &mut Realm) {
let stopped = crate::unit_value::install_with(r.interp(), |i, proto| {
crate::builtins::method(i, proto, "toString", |_, _, _, _| Ok(crate::value::Value::Undefined), 0)?;
let global = i.global;
crate::builtins::data(i, global, "UnitValue", crate::value::Value::Number(1.0), 0)?;
Err(crate::interp::Abrupt::Internal("stopped".into()))
});
assert!(stopped.is_err());
}
#[test]
fn an_install_that_fails_part_way_installs_everything_on_the_next_call() {
let mut r = Realm::new();
stopped_install(&mut r);
assert_eq!(text(&mut r, "typeof UnitValue"), "undefined");
assert!(boolean(&mut r, "!('UnitValue' in this)"));
let mut kept = Realm::new();
kept.run("var UnitValue = 'mine';").unwrap();
stopped_install(&mut kept);
assert_eq!(text(&mut kept, "UnitValue"), "mine");
r.add_unit_values().unwrap();
assert_eq!(number(&mut r, "UnitValue(1, 'in').as('pt')"), 72.0);
assert_eq!(text(&mut r, "String(UnitValue(1, 'in') + UnitValue(72, 'pt'))"), "2 in");
}
#[test]
fn a_second_install_changes_nothing() {
let mut r = realm();