Files
vectorcraft/crates/js/src/scriptui.rs
T
dotCooCoo 3089ed73a5 Scripts: ScriptUI listeners are limited and removed controls leave their dialog
- A control with alignment fill stays within its maximumSize when its
  container's room is larger.
- A window or control takes at most 256 event listeners (VectorCraft's
  own limit); one more throws a RangeError. A show listener can be
  added only to the dialog, and on a control it throws an Error.
- A capturing listener on a container that stops a click keeps the
  default or cancel button from closing the dialog. Listeners on the
  containers that do not capture still leave the button to close it.
- stopPropagation() acts only on an event whose listeners are running,
  and preventDefault() does nothing, since the events are not
  cancelable.
- A control removed from its container has no parent, its events no
  longer reach the containers it was in, and add and remove on it, or
  on a control it holds, throw an Error. Its controls are no longer
  counted toward the dialog's limits.
- remove(text) throws an Error, and removes nothing, when a text getter
  changes the list or container while it looks for a match.
- A group takes its creation properties fourth again when the third
  argument is not an object, as in add("group", undefined, undefined,
  {name: "g"}).
- A click on a radio button now turns off only the radio buttons of its
  set, the ones added next to it with no other kind of control between
  them. It used to turn off every radio button in its container.

Docs: docs/mcp.md now says that notify runs the handlers without closing
the dialog.
2026-11-23 12:25:38 -08:00

1625 lines
70 KiB
Rust

//! ScriptUI's `Window` with modal dialogs, as the help page
//! <https://extendscript.docsforadobe.dev/user-interface-tools/window-object/> describes it, whose
//! drawing a host does ([`crate::Realm::define_script_ui`]).
//!
//! `new Window("dialog"[, title, bounds, properties])` makes a dialog, with or without `new`. A
//! window of type `palette` or `window`, and a resource specification in place of the type, throw
//! an `Error` that says they are not available yet. `add(type[, bounds, text, properties])` adds a
//! control to a dialog, a `group` or a `panel` and returns it: `statictext`, `edittext`, `button`,
//! `checkbox`, `radiobutton`, `dropdownlist`, `listbox` (whose third argument is an array of item
//! texts), `slider` (third to fifth: value, minimum, maximum) and `progressbar` (value, maximum).
//! The other documented control types throw an `Error` that says they are not available yet. A
//! group takes its creation properties as the third argument (or the fourth when the third is not
//! an object), and a list takes them there in place of its items, with the items in
//! `properties.items`. A list's `add("item", text)` adds an item, `find(text)` finds one and
//! `remove(what)` removes one; its `selection` is an item or `null`, and takes an item, an index or
//! `null`. A container's `remove(what)` removes a control, which then has no `parent` and no longer
//! belongs to the dialog: `add` and `remove` on it, or on a control it holds, throw an `Error`.
//! Each element starts with `preferredSize`, `size`, `minimumSize` and `maximumSize` objects whose
//! `width` and `height` are -1, which leaves the size to the layout. Setting a radio button's
//! `value` to true sets the other radio buttons next to it in its container to false, as a click
//! does.
//!
//! `addEventListener(type, handler[, capture])` and `removeEventListener` register handlers for
//! the events `show()` runs: `click`, `change`, `changing`, and `show` on a dialog only; a window
//! or control takes at most [`MAX_LISTENERS`] of them. A handler gets an event object, in the
//! capture phase on the containers above the control, then on the control after its `onClick`,
//! `onChange` or `onChanging`, then in the bubbling phase on the containers again, until one calls
//! `stopPropagation()`. Events are not cancelable, and `preventDefault()` does nothing.
//! `notify(event)` runs the same handlers, and `notify("onClick")` turns a check box over and turns
//! a radio button on first; it does not close the dialog. `Window.alert`, `Window.confirm` and
//! `Window.prompt` are the global functions of those names, when the host defines them.
//!
//! `show()` gives the host's `open` the dialog's description and runs `onShow`. Then, until the
//! dialog closes, it gives the host's `wait` the description again, and `wait` gives back what the
//! person did, which may pause the run ([`crate::interp::Pause`]): a `click` on a control, a
//! `change` with its new value, or a `close`. The control takes the change, its `onClick`,
//! `onChanging` or `onChange` runs, and the next description shows what the handler changed. A
//! button without `onClick` and without a `click` listener of its own closes the dialog with 1 when
//! it is the dialog's default element and with 2 when it is its cancel element, unless a capturing
//! listener on a container stopped the click. `close(result)` closes the dialog after the running
//! handler returns, and `show()` gives `result`, or 0 without one; `onClose` runs first and keeps
//! the dialog open when it returns `false`. A dialog that is showing cannot be shown again, and no
//! other dialog can be shown while one is. When `show()` ends, the host's `end` is given its result
//! and whether it ended normally.
//!
//! An element keeps its kind, its container, its controls and a list's items in properties that a
//! script cannot change. The description lists the dialog first and every control after the
//! container that holds it; it is read with a loop, and a dialog with more than [`MAX_CONTROLS`]
//! controls, nested more than [`MAX_DEPTH`] deep, with more than [`MAX_ITEMS`] list items, or with a
//! text longer than [`MAX_TEXT_BYTES`] throws a `RangeError`. These and [`MAX_LISTENERS`] are
//! VectorCraft's own limits. A getter that changes a list's items after the list was read makes
//! `show()` read the dialog again, and a list that changes again throws an `Error`. A `text` getter
//! that changes a list or a container while `remove(text)` looks for an item or a control makes
//! `remove` throw an `Error`.
use crate::HostItem;
use crate::builtins::{arg, constructor, data, method, native, pausable_method};
use crate::heap::{Class, ENUMERABLE, HIDDEN, NativeKind, OBJECT_BYTES, OPEN, ObjId, Object, PROP_BYTES, Prop, key};
use crate::host::Data;
use crate::interp::{Abrupt, ErrorType, Interp, JsResult, Pause};
use crate::value::{Value, index_key};
/// The most controls a dialog holds, its groups and panels included.
pub const MAX_CONTROLS: usize = 512;
/// How deeply controls nest in a dialog: a control the dialog holds is at depth 1.
pub const MAX_DEPTH: usize = 16;
/// The most items a dialog's lists hold together.
pub const MAX_ITEMS: usize = 1024;
/// The most bytes (UTF-8) of one text in a dialog: a control's text or name, or a list item's.
pub const MAX_TEXT_BYTES: usize = 16 << 10;
/// The most event listeners one window or control holds.
pub const MAX_LISTENERS: usize = 256;
/// The control types `add` makes.
pub const BUILT: [&str; 11] =
["group", "panel", "statictext", "edittext", "button", "checkbox", "radiobutton", "dropdownlist", "listbox", "slider", "progressbar"];
/// The documented control types that are not available yet.
const LATER: [&str; 10] =
["tabbedpanel", "tab", "scrollbar", "image", "iconbutton", "treeview", "flashplayer", "customview", "custombutton", "customboundedvalue"];
const KIND: &str = "__vcKind";
const PARENT: &str = "__vcParent";
const WINDOW: &str = "__vcWindow";
const KIDS: &str = "__vcKids";
const DEPTH: &str = "__vcDepth";
const COUNT: &str = "__vcCount";
const ITEM_COUNT: &str = "__vcItemCount";
const CLOSING: &str = "__vcClosing";
const ITEMS: &str = "__vcItems";
const SELECTION: &str = "__vcSelection";
const LIST: &str = "__vcList";
const VALUE: &str = "__vcValue";
const LISTENERS: &str = "__vcListeners";
const STOPPED: &str = "__vcStopped";
/// The size properties every element starts with, as objects with `width` and `height`.
const SIZES: [&str; 4] = ["preferredSize", "size", "minimumSize", "maximumSize"];
/// The events `addEventListener` takes, which `show()` and `notify` run.
const EVENTS: [&str; 4] = ["click", "change", "changing", "show"];
/// The other documented events, which nothing runs yet.
const EVENTS_LATER: [&str; 14] = [
"move",
"moving",
"resize",
"resizing",
"enterKey",
"focus",
"blur",
"mousedown",
"mouseup",
"mousemove",
"mouseover",
"mouseout",
"keyup",
"keydown",
];
/// The objects of `Window`, kept by the realm, and the dialog that is showing.
#[derive(Clone, Debug)]
pub(crate) struct Classes {
window_proto: ObjId,
control_proto: ObjId,
item_proto: ObjId,
event_proto: ObjId,
layout: ObjId,
host: ObjId,
selection_get: ObjId,
selection_set: ObjId,
radio_get: ObjId,
radio_set: ObjId,
showing: Option<ObjId>,
/// The event objects whose listeners are running, innermost last.
dispatching: Vec<ObjId>,
}
impl Classes {
/// The objects a collection keeps.
pub(crate) fn objects(&self) -> Vec<ObjId> {
let mut all = vec![
self.window_proto,
self.control_proto,
self.item_proto,
self.event_proto,
self.layout,
self.host,
self.selection_get,
self.selection_set,
self.radio_get,
self.radio_set,
];
all.extend(self.showing);
all.extend(self.dispatching.iter().copied());
all
}
}
/// Forgets the dialog that was showing, and the events that were running, when a run ended where
/// it was.
pub(crate) fn end_run(i: &mut Interp) {
if let Some(c) = i.script_ui.as_mut() {
c.showing = None;
c.dispatching.clear();
}
}
fn classes(i: &Interp) -> JsResult<Classes> {
i.script_ui.clone().ok_or_else(|| Abrupt::Internal("Window is not installed".into()))
}
fn error(i: &mut Interp, message: &str) -> Abrupt {
i.throw(ErrorType::Error, message)
}
fn range(i: &mut Interp, message: &str) -> Abrupt {
i.throw(ErrorType::Range, message)
}
/// The value of the own data property `name` of `o`, `undefined` when it has none.
fn slot(i: &Interp, o: ObjId, name: &str) -> JsResult<Value> {
Ok(match i.get_own_property(o, &key(name))? {
Some(Prop::Data { value, .. }) => value,
_ => Value::Undefined,
})
}
/// Sets the property `name` of `o` that a script can read but not change.
fn set_slot(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> {
i.rooted(std::slice::from_ref(&value), |i| i.reserve_heap(PROP_BYTES))?;
i.define(o, key(name), Prop::Data { value, flags: 0 })
}
/// Sets the property `name` of `o` that a script can change.
fn set_open(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> {
i.rooted(std::slice::from_ref(&value), |i| i.reserve_heap(PROP_BYTES))?;
i.define(o, key(name), Prop::Data { value, flags: OPEN })
}
fn number_slot(i: &Interp, o: ObjId, name: &str) -> JsResult<f64> {
Ok(match slot(i, o, name)? {
Value::Number(n) => n,
_ => 0.0,
})
}
fn object_slot(i: &Interp, o: ObjId, name: &str) -> JsResult<Option<ObjId>> {
Ok(slot(i, o, name)?.as_object())
}
/// An element's kind, or `None` for a value that is not a ScriptUI window or control.
fn kind_of(i: &Interp, v: &Value) -> JsResult<Option<(ObjId, String)>> {
let Some(o) = v.as_object() else { return Ok(None) };
Ok(match slot(i, o, KIND)? {
Value::String(s) => Some((o, s.to_rust_string())),
_ => None,
})
}
/// `this` as an element, or a `TypeError` naming `what`.
fn element(i: &mut Interp, this: &Value, what: &str) -> JsResult<(ObjId, String)> {
match kind_of(i, this)? {
Some(e) => Ok(e),
None => Err(i.type_error(&format!("{what}: this is not a ScriptUI window or control"))),
}
}
/// The dialog that holds the element `o`, or an `Error` naming `what` for a control that was
/// removed from its dialog.
fn window_of(i: &mut Interp, o: ObjId, what: &str) -> JsResult<ObjId> {
match object_slot(i, o, WINDOW)? {
Some(w) => Ok(w),
None => Err(error(i, &format!("{what}: the control was removed from its dialog"))),
}
}
/// Are `a` and `b` the same object?
fn same(a: &Value, b: &Value) -> bool {
matches!((a.as_object(), b.as_object()), (Some(x), Some(y)) if x == y)
}
/// The values of an array made by [`list`].
fn list_values(i: &Interp, a: ObjId) -> JsResult<Vec<Value>> {
let length = match &i.object(a)?.class {
Class::Array { length, .. } => *length,
_ => return Ok(Vec::new()),
};
(0..length)
.map(|n| {
Ok(match i.get_own_property(a, &index_key(n))? {
Some(Prop::Data { value, .. }) => value,
_ => Value::Undefined,
})
})
.collect()
}
/// A new array holding `items`: one a script cannot change unless `open`.
fn list(i: &mut Interp, items: &[Value], open: bool) -> JsResult<ObjId> {
i.rooted(items, |i| i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(items.len() + 1))))?;
let proto = i.intrinsics.array_proto;
let a = i.heap.alloc_object(Object::new(Class::Array { length: 0, length_writable: open }, Some(proto)));
let flags = if open { OPEN } else { ENUMERABLE };
for (n, v) in items.iter().enumerate() {
let n = u32::try_from(n).map_err(|_| Abrupt::Internal("a list is too long".into()))?;
i.define(a, index_key(n), Prop::Data { value: v.clone(), flags })?;
}
i.object_mut(a)?.extensible = open;
Ok(a)
}
/// Adds `v` to the end of the list in `o`'s slot `name`, and puts a copy a script can change in
/// its property `public`.
fn append(i: &mut Interp, o: ObjId, name: &str, public: &str, v: Value) -> JsResult<()> {
let mut items = match object_slot(i, o, name)? {
Some(a) => list_values(i, a)?,
None => Vec::new(),
};
items.push(v);
i.rooted(&items.clone(), |i| {
let kept = list(i, &items, false)?;
set_slot(i, o, name, Value::Object(kept))?;
let copy = list(i, &items, true)?;
set_open(i, o, public, Value::Object(copy))
})
}
fn text_arg(i: &mut Interp, v: &Value) -> JsResult<Value> {
Ok(match v {
Value::Undefined => Value::str(""),
v => Value::String(i.to_str(v)?),
})
}
/// A new element of `kind`, with the properties every element has.
fn new_element(
i: &mut Interp,
proto: ObjId,
kind: &str,
parent: Option<ObjId>,
window: Option<ObjId>,
depth: usize,
args: [&Value; 3],
) -> JsResult<ObjId> {
let [bounds, text, props] = args;
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(32)))?;
let o = i.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
i.temp_roots.push(Value::Object(o));
let window = window.unwrap_or(o);
let parent = parent.map_or(Value::Null, Value::Object);
let layout = classes(i)?.layout;
set_slot(i, o, KIND, Value::str(kind))?;
set_slot(i, o, PARENT, parent.clone())?;
set_slot(i, o, WINDOW, Value::Object(window))?;
set_slot(i, o, DEPTH, Value::Number(depth as f64))?;
set_open(i, o, "type", Value::str(kind))?;
set_open(i, o, "parent", parent)?;
set_open(i, o, "window", Value::Object(window))?;
let text = text_arg(i, text)?;
set_open(i, o, "text", text)?;
let props = match props {
Value::Object(_) => props.clone(),
_ => Value::Object(i.new_object()),
};
set_open(i, o, "properties", props.clone())?;
let name = i.get_value(&props, &key("name"))?;
let name = text_arg(i, &name)?;
set_open(i, o, "name", name)?;
set_open(i, o, "enabled", Value::Bool(true))?;
set_open(i, o, "visible", Value::Bool(true))?;
if !matches!(bounds, Value::Undefined | Value::Null) {
set_open(i, o, "bounds", bounds.clone())?;
}
set_open(i, o, "layout", Value::Object(layout))?;
for name in SIZES {
let d = dimension(i, -1.0, -1.0)?;
set_open(i, o, name, d)?;
}
let container = matches!(kind, "dialog" | "group" | "panel");
if container {
let kids = list(i, &[], false)?;
set_slot(i, o, KIDS, Value::Object(kids))?;
let children = list(i, &[], true)?;
set_open(i, o, "children", Value::Object(children))?;
set_open(i, o, "orientation", Value::str(if kind == "group" { "row" } else { "column" }))?;
set_open(i, o, "alignChildren", Value::str("center"))?;
set_open(i, o, "spacing", Value::Number(10.0))?;
set_open(i, o, "margins", Value::Number(if kind == "group" { 0.0 } else { 15.0 }))?;
}
// Layout properties given with the creation properties start the element with those values.
let layout_names: &[&str] = if container { &["alignment", "orientation", "alignChildren", "spacing", "margins"] } else { &["alignment"] };
for name in layout_names {
let v = i.get_value(&props, &key(name))?;
if !matches!(v, Value::Undefined) {
set_open(i, o, name, v)?;
}
}
Ok(o)
}
/// A new size object with `width` and `height`.
fn dimension(i: &mut Interp, width: f64, height: f64) -> JsResult<Value> {
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(2)))?;
let o = i.new_object();
i.rooted(&[Value::Object(o)], |i| {
set_open(i, o, "width", Value::Number(width))?;
set_open(i, o, "height", Value::Number(height))?;
Ok(Value::Object(o))
})
}
/// `new Window(type[, title, bounds, properties])`.
fn window_ctor(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let kind = i.to_str(&arg(args, 0))?.to_rust_string();
if kind.contains('{') {
return Err(error(i, "Window: resource specifications are not available yet; build the dialog with add()"));
}
let lower = kind.to_lowercase();
if lower == "palette" || lower == "window" {
return Err(error(i, &format!("Window: a window of type {lower} is not available yet; use dialog")));
}
if lower != "dialog" {
return Err(error(i, "Window: the type must be dialog, palette or window"));
}
let proto = classes(i)?.window_proto;
let mark = i.temp_roots.len();
let r = (|| {
let o = new_element(i, proto, "dialog", None, None, 0, [&arg(args, 2), &arg(args, 1), &arg(args, 3)])?;
set_slot(i, o, COUNT, Value::Number(0.0))?;
set_slot(i, o, ITEM_COUNT, Value::Number(0.0))?;
Ok(Value::Object(o))
})();
i.temp_roots.truncate(mark);
r
}
fn number_arg(i: &mut Interp, v: &Value, what: &str) -> JsResult<f64> {
let n = i.to_number(v)?;
if !n.is_finite() {
return Err(range(i, &format!("{what} is not a finite number")));
}
Ok(n)
}
/// `container.add(type[, bounds, text, properties])` and `list.add("item", text)`.
fn add(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (o, kind) = element(i, &this, "add")?;
window_of(i, o, "add")?;
let ty = i.to_str(&arg(args, 0))?.to_rust_string();
if kind == "dropdownlist" || kind == "listbox" {
if ty.to_lowercase() != "item" {
return Err(error(i, "add: a list holds only items"));
}
return add_item(i, o, &arg(args, 1));
}
if !matches!(kind.as_str(), "dialog" | "group" | "panel") {
return Err(error(i, &format!("add: a {kind} holds no controls")));
}
if ty.contains('{') {
return Err(error(i, "add: resource specifications are not available yet; add each control by its type"));
}
let ty = ty.to_lowercase();
if LATER.contains(&ty.as_str()) {
return Err(error(i, &format!("add: the control type {ty} is not available yet")));
}
let Some(&ty) = BUILT.iter().find(|b| **b == ty) else { return Err(error(i, &format!("add: there is no control type {ty}"))) };
let depth = number_slot(i, o, DEPTH)? as usize + 1;
if depth > MAX_DEPTH {
return Err(range(i, &format!("add: controls can be nested at most {MAX_DEPTH} deep")));
}
let win = window_of(i, o, "add")?;
let count = number_slot(i, win, COUNT)?;
if count as usize >= MAX_CONTROLS {
return Err(range(i, &format!("add: a dialog can hold at most {MAX_CONTROLS} controls")));
}
let list_kind = ty == "dropdownlist" || ty == "listbox";
let third = arg(args, 2);
let is_array = |i: &Interp, v: &Value| -> JsResult<bool> {
Ok(match v.as_object() {
Some(a) => matches!(i.object(a)?.class, Class::Array { .. }),
None => false,
})
};
// A group takes its creation properties third, and so may a list in place of its items; when
// the third argument is not an object, they are the fourth.
let props_third = third.as_object().is_some() && (ty == "group" || (list_kind && !is_array(i, &third)?));
let props = if props_third { third.clone() } else { arg(args, 3) };
let proto = classes(i)?.control_proto;
let mark = i.temp_roots.len();
let r = (|| {
let mut given_items = if list_kind && !props_third { third.clone() } else { Value::Undefined };
if list_kind && props.as_object().is_some() {
let listed = i.get_value(&props, &key("items"))?;
i.temp_roots.push(listed.clone());
if !matches!(listed, Value::Undefined | Value::Null) {
if !matches!(given_items, Value::Undefined | Value::Null) {
return Err(i.type_error("add: give a list's items as the third argument or in properties.items, not both"));
}
given_items = listed;
}
}
if ty == "listbox" && props.as_object().is_some() && i.get_value(&props, &key("multiselect"))?.truthy() {
return Err(error(i, "add: a list box that selects several items is not available yet"));
}
let creation = if matches!(ty, "slider" | "progressbar") { Value::Undefined } else { props.clone() };
let text = if list_kind || matches!(ty, "group" | "slider" | "progressbar") { Value::Undefined } else { third.clone() };
let c = new_element(i, proto, ty, Some(o), Some(win), depth, [&arg(args, 1), &text, &creation])?;
match ty {
"checkbox" => set_open(i, c, "value", Value::Bool(false))?,
"radiobutton" => {
set_slot(i, c, VALUE, Value::Bool(false))?;
let cl = classes(i)?;
i.define(c, key("value"), Prop::Accessor { get: Some(cl.radio_get), set: Some(cl.radio_set), flags: ENUMERABLE })?;
}
"slider" | "progressbar" => {
let (value, min, max) =
if ty == "slider" { (arg(args, 2), arg(args, 3), arg(args, 4)) } else { (arg(args, 2), Value::Undefined, arg(args, 3)) };
let given = |v: &Value| !matches!(v, Value::Undefined);
let value = if given(&value) { number_arg(i, &value, "add: the value")? } else { 0.0 };
let min = if given(&min) { number_arg(i, &min, "add: the minimum value")? } else { 0.0 };
let max = if given(&max) { number_arg(i, &max, "add: the maximum value")? } else { 100.0 };
set_open(i, c, "minvalue", Value::Number(min))?;
set_open(i, c, "maxvalue", Value::Number(max))?;
set_open(i, c, "value", Value::Number(value))?;
}
"dropdownlist" | "listbox" => {
let kept = list(i, &[], false)?;
set_slot(i, c, ITEMS, Value::Object(kept))?;
set_slot(i, c, SELECTION, Value::Number(-1.0))?;
let items = list(i, &[], true)?;
set_open(i, c, "items", Value::Object(items))?;
let cl = classes(i)?;
i.define(c, key("selection"), Prop::Accessor { get: Some(cl.selection_get), set: Some(cl.selection_set), flags: ENUMERABLE })?;
}
_ => {}
}
// A new list's items take their places in the dialog only once the list is added.
let mut staged = 0usize;
if list_kind {
match given_items.clone() {
Value::Undefined | Value::Null => {}
Value::Object(a) if matches!(i.object(a)?.class, Class::Array { .. }) => {
let length = match &i.object(a)?.class {
Class::Array { length, .. } => *length as usize,
_ => 0,
};
if length > MAX_ITEMS.saturating_sub(number_slot(i, win, ITEM_COUNT)? as usize) {
return Err(range(i, &format!("add: a dialog's lists hold at most {MAX_ITEMS} items in all")));
}
let values = list_values(i, a)?;
i.temp_roots.extend(values.iter().cloned());
for item in &values {
let text = text_arg(i, item)?;
new_item(i, c, text)?;
staged += 1;
}
}
_ => return Err(i.type_error("add: give a list's items as an array")),
}
}
// A getter or a conversion above may have added controls or items itself, or removed the
// container from its dialog.
window_of(i, o, "add")?;
let count = number_slot(i, win, COUNT)?;
if count as usize >= MAX_CONTROLS {
return Err(range(i, &format!("add: a dialog can hold at most {MAX_CONTROLS} controls")));
}
let items = number_slot(i, win, ITEM_COUNT)?;
if items as usize + staged > MAX_ITEMS {
return Err(range(i, &format!("add: a dialog's lists hold at most {MAX_ITEMS} items in all")));
}
append(i, o, KIDS, "children", Value::Object(c))?;
set_slot(i, win, COUNT, Value::Number(count + 1.0))?;
set_slot(i, win, ITEM_COUNT, Value::Number(items + staged as f64))?;
Ok(Value::Object(c))
})();
i.temp_roots.truncate(mark);
r
}
/// Adds an item with `text` to the list `o`, which the dialog holds.
fn add_item(i: &mut Interp, o: ObjId, text: &Value) -> JsResult<Value> {
// The conversion runs first: it may add or remove items itself.
let text = text_arg(i, text)?;
i.rooted(std::slice::from_ref(&text), |i| {
let win = window_of(i, o, "add")?;
let count = number_slot(i, win, ITEM_COUNT)?;
if count as usize >= MAX_ITEMS {
return Err(range(i, &format!("add: a dialog's lists hold at most {MAX_ITEMS} items in all")));
}
let item = new_item(i, o, text.clone())?;
i.rooted(std::slice::from_ref(&item), |i| set_slot(i, win, ITEM_COUNT, Value::Number(count + 1.0)))?;
Ok(item)
})
}
/// Adds an item with `text` to the end of the list `o`, without counting it in the dialog's items.
fn new_item(i: &mut Interp, o: ObjId, text: Value) -> JsResult<Value> {
let proto = classes(i)?.item_proto;
i.rooted(std::slice::from_ref(&text), |i| {
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(4)))?;
let item = i.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
i.rooted(&[Value::Object(item)], |i| {
set_slot(i, item, LIST, Value::Object(o))?;
set_open(i, item, "text", text.clone())?;
append(i, o, ITEMS, "items", Value::Object(item))?;
Ok(Value::Object(item))
})
})
}
fn list_of(i: &mut Interp, this: &Value, what: &str) -> JsResult<ObjId> {
match kind_of(i, this)? {
Some((o, kind)) if kind == "dropdownlist" || kind == "listbox" => Ok(o),
_ => Err(i.type_error(&format!("{what}: this is not a list"))),
}
}
fn items_of(i: &Interp, o: ObjId) -> JsResult<Vec<Value>> {
match object_slot(i, o, ITEMS)? {
Some(a) => list_values(i, a),
None => Ok(Vec::new()),
}
}
fn selection_index(i: &Interp, o: ObjId) -> JsResult<Option<usize>> {
let n = number_slot(i, o, SELECTION)?;
Ok((n >= 0.0).then_some(n as usize))
}
fn selection_get(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let o = list_of(i, &this, "selection")?;
let items = items_of(i, o)?;
Ok(selection_index(i, o)?.and_then(|n| items.get(n).cloned()).unwrap_or(Value::Null))
}
fn selection_set(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = list_of(i, &this, "selection")?;
let items = items_of(i, o)?;
let v = arg(args, 0);
let n = match &v {
Value::Undefined | Value::Null => -1.0,
Value::Number(n) if n.fract() == 0.0 && *n >= 0.0 && (*n as usize) < items.len() => *n,
Value::Number(n) => return Err(range(i, &format!("selection: the list has no item {n}"))),
v => match items.iter().position(|item| same(item, v)) {
Some(n) => n as f64,
None => return Err(i.type_error("selection: give an item of this list, its index or null")),
},
};
set_slot(i, o, SELECTION, Value::Number(n))?;
Ok(Value::Undefined)
}
/// The list an item belongs to and its index there.
fn item_place(i: &mut Interp, this: &Value, what: &str) -> JsResult<(ObjId, Option<usize>)> {
let list = match this.as_object() {
Some(o) => object_slot(i, o, LIST)?,
None => None,
};
let Some(list) = list else { return Err(i.type_error(&format!("{what}: this is not a list item"))) };
let index = items_of(i, list)?.iter().position(|item| same(item, this));
Ok((list, index))
}
fn item_index(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let (_, index) = item_place(i, &this, "index")?;
Ok(Value::Number(index.map_or(-1.0, |n| n as f64)))
}
fn item_selected_get(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let (list, index) = item_place(i, &this, "selected")?;
Ok(Value::Bool(index.is_some() && selection_index(i, list)? == index))
}
fn item_selected_set(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (list, index) = item_place(i, &this, "selected")?;
let Some(index) = index else { return Ok(Value::Undefined) };
if arg(args, 0).truthy() {
set_slot(i, list, SELECTION, Value::Number(index as f64))?;
} else if selection_index(i, list)? == Some(index) {
set_slot(i, list, SELECTION, Value::Number(-1.0))?;
}
Ok(Value::Undefined)
}
fn item_to_string(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
item_place(i, &this, "toString")?;
let text = i.get_value(&this, &key("text"))?;
Ok(Value::String(i.to_str(&text)?))
}
/// `list.removeAll()`.
fn remove_all(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let o = list_of(i, &this, "removeAll")?;
let n = items_of(i, o)?.len() as f64;
let win = window_of(i, o, "removeAll")?;
let count = number_slot(i, win, ITEM_COUNT)?;
set_slot(i, win, ITEM_COUNT, Value::Number((count - n).max(0.0)))?;
let kept = list(i, &[], false)?;
set_slot(i, o, ITEMS, Value::Object(kept))?;
let items = list(i, &[], true)?;
set_open(i, o, "items", Value::Object(items))?;
set_slot(i, o, SELECTION, Value::Number(-1.0))?;
Ok(Value::Undefined)
}
fn kids_of(i: &Interp, o: ObjId) -> JsResult<Vec<Value>> {
match object_slot(i, o, KIDS)? {
Some(a) => list_values(i, a),
None => Ok(Vec::new()),
}
}
/// `findElement(name)`: the first control in the element, in order, whose `name` is `name`.
fn find_element(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (o, _) = element(i, &this, "findElement")?;
let want = i.to_str(&arg(args, 0))?;
let mut stack: Vec<Value> = kids_of(i, o)?.into_iter().rev().collect();
while let Some(v) = stack.pop() {
i.use_fuel()?;
let Some(c) = v.as_object() else { continue };
let name = i.get_value(&v, &key("name"))?;
if matches!(&name, Value::String(s) if *s == want) {
return Ok(v);
}
stack.extend(kids_of(i, c)?.into_iter().rev());
}
Ok(Value::Null)
}
/// Calls the element's handler `name` when it has one. → its result, or `None` without one.
fn handler(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Option<Value>> {
let f = i.get_value(&Value::Object(o), &key(name))?;
if !i.is_callable(&f) {
return Ok(None);
}
i.call(&f, &Value::Object(o), &[]).map(Some)
}
/// The radio buttons that form a set with `c`: it and the radio buttons next to it in its
/// container, up to the first control of another kind on each side.
fn radio_set_of(i: &Interp, c: ObjId) -> JsResult<Vec<ObjId>> {
let Some(parent) = object_slot(i, c, PARENT)? else { return Ok(vec![c]) };
let kids = kids_of(i, parent)?;
let Some(at) = kids.iter().position(|k| same(k, &Value::Object(c))) else { return Ok(vec![c]) };
let radio = |v: &Value| -> JsResult<Option<ObjId>> {
Ok(match kind_of(i, v)? {
Some((o, k)) if k == "radiobutton" => Some(o),
_ => None,
})
};
let mut set = vec![c];
for k in kids[..at].iter().rev() {
let Some(o) = radio(k)? else { break };
set.push(o);
}
for k in &kids[at + 1..] {
let Some(o) = radio(k)? else { break };
set.push(o);
}
Ok(set)
}
fn radio_get(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let (o, _) = element(i, &this, "value")?;
Ok(Value::Bool(slot(i, o, VALUE)?.truthy()))
}
/// A radio button's `value`: true turns the other radio buttons of its set off.
fn radio_set(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (o, _) = element(i, &this, "value")?;
let on = arg(args, 0).truthy();
if on {
for other in radio_set_of(i, o)? {
if other != o {
set_slot(i, other, VALUE, Value::Bool(false))?;
}
}
}
set_slot(i, o, VALUE, Value::Bool(on))?;
Ok(Value::Undefined)
}
/// The listeners registered on `o`: each event type, handler and capture flag.
fn listeners_of(i: &Interp, o: ObjId) -> JsResult<Vec<(Value, Value, bool)>> {
let flat = match object_slot(i, o, LISTENERS)? {
Some(a) => list_values(i, a)?,
None => Vec::new(),
};
Ok(flat.as_chunks::<3>().0.iter().map(|[t, f, c]| (t.clone(), f.clone(), c.truthy())).collect())
}
fn set_listeners(i: &mut Interp, o: ObjId, all: &[(Value, Value, bool)]) -> JsResult<()> {
let flat: Vec<Value> = all.iter().flat_map(|(t, f, c)| [t.clone(), f.clone(), Value::Bool(*c)]).collect();
i.rooted(&flat.clone(), |i| {
let kept = list(i, &flat, false)?;
set_slot(i, o, LISTENERS, Value::Object(kept))
})
}
/// Is the stored event type `t` the type `ty`?
fn is_type(t: &Value, ty: &str) -> bool {
matches!(t, Value::String(s) if s.to_rust_string() == ty)
}
/// The event type `addEventListener` and `removeEventListener` were given, checked.
fn event_type(i: &mut Interp, v: &Value, what: &str) -> JsResult<&'static str> {
let text = i.to_str(v)?.to_rust_string();
if let Some(name) = EVENTS.iter().find(|e| **e == text) {
return Ok(name);
}
if EVENTS_LATER.contains(&text.as_str()) {
return Err(error(i, &format!("{what}: the event {text} is not available yet")));
}
Err(error(i, &format!("{what}: there is no event {}", text.chars().take(64).collect::<String>())))
}
/// `addEventListener(type, handler[, capture])`.
fn add_event_listener(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (o, kind) = element(i, &this, "addEventListener")?;
let ty = event_type(i, &arg(args, 0), "addEventListener")?;
if ty == "show" && kind != "dialog" {
return Err(error(i, "addEventListener: the event show runs only on a dialog"));
}
let f = arg(args, 1);
if !i.is_callable(&f) {
return Err(i.type_error("addEventListener: give the handler as a function"));
}
let capture = arg(args, 2).truthy();
let mut all = listeners_of(i, o)?;
if !all.iter().any(|(t, g, c)| is_type(t, ty) && same(g, &f) && *c == capture) {
if all.len() >= MAX_LISTENERS {
return Err(range(i, &format!("addEventListener: a window or control takes at most {MAX_LISTENERS} listeners")));
}
all.push((Value::str(ty), f, capture));
set_listeners(i, o, &all)?;
}
Ok(Value::Undefined)
}
/// `removeEventListener(type, handler[, capture])`.
fn remove_event_listener(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (o, _) = element(i, &this, "removeEventListener")?;
let ty = event_type(i, &arg(args, 0), "removeEventListener")?;
let f = arg(args, 1);
let capture = arg(args, 2).truthy();
let mut all = listeners_of(i, o)?;
let before = all.len();
all.retain(|(t, g, c)| !(is_type(t, ty) && same(g, &f) && *c == capture));
if all.len() != before {
set_listeners(i, o, &all)?;
}
Ok(Value::Undefined)
}
/// `event.stopPropagation()`: stops an event whose listeners are running; it does nothing to any
/// other object.
fn event_stop(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
if let Some(o) = this.as_object()
&& classes(i)?.dispatching.contains(&o)
{
set_slot(i, o, STOPPED, Value::Bool(true))?;
}
Ok(Value::Undefined)
}
/// Runs the listeners for `ty` on `at` (only those for `capture` unless it is `None`), with `event`.
fn run_listeners(i: &mut Interp, at: ObjId, ty: &str, capture: Option<bool>, event: ObjId, phase: f64) -> JsResult<bool> {
let wanted: Vec<Value> =
listeners_of(i, at)?.into_iter().filter(|(t, _, c)| is_type(t, ty) && capture.is_none_or(|want| *c == want)).map(|(_, f, _)| f).collect();
if wanted.is_empty() {
return Ok(false);
}
i.rooted(&wanted.clone(), |i| {
set_open(i, event, "currentTarget", Value::Object(at))?;
set_open(i, event, "eventPhase", Value::Number(phase))?;
for f in &wanted {
i.call(f, &Value::Object(at), &[Value::Object(event)])?;
}
Ok(true)
})
}
/// Runs the event `ty` on the element `target`: the listeners of the containers above it that
/// capture, its `callback`, its own listeners, then the listeners of the containers that do not
/// capture, until one stops it. → whether the event was handled: the element had a callback or a
/// listener for it, or a capturing listener above it stopped it. Listeners of the containers that
/// do not capture do not count.
fn dispatch(i: &mut Interp, target: ObjId, ty: &str, callback: &str) -> JsResult<bool> {
let mut above = Vec::new();
let mut at = object_slot(i, target, PARENT)?;
while let Some(p) = at {
i.use_fuel()?;
above.push(p);
at = object_slot(i, p, PARENT)?;
}
let proto = classes(i)?.event_proto;
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(10)))?;
let event = i.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
if let Some(c) = i.script_ui.as_mut() {
c.dispatching.push(event);
}
let r = run_event(i, event, target, &above, ty, callback);
if let Some(c) = i.script_ui.as_mut()
&& let Some(n) = c.dispatching.iter().rposition(|e| *e == event)
{
c.dispatching.remove(n);
}
r
}
/// The phases of [`dispatch`] for the event object `event`.
fn run_event(i: &mut Interp, event: ObjId, target: ObjId, above: &[ObjId], ty: &str, callback: &str) -> JsResult<bool> {
i.rooted(&[Value::Object(event)], |i| {
let bubbles = ty != "show";
set_open(i, event, "type", Value::str(ty))?;
set_open(i, event, "target", Value::Object(target))?;
set_open(i, event, "bubbles", Value::Bool(bubbles))?;
set_open(i, event, "cancelable", Value::Bool(false))?;
set_open(i, event, "detail", Value::Number(if ty == "click" { 1.0 } else { 0.0 }))?;
let stopped = |i: &Interp| -> JsResult<bool> { Ok(slot(i, event, STOPPED)?.truthy()) };
for p in above.iter().rev() {
run_listeners(i, *p, ty, Some(true), event, 1.0)?;
if stopped(i)? {
return Ok(true);
}
}
let had = handler(i, target, callback)?.is_some();
let heard = run_listeners(i, target, ty, None, event, 2.0)?;
if bubbles && !stopped(i)? {
for p in above {
run_listeners(i, *p, ty, Some(false), event, 3.0)?;
if stopped(i)? {
break;
}
}
}
Ok(had || heard)
})
}
/// A click on the control `c` of `kind`: a check box turns over and a radio button turns on, then
/// the click runs. → whether the click was handled, as [`dispatch`] says.
fn click_control(i: &mut Interp, c: ObjId, kind: &str) -> JsResult<bool> {
if kind == "checkbox" {
let on = i.get_value(&Value::Object(c), &key("value"))?.truthy();
i.put(c, key("value"), Value::Bool(!on), true)?;
} else if kind == "radiobutton" {
i.put(c, key("value"), Value::Bool(true), true)?;
}
dispatch(i, c, "click", "onClick")
}
/// `notify([event])`: simulates the control's `onClick`, `onChange` or `onChanging`.
fn notify(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (o, kind) = element(i, &this, "notify")?;
let event = match arg(args, 0) {
Value::Undefined if matches!(kind.as_str(), "button" | "checkbox" | "radiobutton") => "onClick".to_string(),
Value::Undefined => "onChange".to_string(),
v => i.to_str(&v)?.to_rust_string(),
};
match event.as_str() {
"onClick" => click_control(i, o, &kind)?,
"onChange" => dispatch(i, o, "change", "onChange")?,
"onChanging" => dispatch(i, o, "changing", "onChanging")?,
_ => return Err(error(i, "notify: give onClick, onChange or onChanging")),
};
Ok(Value::Undefined)
}
/// The index in `values` that `what` names: an index, one of the values, or a text that an
/// element's `text` equals. A text is converted before the values are read.
fn index_named(i: &mut Interp, what: &Value, values: impl Fn(&Interp) -> JsResult<Vec<Value>>) -> JsResult<(Vec<Value>, Option<usize>)> {
match what {
Value::Number(n) => {
let all = values(i)?;
let at = (*n >= 0.0 && n.fract() == 0.0 && (*n as usize) < all.len()).then_some(*n as usize);
Ok((all, at))
}
Value::Object(_) => {
let all = values(i)?;
let at = all.iter().position(|v| same(v, what));
Ok((all, at))
}
v => {
let want = i.to_str(v)?;
let all = values(i)?;
i.rooted(&all.clone(), |i| {
for (n, item) in all.iter().enumerate() {
i.use_fuel()?;
let t = i.get_value(item, &key("text"))?;
if matches!(&t, Value::String(s) if *s == want) {
return Ok((all.clone(), Some(n)));
}
}
Ok((all.clone(), None))
})
}
}
}
/// `list.find(text)`: the first item whose text is `text`, or `null`.
fn find(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = list_of(i, &this, "find")?;
let text = Value::String(i.to_str(&arg(args, 0))?);
let (items, at) = index_named(i, &text, |i| items_of(i, o))?;
Ok(at.and_then(|n| items.get(n).cloned()).unwrap_or(Value::Null))
}
/// The controls in `c` and in the containers it holds, `c` included, and their list items.
fn subtree_counts(i: &mut Interp, c: &Value) -> JsResult<(f64, f64)> {
let (mut controls, mut items) = (0.0, 0.0);
let mut stack = vec![c.clone()];
while let Some(v) = stack.pop() {
i.use_fuel()?;
let Some(o) = v.as_object() else { continue };
controls += 1.0;
items += items_of(i, o)?.len() as f64;
stack.extend(kids_of(i, o)?);
}
Ok((controls, items))
}
/// `remove(what)`: removes a list's item, or a container's control, given by its index, the
/// object itself or its text. Nothing happens when there is no such item or control.
fn remove(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let (o, kind) = element(i, &this, "remove")?;
let what = arg(args, 0);
window_of(i, o, "remove")?;
if kind == "dropdownlist" || kind == "listbox" {
let before = slot(i, o, ITEMS)?;
let (mut items, at) = i.rooted(std::slice::from_ref(&before), |i| index_named(i, &what, |i| items_of(i, o)))?;
// A `text` getter read during the lookup may have changed the list or removed it.
if !same(&slot(i, o, ITEMS)?, &before) {
return Err(error(i, "remove: the list changed while the item was looked up"));
}
let win = window_of(i, o, "remove")?;
let Some(at) = at else { return Ok(Value::Undefined) };
items.remove(at);
i.rooted(&items.clone(), |i| {
let kept = list(i, &items, false)?;
set_slot(i, o, ITEMS, Value::Object(kept))?;
let copy = list(i, &items, true)?;
set_open(i, o, "items", Value::Object(copy))
})?;
let count = number_slot(i, win, ITEM_COUNT)?;
set_slot(i, win, ITEM_COUNT, Value::Number((count - 1.0).max(0.0)))?;
match selection_index(i, o)? {
Some(n) if n == at => set_slot(i, o, SELECTION, Value::Number(-1.0))?,
Some(n) if n > at => set_slot(i, o, SELECTION, Value::Number((n - 1) as f64))?,
_ => {}
}
return Ok(Value::Undefined);
}
if !matches!(kind.as_str(), "dialog" | "group" | "panel") {
return Err(error(i, &format!("remove: a {kind} holds no controls or items")));
}
let before = slot(i, o, KIDS)?;
let (mut kids, at) = i.rooted(std::slice::from_ref(&before), |i| index_named(i, &what, |i| kids_of(i, o)))?;
// A `text` getter read during the lookup may have changed the container or removed it.
if !same(&slot(i, o, KIDS)?, &before) {
return Err(error(i, "remove: the container's controls changed while the control was looked up"));
}
let win = window_of(i, o, "remove")?;
let Some(at) = at else { return Ok(Value::Undefined) };
let gone = kids.remove(at);
i.rooted(std::slice::from_ref(&gone), |i| {
i.rooted(&kids.clone(), |i| {
let kept = list(i, &kids, false)?;
set_slot(i, o, KIDS, Value::Object(kept))?;
let copy = list(i, &kids, true)?;
set_open(i, o, "children", Value::Object(copy))
})?;
let (controls, items) = subtree_counts(i, &gone)?;
let count = number_slot(i, win, COUNT)?;
set_slot(i, win, COUNT, Value::Number((count - controls).max(0.0)))?;
let total = number_slot(i, win, ITEM_COUNT)?;
set_slot(i, win, ITEM_COUNT, Value::Number((total - items).max(0.0)))?;
detach(i, &gone)
})?;
Ok(Value::Undefined)
}
/// Takes the control `c`, removed from its container, out of the dialog: it has no container,
/// and neither it nor the controls it holds belong to the dialog any longer.
fn detach(i: &mut Interp, c: &Value) -> JsResult<()> {
let Some(root) = c.as_object() else { return Ok(()) };
set_slot(i, root, PARENT, Value::Null)?;
set_open(i, root, "parent", Value::Null)?;
let mut stack = vec![c.clone()];
while let Some(v) = stack.pop() {
i.use_fuel()?;
let Some(o) = v.as_object() else { continue };
set_slot(i, o, WINDOW, Value::Null)?;
stack.extend(kids_of(i, o)?);
}
Ok(())
}
fn control_show(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let (o, _) = element(i, &this, "show")?;
i.put(o, key("visible"), Value::Bool(true), true)?;
Ok(Value::Undefined)
}
fn control_hide(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let (o, _) = element(i, &this, "hide")?;
i.put(o, key("visible"), Value::Bool(false), true)?;
Ok(Value::Undefined)
}
fn nothing(_: &mut Interp, _: Value, _: &[Value], _: bool) -> JsResult<Value> {
Ok(Value::Undefined)
}
fn dialog_of(i: &mut Interp, this: &Value, what: &str) -> JsResult<ObjId> {
match kind_of(i, this)? {
Some((o, kind)) if kind == "dialog" => Ok(o),
_ => Err(i.type_error(&format!("{what}: this is not a ScriptUI window"))),
}
}
/// `close([result])`: closes the dialog that is showing once the running handler returns.
fn close(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = dialog_of(i, &this, "close")?;
let result = match arg(args, 0) {
Value::Undefined => 0.0,
v => number_arg(i, &v, "close: the result")?,
};
if classes(i)?.showing == Some(o) {
set_slot(i, o, CLOSING, Value::Number(result))?;
}
Ok(Value::Undefined)
}
/// `hide()`: closes the dialog that is showing with the result 0.
fn hide(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
close(i, this, &[], false)
}
fn host_fn(i: &mut Interp, name: &str) -> JsResult<Value> {
let host = classes(i)?.host;
i.get_value(&Value::Object(host), &key(name))
}
fn set_showing(i: &mut Interp, showing: Option<ObjId>) {
if let Some(c) = i.script_ui.as_mut() {
c.showing = showing;
}
}
/// `show()` on a dialog: gives its description to the host's `open`, then runs it ([`opened`]).
fn show(i: &mut Interp, this: Value, _: &[Value]) -> JsResult<Pause> {
let win = dialog_of(i, &this, "show")?;
match classes(i)?.showing {
Some(o) if o == win => return Err(error(i, "show: this dialog is already showing")),
Some(_) => return Err(error(i, "show: a dialog cannot open while another dialog is showing")),
None => {}
}
set_showing(i, Some(win));
set_slot(i, win, CLOSING, Value::Undefined)?;
let described = describe(i, win);
let (desc, nodes, def, cancel, lists) = match described {
Ok(d) => d,
Err(e) => {
set_showing(i, None);
return Err(e);
}
};
let open = host_fn(i, "open")?;
Ok(Pause {
host: open,
args: vec![desc],
keep: vec![Value::Object(win), Value::Object(nodes), Value::Number(def), Value::Number(cancel), Value::Object(lists)],
resume: opened,
failed: Some(failed),
})
}
/// A host call of `show()` failed, or a handler threw: the dialog is no longer showing.
fn failed(i: &mut Interp, keep: &[Value]) {
let Some(win) = arg(keep, 0).as_object() else { return };
if classes(i).ok().and_then(|c| c.showing) != Some(win) {
return;
}
set_showing(i, None);
if let Ok(end) = host_fn(i, "end") {
let _ = i.call(&end, &Value::Undefined, &[Value::Number(0.0), Value::Bool(false)]);
}
}
/// `show()` ends with `result`.
fn finish(i: &mut Interp, win: ObjId, result: f64) -> JsResult<Value> {
set_showing(i, None);
set_slot(i, win, CLOSING, Value::Undefined)?;
let end = host_fn(i, "end")?;
i.call(&end, &Value::Undefined, &[Value::Number(result), Value::Bool(true)])?;
Ok(Value::Number(result))
}
/// Runs `onClose`. → false when it returned `false`.
fn may_close(i: &mut Interp, win: ObjId) -> JsResult<bool> {
Ok(!matches!(handler(i, win, "onClose")?, Some(Value::Bool(false))))
}
/// Closes the dialog when a handler asked to, or asks the host's `wait` what happens next.
fn next(i: &mut Interp, win: ObjId) -> JsResult<Value> {
loop {
i.use_fuel()?;
if let Value::Number(result) = slot(i, win, CLOSING)? {
set_slot(i, win, CLOSING, Value::Undefined)?;
if may_close(i, win)? {
return finish(i, win, result);
}
continue;
}
let (desc, nodes, def, cancel, lists) = describe(i, win)?;
let wait = host_fn(i, "wait")?;
i.again = Some(Pause {
host: wait,
args: vec![desc],
keep: vec![Value::Object(win), Value::Object(nodes), Value::Number(def), Value::Number(cancel), Value::Object(lists)],
resume: answered,
failed: Some(failed),
});
return Ok(Value::Undefined);
}
}
/// Runs `r`; when it fails, the dialog is no longer showing.
fn guarded(i: &mut Interp, keep: &[Value], r: impl FnOnce(&mut Interp) -> JsResult<Value>) -> JsResult<Value> {
let v = r(i);
if v.is_err() {
failed(i, keep);
}
v
}
fn opened(i: &mut Interp, keep: &[Value], _: Value) -> JsResult<Value> {
guarded(i, keep, |i| {
let Some(win) = arg(keep, 0).as_object() else { return Err(Abrupt::Internal("show lost its dialog".into())) };
dispatch(i, win, "show", "onShow")?;
next(i, win)
})
}
/// What the host's `wait` gave: the event, which the dialog takes, and what happens next.
fn answered(i: &mut Interp, keep: &[Value], answer: Value) -> JsResult<Value> {
guarded(i, keep, |i| {
let (Some(win), Some(nodes)) = (arg(keep, 0).as_object(), arg(keep, 1).as_object()) else {
return Err(Abrupt::Internal("show lost its dialog".into()));
};
let Some(ev) = answer.as_object() else { return Err(Abrupt::Internal("the host gave show no event".into())) };
let event = match slot(i, ev, "event")? {
Value::String(s) => s.to_rust_string(),
_ => String::new(),
};
if event == "close" {
if may_close(i, win)? {
return finish(i, win, 0.0);
}
return next(i, win);
}
let id = match slot(i, ev, "id")? {
Value::Number(n) if n >= 0.0 && n.fract() == 0.0 => n as usize,
_ => return Err(Abrupt::Internal("the host gave show an event without a control".into())),
};
let nodes = list_values(i, nodes)?;
let Some((c, kind)) = nodes.get(id).map(|v| kind_of(i, v)).transpose()?.flatten() else {
return Err(Abrupt::Internal("the host gave show an event for a control the dialog does not hold".into()));
};
let value = slot(i, ev, "value")?;
match event.as_str() {
"click" => {
if !click_control(i, c, &kind)? && kind == "button" {
let is = |k: usize| matches!(arg(keep, k), Value::Number(n) if n == id as f64);
if is(2) {
set_slot(i, win, CLOSING, Value::Number(1.0))?;
} else if is(3) {
set_slot(i, win, CLOSING, Value::Number(2.0))?;
}
}
}
"change" => {
match kind.as_str() {
"edittext" => {
i.put(c, key("text"), value, true)?;
dispatch(i, c, "changing", "onChanging")?;
}
"slider" => {
i.put(c, key("value"), value, true)?;
dispatch(i, c, "changing", "onChanging")?;
}
_ => {
let n = match value {
Value::Number(n) => n,
_ => -1.0,
};
// The index is into the items the person saw.
let shown = match arg(keep, 4).as_object() {
Some(lists) => list_values(i, lists)?.get(id).cloned().unwrap_or(Value::Undefined),
None => Value::Undefined,
};
if !same(&slot(i, c, ITEMS)?, &shown) {
return Err(error(i, "show: a list's items changed before the person's choice was taken"));
}
set_slot(i, c, SELECTION, Value::Number(n))?;
}
}
dispatch(i, c, "change", "onChange")?;
}
_ => return Err(Abrupt::Internal(format!("the host gave show the event {event}"))),
}
next(i, win)
})
}
fn text_of(i: &mut Interp, v: &Value, what: &str) -> JsResult<String> {
let s = i.to_str(v)?;
let text = if s.len() > MAX_TEXT_BYTES { None } else { Some(s.to_rust_string()) };
match text {
Some(t) if t.len() <= MAX_TEXT_BYTES => Ok(t),
_ => Err(range(i, &format!("show: {what} is longer than 16 KiB"))),
}
}
fn get(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Value> {
i.get_value(&Value::Object(o), &key(name))
}
/// [`get`], keeping the value in the roots [`describe`] lets go of when it returns.
fn get_kept(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Value> {
let v = get(i, o, name)?;
i.temp_roots.push(v.clone());
Ok(v)
}
fn number_of(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Data> {
let v = get(i, o, name)?;
Ok(Data::Number(number_arg(i, &v, &format!("show: a control's {name}"))?))
}
/// An alignment: a name, or an array of a horizontal and a vertical name; `null` when not set.
fn alignment(i: &mut Interp, v: &Value) -> JsResult<Data> {
match v {
Value::Undefined | Value::Null => Ok(Data::Null),
Value::Object(a) if matches!(i.object(*a)?.class, Class::Array { .. }) => {
let mut out = Vec::new();
for n in 0..2 {
let part = i.get_value(v, &index_key(n))?;
out.push(Data::String(text_of(i, &part, "an alignment")?.to_lowercase()));
}
Ok(Data::Array(out))
}
v => Ok(Data::String(text_of(i, v, "an alignment")?.to_lowercase())),
}
}
/// Numbers read from `v`'s indices, or else from its properties `names`.
fn numbers(i: &mut Interp, v: &Value, names: &[&str], what: &str) -> JsResult<Data> {
let Some(o) = v.as_object() else {
if matches!(v, Value::Undefined | Value::Null) {
return Ok(Data::Null);
}
return Err(i.type_error(&format!("show: give {what} as an array or an object")));
};
let array = matches!(i.object(o)?.class, Class::Array { .. });
let mut out = Vec::new();
for (n, name) in names.iter().enumerate() {
let part = if array { i.get_value(v, &index_key(n as u32))? } else { i.get_value(v, &key(name))? };
out.push(Data::Number(number_arg(i, &part, &format!("show: {what}"))?));
}
Ok(Data::Array(out))
}
/// Bounds as `[left, top, right, bottom]`: from an array, from `x`, `y`, `width` and `height`, or
/// from `left`, `top`, `right` and `bottom`.
fn bounds(i: &mut Interp, v: &Value) -> JsResult<Data> {
if let Some(o) = v.as_object()
&& !matches!(i.object(o)?.class, Class::Array { .. })
&& !matches!(i.get_value(v, &key("width"))?, Value::Undefined)
{
let Data::Array(xywh) = numbers(i, v, &["x", "y", "width", "height"], "bounds")? else { return Ok(Data::Null) };
let n = |k: usize| match xywh.get(k) {
Some(Data::Number(n)) => *n,
_ => 0.0,
};
return Ok(Data::Array(vec![Data::Number(n(0)), Data::Number(n(1)), Data::Number(n(0) + n(2)), Data::Number(n(1) + n(3))]));
}
numbers(i, v, &["left", "top", "right", "bottom"], "bounds")
}
/// `base`'s width and height, each replaced by `over`'s when that is above 0.
fn overridden(base: Data, over: Data) -> Data {
let Data::Array(over) = over else { return base };
let mut out = match base {
Data::Array(b) => b,
_ => vec![Data::Number(-1.0), Data::Number(-1.0)],
};
for (o, v) in out.iter_mut().zip(over) {
if matches!(v, Data::Number(n) if n > 0.0) {
*o = v;
}
}
Data::Array(out)
}
fn margins(i: &mut Interp, v: &Value) -> JsResult<Data> {
if v.as_object().is_some() {
return numbers(i, v, &["left", "top", "right", "bottom"], "margins");
}
let m = number_arg(i, v, "show: margins")?;
Ok(Data::Array(vec![Data::Number(m); 4]))
}
/// The dialog's description: its elements in order, each after the container that holds it, as
/// [`Data`] for the host's `open` and `wait`, with the elements in that order, and the indices of
/// its default and cancel elements (-1 for none), and each element's array of list items as it
/// was described (`undefined` for an element that is not a list). When a getter changes a list
/// after it was read, the dialog is read again once; a list that changes again throws an `Error`.
fn describe(i: &mut Interp, win: ObjId) -> JsResult<Described> {
for _ in 0..2 {
let mark = i.temp_roots.len();
let r = describe_rooted(i, win);
i.temp_roots.truncate(mark);
if let Some(d) = r? {
return Ok(d);
}
}
Err(error(i, "show: a list's items changed while the dialog was read"))
}
/// A description, the elements in its order, the default and cancel elements, and the lists' items.
type Described = (Value, ObjId, f64, f64, ObjId);
/// Has each list in `order` the array of items it had when it was described?
fn lists_unchanged(i: &Interp, order: &[(ObjId, String, f64)], snapshot: &[Value]) -> JsResult<bool> {
for ((o, ..), kept) in order.iter().zip(snapshot) {
if !matches!(kept, Value::Undefined) && !same(&slot(i, *o, ITEMS)?, kept) {
return Ok(false);
}
}
Ok(true)
}
fn describe_rooted(i: &mut Interp, win: ObjId) -> JsResult<Option<Described>> {
let mut order: Vec<(ObjId, String, f64)> = Vec::new();
let mut seen = std::collections::HashSet::new();
let mut stack: Vec<(Value, f64, usize)> = vec![(Value::Object(win), -1.0, 0)];
while let Some((v, parent, depth)) = stack.pop() {
i.use_fuel()?;
let Some((o, kind)) = kind_of(i, &v)? else { return Err(i.type_error("show: the dialog holds something that is not a control")) };
if !seen.insert(o) {
return Err(i.type_error("show: a control appears twice in the dialog"));
}
if order.len() > MAX_CONTROLS {
return Err(range(i, &format!("show: a dialog can hold at most {MAX_CONTROLS} controls")));
}
if depth > MAX_DEPTH {
return Err(range(i, &format!("show: controls can be nested at most {MAX_DEPTH} deep")));
}
let index = order.len() as f64;
order.push((o, kind, parent));
for kid in kids_of(i, o)?.into_iter().rev() {
stack.push((kid, index, depth + 1));
}
}
let objects: Vec<Value> = order.iter().map(|(o, ..)| Value::Object(*o)).collect();
i.temp_roots.extend(objects.iter().cloned());
let mut nodes = Vec::with_capacity(order.len());
let mut names = Vec::with_capacity(order.len());
let mut items_total = 0usize;
// Each list's array of items as it is read; a change to it means a later getter changed the list.
let mut snapshot = Vec::with_capacity(order.len());
for (o, kind, parent) in &order {
let o = *o;
let mut listed = Value::Undefined;
let name_v = get(i, o, "name")?;
let name = text_of(i, &name_v, "a control's name")?;
let text_v = get(i, o, "text")?;
let text = text_of(i, &text_v, "a control's text")?;
let mut d: Vec<(String, Data)> = vec![
("kind".into(), Data::String(kind.clone())),
("parent".into(), Data::Number(*parent)),
("name".into(), Data::String(name.clone())),
("text".into(), Data::String(text.clone())),
];
names.push((kind.clone(), name, text));
let enabled = get(i, o, "enabled")?.truthy();
let visible = get(i, o, "visible")?.truthy();
d.push(("enabled".into(), Data::Bool(enabled)));
d.push(("visible".into(), Data::Bool(visible)));
// A value a getter gives is kept from collection while its own properties are read.
let b = get_kept(i, o, "bounds")?;
d.push(("bounds".into(), bounds(i, &b)?));
// `size` set to a positive width or height takes the place of `preferredSize`'s.
let preferred = get_kept(i, o, "preferredSize")?;
let preferred = numbers(i, &preferred, &["width", "height"], "preferredSize")?;
let set = get_kept(i, o, "size")?;
let set = numbers(i, &set, &["width", "height"], "size")?;
d.push(("size".into(), overridden(preferred, set)));
for (name, field) in [("minimumSize", "min"), ("maximumSize", "max")] {
let v = get_kept(i, o, name)?;
d.push((field.into(), numbers(i, &v, &["width", "height"], name)?));
}
let a = get_kept(i, o, "alignment")?;
d.push(("alignment".into(), alignment(i, &a)?));
if matches!(kind.as_str(), "dialog" | "group" | "panel") {
let orientation = get(i, o, "orientation")?;
d.push(("orientation".into(), Data::String(text_of(i, &orientation, "an orientation")?.to_lowercase())));
let ac = get_kept(i, o, "alignChildren")?;
d.push(("alignChildren".into(), alignment(i, &ac)?));
d.push(("spacing".into(), number_of(i, o, "spacing")?));
let m = get_kept(i, o, "margins")?;
d.push(("margins".into(), margins(i, &m)?));
}
match kind.as_str() {
"checkbox" | "radiobutton" => {
let on = get(i, o, "value")?.truthy();
d.push(("value".into(), Data::Bool(on)));
}
"slider" | "progressbar" => {
d.push(("value".into(), number_of(i, o, "value")?));
d.push(("minvalue".into(), number_of(i, o, "minvalue")?));
d.push(("maxvalue".into(), number_of(i, o, "maxvalue")?));
}
"edittext" => {
let props = get_kept(i, o, "properties")?;
let (multiline, readonly) = match props {
Value::Object(_) => (i.get_value(&props, &key("multiline"))?.truthy(), i.get_value(&props, &key("readonly"))?.truthy()),
_ => (false, false),
};
d.push(("multiline".into(), Data::Bool(multiline)));
d.push(("readonly".into(), Data::Bool(readonly)));
}
"dropdownlist" | "listbox" => {
let kept = slot(i, o, ITEMS)?;
i.temp_roots.push(kept.clone());
listed = kept;
let items = items_of(i, o)?;
items_total = items_total.saturating_add(items.len());
if items_total > MAX_ITEMS {
return Err(range(i, &format!("show: a dialog's lists hold at most {MAX_ITEMS} items in all")));
}
let mut texts = Vec::with_capacity(items.len());
for item in &items {
let t = i.get_value(item, &key("text"))?;
texts.push(Data::String(text_of(i, &t, "a list item's text")?));
}
d.push(("items".into(), Data::Array(texts)));
let sel = selection_index(i, o)?.filter(|n| *n < items.len());
d.push(("selection".into(), sel.map_or(Data::Null, |n| Data::Number(n as f64))));
}
_ => {}
}
nodes.push(Data::Object(d));
snapshot.push(listed);
}
let target = |i: &mut Interp, name: &str, word: &str| -> JsResult<f64> {
let given = get(i, win, name)?;
if let Some(n) = objects.iter().position(|o| same(o, &given)) {
return Ok(n as f64);
}
if !matches!(given, Value::Undefined | Value::Null) {
return Ok(-1.0);
}
let button = |by_name: bool| names.iter().position(|(k, n, t)| k == "button" && (if by_name { n } else { t }).to_lowercase() == word);
Ok(button(true).or_else(|| button(false)).map_or(-1.0, |n| n as f64))
};
let def = target(i, "defaultElement", "ok")?;
let cancel = target(i, "cancelElement", "cancel")?;
if !lists_unchanged(i, &order, &snapshot)? {
return Ok(None);
}
let desc = Data::Object(vec![
("nodes".into(), Data::Array(nodes)),
("defaultElement".into(), Data::Number(def)),
("cancelElement".into(), Data::Number(cancel)),
]);
let desc = i.write_data(desc, 0, true)?;
i.temp_roots.push(desc.clone());
let kept = list(i, &objects, false)?;
i.temp_roots.push(Value::Object(kept));
let lists = list(i, &snapshot, false)?;
Ok(Some((desc, kept, def, cancel, lists)))
}
/// Adds `Window`, its prototype's methods, and the host functions in `items`.
pub(crate) fn install(i: &mut Interp, items: Vec<(String, HostItem)>) -> JsResult<()> {
let host = i.host_object("Window", items)?;
let object_proto = i.intrinsics.object_proto;
i.rooted(&[Value::Object(host)], |i| {
i.reserve_heap(OBJECT_BYTES.saturating_mul(5))?;
let window_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
i.temp_roots.push(Value::Object(window_proto));
let control_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
i.temp_roots.push(Value::Object(control_proto));
let item_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
i.temp_roots.push(Value::Object(item_proto));
let layout = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
i.temp_roots.push(Value::Object(layout));
let selection_get = native(i, "selection", selection_get, 0, NativeKind::Plain, false)?;
i.temp_roots.push(Value::Object(selection_get));
let selection_set = native(i, "selection", selection_set, 1, NativeKind::Plain, false)?;
i.temp_roots.push(Value::Object(selection_set));
let event_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
i.temp_roots.push(Value::Object(event_proto));
let radio_get = native(i, "value", radio_get, 0, NativeKind::Plain, false)?;
i.temp_roots.push(Value::Object(radio_get));
let radio_set = native(i, "value", radio_set, 1, NativeKind::Plain, false)?;
i.temp_roots.push(Value::Object(radio_set));
i.script_ui = Some(Classes {
window_proto,
control_proto,
item_proto,
event_proto,
layout,
host,
selection_get,
selection_set,
radio_get,
radio_set,
showing: None,
dispatching: Vec::new(),
});
let window = native(i, "Window", window_ctor, 4, NativeKind::Plain, true)?;
constructor(i, "Window", window, window_proto)?;
// `Window.alert`, `Window.confirm` and `Window.prompt` are the host's global functions.
let global = i.global;
for name in ["alert", "confirm", "prompt"] {
if let Some(Prop::Data { value, .. }) = i.get_own_property(global, &key(name))?
&& i.is_callable(&value)
{
data(i, window, name, value, HIDDEN)?;
}
}
for proto in [window_proto, control_proto] {
method(i, proto, "add", add, 4)?;
method(i, proto, "findElement", find_element, 1)?;
method(i, proto, "remove", remove, 1)?;
method(i, proto, "addEventListener", add_event_listener, 3)?;
method(i, proto, "removeEventListener", remove_event_listener, 3)?;
}
method(i, window_proto, "close", close, 1)?;
method(i, window_proto, "hide", hide, 0)?;
method(i, window_proto, "center", nothing, 1)?;
pausable_method(i, window_proto, "show", show, 0)?;
method(i, control_proto, "removeAll", remove_all, 0)?;
method(i, control_proto, "find", find, 1)?;
method(i, control_proto, "notify", notify, 1)?;
method(i, control_proto, "show", control_show, 0)?;
method(i, control_proto, "hide", control_hide, 0)?;
method(i, event_proto, "stopPropagation", event_stop, 0)?;
method(i, event_proto, "preventDefault", nothing, 0)?;
method(i, item_proto, "toString", item_to_string, 0)?;
let index = native(i, "index", item_index, 0, NativeKind::Plain, false)?;
i.define(item_proto, key("index"), Prop::Accessor { get: Some(index), set: None, flags: 0 })?;
let get = native(i, "selected", item_selected_get, 0, NativeKind::Plain, false)?;
let set = native(i, "selected", item_selected_set, 1, NativeKind::Plain, false)?;
i.define(item_proto, key("selected"), Prop::Accessor { get: Some(get), set: Some(set), flags: 0 })?;
method(i, layout, "layout", nothing, 1)?;
method(i, layout, "resize", nothing, 0)?;
i.object_mut(layout)?.extensible = false;
Ok(())
})
}