Merge main into fix/save-screens-unknown-options

This commit is contained in:
dotCooCoo committed 2026-11-08 18:13:32 -08:00
commit bf4ec4253d
190 files changed
+11062 -579

No files matched your search

+47 -5
View File
@@ -395,6 +395,15 @@ impl Source {
}
}
/// The centers of a `cols` × `rows` grid of cells over `b`, row by row.
fn cell_centers(b: Rect, cols: u16, rows: u16) -> impl Iterator<Item = Point> {
let (cw, ch) = (b.width() / cols as f64, b.height() / rows as f64);
(0..rows as usize * cols as usize).map(move |k| {
let (i, j) = (k % cols as usize, k / cols as usize);
Point::new(b.x0 + (i as f64 + 0.5) * cw, b.y0 + (j as f64 + 0.5) * ch)
})
}
/// A freeform gradient ready to sample ([`Freeform::field`]).
#[derive(Clone, Debug)]
pub struct Field {
@@ -429,11 +438,7 @@ impl Field {
/// Samples at the centres of a `cols` × `rows` grid of cells over `b`, row by row: RGB and
/// opacity (as [`Self::sample`]).
pub fn grid(&self, b: Rect, cols: u16, rows: u16) -> impl Iterator<Item = ([f32; 3], f32)> + '_ {
let (cw, ch) = (b.width() / cols as f64, b.height() / rows as f64);
(0..rows as usize * cols as usize).map(move |k| {
let (i, j) = (k % cols as usize, k / cols as usize);
self.sample(Point::new(b.x0 + (i as f64 + 0.5) * cw, b.y0 + (j as f64 + 0.5) * ch))
})
cell_centers(b, cols, rows).map(move |p| self.sample(p))
}
/// Display RGB and opacity at `p`.
@@ -503,6 +508,27 @@ impl ModelField {
};
(c, a)
}
/// [`Self::sample`] at the centers of a `cols` × `rows` grid of cells over `b`, row by row.
pub fn grid(&self, b: Rect, cols: u16, rows: u16) -> impl Iterator<Item = (Color, f32)> + '_ {
cell_centers(b, cols, rows).map(move |p| self.sample(p))
}
/// Display RGB and opacity at `p`: the display color of [`Self::sample`], or the display RGB
/// the points mix in when their models differ.
pub fn display_sample(&self, p: Point) -> ([f32; 3], f32) {
if self.model.is_none() {
return self.first.sample(p);
}
let (c, a) = self.sample(p);
(c.to_rgb(), a)
}
/// [`Self::display_sample`] at the centers of a `cols` × `rows` grid of cells over `b`, row by
/// row.
pub fn display_grid(&self, b: Rect, cols: u16, rows: u16) -> impl Iterator<Item = ([f32; 3], f32)> + '_ {
cell_centers(b, cols, rows).map(move |p| self.display_sample(p))
}
}
#[cfg(test)]
@@ -530,6 +556,22 @@ mod tests {
a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-4)
}
#[test]
fn the_model_field_grid_and_display_samples_match_its_sample() {
let mut f = two(0.0);
(f.points[0].color, f.points[1].color) = (Color::cmyk(1.0, 0.0, 0.0, 0.0), Color::cmyk(0.0, 0.0, 0.0, 1.0));
let field = f.model_field(50.0);
let b = Rect::new(0.0, 0.0, 100.0, 20.0);
let (cells, shown): (Vec<_>, Vec<_>) = (field.grid(b, 5, 1).collect(), field.display_grid(b, 5, 1).collect());
let (mid, a) = field.sample(Point::new(50.0, 10.0));
assert_eq!(cells[2], (mid, a));
assert_eq!(shown[2], (mid.to_rgb(), a));
// Points in different models give their display mix.
(f.points[0].color, f.points[1].color) = (red(), Color::gray(0.5));
let (rgb, _) = f.model_field(50.0).display_sample(Point::new(50.0, 0.0));
assert!(close(rgb, [0.75, 0.25, 0.25]), "{rgb:?}");
}
#[test]
fn the_sample_at_a_point_is_its_colour() {
let f = two(0.0);
+3 -3
View File
@@ -117,9 +117,9 @@ pub use text::FontHeightReference;
pub use text::GlyphChoice;
pub use text::{
AreaFit, AreaOptions, Burasagari, CharAlign, CharPosition, CharStyle, Composer, Digits, FirstBaseline, HyphenationOptions, InlineArt,
JustificationOptions, Justify, Kinsoku, LeadingModel, Mojikumi, ParaDirection, ParaStyle, PathAlign, PathEffect, RubyAlign, RubyPosition,
RubySpan, ScriptMetrics, SingleWordJustify, TabAlign, TabStop, TextFlow, TextKind, TextObject, TextRun, TextStyleDef, TextWrap, VerticalAlign,
WrapShape,
JustificationOptions, Justify, Kinsoku, KinsokuShoriType, LeadingModel, Mojikumi, ParaDirection, ParaStyle, PathAlign, PathEffect, RubyAlign,
RubyPosition, RubySpan, ScriptMetrics, SingleWordJustify, TabAlign, TabStop, TextFlow, TextKind, TextObject, TextRun, TextStyleDef, TextWrap,
VerticalAlign, WrapShape,
};
pub use text::{CharClass, CompositeFont, CompositePart, CompositeParts};
pub use trace::TraceView;
+1
View File
@@ -31,6 +31,7 @@ pub use ids::{LAYER_COLORS, LayerColor, NodeId};
pub use kind::{ImageObject, MAX_BUFFERED_VALUES, NodeKind, with_buffered_value_budget};
pub use live_shape::{LiveShape, Scaling};
pub use opacity::{Knockout, OpacityMask, whiten};
pub use ops::{flattens, keeps_gradient_fit};
use wire::NodeWire;
pub use wire::{MAX_READ_NODES, with_node_budget};
+4
View File
@@ -329,6 +329,10 @@ pub struct Scaling {
/// Pattern fills and strokes move with the art (Transform Patterns; General › Transform
/// Pattern Tiles is its default): their tiles transform too, instead of staying put.
pub patterns: bool,
/// Gradients fitted to the object stay fitted under a transform that flattens it to a line or
/// a point (no area left), as a Transform effect leaves them, instead of being fixed to their
/// fit and flattened to one color with the object.
pub keep_fit_when_flat: bool,
}
impl Scaling {
+14 -3
View File
@@ -431,7 +431,7 @@ impl Node {
// Type and symbols in perspective keep looking the same, moved by `a`.
self.transform_projection(a);
// Refitting an unplaced gradient only reproduces moves and uniform scales.
if !keeps_gradient_fit(a) {
if !keeps_gradient_fit(a) && !(sc.keep_fit_when_flat && flattens(a)) {
self.pin_gradients();
}
let k = Scaling::factor(a);
@@ -770,13 +770,24 @@ impl Node {
}
}
/// Is `a` a move plus a positive uniform scale (after which a refit gradient still matches)?
fn keeps_gradient_fit(a: Affine) -> bool {
/// Is `a` a move plus a positive uniform scale (after which a refit gradient still matches)? The
/// terms are compared within 1e-12 of the scale (at least of 1). A scale of 0 or less, a turn, a
/// reflection and an uneven scale give false, and so does a map that flattens the object
/// ([`flattens`]), which callers that keep a fitted gradient there check apart.
pub fn keeps_gradient_fit(a: Affine) -> bool {
let [m0, m1, m2, m3, _, _] = a.as_coeffs();
let eps = 1e-12 * m0.abs().max(1.0);
m0 > 0.0 && m1.abs() <= eps && m2.abs() <= eps && (m0 - m3).abs() <= eps
}
/// Does `a` flatten what it maps to a line or a point (its determinant 0, within 1e-12 of the
/// squares of its terms), or is it not finite?
pub fn flattens(a: Affine) -> bool {
let [m0, m1, m2, m3, _, _] = a.as_coeffs();
let norm = m0 * m0 + m1 * m1 + m2 * m2 + m3 * m3;
!a.is_finite() || a.determinant().abs() <= 1e-12 * norm
}
impl Node {
/// Whether a document point lies in this object's filled regions ([`Node::clip_shapes`]; true
/// everywhere for objects without them, such as type, whose boxes stand in).
+2 -1
View File
@@ -221,7 +221,8 @@ fn assert_round_corners(n: &Node, back: Affine, r: f64) {
}
}
const KEEP_CORNERS: Scaling = Scaling { strokes: false, effects: None, keep_type_strokes: false, keep_corners: true, patterns: false };
const KEEP_CORNERS: Scaling =
Scaling { strokes: false, effects: None, keep_type_strokes: false, keep_corners: true, patterns: false, keep_fit_when_flat: false };
#[test]
fn uneven_scales_keep_live_corners_circular() {
+2 -2
View File
@@ -23,8 +23,8 @@ pub use composite::{
CompositeParts,
};
pub use para_style::{
Burasagari, Composer, FirstBaseline, HyphenationOptions, JustificationOptions, Justify, Kashida, Kinsoku, LeadingModel, Mojikumi, ParaDirection,
ParaStyle, SingleWordJustify,
Burasagari, Composer, FirstBaseline, HyphenationOptions, JustificationOptions, Justify, Kashida, Kinsoku, KinsokuShoriType, LeadingModel,
Mojikumi, ParaDirection, ParaStyle, SingleWordJustify,
};
pub use runs::{INLINE_CHAR, INLINE_MAX_SCALE, InlineArt, RubyAlign, RubyPosition, RubySpan, TextKind, TextRun, TextStyleDef, TypeAntiAlias};
pub use tabs_lists::{
+28
View File
@@ -234,6 +234,9 @@ pub struct ParaStyle {
/// Kinsoku (Paragraph panel › Kinsoku Set): which Japanese characters may not start or end a
/// line.
pub kinsoku: Kinsoku,
/// Kinsoku Shori Type (Paragraph panel menu): how a line keeps those characters off its start
/// and end, by pushing them in onto the line or out to the next one. No effect without kinsoku.
pub kinsoku_type: KinsokuShoriType,
/// Roman Hanging Punctuation (Paragraph panel menu): quotation marks, hyphens, full stops and
/// commas starting or ending a line hang outside the margin, and asterisks, tildes, ellipses,
/// dashes, colons and semicolons hang by half. East Asian punctuation hangs by burasagari.
@@ -322,6 +325,8 @@ struct ParaStyleFile {
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
kinsoku: Kinsoku,
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
kinsoku_type: KinsokuShoriType,
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
roman_hanging: bool,
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
kashida: Kashida,
@@ -353,6 +358,7 @@ impl From<ParaStyle> for ParaStyleFile {
leading_model,
burasagari,
kinsoku,
kinsoku_type,
roman_hanging,
kashida,
..
@@ -377,6 +383,7 @@ impl From<ParaStyle> for ParaStyleFile {
leading_model,
burasagari,
kinsoku,
kinsoku_type,
roman_hanging,
kashida,
}
@@ -405,6 +412,7 @@ impl From<ParaStyleFile> for ParaStyle {
leading_model,
burasagari,
kinsoku,
kinsoku_type,
roman_hanging,
kashida,
..
@@ -428,6 +436,7 @@ impl From<ParaStyleFile> for ParaStyle {
leading_model,
burasagari,
kinsoku,
kinsoku_type,
roman_hanging,
kashida,
}
@@ -492,6 +501,25 @@ pub enum Kinsoku {
Soft,
}
/// How a line keeps the characters [`Kinsoku`] rules out of its start and end (Kinsoku Shori
/// Type). Pushing in sets the characters that would start the next line (or that an opening
/// bracket ending this line would pull with it) on this line, with the line's gaps next to CJK
/// characters closed to make room, each by the same share of the em of the character before it
/// and by at most a tenth of that em; pushing out moves the character before them to the next
/// line.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum KinsokuShoriType {
/// Push in when the line can close up enough, else push out.
PushInFirst,
/// Push out, unless that would leave the line more than an em short (of the character before
/// the break): then push in when the line can close up enough.
PushOutFirst,
/// Always push out; never push in.
#[default]
PushOutOnly,
}
/// Hanging punctuation (burasagari): an East Asian comma or full stop ending a line (、。,.、。)
/// stands outside the line's measure, in the space its punctuation spacing gives it (half width
/// with Line-end Punctuation Half Width). Closing brackets and Latin punctuation don't hang. Shown
+133
View File
@@ -210,6 +210,139 @@ fn baked_free_distort_moves_placed_gradients_from_the_bounds_to_the_distorted_bo
assert_eq!(baked(&fitted, distort()).1.geom, None);
}
/// A gradient fitted to the object turns, reflects and stretches with a Transform effect that a
/// refit would not reproduce: it is fixed to its fit on the geometry the effect takes, then mapped.
/// Moves and even scales leave it fitted.
#[test]
fn a_fitted_gradient_turns_reflects_and_stretches_with_a_transform_effect() {
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
let fitted = |kind| Paint::Gradient(Box::new(GradientPaint::new(Gradient::new(kind, stops.clone()))));
let geom = |start: (f64, f64), end: (f64, f64)| GradientGeom { start: start.into(), end: end.into(), aspect: 1.0, focal: None };
// The geometry of the baked fill (`own`: the Transform effect is the fill's own effect).
let baked = |kind, effects: &[Effect], own: bool| {
let mut ap = Appearance::basic(fitted(kind), Paint::None, 0.0);
if own {
*ap.items[0].effects_mut() = effects.to_vec();
}
let mut n = Node::path(NodeId(50), shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), ap);
if !own {
n.appearance.effects = effects.to_vec();
}
let d = bake_document(&doc_with(n)).unwrap();
let mut found = vec![];
d.node(NodeId(50)).unwrap().walk(&mut |c: &Node| {
if let Paint::Gradient(g) = c.appearance.fill_paint() {
found.push(g.geom);
}
});
assert_eq!(found.len(), 1, "{effects:?}, own {own}");
found[0]
};
let t = |params| fx("distort.transform", params);
let offset = fx("path.offsetPath", json!({"offset": 10.0, "joins": "miter"}));
let corners = json!({"corners": [[1.0, 0.0], [2.0, 0.0], [2.0, 1.0], [1.0, 1.0]]});
let linear = GradientKind::Linear;
let cases = [
// A quarter turn about the center: the left to right ramp runs from the bottom up.
(linear, vec![t(json!({"rotate": 90.0}))], geom((50.0, 100.0), (50.0, 0.0))),
// Reflected left to right: the ramp runs from right to left.
(linear, vec![t(json!({"reflectX": true}))], geom((100.0, 50.0), (0.0, 50.0))),
// With Scale Strokes & Effects on, the same.
(linear, vec![t(json!({"rotate": 90.0, "strokes": true}))], geom((50.0, 100.0), (50.0, 0.0))),
// Offset Path grows the square to -10..110 first: the turn takes the fit across 120 pt.
(linear, vec![offset.clone(), t(json!({"rotate": 90.0}))], geom((50.0, 110.0), (50.0, -10.0))),
// Stretched to twice the width: the circle of radius 50 about the center becomes an
// ellipse 100 pt across and 50 pt tall.
(GradientKind::Radial, vec![t(json!({"scaleH": 200.0}))], GradientGeom { aspect: 0.5, ..geom((50.0, 50.0), (150.0, 50.0)) }),
// Turned, then moved one width right by Free Distort: the fixed gradient moves with it.
(linear, vec![t(json!({"rotate": 90.0})), fx("distort.freeDistort", corners)], geom((150.0, 100.0), (150.0, 0.0))),
];
for (kind, effects, want) in cases {
for own in [false, true] {
let what = format!("{kind:?} {effects:?}, own {own}");
let got = baked(kind, &effects, own).unwrap_or_else(|| panic!("{what}: fixed to its fit"));
assert_geom_near(got, want, &what);
}
}
// A move and an even scale are what a refit gives: the gradient stays fitted.
for effects in [vec![t(json!({"moveH": 30.0}))], vec![t(json!({"scaleH": 150.0, "scaleV": 150.0})), offset]] {
for own in [false, true] {
assert_eq!(baked(linear, &effects, own), None, "{effects:?}, own {own}");
}
}
// A stroke's gradient is fixed to its fit on the box grown by half its weight (4 pt), -2..102,
// with Scale Strokes & Effects off or on.
let stroked = |effects: &[Effect], own: bool| {
let mut ap = Appearance::basic(Paint::None, fitted(linear), 4.0);
let Some(stroke) = ap.items.iter_mut().find(|i| matches!(i, AppearanceItem::Stroke(_))) else { panic!("a stroke") };
if own {
*stroke.effects_mut() = effects.to_vec();
}
let mut n = Node::path(NodeId(50), shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), ap);
if !own {
n.appearance.effects = effects.to_vec();
}
let d = bake_document(&doc_with(n)).unwrap();
let mut found = vec![];
d.node(NodeId(50)).unwrap().walk(&mut |c: &Node| {
for item in &c.appearance.items {
if let AppearanceItem::Stroke(s) = item
&& let Paint::Gradient(g) = &s.paint
{
found.push(g.geom);
}
}
});
assert_eq!(found.len(), 1, "{effects:?}, own {own}");
found[0]
};
for strokes in [false, true] {
for own in [false, true] {
let what = format!("stroke, strokes {strokes}, own {own}");
let got = stroked(&[t(json!({"rotate": 90.0, "strokes": strokes}))], own).unwrap_or_else(|| panic!("{what}: fixed to its fit"));
assert_geom_near(got, geom((50.0, 102.0), (50.0, -2.0)), &what);
}
}
}
/// A Transform effect that scales the geometry to nothing across leaves a fitted gradient fitted:
/// mapping it with the effect would flatten it to one color.
#[test]
fn a_transform_effect_that_scales_to_nothing_leaves_a_fitted_gradient_fitted() {
use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop};
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
let fitted = Paint::Gradient(Box::new(GradientPaint::new(Gradient::new(GradientKind::Linear, stops))));
// With Scale Strokes & Effects or Transform Patterns on, and for each copy, as without them.
for (options, strokes) in [
(json!({}), 1),
(json!({"strokes": true}), 1),
(json!({"patterns": true}), 1),
(json!({"copies": 2}), 3),
(json!({"strokes": true, "copies": 2}), 3),
(json!({"patterns": true, "copies": 2}), 3),
] {
let mut n = Node::path(NodeId(50), shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), Appearance::basic(Paint::None, fitted.clone(), 4.0));
let mut p = json!({"scaleH": 0.0, "rotate": 30.0});
if let (Some(p), Some(o)) = (p.as_object_mut(), options.as_object()) {
p.extend(o.clone());
}
n.appearance.effects = vec![fx("distort.transform", p)];
let d = bake_document(&doc_with(n)).unwrap();
let mut found = vec![];
d.node(NodeId(50)).unwrap().walk(&mut |c: &Node| {
for item in &c.appearance.items {
if let AppearanceItem::Stroke(s) = item
&& let Paint::Gradient(g) = &s.paint
{
found.push(g.geom);
}
}
});
assert_eq!(found, vec![None; strokes], "{options}");
}
}
#[test]
fn baked_warps_move_placed_gradients_from_the_bounds_to_the_warped_bounds() {
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
+10 -2
View File
@@ -1,4 +1,4 @@
use super::{CROP_MARKS, EXTRUDE, INFLATE, PATHFINDER_EFFECTS, RASTERIZE, REVOLVE, ROTATE, TRAP, Value, json, trap};
use super::{CLASSIC_EXTRUDE, CROP_MARKS, EXTRUDE, INFLATE, PATHFINDER_EFFECTS, RASTERIZE, REVOLVE, ROTATE, TRAP, Value, json, trap};
/// Catalogue entry for one effect.
#[derive(Clone, Debug, PartialEq)]
@@ -80,6 +80,7 @@ fn lengths_of(id: &str) -> Lengths {
"path.offsetPath" => always(&["offset"]),
"threeD.revolve" => always(&["offset"]),
"threeD.extrude" => always(&["depth", "bevelWidth", "bevelHeight"]),
"threeD.classicExtrude" => always(&["depth", "bevelHeight"]),
"threeD.inflate" => always(&["depth"]),
"path.outlineStroke" => always(&["width"]),
"stylize.roundCorners" | "stylize.feather" | "blur.gaussian" | "blur.smart" | "sharpen.unsharpMask" => always(&["radius"]),
@@ -199,6 +200,13 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
"{rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), perspective: % (0..100; the earlier form, read as the perspectiveAngle that shows the same view), shade: bool (true), lightColor: #rrggbb (white, #ffffff; legacy directional light color), lights: null or array of 1..10 objects (color: #rrggbb, intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, softness: % 0..100, behind: bool; overrides the single-light fields; ambient is shared), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 80), lightSoftness: % (0..100, 0; how far round the object the light reaches), ambient: % (0..200, 25; Ambient Light), lightAzimuth: degrees (the earlier form: with lightElevation, read as the lightRotation, lightHeight and lightBehind of the same light), lightElevation: degrees (the earlier form, with lightAzimuth)}. Turns the flat art in 3D about the centre of its bounds; every fill and stroke keeps its paint, shaded as one plane.",
json!({"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"shade":true,"lightColor":"#ffffff","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":80.0,"lightSoftness":0.0,"ambient":25.0}),
),
g(
CLASSIC_EXTRUDE,
"Extrude & Bevel (Classic)…",
&["Effect", "3D and Materials", "3D (Classic)"],
"{depth: pt (0..2000, 50), cap: bool (true: solid; false: hollow), bevel: none|classic|rounded|cove|step|ogee (none), bevelHeight: pt (1..100, 4; the bevel is as wide as it is tall), bevelInside: bool (false: added around the shape; true: carved into it), rotationX: degrees (-180..180, -21.88), rotationY: degrees (-180..180, -23.4), rotationZ: degrees (-180..180, 9.06; the defaults are the Off-Axis Front position), perspectiveAngle: degrees (0..160, 0; 0 is none, wider brings the eye nearer), surface: wireframe|none|diffuse|plastic (plastic; wireframe draws the edges of every face, hidden ones included, and fills none; none keeps the paint unlit; diffuse lights the faces softly; plastic adds a glossy highlight), lights: null or array of 1..10 objects (intensity: % 0..100, rotation: degrees -180..180, height: degrees 0..90, behind: bool; overrides the single-light fields; the lights add up; color and softness are not used), lightRotation: degrees (-180..180, 125.26; round the object on the page, 0 on its right, counterclockwise), lightHeight: degrees (0..90, 30; 0 beside the object, 90 straight in front), lightBehind: bool (false; true lights it from behind), lightIntensity: % (0..100, 100), ambient: % (0..100, 50; Ambient Light, every face whichever way it faces), highlightIntensity: % (0..100, 60; plastic only), highlightSize: % (0..100, 90; 100 broad, 0 a small spot; plastic only), blendSteps: integer (1..256, 25; how many levels the shading and the highlight are rounded to), shadingColor: black|custom|none (black; the color the faces darken toward; none keeps the paint and adds only the highlight), shadingCustom: #rrggbb (#000000; the color shadingColor custom darkens toward), drawHiddenFaces: bool (false; also draws the faces turned away, behind the others)}. The front face is the art, extruded backwards about the center of its bounds; a solid is built for the top fill and for each stroke, each in its own paint. The defaults are VectorCraft's own.",
json!({"depth":50.0,"cap":true,"bevel":"none","bevelHeight":4.0,"bevelInside":false,"rotationX":-21.88,"rotationY":-23.4,"rotationZ":9.06,"perspectiveAngle":0.0,"surface":"plastic","lights":null,"lightRotation":125.26438968275465,"lightHeight":30.0,"lightBehind":false,"lightIntensity":100.0,"ambient":50.0,"highlightIntensity":60.0,"highlightSize":90.0,"blendSteps":25,"shadingColor":"black","shadingCustom":"#000000","drawHiddenFaces":false}),
),
g(
"convertToShape.rectangle",
"Rectangle…",
@@ -239,7 +247,7 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
"distort.transform",
"Transform…",
DT,
"{scaleH: % (100), scaleV: % (100), moveH: pt (0; right = +), moveV: pt (0; down = +), rotate: deg (0; counter-clockwise), copies: 0..1000 (0; each copy transforms the last again; placed gradients transform with the object and with each copy), reflectX: bool (false; flips left to right), reflectY: bool (false; flips top to bottom), reference: 0..8 (4; the point of the bounds' 9-point grid it scales, rotates and reflects about: 0 top left, 4 centre, 8 bottom right), random: bool (false; each scale goes a random share of the way from 100 % to its value, each move and the angle a random share of theirs, differently for each object and the same on every redraw), objects: bool (true; Transform Objects: off, the geometry stays where it is and makes no copies, and only the two options below apply, once), patterns: bool (false; Transform Patterns: pattern fills and strokes transform along; with copies the original keeps its pattern and copy k takes the transformation k times), strokes: bool (false; Scale Strokes & Effects: stroke weights and dashes, and the distances of the effects on the fills and strokes, scale by the mean scale √|scaleH·scaleV|, copy k by its k-th power)}",
"{scaleH: % (100), scaleV: % (100), moveH: pt (0; right = +), moveV: pt (0; down = +), rotate: deg (0; counter-clockwise; when the effect turns, reflects or scales the object unevenly, a gradient fitted to the object is fixed to its fit and turns with the object and with each copy), copies: 0..1000 (0; each copy transforms the last again; placed gradients transform with the object and with each copy), reflectX: bool (false; flips left to right), reflectY: bool (false; flips top to bottom), reference: 0..8 (4; the point of the bounds' 9-point grid it scales, rotates and reflects about: 0 top left, 4 centre, 8 bottom right), random: bool (false; each scale goes a random share of the way from 100 % to its value, each move and the angle a random share of theirs, differently for each object and the same on every redraw), objects: bool (true; Transform Objects: off, the geometry stays where it is and makes no copies, and only the two options below apply, once), patterns: bool (false; Transform Patterns: pattern fills and strokes transform along; with copies the original keeps its pattern and copy k takes the transformation k times), strokes: bool (false; Scale Strokes & Effects: stroke weights and dashes, and the distances of the effects on the fills and strokes, scale by the mean scale √|scaleH·scaleV|, copy k by its k-th power)}",
json!({"scaleH": 100.0, "scaleV": 100.0, "moveH": 0.0, "moveV": 0.0, "rotate": 0.0, "copies": 0, "reflectX": false, "reflectY": false, "reference": 4, "random": false, "objects": true, "patterns": false, "strokes": false}),
),
g(
+40 -15
View File
@@ -174,22 +174,32 @@ pub fn apply_geometry_bez_carrying(effects: &[Effect], path: &BezPath, bounds: R
(out.to_bezpath(), carry)
}
/// How placed gradients follow geometry effects. The Transform effect without copies maps them as
/// it maps the object (with copies, `crate::reshape` gives each copy its own). Free Distort and
/// the Warp effects move and scale them from the object's bounds before the effect to its bounds
/// after it. For the other geometry effects it is empty. It does not change gradients fitted to
/// the object.
/// How gradients follow geometry effects. The Transform effect without copies maps placed
/// gradients as it maps the object (with copies, `crate::reshape` gives each copy its own). Free
/// Distort and the Warp effects move and scale them from the object's bounds before the effect to
/// its bounds after it. For the other geometry effects it is empty. A gradient fitted to the
/// object fits the geometry each effect makes, until a Transform effect turns, reflects or scales
/// it unevenly (a map after which a refit would not match the mapped gradient): it is fixed to its
/// fit on the geometry that effect takes, and then follows that effect and the ones after it as a
/// placed gradient does.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Carry(Vec<CarryStep>);
/// Does `m` fix a gradient fitted to the object to its fit before mapping it? It does when a refit
/// would not reproduce the map (a turn, a reflection or an uneven scale) and `m` keeps area: a map
/// that flattens the geometry to a line (a scale of 0) leaves the gradient fitted, since mapping it
/// would flatten it to one color ([`vectorcraft_doc::node::flattens`]).
pub(crate) fn fixes_fit(m: vectorcraft_geom::Affine) -> bool {
!vectorcraft_doc::node::flattens(m) && !vectorcraft_doc::node::keeps_gradient_fit(m)
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum CarryStep {
Affine(vectorcraft_geom::Affine),
/// Map `m`; `from` is the geometric bounds of the geometry it takes, which a fitted gradient
/// fits before `m` moves it (none: fitted gradients stay fitted).
Affine { m: vectorcraft_geom::Affine, from: Option<Rect> },
/// From box `from` to box `to` (strokes: their stroke-inflated boxes).
Boxes {
from: Rect,
to: Rect,
},
Boxes { from: Rect, to: Rect },
}
impl Carry {
@@ -229,7 +239,7 @@ impl Carry {
return Carry::default();
}
let step = match id {
"distort.transform" => Some(CarryStep::Affine(distort::transform_map(b, p, seed).0)),
"distort.transform" => Some(CarryStep::Affine { m: distort::transform_map(b, p, seed).0, from }),
_ => match (from, to) {
(Some(from), Some(to)) if from != to && [to.x0, to.y0, to.x1, to.y1].iter().all(|v| v.is_finite()) => {
Some(CarryStep::Boxes { from, to })
@@ -249,17 +259,32 @@ impl Carry {
/// freeform gradient's own points included)? Callers check first, so an appearance shared with
/// other objects is not copied for nothing.
pub fn moves(&self, ap: &Appearance) -> bool {
!self.0.is_empty() && ap.has_positioned_gradient()
!self.0.is_empty() && (ap.has_positioned_gradient() || (self.pin().is_some() && ap.has_unplaced_gradient()))
}
/// Move the placed gradients of `ap` along, through each step in turn.
/// The first step that fixes gradients fitted to the object: a map a refit would not
/// reproduce, on geometry whose bounds are known.
fn pin(&self) -> Option<usize> {
self.0.iter().position(|s| matches!(s, CarryStep::Affine { m, from: Some(_) } if fixes_fit(*m)))
}
/// Move the gradients of `ap` along. Placed gradients take every step in turn; fitted ones
/// are fixed to their fit at the first step [`Self::pin`] finds and take it and the steps
/// after it.
pub fn apply(&self, ap: &mut Appearance) {
if !self.moves(ap) {
return;
}
for s in &self.0 {
// The steps move placed gradients only; fitted ones become placed at the pin.
let pin = self.pin();
for (k, s) in self.0.iter().enumerate() {
if Some(k) == pin
&& let CarryStep::Affine { from: Some(b), .. } = *s
{
ap.pin_gradients(b);
}
match *s {
CarryStep::Affine(m) => ap.transform_gradients(m),
CarryStep::Affine { m, .. } => ap.transform_gradients(m),
CarryStep::Boxes { from, to } => crate::reshape::follow_boxes(ap, from, to),
}
}
+64
View File
@@ -598,6 +598,70 @@ fn a_groups_transform_moves_the_gradients_placed_on_an_images_own_fills() {
}
}
/// A group's Transform effect that turns its art turns the gradients fitted to its paths, to the
/// group's own fill, to a nested group's own fill and to an image's and a symbol instance's own
/// fills: each is fixed to its fit on its own object first. A move leaves the members' gradients
/// fitted.
#[test]
fn a_groups_transform_turns_the_gradients_fitted_to_its_members_and_its_own_fill() {
use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop};
use vectorcraft_doc::ImageObject;
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
let fitted = Paint::Gradient(Box::new(GradientPaint::new(Gradient::new(GradientKind::Linear, stops))));
let square = |id: u64, x: f64| {
Arc::new(Node::path(NodeId(id), shapes::rectangle(Rect::new(x, 0.0, x + 10.0, 10.0)), Appearance::basic(fitted.clone(), Paint::None, 0.0)))
};
let mut image = Node::new(
NodeId(4),
NodeKind::Image(Box::new(ImageObject {
key: "k".into(),
width: 10,
height: 10,
xf: vectorcraft_geom::Affine::translate((30.0, 0.0)),
link: None,
placement: Default::default(),
pixels: None,
})),
);
image.appearance = Appearance::basic(fitted.clone(), Paint::None, 0.0).into();
// A symbol instance whose symbol is not in a document is measured as the 20 pt square about
// its origin: here 22.5..27.5 across and 2.5..7.5 down.
let at = vectorcraft_geom::Affine::translate((25.0, 5.0)) * vectorcraft_geom::Affine::scale(0.25);
let mut instance = Node::new(NodeId(6), NodeKind::SymbolInstance { symbol: "s".into(), xf: at });
instance.appearance = Appearance::basic(fitted.clone(), Paint::None, 0.0).into();
// Ends of each gradient by object id; none for a gradient still fitted.
let ends = |params: serde_json::Value| {
let mut inner = Node::group(NodeId(5), vec![square(2, 10.0)]);
inner.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(fitted.clone())));
let mut g = Node::group(NodeId(10), vec![square(1, 0.0), Arc::new(inner), Arc::new(image.clone()), Arc::new(instance.clone())]);
g.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(fitted.clone())));
g.appearance.effects.push(Effect { id: "distort.transform".into(), params, visible: true });
let art = evaluate_container(None, &g).unwrap();
let mut out = std::collections::BTreeMap::new();
art.walk(&mut |c: &Node| {
if let Paint::Gradient(p) = c.appearance.fill_paint() {
out.insert(c.id.0, p.geom.map(|g| (g.start, g.end)));
}
});
out
};
let near = |a: Point, b: Point| a.distance(b) < 1e-6;
// A quarter turn about the group's center (20, 5): (x, 5) goes to (20, 5 + 20 - x), so a ramp
// from (x0, 5) to (x1, 5) runs from (20, 25 - x0) up to (20, 25 - x1).
let turned = ends(serde_json::json!({"rotate": 90}));
let want = [(1, 0.0, 10.0), (2, 10.0, 20.0), (5, 10.0, 20.0), (4, 30.0, 40.0), (6, 22.5, 27.5), (10, 0.0, 40.0)];
for (id, x0, x1) in want {
let got = turned.get(&id).copied().flatten();
let ok = got.is_some_and(|(s, e)| near(s, Point::new(20.0, 25.0 - x0)) && near(e, Point::new(20.0, 25.0 - x1)));
assert!(ok, "object {id}: {got:?} in {turned:?}");
}
// The group's own fill is fixed to its fit on the art it paints, which the move took to 30..70.
let moved = ends(serde_json::json!({"moveH": 30}));
let group = moved.get(&10).copied().flatten();
assert!(group.is_some_and(|(s, e)| near(s, Point::new(30.0, 5.0)) && near(e, Point::new(70.0, 5.0))), "{moved:?}");
assert!(moved.len() == 6 && [1, 2, 4, 5, 6].iter().all(|id| moved[id].is_none()), "{moved:?}");
}
#[test]
fn a_transform_on_a_group_paints_each_copy_with_its_own_gradient() {
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
+3 -2
View File
@@ -85,8 +85,9 @@ pub use reshape::{
};
pub use revolve::{REVOLVE, has_revolve, revolve_art, revolve_options, validate_revolve};
pub use solids::{
EXTRUDE, INFLATE, ROTATE, ROTATION_PRESETS, SOLIDS, SolidMesh, THREE_D_TYPES, extrude_options, has_solid, inflate_options, is_three_d, solid_art,
three_d_meshes, three_d_meshes_of, three_d_params_as, validate_solid, validate_three_d, view_options,
CLASSIC_EXTRUDE, EXTRUDE, INFLATE, ROTATE, ROTATION_PRESETS, SHADING_COLORS, SOLIDS, SolidMesh, THREE_D_TYPES, classic_extrude_options,
classic_shading, extrude_options, has_solid, inflate_options, is_three_d, solid_art, three_d_meshes, three_d_meshes_of, three_d_params_as,
validate_solid, validate_three_d, validate_three_d_params, view_options,
};
pub use stylize::{ScribbleSetting, scribble_setting_id, scribble_settings};
pub use svg_filter::SVG_FILTER;
+16 -5
View File
@@ -708,7 +708,8 @@ fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: b
return;
}
if separate {
let made = copy_leaf(leaf, &path, m, copies, vectorcraft_doc::Scaling::default(), room);
let scaling = vectorcraft_doc::Scaling { keep_fit_when_flat: true, ..Default::default() };
let made = copy_leaf(leaf, &path, m, copies, scaling, room);
used = used.minus(before).plus(made);
return;
}
@@ -718,8 +719,12 @@ fn reshape_leaves_apart(art: &mut Node, fx: &[&Effect], budget: Budget, apart: b
return;
};
used = used.minus(before).plus(Budget::of(&out));
if follows && !boxes && leaf.appearance.has_positioned_gradient() {
crate::Carry::of(&e.id, &params, bounds, None, None, seed).apply(&mut leaf.appearance);
if follows && !boxes {
// A gradient fitted to the path fits the path's own bounds.
let carry = crate::Carry::of(&e.id, &params, bounds, path.bounds(), None, seed);
if carry.moves(&leaf.appearance) {
carry.apply(&mut leaf.appearance);
}
}
set_geometry(leaf, out, rule);
});
@@ -774,6 +779,8 @@ fn transform_leaf(
strokes: options.strokes,
effects: options.strokes.then_some(crate::scale_effect as fn(&mut Effect, f64)),
patterns: options.patterns,
// A map that flattens the path leaves its fitted gradients fitted, as `fixes_fit` does.
keep_fit_when_flat: true,
..Default::default()
};
if !options.objects {
@@ -1052,7 +1059,7 @@ fn is_member_group(n: &Node) -> bool {
}
/// A group below the art that [`is_member_group`] takes and whose own fills or strokes hold a
/// placed gradient: the child indices that lead to it from the art, its geometric bounds, and
/// gradient: the child indices that lead to it from the art, its geometric bounds, and
/// whether a path below it that its bounds measure kept its shape.
type GroupMark = (Vec<usize>, Option<Rect>, bool);
@@ -1069,7 +1076,7 @@ fn group_marks(n: &Node, at: &mut Vec<usize>, kept: &HashSet<NodeId>, out: &mut
for (i, c) in children.iter().enumerate() {
at.push(i);
let (b, h) = group_marks(c, at, kept, out);
if is_member_group(c) && c.appearance.has_positioned_gradient() {
if is_member_group(c) && (c.appearance.has_positioned_gradient() || c.appearance.has_unplaced_gradient()) {
out.push((at.clone(), b, h));
}
at.pop();
@@ -1223,6 +1230,10 @@ fn copy_placed(n: &mut Node, m: Affine, copies: usize, room: &mut usize) {
let src = (**c).clone();
let placed = |xf: Affine| {
let mut one = src.clone();
// Gradients fitted to it are fixed to their fit first when a refit would not match.
if crate::geometry::fixes_fit(xf) {
one.pin_gradients();
}
match &mut one.kind {
NodeKind::Image(im) => im.xf = xf * im.xf,
NodeKind::SymbolInstance { xf: at, .. } => *at = xf * *at,
+8 -2
View File
@@ -66,19 +66,25 @@ pub fn revolve_options(p: &Value) -> Revolve {
/// Validate before a command mutates the document. Loading external files is still bounded
/// by the evaluator; unsupported source kinds remain unchanged when rendered.
pub fn validate_revolve(n: &Node, p: &Value) -> Result<(), String> {
crate::lighting::validate(p)?;
validate_revolve_params(p)?;
let path = profile(n).ok_or("Revolve currently requires a path or compound path. Select a profile drawn with the Pen tool.")?;
let b = path.bounds().ok_or("Revolve requires a profile with at least two anchors")?;
if !path.subpaths.iter().any(|sp| sp.anchors.len() >= 2) {
return Err("Revolve requires at least two anchors".into());
}
vectorcraft_three_d::revolve(&path, b, revolve_options(p)).map(|_| ())
}
/// Check Revolve's parameter values `p` alone: its lights, `edge` and `cap`.
pub(crate) fn validate_revolve_params(p: &Value) -> Result<(), String> {
crate::lighting::validate(p)?;
if p.get("edge").is_some_and(|v| v.as_str() != Some("left") && v.as_str() != Some("right")) {
return Err("Revolve edge must be left or right".into());
}
if p.get("cap").is_some_and(|v| v.as_bool().is_none()) {
return Err("Revolve cap must be a boolean".into());
}
vectorcraft_three_d::revolve(&path, b, revolve_options(p)).map(|_| ())
Ok(())
}
fn profile(n: &Node) -> Option<PathData> {
+32 -8
View File
@@ -1,5 +1,5 @@
//! Extrude & Bevel, Inflate and Rotate as live, shaded vector art, shared by the canvas and every
//! exporter. Like Revolve, the source path stays editable and the effect draws its 3D faces.
//! Extrude & Bevel, Inflate, Rotate and Extrude & Bevel (Classic) as live, shaded vector art,
//! shared by the canvas and every exporter. Like Revolve, the source path stays editable and the effect draws its 3D faces.
use std::sync::Arc;
use serde_json::Value;
@@ -14,11 +14,14 @@ use vectorcraft_doc::color::Color;
use crate::util::{flag, num, text};
use crate::{GeomContext, apply_geometry_with, is_geometry};
mod classic;
pub use classic::{CLASSIC_EXTRUDE, SHADING_COLORS, classic_extrude_options, classic_shading};
pub const EXTRUDE: &str = "threeD.extrude";
pub const INFLATE: &str = "threeD.inflate";
pub const ROTATE: &str = "threeD.rotate";
/// The 3D effects this module draws (Revolve has its own).
pub const SOLIDS: [&str; 3] = [EXTRUDE, INFLATE, ROTATE];
pub const SOLIDS: [&str; 4] = [EXTRUDE, INFLATE, ROTATE, CLASSIC_EXTRUDE];
/// Is `id` one of the 3D and Materials effects (Revolve included)?
pub fn is_three_d(id: &str) -> bool {
@@ -119,6 +122,7 @@ pub fn extrude_options(p: &Value) -> Extrude {
inside: flag(p, "bevelInside", false),
both_sides: flag(p, "bevelBothSides", false),
}),
hidden: false,
}
}
@@ -172,21 +176,32 @@ fn facets(id: &str, path: &PathData, rule: FillRule, bounds: Rect, p: &Value) ->
let view = view_options(p);
match id {
EXTRUDE => vectorcraft_three_d::extrude(path, rule, bounds, extrude_options(p), view),
CLASSIC_EXTRUDE => vectorcraft_three_d::extrude(path, rule, bounds, classic_extrude_options(p), view),
INFLATE => vectorcraft_three_d::inflate(path, rule, bounds, inflate_options(p), view),
_ => vectorcraft_three_d::plane(path, rule, bounds, view),
}
}
/// Validate before a command changes the document; the evaluator bounds loaded files too.
pub fn validate_solid(n: &Node, id: &str, p: &Value) -> Result<(), String> {
/// Check solid `id`'s parameter values `p` alone: the part of [`validate_solid`] that does not
/// need the object.
fn validate_solid_params(id: &str, p: &Value) -> Result<(), String> {
if id == CLASSIC_EXTRUDE {
classic::validate(p)?;
}
crate::lighting::validate(p)?;
let label = crate::effect_info(id).map_or("This effect", |e| e.label.trim_end_matches('…'));
if let Some(v) = p.get("bevel")
&& !v.as_str().is_some_and(|s| s == "none" || BevelShape::parse(s).is_some())
{
let names: Vec<_> = BevelShape::ALL.iter().map(|b| b.id()).collect();
return Err(format!("bevel must be none or one of {}", names.join(", ")));
}
Ok(())
}
/// Validate before a command changes the document; the evaluator bounds loaded files too.
pub fn validate_solid(n: &Node, id: &str, p: &Value) -> Result<(), String> {
validate_solid_params(id, p)?;
let label = crate::effect_info(id).map_or("This effect", |e| e.label.trim_end_matches('…'));
if is_container(n) {
let shapes = members(n, label)?;
let b = union_bounds(&shapes).ok_or_else(|| format!("{label} requires a group holding a shape with at least two anchors"))?;
@@ -273,6 +288,13 @@ pub fn validate_three_d(n: &Node, id: &str, p: &Value) -> Result<(), String> {
if id == crate::REVOLVE { crate::validate_revolve(n, p) } else { validate_solid(n, id, p) }
}
/// Are 3D effect `id`'s parameter values `p` valid? The checks of [`validate_three_d`] that do not
/// build the art, for an edit that hides the effect: hidden, it draws nothing, so its object does
/// not need to fit.
pub fn validate_three_d_params(id: &str, p: &Value) -> Result<(), String> {
if id == crate::REVOLVE { crate::revolve::validate_revolve_params(p) } else { validate_solid_params(id, p) }
}
/// One surface: each facet a path shaded from `paint`, as a group with the paint's opacity and
/// blend mode (so overlapping facets don't add up their transparency).
fn surface(n: &Node, facets: Vec<Facet>, paint: &Paint, opacity: f32, blend: BlendMode, colored: bool) -> Node {
@@ -384,7 +406,8 @@ fn reshape_members(n: &Node, before: &[Effect], shapes: &mut [Profile]) {
fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>> {
let b = union_bounds(shapes)?;
let p = crate::merged_params(&fx.id, &fx.params);
let colored = view_options(&p).has_colored_lights();
let classic = fx.id == CLASSIC_EXTRUDE;
let colored = !classic && view_options(&p).has_colored_lights();
if fx.id == ROTATE {
// Every part turns about the centre of the object's own bounds (a group's members about
// the centre of theirs), so they stay together.
@@ -432,7 +455,8 @@ fn art_for(n: &Node, fx: &Effect, shapes: &[Profile]) -> Option<Vec<Arc<Node>>>
.map(|(item, member, f)| {
let depths: Vec<f64> = f.iter().map(|f| f.depth).collect();
let (paint, opacity, blend) = paint_of(item, member);
(depths, surface(n, f, &paint, opacity, blend, colored))
let art = if classic { classic::surface(n, f, &paint, opacity, blend, &p) } else { surface(n, f, &paint, opacity, blend, colored) };
(depths, art)
})
.collect();
// One order takes a plain group of faces from each solid, one face for each facet. With
+220
View File
@@ -0,0 +1,220 @@
//! Extrude & Bevel (Classic): the solid Extrude & Bevel builds from a path, drawn with the classic
//! surfaces (Wireframe, No Shading, Diffuse and Plastic), their highlight, blend steps and shading
//! color.
use std::sync::Arc;
use serde_json::Value;
use vectorcraft_doc::color::{BlendMode, Color, Paint};
use vectorcraft_doc::{Appearance, AppearanceItem, FillLayer, Node, StrokeLayer};
use vectorcraft_geom::Rect;
use vectorcraft_three_d::{Bevel, BevelShape, ClassicShading, ClassicSurface, Extrude, Facet, Light};
use crate::util::{flag, num, text};
pub const CLASSIC_EXTRUDE: &str = "threeD.classicExtrude";
/// The width of a Wireframe edge, in points.
const WIRE: f64 = 0.5;
/// The Shading Color choices: black, a custom color, or none.
pub const SHADING_COLORS: [&str; 3] = ["black", "custom", "none"];
/// The surface settings of Extrude & Bevel (Classic)'s parameters.
pub fn classic_shading(p: &Value) -> ClassicShading {
let d = ClassicShading::default();
ClassicShading {
surface: ClassicSurface::parse(text(p, "surface", "plastic")).unwrap_or_default(),
ambient: num(p, "ambient", d.ambient * 100.0) / 100.0,
highlight_intensity: num(p, "highlightIntensity", d.highlight_intensity * 100.0) / 100.0,
highlight_size: num(p, "highlightSize", d.highlight_size * 100.0) / 100.0,
blend_steps: num(p, "blendSteps", f64::from(d.blend_steps)).round().clamp(1.0, 256.0) as u32,
}
}
/// The solid of Extrude & Bevel (Classic)'s parameters: its depth, cap and bevel (as tall as it is
/// wide, added around the shape or carved into it), with the faces turned away from the viewer kept
/// for Draw Hidden Faces and for the Wireframe surface.
pub fn classic_extrude_options(p: &Value) -> Extrude {
let height = num(p, "bevelHeight", 4.0);
Extrude {
depth: num(p, "depth", 50.0),
cap: flag(p, "cap", true),
bevel: BevelShape::parse(text(p, "bevel", "none")).map(|shape| Bevel {
shape,
width: height,
height,
inside: flag(p, "bevelInside", false),
..Bevel::default()
}),
hidden: flag(p, "drawHiddenFaces", false) || classic_shading(p).surface == ClassicSurface::Wireframe,
..Extrude::default()
}
}
/// The color faces darken toward: black, the custom color, or none (faces keep their paint and only
/// the highlight lightens them).
fn shading_color(p: &Value) -> Option<Color> {
match text(p, "shadingColor", "black") {
"none" => None,
"custom" => Some(p.get("shadingCustom").and_then(Value::as_str).and_then(parse_hex).unwrap_or(Color::BLACK)),
_ => Some(Color::BLACK),
}
}
fn parse_hex(s: &str) -> Option<Color> {
(s.len() == 7 && s.starts_with('#') && s.bytes().skip(1).all(|b| b.is_ascii_hexdigit())).then(|| Color::from_hex(s)).flatten()
}
/// The lights: the `lights` list when it is given, else the one light of `lightRotation`,
/// `lightHeight`, `lightBehind` and `lightIntensity`. Each is its direction toward the light in view
/// space and its intensity (0..1).
fn lights(p: &Value) -> Vec<([f64; 3], f64)> {
if let Some(list) = crate::lighting::lights(p) {
return list.iter().map(|l| (l.direction(), l.intensity)).collect();
}
let light = Light {
rotation: num(p, "lightRotation", vectorcraft_three_d::DEFAULT_LIGHT.0),
height: num(p, "lightHeight", vectorcraft_three_d::DEFAULT_LIGHT.1),
behind: flag(p, "lightBehind", false),
..Light::default()
};
vec![(light.direction(), num(p, "lightIntensity", 100.0) / 100.0)]
}
/// The numeric parameters: (key, lowest, highest, whole numbers only).
const RANGES: [(&str, f64, f64, bool); 13] = [
("depth", 0.0, 2000.0, false),
("bevelHeight", 1.0, 100.0, false),
("rotationX", -180.0, 180.0, false),
("rotationY", -180.0, 180.0, false),
("rotationZ", -180.0, 180.0, false),
("perspectiveAngle", 0.0, 160.0, false),
("lightRotation", -180.0, 180.0, false),
("lightHeight", 0.0, 90.0, false),
("lightIntensity", 0.0, 100.0, false),
("ambient", 0.0, 100.0, false),
("highlightIntensity", 0.0, 100.0, false),
("highlightSize", 0.0, 100.0, false),
("blendSteps", 1.0, 256.0, true),
];
const FLAGS: [&str; 4] = ["cap", "bevelInside", "lightBehind", "drawHiddenFaces"];
/// Check Extrude & Bevel (Classic)'s parameters `p` (its defaults filled in): every key is one of
/// its parameters, numbers are in range, flags are true or false, and `surface`, `shadingColor` and
/// `shadingCustom` name a surface, a shading color and a #rrggbb color.
pub fn validate(p: &Value) -> Result<(), String> {
let Some(map) = p.as_object() else { return Ok(()) };
let defaults = crate::default_params(CLASSIC_EXTRUDE).unwrap_or(Value::Null);
let known: Vec<&str> = defaults.as_object().map(|m| m.keys().map(String::as_str).collect()).unwrap_or_default();
for key in map.keys() {
if !known.contains(&key.as_str()) {
let keys = known.join(", ");
return Err(format!("unknown parameter `{key}`; the command takes: {keys}"));
}
}
for (key, min, max, whole) in RANGES {
let Some(v) = map.get(key) else { continue };
let Some(x) = v.as_f64() else { return Err(format!("`{key}` must be a number")) };
if whole && (x.fract() != 0.0 || !(min..=max).contains(&x)) {
return Err(format!("`{key}` must be a whole number between {min} and {max} (got {x})"));
}
if !(min..=max).contains(&x) {
return Err(format!("`{key}` must be between {min} and {max} (got {x})"));
}
}
for key in FLAGS {
if map.get(key).is_some_and(|v| !v.is_boolean()) {
return Err(format!("`{key}` must be true or false"));
}
}
let one_of = |key: &str, ids: &[&str]| -> Result<(), String> {
match map.get(key) {
Some(v) if !v.as_str().is_some_and(|s| ids.contains(&s)) => Err(format!("`{key}` must be one of {}", ids.join(", "))),
_ => Ok(()),
}
};
let surfaces: Vec<&str> = ClassicSurface::ALL.iter().map(|s| s.id()).collect();
one_of("surface", &surfaces)?;
one_of("shadingColor", &SHADING_COLORS)?;
let key = "shadingCustom";
if map.get(key).is_some_and(|v| !v.as_str().is_some_and(|s| parse_hex(s).is_some())) {
return Err(format!("`{key}` must be a #rrggbb color"));
}
Ok(())
}
/// `c` with tone `tone` (0 the shading color, 1 the color itself) and highlight `spec` (toward
/// white) on top.
fn shaded(c: Color, tone: f64, spec: f64, shade: Option<Color>) -> Color {
let base = c.to_rgb();
let dark = shade.map_or(base, |s| s.to_rgb());
let (tone, spec) = (tone as f32, spec as f32);
let [r, g, b] = [0, 1, 2].map(|i| {
let mixed = dark[i] + (base[i] - dark[i]) * tone;
(mixed + (1.0 - mixed) * spec).clamp(0.0, 1.0)
});
Color::rgb(r, g, b)
}
/// One solid's faces in paint `paint`, as a group with the paint's opacity and blend mode, one child
/// for each facet: the edges of the faces on Wireframe, else each face filled in its paint shaded by
/// its tone and highlight. A gradient with transparent stops is shaded opaque and then masked by
/// its own transparency, so the shading follows the gradient's coverage.
pub(super) fn surface(n: &Node, facets: Vec<Facet>, source: &Paint, opacity: f32, blend: BlendMode, p: &Value) -> Node {
let shading = classic_shading(p);
let lights = lights(p);
let shade = shading_color(p);
let wire = shading.surface == ClassicSurface::Wireframe;
let (paint, mask) = if wire { (source.clone(), None) } else { crate::lighting::opaque_paint(source) };
let paint = &paint;
let children = facets
.into_iter()
.map(|face| {
if wire {
let mut edge = StrokeLayer::new(paint.clone(), WIRE);
edge.join = vectorcraft_doc::LineJoin::Round;
return Arc::new(Node::path(n.id, face.path(), Appearance { items: vec![AppearanceItem::Stroke(edge)], ..Default::default() }));
}
let (tone, spec) = shading.tone(face.normal, &lights);
// A narrow stroke of the face's own paint closes the hairline seams between faces.
let seam = (face.thickness() / 2.0).clamp(0.05, 0.75);
let items = match paint.color() {
// Unshaded (No Shading, no shading color, or a face in full light) with no
// highlight: the paint itself, its color space and swatch kept.
Some(_) if (tone >= 1.0 || shade.is_none()) && spec <= 0.0 => {
vec![AppearanceItem::Fill(FillLayer::new(paint.clone())), AppearanceItem::Stroke(StrokeLayer::new(paint.clone(), seam))]
}
Some(c) => {
let fill = Paint::solid(shaded(c, tone, spec, shade));
vec![AppearanceItem::Fill(FillLayer::new(fill.clone())), AppearanceItem::Stroke(StrokeLayer::new(fill, seam))]
}
// Gradients and patterns keep their paint, darkened by a layer of the shading color
// and lightened by one of white. As with the other 3D effects' pattern shadow, the
// layers cover the whole face, transparent pattern cells included.
None => {
let mut items =
vec![AppearanceItem::Fill(FillLayer::new(paint.clone())), AppearanceItem::Stroke(StrokeLayer::new(paint.clone(), seam))];
for (color, share) in [(shade, 1.0 - tone), (Some(Color::WHITE), spec)] {
if let Some(color) = color.filter(|_| share > 0.0) {
let mut layer = FillLayer::new(Paint::solid(color));
layer.opacity = share as f32;
items.push(AppearanceItem::Fill(layer));
}
}
items
}
};
Arc::new(Node::path(n.id, face.path(), Appearance { items, ..Default::default() }))
})
.collect();
let mut group = Node::group(n.id, children);
group.opacity = opacity;
group.blend = blend;
if let Some(mask) = mask {
let bounds = group.visual_bounds().unwrap_or(Rect::ZERO).inflate(1.0, 1.0);
let coverage = Node::path(n.id, vectorcraft_geom::shapes::rectangle(bounds), Appearance::basic(mask, Paint::None, 0.0));
group.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(coverage, true)));
}
group
}
+164 -2
View File
@@ -45,10 +45,15 @@ fn every_solid_draws_shaded_faces_and_leaves_the_source_editable() {
assert_eq!(n.path_data(), Some(&source), "{id}");
assert!(!faces(&art).is_empty(), "{id}");
// The faces are the fill's red or the stroke's black, shaded: never brighter, never another
// hue.
// hue. The classic Plastic surface, its default, also lightens a face toward white, which
// scales every channel's distance from white by the same factor and so keeps r - g to
// r - b at 6 to 7.
for f in faces(&art) {
let [r, g, b] = fill_rgb(f);
assert!(r <= 0.8 + 1e-6 && (g * 4.0 - r).abs() < 1e-4 && (b * 8.0 - r).abs() < 1e-4 || id == ROTATE, "{id}: {r} {g} {b}");
let shaded = r <= 0.8 + 1e-6 && (g * 4.0 - r).abs() < 1e-4 && (b * 8.0 - r).abs() < 1e-4;
let highlighted =
id == CLASSIC_EXTRUDE && [r, g, b].iter().all(|c| (0.0..=1.0).contains(c)) && ((r - g) * 7.0 - (r - b) * 6.0).abs() < 1e-4;
assert!(shaded || highlighted || id == ROTATE, "{id}: {r} {g} {b}");
}
// The art follows the source when it moves.
let before = art.visual_bounds().unwrap();
@@ -595,3 +600,160 @@ fn a_3d_effect_on_a_group_turns_every_member_about_one_shared_axis() {
assert!(err.contains("clipping groups"), "{err}");
assert!(solid_art(&g).is_none(), "drawn as the group it is");
}
/// The red square without its stroke, with Extrude & Bevel (Classic) and `params`.
fn classic(params: Value) -> Node {
let mut n = square_node();
n.appearance.items.retain(|i| matches!(i, AppearanceItem::Fill(_)));
n.appearance.effects.push(fx(CLASSIC_EXTRUDE, params));
n
}
fn classic_faces(params: Value) -> Vec<Node> {
let art = solid_art(&classic(params)).unwrap();
faces(&art).into_iter().cloned().collect()
}
fn close3(a: [f32; 3], b: [f32; 3]) -> bool {
a.iter().zip(b).all(|(a, b)| (a - b).abs() < 1e-4)
}
#[test]
fn classic_surfaces_draw_edges_flat_paint_soft_light_and_a_highlight() {
// Wireframe: the edges of every face, the hidden ones too, in the paint; no fills.
let wire = classic_faces(json!({"surface": "wireframe"}));
let plastic = classic_faces(json!({}));
assert_eq!(plastic.len(), 3, "a box shows three faces");
assert_eq!(wire.len(), 6, "and has six");
for f in &wire {
let [AppearanceItem::Stroke(s)] = &f.appearance.items[..] else { panic!("a stroke alone: {:?}", f.appearance.items) };
assert_eq!(s.paint, Paint::solid(RED));
}
// No Shading: every face in the art's own paint, a CMYK color kept as it is.
assert!(classic_faces(json!({"surface": "none"})).iter().all(|f| close3(fill_rgb(f), RED.to_rgb())));
let cmyk = Paint::solid(Color::Cmyk { c: 0.1, m: 0.9, y: 0.8, k: 0.0 });
let mut n = classic(json!({"surface": "none"}));
if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() {
f.paint = cmyk.clone();
}
for f in faces(&solid_art(&n).unwrap()) {
let AppearanceItem::Fill(fill) = &f.appearance.items[0] else { panic!("a fill first") };
assert_eq!(fill.paint, cmyk);
}
// Diffuse: the red darkened toward black by how far each face turns from the light, so the
// faces differ but keep their hue.
let diffuse: Vec<[f32; 3]> = classic_faces(json!({"surface": "diffuse"})).iter().map(fill_rgb).collect();
assert!(diffuse.iter().all(|[r, g, b]| *r <= 0.8 + 1e-6 && (g * 4.0 - r).abs() < 1e-4 && (b * 8.0 - r).abs() < 1e-4), "{diffuse:?}");
assert!(diffuse.windows(2).any(|w| !close3(w[0], w[1])), "{diffuse:?}");
// Plastic: the same faces, one of them lightened toward white by the highlight.
let shiny: Vec<[f32; 3]> =
classic_faces(json!({"highlightIntensity": 100, "highlightSize": 100, "lightHeight": 60})).iter().map(fill_rgb).collect();
let soft: Vec<[f32; 3]> = classic_faces(json!({"surface": "diffuse", "lightHeight": 60})).iter().map(fill_rgb).collect();
assert!(shiny.iter().zip(&soft).any(|(s, d)| s[1] > d[1] + 0.01), "{shiny:?} {soft:?}");
assert!(shiny.iter().zip(&soft).all(|(s, d)| s[1] >= d[1] - 1e-4));
// Without highlight, Plastic shades as Diffuse.
let dull: Vec<[f32; 3]> = classic_faces(json!({"highlightIntensity": 0, "lightHeight": 60})).iter().map(fill_rgb).collect();
assert!(dull.iter().zip(&soft).all(|(a, b)| close3(*a, *b)));
}
#[test]
fn classic_shading_color_blend_steps_and_lights() {
let base = json!({"surface": "diffuse", "ambient": 20});
let with = |extra: Value| {
let mut p = base.clone();
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
classic_faces(p).iter().map(fill_rgb).collect::<Vec<_>>()
};
// A custom shading color darkens toward that color; None keeps the paint.
assert!(with(json!({"shadingColor": "custom", "shadingCustom": "#0000ff"})).iter().any(|c| c[2] > c[0] / 8.0 + 0.01));
assert!(with(json!({"shadingColor": "none"})).iter().all(|c| close3(*c, RED.to_rgb())));
// One blend step: every face fully lit or fully in shadow.
let stepped = with(json!({"blendSteps": 1, "ambient": 0}));
assert!(stepped.iter().all(|c| close3(*c, RED.to_rgb()) || close3(*c, [0.0; 3])), "{stepped:?}");
// No light: the ambient share of the red. Two lights add up.
let dark = with(json!({"lightIntensity": 0, "blendSteps": 256}));
assert!(dark.iter().all(|c| (c[0] - 0.8 * 0.2).abs() < 0.01), "{dark:?}");
let one = with(json!({"lights": [{"intensity": 30}], "blendSteps": 256}));
let two = with(json!({"lights": [{"intensity": 30}, {"intensity": 30}], "blendSteps": 256}));
assert!(one.iter().zip(&two).all(|(a, b)| b[0] >= a[0] - 1e-4) && one.iter().zip(&two).any(|(a, b)| b[0] > a[0] + 0.01));
// Draw Hidden Faces keeps the faces turned away, drawn first.
assert_eq!(with(json!({"drawHiddenFaces": true})).len(), 6);
}
#[test]
fn classic_bevel_height_extent_and_cap_shape_the_solid() {
let bounds = |p: Value| solid_art(&classic(p)).unwrap().visual_bounds().unwrap();
let front = json!({"rotationX": 0, "rotationY": 0, "rotationZ": 0, "bevel": "classic", "bevelHeight": 10});
let out = bounds(front.clone());
let mut inside = front.clone();
inside["bevelInside"] = json!(true);
// Straight on, a bevel added around the shape widens it; one carved into it keeps its size.
assert!(out.width() > 100.0 + 15.0, "{out:?}");
let carved = bounds(inside);
assert!((carved.width() - 100.0).abs() < 2.0, "{carved:?}");
// Hollow: no caps, so the walls seen from inside show too.
assert!(classic_faces(json!({"cap": false})).len() > classic_faces(json!({})).len());
// The depth sets how far back the solid goes.
let side = json!({"rotationX": 0, "rotationY": 90, "rotationZ": 0});
let mut deep = side.clone();
deep["depth"] = json!(120);
assert!((bounds(deep).width() - 120.0).abs() < 1.0);
}
/// A gradient fill with a transparent stop is shaded opaque and masked by its own transparency, so
/// the shading does not cover the transparent part; an opaque gradient needs no mask.
#[test]
fn classic_shading_keeps_a_gradients_transparency() {
use vectorcraft_doc::color::{Gradient, GradientKind, GradientPaint, GradientStop};
for (end, masked) in [(0.25, true), (1.0, false)] {
let mut stop = GradientStop::new(1.0, RED);
stop.opacity = end;
let gradient = GradientPaint::new(Gradient::new(GradientKind::Linear, vec![GradientStop::new(0.0, RED), stop]));
let mut n = classic(json!({}));
if let Some(AppearanceItem::Fill(f)) = n.appearance.items.first_mut() {
f.paint = Paint::Gradient(Box::new(gradient));
}
let art = solid_art(&n).unwrap();
let surface = &art.children().unwrap()[0];
assert_eq!(surface.extras().mask.is_some(), masked, "{end}");
let mut opaque = true;
surface.walk(&mut |c| {
for item in &c.appearance.items {
if let AppearanceItem::Fill(f) = item
&& let Paint::Gradient(g) = &f.paint
{
opaque &= g.gradient.stops.iter().all(|s| s.opacity == 1.0);
}
}
});
assert!(opaque, "{end}: the faces are drawn opaque");
}
}
#[test]
fn classic_parameters_are_checked() {
let n = classic(json!({}));
let check = |p: Value| validate_solid(&n, CLASSIC_EXTRUDE, &crate::merged_params(CLASSIC_EXTRUDE, &p));
assert!(check(json!({})).is_ok());
assert!(check(json!({"surface": "wireframe", "shadingColor": "custom", "shadingCustom": "#12aBcd", "blendSteps": 256, "bevel": "ogee"})).is_ok());
for (p, says) in [
(json!({"surface": "glossy"}), "`surface` must be one of wireframe, none, diffuse, plastic"),
(json!({"shadingColor": "red"}), "`shadingColor` must be one of black, custom, none"),
(json!({"shadingCustom": "blue"}), "`shadingCustom` must be a #rrggbb color"),
(json!({"shadingCustom": "#fff"}), "`shadingCustom` must be a #rrggbb color"),
(json!({"depth": 2001}), "`depth` must be between 0 and 2000 (got 2001)"),
(json!({"bevelHeight": 0}), "`bevelHeight` must be between 1 and 100 (got 0)"),
(json!({"ambient": -1}), "`ambient` must be between 0 and 100 (got -1)"),
(json!({"blendSteps": 2.5}), "`blendSteps` must be a whole number between 1 and 256 (got 2.5)"),
(json!({"blendSteps": 0}), "`blendSteps` must be a whole number between 1 and 256 (got 0)"),
(json!({"highlightSize": "big"}), "`highlightSize` must be a number"),
(json!({"cap": "yes"}), "`cap` must be true or false"),
(json!({"drawHiddenFaces": 1}), "`drawHiddenFaces` must be true or false"),
(json!({"bevel": "fancy"}), "bevel must be none or one of"),
(json!({"twist": 10}), "unknown parameter `twist`"),
(json!({"lights": []}), "lights must contain one to ten lights"),
] {
let err = check(p.clone()).unwrap_err();
assert!(err.starts_with(says), "{p}: {err}");
}
}
+17 -4
View File
@@ -78,7 +78,7 @@ pub fn specs() -> Vec<CommandSpec> {
"3D Lighting Preset",
[],
None,
"{index: int, preset: standard|diffuse|topLeft|right, item?: appearance item index|null (omitted: the active item, else the object's), ids?|id?: [..], target?: object|contents, aligned?: bool (index and item are rows of the first target's stack: each other target edits its equal one, as the Appearance panel does for a mixed selection)} replace an applied 3D effect's light rig and ambient with an original lighting preset; preserve shading, geometry and view; all targets must have a 3D effect at index; one undo step → {ids}",
"{index: int, preset: standard|diffuse|topLeft|right, item?: appearance item index|null (omitted: the active item, else the object's), ids?|id?: [..], target?: object|contents, aligned?: bool (index and item are rows of the first target's stack: each other target edits its equal one, as the Appearance panel does for a mixed selection)} replace an applied 3D effect's light rig and ambient with an original lighting preset; preserve shading, geometry and view; all targets must have a 3D effect at index other than Extrude & Bevel (Classic), which has no lighting presets; one undo step → {ids}",
has_doc,
lighting_preset
),
@@ -505,6 +505,13 @@ fn lighting_preset(s: &mut Session, p: &Value) -> Result<Value> {
if !effect.is_some_and(|e| effects::is_three_d(&e.id)) {
return Err(bad(C, format!("object {} needs a 3D effect at index {index}", id.0)));
}
// Extrude & Bevel (Classic) has no lighting presets; its lights are set in its own options.
if effect.is_some_and(|e| e.id == effects::CLASSIC_EXTRUDE) {
return Err(bad(
C,
format!("object {}: Extrude & Bevel (Classic) at index {index} has no lighting presets; set its lights with effect.setParams", id.0),
));
}
}
let mut options = p.clone();
let map = options.as_object_mut().ok_or_else(|| bad(C, "parameters must be an object"))?;
@@ -516,7 +523,7 @@ fn lighting_preset(s: &mut Session, p: &Value) -> Result<Value> {
/// `effect.setParams`: merges `params` into the effect at `index` of the targets' active item, and
/// sets its Warp style (`warpStyle`), its 3D type (`threeDType`) or visibility (`visible`), as one
/// undo step. They are checked as `effect.apply` checks them, and a visible 3D effect must still
/// fit. → `{ids}`.
/// fit; a hidden one has its given values checked. → `{ids}`.
fn set_params(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "effect.setParams";
let index = index_param(p, "index", C)?;
@@ -574,7 +581,6 @@ fn set_params(s: &mut Session, p: &Value) -> Result<Value> {
}
if let Some(e) = effect
&& effects::is_three_d(&e.id)
&& visible != Some(false)
{
let to = kind.unwrap_or(&e.id);
let mut prospective =
@@ -582,7 +588,14 @@ fn set_params(s: &mut Session, p: &Value) -> Result<Value> {
if let (Some(cur), Value::Object(new)) = (prospective.as_object_mut(), effects::upgrade_params(to, &params)) {
cur.extend(new);
}
effects::validate_three_d(n, to, &prospective).map_err(|e| bad(C, e))?;
if visible != Some(false) {
effects::validate_three_d(n, to, &prospective).map_err(|e| bad(C, e))?;
} else {
// Hidden, the effect draws nothing, so its object need not fit. The values given are
// checked, and after a change of 3D type so is every value it keeps.
let checked = if to == e.id { effects::upgrade_params(to, &params) } else { prospective };
effects::validate_three_d_params(to, &checked).map_err(|e| bad(C, e))?;
}
}
}
let ids = edit_effects(s, p, item, C, "Effect Options", &format!("{C}: no effect at index {index}"), index, |fx, index| {
@@ -91,3 +91,27 @@ fn lighting_presets_reject_a_mixed_selection_before_changing_any_effect() {
assert_eq!(s.doc().unwrap().history.undo.len(), history);
assert!(!s.doc().unwrap().is_dirty());
}
#[test]
fn lighting_presets_refuse_extrude_and_bevel_classic_and_leave_a_mixed_selection_unchanged() {
let (mut s, first) = setup("threeD.rotate", Value::Null);
let classic = s.execute("shape.rectangle", &json!({"x":250,"y":100,"width":80,"height":80})).unwrap()["id"].as_u64().unwrap();
s.execute("effect.apply", &json!({"effect":"threeD.classicExtrude","item":null,"params":{"surface":"diffuse","lightRotation":40}})).unwrap();
for ids in [vec![classic], vec![first.0, classic]] {
for preset in LIGHTING_PRESETS {
s.doc_mut().unwrap().mark_saved();
let before = s.doc().unwrap().doc.clone();
let history = s.doc().unwrap().history.undo.len();
let p = json!({"ids":ids,"index":0,"item":null,"preset":preset.id});
let err = s.execute("effect.lightingPreset", &p).unwrap_err().to_string();
assert!(err.contains(&format!("object {classic}: Extrude & Bevel (Classic) at index 0 has no lighting presets")), "{err}");
assert_eq!(s.doc().unwrap().doc, before);
assert_eq!(s.doc().unwrap().history.undo.len(), history);
assert!(!s.doc().unwrap().is_dirty());
}
}
// Its lights are set with its own parameters instead.
s.execute("effect.setParams", &json!({"ids":[classic],"index":0,"item":null,"params":{"lightRotation":135,"lightHeight":45}})).unwrap();
let params = &s.doc().unwrap().doc.node(NodeId(classic)).unwrap().appearance.effects[0].params;
assert_eq!((params["lightRotation"].as_f64(), params["lightHeight"].as_f64()), (Some(135.0), Some(45.0)));
}
+50
View File
@@ -46,6 +46,56 @@ fn revolve_commands_keep_profile_live_edit_options_expand_and_undo() {
assert_eq!(effects_for_test(&s, id), before);
}
/// `effect.setParams` checks the values it is given for a 3D effect when the same edit hides it:
/// a bad value is refused and the document is left as it was, and good values are stored.
#[test]
fn hiding_a_3d_effect_still_checks_its_new_values() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
let rect = |s: &mut Session, x: u32| {
NodeId(s.execute("shape.rectangle", &json!({"x": x, "y": 100, "width": 60, "height": 60})).unwrap()["id"].as_u64().unwrap())
};
let classic = rect(&mut s, 20);
s.execute("effect.apply", &json!({"effect": effects::CLASSIC_EXTRUDE, "ids": [classic.0], "item": null})).unwrap();
let extrude = rect(&mut s, 120);
s.execute("effect.apply", &json!({"effect": effects::EXTRUDE, "ids": [extrude.0], "item": null})).unwrap();
let profile = NodeId(s.execute("path.create", &json!({"d": "M 300 40 L 330 60 L 330 140 L 300 160"})).unwrap()["id"].as_u64().unwrap());
s.execute("effect.apply", &json!({"effect": effects::REVOLVE, "ids": [profile.0], "item": null})).unwrap();
for (id, params, why) in [
(classic, json!({"depth": 2001}), "`depth` must be between 0 and 2000"),
(classic, json!({"surface": "glossy"}), "`surface` must be one of"),
(classic, json!({"bevel": "wavy"}), "bevel must be none or one of"),
(extrude, json!({"lights": []}), "lights must contain one to ten lights"),
(extrude, json!({"lightColor": "red"}), "lightColor must be an opaque RGB color"),
(profile, json!({"edge": "top"}), "Revolve edge must be left or right"),
] {
let before = s.doc().unwrap().doc.clone();
let undo = s.doc().unwrap().history.undo.len();
let p = json!({"ids": [id.0], "index": 0, "item": null, "params": params, "visible": false});
let e = s.execute("effect.setParams", &p).unwrap_err().to_string();
assert!(e.contains(why), "{params}: {e}");
assert_eq!(s.doc().unwrap().doc, before, "{params}");
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "{params}");
}
// A hidden switch of 3D type checks the values given with it for the new type.
let before = s.doc().unwrap().doc.clone();
let p = json!({"ids": [extrude.0], "index": 0, "item": null, "threeDType": "inflate", "params": {"lights": []}, "visible": false});
let e = s.execute("effect.setParams", &p).unwrap_err().to_string();
assert!(e.contains("lights must contain one to ten lights"), "{e}");
assert_eq!(s.doc().unwrap().doc, before);
// Hiding alone checks nothing.
s.execute("effect.setParams", &json!({"ids": [extrude.0], "index": 0, "item": null, "visible": false})).unwrap();
assert!(!node(&s, extrude).appearance.effects[0].visible);
s.execute("effect.setParams", &json!({"ids": [classic.0], "index": 0, "item": null, "params": {"depth": 2000}, "visible": false})).unwrap();
let e = &node(&s, classic).appearance.effects[0];
assert_eq!((e.params["depth"].as_f64(), e.visible), (Some(2000.0), false));
// A value the effect already holds is not checked again: a key it does not take, as a file
// can carry, does not stop a hidden edit of another value.
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(classic).unwrap().appearance.effects[0].params["extra"] = json!(1);
s.execute("effect.setParams", &json!({"ids": [classic.0], "index": 0, "item": null, "params": {"depth": 10}, "visible": false})).unwrap();
assert_eq!(node(&s, classic).appearance.effects[0].params["depth"].as_f64(), Some(10.0));
}
/// Extrude & Bevel, Inflate and Rotate: applied by command, saved and reopened, edited,
/// exported, expanded and undone; one 3D effect per object.
#[test]
+7 -3
View File
@@ -17,6 +17,8 @@
//! - `eps`: EPS export options (flattened transparency, previews, the embedded document), `document.exportEps`.
//! - `dxfimport`: the DXF options `document.open` and Place read, `document.dxfInfo`.
//! - `metafile`: EMF and WMF export, open and place.
//! - `importer`: files that importer plug-ins open (`document.open {importer}`, and extensions no
//! built-in format reads).
//! - `mesh3d`: 3D assets as OBJ, glTF, USDA and USDZ mesh files (`assets.export3D`).
//!
//! [`FORMATS`] is the single list of formats (append-only); open dialogs use [`open_filters`],
@@ -31,6 +33,7 @@ mod encode;
pub mod eps;
mod export;
pub(crate) mod imagemap;
mod importer;
mod legacy_ai;
mod load;
mod mesh3d;
@@ -136,9 +139,10 @@ pub(crate) use export_names::{
use export_names::{distinct_names_by, write_encoded};
use formats::{ARTBOARD, ARTBOARDS, RANGE, USE_ARTBOARDS, import_name, writable};
pub use formats::{
DOC_EXTS, DOCX_EXTS, FORMATS, Format, FormatOption, OPEN_EXTS, PLACE_EXTS, RTF_EXTS, TEXT_EXTS, UNASSOCIATED_EXTS, export_extensions, extension,
file_stem, format, format_for_name, has_own_title, library_open_filters, open_filters, place_filters, plugin_formats, read_extensions,
readable_format, readable_format_for_name, titled_for_export, writable_format,
DOC_EXTS, DOCX_EXTS, FORMATS, Format, FormatOption, OPEN_EXTS, PLACE_EXTS, RTF_EXTS, TEXT_EXTS, UNASSOCIATED_EXTS, builtin_open_filters,
export_extensions, extension, file_stem, format, format_for_name, has_own_title, importer_formats, library_open_filters, open_extensions,
open_filters, opens_as_other, opens_with_importer, place_filters, plugin_formats, read_extensions, readable_format, readable_format_for_name,
titled_for_export, writable_format,
};
#[cfg(test)]
+2 -2
View File
@@ -728,12 +728,12 @@ fn collect_files(folder: &str, include_subdirs: bool, depth: usize) -> Result<Ve
#[cfg(not(target_arch = "wasm32"))]
fn is_openable(path: &str) -> bool {
use super::{FORMATS, OPEN_EXTS, extension};
use super::{FORMATS, extension, open_extensions};
let ext = extension(path);
if ext.is_empty() {
return false;
}
OPEN_EXTS.iter().any(|e| *e == ext) || FORMATS.iter().any(|f| f.read && f.extensions.iter().any(|e| *e == ext))
open_extensions().iter().any(|e| *e == ext) || FORMATS.iter().any(|f| f.read && f.extensions.iter().any(|e| *e == ext))
}
#[cfg(test)]
+2 -2
View File
@@ -9,7 +9,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Open Document",
[],
None,
"{path | name | dataBase64 (a file's path, or its name with its bytes), pages?: \"2-3, 5\" (PDF/.ai: 1-based, default all; one artboard each) | page?: n, cropTo?: bounding|art|crop (default)|trim|bleed|media (the box each artboard gets; bounding: the art's bounds; trim: the bleed box round it becomes the document's bleed; a PDF-compatible .ai or a PDF saved with its editing data opens on its trim boxes for crop), password? (encrypted PDFs; see document.pdfInfo), textAs?: text (default: point type per line, lines wrapped in a frame as area type where they lay out the same, in the file's fonts by name; missing ones are listed in warnings) | outlines (glyph paths), layers?: true (default: optional content groups become layers with their visibility, print state and lock — art that is off comes in as a hidden layer; other art goes to a layer per page) | false (one layer per page, without the art that is off; Save then asks for a name), editingData?: true (default: an EPS, .ai or PDF that carries its editor's own copy of the art opens from it, with its layers, hidden objects, guides, artboards and the art outside them; a .ai or PDF whose type shows opens from its PDF part with textAs: outlines) | \"always\" (as true, and a .ai's editing copy is used even where it draws differently from its PDF part, which can be older; the warnings say so) | false (only what its page or PDF part prints, for print pipelines; a .ai saved without PDF compatibility then can't be opened), colorMode?: rgb|cmyk (the mode the document opens in, its colours converted as file.documentColorMode does, grays? as there; default: the file's — a PDF keeps CMYK, Gray and spot inks (spot swatches at a tint) and opens in CMYK when painted mostly in CMYK), grays?: as file.documentColorMode, dxf?: {layout?: \"Model\" (default) | a paper layout's name (document.dxfInfo lists them), unit?, scale? (the ratio: 1 unit of the art = scale drawing units; default: the drawing at 1:1 in its own unit), fit?: false (scale to fit an artboard of fitTo?: [612, 792], turned to the art's orientation), scaleLineweights?: false (lineweights scale with the art), center?: true (false: the drawing's origin on the artboard's bottom-left corner; fitted art's bottom-left corner), mergeLayers?: false (all art on one layer)} (DXF), crop?: as cropTo (its older name)} → {index, title, format, warnings, restored, missingLinks, modifiedLinks, updatedLinks: [{name, path, ids}]}; any readable format (see document.formats): .vectorcraft/.drawcraft, .svg/.svgz, .pdf/.ai, .ait, ASCII .dxf (each DXF layer with art a layer; blocks symbols; what can't come in is listed in warnings), PNG/JPEG/GIF/WebP/TIFF/BMP (an image opens as a document of its size at the resolution it declares, as file.place sizes it; 72 ppi, 1 px = 1 pt, when it declares none), .emf/.wmf (one artboard, the picture's frame; records VectorCraft doesn't read are skipped with one warning), .eps and PostScript .ai (one artboard, the bounding box: an EPS file VectorCraft wrote restores the document it carries, restored: true; other files are read by a PostScript interpreter — paths, colours and spot inks, clips, gradients, images, type in the fonts named — and come in as their TIFF preview, with a warning, when their PostScript can't be read). A PDF/.ai/.ait or SVG saved with Preserve Editing restores the native document it carries (restored: true; a PDF only when no pages are picked), unless the file was changed elsewhere since or the data can't be read: then its artwork is imported and the first warning says why. Templates (native templates, .ait) open as a new untitled document; a restored .ai keeps its path (Save writes .ai again) and opens on the artboard its editing copy names as active. Linked images are read from their files (looked for at their path, then relative to the document): missing ones show their saved preview (links.relink), modified ones are read again only with Preferences › Update Links: Automatically (else links.update). An SVG's <image> files (relative links from the SVG's folder) stay linked, SVG files become art, missing ones a placeholder in their box (a warning and missingLinks)",
"{path | name | dataBase64 (a file's path, or its name with its bytes), pages?: \"2-3, 5\" (PDF/.ai: 1-based, default all; one artboard each) | page?: n, cropTo?: bounding|art|crop (default)|trim|bleed|media (the box each artboard gets; bounding: the art's bounds; trim: the bleed box round it becomes the document's bleed; a PDF-compatible .ai or a PDF saved with its editing data opens on its trim boxes for crop), password? (encrypted PDFs; see document.pdfInfo), textAs?: text (default: point type per line, lines wrapped in a frame as area type where they lay out the same, in the file's fonts by name; missing ones are listed in warnings) | outlines (glyph paths), layers?: true (default: optional content groups become layers with their visibility, print state and lock — art that is off comes in as a hidden layer; other art goes to a layer per page) | false (one layer per page, without the art that is off; Save then asks for a name), editingData?: true (default: an EPS, .ai or PDF that carries its editor's own copy of the art opens from it, with its layers, hidden objects, guides, artboards and the art outside them; a .ai or PDF whose type shows opens from its PDF part with textAs: outlines) | \"always\" (as true, and a .ai's editing copy is used even where it draws differently from its PDF part, which can be older; the warnings say so) | false (only what its page or PDF part prints, for print pipelines; a .ai saved without PDF compatibility then can't be opened), colorMode?: rgb|cmyk (the mode the document opens in, its colours converted as file.documentColorMode does, grays? as there; default: the file's — a PDF keeps CMYK, Gray and spot inks (spot swatches at a tint) and opens in CMYK when painted mostly in CMYK), grays?: as file.documentColorMode, dxf?: {layout?: \"Model\" (default) | a paper layout's name (document.dxfInfo lists them), unit?, scale? (the ratio: 1 unit of the art = scale drawing units; default: the drawing at 1:1 in its own unit), fit?: false (scale to fit an artboard of fitTo?: [612, 792], turned to the art's orientation), scaleLineweights?: false (lineweights scale with the art), center?: true (false: the drawing's origin on the artboard's bottom-left corner; fitted art's bottom-left corner), mergeLayers?: false (all art on one layer)} (DXF), crop?: as cropTo (its older name), importer?: \"plugin.<id>\" (an importer plug-in's format id, see document.formats: it opens the file, which must have an extension it lists, also one a built-in format reads), params?: {…} (the importer's own parameters, see plugin.info)} → {index, title, format, warnings, restored, missingLinks, modifiedLinks, updatedLinks: [{name, path, ids}]}; any readable format (see document.formats): .vectorcraft/.drawcraft, .svg/.svgz, .pdf/.ai, .ait, ASCII .dxf (each DXF layer with art a layer; blocks symbols; what can't come in is listed in warnings), PNG/JPEG/GIF/WebP/TIFF/BMP (an image opens as a document of its size at the resolution it declares, as file.place sizes it; 72 ppi, 1 px = 1 pt, when it declares none), .emf/.wmf (one artboard, the picture's frame; records VectorCraft doesn't read are skipped with one warning), .eps and PostScript .ai (one artboard, the bounding box: an EPS file VectorCraft wrote restores the document it carries, restored: true; other files are read by a PostScript interpreter — paths, colours and spot inks, clips, gradients, images, type in the fonts named — and come in as their TIFF preview, with a warning, when their PostScript can't be read). A PDF/.ai/.ait or SVG saved with Preserve Editing restores the native document it carries (restored: true; a PDF only when no pages are picked), unless the file was changed elsewhere since or the data can't be read: then its artwork is imported and the first warning says why. Templates (native templates, .ait) open as a new untitled document; a restored .ai keeps its path (Save writes .ai again) and opens on the artboard its editing copy names as active. A file that no built-in format reads, by its content or its extension, opens with the installed importer plug-in that lists its extension (the one whose id sorts first, when several do): it is given the file, at most 128 MiB, and its objects become a new document whose artboard is their bounds; Save then asks for a name, and the warnings are the importer's. Linked images are read from their files (looked for at their path, then relative to the document): missing ones show their saved preview (links.relink), modified ones are read again only with Preferences › Update Links: Automatically (else links.update). An SVG's <image> files (relative links from the SVG's folder) stay linked, SVG files become art, missing ones a placeholder in their box (a warning and missingLinks)",
always,
load::open
),
@@ -126,7 +126,7 @@ pub fn specs() -> Vec<CommandSpec> {
"File Formats",
[],
None,
"{} → {formats: [{id, label, extensions, mime, read, write, raster, options: {name: {type, default, description}}, plugin?: the id of the exporter plug-in that adds the format (its id is plugin.<plug-in id>; see plugin.list)}], readable: [id…], writable: [id…], openExtensions: [ext…], unsupported: [{id, label, extensions, hint}] (formats asked for that can be neither opened nor written, such as DWG and PICT, with what to use instead)}",
"{} → {formats: [{id, label, extensions, mime, read, write, raster, options: {name: {type, default, description}}, plugin?: the id of the exporter plug-in (write only) or importer plug-in (read only) that adds the format (its id is plugin.<plug-in id>; see plugin.list)}], readable: [id…], writable: [id…], openExtensions: [ext…] (the importers' extensions that no built-in format reads included), unsupported: [{id, label, extensions, hint}] (formats asked for that can be neither opened nor written, such as DWG and PICT, with what to use instead)}",
always,
formats
),
+136 -23
View File
@@ -659,11 +659,12 @@ pub fn read_extensions(f: &Format) -> &'static [&'static str] {
READ_ALIASES.iter().find(|(id, _)| *id == f.id).map_or(f.extensions, |(_, exts)| *exts)
}
/// The readable format of extension `ext` (any case, leading dot allowed): [`format`]'s when it
/// reads, else the one [`READ_ALIASES`] gives it (`.dib` is BMP).
/// The built-in readable format of extension `ext` (any case, leading dot allowed): [`format`]'s
/// when it reads, else the one [`READ_ALIASES`] gives it (`.dib` is BMP). Importer plug-ins are
/// not looked at ([`importer_formats`]).
pub fn readable_format(ext: &str) -> Option<&'static Format> {
let k = ext.trim_start_matches('.').to_ascii_lowercase();
format(&k).filter(|f| f.read).or_else(|| READ_ALIASES.iter().find(|(_, exts)| exts.contains(&k.as_str())).and_then(|(id, _)| format(id)))
builtin(&k).filter(|f| f.read).or_else(|| READ_ALIASES.iter().find(|(_, exts)| exts.contains(&k.as_str())).and_then(|(id, _)| builtin(id)))
}
/// The readable format a file name's extension names ([`readable_format`]).
@@ -676,18 +677,110 @@ fn format_filters() -> impl Iterator<Item = (&'static str, &'static [&'static st
FORMATS.iter().filter(|f| f.read).map(|f| (f.label, read_extensions(f)))
}
/// Open-dialog filters: "All readable files" first, then one per readable format, then swatch
/// libraries (which open in the library panel), Libraries panel libraries, flattener, PDF, print
/// and export presets (imported), scripts (checked without running them) and plug-ins (installed).
/// Whether a file named `name` can only be read by an importer plug-in: an installed importer lists
/// its extension and File › Place does not read that extension.
pub fn opens_with_importer(name: &str) -> bool {
let ext = extension(name);
!ext.is_empty() && !PLACE_EXTS.contains(&ext.as_str()) && !vectorcraft_plugins::import::for_extension(&ext).is_empty()
}
/// Whether File › Open opens a file of extension `ext` (lower case) as something other than a
/// document: a plug-in (installed), a shortcut set, a script (checked), flattener, PDF, print,
/// perspective grid or export presets or composite fonts (imported), a Libraries panel library, or a
/// swatch or graphic style library.
pub fn opens_as_other(ext: &str) -> bool {
[
crate::cmd::plugin::EXTS,
crate::cmd::shortcutset::EXTS,
crate::cmd::scripts::EXTS,
crate::cmd::flatten::PRESET_EXTS,
crate::cmd::pdfcmds::PRESET_EXTS,
crate::cmd::printpresets::PRESET_EXTS,
crate::cmd::perspgrid::PRESET_EXTS,
screens::PRESET_FILE_EXTS,
crate::cmd::compositefonts::EXTS,
crate::cmd::library::LIBRARY_EXTS,
crate::cmd::swatchlib::LIBRARY_EXTS,
std::slice::from_ref(&vectorcraft_doc::style_libs::STYLES_EXT),
]
.iter()
.any(|exts| exts.contains(&ext))
}
/// File › Open's extensions and filters for the installed importer plug-ins.
type ImporterPart = (&'static [&'static str], Vec<(&'static str, &'static [&'static str])>);
/// File › Open's part for the installed importer plug-ins: the extensions of "All readable files"
/// ([`OPEN_EXTS`], then the importers' extensions in plug-in id order) and one filter per importer
/// that has extensions left, under its name. An importer's extensions leave out those a built-in
/// format reads, which File › Open reads with that format, and those File › Open opens as something
/// else ([`opens_as_other`]). Made once for each set of installed plug-ins, so it lives as long as
/// the dialogs that hold it.
fn importer_open_part() -> ImporterPart {
use vectorcraft_plugins::registry;
static MADE: std::sync::Mutex<Option<(u64, ImporterPart)>> = std::sync::Mutex::new(None);
let mut made = MADE.lock().unwrap_or_else(|e| e.into_inner());
let revision = registry::revision();
if let Some((r, part)) = made.as_ref()
&& *r == revision
{
return part.clone();
}
let mut exts: Vec<&'static str> = OPEN_EXTS.to_vec();
let mut filters = Vec::new();
for f in importer_formats() {
let own: Vec<&'static str> = f.extensions.iter().copied().filter(|e| readable_format(e).is_none() && !opens_as_other(e)).collect();
if own.is_empty() {
continue;
}
for &e in &own {
if !exts.contains(&e) {
exts.push(e);
}
}
let own: &'static [&'static str] = if own.len() == f.extensions.len() { f.extensions } else { Box::leak(own.into_boxed_slice()) };
filters.push((f.label, own));
}
let exts: &'static [&'static str] = if exts.len() == OPEN_EXTS.len() { OPEN_EXTS } else { Box::leak(exts.into_boxed_slice()) };
let part = (exts, filters);
*made = Some((revision, part.clone()));
part
}
/// Every extension File › Open reads as a document: [`OPEN_EXTS`], then the extensions the
/// installed importer plug-ins list that no built-in format reads and that File › Open doesn't open
/// as something else ([`opens_as_other`]), in plug-in id order.
pub fn open_extensions() -> &'static [&'static str] {
importer_open_part().0
}
/// File › Open's filters: "All readable files" first, then one per readable format, then one per
/// importer plug-in (under its name, with the extensions [`open_extensions`] takes from it), then
/// swatch libraries (which open in the library panel), Libraries panel libraries, flattener, PDF,
/// print and export presets (imported), scripts (checked without running them) and plug-ins
/// (installed).
pub fn open_filters() -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
std::iter::once(("All readable files", OPEN_EXTS))
.chain(format_filters())
.chain(std::iter::once(("Swatch libraries", crate::cmd::swatchlib::LIBRARY_EXTS)))
let (all, importers) = importer_open_part();
std::iter::once(("All readable files", all)).chain(format_filters()).chain(importers).chain(other_open_filters())
}
/// [`open_filters`] without the importer plug-ins: "All readable files" lists [`OPEN_EXTS`]. For
/// open dialogs whose files the built-in readers read (New from Template, Load Styles, a document
/// picked for its swatches, symbols, brushes or graphic styles).
pub fn builtin_open_filters() -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
std::iter::once(("All readable files", OPEN_EXTS)).chain(format_filters()).chain(other_open_filters())
}
/// The open-dialog filters after the document formats: swatch libraries, Libraries panel
/// libraries, presets, composite fonts, shortcut sets, scripts and plug-ins.
fn other_open_filters() -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
std::iter::once(("Swatch libraries", crate::cmd::swatchlib::LIBRARY_EXTS))
.chain(std::iter::once(("Libraries", crate::cmd::library::LIBRARY_EXTS)))
.chain(std::iter::once(("Flattener presets", crate::cmd::flatten::PRESET_EXTS)))
.chain(std::iter::once(("PDF presets", crate::cmd::pdfcmds::PRESET_EXTS)))
.chain(std::iter::once(("Print presets", crate::cmd::printpresets::PRESET_EXTS)))
.chain(std::iter::once(("Export presets", screens::PRESET_FILE_EXTS)))
.chain(std::iter::once(("Composite fonts", crate::cmd::compositefonts::EXTS)))
.chain(std::iter::once(("Shortcut sets", crate::cmd::shortcutset::EXTS)))
.chain(std::iter::once(("Scripts", crate::cmd::scripts::EXTS)))
.chain(std::iter::once(("Plug-ins", crate::cmd::plugin::EXTS)))
@@ -696,12 +789,13 @@ pub fn open_filters() -> impl Iterator<Item = (&'static str, &'static [&'static
/// Open-dialog filters for Swatch Libraries and Graphic Style Libraries Other Library… and Locate
/// Library…. The library formats come first so Linux file portals (which grey out files that don't
/// match the default filter) still list `.ase` and the other library extensions; then the same
/// filters as [`open_filters`] so a document can be picked for its swatches or graphic styles.
/// filters as [`builtin_open_filters`] so a document can be picked for its swatches or graphic
/// styles.
pub fn library_open_filters(
library_label: &'static str,
library_exts: &'static [&'static str],
) -> impl Iterator<Item = (&'static str, &'static [&'static str])> {
std::iter::once((library_label, library_exts)).chain(open_filters())
std::iter::once((library_label, library_exts)).chain(builtin_open_filters())
}
/// File → Place dialog filters: "All placeable files", then one per placeable format, then text,
@@ -715,26 +809,40 @@ pub fn place_filters() -> impl Iterator<Item = (&'static str, &'static [&'static
}
/// A format by id or extension (any case, leading dot allowed; `jpeg` finds `jpg`): a built-in
/// one, else one an exporter plug-in adds ([`plugin_formats`]). A built-in format keeps its
/// extension, so an exporter that lists it too is found by its format id.
/// one, else one a plug-in adds ([`plugin_formats`]), an exporter's before an importer's for an
/// extension both list. A built-in format keeps its extension, so an exporter or importer that
/// lists it too is found by its format id.
pub fn format(id_or_ext: &str) -> Option<&'static Format> {
let k = id_or_ext.trim_start_matches('.').to_ascii_lowercase();
FORMATS.iter().find(|f| f.id == k).or_else(|| FORMATS.iter().find(|f| f.extensions.contains(&k.as_str()))).or_else(|| {
builtin(&k).or_else(|| {
let added = plugin_formats();
added.iter().find(|f| f.id.eq_ignore_ascii_case(&k)).or_else(|| added.iter().find(|f| f.extensions.contains(&k.as_str()))).copied()
let by_ext = |f: &&&Format| f.extensions.contains(&k.as_str());
added
.iter()
.find(|f| f.id.eq_ignore_ascii_case(&k))
.or_else(|| added.iter().filter(|f| f.write).find(by_ext))
.or_else(|| added.iter().find(by_ext))
.copied()
})
}
/// The formats the installed exporter plug-ins add ([`vectorcraft_plugins::export`]), by plug-in
/// id: write only, with the plug-in's name as the label and its extensions. Each is made once for
/// each installation of its plug-in, its strings interned, so formats live as long as the catalogues
/// A built-in format by id or extension (lower case, without the dot).
fn builtin(k: &str) -> Option<&'static Format> {
FORMATS.iter().find(|f| f.id == k).or_else(|| FORMATS.iter().find(|f| f.extensions.contains(&k)))
}
/// The formats the installed exporter and importer plug-ins add ([`vectorcraft_plugins::export`],
/// [`vectorcraft_plugins::import`]), by plug-in id: an exporter's write only and an importer's read
/// only, with the plug-in's name as the label and its extensions. Each is made once for each
/// installation of its plug-in, its strings interned, so formats live as long as the catalogues
/// that hold them.
pub fn plugin_formats() -> Vec<&'static Format> {
use vectorcraft_plugins::{export, registry};
use vectorcraft_plugins::{Kind, export, registry};
static MADE: std::sync::Mutex<Vec<(u64, &'static Format)>> = std::sync::Mutex::new(Vec::new());
let mut made = MADE.lock().unwrap_or_else(|e| e.into_inner());
export::installed()
registry::list()
.iter()
.filter(|p| matches!(p.manifest().kind, Kind::Exporter | Kind::Importer))
.map(|p| {
if let Some((_, f)) = made.iter().find(|(serial, _)| *serial == p.serial()) {
return *f;
@@ -746,8 +854,8 @@ pub fn plugin_formats() -> Vec<&'static Format> {
label: registry::intern(&m.name),
extensions: Box::leak(extensions.into_boxed_slice()),
mime: "application/octet-stream",
read: false,
write: true,
read: m.kind == Kind::Importer,
write: m.kind == Kind::Exporter,
raster: false,
options: &[],
}));
@@ -757,6 +865,11 @@ pub fn plugin_formats() -> Vec<&'static Format> {
.collect()
}
/// The formats the installed importer plug-ins add ([`plugin_formats`] that read), by plug-in id.
pub fn importer_formats() -> Vec<&'static Format> {
plugin_formats().into_iter().filter(|f| f.read).collect()
}
/// The lower-case extension of a file name or path (empty when it has none).
pub fn extension(name: &str) -> String {
std::path::Path::new(name).extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default()
@@ -845,7 +958,7 @@ pub(super) fn formats(_: &mut Session, _: &Value) -> Result<Value> {
"formats": all().map(listed).collect::<Vec<_>>(),
"readable": ids(|f| f.read),
"writable": ids(|f| f.write),
"openExtensions": OPEN_EXTS,
"openExtensions": open_extensions(),
"unsupported": UNSUPPORTED.iter().map(Unsupported::to_json).collect::<Vec<_>>(),
}))
}
+170
View File
@@ -0,0 +1,170 @@
//! `document.open` of the files importer plug-ins read ([`vectorcraft_plugins::import`]).
//!
//! A file opens with an importer when `importer` names one (its format id, `plugin.<id>`), which
//! must list the file's extension, or when no built-in format reads the file, by its content or by
//! its extension, and an importer lists its extension (the one whose id sorts first, when several
//! do). A built-in format that can't read a damaged file reports its own error: the file is not
//! handed to an importer instead. Place reads only the built-in formats.
//!
//! The importer is given the file's bytes, at most [`import::MAX_INPUT_BYTES`] of them: a file
//! named by `path` that only an importer can read is read no further than that. Its objects become
//! a new document whose artboard is their bounds, and nothing opens when the importer fails,
//! declines, is cancelled or returns objects that don't pass the checks a filter's output does.
use std::sync::Arc;
use serde_json::Value;
use vectorcraft_doc::{Document, MAX_COORD};
use vectorcraft_geom::Rect;
use vectorcraft_plugins::{Plugin, export, import, objects};
use super::super::{Result, bad, str_param};
use super::load::{Loaded, detect, file_name};
use super::{err, extension, file_stem, format};
use crate::EngineError;
const C: &str = "document.open";
/// The importer `document.open`'s params `p` name (`importer`), if any; a value that isn't a
/// string is an error.
fn named(p: &Value) -> Result<Option<&str>> {
match p.get("importer").filter(|v| !v.is_null()) {
None => Ok(None),
Some(v) => v.as_str().map(Some).ok_or_else(|| bad(C, "`importer` must be a plug-in format id (see document.formats)")),
}
}
/// The importer that opens the file `name` holding `bytes` with `document.open`'s params `p`: the
/// one `importer` names, which must list the file's extension; else, when no built-in format reads
/// the file, the first installed importer that lists its extension; else `None`.
pub(super) fn pick(name: &str, bytes: &[u8], p: &Value) -> Result<Option<Arc<Plugin>>> {
let ext = extension(name);
if let Some(id) = named(p)? {
let plugin = import::named(id).map_err(|e| bad(C, e.to_string()))?;
let m = plugin.manifest();
if !m.extensions.contains(&ext) {
let shown = vectorcraft_plugins::permissions::display_text(&m.name, vectorcraft_plugins::permissions::MAX_SHOWN_NAME);
let exts: Vec<String> = m.extensions.iter().map(|e| format!(".{e}")).collect();
return Err(bad(C, format!("“{shown}” opens {} files, not `{}`", exts.join(", "), file_name(name))));
}
return Ok(Some(plugin));
}
if detect(name, bytes).is_some() {
return Ok(None);
}
Ok(import::for_extension(&ext).into_iter().next())
}
/// How much of a file [`detect`] is given: more than its content checks read.
const SNIFF_BYTES: usize = 64 << 10;
/// Refuses `document.open`'s `dataBase64` in `p` before it is decoded when it holds a file past
/// [`import::MAX_INPUT_BYTES`] that only an importer reads, by the rule of `read_path`.
pub(super) fn check_data(p: &Value) -> Result<()> {
let Some(b64) = str_param(p, "dataBase64") else {
return Ok(());
};
let name = str_param(p, "name").unwrap_or("Untitled");
let ext = extension(name);
let explicit = named(p)?.is_some();
let built_in = !explicit && (super::readable_format(&ext).is_some() || import::for_extension(&ext).is_empty());
if built_in || vectorcraft_format::base64_decoded_len(b64) <= import::MAX_INPUT_BYTES as u64 {
return Ok(());
}
if !explicit {
let start: Vec<u8> = b64.bytes().filter(|b| !b.is_ascii_whitespace()).take(SNIFF_BYTES / 3 * 4).collect();
let head = std::str::from_utf8(&start).ok().and_then(vectorcraft_format::base64_decode);
if head.is_some_and(|head| detect(name, &head).is_some()) {
return Ok(());
}
}
Err(err(format!("can't open `{}`: {}", file_name(name), import::too_large())))
}
/// The file at `path` for `document.open`'s params `p`. A file that only an importer reads (one
/// `importer` names, or an extension no built-in format reads that an importer lists) is read up
/// to [`import::MAX_INPUT_BYTES`]: past that, it is an error, unless the start of the file shows a
/// built-in format and no importer was named, which is read whole as any other file is. A file past
/// the limit that isn't read whole is reported from its size, without reading it.
#[cfg(not(target_arch = "wasm32"))]
pub(super) fn read_path(path: &str, p: &Value) -> Result<Vec<u8>> {
let ext = extension(path);
let explicit = named(p)?.is_some();
if !explicit && (super::readable_format(&ext).is_some() || import::for_extension(&ext).is_empty()) {
return super::read_file(path);
}
let head = super::read_file_head(path, SNIFF_BYTES)?;
if head.len() < SNIFF_BYTES {
return Ok(head);
}
if !explicit && detect(path, &head).is_some() {
return super::read_file(path);
}
let too_large = || err(format!("can't open `{}`: {}", file_name(path), import::too_large()));
let size = std::fs::metadata(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?.len();
if size > import::MAX_INPUT_BYTES as u64 {
return Err(too_large());
}
// The file can grow after the size is read.
let file = super::read_file_head(path, import::MAX_INPUT_BYTES + 1)?;
if file.len() > import::MAX_INPUT_BYTES {
return Err(too_large());
}
Ok(file)
}
#[cfg(target_arch = "wasm32")]
pub(super) fn read_path(path: &str, _: &Value) -> Result<Vec<u8>> {
super::read_file(path)
}
/// The file `name` holding `bytes` read by importer `plugin` with `document.open`'s params `p`
/// (`params`: the importer's own parameters) into a new document, its artboard the bounds of what
/// the importer returned.
pub(super) fn load(name: &str, bytes: &[u8], plugin: &Plugin, p: &Value) -> Result<Loaded> {
let m = plugin.manifest();
let format = format(&export::format_id(&m.id)).filter(|f| f.read).ok_or_else(|| err(vectorcraft_plugins::Error::NotFound(m.id.clone())))?;
let (ext, stem) = (extension(name), file_stem(name));
let source = import::Source { extension: &ext, name: &stem };
let params = p.get("params").cloned().unwrap_or(Value::Null);
let imported = import::run(plugin, bytes, &params, source).map_err(|e| match e {
vectorcraft_plugins::Error::Cancelled => EngineError::Cancelled,
vectorcraft_plugins::Error::Params(why) => bad(C, why),
e => err(format!("can't open `{}`: {e}", file_name(name))),
})?;
let mut doc = Document::new(1.0, 1.0);
let layer = doc.layers.first().map(|l| l.id);
let admit = |fx: &[vectorcraft_doc::Effect], n: &vectorcraft_doc::Node| crate::cmd::plugin::admit_effects_for(C, fx, n);
let ids =
objects::apply_output(&mut doc, &[], imported.objects, layer, &admit).map_err(|e| err(format!("can't open `{}`: {e}", file_name(name))))?;
if let (Some(bounds), Some(board)) = (doc.bounds_of(&ids, true), doc.artboards.first_mut()) {
board.rect = artboard_rect(bounds);
}
doc.title = file_name(name);
Ok(Loaded {
doc,
format,
warnings: imported.warnings,
restored: false,
converted: false,
preview_only: false,
editing_data: false,
native_pdf_compatible: None,
})
}
/// The artboard of art with bounds `r`: `r` within ±[`MAX_COORD`], at least 1 pt wide and high
/// about its center (the artboard of a single line or point). A side that would pass the edge
/// moves inward, so art at the edge keeps an artboard the document's edits accept.
fn artboard_rect(r: Rect) -> Rect {
let side = |lo: f64, hi: f64| {
let (lo, hi) = (lo.max(-MAX_COORD), hi.min(MAX_COORD));
if hi - lo >= 1.0 {
return (lo, hi);
}
let mid = ((lo + hi) / 2.0).clamp(-MAX_COORD + 0.5, MAX_COORD - 0.5);
(mid - 0.5, mid + 0.5)
};
let ((x0, x1), (y0, y1)) = (side(r.x0, r.x1), side(r.y0, r.y1));
Rect::new(x0, y0, x1, y1)
}
+15 -3
View File
@@ -567,10 +567,15 @@ impl ReadFile {
/// [`open_read`] opens. Type in the file's composite fonts is measured again once the document
/// opens and its composite fonts are registered. The linked files are looked for from `path`, where the file was read from,
/// and modified ones are read again when `update_links` (Preferences › File Handling › Update
/// Links set to Automatically; see [`crate::cmd::links::resolve`]).
/// Links set to Automatically; see [`crate::cmd::links::resolve`]). A file an importer plug-in
/// reads (`importer` in `p`, or an extension only an importer lists) is read by it
/// ([`super::importer`]).
pub fn read_for_open(name: &str, bytes: &[u8], path: Option<String>, p: &Value, update_links: bool, low_res_proxy_eps: bool) -> Result<ReadFile> {
let opts = LoadOptions::from_params("document.open", p)?;
let mut loaded = load_with(name, bytes, &opts)?;
let mut loaded = match super::importer::pick(name, bytes, p)? {
Some(plugin) => super::importer::load(name, bytes, &plugin, p)?,
None => load_with(name, bytes, &opts)?,
};
let links = crate::cmd::links::resolve(&mut loaded.doc, crate::cmd::links::LinkBase::file(path.as_deref()), update_links, low_res_proxy_eps);
Session::refresh_text_bounds(&mut loaded.doc);
Ok(ReadFile { loaded, opts, path, links })
@@ -681,7 +686,14 @@ pub(crate) fn source<'a>(p: &'a Value, cmd: &str) -> Result<Source<'a>> {
}
pub(super) fn open(s: &mut Session, p: &Value) -> Result<Value> {
let src = source(p, "document.open")?;
// A file only an importer plug-in reads is read up to the importer's input limit.
let src = match str_param(p, "path") {
Some(path) => Source { name: path, bytes: super::importer::read_path(path, p)?, path: Some(path) },
None => {
super::importer::check_data(p)?;
source(p, "document.open")?
}
};
// A relative path is the file in the working directory: its folder is where relative links
// are found (a bare file name has no folder of its own).
let path = src.path.map(super::absolute_path);
+25 -2
View File
@@ -377,8 +377,8 @@ fn open_exts_cover_every_readable_format() {
assert_eq!(filters[0], ("All readable files", OPEN_EXTS));
assert_eq!(
filters.len(),
10 + FORMATS.iter().filter(|f| f.read).count(),
"and swatch libraries, libraries, flattener, PDF, print and export presets, shortcut sets, scripts, plug-ins"
11 + FORMATS.iter().filter(|f| f.read).count(),
"and swatch libraries, libraries, flattener, PDF, print and export presets, composite fonts, shortcut sets, scripts, plug-ins"
);
assert!(filters.contains(&("Export presets", SCREEN_EXPORT_PRESET_EXTS)), "File › Open imports export presets");
assert!(filters.contains(&("Scripts", crate::cmd::scripts::EXTS)), "File › Open checks scripts");
@@ -891,3 +891,26 @@ fn a_photo_places_the_way_its_exif_orientation_says() {
let red = |p: &image::Rgb<u8>| p[0] > 150 && p[1] < 80;
assert!(red(px.get_pixel(4, 3)) && !red(px.get_pixel(4, 12)), "the left half is on top");
}
/// With `importer` named, a file up to the importer's limit is read whole and a larger one is an
/// error, whatever its content.
#[test]
fn read_path_for_a_named_importer_stops_at_the_limit() {
let limit = vectorcraft_plugins::import::MAX_INPUT_BYTES as u64;
let dir = tmp_dir("read-path-limit");
let path = dir.join("drawing.tuv");
let name = path.to_string_lossy().into_owned();
let named = json!({"importer": "plugin.test.notinstalled"});
let sized = |len: u64| {
std::fs::write(&path, b"\x89PNG\r\n\x1a\n").unwrap();
std::fs::OpenOptions::new().write(true).open(&path).unwrap().set_len(len).unwrap();
};
for len in [8, 64 << 10, 70_000, limit] {
sized(len);
assert_eq!(importer::read_path(&name, &named).unwrap().len() as u64, len);
}
sized(limit + 1);
let e = importer::read_path(&name, &named).unwrap_err();
assert!(e.to_string().contains("larger than") && e.to_string().contains("drawing.tuv"), "{e}");
let _ = std::fs::remove_dir_all(&dir);
}
+31 -3
View File
@@ -5,8 +5,8 @@
//! `stroke.set` whatever is selected), a graphic style clicked with nothing selected (more fills
//! and strokes, effects, opacity and blend mode, and the link to the style) or, with the Appearance
//! panel's New Art Has Basic Appearance off, the whole appearance of the last selection. With the
//! option on (the default) an inherited appearance gives new art its top stroke's options only.
//! `paint.default` resets it.
//! option on (the default) an inherited appearance gives new art its top stroke's options only,
//! and a change of selection loads only the proxies. `paint.default` resets it.
use std::borrow::Cow;
@@ -25,7 +25,7 @@ pub fn specs() -> Vec<CommandSpec> {
"New Art Has Basic Appearance",
["Window", "Appearance"],
None,
"{on?: bool (omitted: toggle)} the Appearance panel option (the preference newArtBasic, on by default): on, new art takes one fill and stroke (with the Stroke panel's options); off, the whole appearance of the last selected object (every fill, stroke and effect, opacity and blend mode) → {on}",
"{on?: bool (omitted: toggle)} the Appearance panel option (the preference newArtBasic, on by default): on, new art takes one fill and stroke (with the Stroke panel's options), the fill, stroke and weight the selection shares when it last changed; off, the whole appearance of the last selected object (every fill, stroke and effect, opacity and blend mode) → {on}",
always,
set_basic
),
@@ -213,6 +213,34 @@ impl Session {
let (ap, opacity, blend) = (super::style::captured(n), n.opacity, n.blend);
self.take_new_art(ap, opacity, blend, style, true);
}
/// After a command changed the selection, with New Art Has Basic Appearance on: the fill and
/// stroke proxies and the weight take what the selection shows in them, so new art looks like
/// it. A paint the selected objects don't share (a "?" proxy) stays as it was, and art without
/// paint (a placed image, whose None would make new art invisible) changes nothing. The
/// template is left alone, and without a stroke to show the weight stays too.
pub(crate) fn load_selection_paints(&mut self) {
if !self.prefs.new_art_basic {
return;
}
let has_paint = self.proxy_source(false).is_some_and(|(n, _)| matches!(n.kind, NodeKind::Text(_)) || !n.appearance.items.is_empty());
if !has_paint {
return;
}
let (fill_mixed, stroke_mixed) = self.proxy_mixed();
let (fill, stroke) = self.proxy_paints();
let weight_mixed = self.active().is_some_and(|st| st.stroke_mixed().weight);
let weight = self.shown_stroke().filter(|st| st.paint != Paint::None && !weight_mixed).map(|st| st.width);
if !fill_mixed {
self.paint.fill = unplaced(&fill);
}
if !stroke_mixed {
self.paint.stroke = unplaced(&stroke);
}
if let Some(width) = weight {
self.paint.stroke_width = width;
}
}
}
fn set_basic(s: &mut Session, p: &Value) -> Result<Value> {
+14 -1
View File
@@ -67,7 +67,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Character / Paragraph",
[],
None,
"{ids?|id?, kerning?: 1/1000 em|\"auto\"|\"optical\", baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg, underline?, strikethrough?, allCaps?, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), antiAlias?: \"none\"|\"sharp\"|\"crisp\"|\"strong\" (Character panel › Anti-aliasing Method, how the type's edges are drawn as pixels; it sets each targeted type object, with or without a range; each frame of a thread keeps its own method; new type: \"sharp\"), leftIndent?, rightIndent?, firstLineIndent?, spaceBefore?, spaceAfter?: pt (±1296), list?: \"none\"|\"bullet\"|\"number\", bullet?: \"none\"|\"dot\"|\"dash\"|\"circle\"|\"square\", number?: \"none\"|\"arabic\"|\"upperLetters\"|\"lowerLetters\"|\"upperRoman\"|\"lowerRoman\"|\"twoDigits\"|\"threeDigits\", listAlign?: \"left\"|\"right\", listLevel?: 1..9 (Paragraph panel › Bullets and Numbered List: each paragraph of a list starts with its marker, drawn in the font, size and color of the paragraph's first character, and its text starts at the next tab stop after the marker, the left indent counting as a stop; a bulleted list's marker is its bullet, and a numbered list's is the paragraph's number and a period: 1, 2, 3; A to Z, then AA, BB; a to z, then aa, bb; I, II, III; i, ii, iii; 01 to 99, then 100; 001 to 999, then 1000; letters and roman numerals past 3999 in Arabic digits; the numbers count the story's numbered paragraphs, its threaded frames included, from 1 after a paragraph in no list, and an empty or bulleted paragraph is not counted; list \"bullet\" keeps a list's bullet, else a dot, and list \"number\" keeps a numbered list's style, else arabic; without list, a number style makes a numbered list and a bullet makes a bulleted one; \"none\" in any of the three turns the list off; a paragraph that joins a list with left and first-line indents of 0 gets 18 and -18 unless the call sets an indent; a horizontal right-to-left list paragraph has its indents mirrored, measured from the right; listAlign \"right\" (Bullets and Numbering…'s Alignment) puts the marker against the text, and \"left\" (the default) puts it at the first line's start, the text starting at the same place with either; it sets the paragraphs that are in a list or that the call puts in one, and a list keeps it when it changes between bullets and numbers; listLevel (Tab and Shift+Tab at the start of a list paragraph, Bullets and Numbering…'s Level) is a whole number from 1 to 9 that sets the paragraphs in a list or that the call puts in one, a list keeping it when it changes between bullets and numbers, and a list turned on without listLevel starting at level 1; each level past the first sets the paragraph 18 pt further in than its left indent, and each level counts its own numbers: a list paragraph with text sets the counts of the levels deeper than its own back to 0, so a deeper numbered paragraph after it counts from 1, and a paragraph back at a shallower level goes on with that level's count), hyphenate?: bool, wordsLongerThan?: letters 1..25 (5), afterFirst?, beforeLast?: letters 1..15 (3), hyphenLimit?: lines 0..25 (0: no limit), hyphenationZone?: pt 0..1296 (0; Single-line Composer only), hyphenateCapitalized?: bool (true) (Paragraph panel menu › Hyphenation…: only words longer than wordsLongerThan break, keeping afterFirst letters before the hyphen and beforeLast after it; at most hyphenLimit lines in a row end with a hyphen; a line whose text already ends within hyphenationZone of its right edge breaks before the next word instead of hyphenating it; the counts are rounded, and every value is clamped to its range), wordSpacing?: % 0..1000 (80, 100, 133), letterSpacing?: % of a word space -100..500 (0, 0, 0), glyphScaling?: % 50..200 (100, 100, 100): each [min, desired, max] or {min?, desired?, max?} (Paragraph panel menu › Justification…: Desired applies to every line, Minimum and Maximum bound what justified lines may use; values are clamped to their range, and Minimum ≤ Desired ≤ Maximum must hold afterwards or nothing changes), autoLeading?: % of the size 0..500 (120; the leading of characters whose leading is Auto), singleWordJustification?: \"fullJustify\"|\"alignLeft\"|\"alignCenter\"|\"alignRight\" (a justified line holding one word), mojikumi?: \"none\"|\"lineEndHalf\" (Japanese punctuation spacing), kinsoku?: \"none\"|\"hard\"|\"soft\" (Kinsoku Set: Soft lets 々, ー and small kana start a line), direction?: \"auto\"|\"leftToRight\"|\"rightToLeft\" (paragraph direction; auto: from each paragraph's first strong character), leadingModel?: \"romanBaseline\"|\"emBoxTop\" (leading measured baseline to baseline, or em box top to top), charAlign?: \"romanBaseline\"|\"emBoxTop\"|\"emBoxCenter\"|\"emBoxBottom\"|\"icfTop\"|\"icfBottom\" (where characters smaller than the largest on their line line up with it), proportionalMetrics?: bool (full-width glyphs on the font's proportional widths: `pal Line truncated
"{ids?|id?, kerning?: 1/1000 em|\"auto\"|\"optical\", baselineShift?: pt, hScale?: %, vScale?: %, rotation?: deg, underline?, strikethrough?, allCaps?, smallCaps?: bool, position?: \"normal\"|\"superscript\"|\"subscript\" (sizes from Document Setup), antiAlias?: \"none\"|\"sharp\"|\"crisp\"|\"strong\" (Character panel › Anti-aliasing Method, how the type's edges are drawn as pixels; it sets each targeted type object, with or without a range; each frame of a thread keeps its own method; new type: \"sharp\"), leftIndent?, rightIndent?, firstLineIndent?, spaceBefore?, spaceAfter?: pt (±1296), list?: \"none\"|\"bullet\"|\"number\", bullet?: \"none\"|\"dot\"|\"dash\"|\"circle\"|\"square\", number?: \"none\"|\"arabic\"|\"upperLetters\"|\"lowerLetters\"|\"upperRoman\"|\"lowerRoman\"|\"twoDigits\"|\"threeDigits\", listAlign?: \"left\"|\"right\", listLevel?: 1..9 (Paragraph panel › Bullets and Numbered List: each paragraph of a list starts with its marker, drawn in the font, size and color of the paragraph's first character, and its text starts at the next tab stop after the marker, the left indent counting as a stop; a bulleted list's marker is its bullet, and a numbered list's is the paragraph's number and a period: 1, 2, 3; A to Z, then AA, BB; a to z, then aa, bb; I, II, III; i, ii, iii; 01 to 99, then 100; 001 to 999, then 1000; letters and roman numerals past 3999 in Arabic digits; the numbers count the story's numbered paragraphs, its threaded frames included, from 1 after a paragraph in no list, and an empty or bulleted paragraph is not counted; list \"bullet\" keeps a list's bullet, else a dot, and list \"number\" keeps a numbered list's style, else arabic; without list, a number style makes a numbered list and a bullet makes a bulleted one; \"none\" in any of the three turns the list off; a paragraph that joins a list with left and first-line indents of 0 gets 18 and -18 unless the call sets an indent; a horizontal right-to-left list paragraph has its indents mirrored, measured from the right; listAlign \"right\" (Bullets and Numbering…'s Alignment) puts the marker against the text, and \"left\" (the default) puts it at the first line's start, the text starting at the same place with either; it sets the paragraphs that are in a list or that the call puts in one, and a list keeps it when it changes between bullets and numbers; listLevel (Tab and Shift+Tab at the start of a list paragraph, Bullets and Numbering…'s Level) is a whole number from 1 to 9 that sets the paragraphs in a list or that the call puts in one, a list keeping it when it changes between bullets and numbers, and a list turned on without listLevel starting at level 1; each level past the first sets the paragraph 18 pt further in than its left indent, and each level counts its own numbers: a list paragraph with text sets the counts of the levels deeper than its own back to 0, so a deeper numbered paragraph after it counts from 1, and a paragraph back at a shallower level goes on with that level's count), hyphenate?: bool, wordsLongerThan?: letters 1..25 (5), afterFirst?, beforeLast?: letters 1..15 (3), hyphenLimit?: lines 0..25 (0: no limit), hyphenationZone?: pt 0..1296 (0; Single-line Composer only), hyphenateCapitalized?: bool (true) (Paragraph panel menu › Hyphenation…: only words longer than wordsLongerThan break, keeping afterFirst letters before the hyphen and beforeLast after it; at most hyphenLimit lines in a row end with a hyphen; a line whose text already ends within hyphenationZone of its right edge breaks before the next word instead of hyphenating it; the counts are rounded, and every value is clamped to its range), wordSpacing?: % 0..1000 (80, 100, 133), letterSpacing?: % of a word space -100..500 (0, 0, 0), glyphScaling?: % 50..200 (100, 100, 100): each [min, desired, max] or {min?, desired?, max?} (Paragraph panel menu › Justification…: Desired applies to every line, Minimum and Maximum bound what justified lines may use; values are clamped to their range, and Minimum ≤ Desired ≤ Maximum must hold afterwards or nothing changes), autoLeading?: % of the size 0..500 (120; the leading of characters whose leading is Auto), singleWordJustification?: \"fullJustify\"|\"alignLeft\"|\"alignCenter\"|\"alignRight\" (a justified line holding one word), mojikumi?: \"none\"|\"lineEndHalf\" (Japanese punctuation spacing), kinsoku?: \"none\"|\"hard\"|\"soft\" (Kinsoku Set: Soft lets 々, ー and small kana start a line), kinsokuType?: \"pushInFirst\"|\"pushOutFirst\"|\"pushOutOnly\" (Paragraph panel menu › Kinsoku Shori Type: how area type keeps those characters off a line's ends; pushInFirst sets them on the line when its gaps next to CJK characters can close up enough, by at most a tenth of an em each, pushOutFirst does so only when moving the character before them to the next line would leave the line more than an em short, and pushOutOnly, the default, moves it unless Burasagari hangs an overflowing com Line truncated
has_doc,
set_format
),
@@ -534,6 +534,7 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
"singleWordJustification",
"mojikumi",
"kinsoku",
"kinsokuType",
"direction",
"leadingModel",
"charAlign",
@@ -600,6 +601,15 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
_ => return Err(bad(C, "`kinsoku` must be \"none\", \"hard\" or \"soft\"")),
}),
};
let kinsoku_type = match p.get("kinsokuType") {
None => None,
Some(v) => Some(match v.as_str() {
Some("pushInFirst") => vectorcraft_doc::KinsokuShoriType::PushInFirst,
Some("pushOutFirst") => vectorcraft_doc::KinsokuShoriType::PushOutFirst,
Some("pushOutOnly") => vectorcraft_doc::KinsokuShoriType::PushOutOnly,
_ => return Err(bad(C, "`kinsokuType` must be \"pushInFirst\", \"pushOutFirst\" or \"pushOutOnly\"")),
}),
};
let direction = match p.get("direction") {
None => None,
Some(v) => Some(match v.as_str() {
@@ -851,6 +861,9 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(v) = kinsoku {
para.kinsoku = v;
}
if let Some(v) = kinsoku_type {
para.kinsoku_type = v;
}
if let Some(v) = direction {
para.direction = v;
}
+35 -15
View File
@@ -7,7 +7,8 @@
//! plug-in that fails leaves the document untouched), and read and write the data they keep with
//! the document ([`vectorcraft_doc::plugin_data`], theirs alone); live-effect plug-ins are applied
//! like any effect (`effect.apply {effect: "plugin.<id>"}`) and drawn by `vectorcraft-effects`;
//! exporters add a file format that `document.export` writes (`fileio::plugin_formats`).
//! exporters add a file format that `document.export` writes, and importers one that
//! `document.open` reads (`fileio::plugin_formats`).
//!
//! A filter may ask for a heavy budget (`Budget::Heavy`: minutes rather than a minute, for routing,
//! packing or simulation). It installs only once the user agrees (`plugin.install {allowHeavy}`),
@@ -65,7 +66,7 @@ pub fn describe(p: &Plugin) -> Value {
});
match m.kind {
Kind::Effect => v["effect"] = json!(effect::effect_id(&m.id)),
Kind::Exporter => {
Kind::Exporter | Kind::Importer => {
v["format"] = json!(export::format_id(&m.id));
v["extensions"] = json!(m.extensions);
}
@@ -158,6 +159,12 @@ impl FilterRun {
format!("`{}` is an exporter: export with document.export {{\"format\": \"{}\"}}", m.id, export::format_id(&m.id)),
));
}
Kind::Importer => {
return Err(bad(
C,
format!("`{}` is an importer: open a file with document.open {{\"importer\": \"{}\"}}", m.id, export::format_id(&m.id)),
));
}
}
Self::prepare_with(s, plugin, None, C, p)
}
@@ -446,8 +453,13 @@ pub(crate) fn run_command(s: &mut Session, id: &str, p: &Value) -> Result<Value>
/// Texturizer) comes as the effect stores it, never as a file to read: a plug-in can't have the
/// host read one.
fn admit_effects(fx: &[Effect], n: &Node) -> std::result::Result<Vec<Effect>, String> {
admit_effects_for("plugin.run", fx, n)
}
/// [`admit_effects`] for command `cmd`, which bad parameters name: `plugin.run`, or `document.open`
/// for the objects an importer plug-in returns.
pub(crate) fn admit_effects_for(cmd: &str, fx: &[Effect], n: &Node) -> std::result::Result<Vec<Effect>, String> {
use vectorcraft_render::effects;
const C: &str = "plugin.run";
let mut out = Vec::with_capacity(fx.len());
for (j, e) in fx.iter().enumerate() {
if n.appearance.effects.contains(e) {
@@ -460,7 +472,7 @@ fn admit_effects(fx: &[Effect], n: &Node) -> std::result::Result<Vec<Effect>, St
Some(plugin) => {
plugin.manifest().resolve_params(&params).map_err(|why| refuse(why.to_string()))?;
}
None => super::params::check_effect_params(C, &e.id, &params).map_err(|why| refuse(why.to_string()))?,
None => super::params::check_effect_params(cmd, &e.id, &params).map_err(|why| refuse(why.to_string()))?,
}
if super::effectcmd::texture_load_effect(&e.id)
&& let Some(t) = params.get(effects::pixel::TEXTURE_IMAGE)
@@ -535,8 +547,8 @@ fn takes_over(s: &Session, plugin: &Plugin) -> bool {
}
/// `plugin.install`: loads and checks the module, refuses one that asks for a heavy budget unless
/// `allowHeavy` says the user agreed, one that lists permissions the user hasn't granted its id
/// unless `grant` lists them all, one that would take another module's place ([`takes_over`])
/// `allowHeavy` says the user agreed, one that lists permissions the user hasn't granted this very
/// module unless `grant` lists them all, one that would take another module's place ([`takes_over`])
/// unless `replace` is true, and one whose SHA-256 isn't the `hash` given; records the grant with
/// the module's hash, keeps it for the next start and installs it → its `plugin.list` entry.
fn install(s: &mut Session, p: &Value) -> Result<Value> {
@@ -581,8 +593,7 @@ fn install(s: &mut Session, p: &Value) -> Result<Value> {
));
}
// A plug-in that lists permissions installs once the user grants them: whoever installs it
// asks them first, unless they already granted all of them (with a plug-in store, to this
// module).
// asks them first, unless they already granted this very module all of them.
let m = plugin.manifest();
let wanted = m.permissions();
if !s.plugin_approved(&plugin) && !grant.as_ref().is_some_and(|g| wanted.iter().all(|w| g.contains(w))) {
@@ -590,13 +601,22 @@ fn install(s: &mut Session, p: &Value) -> Result<Value> {
let asked = permissions::names(&wanted).join(", ");
return Err(bad(C, format!("“{name}” asks for the permissions {asked}: install it with `grant` listing them once the user agrees")));
}
// Installed plug-ins stay installed after a restart (#2541). The module is written first and its
// grant only once it is there; when the grant can't be written, the store gets the module it
// held back. A failed write of that module is only logged, and the grant then doesn't cover
// the new module. A stored module that can't be read back isn't replaced by one that needs a
// grant.
let previous = s.stored_plugin_bytes(plugin.id());
let mut stored = (previous.is_ok() || !m.asks_permissions()) && s.store_plugin(plugin.id(), &bytes);
if m.asks_permissions() {
let g = Grant { id: m.id.clone(), hash: plugin.hash().to_string(), permissions: wanted };
registry::grant(g.clone());
s.store_grant(&m.id, Some(g));
if stored && !s.store_grant(&m.id, Some(g)) {
s.put_back_plugin(plugin.id(), previous.ok().flatten().as_deref());
stored = false;
}
}
// Installed plug-ins stay installed after a restart (#2541), with their settings and cache.
let stored = s.store_plugin(plugin.id(), &bytes);
// It gets the settings and cache kept for its id.
s.attach_plugin_storage(plugin.id());
// Installing a disabled plug-in again (an updated version) turns it on for the next launch too,
// also when it is disabled by its stored file alone (it stopped a launch while loading). When
@@ -851,7 +871,7 @@ pub fn specs() -> Vec<CommandSpec> {
"List Plug-ins",
[],
None,
"{} → {plugins: [{id, name, version, kind: \"filter\"|\"effect\"|\"exporter\", author, description, params (doc), paramsSchema, effect? (the effect id of a live effect: plugin.<id>), format? and extensions? (an exporter's format id, plugin.<id>, for document.export, and its file extensions), budget?: \"heavy\" (a filter that may compute for longer, see plugin.install's allowHeavy), size, source, permissions: [name] (what it may do: the ones its manifest lists, else the ones its kind implies; progress: it may report its progress, see Cancel and write log lines; settings: it may keep settings of its own; cache: it may keep a cache of its own), hash (the module's SHA-256), api (the plug-in API level it was built for, 1 or 2), cacheBytes (the bytes its cache holds; see plugin.clearCache), commands?: [{id, command (its command id, plugin.<id>.<command id>, which runs it), label, params (doc), paramsSchema}] (the menu commands a filter or live effect adds)}], folder: the Additional Plug-ins Folder load report {folder, loaded, failed: [{file, error}]} | null, safeMode: bool (no plug-in loads or runs, see plugin.safeMode), disabled: [{id, file, name, label}] (turned off for the next launch, see plugin.setEnabled)}",
"{} → {plugins: [{id, name, version, kind: \"filter\"|\"effect\"|\"exporter\"|\"importer\", author, description, params (doc), paramsSchema, effect? (the effect id of a live effect: plugin.<id>), format? and extensions? (an exporter's or importer's format id, plugin.<id>, for document.export or document.open's importer, and its file extensions), budget?: \"heavy\" (a filter that may compute for longer, see plugin.install's allowHeavy), size, source, permissions: [name] (what it may do: the ones its manifest lists, else the ones its kind implies; progress: it may report its progress, see Cancel and write log lines; settings: it may keep settings of its own; cache: it may keep a cache of its own), hash (the module's SHA-256), api (the plug-in API level it was built for, 1 or 2), cacheBytes (the bytes its cache holds; see plugin.clearCache), commands?: [{id, command (its command id, plugin.<id>.<command id>, which runs it), label, params (doc), paramsSchema}] (the menu commands a filter or live effect adds)}], folder: the Additional Plug-ins Folder load report {folder, loaded, failed: [{file, error}]} | null, safeMode: bool (no plug-in loads or runs, see plugin.safeMode), disabled: [{id, file, name, label}] (turned off for the next launch, see plugin.setEnabled)}",
always,
list
),
@@ -878,7 +898,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Install Plug-in",
[],
None,
"{path: a .wasm file (desktop) | dataBase64: the module (+ name?: shown as its source), replace?: bool (true once the user agreed: a module with other bytes than the plug-in installed with its id, or with none installed, one whose id has settings or a cache in the plug-in store, installs only with it, takes that plug-in's place and gets its settings and cache; false: one whose id is installed is refused; left out, the same module installs again), name?: file name (with dataBase64), allowHeavy?: bool (true once the user agreed to a heavy budget: a filter whose manifest asks for one, budget \"heavy\" in plugin.inspect, installs only with it), grant?: [permission] (the permissions the user granted: a plug-in whose manifest lists permissions, needsApproval in plugin.inspect, installs only when grant holds every one of them; the grant is recorded with the module's hash, and replacing the plug-in with one that lists no new permission needs none; with a plug-in store attached, only the grants kept there count, each for the module with its hash alone), hash?: string (the module's SHA-256 as plugin.inspect gave it: a module with other bytes is refused)} validate and install a WebAssembly plug-in for this session → its plugin.list entry",
"{path: a .wasm file (desktop) | dataBase64: the module (+ name?: shown as its source), replace?: bool (true once the user agreed: a module with other bytes than the plug-in installed with its id, or with none installed, one whose id has settings or a cache in the plug-in store, installs only with it, takes that plug-in's place and gets its settings and cache; false: one whose id is installed is refused; left out, the same module installs again), name?: file name (with dataBase64), allowHeavy?: bool (true once the user agreed to a heavy budget: a filter whose manifest asks for one, budget \"heavy\" in plugin.inspect, installs only with it), grant?: [permission] (the permissions the user granted: a plug-in whose manifest lists permissions, needsApproval in plugin.inspect, installs only when grant holds every one of them; the grant is recorded with the module's hash and covers only that module, so a module with other bytes needs a new grant even when it lists the same permissions; with a plug-in store attached, only the grants kept there count), hash?: string (the module's SHA-256 as plugin.inspect gave it: a module with other bytes is refused)} validate and install a WebAssembly plug-in for this session → its plugin.list entry",
always,
install
),
@@ -887,7 +907,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Inspect Plug-in",
[],
None,
"{path: a .wasm file (desktop) | dataBase64: the module, name?: the file name shown as its source (with dataBase64)} validate a WebAssembly plug-in without installing it → the plugin.list entry it would have, plus needsApproval: bool (true when it lists permissions the user hasn't granted its id: plugin.install then needs grant) and needsReplace: bool (true when it would take the place of another module with its id: plugin.install then needs replace: true)",
"{path: a .wasm file (desktop) | dataBase64: the module, name?: the file name shown as its source (with dataBase64)} validate a WebAssembly plug-in without installing it → the plugin.list entry it would have, plus needsApproval: bool (true when it lists permissions the user hasn't granted this very module, its bytes included: plugin.install then needs grant) and needsReplace: bool (true when it would take the place of another module with its id: plugin.install then needs replace: true)",
always,
inspect
),
@@ -914,7 +934,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Reload Plug-ins",
["Object", "Plug-ins"],
None,
"{path?: a folder (default: the Additional Plug-ins Folder preference)} install every *.wasm in it (desktop; a bad module, and one disabled with plugin.setEnabled, is reported and skipped, and so are a heavy filter and a plug-in that lists permissions the user hasn't granted its id; an error in Safe Mode) → {folder, loaded: [id], failed: [{file, error}]}",
"{path?: a folder (default: the Additional Plug-ins Folder preference)} install every *.wasm in it (desktop; a bad module, and one disabled with plugin.setEnabled, is reported and skipped, and so are a heavy filter and a plug-in that lists permissions the user hasn't granted this very module, its bytes included; an error in Safe Mode) → {folder, loaded: [id], failed: [{file, error}]}",
always,
reload
),
+1
View File
@@ -226,6 +226,7 @@ pub fn para_attrs(v: &ParaStyle) -> Map<String, Value> {
("leading_model", json!(v.leading_model)),
("burasagari", json!(v.burasagari)),
("kinsoku", json!(v.kinsoku)),
("kinsoku_type", json!(v.kinsoku_type)),
("roman_hanging", json!(v.roman_hanging)),
("kashida", json!(v.kashida)),
] {
@@ -98,6 +98,43 @@ fn kinsoku_set_is_set_per_paragraph_and_saved_only_when_not_hard() {
assert_eq!(old.kinsoku, Kinsoku::Hard);
}
/// Paragraph panel menu › Kinsoku Shori Type: `text.setFormat {kinsokuType}` sets it per
/// paragraph in one undo step, refuses unknown values, saves it only when it is not Push Out Only
/// (new type's and older documents' type), reopens with the file, and paragraph styles carry it.
#[test]
fn kinsoku_shori_type_is_set_per_paragraph_and_saved_only_when_not_push_out_only() {
use vectorcraft_doc::KinsokuShoriType;
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
let id = s.execute("text.create", &json!({"x": 10, "y": 10, "text": "あ」\nい", "area": {"width": 100, "height": 100}})).unwrap()["id"]
.as_u64()
.unwrap();
let text = |s: &Session| match &s.doc().unwrap().doc.node(NodeId(id)).unwrap().kind {
NodeKind::Text(t) => t.clone(),
_ => panic!("text"),
};
assert_eq!(text(&s).para_at(0).kinsoku_type, KinsokuShoriType::PushOutOnly);
assert!(serde_json::to_value(text(&s).para_at(0)).unwrap().get("kinsoku_type").is_none());
s.execute("text.setFormat", &json!({"id": id, "start": 0, "end": 1, "kinsokuType": "pushInFirst"})).unwrap();
assert_eq!((text(&s).para_at(0).kinsoku_type, text(&s).para_at(1).kinsoku_type), (KinsokuShoriType::PushInFirst, KinsokuShoriType::PushOutOnly));
assert_eq!(serde_json::to_value(text(&s).para_at(0)).unwrap()["kinsoku_type"], "pushInFirst");
let reopened = vectorcraft_format::load(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap();
let Some(NodeKind::Text(back)) = reopened.node(NodeId(id)).map(|n| &n.kind) else { panic!("text") };
assert_eq!((back.para_at(0).kinsoku_type, back.para_at(1).kinsoku_type), (KinsokuShoriType::PushInFirst, KinsokuShoriType::PushOutOnly));
assert!(s.execute("text.setFormat", &json!({"id": id, "kinsokuType": "pushIn"})).is_err());
assert_eq!(text(&s).para_at(0).kinsoku_type, KinsokuShoriType::PushInFirst, "an unknown value changes nothing");
s.execute("text.setFormat", &json!({"id": id, "kinsokuType": "pushOutFirst"})).unwrap();
assert_eq!(text(&s).para_at(1).kinsoku_type, KinsokuShoriType::PushOutFirst);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!((text(&s).para_at(0).kinsoku_type, text(&s).para_at(1).kinsoku_type), (KinsokuShoriType::PushInFirst, KinsokuShoriType::PushOutOnly));
// A paragraph style carries it.
s.execute("paraStyle.new", &json!({"name": "Pushed in", "attrs": {"kinsoku_type": "pushOutFirst"}})).unwrap();
s.execute("paraStyle.apply", &json!({"name": "Pushed in", "id": id})).unwrap();
assert_eq!(text(&s).para_at(1).kinsoku_type, KinsokuShoriType::PushOutFirst);
let old: vectorcraft_doc::ParaStyle = serde_json::from_value(json!({"justify": "Left"})).unwrap();
assert_eq!(old.kinsoku_type, KinsokuShoriType::PushOutOnly);
}
#[test]
fn burasagari_is_set_per_paragraph_and_saved_only_when_on() {
use vectorcraft_doc::Burasagari;
+19 -1
View File
@@ -19,12 +19,20 @@ pub struct HistoryEntry {
next: usize,
/// The template and name `label` was made from ([`Session::edit_named`]).
pub(crate) named: Option<NamedStep>,
/// A number no other step takes ([`History::step`]).
serial: u64,
/// Original and converted new-art colors, only for steps that change document color mode.
pub(crate) color_mode_paints: Option<(vectorcraft_tools::PaintDefaults, vectorcraft_tools::PaintDefaults)>,
/// Owned new-art snapshots included in both directional estimates.
color_mode_bytes: usize,
}
/// The next [`HistoryEntry::serial`]: 1 for the first step made, so 0 never names a step.
fn next_serial() -> u64 {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
/// A step name made by filling the `{name}` placeholder of a template with a name
/// ([`Session::edit_named`]).
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -57,6 +65,7 @@ impl HistoryEntry {
next_bytes,
next: std::ptr::from_ref(next) as usize,
named: None,
serial: next_serial(),
color_mode_paints: None,
color_mode_bytes: 0,
}
@@ -88,6 +97,7 @@ impl HistoryEntry {
next_bytes: costs.bytes,
next: Arc::as_ptr(now) as usize,
named: None,
serial: next_serial(),
color_mode_paints: None,
color_mode_bytes: 0,
}
@@ -155,6 +165,13 @@ impl History {
self.undo.iter().chain(&self.redo).map(|e| e.bytes).sum()
}
/// Which step the document is at: the newest undo step and the next redo step, by numbers no
/// other step takes (0 for none). Every new step, Undo, Redo and edit merged into the newest
/// step (an undo group) changes it; a selection change does not.
pub fn step(&self) -> (u64, u64) {
(self.undo.last().map_or(0, |e| e.serial), self.redo.last().map_or(0, |e| e.serial))
}
/// The document at row `row` of the History panel, `now` being the document now: the rows before
/// it are the undo steps, oldest first, and the rows after it the redo steps, nearest first.
pub(crate) fn state<'a>(&'a self, now: &'a Arc<Document>, row: usize) -> Option<&'a Arc<Document>> {
@@ -192,13 +209,14 @@ impl History {
}
/// Estimate the newest undo step again, its next state now being `now` (the steps after it were
/// merged into it, or an undo group extended it).
/// merged into it, or an undo group extended it). It takes a new number ([`History::step`]).
pub(crate) fn recost_last(&mut self, now: &Document) {
if let Some(e) = self.undo.last_mut() {
let (before, after) = e.doc.retained_bytes_each(now);
e.bytes = before.saturating_add(e.color_mode_bytes);
e.next_bytes = after.saturating_add(e.color_mode_bytes);
e.next = std::ptr::from_ref(now) as usize;
e.serial = next_serial();
}
}
}
+4
View File
@@ -62,6 +62,10 @@ pub struct Session {
/// While Cmd lends `tool` (a selection tool) for a drag: the tool it was lent to, which comes
/// back as it was at the release ([`Session::pointer`]).
pub(crate) lender: Option<Box<dyn Tool>>,
/// The active document (its uid) and its [`History::step`] when the tool last handled a pointer
/// event or drew its overlays: when they differ at the next one, the tool is told first
/// ([`vectorcraft_tools::Tool::history_moved`]). None until the first.
pub(crate) tool_step: Option<(u64, (u64, u64))>,
/// The selection of the active document (its uid) as Cmd lent `tool`: what the lent tool's
/// gesture found, so that one that leaves it as it was picks nothing.
pub(crate) selection_at_lend: Option<(u64, vectorcraft_doc::Selection)>,
+1
View File
@@ -46,6 +46,7 @@ impl Session {
journal: Journal::default(),
tool: vectorcraft_tools::create("selection"),
lender: None,
tool_step: None,
selection_at_lend: None,
lent_selection: None,
last_selection_tool: None,
+6
View File
@@ -113,6 +113,9 @@ impl Session {
})
.flatten();
let selected_before = select_first.as_ref().and(self.active()).map(|d| (d.uid, d.selection.clone(), d.active_layer));
// Only a selection command that changes the selection loads its paints for new art: other
// commands, and clicking what is already selected, keep a sampled paint in the proxies.
let selection_was = (id.starts_with("select.") || id == "layer.target").then(|| self.active().map(|d| d.selection.clone())).flatten();
let run = |s: &mut Session| {
if let Some(ids) = &select_first {
s.execute("select.set", &serde_json::json!({ "ids": ids }))?;
@@ -150,6 +153,9 @@ impl Session {
});
if !restored {
self.inherit_new_art();
if selection_was.is_some_and(|was| self.active().is_some_and(|d| d.selection != was)) {
self.load_selection_paints();
}
}
for uid in std::mem::take(&mut self.batch_paint_docs) {
if let Some(st) = self.docs.iter_mut().find(|d| d.uid == uid)
+44 -18
View File
@@ -289,8 +289,8 @@ impl Session {
/// Install the plug-ins kept in the plug-in folder: none in Safe Mode, and none the user
/// disabled (matched by file name, which is made from the id). The launch marker names each
/// file while it loads. A module that doesn't load, and one that lists permissions the grants
/// there don't grant it, is skipped with a log record. A grant covers only the module it was
/// given to ([`grant_covers`]).
/// there don't grant that very module ([`grant_covers`]), is skipped with a log record, and
/// stays in the folder with its grant.
pub fn load_stored_plugins(&mut self) {
let Some(dir) = self.plugin_dir.clone() else { return };
if safe_mode::active() {
@@ -376,9 +376,9 @@ impl Session {
written && merged.complete()
}
/// Whether `plugin` installs without asking the user: it lists no permissions, or its id was
/// granted every one it lists, in the grants of the attached plug-in store when there is one
/// (for this module alone, [`grant_covers`]), else in this process's
/// Whether `plugin` installs without asking the user: it lists no permissions, or this very
/// module (its id and SHA-256) was granted every one it lists, in the grants of the attached
/// plug-in store when there is one ([`grant_covers`]), else in this process's
/// ([`vectorcraft_plugins::registry::approved`]).
pub(crate) fn plugin_approved(&self, plugin: &vectorcraft_plugins::Plugin) -> bool {
self.plugin_approver()(plugin)
@@ -403,6 +403,29 @@ impl Session {
self.plugin_dir.as_deref().is_some_and(|dir| put(dir, &format!("{}.{PLUGIN_EXT}", file_stem(id)), bytes))
}
/// The module the store holds for the plug-in `id`: `None` without a folder or such a file, and
/// an error, with a log record, when the file can't be read or is larger than the module limit.
pub(crate) fn stored_plugin_bytes(&self, id: &str) -> Result<Option<Vec<u8>>, String> {
let Some(dir) = self.plugin_dir.as_deref() else { return Ok(None) };
let path = dir.join(format!("{}.{PLUGIN_EXT}", file_stem(id)));
if !path.exists() {
return Ok(None);
}
let max = vectorcraft_plugins::Limits::default().max_module_bytes as u64;
read_capped(&path, max).map(Some).inspect_err(|e| log::warn!("can't read the stored plug-in {}: {e}", path.display()))
}
/// Return the store's module for the plug-in `id` to `previous`, the bytes it held before a
/// write ([`Session::stored_plugin_bytes`]): with `None`, the file is removed.
pub(crate) fn put_back_plugin(&self, id: &str, previous: Option<&[u8]>) {
match previous {
Some(bytes) => {
self.store_plugin(id, bytes);
}
None => self.unstore_plugin(id),
}
}
/// The file the plug-in `id` is stored in, spelled as [`Session::load_stored_plugins`] names it
/// (`None` without a plug-in folder).
pub(crate) fn stored_plugin_file(&self, id: &str) -> Option<String> {
@@ -413,14 +436,15 @@ impl Session {
/// other grants as they are; with none left, remove the file. Entries this version can't use
/// are written back as they were, except one for `id`. A grants file that is too large, can't
/// be read or doesn't hold a list is left as it is, with a log record, and nothing is recorded;
/// so is one whose new content would be larger than `MAX_GRANTS_FILE`.
pub(crate) fn store_grant(&self, id: &str, grant: Option<vectorcraft_plugins::permissions::Grant>) {
let Some(dir) = &self.plugin_dir else { return };
/// so is one whose new content would be larger than `MAX_GRANTS_FILE`. → whether the store now
/// holds what was asked (`false` without a folder).
pub(crate) fn store_grant(&self, id: &str, grant: Option<vectorcraft_plugins::permissions::Grant>) -> bool {
let Some(dir) = &self.plugin_dir else { return false };
let mut file = match read_grants(dir) {
Ok(file) => file,
Err(e) => {
log::warn!("not updating the stored plug-in grants {}: {e}", dir.join(GRANTS_FILE).display());
return;
return false;
}
};
let others = file.other.len();
@@ -430,22 +454,23 @@ impl Session {
None => file.grants.remove(id).is_some(),
} || file.other.len() != others;
if !changed {
return;
return true;
}
let mut entries: Vec<serde_json::Value> = file.grants.into_values().filter_map(|g| serde_json::to_value(g).ok()).collect();
entries.extend(file.other);
if entries.is_empty() {
take(dir, GRANTS_FILE);
return;
return true;
}
// The file is read back only up to its cap: written compactly when pretty-printed is larger,
// and not at all when that is too.
let fits = |bytes: &Vec<u8>| bytes.len() as u64 <= MAX_GRANTS_FILE;
match serde_json::to_vec_pretty(&entries).ok().filter(fits).or_else(|| serde_json::to_vec(&entries).ok().filter(fits)) {
Some(bytes) => {
put(dir, GRANTS_FILE, &bytes);
Some(bytes) => put(dir, GRANTS_FILE, &bytes),
None => {
log::warn!("not updating the stored plug-in grants {}: {}", dir.join(GRANTS_FILE).display(), too_large(MAX_GRANTS_FILE));
false
}
None => log::warn!("not updating the stored plug-in grants {}: {}", dir.join(GRANTS_FILE).display(), too_large(MAX_GRANTS_FILE)),
}
}
@@ -577,12 +602,13 @@ fn io(e: &std::io::Error) -> StorageError {
StorageError::Io(e.to_string())
}
/// Does grant `g` cover `plugin`: was it given to this module (the same SHA-256) for every
/// Does grant `g` cover `plugin`: was it given to this module (the same id and SHA-256) for every
/// permission the module lists? A module with other bytes under the id needs the user's approval
/// again, and so doesn't get the settings and cache kept for the id without it, and so does every
/// module when the grant records no hash.
/// again, and so doesn't get the settings and cache kept for the id without it. A grant whose hash
/// is missing or not a valid SHA-256 covers no module.
fn grant_covers(g: &vectorcraft_plugins::permissions::Grant, plugin: &vectorcraft_plugins::Plugin) -> bool {
!g.hash.is_empty() && g.hash == plugin.hash() && g.covers(&plugin.manifest().permissions())
let m = plugin.manifest();
g.covers(&m.id, plugin.hash(), &m.permissions())
}
/// Is `path` a link (a symbolic link or, on Windows, a junction)?
+97 -1
View File
@@ -1,6 +1,6 @@
//! Blend Options (defaults with nothing selected, `object.blend.info`) and the Blend tool:
//! start points from clicked anchors, adding keys to a blend instead of nesting it (M8.18), and
//! Escape dropping the first pick.
//! Escape, or an edit made outside the tool, dropping the first pick.
use serde_json::{Value, json};
use vectorcraft_doc::live::{BlendOrientation, BlendSpacing, BlendSpec};
@@ -233,6 +233,102 @@ fn escape_drops_the_blend_tools_first_pick() {
assert_eq!(spec(&s, g).0, vec![b, c], "a was not added");
}
#[test]
fn an_edit_undo_or_redo_made_outside_the_blend_tool_drops_its_first_pick() {
for case in ["edit", "undo", "redo", "group"] {
let undo = case == "undo";
let mut s = session();
let (a, b, c) = (rect(&mut s, 0.0, 0.0, 40.0), rect(&mut s, 200.0, 0.0, 40.0), rect(&mut s, 400.0, 0.0, 40.0));
if case == "redo" {
s.execute("paint.setFill", &json!({"ids": [c.0], "color": "#ff0000"})).unwrap();
s.execute("edit.undo", &json!({})).unwrap();
}
// In an open undo group, the edit after the pick is merged into the group's step.
if case == "group" {
s.begin_undo_group();
s.execute("paint.setFill", &json!({"ids": [c.0], "color": "#00ff00"})).unwrap();
}
s.select_tool("blend", ViewInfo::default()).unwrap();
let click = |s: &mut Session, x: f64, y: f64| s.pointer(&PointerEvent::new(PointerKind::Down, x, y), ViewInfo::default()).unwrap();
// Undo takes away c, the object picked first; an edit or a redo recolors c, which is not picked.
click(&mut s, if undo { 420.0 } else { 20.0 }, 20.0);
assert!(!s.overlays(ViewInfo::default()).is_empty());
match case {
"undo" => s.execute("edit.undo", &json!({})).unwrap(),
"redo" => s.execute("edit.redo", &json!({})).unwrap(),
_ => s.execute("paint.setFill", &json!({"ids": [c.0], "color": "#ff0000"})).unwrap(),
};
assert!(s.overlays(ViewInfo::default()).is_empty(), "{case}: the pick is not marked");
let (layers, depth) = (s.doc().unwrap().doc.layers.clone(), s.doc().unwrap().history.undo.len());
click(&mut s, 220.0, 20.0);
assert_eq!(s.doc().unwrap().selection.objects, vec![b], "{case}: b is picked first");
assert_eq!((&s.doc().unwrap().doc.layers, s.doc().unwrap().history.undo.len()), (&layers, depth), "{case}: the click changes no art");
click(&mut s, if undo { 20.0 } else { 420.0 }, 20.0);
let g = s.doc().unwrap().selection.objects[0];
assert_eq!(spec(&s, g).0, if undo { vec![a, b] } else { vec![b, c] }, "{case}");
}
}
#[test]
fn two_picks_with_no_edit_between_them_blend() {
let mut s = session();
let (a, b) = (rect(&mut s, 0.0, 0.0, 40.0), rect(&mut s, 200.0, 0.0, 40.0));
s.select_tool("blend", ViewInfo::default()).unwrap();
let click = |s: &mut Session, x: f64, y: f64| s.pointer(&PointerEvent::new(PointerKind::Down, x, y), ViewInfo::default()).unwrap();
click(&mut s, 20.0, 20.0);
// Drawing the overlays between the picks does not drop the first one.
assert!(!s.overlays(ViewInfo::default()).is_empty());
click(&mut s, 220.0, 20.0);
let g = s.doc().unwrap().selection.objects[0];
assert_eq!(spec(&s, g).0, vec![a, b]);
}
#[test]
fn removing_the_blend_the_blend_tool_adds_to_ends_adding() {
let mut s = session();
let (a, b, c) = (rect(&mut s, 0.0, 0.0, 40.0), rect(&mut s, 200.0, 0.0, 40.0), rect(&mut s, 400.0, 0.0, 40.0));
let (d, e) = (rect(&mut s, 0.0, 300.0, 40.0), rect(&mut s, 200.0, 300.0, 40.0));
let other = id_of(&s.execute("object.blend.make", &json!({"ids": [d.0, e.0]})).unwrap());
s.select_tool("blend", ViewInfo::default()).unwrap();
let click = |s: &mut Session, x: f64, y: f64| s.pointer(&PointerEvent::new(PointerKind::Down, x, y), ViewInfo::default()).unwrap();
click(&mut s, 20.0, 20.0);
click(&mut s, 220.0, 20.0);
let g = s.doc().unwrap().selection.objects[0];
s.execute("edit.clear", &json!({"ids": [g.0]})).unwrap();
sel(&mut s, &[other]);
click(&mut s, 420.0, 20.0);
assert_eq!(s.doc().unwrap().selection.objects, vec![c], "c is picked first");
assert_eq!(spec(&s, other).0, vec![d, e], "c is not added to the selected blend");
assert!([a, b, g].iter().all(|id| s.doc().unwrap().doc.node(*id).is_none()), "the blend went with its keys");
}
#[test]
fn a_selection_change_keeps_the_blend_tools_first_pick() {
for drag in [false, true] {
let mut s = session();
let (a, b, c) = (rect(&mut s, 0.0, 0.0, 40.0), rect(&mut s, 200.0, 0.0, 40.0), rect(&mut s, 400.0, 0.0, 40.0));
let view = ViewInfo::default();
s.select_tool("blend", view).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Down, 20.0, 20.0), view).unwrap();
sel(&mut s, &[c]);
// Cmd lends the selection tool: a click on c selects it, a drag moves it (an edit).
let cmd = Mods { cmd: true, ..Mods::default() };
let to = if drag { 440.0 } else { 420.0 };
s.pointer(&PointerEvent::new(PointerKind::Down, 420.0, 20.0).with_mods(cmd), view).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, to, 20.0).with_mods(cmd), view).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Up, to, 20.0).with_mods(cmd), view).unwrap();
assert_eq!(s.tool_id(), "blend");
assert_eq!(s.overlays(view).is_empty(), drag, "drag {drag}");
s.pointer(&PointerEvent::new(PointerKind::Down, 220.0, 20.0), view).unwrap();
if drag {
assert_eq!(s.doc().unwrap().selection.objects, vec![b], "the drag dropped the pick");
} else {
let g = s.doc().unwrap().selection.objects[0];
assert_eq!(spec(&s, g).0, vec![a, b]);
}
}
}
#[test]
fn start_points_round_trip_and_old_blends_load() {
let mut s = session();
+41
View File
@@ -180,6 +180,47 @@ fn redo_during_an_interaction_keeps_the_history_and_the_document_in_step() {
assert_eq!(first_x(&s), start);
}
/// `History::step` changes with every new step, Undo and Redo, also when an undo and a new edit
/// leave as many steps as before, and with an edit merged into an undo group's step. It stays the
/// same for a selection change.
#[test]
fn the_history_step_changes_with_each_step_undo_and_redo() {
let mut s = session_with_rects(2);
let step = |s: &Session| s.doc().unwrap().history.step();
let start = step(&s);
assert!(start.0 != 0 && start.1 == 0, "the Add step, and nothing to redo: {start:?}");
s.execute("object.move", &json!({"dx": 1, "dy": 0})).unwrap();
let moved = step(&s);
assert!(moved.0 != start.0 && moved.1 == 0, "{moved:?}");
s.execute("edit.undo", &json!({})).unwrap();
let undone = step(&s);
assert!(undone.0 == start.0 && undone.1 != 0, "{undone:?}");
s.execute("edit.redo", &json!({})).unwrap();
let redone = step(&s);
assert!(![start, moved, undone].contains(&redone) && redone.1 == 0, "{redone:?}");
// An undo and a new edit: as many undo steps as after the move, at a different step.
s.execute("edit.undo", &json!({})).unwrap();
let depth = s.doc().unwrap().history.undo.len();
s.execute("object.move", &json!({"dx": 0, "dy": 1})).unwrap();
let other = step(&s);
assert_eq!(s.doc().unwrap().history.undo.len(), depth + 1);
assert!(![start, moved, undone, redone].contains(&other), "{other:?}");
s.execute("select.none", &json!({})).unwrap();
assert_eq!(step(&s), other, "a selection change is no step");
s.execute("select.all", &json!({})).unwrap();
s.begin_undo_group();
s.execute("object.move", &json!({"dx": 1, "dy": 0})).unwrap();
let (grouped, depth) = (step(&s), s.doc().unwrap().history.undo.len());
assert_ne!(grouped, other);
s.execute("object.move", &json!({"dx": 1, "dy": 0})).unwrap();
assert_eq!(s.doc().unwrap().history.undo.len(), depth, "the second move is merged into the first");
let merged = step(&s);
assert!(merged != grouped && merged.1 == 0, "a merged edit moves the step: {merged:?}");
s.execute("select.none", &json!({})).unwrap();
assert_eq!(step(&s), merged);
s.end_undo_group(false);
}
#[test]
fn color_mode_paint_snapshots_count_toward_the_history_budget() {
let mut s = Session::new();
+130
View File
@@ -141,6 +141,136 @@ fn a_graphic_style_clicked_with_nothing_selected_styles_the_next_object() {
assert!(s.execute("graphicStyle.apply", &json!({"name": name, "ids": []})).is_err(), "explicit ids that match nothing");
}
/// A rectangle with this fill, stroke and weight, selected.
fn painted(s: &mut Session, x: f64, fill: &str, stroke: &str, weight: f64) -> NodeId {
let id = rect(s, x);
run(s, "paint.setFill", json!({"color": fill}));
run(s, "paint.setStroke", json!({"color": stroke}));
run(s, "stroke.set", json!({"weight": weight}));
id
}
fn select(s: &mut Session, ids: &[NodeId]) {
run(s, "select.set", json!({"ids": ids.iter().map(|id| id.0).collect::<Vec<_>>()}));
}
#[test]
fn selecting_an_object_gives_new_art_its_fill_stroke_and_weight_with_basic_appearance_on() {
let mut s = session();
assert!(s.prefs.new_art_basic, "on by default");
let green = painted(&mut s, 20.0, "#ff0000", "#00ff00", 5.0);
painted(&mut s, 120.0, "#0000ff", "#000000", 2.0);
select(&mut s, &[green]);
run(&mut s, "select.none", json!({}));
let id = rect(&mut s, 220.0);
let (n, from) = (node(&s, id).appearance, node(&s, green).appearance);
assert_eq!((n.fill_paint(), n.stroke_paint(), n.stroke_width()), (from.fill_paint(), from.stroke_paint(), 5.0));
}
#[test]
fn with_basic_appearance_on_only_the_basic_fill_and_stroke_follow_the_selection() {
let mut s = session();
let src = fancy(&mut s);
painted(&mut s, 120.0, "#00ff00", "#000000", 2.0);
select(&mut s, &[src]);
run(&mut s, "select.none", json!({}));
let id = rect(&mut s, 220.0);
let n = node(&s, id);
assert_eq!(n.appearance.fill_paint(), node(&s, src).appearance.fill_paint(), "the top fill");
assert_eq!((n.appearance.items.len(), n.appearance.effects.len(), n.opacity), (2, 0, 1.0), "one fill and stroke, nothing else");
}
#[test]
fn a_paint_the_selected_objects_do_not_share_stays_as_it_was() {
let mut s = session();
let a = painted(&mut s, 20.0, "#ff0000", "#00ff00", 5.0);
let b = painted(&mut s, 120.0, "#0000ff", "#00ff00", 5.0);
painted(&mut s, 220.0, "#ffff00", "#000000", 1.0);
let before = s.paint.fill.clone();
select(&mut s, &[a, b]);
assert_eq!(s.paint.fill, before, "the fills differ");
assert_eq!((s.paint.stroke.clone(), s.paint.stroke_width), (node(&s, a).appearance.stroke_paint(), 5.0), "the strokes are shared");
}
#[test]
fn art_without_paint_leaves_the_proxies_as_they_were() {
let mut s = session();
painted(&mut s, 20.0, "#ff0000", "#00ff00", 5.0);
let (fill, stroke) = (s.paint.fill.clone(), s.paint.stroke.clone());
let dir = super::links::Folder::new("new-art-image");
let path = dir.file("pic.png");
super::links::write(&path, &super::links::png(4, 4, super::links::RED));
let image = super::links::place(&mut s, &path);
run(&mut s, "select.none", json!({}));
select(&mut s, &[image]);
assert_eq!((s.paint.fill.clone(), s.paint.stroke.clone(), s.paint.stroke_width), (fill, stroke, 5.0), "new art stays visible");
}
#[test]
fn an_object_without_a_stroke_clears_the_stroke_and_keeps_the_weight() {
let mut s = session();
let bare = rect(&mut s, 20.0);
run(&mut s, "paint.setStroke", json!({"none": true}));
painted(&mut s, 120.0, "#0000ff", "#000000", 7.0);
select(&mut s, &[bare]);
assert_eq!((s.paint.stroke.clone(), s.paint.stroke_width), (Paint::None, 7.0));
}
#[test]
fn a_selected_gradient_loads_without_its_place_on_the_object() {
let mut s = session();
let src = rect(&mut s, 20.0);
let stops = json!([{"offset": 0, "color": "#ffffff"}, {"offset": 1, "color": "#000000"}]);
run(&mut s, "paint.setFill", json!({"gradient": {"stops": stops, "start": [20, 40], "end": [50, 40]}}));
painted(&mut s, 120.0, "#0000ff", "#000000", 2.0);
select(&mut s, &[src]);
assert!(matches!(s.paint.fill, Paint::Gradient(_)), "{:?}", s.paint.fill);
run(&mut s, "select.none", json!({}));
let id = rect(&mut s, 220.0);
assert!(matches!(node(&s, id).appearance.fill_paint(), Paint::Gradient(_)));
}
#[test]
fn selecting_leaves_the_stroke_panels_options_for_new_art_alone() {
let mut s = session();
let plain = painted(&mut s, 20.0, "#ff0000", "#00ff00", 5.0);
run(&mut s, "select.none", json!({}));
run(&mut s, "stroke.set", json!({"cap": "round", "dash": [4, 2]}));
select(&mut s, &[plain]);
run(&mut s, "select.none", json!({}));
let id = rect(&mut s, 120.0);
let st = node(&s, id).appearance.stroke().unwrap().clone();
assert_eq!((st.paint.clone(), st.width), (node(&s, plain).appearance.stroke_paint(), 5.0));
assert_eq!((st.cap, st.dash.map(|d| d.pattern)), (LineCap::Round, Some(vec![4.0, 2.0])));
}
#[test]
fn a_sampled_paint_stays_until_the_selection_changes() {
let mut s = session();
let a = painted(&mut s, 20.0, "#ff0000", "#00ff00", 5.0);
let sampled = Paint::solid(vectorcraft_color::Color::WHITE);
s.paint.fill = sampled.clone();
run(&mut s, "transparency.set", json!({"opacity": 50}));
assert_eq!(s.paint.fill, sampled, "not a selection command");
select(&mut s, &[a]);
assert_eq!(s.paint.fill, sampled, "the same selection again");
run(&mut s, "select.none", json!({}));
assert_eq!(s.paint.fill, sampled, "nothing selected shows nothing to load");
select(&mut s, &[a]);
assert_eq!(s.paint.fill, node(&s, a).appearance.fill_paint(), "selected anew");
}
#[test]
fn targeting_a_layer_loads_the_paints_of_its_contents() {
let mut s = session();
let a = painted(&mut s, 20.0, "#ff0000", "#00ff00", 5.0);
let layer = s.doc().unwrap().doc.layers[0].id;
run(&mut s, "select.none", json!({}));
run(&mut s, "paint.setFill", json!({"color": "#0000ff"}));
run(&mut s, "layer.target", json!({"id": layer.0}));
assert_eq!(s.paint.fill, node(&s, a).appearance.fill_paint());
}
#[test]
fn the_option_round_trips_with_the_preferences() {
let mut s = session();
+68 -22
View File
@@ -164,6 +164,33 @@ fn a_placed_gradient_moved_by_a_transform_or_free_distort_expands_as_the_canvas_
}
}
/// A gradient fitted to the object turns with a Transform effect on the canvas, and Object › Expand
/// of the fill makes strips that draw the same.
#[test]
fn a_fitted_gradient_turned_by_a_transform_expands_as_the_canvas_draws_it() {
// The gray level at (x, y).
let gray = |r: &vectorcraft_render::Rendered, x: usize, y: usize| r.pixels[(y * 200 + x) * 4] as i32;
// The object's own effect, and the fill's (the first appearance item).
for (params, item) in
[(json!({"rotate": 90}), json!(null)), (json!({"rotate": 90}), json!(0)), (json!({"rotate": 30, "scaleH": 60}), json!(null))]
{
let mut s = session();
let id = gradient_rect(&mut s, Rect::new(60.0, 60.0, 140.0, 140.0), white_black(), false);
run(&mut s, "effect.apply", json!({"effect": "distort.transform", "params": params, "ids": [id.0], "item": item}));
let before = render(&s);
if params["rotate"] == 90 {
// The white to black ramp runs up or down the turned square, not across it.
let (top, bottom) = (gray(&before, 100, 70), gray(&before, 100, 130));
let (left, right) = (gray(&before, 70, 100), gray(&before, 130, 100));
assert!((top - bottom).abs() > 100 && (left - right).abs() < 20, "{params} on item {item}: {top} {bottom} {left} {right}");
}
run(&mut s, "object.expand", json!({"object": false, "stroke": false, "steps": 64}));
let after = render(&s);
let diff = mean_diff(&before, &after);
assert!(diff < 1.0, "{params} on item {item}: mean difference {diff}");
}
}
#[test]
fn a_placed_gradient_moved_by_a_warp_expands_as_the_canvas_draws_it() {
// The object's own effect, and the fill's (the first appearance item).
@@ -947,19 +974,48 @@ fn expanded_colours_keep_the_stops_colour_model() {
assert!((c - 0.625).abs() < 1e-5 && (m - 0.375).abs() < 1e-5, "interpolated in CMYK: {c} {m}");
}
/// A new 200 × 200 CMYK document.
fn cmyk_session() -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 200, "colorMode": "cmyk"})).unwrap();
s
}
/// The selection's freeform gradient expanded: the mesh's colors, after checking that it draws
/// within `most` (mean channel difference) of the gradient.
fn expanded(s: &mut Session, most: f64) -> Vec<Color> {
let before = render(s);
run(s, "object.expand", json!({}));
let diff = mean_diff(&before, &render(s));
assert!(diff < most, "looks the same: {diff}");
let NodeKind::Mesh(m) = selected(s).kind else { panic!("a mesh: {:?}", selected(s).kind) };
m.points.into_iter().map(|p| p.color).collect()
}
/// Cyan and black points mix as inks: cyan and black only, adding up to one. The gradient and the
/// mesh both mix inks, so the two draw alike.
fn cyan_and_black_points_expand_as_they_draw(mut s: Session) {
let (cyan, black) = (json!({"c": 1.0, "m": 0.0, "y": 0.0, "k": 0.0}), json!({"c": 0.0, "m": 0.0, "y": 0.0, "k": 1.0}));
freeform_rect(&mut s, Rect::new(20.0, 20.0, 180.0, 180.0), [cyan.clone(), cyan, black.clone(), black]);
for c in expanded(&mut s, 2.5) {
let [c, m, y, k] = inks(c);
assert!(m.abs() < 1e-5 && y.abs() < 1e-5 && (c + k - 1.0).abs() < 1e-5, "C{c} M{m} Y{y} K{k}");
}
}
#[test]
fn cyan_and_black_freeform_points_expand_as_they_draw_in_an_rgb_document() {
cyan_and_black_points_expand_as_they_draw(session());
}
#[test]
fn cyan_and_black_freeform_points_expand_as_they_draw_in_a_cmyk_document() {
cyan_and_black_points_expand_as_they_draw(cmyk_session());
}
#[test]
fn a_freeform_gradient_expands_with_the_inks_of_its_points() {
let ink = |c: f32, m: f32, y: f32, k: f32| json!({"c": c, "m": m, "y": y, "k": k});
// The mesh's colors, after checking that it draws within `most` (mean channel difference) of
// the gradient.
let expanded = |s: &mut Session, most: f64| {
let before = render(s);
run(s, "object.expand", json!({}));
let diff = mean_diff(&before, &render(s));
assert!(diff < most, "looks the same: {diff}");
let NodeKind::Mesh(m) = selected(s).kind else { panic!("a mesh: {:?}", selected(s).kind) };
m.points.into_iter().map(|p| p.color).collect::<Vec<_>>()
};
// Points of one ink set give every mesh point that ink set.
let mut s = session();
let one = ink(0.5, 0.2, 0.0, 0.1);
@@ -967,19 +1023,9 @@ fn a_freeform_gradient_expands_with_the_inks_of_its_points() {
for c in expanded(&mut s, 1.0) {
assert!(close_inks(inks(c), [0.5, 0.2, 0.0, 0.1]), "{c:?}");
}
// Cyan and black points mix as inks: cyan and black only, adding up to one. The gradient blends
// the points' display colors and the mesh blends their inks, so the two draw a little apart in between.
let mut s = session();
let (cyan, black) = (ink(1.0, 0.0, 0.0, 0.0), ink(0.0, 0.0, 0.0, 1.0));
freeform_rect(&mut s, Rect::new(20.0, 20.0, 180.0, 180.0), [cyan.clone(), cyan.clone(), black.clone(), black]);
for c in expanded(&mut s, 7.5) {
let [c, m, y, k] = inks(c);
assert!(m.abs() < 1e-5 && y.abs() < 1e-5 && (c + k - 1.0).abs() < 1e-5, "C{c} M{m} Y{y} K{k}");
}
// Points in different models mix in display RGB, stored in a CMYK document as CMYK.
let mut s = Session::new();
s.execute("file.new", &json!({"width": 200, "height": 200, "colorMode": "cmyk"})).unwrap();
let gray = json!({"gray": 0.5});
let (cyan, gray) = (ink(1.0, 0.0, 0.0, 0.0), json!({"gray": 0.5}));
let mut s = cmyk_session();
freeform_rect(&mut s, Rect::new(20.0, 20.0, 180.0, 180.0), [cyan.clone(), cyan, gray.clone(), gray]);
for c in expanded(&mut s, 2.5) {
inks(c);
+229 -7
View File
@@ -579,9 +579,9 @@ fn asking(id: &str, permissions: &str, output: &Value) -> Vec<u8> {
/// A plug-in whose manifest lists permissions installs only once they are all granted (`grant`),
/// which `plugin.inspect` tells beforehand (`needsApproval`) with the permissions and the module's
/// hash. The grant is recorded with that hash: replacing the plug-in (`replace: true`) with one
/// that lists the same permissions needs no new grant, and one that lists another needs one. Removing it forgets the
/// grant. A plug-in without `permissions` installs as before, with the ones its kind implies.
/// hash. The grant is recorded with that hash and covers only that module: replacing the plug-in
/// (`replace: true`) with one with other bytes needs a new grant. Removing it forgets the grant. A
/// plug-in without `permissions` installs as before, with the ones its kind implies.
#[test]
fn a_plugin_that_lists_permissions_installs_once_they_are_granted() {
let mut s = session();
@@ -617,11 +617,11 @@ fn a_plugin_that_lists_permissions_installs_once_they_are_granted() {
assert_eq!(installed["permissions"], json!(["doc.read"]));
let g = vectorcraft_plugins::registry::granted(id).unwrap();
assert_eq!((g.hash.as_str(), g.permissions.as_slice()), (hash.as_str(), &[vectorcraft_plugins::permissions::Permission::DocRead][..]));
// Replaced with another module that lists the same permissions: no new grant needed, and the
// grant now records the new module's hash.
// Replaced with another module that lists the same permissions: granted again, the grant now
// records the new module's hash.
let other = asking(id, r#"["doc.read"]"#, &json!({"objects": [], "error": null}));
assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": vectorcraft_format::base64_encode(&other)}))["needsApproval"], false);
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&other), "replace": true}));
assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": vectorcraft_format::base64_encode(&other)}))["needsApproval"], true);
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&other), "grant": ["doc.read"], "replace": true}));
assert_eq!(vectorcraft_plugins::registry::granted(id).unwrap().hash, vectorcraft_plugins::permissions::sha256_hex(&other));
// One that asks for more is refused until it is granted all of them.
let more = data(r#"["doc.read","doc.write"]"#);
@@ -641,6 +641,221 @@ fn a_plugin_that_lists_permissions_installs_once_they_are_granted() {
assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": vectorcraft_format::base64_encode(DESATURATE)}))["needsApproval"], false);
}
/// A module with other bytes under an id that was granted is not covered, even when it lists the
/// same permissions: `plugin.inspect` says so, `plugin.install` refuses it and leaves the installed
/// module and its grant alone, and a new `grant` installs it and records its hash. The same bytes
/// install again without asking, and a module that lists no permissions never asks.
#[test]
fn a_grant_covers_only_the_module_that_was_approved() {
use vectorcraft_plugins::permissions::sha256_hex;
let mut s = session();
let id = "test.engine.permissions.bytes";
let first = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
let changed = asking(id, r#"["doc.read"]"#, &json!({"objects": [], "error": null}));
let (first_data, changed_data) = (vectorcraft_format::base64_encode(&first), vectorcraft_format::base64_encode(&changed));
let (first_hash, changed_hash) = (sha256_hex(&first), sha256_hex(&changed));
run(&mut s, "plugin.install", json!({"dataBase64": first_data, "grant": ["doc.read"]}));
assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": changed_data}))["needsApproval"], true);
let e = s.execute("plugin.install", &json!({"dataBase64": changed_data, "replace": true})).unwrap_err().to_string();
assert!(e.contains("doc.read") && e.contains("`grant`"), "{e}");
let e = s.execute("plugin.install", &json!({"dataBase64": changed_data, "replace": true, "hash": changed_hash})).unwrap_err().to_string();
assert!(e.contains("`grant`"), "`hash` is not an approval: {e}");
assert_eq!(vectorcraft_plugins::registry::get(id).unwrap().hash(), first_hash, "the installed module stays");
assert_eq!(vectorcraft_plugins::registry::granted(id).unwrap().hash, first_hash, "its grant stays");
// The same bytes need no new grant.
assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": first_data}))["needsApproval"], false);
run(&mut s, "plugin.install", json!({"dataBase64": first_data}));
// A new grant installs the changed module, and the old bytes are no longer covered.
run(&mut s, "plugin.install", json!({"dataBase64": changed_data, "grant": ["doc.read"], "hash": changed_hash, "replace": true}));
assert_eq!(vectorcraft_plugins::registry::get(id).unwrap().hash(), changed_hash);
assert_eq!(vectorcraft_plugins::registry::granted(id).unwrap().hash, changed_hash);
assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": first_data}))["needsApproval"], true);
run(&mut s, "plugin.remove", json!({"id": id}));
// A manifest without a list, or with an empty one, asks for nothing, whatever its bytes.
let exempt = "test.engine.permissions.exempt";
for (permissions, out) in [("null", json!({"objects": []})), ("null", json!({"objects": [], "error": null})), ("[]", json!({"objects": []}))] {
let data = vectorcraft_format::base64_encode(&asking(exempt, permissions, &out));
assert_eq!(run(&mut s, "plugin.inspect", json!({"dataBase64": data}))["needsApproval"], false, "{permissions}");
run(&mut s, "plugin.install", json!({"dataBase64": data, "replace": true}));
}
run(&mut s, "plugin.remove", json!({"id": exempt}));
}
/// A stored module whose bytes changed is skipped at the next start and stays in the store with its
/// grant, and the original bytes load again. A stored grant whose hash is malformed covers nothing.
/// With a store attached, a matching grant held only by the process doesn't count.
#[test]
fn a_stored_grant_covers_only_the_stored_module_it_recorded() {
use vectorcraft_plugins::permissions::{Grant, Permission, sha256_hex};
let folder = crate::tests_links::Folder::new("plugin-store-recorded-module");
let id = "test.engine.permissions.storedbytes";
let first = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
let changed = asking(id, r#"["doc.read"]"#, &json!({"objects": [], "error": null}));
let mut s = session();
s.attach_plugin_store(folder.0.clone());
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&first), "grant": ["doc.read"]}));
let grants = folder.0.join("grants.json");
let recorded = std::fs::read(&grants).unwrap();
let module = std::fs::read_dir(&folder.0)
.unwrap()
.map(|e| e.unwrap().path())
.find(|p| p.extension().is_some_and(|x| x == "wasm"))
.expect("the stored module");
let forget = || {
vectorcraft_plugins::registry::remove(id);
vectorcraft_plugins::registry::revoke(id);
};
let starts = || {
forget();
let mut next = session();
next.attach_plugin_store(folder.0.clone());
vectorcraft_plugins::registry::get(id).is_some()
};
assert!(starts(), "the module that was approved loads");
std::fs::write(&module, &changed).unwrap();
assert!(!starts(), "other bytes under the old grant are skipped");
assert_eq!(std::fs::read(&module).unwrap(), changed, "the module stays in the store");
assert_eq!(std::fs::read(&grants).unwrap(), recorded, "its grant stays");
std::fs::write(&module, &first).unwrap();
assert!(starts(), "the approved bytes load again");
// A malformed hash is no wildcard, even for the module on disk.
let upper = sha256_hex(&first).to_uppercase();
for hash in ["", "00", upper.as_str()] {
std::fs::write(&grants, serde_json::to_vec(&json!([{"id": id, "hash": hash, "permissions": ["doc.read"]}])).unwrap()).unwrap();
assert!(!starts(), "hash {hash:?}");
}
// The store's grants are the ones that count, even when the process holds one for these bytes.
std::fs::write(&grants, &recorded).unwrap();
assert!(starts());
let mut attached = session();
attached.attach_plugin_store(folder.0.clone());
vectorcraft_plugins::registry::grant(Grant { id: id.into(), hash: sha256_hex(&changed), permissions: vec![Permission::DocRead] });
let inspected = run(&mut attached, "plugin.inspect", json!({"dataBase64": vectorcraft_format::base64_encode(&changed)}));
assert_eq!(inspected["needsApproval"], true, "a grant held only by the process doesn't count with a store attached");
run(&mut attached, "plugin.remove", json!({"id": id}));
}
/// A write to the plug-in store that fails leaves the store's module beside the grant that approved
/// it, so the old module loads at the next start: a module that can't be written doesn't update the
/// grant, and a grant that can't be written puts the old module back. On a first install, it takes
/// the new module out again.
#[test]
fn a_failed_store_write_keeps_the_stored_module_with_its_grant() {
use vectorcraft_plugins::permissions::sha256_hex;
let folder = crate::tests_links::Folder::new("plugin-store-failed-write");
let id = "test.engine.permissions.failedwrite";
let first = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
let changed = asking(id, r#"["doc.read","doc.write"]"#, &json!({"objects": []}));
let first_data = vectorcraft_format::base64_encode(&first);
let update = json!({"dataBase64": vectorcraft_format::base64_encode(&changed), "grant": ["doc.read", "doc.write"], "replace": true});
let mut s = session();
s.attach_plugin_store(folder.0.clone());
run(&mut s, "plugin.install", json!({"dataBase64": first_data, "grant": ["doc.read"]}));
let grants = folder.0.join("grants.json");
let recorded = std::fs::read(&grants).unwrap();
let module = std::path::PathBuf::from(s.stored_plugin_file(id).unwrap());
let starts = || {
vectorcraft_plugins::registry::remove(id);
vectorcraft_plugins::registry::revoke(id);
let mut next = session();
next.attach_plugin_store(folder.0.clone());
vectorcraft_plugins::registry::get(id).map(|p| p.hash().to_string())
};
let block = |path: &std::path::Path| {
std::fs::remove_file(path).unwrap();
std::fs::create_dir_all(path.join("block")).unwrap();
};
// The module can't be written: the grant stays the old module's.
block(&module);
run(&mut s, "plugin.install", update.clone());
assert_eq!(std::fs::read(&grants).unwrap(), recorded, "the grant isn't updated for a module that wasn't stored");
std::fs::remove_dir_all(&module).unwrap();
std::fs::write(&module, &first).unwrap();
assert_eq!(starts(), Some(sha256_hex(&first)), "the old module loads after a failed module write");
// The grants file is full: the new grant, which is longer, doesn't fit.
let cap = 1 << 20;
let mut entries: Vec<Value> = serde_json::from_slice(&recorded).unwrap();
entries.push(json!({"id": "", "pad": ""}));
let room = cap - serde_json::to_vec(&entries).unwrap().len();
*entries.last_mut().unwrap() = json!({"id": "", "pad": "x".repeat(room)});
let full = serde_json::to_vec(&entries).unwrap();
assert_eq!(full.len(), cap);
std::fs::write(&grants, &full).unwrap();
run(&mut s, "plugin.install", update.clone());
assert_eq!(std::fs::read(&grants).unwrap(), full, "not written past the cap");
assert_eq!(std::fs::read(&module).unwrap(), first, "the old module is put back");
assert_eq!(starts(), Some(sha256_hex(&first)), "the old module loads after a failed grant write");
// The grants file can't be read, so nothing can be recorded in it.
block(&grants);
run(&mut s, "plugin.install", update.clone());
assert_eq!(std::fs::read(&module).unwrap(), first, "the old module is put back");
std::fs::remove_dir_all(&grants).unwrap();
std::fs::write(&grants, &recorded).unwrap();
assert_eq!(starts(), Some(sha256_hex(&first)), "the old module loads once the grants are readable");
// A stored module that can't be read back isn't replaced, so a failed grant write can't lose it.
let oversized = vectorcraft_plugins::Limits::default().max_module_bytes as u64 + 1;
std::fs::File::options().write(true).open(&module).unwrap().set_len(oversized).unwrap();
block(&grants);
run(&mut s, "plugin.install", update);
assert_eq!(std::fs::metadata(&module).unwrap().len(), oversized, "the stored module stays");
std::fs::remove_dir_all(&grants).unwrap();
std::fs::write(&grants, &recorded).unwrap();
std::fs::write(&module, &first).unwrap();
// A first install whose grant can't be written leaves no module without a grant in the store.
run(&mut s, "plugin.remove", json!({"id": id}));
assert!(!module.exists() && !grants.exists());
std::fs::create_dir_all(grants.join("block")).unwrap();
run(&mut s, "plugin.install", json!({"dataBase64": first_data, "grant": ["doc.read"]}));
assert!(!module.exists(), "the store holds no module its grants don't cover");
vectorcraft_plugins::registry::remove(id);
vectorcraft_plugins::registry::revoke(id);
}
/// A plug-in folder reports a changed module as needing approval and still loads the other files;
/// granted on its own, the changed module loads from the folder. A session without a store uses the
/// grants held by the process.
#[test]
fn a_plugin_folder_skips_a_changed_module_until_it_is_approved() {
let mut s = session();
let id = "test.engine.permissions.folderbytes";
let plain = "test.engine.permissions.folderplain";
let first = asking(id, r#"["doc.read"]"#, &json!({"objects": []}));
let changed = asking(id, r#"["doc.read"]"#, &json!({"objects": [], "error": null}));
let dir = crate::tests_links::Folder::new("plugin-folder-changed-module");
let file = dir.0.join("asks.wasm");
std::fs::write(&file, &first).unwrap();
std::fs::write(dir.0.join("plain.wasm"), asking(plain, "null", &json!({"objects": []}))).unwrap();
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&first), "grant": ["doc.read"]}));
let path = json!({"path": dir.0.display().to_string()});
let r = run(&mut s, "plugin.reload", path.clone());
assert_eq!((r["loaded"].as_array().unwrap().len(), r["failed"].as_array().unwrap().len()), (2, 0), "{r}");
std::fs::write(&file, &changed).unwrap();
let r = run(&mut s, "plugin.reload", path.clone());
assert_eq!(r["loaded"], json!([plain]), "the other file still loads");
let failed = r["failed"].as_array().unwrap();
assert_eq!(failed.len(), 1, "{r}");
assert!(failed[0]["file"].as_str().unwrap().ends_with("asks.wasm"), "{r}");
let why = failed[0]["error"].as_str().unwrap();
assert!(why.contains("needs approval") && why.contains("doc.read"), "{why}");
let hash = vectorcraft_plugins::permissions::sha256_hex(&first);
assert_eq!(vectorcraft_plugins::registry::get(id).unwrap().hash(), hash, "the module installed earlier stays");
run(&mut s, "plugin.install", json!({"dataBase64": vectorcraft_format::base64_encode(&changed), "grant": ["doc.read"], "replace": true}));
let r = run(&mut s, "plugin.reload", path);
assert_eq!(r["failed"], json!([]), "approved on its own, it loads: {r}");
run(&mut s, "plugin.remove", json!({"id": id}));
run(&mut s, "plugin.remove", json!({"id": plain}));
}
/// The host enforces what a filter was granted: without `doc.write` an output that changes the
/// document is refused and nothing changes, without `doc.read` it is given no objects, and without
/// `state` it is given no data kept with the document and may not write any.
@@ -815,18 +1030,25 @@ fn a_grant_covers_only_the_module_it_was_given_to() {
std::fs::write(&stored[0], &other).unwrap();
vectorcraft_plugins::registry::remove(id);
vectorcraft_plugins::registry::revoke(id);
vectorcraft_plugins::registry::set_storage(id, None);
let no_storage = |how: &str| {
assert!(vectorcraft_plugins::registry::storage(id).is_none(), "{how} gives another module the id's settings and cache");
};
let mut next = session();
next.attach_plugin_store(folder.0.clone());
assert!(vectorcraft_plugins::registry::get(id).is_none(), "another module isn't loaded under the grant");
no_storage("the next start");
let dir = crate::tests_links::Folder::new("plugin-folder-grant-hash");
std::fs::write(dir.0.join("other.wasm"), &other).unwrap();
let r = run(&mut next, "plugin.reload", json!({"path": dir.0.display().to_string()}));
assert_eq!(r["loaded"], json!([]));
let why = r["failed"][0]["error"].as_str().unwrap();
assert!(why.contains("needs approval"), "{why}");
no_storage("a folder load");
let data = vectorcraft_format::base64_encode(&other);
let e = next.execute("plugin.install", &json!({"dataBase64": data, "replace": true})).unwrap_err().to_string();
assert!(e.contains("`grant`"), "{e}");
no_storage("a refused install");
// Granted, it reads the settings the first module set.
run(&mut next, "plugin.install", json!({"dataBase64": data, "grant": ["settings", "cache"], "replace": true}));
run(&mut next, "plugin.run", json!({"id": id, "ids": []}));
+27
View File
@@ -241,6 +241,33 @@ fn options_are_saved_with_the_preferences() {
assert_eq!(t.prefs.tool_settings["polygon"]["sides"], json!(9));
}
/// Escape on a polygon or rounded rectangle drag puts back the side count or corner radius its
/// arrows changed, in the saved options and the Corner Radius preference too.
#[test]
fn escape_on_a_shape_drag_saves_the_option_it_put_back() {
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
let mut s = session();
let v = ViewInfo { smart_guides: false, ..Default::default() };
let drag = |s: &mut Session| {
s.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 100.0), v).unwrap();
s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 180.0), v).unwrap();
s.tool_key(ToolKey::Up, Mods::default(), v).unwrap();
// Saves every option, the one the arrow changed included.
s.set_tool_option("length", &json!(50));
};
select(&mut s, "polygon");
drag(&mut s);
assert_eq!(s.prefs.tool_settings["polygon"]["sides"], json!(7));
s.tool_key(ToolKey::Escape, Mods::default(), v).unwrap();
assert_eq!(s.prefs.tool_settings["polygon"]["sides"], json!(6));
select(&mut s, "roundedRectangle");
let radius = s.prefs.corner_radius;
drag(&mut s);
assert_eq!(s.prefs.corner_radius, radius + 1.0);
s.tool_key(ToolKey::Escape, Mods::default(), v).unwrap();
assert_eq!(s.prefs.corner_radius, radius);
}
/// The Fidelity an Alt smoothing drag uses on a selected path.
fn alt_smooth_fidelity(s: &mut Session) -> Option<f64> {
use vectorcraft_tools::{Action, Mods, PointerEvent, PointerKind};
+13
View File
@@ -1024,6 +1024,19 @@ fn lasso_picks_gradient_mesh_points() {
/// Shift-drag a marquee with the Selection tool (#483): the selected objects it reaches are
/// deselected and the others selected, the rest of the selection staying as it was.
#[test]
fn a_selection_tool_click_gives_new_art_the_objects_fill() {
let mut s = session();
let a = rect(&mut s, 100.0, 100.0, 50.0, 50.0);
s.execute("paint.setFill", &json!({"color": "#00ff00"})).unwrap();
rect(&mut s, 200.0, 100.0, 50.0, 50.0);
s.execute("paint.setFill", &json!({"color": "#0000ff"})).unwrap();
s.execute("select.none", &json!({})).unwrap();
gesture(&mut s, "selection", &[(125.0, 125.0), (125.0, 125.0)], Mods::default());
assert_eq!(s.doc().unwrap().selection.objects, vec![a]);
assert_eq!(s.paint.fill, s.doc().unwrap().doc.node(a).unwrap().appearance.fill_paint());
}
#[test]
fn selection_shift_marquee_toggles_objects() {
let mut s = session();
+28
View File
@@ -402,6 +402,25 @@ impl Session {
self.tool_panic.take().map_or(Ok(()), Err)
}
/// The active document (its uid) and its [`crate::History::step`].
fn history_step(&self) -> Option<(u64, (u64, u64))> {
self.active().map(|d| (d.uid, d.history.step()))
}
/// Tell the active tool when the history moved, or another document became active, since it
/// last handled a pointer event or was last told ([`vectorcraft_tools::Tool::history_moved`]).
/// A lent selection tool is not told.
fn tell_tool_history_moved(&mut self) {
if self.lender.is_some() {
return;
}
let now = self.history_step();
if self.tool_step.is_some_and(|seen| Some(seen) != now) {
self.tool.history_moved();
}
self.tool_step = now;
}
/// Feed a pointer event to the active tool. Returns requests for the UI.
pub fn pointer(&mut self, ev: &PointerEvent, view: ViewInfo) -> Result<Vec<UiRequest>> {
self.pointer_event(ev, view, true)
@@ -495,7 +514,9 @@ impl Session {
return r;
}
self.leave_draw_inside(ev, view)?;
self.tell_tool_history_moved();
let ev = &self.lend_selection_tool(ev);
let lent = self.lender.is_some();
let mut acts = self.with_tool_cx(view, |t, cx| t.pointer(cx, ev));
if !keep {
acts.retain(|a| matches!(a, Action::Cancel | Action::Notify(_)));
@@ -507,6 +528,11 @@ impl Session {
}
self.apply_actions(acts)
});
// The tool's own edits do not move the history for it. A selection tool that Cmd lends
// edits for the lender, which hears of the edit at its next event.
if !lent {
self.tool_step = self.history_step();
}
match ev.kind {
PointerKind::Down => self.press_point = Some(self.freeform_point),
PointerKind::Up => self.press_point = None,
@@ -764,6 +790,8 @@ impl Session {
}
pub fn overlays(&mut self, view: ViewInfo) -> Vec<Overlay> {
// The Blend tool's first pick is not marked after an edit that dropped it.
self.tell_tool_history_moved();
let mut v = self.with_tool_cx(view, |t, cx| t.overlays(cx));
// The Type tools draw the ports of selected type themselves.
if !matches!(self.tool.id(), "type" | "areaType" | "typeOnPath" | "verticalType" | "verticalAreaType" | "verticalTypeOnPath") {
+36
View File
@@ -347,6 +347,42 @@ fn a_freeform_gradient_expands_with_its_rgb_values_in_the_working_rgb_space() {
}
}
/// With a working RGB space other than sRGB, RGB freeform points mix in that space on the canvas
/// and in a writer, as Object > Expand mixes them.
#[test]
fn freeform_rgb_points_mix_in_the_working_rgb_space() {
use vectorcraft_color::freeform::spread_scale;
use vectorcraft_color::{Color, Freeform, FreeformPoint, Gradient, GradientKind, GradientPaint, Paint};
use vectorcraft_doc::{Appearance, Document, Node};
use vectorcraft_geom::{Point, Rect, shapes};
let _g = settings();
let old = cms::active_settings();
cms::set_active(&cms::ColorSettings { rgb: cms::WIDE_GAMUT_RGB.into(), ..old.clone() }).unwrap();
let box_ = Rect::new(0.0, 0.0, 100.0, 100.0);
let (a, b) = (Color::rgb(0.9, 0.1, 0.1), Color::rgb(0.1, 0.2, 0.9));
let p = |x, c| FreeformPoint::new(Point::new(x, 50.5), c);
let f = Freeform { points: vec![p(20.5, a), p(80.5, b)], ..Default::default() };
let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() });
g.freeform = Some(f.clone());
let mut d = Document::new(100.0, 100.0);
let id = d.alloc_id();
d.insert(Some(d.layers[0].id), 0, Node::path(id, shapes::rectangle(box_), Appearance::basic(Paint::Gradient(Box::new(g)), Paint::None, 0.0)))
.unwrap();
let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
let canvas = Renderer::new().render(&d, 100, 100, Affine::IDENTITY, &opts).pixel(50, 50);
let svg = vectorcraft_svg::export_full(&d, &vectorcraft_svg::ExportOptions::default(), None).svg;
let data = svg.split("data:image/png;base64,").nth(1).unwrap().split('"').next().unwrap();
let png = image::load_from_memory(&vectorcraft_testkit::format::base64_decode(data).unwrap()).unwrap().to_rgba8();
let written = png.get_pixel(png.width() / 2, png.height() / 2).0;
let want = Color::rgb(0.5, 0.15, 0.5).to_rgba8(1.0);
let display = f.field(spread_scale(box_)).sample(Point::new(50.5, 50.5)).0.map(|v| (v * 255.0).round() as u8);
cms::set_active(&old).unwrap();
let near = |got: [u8; 4]| got.iter().zip(want).take(3).all(|(g, w)| g.abs_diff(w) <= 3);
assert!(near(canvas), "canvas: {canvas:?} vs {want:?}");
assert!(near(written), "SVG: {written:?} vs {want:?}");
assert!(display.iter().zip(want).any(|(g, w)| g.abs_diff(w) > 8), "the display mix differs: {display:?} vs {want:?}");
}
/// Loading a new file of the working RGB profile (named after its description, as before) takes
/// effect at once: CMYK colors convert into the new file and the canvas redraws, without choosing
/// the working spaces again.
+80 -16
View File
@@ -15,7 +15,7 @@ use proptest::prelude::*;
use serde_json::{Map, Value, json};
use vectorcraft_engine::Session;
use vectorcraft_plugins::wat as tpl;
use vectorcraft_plugins::{Error, Limits, Plugin};
use vectorcraft_plugins::{Error, Kind, Limits, Plugin};
use vectorcraft_testkit::catch_quiet;
use vectorcraft_testkit::fixtures::rich_session;
@@ -67,6 +67,29 @@ fn arb_junk() -> impl Strategy<Value = Value> {
})
}
/// One entry of a manifest's `extensions`: valid, dotted, mixed-case, empty or too long, or junk.
fn arb_extension() -> impl Strategy<Value = Value> {
prop_oneof![
4 => "[a-z0-9]{1,16}".prop_map(Value::from),
1 => prop::sample::select(vec![".fz", "FZ", "fZ", "a b", "", "x.y", "é", "a\u{202E}", "abcdefghijklmnopq"]).prop_map(Value::from),
1 => "[a-z0-9]{17,32}".prop_map(Value::from),
1 => arb_junk(),
]
}
/// A manifest's `extensions`: `None` leaves the key out; otherwise an empty list, up to the eight
/// allowed, too many, one repeated, or junk.
fn arb_extensions() -> impl Strategy<Value = Option<Value>> {
prop_oneof![
1 => Just(None),
1 => Just(Some(json!([]))),
4 => prop::collection::vec(arb_extension(), 1..=8).prop_map(|l| Some(Value::Array(l))),
1 => prop::collection::vec(arb_extension(), 9..=12).prop_map(|l| Some(Value::Array(l))),
1 => "[a-z0-9]{1,8}".prop_map(|e| Some(json!([e, "fz", e]))),
1 => arb_junk().prop_map(Some),
]
}
/// Numbers that tend to break arithmetic.
fn arb_num() -> impl Strategy<Value = Value> {
prop_oneof![
@@ -512,6 +535,24 @@ proptest! {
renders(&s, &out)?;
}
/// An importer's hostile output (`document.open`) either opens a document that renders and
/// exports, or is an error that opens nothing.
#[test]
fn hostile_importer_output_never_crashes(
objects in prop::collection::vec(arb_object(), 0..5),
junk in prop::option::weighted(0.1, arb_junk()),
) {
let mut s = Session::new();
let out = output(&objects, &[], junk);
install(&mut s, &wat::parse_str(tpl::returning(&tpl::importer("fuzz.importer", r#"["fzi"]"#, "{}"), &out)).unwrap()).unwrap();
let r = catch_quiet(|| s.execute("document.open", &json!({"name": "x.fzi", "dataBase64": ""})).map(|_| ()));
match r {
Err(msg) => return Err(TestCaseError::fail(format!("panicked on {out}: {msg}"))),
Ok(Err(_)) => prop_assert!(s.documents().is_empty(), "a failed open opened a document: {}", out),
Ok(Ok(())) => renders(&s, &out)?,
}
}
#[test]
fn bulk_memory_plugins_never_crash(case in arb_bulk_case(), fuel in arb_fuel()) {
let r = catch_quiet(|| run_bulk(case, fuel)).map_err(|msg| TestCaseError::fail(format!("panicked on {case:?}: {msg}")))?;
@@ -566,17 +607,18 @@ proptest! {
renders(&s, "after mutated modules")?;
}
/// Manifests with hostile `permissions`, `api` and names (unknown, reserved and repeated
/// permissions, junk of every type, control and bidirectional override characters) parse to
/// an error or to a manifest whose permissions fit its kind, and installing one with a junk
/// `grant` ends as an error or an install, never a panic. A prompt never shows a hidden
/// Manifests with hostile `permissions`, `api`, names and `extensions` (unknown, reserved and
/// repeated permissions, junk of every type, control and bidirectional override characters,
/// extensions missing, dotted, mixed-case, repeated, too long or too many) parse to an error
/// or to a manifest whose permissions and extensions fit its kind, and installing one with a
/// junk `grant` ends as an error or an install, never a panic. A prompt never shows a hidden
/// character from the name.
#[test]
fn hostile_manifest_permissions_never_crash(
kind in prop::sample::select(vec!["filter", "effect", "exporter"]),
kind in prop::sample::select(vec!["filter", "effect", "exporter", "importer"]),
names in prop::collection::vec(
prop_oneof![
prop::sample::select(vec!["doc.read", "doc.write", "state", "effect.render", "export.write", "progress", "ui.panel", ""]),
prop::sample::select(vec!["doc.read", "doc.write", "state", "effect.render", "export.write", "import.read", "progress", "ui.panel", ""]),
Just("doc.read\u{202E}"),
Just("\u{0007}"),
],
@@ -585,21 +627,43 @@ proptest! {
permissions in prop::option::weighted(0.8, prop_oneof![Just(Value::Null), arb_junk()]),
api in prop_oneof![2 => Just(json!(1)), 1 => Just(json!(2)), 1 => arb_junk()],
name in prop_oneof![3 => "[a-z\u{202E}\u{2066}\u{200F} ]{1,64}", 1 => "[a-z\u{202E}\u{2066}\u{0007}\u{200F} ]{1,80}"],
grant in prop_oneof![Just(Value::Null), arb_junk(), Just(json!(["doc.read", "doc.write", "state", "effect.render", "export.write", "progress"]))],
grant in prop_oneof![
Just(Value::Null),
arb_junk(),
Just(json!(["doc.read", "doc.write", "state", "effect.render", "export.write", "import.read", "progress"]))
],
extensions in prop::option::weighted(0.5, arb_extensions()),
) {
let mut m = json!({"id": "fuzz.permissions", "name": name, "kind": kind, "api": api});
if kind == "exporter" {
m["extensions"] = json!(["fz"]);
// Hostile extensions come with a valid `api` and no `permissions`, so the parse reaches
// them. The other half of the cases keep a valid list and reach the other fields.
let hostile_extensions = extensions.is_some();
match extensions {
None if matches!(kind, "exporter" | "importer") => m["extensions"] = json!(["fz"]),
None | Some(None) => {}
Some(Some(hostile)) => m["extensions"] = hostile,
}
if hostile_extensions {
m["api"] = json!(1);
} else {
m["permissions"] = match permissions {
Some(Value::Null) | None => json!(names),
Some(junk) => junk,
};
}
m["permissions"] = match permissions {
Some(Value::Null) | None => json!(names),
Some(junk) => junk,
};
let text = m.to_string();
let parsed = catch_quiet(|| vectorcraft_plugins::Manifest::parse(text.as_bytes()));
let parsed = parsed.map_err(|msg| TestCaseError::fail(format!("panicked on {text}: {msg}")))?;
if let Ok(man) = &parsed {
prop_assert!(man.permissions().iter().all(|p| p.applies(man.kind, !man.commands.is_empty())), "{}", text);
let exts = &man.extensions;
if matches!(man.kind, Kind::Exporter | Kind::Importer) {
prop_assert!((1..=8).contains(&exts.len()), "{}", text);
prop_assert!(exts.iter().all(|e| (1..=16).contains(&e.len()) && e.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit())), "{}", text);
prop_assert!(exts.iter().enumerate().all(|(i, e)| !exts[..i].contains(e)), "{}", text);
} else {
prop_assert!(exts.is_empty(), "{}", text);
}
let shown = vectorcraft_plugins::permissions::display_text(&man.name, vectorcraft_plugins::permissions::MAX_SHOWN_NAME);
prop_assert!(!shown.chars().any(|c| c.is_control() || matches!(c, '\u{202E}' | '\u{2066}' | '\u{200F}')), "{}", shown);
prop_assert!(shown.chars().count() <= vectorcraft_plugins::permissions::MAX_SHOWN_NAME);
@@ -624,7 +688,7 @@ proptest! {
/// error or an edit, never a panic, and the document then renders and exports.
#[test]
fn hostile_manifest_commands_never_crash(
kind in prop::sample::select(vec!["filter", "effect", "exporter"]),
kind in prop::sample::select(vec!["filter", "effect", "exporter", "importer"]),
entries in prop::collection::vec(
prop_oneof![
3 => (
@@ -655,7 +719,7 @@ proptest! {
given in prop_oneof![Just(json!({})), Just(json!({"by": 3})), arb_junk()],
) {
let mut m = json!({"id": "fuzz.commands", "name": "Fuzz \u{202E}Commands", "kind": kind});
if kind == "exporter" {
if matches!(kind, "exporter" | "importer") {
m["extensions"] = json!(["fz"]);
}
m["commands"] = junk.unwrap_or(json!(entries));
+409
View File
@@ -0,0 +1,409 @@
//! Importer plug-ins (`"kind": "importer"`, docs/plugins.md › Importers) through the engine: the
//! formats they add to `document.formats` and `document.open`, which files they are given, the
//! document their objects become, and what happens when they fail, decline or are canceled.
//!
//! A test binary of its own: installed plug-ins are process-wide, and the unit tests that check the
//! exact list of formats would see these ones.
// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md › Robustness).
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::path::PathBuf;
use serde_json::{Value, json};
use vectorcraft_engine::cmd::fileio;
use vectorcraft_engine::{EngineError, Session};
use vectorcraft_plugins::wat::{self as tpl, Given};
use vectorcraft_plugins::{Limits, Plugin};
/// An importer's output: one red rectangle from (10, 20) to (110, 70) and a warning.
const RECT: &str = r#"{"objects":[{"type":"path","name":"Box","path":{"subpaths":[{"closed":true,"anchors":[{"p":[10,20]},{"p":[110,20]},{"p":[110,70]},{"p":[10,70]}]}]},"fills":[{"type":"solid","color":{"model":"rgb","r":1,"g":0,"b":0}}]}],"warnings":["2 layers merged"]}"#;
/// An importer's output: a horizontal line from (0, 5) to (50, 5).
const LINE: &str = r#"{"objects":[{"type":"path","path":{"subpaths":[{"closed":false,"anchors":[{"p":[0,5]},{"p":[50,5]}]}]}}]}"#;
/// The `vc_run` body of an importer that never returns.
const SPINS: &str = "(loop $l (br $l)) (i64.const 0)";
/// Installs the plug-in written in WAT `src` → its `plugin.list` entry.
fn install(s: &mut Session, src: &str) -> Value {
let wasm = wat::parse_str(src).unwrap_or_else(|e| panic!("bad test WAT: {e}"));
s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&wasm)})).unwrap()
}
/// An importer `id` reading `extensions` (a JSON list) whose objects are the file it is given.
fn echo(s: &mut Session, id: &str, extensions: &str) -> Value {
install(s, &tpl::module(&tpl::importer(id, extensions, "{}"), tpl::ECHO, "", 1))
}
/// `document.open` of a file `name` holding `bytes`, with more params `extra`.
fn open(s: &mut Session, name: &str, bytes: &[u8], extra: Value) -> Result<Value, EngineError> {
let mut p = json!({"name": name, "dataBase64": vectorcraft_format::base64_encode(bytes)});
if let (Some(p), Some(extra)) = (p.as_object_mut(), extra.as_object()) {
p.extend(extra.clone());
}
s.execute("document.open", &p)
}
/// The active document's first artboard, as `(x0, y0, x1, y1)`.
fn artboard(s: &Session) -> (f64, f64, f64, f64) {
let r = s.doc().unwrap().doc.artboards[0].rect;
(r.x0, r.y0, r.x1, r.y1)
}
/// How many objects the active document's first layer holds.
fn objects(s: &Session) -> usize {
s.doc().unwrap().doc.layers[0].children().unwrap().len()
}
/// The open documents' count, the active one's index, its undo and redo steps, whether it has
/// unsaved changes, and its content.
fn state(s: &mut Session) -> (usize, Option<usize>, usize, usize, bool, Value) {
let content = s.execute("document.inspect", &json!({})).unwrap();
let d = s.doc().unwrap();
(s.documents().len(), s.active_index(), d.history.undo.len(), d.history.redo.len(), d.is_dirty(), content)
}
/// Opens a document through a new importer `id`, with an undo step and a redo step → its [`state`].
fn open_edited(s: &mut Session, id: &str) -> (usize, Option<usize>, usize, usize, bool, Value) {
install(s, &tpl::returning(&tpl::importer(id, r#"["tsk"]"#, "{}"), RECT));
open(s, "Kept.tsk", b"?", json!({"importer": format!("plugin.{id}")})).unwrap();
s.execute("select.all", &json!({})).unwrap();
s.execute("object.move", &json!({"dx": 5, "dy": 0})).unwrap();
s.execute("object.move", &json!({"dx": 5, "dy": 0})).unwrap();
s.execute("edit.undo", &json!({})).unwrap();
let before = state(s);
assert!(before.2 > 0 && before.3 > 0, "{before:?}");
before
}
/// A fresh folder for files this test writes.
fn tmp_dir(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("vc-plugin-import-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn path_of(p: &std::path::Path) -> String {
p.to_string_lossy().into_owned()
}
#[test]
fn an_importer_adds_a_read_only_format_while_it_is_installed() {
let mut s = Session::new();
let entry = echo(&mut s, "test.list", r#"["tsl","tsl2"]"#);
assert_eq!((&entry["kind"], &entry["format"], &entry["extensions"]), (&json!("importer"), &json!("plugin.test.list"), &json!(["tsl", "tsl2"])));
assert_eq!(entry["permissions"], json!(["import.read"]));
let formats = s.execute("document.formats", &json!({})).unwrap();
let listed = formats["formats"].as_array().unwrap().iter().find(|f| f["id"] == "plugin.test.list").expect("listed");
assert_eq!((&listed["label"], &listed["extensions"]), (&json!("Test test.list"), &json!(["tsl", "tsl2"])));
assert_eq!((&listed["read"], &listed["write"], &listed["plugin"]), (&json!(true), &json!(false), &json!("test.list")));
assert!(formats["readable"].as_array().unwrap().contains(&json!("plugin.test.list")));
assert!(!formats["writable"].as_array().unwrap().contains(&json!("plugin.test.list")));
assert!(formats["openExtensions"].as_array().unwrap().contains(&json!("tsl2")));
assert!(fileio::open_extensions().contains(&"tsl"));
assert!(!fileio::export_extensions().contains(&"tsl"));
assert_eq!(fileio::format(".TSL2").map(|f| f.id), Some("plugin.test.list"), "by any of its extensions, in any case");
s.execute("plugin.remove", &json!({"id": "test.list"})).unwrap();
let formats = s.execute("document.formats", &json!({})).unwrap();
assert!(!formats["readable"].as_array().unwrap().contains(&json!("plugin.test.list")));
assert!(!formats["openExtensions"].as_array().unwrap().contains(&json!("tsl")));
assert!(fileio::format("tsl").is_none());
assert!(open(&mut s, "a.tsl", RECT.as_bytes(), json!({})).is_err());
assert!(s.documents().is_empty());
}
/// A file of an extension only the importer lists opens as a new document of editable art, its
/// artboard the art's bounds, with the importer's warnings; Save then asks for a name.
#[test]
fn a_file_an_importer_reads_opens_as_editable_art() {
let dir = tmp_dir("open");
let mut s = Session::new();
echo(&mut s, "test.open", r#"["tso"]"#);
let r = open(&mut s, "Shapes.tso", RECT.as_bytes(), json!({})).unwrap();
assert_eq!((&r["format"], &r["warnings"], &r["restored"]), (&json!("plugin.test.open"), &json!(["2 layers merged"]), &json!(false)));
assert_eq!(s.documents().len(), 1);
assert_eq!(objects(&s), 1);
assert_eq!(artboard(&s), (10.0, 20.0, 110.0, 70.0));
// Editable: the rectangle moves like any path.
s.execute("select.all", &json!({})).unwrap();
s.execute("object.move", &json!({"dx": 5, "dy": 0})).unwrap();
// A line's artboard is at least 1 pt high.
open(&mut s, "Line.tso", LINE.as_bytes(), json!({})).unwrap();
assert_eq!(artboard(&s), (0.0, 4.5, 50.0, 5.5));
// Art at the edge of the canvas keeps its artboard within ±4,000,000 pt, so the document can
// still be edited: a vertical line at x = 4,000,000 (its 1 pt moves inward) and a stroked line
// along y = -4,000,000 (its stroke passes the edge).
let edge = r#"{"objects":[{"type":"path","path":{"subpaths":[{"closed":false,"anchors":[{"p":[4000000,0]},{"p":[4000000,50]}]}]}}]}"#;
open(&mut s, "Edge.tso", edge.as_bytes(), json!({})).unwrap();
assert_eq!(artboard(&s), (3_999_999.0, 0.0, 4_000_000.0, 50.0));
s.execute("select.all", &json!({})).unwrap();
s.execute("object.move", &json!({"dx": -5, "dy": 0})).unwrap();
let stroked = r#"{"objects":[{"type":"path","path":{"subpaths":[{"closed":false,"anchors":[{"p":[0,-4000000]},{"p":[50,-4000000]}]}]},"strokes":[{"paint":{"type":"solid","color":{"model":"rgb","r":0,"g":0,"b":0}},"width":10}]}]}"#;
open(&mut s, "Stroked.tso", stroked.as_bytes(), json!({})).unwrap();
let (x0, y0, x1, y1) = artboard(&s);
assert!(x0 < 0.0 && x1 > 50.0 && y0 == -4_000_000.0 && y1 > y0 + 1.0, "{:?}", artboard(&s));
s.execute("select.all", &json!({})).unwrap();
s.execute("object.move", &json!({"dx": 0, "dy": 5})).unwrap();
// From a path: the document keeps no path, so Save asks for a name.
let path = dir.join("Shapes.tso");
std::fs::write(&path, RECT).unwrap();
let r = s.execute("document.open", &json!({"path": path_of(&path)})).unwrap();
assert_eq!(r["format"], "plugin.test.open");
assert_eq!(s.doc().unwrap().path, None);
assert!(s.doc().unwrap().doc.title.contains("Shapes"), "{}", s.doc().unwrap().doc.title);
let _ = std::fs::remove_dir_all(&dir);
}
/// A built-in format keeps its files: an importer that lists `svg` is given an `.svg` only when
/// `importer` names it, and a file with a built-in format's content opens with the built-in reader
/// whatever its extension.
#[test]
fn built_in_formats_keep_their_files() {
let mut s = Session::new();
install(&mut s, &tpl::returning(&tpl::importer("test.svgtoo", r#"["tsv","svg"]"#, "{}"), RECT));
let svg = br#"<svg xmlns="http://www.w3.org/2000/svg" width="50" height="40"><rect width="10" height="10"/></svg>"#;
assert_eq!(open(&mut s, "a.svg", svg, json!({})).unwrap()["format"], "svg");
// A damaged SVG reports the SVG reader's error; the importer isn't tried instead.
match open(&mut s, "b.svg", b"<svg not closed", json!({})) {
Ok(r) => assert_eq!(r["format"], "svg"),
Err(e) => assert!(!e.to_string().contains("importer"), "{e}"),
}
// Named, the importer opens an .svg.
let r = open(&mut s, "c.svg", svg, json!({"importer": "plugin.test.svgtoo"})).unwrap();
assert_eq!(r["format"], "plugin.test.svgtoo");
// An SVG named as the importer's file is still an SVG.
assert_eq!(open(&mut s, "d.tsv", svg, json!({})).unwrap()["format"], "svg");
assert_eq!(fileio::format("svg").map(|f| f.id), Some("svg"));
assert_eq!(fileio::open_extensions().iter().filter(|e| **e == "svg").count(), 1);
}
/// File › Open lists an importer's extensions only where it would open the file with the importer:
/// not those a built-in format reads, and not those it opens as a plug-in, a shortcut set, a
/// script, presets or a library. An importer with none of its own gets no filter.
#[test]
fn file_open_lists_only_the_extensions_it_opens_with_an_importer() {
let mut s = Session::new();
install(&mut s, &tpl::returning(&tpl::importer("test.claimed", r#"["tcl","ase","wasm","kys","jsx","vcstyles","svg"]"#, "{}"), RECT));
install(&mut s, &tpl::returning(&tpl::importer("test.claimedonly", r#"["ase","gpl"]"#, "{}"), RECT));
for ext in ["ase", "gpl", "wasm", "kys", "jsx", "vcstyles"] {
assert!(fileio::opens_as_other(ext), "{ext}");
assert!(!fileio::open_extensions().contains(&ext), "All readable files: {ext}");
}
assert!(!fileio::opens_as_other("tcl") && !fileio::opens_as_other("svg"));
assert!(fileio::open_extensions().contains(&"tcl"));
let filters: Vec<_> = fileio::open_filters().collect();
assert!(filters.contains(&("Test test.claimed", &["tcl"][..])), "{filters:?}");
assert!(!filters.iter().any(|(label, _)| *label == "Test test.claimedonly"), "{filters:?}");
assert!(filters.contains(&("Composite fonts", vectorcraft_engine::cmd::compositefonts::EXTS)), "{filters:?}");
s.execute("plugin.remove", &json!({"id": "test.claimed"})).unwrap();
s.execute("plugin.remove", &json!({"id": "test.claimedonly"})).unwrap();
}
/// Several importers list one extension: the one whose id sorts first opens the file, and
/// `importer` picks another (by its format id or its id).
#[test]
fn the_first_importer_by_id_opens_a_shared_extension() {
let mut s = Session::new();
install(&mut s, &tpl::returning(&tpl::importer("test.shared.b", r#"["tsh"]"#, "{}"), LINE));
install(&mut s, &tpl::returning(&tpl::importer("test.shared.a", r#"["tsh"]"#, "{}"), RECT));
assert_eq!(open(&mut s, "x.tsh", b"?", json!({})).unwrap()["format"], "plugin.test.shared.a");
assert_eq!(open(&mut s, "x.tsh", b"?", json!({"importer": "plugin.test.shared.b"})).unwrap()["format"], "plugin.test.shared.b");
assert_eq!(open(&mut s, "x.tsh", b"?", json!({"importer": "test.shared.b"})).unwrap()["format"], "plugin.test.shared.b");
}
#[test]
fn importer_names_that_do_not_fit_are_errors() {
let mut s = Session::new();
echo(&mut s, "test.names", r#"["tsn"]"#);
install(&mut s, &tpl::returning(&tpl::manifest("test.names.filter", "filter", "{}"), r#"{"objects":[]}"#));
install(&mut s, &tpl::returning(&tpl::exporter("test.names.exporter", r#"["tsx"]"#, "{}"), r#"{"dataBase64":""}"#));
let cases = [
(json!({"importer": "plugin.test.names"}), "x.png", "opens .tsn files, not `x.png`"),
(json!({"importer": "plugin.test.names.filter"}), "x.tsn", "is a plug-in of kind `filter`, not an importer"),
(json!({"importer": "plugin.test.names.exporter"}), "x.tsn", "is a plug-in of kind `exporter`, not an importer"),
(json!({"importer": "plugin.test.nothere"}), "x.tsn", "test.nothere"),
(json!({"importer": 3}), "x.tsn", "`importer` must be a plug-in format id"),
];
for (extra, name, why) in cases {
let e = open(&mut s, name, RECT.as_bytes(), extra.clone()).unwrap_err();
assert!(matches!(e, EngineError::BadParams { .. }), "{extra}: {e}");
assert!(e.to_string().contains(why), "{extra}: {e}");
}
assert!(s.documents().is_empty());
// Without an importer, a file nothing reads stays unsupported.
assert!(open(&mut s, "x.tsunknown", RECT.as_bytes(), json!({})).is_err());
// An importer is not a filter.
s.execute("file.new", &json!({})).unwrap();
let e = s.execute("plugin.run", &json!({"id": "test.names"})).unwrap_err();
assert!(e.to_string().contains("document.open"), "{e}");
}
/// The importer is given its parameters, checked against its manifest, and `_context` with the
/// file's extension and name.
#[test]
fn the_importer_is_given_its_parameters_and_the_file_s_name() {
let mut s = Session::new();
let params = r#"{"density":{"type":"number","min":0,"max":10,"default":4}}"#;
let needles = [r#""mode":"import""#, r#""extension":"tsc""#, r#""name":"Shapes""#, r#""density":10.0"#];
for (i, needle) in needles.into_iter().enumerate() {
// RECT when the parameters hold the needle; the file, which isn't JSON, otherwise.
let wasm = wat::parse_str(tpl::when_given(&tpl::importer("test.params", r#"["tsc"]"#, params), Given::Params, needle, RECT)).unwrap();
s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "replace": i > 0})).unwrap();
let r = open(&mut s, "Shapes.tsc", b"not json", json!({"params": {"density": 12}}));
assert_eq!(r.map(|r| r["format"].clone()).map_err(|e| e.to_string()), Ok(json!("plugin.test.params")), "{needle}");
}
let before = s.documents().len();
let e = open(&mut s, "Shapes.tsc", b"", json!({"params": {"density": "lots"}})).unwrap_err();
assert!(matches!(e, EngineError::BadParams { .. }), "{e}");
assert_eq!(s.documents().len(), before);
}
#[test]
fn failures_open_nothing() {
let mut s = Session::new();
let far = r#"{"objects":[{"type":"path","path":{"subpaths":[{"closed":false,"anchors":[{"p":[0,0]},{"p":[9000000,0]}]}]}}]}"#;
let bad_warnings = LINE.replace("]}]}}]}", r#"]}]}}],"warnings":"no"}"#);
let cases = [
("test.fail.declines", r#"{"error":"Not a drawing."}"#, "Not a drawing."),
("test.fail.notjson", "not json", "JSON"),
("test.fail.empty", r#"{"objects":[]}"#, "the importer returned no objects"),
("test.fail.key", r#"{"objects":[],"layers":[]}"#, "an importer returns `objects` and `warnings`, not `layers`"),
("test.fail.id", r#"{"objects":[{"id":3}]}"#, "is not the id of an input object"),
("test.fail.far", far, "4000000"),
("test.fail.warnings", bad_warnings.as_str(), "`warnings` must be a list of strings"),
];
for (id, output, why) in cases {
install(&mut s, &tpl::returning(&tpl::importer(id, r#"["tsf"]"#, "{}"), output));
let e = open(&mut s, "x.tsf", b"?", json!({"importer": format!("plugin.{id}")})).unwrap_err();
assert!(e.to_string().contains(why), "{id}: {e}");
assert!(e.to_string().contains("x.tsf"), "{id}: names the file: {e}");
}
install(&mut s, &tpl::module(&tpl::importer("test.fail.traps", r#"["tsf"]"#, "{}"), "(unreachable)", "", 1));
assert!(open(&mut s, "x.tsf", b"?", json!({"importer": "plugin.test.fail.traps"})).is_err());
assert!(s.documents().is_empty(), "nothing opened");
// A document that is already open keeps its tab, content and undo and redo steps.
install(&mut s, &tpl::module(&tpl::importer("test.fail.spins", r#"["tsf"]"#, "{}"), SPINS, "", 1));
let before = open_edited(&mut s, "test.fail.opens");
for id in cases.iter().map(|c| c.0).chain(["test.fail.traps"]) {
assert!(open(&mut s, "x.tsf", b"?", json!({"importer": format!("plugin.{id}")})).is_err(), "{id}");
assert_eq!(state(&mut s), before, "{id}");
}
let token = vectorcraft_doc::cancel::CancelToken::new();
token.cancel();
let r = vectorcraft_doc::cancel::scope(&token, None, || open(&mut s, "x.tsf", b"?", json!({"importer": "plugin.test.fail.spins"})));
assert!(matches!(r, Err(EngineError::Cancelled)), "{r:?}");
assert_eq!(state(&mut s), before, "canceled");
}
/// Cancel stops an importer between two slices of its budget, and nothing opens: here one that
/// never returns, run with a token canceled beforehand.
#[test]
fn a_canceled_open_stops_the_importer() {
let mut s = Session::new();
install(&mut s, &tpl::module(&tpl::importer("test.spins", r#"["tss"]"#, "{}"), SPINS, "", 1));
let token = vectorcraft_doc::cancel::CancelToken::new();
token.cancel();
let r = vectorcraft_doc::cancel::scope(&token, None, || open(&mut s, "x.tss", b"?", json!({})));
assert!(matches!(r, Err(EngineError::Cancelled)), "{r:?}");
assert!(s.documents().is_empty());
}
/// An importer that never returns, held to a small import budget, ends at its instruction limit
/// or at its time limit (0 ms, which has passed at the first check between two slices of the
/// budget). The open reports the limit, opens no document, and leaves an open document's content
/// and undo and redo steps as they were.
#[test]
fn an_importer_past_its_budget_opens_nothing() {
let cases = [
("test.budget.fuel", Limits { export_fuel: 1_000_000, ..Limits::default() }, "instruction budget"),
("test.budget.time", Limits { export_fuel: 1_000_000_000, export_wall_time_ms: 0, ..Limits::default() }, "time budget"),
];
for (id, limits, _) in &cases {
let wasm = wat::parse_str(tpl::module(&tpl::importer(id, r#"["tbg"]"#, "{}"), SPINS, "", 1)).unwrap();
vectorcraft_plugins::registry::install(Plugin::load(&wasm, limits.clone()).unwrap());
}
let mut s = Session::new();
for (id, _, why) in &cases {
let e = open(&mut s, "x.tbg", b"?", json!({"importer": format!("plugin.{id}")})).unwrap_err();
assert!(e.to_string().contains(why) && e.to_string().contains("x.tbg"), "{id}: {e}");
assert!(s.documents().is_empty(), "{id}");
}
let before = open_edited(&mut s, "test.budget.opens");
for (id, _, why) in &cases {
let e = open(&mut s, "x.tbg", b"?", json!({"importer": format!("plugin.{id}")})).unwrap_err();
assert!(e.to_string().contains(why), "{id}: {e}");
assert_eq!(state(&mut s), before, "{id}");
}
for (id, _, _) in &cases {
vectorcraft_plugins::registry::remove(id);
}
}
/// A file past the importer's input limit is not read past it, and doesn't open: an open document
/// keeps its content and its undo and redo steps. Given as data, it is refused the same way.
#[test]
fn a_file_past_the_input_limit_is_refused() {
let dir = tmp_dir("large");
let mut s = Session::new();
echo(&mut s, "test.large", r#"["tsb"]"#);
let path = dir.join("big.tsb");
let max = vectorcraft_plugins::import::MAX_INPUT_BYTES;
std::fs::File::create(&path).unwrap().set_len(max as u64 + 1).unwrap();
let e = s.execute("document.open", &json!({"path": path_of(&path)})).unwrap_err();
assert!(e.to_string().contains("larger than"), "{e}");
assert!(e.to_string().contains("big.tsb"), "{e}");
assert!(s.documents().is_empty());
let before = open_edited(&mut s, "test.large.opens");
let e = s.execute("document.open", &json!({"path": path_of(&path)})).unwrap_err();
assert!(e.to_string().contains("larger than"), "{e}");
assert_eq!(state(&mut s), before, "by path");
let e = open(&mut s, "big.tsb", &vec![0; max + 1], json!({})).unwrap_err();
assert!(e.to_string().contains("larger than"), "{e}");
assert_eq!(state(&mut s), before, "as data");
// The size is checked before the text is decoded: this text is not base64 at all.
let not_base64 = "!".repeat((max + 1) / 3 * 4 + 8);
for extra in [json!({}), json!({"importer": "plugin.test.large"})] {
let mut p = json!({"name": "big.tsb", "dataBase64": not_base64});
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
let e = s.execute("document.open", &p).unwrap_err();
assert!(e.to_string().contains("larger than"), "{e}");
assert_eq!(state(&mut s), before, "before decoding");
}
// White space is not part of the file's size.
let spaced = format!("QUJD{}", " ".repeat(max / 3 * 4 + 8));
if let Err(e) = s.execute("document.open", &json!({"name": "small.tsb", "dataBase64": spaced})) {
assert!(!e.to_string().contains("larger than"), "{e}");
}
let _ = std::fs::remove_dir_all(&dir);
}
/// A file past the importer's input limit whose start shows a built-in format opens as that format,
/// whatever its extension; named, the importer's limit applies to it.
#[test]
fn a_built_in_file_past_the_input_limit_still_opens() {
let dir = tmp_dir("largebuiltin");
let mut s = Session::new();
echo(&mut s, "test.largepng", r#"["tsp"]"#);
let path = dir.join("big.tsp");
let mut png = std::io::Cursor::new(vec![]);
image::RgbaImage::new(2, 2).write_to(&mut png, image::ImageFormat::Png).unwrap();
std::fs::write(&path, png.get_ref()).unwrap();
std::fs::OpenOptions::new().write(true).open(&path).unwrap().set_len(vectorcraft_plugins::import::MAX_INPUT_BYTES as u64 + 1).unwrap();
let e = s.execute("document.open", &json!({"path": path_of(&path), "importer": "plugin.test.largepng"})).unwrap_err();
assert!(e.to_string().contains("larger than"), "{e}");
assert!(s.documents().is_empty());
let r = s.execute("document.open", &json!({"path": path_of(&path)})).unwrap();
assert_eq!(r["format"], "png");
let mut data = png.into_inner();
data.resize((vectorcraft_plugins::import::MAX_INPUT_BYTES + 3) / 3 * 3, 0);
let encoded = json!({"name": "big.tsp", "dataBase64": vectorcraft_format::base64_encode(&data)});
assert_eq!(s.execute("document.open", &encoded).unwrap()["format"], "png");
let _ = std::fs::remove_dir_all(&dir);
}
+19
View File
@@ -198,6 +198,25 @@ fn installing_a_disabled_plug_in_again_turns_it_on() {
let _ = std::fs::remove_dir_all(&dir);
}
/// When the grant of an update can't be stored, the store keeps the old module, and it stays disabled.
#[test]
fn a_disabled_plug_in_whose_new_grant_cant_be_stored_stays_disabled() {
let _f = fresh();
let dir = tmp_dir("reinstall-no-grant");
let mut s = launch(&dir);
let manifest = r#"{"id":"test.sm.g","name":"Test","version":"1","kind":"filter","permissions":["doc.read"]}"#;
let module = |out: &str| vectorcraft_format::base64_encode(&wat::parse_str(tpl::returning(manifest, out)).unwrap());
s.execute("plugin.install", &json!({"dataBase64": module(r#"{"objects":[]}"#), "grant": ["doc.read"]})).unwrap();
s.execute("plugin.setEnabled", &json!({"id": "test.sm.g", "enabled": false})).unwrap();
let grants = dir.join("grants.json");
std::fs::remove_file(&grants).unwrap();
std::fs::create_dir_all(grants.join("block")).unwrap();
s.execute("plugin.install", &json!({"dataBase64": module(r#"{"objects":[],"error":null}"#), "grant": ["doc.read"], "replace": true})).unwrap();
assert_eq!(safe_mode::disabled().len(), 1, "the module the update could not replace stays disabled");
safe_mode::set_disabled(vec![]);
let _ = std::fs::remove_dir_all(&dir);
}
/// A launch where the stored plug-in `id` stopped while loading: it is installed, then disabled by
/// its stored file alone (as Disable and Continue saves it), and the next launch skips it.
fn stored_plug_in_disabled_by_file(dir: &std::path::Path, id: &str) -> Session {
+8 -1
View File
@@ -169,7 +169,8 @@
//! layers and opacity masks nested too deep, and, on objects written without an art style, live
//! effects other than geometry effects left out; graphic styles the reader wouldn't read, links to
//! graphic styles on objects not written with them, and every art style on a large canvas, are left
//! out.
//! out; art past [`crate::import::MAX_EDITING_OBJECTS`] objects in all (the most the reader reads),
//! counted as the reader counts them, is left out.
//!
//! The data streams hold the program compressed with Zstandard after `%AI24_ZStandard_Data`, with
//! the version numbers of the newest files users sent ([`private_data`]).
@@ -242,6 +243,11 @@ pub struct AiEditingOutput {
/// Write `doc` as the editing data of a `.ai` file: every artboard and layer.
pub fn export_ai_editing(doc: &Document, o: &AiEditingOptions) -> Result<AiEditingOutput, String> {
export_within(doc, o, crate::import::MAX_EDITING_OBJECTS)
}
/// [`export_ai_editing`] writing at most `max_objects` objects, counted as the reader counts them.
fn export_within(doc: &Document, o: &AiEditingOptions, max_objects: usize) -> Result<AiEditingOutput, String> {
let board = doc.artboards.first().ok_or("the document has no artboard")?.rect;
if !(board.is_finite() && board.width() > 0.0 && board.height() > 0.0) {
return Err("the document has no artboard".into());
@@ -283,6 +289,7 @@ pub fn export_ai_editing(doc: &Document, o: &AiEditingOptions) -> Result<AiEditi
// On a large canvas the layers' symbol instances are written as their art.
w.symbols_live = scale == 1.0;
w.symbol_doc = symbol_doc;
w.max_objects = max_objects;
w.reserve_swatch_gradients();
if scale == 1.0 {
w.styled = styled;
+172
View File
@@ -833,6 +833,178 @@ fn nested_symbol_instances_stop_at_the_symbol_art_limit() {
assert_eq!(layers.matches("\r\nf\r\n").count(), 3198, "the squares of the leaves written");
}
/// The documents [`the_writer_counts_objects_as_the_reader_reads_them`] writes: groups, compound
/// paths, guides, sublayers, clipping groups and layers, painted clipping paths, opacity masks,
/// gradients, pattern definitions and tiles, images, type, symbol definitions and instances,
/// objects with art styles and objects bound to variables.
fn counted() -> Vec<(&'static str, Document)> {
vec![
("sample", sample()),
("unsupported", unsupported()),
("transparent", transparent()),
("gradients and patterns", gradients_and_patterns()),
("images", images_doc()),
("typed", typed()),
("variables", variables_doc()),
("symbols", symbols_document()),
("styles", styles_document()),
("painted clips", painted_clips()),
("blend", blend_doc()),
]
}
/// A blend of two squares 20 pt apart with a step every 10 pt, written as its steps.
fn blend_doc() -> Document {
let mut doc = Document::new(200.0, 100.0);
let layer = doc.layers[0].id;
let keys = vec![
Arc::new(square(&mut doc, Rect::new(10.0, 10.0, 12.0, 12.0), Color::gray(0.5))),
Arc::new(square(&mut doc, Rect::new(30.0, 10.0, 32.0, 12.0), Color::gray(0.5))),
];
let spec = vectorcraft_doc::BlendSpec { spacing: vectorcraft_doc::BlendSpacing::Distance(10.0), ..Default::default() };
let blend = Node::new(doc.alloc_id(), NodeKind::Blend { children: keys, spec: Box::new(spec) });
doc.insert(Some(layer), usize::MAX, blend).unwrap();
doc
}
/// Clipping groups whose clipping paths are filled: a square with a color, and a ring (a path of
/// two subpaths) with a color, with a gradient, and with a color and a gradient stroke.
fn painted_clips() -> Document {
let mut doc = Document::new(260.0, 100.0);
let layer = doc.layers[0].id;
let ring = |x: f64| {
let mut p = shapes::rectangle(Rect::new(x, 10.0, x + 20.0, 30.0));
p.subpaths.extend(shapes::rectangle(Rect::new(x + 5.0, 15.0, x + 15.0, 25.0)).subpaths);
p
};
let g = Gradient::new(
GradientKind::Linear,
vec![GradientStop::new(0.0, Color::rgb(1.0, 1.0, 1.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 0.0))],
);
let shade = || Paint::Gradient(Box::new(GradientPaint::new(g.clone())));
let clips = [
(10.0, shapes::rectangle(Rect::new(10.0, 10.0, 30.0, 30.0)), solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None),
(70.0, ring(70.0), solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None),
(130.0, ring(130.0), shade(), Paint::None),
(190.0, ring(190.0), solid(Color::rgb(0.0, 1.0, 0.0)), shade()),
];
for (x, path, fill, stroke) in clips {
let mut clip = clipping(&mut doc, path);
let width = if matches!(stroke, Paint::None) { 0.0 } else { 2.0 };
clip.appearance = Appearance::basic(fill, stroke, width).into();
let inside = square(&mut doc, Rect::new(x - 5.0, 5.0, x + 10.0, 20.0), Color::gray(0.5));
let g = group(&mut doc, vec![clip, inside], true);
doc.insert(Some(layer), usize::MAX, g).unwrap();
}
doc
}
/// The writer counts the objects it writes as the reader counts the objects it reads: with room
/// for as many objects as the reader reads, nothing is left out; with one less, art is left out,
/// with a warning. With room for fewer, the reader never reads more objects than there is room for.
#[test]
fn the_writer_counts_objects_as_the_reader_reads_them() {
let objects = |out: &AiEditingOutput| crate::import::objects_read(&out.program).unwrap();
let limited = |out: &AiEditingOutput| out.warnings.iter().any(|w| w == writer::OBJECT_LIMIT);
for (name, doc) in counted() {
let all = export_ai_editing(&doc, &opts()).unwrap();
let read = objects(&all);
assert!(read > 0 && !limited(&all), "{name}: {read} objects, {:?}", all.warnings);
let room = export_within(&doc, &opts(), read).unwrap();
assert!(!limited(&room), "{name}: nothing is left out with room for {read} objects");
assert_eq!(room.program, all.program, "{name}");
for max in 0..read {
let out = export_within(&doc, &opts(), max).unwrap();
assert!(limited(&out), "{name}: art is left out with room for {max} of {read} objects");
assert!(objects(&out) <= max, "{name}: {} objects read with room for {max}", objects(&out));
}
}
}
/// An opacity mask whose art doesn't all fit within the object limit is left out with its art,
/// and the object it masks is written without it; the art left out leaves room for the objects
/// after it.
#[test]
fn a_mask_whose_art_passes_the_object_limit_is_left_out() {
let mut doc = Document::new(200.0, 100.0);
let layer = doc.layers[0].id;
let mut masked = square(&mut doc, Rect::new(10.0, 10.0, 50.0, 50.0), Color::gray(0.5));
let a = square(&mut doc, Rect::new(0.0, 0.0, 30.0, 30.0), Color::rgb(1.0, 1.0, 1.0));
let b = square(&mut doc, Rect::new(30.0, 30.0, 60.0, 60.0), Color::rgb(1.0, 1.0, 1.0));
let art = group(&mut doc, vec![a, b], false);
masked.extras_mut().mask = Some(Box::new(OpacityMask::new(art, false)));
doc.insert(Some(layer), usize::MAX, masked).unwrap();
let after = square(&mut doc, Rect::new(100.0, 10.0, 140.0, 50.0), Color::gray(0.5));
doc.insert(Some(layer), usize::MAX, after).unwrap();
let text = |max: usize| {
let out = export_within(&doc, &opts(), max).unwrap();
let limited = out.warnings.iter().any(|w| w == writer::OBJECT_LIMIT);
(String::from_utf8_lossy(&out.program).replace("\r\n", "\n"), limited)
};
let read = crate::import::objects_read(&export_ai_editing(&doc, &opts()).unwrap().program).unwrap();
let (all, limited) = text(read);
assert!(!limited && all.contains("(AI9_Mask)"), "{all}");
let has_mask = |max: usize| text(max).0.contains("(AI9_Mask)");
let Some(fit) = (0..=read).find(|&max| has_mask(max)) else { panic!("the mask fits with room for {read} objects") };
assert!(fit > 2, "the mask needs room beyond the two squares: {fit}");
for max in fit..=read {
assert!(has_mask(max), "room for {max} of {read}, the mask fits in {fit}");
}
let (short, limited) = text(fit - 1);
assert!(limited);
assert!(!short.contains("(AI9_Mask)") && !short.contains("%_/ArtDictionary :\n%_/Mask"), "{short}");
assert_eq!(short.matches("\nf\n").count(), 2, "both squares are written: {short}");
}
/// A symbol's definition is written only when there is room for its instance too.
#[test]
fn a_symbol_definition_is_written_only_with_room_for_its_instance() {
let mut doc = Document::new(200.0, 100.0);
let layer = doc.layers[0].id;
let a = square(&mut doc, Rect::new(0.0, 0.0, 10.0, 10.0), Color::gray(0.5));
let b = square(&mut doc, Rect::new(10.0, 10.0, 20.0, 20.0), Color::gray(0.5));
let art = group(&mut doc, vec![a, b], false);
doc.symbols.push(Symbol { dynamic: false, name: "Pair".into(), art: Arc::new(art) });
let placed = instance(&mut doc, "Pair", Affine::translate((50.0, 10.0)));
doc.insert(Some(layer), usize::MAX, placed).unwrap();
add_rect(&mut doc, Rect::new(100.0, 10.0, 110.0, 20.0));
let program = export_ai_editing(&doc, &opts()).unwrap().program;
let all = String::from_utf8_lossy(&program).replace("\r\n", "\n");
assert!(all.contains("%AI14_BeginSymbol") && all.contains("/SymbolInstance :"), "{all}");
let read = crate::import::objects_read(&program).unwrap();
for max in 0..read {
let out = export_within(&doc, &opts(), max).unwrap();
let text = String::from_utf8_lossy(&out.program).replace("\r\n", "\n");
let defined = text.contains("%AI14_BeginSymbol");
assert_eq!(defined, text.contains("/SymbolInstance :"), "room for {max} of {read}: {text}");
assert!(out.warnings.iter().any(|w| w == writer::OBJECT_LIMIT), "art is left out with room for {max} of {read} objects");
let objects = crate::import::objects_read(&out.program).unwrap();
assert!(objects <= max, "{objects} objects read with room for {max}");
}
}
/// The writer's limit is the reader's, which its warning names.
#[test]
fn the_object_limit_is_the_readers() {
assert_eq!(crate::import::MAX_EDITING_OBJECTS, 4_000_000);
assert!(writer::OBJECT_LIMIT.contains("4,000,000"));
}
/// Paths past the object limit are left out of a layer of many paths, with a warning: the paths
/// that fit are written.
#[test]
fn paths_past_the_object_limit_are_left_out() {
let mut doc = Document::new(200.0, 100.0);
for i in 0..10 {
add_rect(&mut doc, Rect::new(f64::from(i) * 10.0, 0.0, f64::from(i) * 10.0 + 5.0, 5.0));
}
let out = export_within(&doc, &opts(), 6).unwrap();
assert!(out.warnings.iter().any(|w| w == writer::OBJECT_LIMIT), "{:?}", out.warnings);
assert_eq!(crate::import::objects_read(&out.program).unwrap(), 6);
let text = String::from_utf8_lossy(&out.program).replace("\r\n", "\n");
assert_eq!(text.matches("\nf\n").count(), 6, "the first six paths are written");
}
/// On a canvas of the usual size, nested symbols whose instances multiply, written as their art in
/// art cut to a clipping object, stop at [`vectorcraft_doc::MAX_SYMBOL_NODES`] objects of symbol art
/// in all, with a warning.
+225 -43
View File
@@ -62,6 +62,7 @@ const GROUPED_LAYER: &str =
pub(super) const EFFECTS: &str = "live effects other than geometry effects are left out of the editing data for other apps";
pub(super) const SWATCHES: &str = "freeform gradient swatches, gradient swatches of more than 256 stops, and pattern swatches whose patterns the data can't define, are left out of the editing data for other apps";
pub(super) const SYMBOL_LIMIT: &str = "symbol instances past 16,000,000 objects of symbol art in all are left out of the editing data for other apps";
pub(super) const OBJECT_LIMIT: &str = "art past 4,000,000 objects in all is left out of the editing data for other apps";
const TEMPLATES: &str = "template layers are written as dimmed layers that don't print in the editing data for other apps";
/// The transparency `mode opacity isolate knockout shape Xy` sets: the blending mode ([`blend_code`]),
@@ -117,6 +118,16 @@ fn blend_code(b: BlendMode) -> u8 {
}
}
/// What the writer had charged when a part of the art started ([`Writer::charges`]).
#[derive(Clone, Copy)]
struct Charges {
objects: usize,
symbol_nodes: u64,
kept_objects: usize,
kept_symbol_nodes: u64,
left_out: usize,
}
pub(super) struct Writer<'a> {
doc: &'a Document,
/// The document → art space.
@@ -144,6 +155,18 @@ pub(super) struct Writer<'a> {
brushes: Option<Vec<vectorcraft_brush::Brush>>,
/// The objects of the symbol art written so far, within [`vectorcraft_doc::MAX_SYMBOL_NODES`].
symbol_nodes: u64,
/// The objects written so far, counted as the reader counts the objects it reads.
objects: usize,
/// How many times art was left out for [`Self::max_objects`].
left_out: usize,
/// The objects of the pattern and symbol definitions written so far, which stay written when
/// the art that first used them is not ([`Self::give_back`]).
definition_objects: usize,
/// The objects of symbol art in those definitions ([`Self::symbol_nodes`]).
definition_symbol_nodes: u64,
/// The most objects written ([`Self::take_objects`]): the most the reader reads,
/// [`crate::import::MAX_EDITING_OBJECTS`].
pub(super) max_objects: usize,
/// The document → the space gradients' matrices (`Xm`, `Bm`, `Bc`) and focal points are
/// written in: art space, and in a pattern's definition [`Self::tile_grad_space`].
grad_space: Affine,
@@ -286,6 +309,85 @@ impl<'a> Writer<'a> {
styled: HashMap::new(),
styles: styles::Styles::default(),
in_look: false,
objects: 0,
left_out: 0,
definition_objects: 0,
definition_symbol_nodes: 0,
max_objects: crate::import::MAX_EDITING_OBJECTS,
}
}
/// Whether `count` more objects may be written: the objects written so far, counted as the
/// reader counts them ([`object_count`]), stay within [`Self::max_objects`]. Past it the art is
/// left out, with a warning.
fn take_objects(&mut self, count: usize) -> bool {
let total = self.objects.saturating_add(count);
if total > self.max_objects {
self.warn(OBJECT_LIMIT);
self.left_out += 1;
return false;
}
self.objects = total;
true
}
/// Start a group with `op` (`u`, or `q` for a clipping group), counted as one object
/// ([`Self::take_objects`]). False, writing nothing, when there is no room for it.
fn open_group(&mut self, op: &str) -> bool {
if !self.take_objects(1) {
return false;
}
self.line(op);
true
}
/// Whether `n`, an instance of `symbol` placed by `xf`, is written as a live instance
/// ([`Self::live_instance`]). The instance is one object, without the art of its definition,
/// and room for it is kept before the definition is written.
fn live_instance_in_room(&mut self, n: &Node, symbol: &str, xf: Affine) -> bool {
if !self.take_objects(1) {
return false;
}
if self.live_instance(n, symbol, xf) {
return true;
}
self.objects -= 1;
false
}
/// What has been charged so far, for [`Self::keep_since`] and [`Self::give_back`].
fn charges(&self) -> Charges {
Charges {
objects: self.objects,
symbol_nodes: self.symbol_nodes,
kept_objects: self.definition_objects,
kept_symbol_nodes: self.definition_symbol_nodes,
left_out: self.left_out,
}
}
/// Whether art was left out for [`Self::max_objects`] since `c`.
fn left_out_since(&self, c: Charges) -> bool {
self.left_out != c.left_out
}
/// A definition that started at `c` is written: what was charged since stays charged when the
/// art that first used it is given back ([`Self::give_back`]).
fn keep_since(&mut self, c: Charges) {
self.definition_objects = c.kept_objects + (self.objects - c.objects);
self.definition_symbol_nodes = c.kept_symbol_nodes + (self.symbol_nodes - c.symbol_nodes);
}
/// Art written since `c` as `lines` is not used: the objects and symbol art charged since are
/// given back, except those of the definitions written since, and the images `lines` marks
/// are emptied (the others keep their indices).
fn give_back(&mut self, c: Charges, lines: &str) {
self.objects = c.objects + (self.definition_objects - c.kept_objects);
self.symbol_nodes = c.symbol_nodes + (self.definition_symbol_nodes - c.kept_symbol_nodes);
for index in lines.split(PAYLOAD).skip(1).step_by(2) {
if let Some(p) = index.parse::<usize>().ok().and_then(|i| self.payloads.get_mut(i)) {
*p = Vec::new();
}
}
}
@@ -510,19 +612,22 @@ impl<'a> Writer<'a> {
NodeKind::Layer { .. } => {}
NodeKind::Group { children, clip: true } => self.clip_group(children, xy),
NodeKind::Group { children, clip: false } => {
self.line("u");
for c in children {
self.node(c);
if self.open_group("u") {
for c in children {
self.node(c);
}
self.end_group("U", xy);
}
self.end_group("U", xy);
}
NodeKind::Path { path, rule, guide, .. } => {
if *guide {
// Its own transparency, not the one the object before it set.
if path.subpaths.iter().any(|sp| !sp.anchors.is_empty()) {
// A path for each subpath with points, with the guide's own transparency, not
// the one the object before it set.
let subpaths = path.subpaths.iter().filter(|sp| !sp.anchors.is_empty()).count();
if subpaths > 0 && self.take_objects(subpaths) {
self.set_object_xy(xy.object());
ai_ops::guide(&mut self.body, self.to_art, path, true);
}
ai_ops::guide(&mut self.body, self.to_art, path, true);
} else {
self.shape(n, path, *rule, xy);
}
@@ -530,8 +635,11 @@ impl<'a> Writer<'a> {
NodeKind::Compound { rule, .. } => self.shape(n, &compound_path(n), *rule, xy),
NodeKind::Text(_) => self.text(n, xy),
NodeKind::Image(im) => self.image(im, xy),
NodeKind::SymbolInstance { symbol, xf } if self.live_instance(n, symbol, *xf) => self.symbol_instance(symbol, *xf, xy),
NodeKind::SymbolInstance { symbol, xf } if self.live_instance_in_room(n, symbol, *xf) => self.symbol_instance(symbol, *xf, xy),
NodeKind::SymbolInstance { symbol, xf } => {
if !self.open_group("u") {
return;
}
// The canvas places an instance inside a symbol's art where the outer instance puts
// it (9-slice guides, stains and patterns included), and this art doesn't move with it.
self.wrote_fixed |= self.in_symbol;
@@ -541,7 +649,6 @@ impl<'a> Writer<'a> {
} else {
self.warn(symbols::MISSING_SYMBOL);
}
self.line("u");
if let Some(sym) = doc.symbols.iter().find(|s| &s.name == symbol)
&& self.nest < MAX_NEST
{
@@ -583,8 +690,10 @@ impl<'a> Writer<'a> {
| NodeKind::Repeat(_)
| NodeKind::PlacedDocument(_)
| NodeKind::Intertwine { .. } => {
if !self.open_group("u") {
return;
}
self.warn(LIVE);
self.line("u");
if self.nest < MAX_NEST {
let g = vectorcraft_effects::expand_live_deep(Some(self.doc), n);
self.nest += 1;
@@ -629,11 +738,14 @@ impl<'a> Writer<'a> {
if name.is_none() && mask.is_none() && bound.is_empty() && entries.is_none() {
return;
}
let start = self.body.len();
let has_id = name.is_some() || !bound.is_empty();
let has_entries = entries.is_some();
self.body.push_str("%_/ArtDictionary :\n");
if let Some(e) = entries {
self.body.push_str(&e);
}
if name.is_some() || !bound.is_empty() {
if has_id {
let id = self.take_id(name.unwrap_or("XMLID"));
let _ = writeln!(self.body, "%_/XMLUID : {} ; (AI10_ArtUID) ,", ps::bytes(id.as_bytes()));
if name.is_none() {
@@ -644,8 +756,13 @@ impl<'a> Writer<'a> {
self.bound.push((n.id, id, bound));
}
}
if let Some(m) = mask {
self.mask(m);
// A mask whose art doesn't fit is left out, and with nothing else in it the dictionary too.
if let Some(m) = mask
&& !self.mask(m)
&& !has_id
&& !has_entries
{
return self.body.truncate(start);
}
self.body.push_str("%_;\n%_\n");
}
@@ -673,10 +790,13 @@ impl<'a> Writer<'a> {
/// An opacity mask, as an entry of its object's dictionary: `/Mask :`, `X=`, its art on `%_`
/// lines (from the state a dictionary's art starts in: unlocked, shown, opaque), `X+`, `(Art) ,`,
/// its options as the `/Bool` entries `Clipping`, `Inverted`, `Disabled` and `Linked`, and
/// `; (AI9_Mask) ,`. The art is in document coordinates, as the object's is.
fn mask(&mut self, m: &OpacityMask) {
/// `; (AI9_Mask) ,`. The art is in document coordinates, as the object's is. False, writing
/// nothing, when some of the art is left out for the object limit ([`Self::take_objects`]): a
/// mask without all its art would change what it shows.
fn mask(&mut self, m: &OpacityMask) -> bool {
let outer = std::mem::take(&mut self.body);
let (state, clip) = (self.state, self.clip.take());
let (state, gs, clip) = (self.state, self.gs, self.clip.take());
let (charges, bound) = (self.charges(), self.bound.len());
(self.state, self.gs) = (Some((false, false)), Some(Xy::OPAQUE));
// The canvas draws the art's root even when it is hidden, and the reader puts a hidden object
// in a group that doesn't draw it, so a hidden root is written shown.
@@ -709,6 +829,13 @@ impl<'a> Writer<'a> {
self.hidden -= 1;
self.nest -= 1;
let lines = std::mem::replace(&mut self.body, outer);
if self.left_out_since(charges) {
// The art isn't written, nor are the variable bindings of its objects.
self.give_back(charges, &lines);
self.bound.truncate(bound);
(self.state, self.gs, self.clip, self.block_end) = (state, gs, clip, usize::MAX);
return false;
}
self.body.push_str("%_/Mask :\n%_X=\n");
hide(&lines, &mut self.body);
self.body.push_str("%_X+\n%_(Art) ,\n");
@@ -718,6 +845,7 @@ impl<'a> Writer<'a> {
self.body.push_str("%_; (AI9_Mask) ,\n");
// The dictionary ends with the state before it, except the transparency its art set last.
(self.state, self.gs, self.clip, self.block_end) = (state, None, clip, usize::MAX);
true
}
/// Warn that art is cut to clipping object `first` when it isn't a path or compound path (a path
@@ -739,12 +867,16 @@ impl<'a> Writer<'a> {
if self.clip.is_some() && !children.is_empty() {
self.warn(CLIPPING);
}
self.line("u");
self.clip_contents(children);
return self.end_group("U", xy);
if self.open_group("u") {
self.clip_contents(children);
self.end_group("U", xy);
}
return;
}
let at_q = (self.state, self.gs);
self.line("q");
if !self.open_group("q") {
return;
}
self.clipped(children);
(self.state, self.gs) = at_q;
self.end_group("Q", xy);
@@ -753,7 +885,8 @@ impl<'a> Writer<'a> {
/// The members of a clipping group or layer, the first one clipping the others: the others,
/// then the first one as the clipping path (see [`Self::clip_group`]), its name after it. Fills
/// and strokes the clipping path can't paint itself are paths of their own, the fills under the
/// others and the strokes over them.
/// others and the strokes over them. Without room for the clipping path, nothing is written
/// ([`Self::take_objects`]).
fn clipped(&mut self, children: &[Arc<Node>]) {
let Some((first, rest)) = children.split_first() else { return };
let Some((path, rule)) = clip_path(first) else {
@@ -762,6 +895,12 @@ impl<'a> Writer<'a> {
};
let items = self.items(first, path.to_bezpath().bounding_box());
let painted = self.plain_paint(&items);
// The clipping path goes after the others, so its objects are counted first.
let subpaths: Vec<&SubPath> = path.subpaths.iter().filter(|sp| !sp.anchors.is_empty()).collect();
let (fill, stroke) = painted.unwrap_or_default();
if !self.take_objects(clip_object_count(&subpaths, fill, stroke)) {
return;
}
if items.iter().any(|i| i.opacity() < 1.0 || i.blend() != BlendMode::Normal) {
self.warn(ITEM_TRANSPARENCY);
}
@@ -779,12 +918,10 @@ impl<'a> Writer<'a> {
self.node(c);
}
self.clip_paint_part(first, &path, rule, &strokes);
let (fill, stroke) = painted.unwrap_or_default();
self.state(first.locked, !first.visible);
let start = self.body.len();
self.paint_state(fill, stroke, Xy::of(first).object(), rule);
let op = paint_op(fill.is_some(), stroke.is_some());
let subpaths: Vec<&SubPath> = path.subpaths.iter().filter(|sp| !sp.anchors.is_empty()).collect();
let several = subpaths.len() > 1;
if several {
self.line("*u");
@@ -817,7 +954,9 @@ impl<'a> Writer<'a> {
if let [one] = items {
return self.item(path, rule, one, xy);
}
self.line("u");
if !self.open_group("u") {
return;
}
for i in items {
self.item(path, rule, i, Xy::PLAIN);
}
@@ -870,11 +1009,12 @@ impl<'a> Writer<'a> {
(Some((fill, stroke)), _) => self.path_object(path, rule, fill, stroke, xy),
(None, [one]) => self.item(path, rule, one, xy),
(None, items) => {
self.line("u");
for i in items {
self.item(path, rule, i, Xy::PLAIN);
if self.open_group("u") {
for i in items {
self.item(path, rule, i, Xy::PLAIN);
}
self.end_group("U", xy);
}
self.end_group("U", xy);
}
}
}
@@ -898,7 +1038,8 @@ impl<'a> Writer<'a> {
}
/// A brushed stroke `st` of `path` as a group of its brush's art, of transparency `xy`. False
/// when it has no brush the document knows (or brushes nest too deep).
/// when it has no brush the document knows (or brushes nest too deep). Without room for the
/// group ([`Self::take_objects`]), the stroke is left out.
fn brush_art(&mut self, path: &PathData, st: &StrokeLayer, xy: Xy) -> bool {
if self.nest >= MAX_NEST {
return false;
@@ -908,8 +1049,10 @@ impl<'a> Writer<'a> {
let Some(b) = st.brush.as_deref().and_then(|name| brushes.iter().find(|b| b.name == name)) else { return false };
let art = vectorcraft_brush::stroke_pieces(doc, b, &path.to_bezpath(), st);
let state = self.state;
if !self.open_group("u") {
return true;
}
self.nest += 1;
self.line("u");
for piece in &art {
self.node(piece);
}
@@ -948,13 +1091,15 @@ impl<'a> Writer<'a> {
let flat = FillLayer { paint: Paint::solid(Color::gray(0.5)), ..f.clone() };
return self.path_object(path, rule, Some(&flat), None, xy);
}
let state = self.state;
if !self.open_group("u") {
return;
}
self.wrote_pattern = true;
let outer = self.clip.clone();
let region = self.cut(path, rule);
self.clip = Some(region);
self.nest += 1;
let state = self.state;
self.line("u");
for t in &tiles {
self.node(t);
}
@@ -979,8 +1124,8 @@ impl<'a> Writer<'a> {
}
let both = fill.is_some() && stroke.is_some();
let own = if both { Xy::OPAQUE } else { xy.object() };
if both {
self.line("u");
if both && !self.open_group("u") {
return;
}
if fill.is_some() || stroke.is_none() {
let region = self.cut(path, rule);
@@ -1026,7 +1171,7 @@ impl<'a> Writer<'a> {
/// points and painting operator (`*u` … `*U` around several).
fn plain_object(&mut self, path: &PathData, rule: FillRule, fill: Option<&FillLayer>, stroke: Option<&StrokeLayer>, xy: Xy) {
let subpaths: Vec<&SubPath> = path.subpaths.iter().filter(|sp| !sp.anchors.is_empty()).collect();
if subpaths.is_empty() {
if subpaths.is_empty() || !self.take_objects(object_count(subpaths.len())) {
return;
}
self.paint_state(fill, stroke, xy, rule);
@@ -1057,18 +1202,29 @@ impl<'a> Writer<'a> {
fn text(&mut self, n: &Node, xy: Xy) {
let NodeKind::Text(t) = &n.kind else { return };
let doc = self.doc;
let placed = if self.hidden > 0 || self.clip.is_some() || self.in_symbol {
None
} else if let Some(thread) = doc.text_threads.iter().find(|th| th.contains(&n.id)) {
let thread = doc.text_threads.iter().find(|th| th.contains(&n.id));
// Type that can't be a stub is written as its outlines, which count their own objects.
if self.hidden > 0 || self.clip.is_some() || self.in_symbol || (thread.is_none() && !texts::holds(n, t)) {
return self.outlined_text(n, xy);
}
// The stub's objects ([`texts::stub_objects`], [`Self::take_objects`]); without room for
// them, the type is left out before its story goes into the text document.
let stub = texts::stub_objects(t);
if !self.take_objects(stub) {
return;
}
let placed = if let Some(thread) = thread {
// A thread is checked for what it loses, in every frame, when its story is written.
let placed = self.texts.thread(doc, thread, n.id, self.canvas, &|id| !cut_to_clipping_object(doc, id));
placed.map(|p| (p, self.texts.take_lost()))
} else if texts::holds(n, t) {
self.texts.add(t, self.canvas).map(|story| ((story, 0), texts::loses(t)))
} else {
None
self.texts.add(t, self.canvas).map(|story| ((story, 0), texts::loses(t)))
};
let Some(((story, frame), lost)) = placed else {
// The text document can't hold it: the outlines count their own objects.
self.objects -= stub;
return self.outlined_text(n, xy);
};
let Some(((story, frame), lost)) = placed else { return self.outlined_text(n, xy) };
self.typed = true;
if lost {
self.warn(texts::TYPE_ATTRIBUTES);
@@ -1088,8 +1244,10 @@ impl<'a> Writer<'a> {
/// graphics as their symbols' art where the layout places them, as the PDF page draws them;
/// the group has the type's transparency `xy`.
fn outlined_text(&mut self, n: &Node, xy: Xy) {
if !self.open_group("u") {
return;
}
self.warn(TYPE);
self.line("u");
let doc = self.doc;
let resolved = match &n.kind {
NodeKind::Text(t) => Some(doc.inline_resolved(t).into_owned()),
@@ -1343,6 +1501,30 @@ fn paint_op(fill: bool, stroke: bool) -> char {
}
}
/// The objects the reader makes of a path object of `subpaths` subpaths with points: a path for
/// each, and a compound path holding them when there are several.
fn object_count(subpaths: usize) -> usize {
match subpaths {
0 => 0,
1 => 1,
n => n + 1,
}
}
/// The objects the reader counts for clipping path `subpaths` (those with points) painted with
/// `fill` and `stroke` ([`Writer::clipped`]). Its last subpath carries a gradient fill's or
/// stroke's gradient, so the subpaths of a filled compound clipping path with a gradient paint
/// differently, and the reader reads their fills apart from it: a compound path of a copy of each,
/// which counts the copy and its points ([`object_count`] for the clipping path itself).
fn clip_object_count(subpaths: &[&SubPath], fill: Option<&FillLayer>, stroke: Option<&StrokeLayer>) -> usize {
let own = object_count(subpaths.len());
let apart = subpaths.len() > 1 && fill.is_some() && (gradient(fill.map(|f| &f.paint)).is_some() || gradient(stroke.map(|s| &s.paint)).is_some());
if !apart {
return own;
}
subpaths.iter().fold(own.saturating_add(1), |n, sp| n.saturating_add(1).saturating_add(sp.anchors.len()))
}
/// The subpaths of compound path `n`'s members, as one path.
fn compound_path(n: &Node) -> PathData {
let mut subpaths = vec![];
@@ -107,6 +107,11 @@ impl Writer<'_> {
if self.clip.as_ref().is_some_and(|c| c.subpaths.iter().all(|sp| sp.anchors.is_empty())) {
return;
}
// The image, and where it is cut, the clipping group and its clipping path.
let clipping = self.clip.as_ref().map_or(0, |c| 1 + super::object_count(c.subpaths.iter().filter(|sp| !sp.anchors.is_empty()).count()));
if !self.take_objects(1 + clipping) {
return;
}
if deep {
self.warn(IMAGE_DEPTH);
}
+11 -2
View File
@@ -381,7 +381,8 @@ impl Writer<'_> {
/// Write pattern `name`'s definition, once: `%AI3_BeginPattern: (name)`, `(name) llx lly urx
/// ury` (the tile, y up), the tile's box (an unpainted path whose dictionary marks it,
/// `AIPattern_Editor_Backing_Tile_Rect`) and the art on `%_` lines, `E`, `%AI3_EndPattern`.
/// False when it can't be ([`Self::pattern_writable`]).
/// False when it can't be ([`Self::pattern_writable`]), or when its box and art don't all fit
/// within the object limit ([`Self::take_objects`]).
fn define_pattern(&mut self, name: &str) -> bool {
if !self.pattern_writable(name) {
return false;
@@ -392,7 +393,9 @@ impl Writer<'_> {
return true;
}
let doc = self.doc;
let Some(def) = doc.pattern(name) else { return false };
// The tile's box is an object of the definition ([`Self::take_objects`]).
let (before, bound) = (self.charges(), self.bound.len());
let Some(def) = doc.pattern(name).filter(|_| self.take_objects(1)) else { return false };
self.defining.push(name.to_string());
// The art is written in the pattern's space, y up, from the state a definition starts in.
let saved = (
@@ -427,6 +430,12 @@ impl Writer<'_> {
(art, (self.to_art, self.grad_space, self.canvas), self.state, self.gs, self.clip, self.block_end) = saved;
let art = std::mem::replace(&mut self.body, art);
self.defining.pop();
if self.left_out_since(before) {
self.give_back(before, &art);
self.bound.truncate(bound);
return false;
}
self.keep_since(before);
let tile = Affine::FLIP_Y.transform_rect_bbox(def.tile);
let quoted = ps::bytes(name.as_bytes());
let mut text = format!("%AI3_BeginPattern: {quoted}\n{quoted} ");
@@ -688,7 +688,18 @@ impl Writer<'_> {
/// after the group's end, `0 0 Xd`, `6 () XW`); the object itself on `%_` lines (a path's or
/// compound path's points painted with nothing, or with its gradient fill when its style has
/// one; a group with its members), its name and opacity mask; then `1 (name) XW`.
/// The look's group and the object itself are counted first ([`Self::take_objects`]): without
/// room for them, nothing is written.
pub(super) fn styled_object(&mut self, n: &Node, orig: &Node, name: &str) {
let own = match &orig.kind {
NodeKind::Path { path, .. } => super::object_count(path.subpaths.iter().filter(|sp| !sp.anchors.is_empty()).count()),
NodeKind::Compound { .. } => super::object_count(compound_path(orig).subpaths.iter().filter(|sp| !sp.anchors.is_empty()).count()),
NodeKind::Group { .. } => 1,
_ => 0,
};
if !self.take_objects(1 + own) {
return;
}
self.state(orig.locked, !orig.visible);
let xy = Xy::of(orig);
let was = self.in_look;
@@ -717,6 +728,10 @@ impl Writer<'_> {
let saved = std::mem::take(&mut self.body);
self.hidden += 1;
let fit = drawn_outline(n, orig).map(|p| p.bounding_box());
// A path's objects were kept for it above; it counts them itself.
if !matches!(orig.kind, NodeKind::Group { .. }) {
self.objects -= own;
}
match &orig.kind {
NodeKind::Path { path, rule, .. } => self.styled_outline(orig, path, *rule, fit, xy),
NodeKind::Compound { rule, .. } => self.styled_outline(orig, &compound_path(orig), *rule, fit, xy),
+23 -2
View File
@@ -183,8 +183,9 @@ impl Writer<'_> {
/// written from the top of the nesting whatever the instance that asks for it, so a definition
/// is the same wherever its first instance lies. False when it can't be: the document doesn't
/// have the symbol, it is being written (its art holds an instance of itself), it is reached
/// through more than [`MAX_NEST`] definitions being written, or the definitions written and
/// being written leave no place under [`MAX_SYMBOLS`].
/// through more than [`MAX_NEST`] definitions being written, the definitions written and
/// being written leave no place under [`MAX_SYMBOLS`], or its art doesn't all fit within the
/// object limit ([`Self::take_objects`]).
pub(super) fn define_symbol(&mut self, name: &str) -> bool {
if self.defined_symbols.contains(name) {
return true;
@@ -206,6 +207,8 @@ impl Writer<'_> {
// The art's effects are its own, not those of a drawn look that first places it.
let in_look = std::mem::take(&mut self.in_look);
self.defining_symbols.push(name.to_string());
// The objects of this definition and of those it defines first.
let before = self.charges();
let mut used = vec![];
vectorcraft_doc::swatches::walk_with_masks(&sym.art, &mut |m| {
if let NodeKind::SymbolInstance { symbol, .. } = &m.kind {
@@ -217,10 +220,19 @@ impl Writer<'_> {
}
if self.symbol_defs.len() + self.defining_symbols.len() > MAX_SYMBOLS {
self.defining_symbols.pop();
self.keep_since(before);
self.warn(SYMBOL_COUNT);
self.in_look = in_look;
return false;
}
// The group that holds the art is an object of the definition ([`Self::take_objects`]);
// without room for it, or for all the art, there is no definition.
let (art_start, bound) = (self.charges(), self.bound.len());
if !self.take_objects(1) {
self.defining_symbols.pop();
self.keep_since(before);
return false;
}
// The art is written in the symbol's space, from the state `0 A` and `0 Xw` set, from the
// document at its own size.
let origin = self.canvas_origin;
@@ -264,7 +276,9 @@ impl Writer<'_> {
let _ = write!(self.body, "U\n%_/ArtDictionary :\n{entries}%_;\n%_\n9 () XW\n");
} else {
// A group always writes its dictionary ([`Self::dictionary`]), which takes the entries.
// It counts itself, in the room kept for it above.
self.root_entries = Some((root.id, entries));
self.objects -= 1;
self.node(root);
}
self.nest = nest;
@@ -297,6 +311,13 @@ impl Writer<'_> {
self.in_look = in_look;
let art = std::mem::replace(&mut self.body, art);
self.defining_symbols.pop();
if self.left_out_since(art_start) {
self.give_back(art_start, &art);
self.bound.truncate(bound);
self.keep_since(before);
return false;
}
self.keep_since(before);
let text = format!("%AI14_BeginSymbol\n{}\n0 A\n0 Xw\n0 Ae\n{art}%AI10_EndSymbol\n", ps::bytes(name.as_bytes()));
self.defined_symbols.insert(name.to_string());
self.symbol_defs.push(text);
@@ -1016,6 +1016,12 @@ fn clone_v(v: &V) -> V {
}
}
/// The objects the reader reads of [`stub`] for `t`: the text object, and a path for each subpath
/// of its frame's path.
pub(in crate::ai_editing) fn stub_objects(t: &TextObject) -> usize {
1 + shape(t).map_or(0, |p| p.subpaths.iter().filter(|sp| !sp.anchors.is_empty()).count())
}
/// The text object stub of story `story`, frame `frame`: `/AI11Text :`, `/FreeUndo`, `/FrameIndex`,
/// `/StoryIndex`, for area type and type on a path the frame's path (`/Art :`, `X=`, the path in
/// art space through `to_art`, `X+`, `; /ConfiningPath ,`), `/TextAntialiasing`, `;`.
+4 -1
View File
@@ -73,8 +73,11 @@ use graphics::{GState, Out};
use interp::{Fault, Interp};
use obj::PsError;
#[cfg(test)]
pub(crate) use ai::objects_read;
pub(crate) use ai::{
MAX_GRADIENT_STOPS, MAX_LISTED, MAX_LISTED_NAME, MAX_SYMBOLS, read_art_style, read_style_list, style_moves_with, symbol_art_member, warp_number,
MAX_EDITING_OBJECTS, MAX_GRADIENT_STOPS, MAX_LISTED, MAX_LISTED_NAME, MAX_SYMBOLS, read_art_style, read_style_list, style_moves_with,
symbol_art_member, warp_number,
};
pub(crate) use ate::{MAX_BYTES as MAX_TEXT_DOCUMENT, MAX_FRAMES, MAX_SEGMENTS, MAX_STORY, MAX_VALUES as MAX_TEXT_VALUES};
pub use native::{AiLayers, ai_alone, ai_alone_on_page, ai_layers_alone, ai_opened_artboard, is_loss, layered_ai, layered_ai_from};
+4 -1
View File
@@ -41,13 +41,16 @@ pub(crate) use envelope::{WARPED_TYPE_DRAWN, WarpedType};
pub(crate) use frames::text_frames;
pub(crate) use paint::MAX_STOPS as MAX_GRADIENT_STOPS;
#[cfg(test)]
pub(crate) use read::objects_read;
#[cfg(test)]
pub(crate) use read::with_image_limits;
pub use read::{
BindingRef, Brushes, PaletteSwatches, PlacedPage, Structure, Symbols, active_artboard, artboards, bindings_note, brushes, graphic_styles,
palette, read, read_beside, symbols, variables,
};
pub(crate) use read::{
MAX_SYMBOLS, join_and, read_art_style, read_over_page, read_style_list, slot_frame, slot_of, style_moves_with, symbol_art_member,
MAX_NODES as MAX_EDITING_OBJECTS, MAX_SYMBOLS, join_and, read_art_style, read_over_page, read_style_list, slot_frame, slot_of, style_moves_with,
symbol_art_member,
};
pub(crate) use read::is_look_slot;
+18 -5
View File
@@ -28,7 +28,11 @@
//! [`vectorcraft_brush::ArtBrush`]'s `overlap: false`).
//! - A Scatter, Art or Pattern brush's parameters are fields split by `/`: a number, the art's
//! section names (one for Scatter, five for Art and Pattern, empty where there is none, each
//! after a space for Art and Pattern), more fields, and last 15 numbers.
//! after a space for Art and Pattern), more fields, and last 15 numbers. Older files write
//! brushes with first number 1, one field fewer before the numbers and 8 numbers; an Art brush
//! with 8 numbers, whatever its first number, is read from its art's name alone (its numbers
//! aren't known, so it keeps the defaults and its paths their drawn look). A Pattern brush needs
//! only its first 3 numbers (its scale is the third).
//! - Scatter: the size (2, 3), spacing (5, 6) and scatter (8, 9) as (low, high) factors of the
//! art, the rotation in degrees (11, 12), how each varies (4, 7, 10, 13) and whether the
//! rotation follows the path (14), and 1 Tints (1).
@@ -312,11 +316,10 @@ pub(super) fn parse_tool_object(kind: Tool, name: &str, params: &str) -> Result<
})
.collect();
let n = fields.last().and_then(|f| numbers(f)).ok_or(BrushError::Values)?;
let n: &[f64; 15] = n.as_slice().try_into().map_err(|_| BrushError::Values)?;
let [_, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, _] = n;
let art = || tiles.first().cloned().flatten().ok_or(BrushError::NoArt);
match kind {
Tool::Scatter => {
let [_, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, _] = n.as_slice() else { return Err(BrushError::Values) };
let pct = |a: f64, b: f64| (a * 100.0, b * 100.0);
let values = [pct(*p2, *p3), pct(*p5, *p6), pct(*p8, *p9), (*p11, *p12)];
let codes = [*p4, *p7, *p10, *p13];
@@ -336,10 +339,20 @@ pub(super) fn parse_tool_object(kind: Tool, name: &str, params: &str) -> Result<
DraftKind::Scatter { brush, art: art()? }
}
Tool::Art => {
let colorization = if *p2 == 1.0 { Colorization::Tints } else { Colorization::None };
DraftKind::Art { brush: ArtBrush { colorization, ..ArtBrush::default() }, art: art()?, plain: art_is_plain(n) }
// The older form's 8 numbers aren't known: its brush keeps the defaults and its
// paths their drawn look. The count tells the form, not the first number.
// Any other count isn't a form this reads.
let (colorization, plain) = if let Ok(n15) = <&[f64; 15]>::try_from(n.as_slice()) {
(if n15[1] == 1.0 { Colorization::Tints } else { Colorization::None }, art_is_plain(n15))
} else if n.len() == 8 {
(ArtBrush::default().colorization, false)
} else {
return Err(BrushError::Values);
};
DraftKind::Art { brush: ArtBrush { colorization, ..ArtBrush::default() }, art: art()?, plain }
}
_ => {
let [_, _, p3, ..] = n.as_slice() else { return Err(BrushError::Values) };
let tiles: [Option<String>; 5] = tiles.try_into().map_err(|_| BrushError::Values)?;
DraftKind::Pattern { tiles, scale: p3 * 100.0 }
}
+9
View File
@@ -149,6 +149,7 @@ pub(crate) use document::{slot_frame, slot_of};
pub(crate) use ops::{read_art_style, read_style_list, style_moves_with};
#[cfg(test)]
pub(crate) use raster::with_image_limits;
pub(crate) use types::MAX_NODES;
pub use types::PlacedPage;
use types::{BLEND_BUDGET, BrushArtColors, FLIP, Frame, GState, Graph, Kind, PatternHead, PendingEnvelope, Place, PlacedTail, Raster, SymbolHead};
#[cfg(test)]
@@ -259,6 +260,14 @@ fn read_with(data: &[u8], large_canvas: bool, page_art: bool) -> Result<Structur
r.finish()
}
/// How many objects [`read`] counts against [`MAX_NODES`] in the editing data `data`.
#[cfg(test)]
pub(crate) fn objects_read(data: &[u8]) -> Result<usize, String> {
let mut r = Reader::with_names(data);
r.run()?;
Ok(r.nodes)
}
/// [`read`] for editing data kept apart from the program `ps` it came with (an EPS's private
/// data): the program's header gives the author and the custom colors the editing data's header
/// doesn't. `page_art`: as [`read_over_page`] reads a file.
+2 -2
View File
@@ -11,8 +11,8 @@ pub(super) const MAX_SUBREADERS: usize = 32;
/// Most operands on the stack.
pub(super) const MAX_STACK: usize = 1 << 20;
/// Most objects read.
pub(super) const MAX_NODES: usize = 4_000_000;
/// Most objects read. The `.ai` editing data writer writes no more ([`crate::import::MAX_EDITING_OBJECTS`]).
pub(crate) const MAX_NODES: usize = 4_000_000;
/// The most objects all the blends of one import may lay out to be checked (each twice) and made
/// live; past it, blends keep their drawn steps.
+48
View File
@@ -8881,6 +8881,54 @@ fn a_pattern_brushs_five_slots_are_kept() {
assert!(matches!(d.kind, brush::DraftKind::Art { ref art, .. } if art == "Ribbon art"), "{d:?}");
}
/// An Art brush in the older form (first number 1: one field fewer before its 8 numbers) is read
/// with the art of its section, but not as plain (its numbers aren't known); a Scatter brush in
/// that form isn't read (its values aren't all there).
#[test]
fn an_older_art_brush_comes_in_with_its_art() {
const OLD_RIBBON: &str = "1 / Ribbon art/ / / / / 0 1 / 1 0 1 0 1 0 0 0";
let d = brush::parse_tool_object(brush::Tool::Art, "Ribbon", OLD_RIBBON).unwrap();
assert!(matches!(d.kind, brush::DraftKind::Art { ref art, plain: false, .. } if art == "Ribbon art"), "{d:?}");
let brush::DraftKind::Art { ref brush, .. } = d.kind else { panic!("art") };
assert_eq!(brush.colorization, vectorcraft_brush::ArtBrush::default().colorization, "the default colorization");
// A count of numbers neither form has isn't read.
for bad in ["1 / Ribbon art/ / / / / 0 1 / 1 0 1 0 1 0 0", "1 / Ribbon art/ / / / / 0 1 / 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0"] {
assert_eq!(brush::parse_tool_object(brush::Tool::Art, "Ribbon", bad), Err(brush::BrushError::Values), "{bad}");
}
assert_eq!(brush::parse_tool_object(brush::Tool::Scatter, "Spray", "1 /Dot art/ 0 1 / 1 0 1 0 1 0 0 0"), Err(brush::BrushError::Values));
// 8 numbers are the older form whatever the first number says: read with the defaults.
let d = brush::parse_tool_object(brush::Tool::Art, "Ribbon", "0 / Ribbon art/ / / / / 0 1 / 1 0 1 0 1 0 0 0").unwrap();
let brush::DraftKind::Art { ref brush, ref art, plain } = d.kind else { panic!("art") };
assert_eq!((art.as_str(), plain, brush.colorization), ("Ribbon art", false, vectorcraft_brush::ArtBrush::default().colorization));
let objects = [plugin_object(ART_BRUSH, "Ribbon", OLD_RIBBON), plugin_object(ART_BRUSH, "Bare", "1 / / / / / / 0 1 / 1 0 1 0 1 0 0 0")].concat();
let r = read_ok(&format!("{}{objects}{}", brush_art("Ribbon art", 4.0), layer("")));
let lib = vectorcraft_brush::library(&r.doc);
let vectorcraft_brush::BrushKind::Art(a) = brush_named(&lib, "Ribbon") else { panic!("art") };
assert_eq!(a.art.geometric_bounds().map(|b| (b.width(), b.height())), Some((4.0, 4.0)), "the art of its section");
assert_eq!(lib.len(), 1, "a brush naming no art is left out");
assert!(r.warnings.iter().any(|w| w.ends_with("Brushes panel (1)")), "{:?}", r.warnings);
}
/// A Pattern brush needs only its first 3 numbers, its scale the third (as a percentage), so the
/// older form's 8 numbers read too; fewer than 3 aren't read.
#[test]
fn a_pattern_brush_reads_its_scale_from_three_or_more_numbers() {
const SLOTS: &str = "1 / Tile one/ Tile two/ / / Tile five/ 0 1 / ";
for numbers in ["1 0 0.5", "1 0 0.5 0 1 0 0 0"] {
let d = brush::parse_tool_object(brush::Tool::Pattern, "Fence", &format!("{SLOTS}{numbers}")).unwrap();
let brush::DraftKind::Pattern { tiles, scale } = d.kind else { panic!("{d:?}") };
assert_eq!(tiles[1].as_deref(), Some("Tile two"), "{numbers}");
assert!((scale - 50.0).abs() < 1e-9, "{numbers}: {scale}");
}
for numbers in ["", "1", "1 0"] {
assert_eq!(
brush::parse_tool_object(brush::Tool::Pattern, "Fence", &format!("{SLOTS}{numbers}")),
Err(brush::BrushError::Values),
"{numbers:?}"
);
}
}
/// A brush's art filled with a pattern the file defines before it is read, with that pattern.
#[test]
fn a_brushs_art_takes_the_patterns_defined_before_it() {
+2 -1
View File
@@ -22,7 +22,8 @@ pub(super) enum Page {
/// How art moved from a page to its place in the layers' document transforms: its pattern tiles
/// move with it.
pub(super) const PAGE_MOVE: Scaling = Scaling { strokes: false, effects: None, keep_type_strokes: false, keep_corners: false, patterns: true };
pub(super) const PAGE_MOVE: Scaling =
Scaling { strokes: false, effects: None, keep_type_strokes: false, keep_corners: false, patterns: true, keep_fit_when_flat: false };
/// The page (`visible`, the file read as its page) and the place of its area in the layers'
/// document.
+57 -19
View File
@@ -1098,6 +1098,7 @@ impl<'a> Scene<'a> {
/// Paint all of `area` with an image of freeform gradient `g` placed on `bounds`, sampled at
/// the document's raster effects resolution (at most [`MAX_FIELD_PX`] a side).
fn field_image(&mut self, g: &GradientPaint, bounds: Rect, area: Rect) {
use vectorcraft_color::cms::Model;
use vectorcraft_color::freeform::{painted_box, spread_scale};
let Some(b) = painted_box(bounds) else { return };
let area = area.abs();
@@ -1108,11 +1109,24 @@ impl<'a> Scene<'a> {
let k = (self.doc.raster_effects_ppi / 72.0).min(MAX_FIELD_PX / long);
let along = |len: f64| (len * k).ceil().clamp(1.0, MAX_FIELD_PX) as u16;
let (cols, rows) = (along(area.width()), along(area.height()));
let field = g.freeform_on(b).field_with(spread_scale(b), &|c| c.to_rgb());
let field = g.freeform_on(b).model_field(spread_scale(b));
let q = |v: f32| (unit(v) * 255.0).round() as u8;
let rgba: Vec<u8> = field.grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect();
let (w, h) = (u32::from(cols), u32::from(rows));
let image = if field.model() == Some(Model::Cmyk) || (self.cmyk && field.model() == Some(Model::Gray)) {
let inks: Vec<u8> = field
.grid(area, cols, rows)
.flat_map(|(color, a)| {
let [c, m, y, k] = color.to_cmyk();
[q(c), q(m), q(y), q(k), q(a)]
})
.collect();
self.encode_inks(&inks, w, h)
} else {
let rgba: Vec<u8> = field.display_grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect();
self.encode_image(&rgba, w, h)
};
let place = Affine::translate(area.origin().to_vec2()) * Affine::scale_non_uniform(area.width(), area.height());
if let Some(image) = self.encode_image(&rgba, u32::from(cols), u32::from(rows)) {
if let Some(image) = image {
self.place_image(&image, place);
}
}
@@ -1313,41 +1327,65 @@ impl<'a> Scene<'a> {
/// Level 3 (a key colour no opaque pixel has), white at Level 2; partly transparent ones over
/// white. `None` for an empty image.
fn encode_image(&mut self, rgba: &[u8], w: u32, h: u32) -> Option<String> {
let space = if self.cmyk { Space::Cmyk } else { Space::Rgb };
let mut cache: HashMap<[u8; 3], [u8; 4]> = HashMap::new();
let cms = self.cms.clone();
let intent = cms.settings().intent;
self.encode_samples(rgba.as_chunks::<4>().0, w, h, space, |p: &[u8; 4], al| {
let rgb = [0, 1, 2].map(|i| ((u32::from(p[i]) * al + 255 * (255 - al) + 127) / 255) as u8);
match space {
Space::Cmyk => *cache
.entry(rgb)
.or_insert_with(|| cms.srgb_to_cmyk(rgb.map(|x| f32::from(x) / 255.0), intent).map(|x| (unit(x) * 255.0).round() as u8)),
_ => [rgb[0], rgb[1], rgb[2], 0],
}
})
}
/// Straight CMYK pixels with opacity (five bytes a pixel, `w` × `h`, rows from the top), drawn
/// as [`Self::encode_image`] draws its pixels and keeping their ink amounts: partly
/// transparent pixels lay down that share of each ink.
fn encode_inks(&mut self, cmyka: &[u8], w: u32, h: u32) -> Option<String> {
self.encode_samples(cmyka.as_chunks::<5>().0, w, h, Space::Cmyk, |p: &[u8; 5], al| {
[0, 1, 2, 3].map(|i| ((u32::from(p[i]) * al + 127) / 255) as u8)
})
}
/// Pixels (`w` × `h`, rows from the top, each ending in its opacity) as the operators and data
/// that draw them in `space` on the unit square. `sample` gives the samples of a pixel that
/// isn't clear, from the pixel and its opacity.
fn encode_samples<const N: usize>(
&mut self,
px: &[[u8; N]],
w: u32,
h: u32,
space: Space,
mut sample: impl FnMut(&[u8; N], u32) -> [u8; 4],
) -> Option<String> {
if w == 0 || h == 0 {
return None;
}
let px = rgba.as_chunks::<4>().0;
let clear = px.iter().any(|p| p[3] == 0);
if px.iter().any(|p| p[3] > 0 && p[3] < 255) {
let opacity = |p: &[u8; N]| p[N - 1];
let clear = px.iter().any(|p| opacity(p) == 0);
if px.iter().any(|p| opacity(p) > 0 && opacity(p) < 255) {
self.warn(IMAGE_PARTLY);
}
let keyed = clear && self.o.level == Level::Three;
if clear && !keyed {
self.warn(IMAGE_ALPHA_L2);
}
let space = if self.cmyk { Space::Cmyk } else { Space::Rgb };
let n = space.n();
// The key colour, which opaque pixels that happen to have it are nudged off.
let key: [u8; 4] = [255, 0, 255, 0];
// White: no ink, or full light.
let white: [u8; 4] = if space == Space::Cmyk { [0; 4] } else { [255; 4] };
let mut cache: HashMap<[u8; 3], [u8; 4]> = HashMap::new();
let cms = self.cms.clone();
let intent = cms.settings().intent;
let mut data = Vec::with_capacity(px.len() * n);
for p in px {
if p[3] == 0 {
if opacity(p) == 0 {
data.extend(if keyed { &key } else { &white }.iter().take(n));
continue;
}
let al = u32::from(p[3]);
let rgb = [0, 1, 2].map(|i| ((u32::from(p[i]) * al + 255 * (255 - al) + 127) / 255) as u8);
let mut v = match space {
Space::Cmyk => *cache
.entry(rgb)
.or_insert_with(|| cms.srgb_to_cmyk(rgb.map(|x| f32::from(x) / 255.0), intent).map(|x| (unit(x) * 255.0).round() as u8)),
_ => [rgb[0], rgb[1], rgb[2], 0],
};
let mut v = sample(p, u32::from(opacity(p)));
if keyed && v.iter().take(n).eq(key.iter().take(n)) {
v[1] = 1;
}
+55 -1
View File
@@ -3,7 +3,7 @@
use std::sync::Arc;
use vectorcraft_color::{Color, Gradient, GradientKind, GradientMethod, GradientPaint, GradientStop, Paint, Swatch};
use vectorcraft_color::{Color, Freeform, FreeformPoint, Gradient, GradientKind, GradientMethod, GradientPaint, GradientStop, Paint, Swatch};
use vectorcraft_doc::{Appearance, AppearanceItem, Dash, Document, ImageBlob, ImageObject, LineCap, LineJoin, Node, NodeKind};
use vectorcraft_geom::{Affine, PathData, Point, Rect, shapes};
@@ -574,6 +574,60 @@ fn image_data(page: &str) -> Vec<u8> {
ps::ascii85_decode(&page[at..end]).unwrap()
}
/// A square filled with a freeform gradient of `a` on the left and `b` on the right, with those
/// points at `opacity`, exported: (the page, the middle pixel's samples, the warnings).
fn freeform_mid(a: Color, b: Color, opacity: f32, n: usize) -> (String, Vec<u8>, Vec<String>) {
let p = |x, c| FreeformPoint { opacity, ..FreeformPoint::new(Point::new(x, 100.0), c) };
let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() });
g.freeform = Some(Freeform { points: vec![p(40.0, a), p(160.0, b)], ..Default::default() });
let d = doc_with(|d| vec![path(d, shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 200.0)), Paint::Gradient(Box::new(g)), Paint::None, 0.0)]);
let out = export(&d, &opts(&d), None, None).unwrap();
let page = text(&out);
let size = |key: &str| {
let at = page.find(key).unwrap() + key.len();
page[at..].split(' ').next().unwrap().parse::<usize>().unwrap()
};
let (w, h) = (size("/Width "), size("/Height "));
let pixels = ps::inflate(&image_data(&page), 1 << 28).unwrap();
let at = (h / 2 * w + w / 2) * n;
(page, pixels[at..at + n].to_vec(), out.warnings)
}
#[test]
fn freeform_gradients_keep_the_inks_of_cmyk_points_and_mix_in_the_points_model() {
let (cyan, black) = (Color::cmyk(1.0, 0.0, 0.0, 0.0), Color::cmyk(0.0, 0.0, 0.0, 1.0));
let (page, mid, _) = freeform_mid(cyan, black, 1.0, 4);
assert!(page.contains("/DeviceCMYK setcolorspace"), "{page}");
assert!(mid.iter().zip([128, 0, 0, 128]).all(|(g, w): (&u8, i32)| (i32::from(*g) - w).abs() <= 6), "{mid:?}");
// A partly transparent point lays down that share of each ink.
let (_, mid, warnings) = freeform_mid(cyan, black, 0.5, 4);
assert!(mid.iter().zip([64, 0, 0, 64]).all(|(g, w): (&u8, i32)| (i32::from(*g) - w).abs() <= 6), "{mid:?}");
assert!(warnings.iter().any(|w| w.contains("partly transparent")), "{warnings:?}");
// Lab points go out as RGB, mixed first in Lab.
let (page, mid, _) = freeform_mid(Color::lab(90.0, -60.0, 40.0), Color::lab(30.0, 60.0, -40.0), 1.0, 3);
assert!(page.contains("/DeviceRGB setcolorspace"), "{page}");
let want = Color::lab(60.0, 0.0, 0.0).to_rgba8(1.0);
assert!(mid.iter().zip(want).all(|(g, w)| g.abs_diff(w) <= 6), "{mid:?} vs {want:?}");
}
#[test]
fn gray_freeform_points_are_written_as_black_ink_in_a_cmyk_document() {
let p = |x, c| FreeformPoint::new(Point::new(x, 100.0), c);
let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() });
g.freeform = Some(Freeform { points: vec![p(40.0, Color::gray(0.2)), p(160.0, Color::gray(0.8))], ..Default::default() });
let mut d = doc_with(|d| vec![path(d, shapes::rectangle(Rect::new(0.0, 0.0, 200.0, 200.0)), Paint::Gradient(Box::new(g)), Paint::None, 0.0)]);
d.color_mode = vectorcraft_doc::ColorMode::Cmyk;
let page = text(&export(&d, &opts(&d), None, None).unwrap());
let size = |key: &str| {
let at = page.find(key).unwrap() + key.len();
page[at..].split(' ').next().unwrap().parse::<usize>().unwrap()
};
let (w, h) = (size("/Width "), size("/Height "));
let pixels = ps::inflate(&image_data(&page), 1 << 28).unwrap();
let at = (h / 2 * w + w / 2) * 4;
assert!(pixels[at..at + 3] == [0, 0, 0] && pixels[at + 3].abs_diff(128) <= 6, "{:?}", &pixels[at..at + 4]);
}
#[test]
fn images_are_flate_with_masked_pixels_at_level_3_and_run_length_at_level_2() {
let d = png_doc();
+1 -1
View File
@@ -56,7 +56,7 @@ use vectorcraft_doc::{Document, ImageBlob};
#[cfg(not(target_arch = "wasm32"))]
pub use atomic::{sync_file, write_atomic, write_atomic_with, write_new_with};
pub use craft_vectorcraft_format::base64::{decode as base64_decode, encode as base64_encode};
pub use craft_vectorcraft_format::base64::{decode as base64_decode, decoded_len as base64_decoded_len, encode as base64_encode};
pub use craft_vectorcraft_format::{
BLOB_MAGIC, COMPRESSED_SINCE, EXTENSION, ICC_MIME, INLINE_MAX, LEGACY_EXTENSION, MIN_VERSION, PREVIEW_MAX, VERSION, is_compressed, json_part,
sniff,
+263 -15
View File
@@ -1,16 +1,29 @@
//! The built-in objects the interpreter itself needs (ECMA-262 5.1 section 15): `Object` and the
//! basic methods of `Object.prototype`, `Function` with `Function.prototype.call`, `apply` and
//! `toString`, `eval`, the error constructors and `Error.prototype.toString`, and the prototypes
//! primitive values look properties up on. `String`, `Number`, `RegExp` and `Date` are in their
//! own modules; the rest of the standard library is not here yet.
//! The built-in objects (ECMA-262 5.1 section 15): `Object` with `Object.prototype` and the
//! ES5 statics, `Function` with `Function.prototype.call`, `apply`, `bind` and `toString`,
//! `Array`, the error constructors, `Math`, `JSON`, the global functions and constants, Annex
//! B's `escape` and `unescape`, and the prototypes primitive values look properties up on, with
//! `Boolean.prototype`'s `toString` and `valueOf`. `String`, `Number`, `RegExp` and `Date` are in
//! their own modules.
//!
//! Every loop in a built-in takes a unit of fuel for each step, and every string, array, object
//! or property it makes is checked against the heap limit before it is made, so no built-in can
//! run past [`crate::Limits`].
mod array;
mod global;
mod json;
mod math;
mod object;
use std::rc::Rc;
use crate::bytecode::{CodeKind, Source};
use crate::compiler::{CompileFail, compile_constructed_function};
use crate::heap::{Class, Func, HIDDEN, NativeFn, NativeKind, ObjId, Object, Prop, key, string_bytes};
use crate::interp::{Abrupt, ErrorType, Interp, JsResult};
use crate::value::{Str, Value};
use crate::heap::{
BOUND_ARG_BYTES, Class, Func, HIDDEN, Key, NativeFn, NativeKind, OBJECT_BYTES, OPEN, ObjId, Object, PROP_BYTES, Prop, key, string_bytes,
};
use crate::interp::{Abrupt, Desc, ErrorType, Interp, JsResult};
use crate::value::{Str, Value, index_key, number_to_str, to_uint32};
fn alloc(interp: &mut Interp, class: Class, proto: Option<ObjId>) -> ObjId {
interp.heap.alloc_object(Object::new(class, proto))
@@ -42,12 +55,16 @@ pub(crate) fn constructor(interp: &mut Interp, name: &'static str, ctor: ObjId,
}
/// Creates the built-ins and the global object's properties. The caller installs them before it
/// sets the realm's limits, so no limit stops the install.
/// sets the realm's limits, so no limit stops the install. If it fails, the failure is kept in
/// `interp.install_error` and every run of the realm ends with it.
pub(crate) fn install(interp: &mut Interp) {
if let Err(e) = try_install(interp) {
// Installing touches only objects it has just made, so this does not happen; a realm
// that somehow lacks a built-in reports it when a script uses it.
let _ = e;
let reason = match e {
Abrupt::Throw(v) => interp.describe(&v),
Abrupt::Limit(kind) => format!("limit reached: {kind:?}"),
Abrupt::Internal(message) => message,
};
interp.install_error = Some(format!("the built-ins could not be installed: {reason}"));
}
}
@@ -68,7 +85,8 @@ fn try_install(interp: &mut Interp) -> JsResult<()> {
interp.intrinsics.array_proto = alloc(interp, Class::Array { length: 0, length_writable: true }, Some(object_proto));
interp.intrinsics.string_proto = alloc(interp, Class::String(Str::from("")), Some(object_proto));
interp.intrinsics.number_proto = alloc(interp, Class::Number(0.0), Some(object_proto));
interp.intrinsics.boolean_proto = alloc(interp, Class::Boolean, Some(object_proto));
let boolean_proto = alloc(interp, Class::Boolean(false), Some(object_proto));
interp.intrinsics.boolean_proto = boolean_proto;
interp.intrinsics.regexp_proto = alloc(interp, Class::Ordinary, Some(object_proto));
let thrower = native(interp, "", throw_type_error, 0, NativeKind::Plain, false)?;
if let Some(t) = interp.heap.object_mut(thrower) {
@@ -98,15 +116,189 @@ fn try_install(interp: &mut Interp) -> JsResult<()> {
data(interp, function_proto, "call", Value::Object(call), HIDDEN)?;
let apply = native(interp, "apply", unreachable_native, 2, NativeKind::Apply, false)?;
data(interp, function_proto, "apply", Value::Object(apply), HIDDEN)?;
method(interp, function_proto, "bind", function_bind, 1)?;
method(interp, function_proto, "toString", function_to_string, 0)?;
method(interp, boolean_proto, "toString", boolean_to_string, 0)?;
method(interp, boolean_proto, "valueOf", boolean_value_of, 0)?;
install_errors(interp, object_proto)?;
object::install(interp, object)?;
array::install(interp)?;
math::install(interp)?;
json::install(interp)?;
global::install(interp)?;
crate::string_builtins::install(interp)?;
crate::number_builtins::install(interp)?;
crate::regexp_builtins::install(interp)?;
crate::date_builtins::install(interp)
}
// ---- helpers for the built-ins ----
/// ToInteger (section 9.4).
fn to_integer(interp: &mut Interp, v: &Value) -> JsResult<f64> {
let n = interp.to_number(v)?;
Ok(if n.is_nan() { 0.0 } else { n.trunc() })
}
/// The property name of the index `n`, which may be past the largest array index.
fn key_of(n: f64) -> Key {
if (0.0..4_294_967_295.0).contains(&n) && n.fract() == 0.0 {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
return index_key(n as u32);
}
number_to_str(n).flat()
}
/// ToUint32 of an object's `length`.
fn length_of(interp: &mut Interp, o: ObjId) -> JsResult<u32> {
let len = interp.get_from(o, &key("length"), &Value::Object(o))?;
Ok(to_uint32(interp.to_number(&len)?))
}
fn get_at(interp: &mut Interp, o: ObjId, k: f64) -> JsResult<Value> {
interp.get_from(o, &key_of(k), &Value::Object(o))
}
fn has_at(interp: &mut Interp, o: ObjId, k: f64) -> JsResult<bool> {
interp.has_property(o, &key_of(k))
}
/// The estimated bytes a new element adds.
const ELEMENT_BYTES: usize = PROP_BYTES + 32 + 2 * 10;
/// `o[k] = v` as the array methods assign: a failure throws. When `o` has no own property `k`, `v`
/// stays rooted while heap is reserved for the new element, since the caller may hold it nowhere
/// else; overwriting an own property reserves nothing.
pub(crate) fn put_at(interp: &mut Interp, o: ObjId, k: f64, v: Value) -> JsResult<()> {
let k = key_of(k);
if interp.heap.object(o).is_some_and(|obj| obj.props.contains(&k)) {
return interp.put(o, k, v, true);
}
interp.rooted(std::slice::from_ref(&v), |i| {
i.reserve_heap(ELEMENT_BYTES)?;
i.put(o, k.clone(), v.clone(), true)
})
}
fn delete_at(interp: &mut Interp, o: ObjId, k: f64) -> JsResult<()> {
interp.delete(o, &key_of(k), true).map(drop)
}
/// Defines `a[k]` as a writable, enumerable and configurable data property, as the methods that
/// build a new array do (DefineOwnProperty with `throw` false). `v` stays rooted while heap is
/// reserved for it.
fn create_at(interp: &mut Interp, a: ObjId, k: f64, v: Value) -> JsResult<()> {
interp.rooted(std::slice::from_ref(&v), |i| {
i.reserve_heap(ELEMENT_BYTES)?;
i.define_own_property(a, key_of(k), &Desc::data(v.clone(), OPEN), false).map(drop)
})
}
/// A new array with the given length and no elements.
fn new_array(interp: &mut Interp, length: u32) -> JsResult<ObjId> {
interp.reserve_heap(OBJECT_BYTES)?;
let proto = interp.intrinsics.array_proto;
Ok(interp.heap.alloc_object(Object::new(Class::Array { length, length_writable: true }, Some(proto))))
}
/// A new string from code units, checked against the heap limit and charged to the heap.
fn new_string(interp: &mut Interp, units: Vec<u16>) -> JsResult<Value> {
let bytes = string_bytes(units.len());
interp.reserve_heap(bytes)?;
interp.heap.bytes = interp.heap.bytes.saturating_add(bytes);
Ok(Value::String(Str::from_units(units)))
}
/// Code units a built-in may write into a string for one unit of fuel.
const UNITS_PER_FUEL: usize = 16;
/// A string the built-ins build piece by piece: each piece takes fuel for its length, is checked
/// against the longest string the heap limit allows, and has its bytes reserved before it is
/// added. The buffer's bytes count toward the heap until `finish` or until the native call that
/// made the builder returns. Everything the caller still needs must be reachable from the roots.
struct Builder {
units: Vec<u16>,
/// Code units already reserved and charged to the heap.
charged: usize,
/// Code units written since fuel was last taken, fewer than `UNITS_PER_FUEL`.
unpaid: usize,
}
impl Builder {
fn new() -> Self {
Builder { units: Vec::new(), charged: 0, unpaid: 0 }
}
/// Checks that `more` code units fit, reserving heap for them, and takes fuel for writing them.
fn make_room(&mut self, interp: &mut Interp, more: usize) -> JsResult<()> {
let want = self.units.len().saturating_add(more);
if want > interp.limits.heap_bytes / 4 {
return Err(Abrupt::Limit(crate::LimitKind::Heap));
}
self.unpaid += more;
let fuel = (self.unpaid / UNITS_PER_FUEL) as u64;
self.unpaid %= UNITS_PER_FUEL;
if fuel > 0 {
if interp.fuel < fuel {
interp.fuel = 0;
return Err(Abrupt::Limit(crate::LimitKind::Fuel));
}
// The last unit goes through `use_fuel`, which also checks for an interrupt.
interp.fuel -= fuel - 1;
interp.use_fuel()?;
}
if want > self.charged {
let grow = (want - self.charged).max(self.charged / 2).max(256).min((interp.limits.heap_bytes / 4).saturating_sub(self.charged));
interp.reserve_heap(2 * grow)?;
interp.heap.bytes = interp.heap.bytes.saturating_add(2 * grow);
interp.held_bytes = interp.held_bytes.saturating_add(2 * grow);
self.charged += grow;
}
Ok(())
}
fn push_units(&mut self, interp: &mut Interp, units: &[u16]) -> JsResult<()> {
self.make_room(interp, units.len())?;
self.units.extend_from_slice(units);
Ok(())
}
/// Appends `units` `count` times. Nothing is written for an empty `units`, however large
/// `count` is.
fn push_repeated(&mut self, interp: &mut Interp, units: &[u16], count: u64) -> JsResult<()> {
if units.is_empty() || count == 0 {
return Ok(());
}
let more = usize::try_from(count).unwrap_or(usize::MAX).saturating_mul(units.len());
self.make_room(interp, more)?;
for _ in 0..count {
self.units.extend_from_slice(units);
}
Ok(())
}
fn push_str(&mut self, interp: &mut Interp, s: &str) -> JsResult<()> {
let units: Vec<u16> = s.encode_utf16().collect();
self.push_units(interp, &units)
}
/// The string built, whose bytes were charged as it grew. From here on the heap counts them
/// as the string's.
fn finish(self, interp: &mut Interp) -> Value {
interp.held_bytes = interp.held_bytes.saturating_sub(2 * self.charged);
Value::String(Str::from_units(self.units))
}
}
fn require_callable(interp: &mut Interp, v: &Value, what: &str) -> JsResult<()> {
if interp.is_callable(v) {
return Ok(());
}
Err(interp.type_error(&format!("{what} is not a function")))
}
fn install_errors(interp: &mut Interp, object_proto: ObjId) -> JsResult<()> {
let kinds: [(&'static str, NativeFn); 7] = [
("Error", error_ctor),
@@ -177,13 +369,40 @@ fn object_to_string(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> J
Ok(Value::str(&format!("[object {class}]")))
}
/// The Boolean a `Boolean.prototype` method was called on (sections 15.6.4.2 and 15.6.4.3): a
/// primitive, or the primitive value of a Boolean object.
fn this_boolean(interp: &mut Interp, this: &Value, name: &str) -> JsResult<bool> {
let b = match this {
Value::Bool(b) => Some(*b),
Value::Object(o) => match interp.heap.object(*o).map(|x| &x.class) {
Some(Class::Boolean(b)) => Some(*b),
_ => None,
},
_ => None,
};
b.ok_or_else(|| interp.type_error(&format!("Boolean.prototype.{name} was called on a value that is not a Boolean")))
}
/// `Boolean.prototype.toString` (section 15.6.4.2).
fn boolean_to_string(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let b = this_boolean(interp, &this, "toString")?;
Ok(Value::str(if b { "true" } else { "false" }))
}
/// `Boolean.prototype.valueOf` (section 15.6.4.3).
fn boolean_value_of(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
this_boolean(interp, &this, "valueOf").map(Value::Bool)
}
/// `Object.prototype.toLocaleString` (section 15.2.4.3).
fn object_to_locale_string(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
let o = interp.to_object(&this)?;
let f = interp.get_from(o, &key("toString"), &this)?;
let receiver = Value::Object(o);
let f = interp.get_from(o, &key("toString"), &receiver)?;
if !interp.is_callable(&f) {
return Err(interp.type_error("toString is not a function"));
}
interp.call(&f, &this, &[])
interp.call(&f, &receiver, &[])
}
fn object_value_of(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
@@ -283,11 +502,40 @@ fn function_to_string(interp: &mut Interp, this: Value, _: &[Value], _: bool) ->
}
Some(Func::Native { name, .. }) => format!("function {name}() {{ [native code] }}"),
Some(Func::Host { name, .. }) => format!("function {name}() {{ [native code] }}"),
Some(Func::Bound { .. }) => "function () { [native code] }".to_string(),
None => return Err(interp.type_error("Function.prototype.toString was called on a value that is not a function")),
};
Ok(Value::str(&text))
}
/// `Function.prototype.bind` (section 15.3.4.5).
fn function_bind(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
require_callable(interp, &this, "the value bind was called on")?;
let Some(target) = this.as_object() else { return Err(interp.type_error("the value bind was called on is not a function")) };
let bound_this = arg(args, 0);
let bound_args: Rc<[Value]> = args.get(1..).unwrap_or_default().into();
// A host function's `length` is an ordinary property a script can set, so it is converted
// to an integer first, as later editions specify.
let target_length = match interp.get_from(target, &key("length"), &this)? {
Value::Number(n) if n.is_nan() => 0.0,
Value::Number(n) => n.trunc(),
_ => 0.0,
};
// Adding 0 turns a negative zero into 0.
let length = (target_length - bound_args.len() as f64).max(0.0) + 0.0;
interp.reserve_heap(OBJECT_BYTES + 4 * PROP_BYTES + BOUND_ARG_BYTES * bound_args.len())?;
let proto = interp.intrinsics.function_proto;
let class = Class::Function(Func::Bound { target, this: bound_this, args: bound_args.clone() });
let f = interp.heap.alloc_object(Object::new(class, Some(proto)));
interp.heap.bytes = interp.heap.bytes.saturating_add(BOUND_ARG_BYTES * bound_args.len());
interp.define(f, key("length"), Prop::Data { value: Value::Number(length), flags: 0 })?;
let thrower = Some(interp.intrinsics.throw_type_error);
for name in ["caller", "arguments"] {
interp.define(f, key(name), Prop::Accessor { get: thrower, set: thrower, flags: 0 })?;
}
Ok(Value::Object(f))
}
/// The global `eval` called indirectly (section 15.1.2.1.1): global code in the global
/// environment.
fn eval(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
+854
View File
@@ -0,0 +1,854 @@
//! `Array` (ECMA-262 5.1 section 15.4): the constructor, `Array.isArray` and every method of
//! `Array.prototype`. The methods are generic: they read `length` and the elements of any object.
//!
//! A method that walks a range of indices visits only the indices that can hold an element when
//! the range is much longer than the object has properties (a sparse array such as
//! `new Array(4294967295)`), so a long empty range costs nothing and no index is allocated for
//! it. The list of indices is made again whenever a script adds an index property to any object
//! while the method runs. Every index visited takes a unit of fuel.
use super::{
Builder, ELEMENT_BYTES, arg, constructor, create_at, delete_at, get_at, has_at, length_of, method, native, new_array, object_to_string, put_at,
require_callable, to_integer,
};
use crate::heap::{Class, NativeKind, ObjId};
use crate::interp::{Abrupt, Interp, JsResult, strict_eq};
use crate::value::{Str, Value, to_uint32};
pub(super) fn install(interp: &mut Interp) -> JsResult<()> {
let proto = interp.intrinsics.array_proto;
let array = native(interp, "Array", array_ctor, 1, NativeKind::Plain, true)?;
constructor(interp, "Array", array, proto)?;
method(interp, array, "isArray", is_array, 1)?;
let methods: [(&'static str, crate::heap::NativeFn, u32); 21] = [
("toString", to_string, 0),
("toLocaleString", to_locale_string, 0),
("concat", concat, 1),
("join", join, 1),
("pop", pop, 0),
("push", push, 1),
("reverse", reverse, 0),
("shift", shift, 0),
("slice", slice, 2),
("sort", sort, 1),
("splice", splice, 2),
("unshift", unshift, 1),
("indexOf", index_of, 1),
("lastIndexOf", last_index_of, 1),
("every", every, 1),
("some", some, 1),
("forEach", for_each, 1),
("map", map, 1),
("filter", filter, 1),
("reduce", reduce, 1),
("reduceRight", reduce_right, 1),
];
for (name, f, length) in methods {
method(interp, proto, name, f, length)?;
}
Ok(())
}
// ---- visiting indices ----
/// Ranges at most this long are visited index by index.
const DENSE_SPAN: u64 = 4096;
/// How an index an object holds names an index to visit.
#[derive(Clone, Copy)]
enum Map {
/// The index `p` asks for a visit of `p - shift`.
Shift(i64),
/// The index `p` asks for a visit of `p` and of `mirror - p`.
Mirror(i64),
}
/// The indices of a range, in order, that an operation must visit.
struct Indices {
obj: ObjId,
/// The range left to visit, `lo` included and `hi` not.
lo: u64,
hi: u64,
ascending: bool,
maps: Vec<Map>,
/// The indices left when the range is visited sparsely.
sparse: Option<Sparse>,
}
struct Sparse {
list: Vec<u64>,
pos: usize,
/// [`crate::heap::Heap::index_epoch`] when the list was made.
epoch: u64,
}
/// The canonical numeric property name `k` stands for, up to 15 digits.
fn numeric_index(k: &[u16]) -> Option<u64> {
if k.is_empty() || k.len() > 15 || (k.len() > 1 && k.first() == Some(&u16::from(b'0'))) {
return None;
}
let mut n = 0u64;
for &u in k {
let d = u.checked_sub(u16::from(b'0')).filter(|d| *d <= 9)?;
n = n * 10 + u64::from(d);
}
Some(n)
}
/// The objects of the prototype chain from `obj`, with how many properties (and String object
/// indices) each holds.
fn chain(interp: &Interp, obj: ObjId) -> Vec<(ObjId, usize)> {
let mut out = Vec::new();
let mut cur = Some(obj);
while let Some(id) = cur {
let Some(o) = interp.heap.object(id) else { break };
let string_len = if let Class::String(s) = &o.class { s.len() } else { 0 };
out.push((id, o.props.len().saturating_add(string_len)));
cur = o.proto;
}
out
}
impl Indices {
fn new(interp: &mut Interp, obj: ObjId, lo: u64, hi: u64, ascending: bool, maps: Vec<Map>) -> JsResult<Self> {
let mut it = Indices { obj, lo, hi, ascending, maps, sparse: None };
if hi.saturating_sub(lo) > DENSE_SPAN {
let present: usize = chain(interp, obj).iter().map(|(_, n)| *n).fold(0, usize::saturating_add);
if hi - lo > (present as u64).saturating_mul(8) {
it.sparse = Some(it.gather(interp)?);
}
}
Ok(it)
}
fn up(interp: &mut Interp, obj: ObjId, lo: u64, hi: u64) -> JsResult<Self> {
Self::new(interp, obj, lo, hi, true, vec![Map::Shift(0)])
}
fn down(interp: &mut Interp, obj: ObjId, lo: u64, hi: u64) -> JsResult<Self> {
Self::new(interp, obj, lo, hi, false, vec![Map::Shift(0)])
}
/// Lists the indices left to visit from the properties of the object and its prototypes.
/// Each property takes a unit of fuel.
fn gather(&self, interp: &mut Interp) -> JsResult<Sparse> {
let epoch = interp.heap.index_epoch;
let mut list: Vec<u64> = Vec::new();
for (id, n) in chain(interp, self.obj) {
for _ in 0..n {
interp.use_fuel()?;
}
interp.reserve_heap(n.saturating_mul(8).saturating_mul(self.maps.len() + 1))?;
let Some(o) = interp.heap.object(id) else { continue };
let string_len = if let Class::String(s) = &o.class { s.len() } else { 0 };
let present = (0..string_len as u64).chain(o.props.iter().filter_map(|(k, _)| numeric_index(k)));
for p in present {
for map in &self.maps {
let p = p as i64;
let ks = match *map {
Map::Shift(s) => [Some(p - s), None],
Map::Mirror(m) => [Some(p), Some(m - p)],
};
for k in ks.into_iter().flatten() {
if k >= 0 && (k as u64) >= self.lo && (k as u64) < self.hi {
list.push(k as u64);
}
}
}
}
}
if self.ascending {
list.sort_unstable();
} else {
list.sort_unstable_by(|a, b| b.cmp(a));
}
list.dedup();
Ok(Sparse { list, pos: 0, epoch })
}
/// The next index to visit; it takes a unit of fuel.
fn next(&mut self, interp: &mut Interp) -> JsResult<Option<u64>> {
if self.lo >= self.hi {
return Ok(None);
}
if self.sparse.as_ref().is_some_and(|s| s.epoch != interp.heap.index_epoch) {
self.sparse = Some(self.gather(interp)?);
}
let k = match &mut self.sparse {
None if self.ascending => self.lo,
None => self.hi - 1,
Some(s) => {
let Some(&k) = s.list.get(s.pos) else {
self.lo = self.hi;
return Ok(None);
};
s.pos += 1;
k
}
};
if self.ascending {
self.lo = k + 1;
} else {
self.hi = k;
}
interp.use_fuel()?;
Ok(Some(k))
}
}
/// The element at `k` when the object or its prototypes have one.
fn element(interp: &mut Interp, o: ObjId, k: u64) -> JsResult<Option<Value>> {
let k = k as f64;
if !has_at(interp, o, k)? {
return Ok(None);
}
get_at(interp, o, k).map(Some)
}
/// ToObject of `this`, kept reachable while `f` runs.
fn with_this<T>(interp: &mut Interp, this: &Value, f: impl FnOnce(&mut Interp, ObjId) -> JsResult<T>) -> JsResult<T> {
let o = interp.to_object(this)?;
interp.rooted(&[Value::Object(o)], |interp| f(interp, o))
}
/// The start a relative index gives within `0..=len` (`slice` and `splice`).
fn relative(interp: &mut Interp, v: &Value, len: u32) -> JsResult<u64> {
let n = to_integer(interp, v)?;
let len = f64::from(len);
Ok(if n < 0.0 { (len + n).max(0.0) } else { n.min(len) } as u64)
}
fn number(k: u64) -> Value {
Value::Number(k as f64)
}
// ---- the constructor ----
/// `Array(…)` and `new Array(…)` (sections 15.4.1 and 15.4.2).
fn array_ctor(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
if let [Value::Number(n)] = args {
let len = to_uint32(*n);
if f64::from(len) != *n {
return Err(interp.throw(crate::interp::ErrorType::Range, "invalid array length"));
}
return new_array(interp, len).map(Value::Object);
}
let a = new_array(interp, 0)?;
interp.rooted(&[Value::Object(a)], |interp| {
for (i, v) in args.iter().enumerate() {
interp.use_fuel()?;
create_at(interp, a, i as f64, v.clone())?;
}
Ok(Value::Object(a))
})
}
/// `Array.isArray` (section 15.4.3.2).
fn is_array(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let a = arg(args, 0).as_object().and_then(|o| interp.heap.object(o)).is_some_and(|o| matches!(o.class, Class::Array { .. }));
Ok(Value::Bool(a))
}
// ---- conversions ----
/// `Array.prototype.toString` (section 15.4.4.2).
fn to_string(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let join = interp.get_from(o, &crate::heap::key("join"), &Value::Object(o))?;
if interp.is_callable(&join) {
return interp.call(&join, &Value::Object(o), &[]);
}
object_to_string(interp, Value::Object(o), &[], false)
})
}
/// Joins the elements `0..len` of `o`, each turned into a string by `item`, with `separator`
/// between them (`,` when it is `undefined`). `length` is read before the separator is converted,
/// and the separator is converted even when `length` is 0 (section 15.4.4.5 steps 2 to 6). A cycle
/// back to an object being joined joins as an empty string.
fn join_with(interp: &mut Interp, o: ObjId, separator: &Value, item: fn(&mut Interp, Value) -> JsResult<Option<Str>>) -> JsResult<Value> {
if interp.join_stack.contains(&o) {
return Ok(Value::str(""));
}
let len = length_of(interp, o)?;
let sep = match separator {
Value::Undefined => Str::from(","),
v => interp.to_str(v)?,
};
if len == 0 {
return Ok(Value::str(""));
}
let flat = sep.flat();
let sep = &flat[..];
// A result that cannot fit is refused before any element is read.
let least = (sep.len() as u64).saturating_mul(u64::from(len - 1));
if least > (interp.limits.heap_bytes / 4) as u64 {
return Err(Abrupt::Limit(crate::LimitKind::Heap));
}
interp.join_stack.push(o);
let r = join_elements(interp, o, len, sep, item);
interp.join_stack.pop();
r
}
fn join_elements(interp: &mut Interp, o: ObjId, len: u32, sep: &[u16], item: fn(&mut Interp, Value) -> JsResult<Option<Str>>) -> JsResult<Value> {
let mut out = Builder::new();
// Separators written so far: element `k` follows `k` of them.
let mut seps = 0u64;
let mut it = Indices::up(interp, o, 0, u64::from(len))?;
while let Some(k) = it.next(interp)? {
let v = get_at(interp, o, k as f64)?;
let Some(s) = item(interp, v)? else { continue };
out.push_repeated(interp, sep, k.saturating_sub(seps))?;
seps = k;
out.push_units(interp, &s.flat())?;
}
out.push_repeated(interp, sep, u64::from(len - 1).saturating_sub(seps))?;
Ok(out.finish(interp))
}
/// An element as `join` writes it: nothing for `undefined` and `null`.
fn join_item(interp: &mut Interp, v: Value) -> JsResult<Option<Str>> {
if v.is_nullish() {
return Ok(None);
}
interp.to_str(&v).map(Some)
}
/// An element as `toLocaleString` writes it (section 15.4.4.3).
fn locale_item(interp: &mut Interp, v: Value) -> JsResult<Option<Str>> {
if v.is_nullish() {
return Ok(None);
}
let e = interp.to_object(&v)?;
let f = interp.get_from(e, &crate::heap::key("toLocaleString"), &Value::Object(e))?;
require_callable(interp, &f, "an element's toLocaleString")?;
let s = interp.call(&f, &Value::Object(e), &[])?;
interp.to_str(&s).map(Some)
}
/// `Array.prototype.join` (section 15.4.4.5).
fn join(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let separator = arg(args, 0);
with_this(interp, &this, |interp, o| join_with(interp, o, &separator, join_item))
}
/// `Array.prototype.toLocaleString` (section 15.4.4.3).
fn to_locale_string(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| join_with(interp, o, &Value::Undefined, locale_item))
}
// ---- methods that change the object ----
/// `Array.prototype.concat` (section 15.4.4.4).
fn concat(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let a = new_array(interp, 0)?;
interp.rooted(&[Value::Object(a)], |interp| {
let mut n = 0u64;
for e in std::iter::once(Value::Object(o)).chain(args.iter().cloned()) {
interp.use_fuel()?;
let spread = e.as_object().filter(|&id| interp.heap.object(id).is_some_and(|x| matches!(x.class, Class::Array { .. })));
let Some(e) = spread else {
create_at(interp, a, n as f64, e)?;
n += 1;
continue;
};
let len = u64::from(length_of(interp, e)?);
let mut it = Indices::up(interp, e, 0, len)?;
while let Some(k) = it.next(interp)? {
if let Some(v) = element(interp, e, k)? {
create_at(interp, a, (n + k) as f64, v)?;
}
}
n += len;
}
set_length(interp, a, n as f64)?;
Ok(Value::Object(a))
})
})
}
fn set_length(interp: &mut Interp, o: ObjId, n: f64) -> JsResult<()> {
interp.put(o, crate::heap::key("length"), Value::Number(n), true)
}
/// `Array.prototype.pop` (section 15.4.4.6).
fn pop(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = length_of(interp, o)?;
if len == 0 {
set_length(interp, o, 0.0)?;
return Ok(Value::Undefined);
}
let last = f64::from(len - 1);
let v = get_at(interp, o, last)?;
interp.rooted(std::slice::from_ref(&v), |interp| {
delete_at(interp, o, last)?;
set_length(interp, o, last)
})?;
Ok(v)
})
}
/// `Array.prototype.push` (section 15.4.4.7).
fn push(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let mut n = f64::from(length_of(interp, o)?);
for v in args {
interp.use_fuel()?;
put_at(interp, o, n, v.clone())?;
n += 1.0;
}
set_length(interp, o, n)?;
Ok(Value::Number(n))
})
}
/// `Array.prototype.reverse` (section 15.4.4.8).
fn reverse(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = u64::from(length_of(interp, o)?);
let middle = len / 2;
let mirror = len as i64 - 1;
let mut it = Indices::new(interp, o, 0, middle, true, vec![Map::Mirror(mirror)])?;
while let Some(lower) = it.next(interp)? {
let upper = len - lower - 1;
let lower_value = element(interp, o, lower)?;
let upper_value = interp.rooted(lower_value.as_slice(), |interp| element(interp, o, upper))?;
let mut roots: Vec<Value> = lower_value.iter().cloned().collect();
roots.extend(upper_value.iter().cloned());
interp.rooted(&roots, |interp| match (lower_value, upper_value) {
(Some(l), Some(u)) => {
put_at(interp, o, lower as f64, u)?;
put_at(interp, o, upper as f64, l)
}
(None, Some(u)) => {
put_at(interp, o, lower as f64, u)?;
delete_at(interp, o, upper as f64)
}
(Some(l), None) => {
delete_at(interp, o, lower as f64)?;
put_at(interp, o, upper as f64, l)
}
(None, None) => Ok(()),
})?;
}
Ok(Value::Object(o))
})
}
/// Moves the element at `from` to `to`, or deletes `to` when there is none at `from`.
fn move_element(interp: &mut Interp, o: ObjId, from: u64, to: u64) -> JsResult<()> {
match element(interp, o, from)? {
Some(v) => put_at(interp, o, to as f64, v),
None => delete_at(interp, o, to as f64),
}
}
/// `Array.prototype.shift` (section 15.4.4.9).
fn shift(interp: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = u64::from(length_of(interp, o)?);
if len == 0 {
set_length(interp, o, 0.0)?;
return Ok(Value::Undefined);
}
let first = get_at(interp, o, 0.0)?;
interp.rooted(std::slice::from_ref(&first), |interp| {
let mut it = Indices::new(interp, o, 1, len, true, vec![Map::Shift(0), Map::Shift(-1)])?;
while let Some(k) = it.next(interp)? {
move_element(interp, o, k, k - 1)?;
}
delete_at(interp, o, (len - 1) as f64)?;
set_length(interp, o, (len - 1) as f64)
})?;
Ok(first)
})
}
/// `Array.prototype.unshift` (section 15.4.4.13).
fn unshift(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = u64::from(length_of(interp, o)?);
let count = args.len() as u64;
// With no arguments each element is still read and written back in place (steps 6 and 7).
let maps = vec![Map::Shift(-1), Map::Shift(count as i64 - 1)];
let mut it = Indices::new(interp, o, 1, len + 1, false, maps)?;
while let Some(k) = it.next(interp)? {
move_element(interp, o, k - 1, k + count - 1)?;
}
for (j, v) in args.iter().enumerate() {
interp.use_fuel()?;
put_at(interp, o, j as f64, v.clone())?;
}
let n = (len + count) as f64;
set_length(interp, o, n)?;
Ok(Value::Number(n))
})
}
/// `Array.prototype.slice` (section 15.4.4.10).
fn slice(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = length_of(interp, o)?;
let start = relative(interp, &arg(args, 0), len)?;
let end = match arg(args, 1) {
Value::Undefined => u64::from(len),
v => relative(interp, &v, len)?,
};
let a = new_array(interp, 0)?;
interp.rooted(&[Value::Object(a)], |interp| {
let mut it = Indices::up(interp, o, start, end)?;
while let Some(k) = it.next(interp)? {
if let Some(v) = element(interp, o, k)? {
create_at(interp, a, (k - start) as f64, v)?;
}
}
set_length(interp, a, end.saturating_sub(start) as f64)?;
Ok(Value::Object(a))
})
})
}
/// `Array.prototype.splice` (section 15.4.4.12).
fn splice(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = length_of(interp, o)?;
let start = relative(interp, &arg(args, 0), len)?;
let len = u64::from(len);
let delete_count = match args.get(1) {
// Without a count, everything from the start is deleted, as later editions and
// every implementation do.
None if !args.is_empty() => len - start,
None => 0,
Some(v) => to_integer(interp, v)?.max(0.0).min((len - start) as f64) as u64,
};
let items = args.get(2..).unwrap_or_default();
let a = new_array(interp, 0)?;
interp.rooted(&[Value::Object(a)], |interp| {
let mut it = Indices::up(interp, o, start, start + delete_count)?;
while let Some(k) = it.next(interp)? {
if let Some(v) = element(interp, o, k)? {
create_at(interp, a, (k - start) as f64, v)?;
}
}
set_length(interp, a, delete_count as f64)?;
move_tail(interp, o, len, start, delete_count, items.len() as u64)?;
for (j, v) in items.iter().enumerate() {
interp.use_fuel()?;
put_at(interp, o, (start + j as u64) as f64, v.clone())?;
}
set_length(interp, o, (len - delete_count + items.len() as u64) as f64)?;
Ok(Value::Object(a))
})
})
}
/// Moves the elements after a spliced range so that `item_count` items replace `delete_count`
/// elements at `start` (section 15.4.4.12, steps 12 and 13).
fn move_tail(interp: &mut Interp, o: ObjId, len: u64, start: u64, delete_count: u64, item_count: u64) -> JsResult<()> {
let (dc, ic) = (delete_count as i64, item_count as i64);
if item_count < delete_count {
let mut it = Indices::new(interp, o, start, len - delete_count, true, vec![Map::Shift(dc), Map::Shift(ic)])?;
while let Some(k) = it.next(interp)? {
move_element(interp, o, k + delete_count, k + item_count)?;
}
let mut it = Indices::down(interp, o, len - delete_count + item_count, len)?;
while let Some(k) = it.next(interp)? {
delete_at(interp, o, k as f64)?;
}
} else if item_count > delete_count {
let mut it = Indices::new(interp, o, start + 1, len - delete_count + 1, false, vec![Map::Shift(dc - 1), Map::Shift(ic - 1)])?;
while let Some(k) = it.next(interp)? {
move_element(interp, o, k + delete_count - 1, k + item_count - 1)?;
}
}
Ok(())
}
// ---- sort ----
/// A stable merge sort whose comparison may fail. `greater(a, b)` says whether `a` sorts after
/// `b`; each comparison takes a unit of fuel.
fn merge_sort<T: Clone>(interp: &mut Interp, items: &mut Vec<T>, mut greater: impl FnMut(&mut Interp, &T, &T) -> JsResult<bool>) -> JsResult<()> {
let n = items.len();
let mut width = 1;
let mut buf: Vec<T> = Vec::with_capacity(n);
while width < n {
buf.clear();
let mut lo = 0;
while lo < n {
let mid = (lo + width).min(n);
let hi = (lo + 2 * width).min(n);
let (mut i, mut j) = (lo, mid);
while i < mid && j < hi {
let (Some(a), Some(b)) = (items.get(i), items.get(j)) else { break };
interp.use_fuel()?;
if greater(interp, a, b)? {
buf.push(b.clone());
j += 1;
} else {
buf.push(a.clone());
i += 1;
}
}
buf.extend(items.get(i..mid).unwrap_or_default().iter().cloned());
buf.extend(items.get(j..hi).unwrap_or_default().iter().cloned());
lo = hi;
}
std::mem::swap(items, &mut buf);
width *= 2;
}
Ok(())
}
/// `Array.prototype.sort` (section 15.4.4.11). The elements are read, sorted with `undefined`
/// after every other value, written back from index 0, and the indices after them are deleted.
fn sort(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
let compare = arg(args, 0);
if !matches!(compare, Value::Undefined) {
require_callable(interp, &compare, "the comparison")?;
}
with_this(interp, &this, |interp, o| {
let len = u64::from(length_of(interp, o)?);
let mark = interp.temp_roots.len();
let r = sort_elements(interp, o, len, &compare);
interp.temp_roots.truncate(mark);
r?;
Ok(Value::Object(o))
})
}
fn sort_elements(interp: &mut Interp, o: ObjId, len: u64, compare: &Value) -> JsResult<()> {
let mut values = Vec::new();
let mut undefined = 0u64;
let mut it = Indices::up(interp, o, 0, len)?;
while let Some(k) = it.next(interp)? {
match element(interp, o, k)? {
Some(Value::Undefined) => undefined += 1,
Some(v) => {
interp.temp_roots.push(v.clone());
interp.reserve_heap(ELEMENT_BYTES)?;
values.push(v);
}
None => {}
}
}
if matches!(compare, Value::Undefined) {
let mut keyed = Vec::with_capacity(values.len());
for v in values {
interp.use_fuel()?;
let s = interp.to_str(&v)?;
// Flattening a concatenation makes a new buffer, so it is charged. A key that is
// not an element is rooted so that collections count it.
let rope = s.buffer_addr().is_none();
let bytes = crate::heap::string_bytes(s.len());
if rope {
interp.reserve_heap(bytes)?;
}
let units = s.flat();
if rope {
interp.heap.bytes = interp.heap.bytes.saturating_add(bytes);
}
if !matches!(v, Value::String(_)) {
interp.temp_roots.push(Value::String(s));
}
keyed.push((units, v));
}
merge_sort(interp, &mut keyed, |_, a, b| Ok(a.0 > b.0))?;
values = keyed.into_iter().map(|(_, v)| v).collect();
} else {
merge_sort(interp, &mut values, |interp, a, b| {
let r = interp.call(compare, &Value::Undefined, &[a.clone(), b.clone()])?;
Ok(interp.to_number(&r)? > 0.0)
})?;
}
let mut i = 0u64;
for v in values {
interp.use_fuel()?;
put_at(interp, o, i as f64, v)?;
i += 1;
}
for _ in 0..undefined {
interp.use_fuel()?;
put_at(interp, o, i as f64, Value::Undefined)?;
i += 1;
}
let mut it = Indices::down(interp, o, i, len)?;
while let Some(k) = it.next(interp)? {
delete_at(interp, o, k as f64)?;
}
Ok(())
}
// ---- searching ----
/// `Array.prototype.indexOf` (section 15.4.4.14).
fn index_of(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = f64::from(length_of(interp, o)?);
if len == 0.0 {
return Ok(Value::Number(-1.0));
}
let n = if args.len() > 1 { to_integer(interp, &arg(args, 1))? } else { 0.0 };
if n >= len {
return Ok(Value::Number(-1.0));
}
let start = if n >= 0.0 { n } else { (len + n).max(0.0) };
let target = arg(args, 0);
let mut it = Indices::up(interp, o, start as u64, len as u64)?;
while let Some(k) = it.next(interp)? {
if let Some(v) = element(interp, o, k)?
&& strict_eq(&v, &target)
{
return Ok(number(k));
}
}
Ok(Value::Number(-1.0))
})
}
/// `Array.prototype.lastIndexOf` (section 15.4.4.15).
fn last_index_of(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = f64::from(length_of(interp, o)?);
if len == 0.0 {
return Ok(Value::Number(-1.0));
}
let n = if args.len() > 1 { to_integer(interp, &arg(args, 1))? } else { len - 1.0 };
let from = if n >= 0.0 { n.min(len - 1.0) } else { len + n };
if from < 0.0 {
return Ok(Value::Number(-1.0));
}
let target = arg(args, 0);
let mut it = Indices::down(interp, o, 0, from as u64 + 1)?;
while let Some(k) = it.next(interp)? {
if let Some(v) = element(interp, o, k)?
&& strict_eq(&v, &target)
{
return Ok(number(k));
}
}
Ok(Value::Number(-1.0))
})
}
// ---- iteration ----
/// What an iteration method does with each callback result.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Each {
Every,
Some,
/// `forEach`.
Visit,
Map,
Filter,
}
/// `every`, `some`, `forEach`, `map` and `filter` (sections 15.4.4.16 to 15.4.4.20).
fn iterate(interp: &mut Interp, this: Value, args: &[Value], kind: Each) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = length_of(interp, o)?;
let callback = arg(args, 0);
require_callable(interp, &callback, "the callback")?;
let this_arg = arg(args, 1);
let a = match kind {
Each::Map => Some(new_array(interp, len)?),
Each::Filter => Some(new_array(interp, 0)?),
_ => None,
};
let roots: Vec<Value> = a.map(Value::Object).into_iter().collect();
interp.rooted(&roots, |interp| {
let mut to = 0u64;
let mut it = Indices::up(interp, o, 0, u64::from(len))?;
while let Some(k) = it.next(interp)? {
let Some(v) = element(interp, o, k)? else { continue };
let r = interp.call(&callback, &this_arg, &[v.clone(), number(k), Value::Object(o)])?;
match (kind, a) {
(Each::Every, _) if !r.truthy() => return Ok(Value::Bool(false)),
(Each::Some, _) if r.truthy() => return Ok(Value::Bool(true)),
(Each::Map, Some(a)) => create_at(interp, a, k as f64, r)?,
(Each::Filter, Some(a)) if r.truthy() => {
create_at(interp, a, to as f64, v)?;
to += 1;
}
_ => {}
}
}
Ok(match (kind, a) {
(Each::Every, _) => Value::Bool(true),
(Each::Some, _) => Value::Bool(false),
(_, Some(a)) => Value::Object(a),
_ => Value::Undefined,
})
})
})
}
fn every(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
iterate(interp, this, args, Each::Every)
}
fn some(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
iterate(interp, this, args, Each::Some)
}
fn for_each(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
iterate(interp, this, args, Each::Visit)
}
fn map(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
iterate(interp, this, args, Each::Map)
}
fn filter(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
iterate(interp, this, args, Each::Filter)
}
/// `reduce` and `reduceRight` (sections 15.4.4.21 and 15.4.4.22).
fn fold(interp: &mut Interp, this: Value, args: &[Value], right: bool) -> JsResult<Value> {
with_this(interp, &this, |interp, o| {
let len = u64::from(length_of(interp, o)?);
let callback = arg(args, 0);
require_callable(interp, &callback, "the callback")?;
let mut it = Indices::new(interp, o, 0, len, !right, vec![Map::Shift(0)])?;
let slot = interp.temp_roots.len();
interp.temp_roots.push(Value::Undefined);
// The accumulator is kept in a root slot while the elements are read.
let keep = |interp: &mut Interp, acc: &Value| {
if let Some(s) = interp.temp_roots.get_mut(slot) {
*s = acc.clone();
}
};
let r = (|| {
let mut acc = match args.get(1) {
Some(v) => v.clone(),
None => loop {
let Some(k) = it.next(interp)? else { return Err(interp.type_error("reduce of an empty array with no initial value")) };
if let Some(v) = element(interp, o, k)? {
break v;
}
},
};
keep(interp, &acc);
while let Some(k) = it.next(interp)? {
let Some(v) = element(interp, o, k)? else { continue };
acc = interp.call(&callback, &Value::Undefined, &[acc, v, number(k), Value::Object(o)])?;
keep(interp, &acc);
}
Ok(acc)
})();
interp.temp_roots.truncate(slot);
r
})
}
fn reduce(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
fold(interp, this, args, false)
}
fn reduce_right(interp: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
fold(interp, this, args, true)
}
+364
View File
@@ -0,0 +1,364 @@
//! The global functions (ECMA-262 5.1 section 15.1.2 and 15.1.3): `parseInt`, `parseFloat`,
//! `isNaN`, `isFinite` and the URI functions, and Annex B's `escape` and `unescape` (sections
//! B.2.1 and B.2.2). Each code unit read takes a unit of fuel, and the strings they build are
//! checked against the heap limit as they grow.
use super::{Builder, arg, method};
use crate::heap::NativeFn;
use crate::interp::{ErrorType, Interp, JsResult};
use crate::value::{Value, is_str_space, to_int32};
pub(super) fn install(interp: &mut Interp) -> JsResult<()> {
let global = interp.global;
let functions: [(&'static str, NativeFn, u32); 10] = [
("parseInt", parse_int, 2),
("parseFloat", parse_float, 1),
("isNaN", is_nan, 1),
("isFinite", is_finite, 1),
("decodeURI", decode_uri, 1),
("decodeURIComponent", decode_uri_component, 1),
("encodeURI", encode_uri, 1),
("encodeURIComponent", encode_uri_component, 1),
("escape", escape, 1),
("unescape", unescape, 1),
];
for (name, f, length) in functions {
method(interp, global, name, f, length)?;
}
Ok(())
}
/// `s` after its leading white space and line terminators.
fn trim_start(s: &[u16]) -> &[u16] {
s.get(s.iter().position(|&u| !is_str_space(u)).unwrap_or(s.len())..).unwrap_or_default()
}
/// ToString of the first argument, as code units, with each one taking a unit of fuel. A
/// concatenation is flattened into a new buffer, which is charged to the heap.
pub(super) fn units_of(interp: &mut Interp, args: &[Value]) -> JsResult<std::rc::Rc<[u16]>> {
let s = interp.to_str(&arg(args, 0))?;
for _ in 0..s.len() {
interp.use_fuel()?;
}
let rope = s.buffer_addr().is_none();
let bytes = crate::heap::string_bytes(s.len());
if rope {
let mark = interp.temp_roots.len();
interp.temp_roots.push(Value::String(s.clone()));
let reserved = interp.reserve_heap(bytes);
interp.temp_roots.truncate(mark);
reserved?;
}
let units = s.flat();
if rope {
interp.heap.bytes = interp.heap.bytes.saturating_add(bytes);
}
Ok(units)
}
fn digit_value(u: u16) -> Option<u32> {
char::from_u32(u32::from(u)).and_then(|c| c.to_digit(36))
}
/// `parseInt` (section 15.1.2.2).
fn parse_int(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let s = units_of(interp, args)?;
let radix = to_int32(interp.to_number(&arg(args, 1))?);
let mut rest = trim_start(&s);
let sign = match rest.first() {
Some(&u) if u == u16::from(b'-') => -1.0,
_ => 1.0,
};
if matches!(rest.first(), Some(&u) if u == u16::from(b'-') || u == u16::from(b'+')) {
rest = rest.get(1..).unwrap_or_default();
}
let mut r = radix as u32;
let mut strip_prefix = true;
if radix != 0 {
if !(2..=36).contains(&radix) {
return Ok(Value::Number(f64::NAN));
}
strip_prefix = radix == 16;
} else {
r = 10;
}
if strip_prefix
&& rest.len() >= 2
&& rest.first() == Some(&u16::from(b'0'))
&& matches!(rest.get(1), Some(&u) if u == u16::from(b'x') || u == u16::from(b'X'))
{
rest = rest.get(2..).unwrap_or_default();
r = 16;
}
let n = rest.iter().map_while(|&u| digit_value(u).filter(|&d| d < r)).count();
let prefix = rest.get(..n).unwrap_or_default();
if prefix.is_empty() {
return Ok(Value::Number(f64::NAN));
}
let digits = prefix.iter().filter_map(|&u| digit_value(u));
let value = match r {
10 => parse_decimal(interp, prefix)?,
2 | 4 | 8 | 16 | 32 => crate::chars::radix_value(digits, r).unwrap_or(f64::NAN),
// Other radices may be approximated (section 15.1.2.2, step 13).
_ => digits.fold(0.0, |n, d| n * f64::from(r) + f64::from(d)),
};
Ok(Value::Number(sign * value))
}
/// The number that `text`, ASCII decimal digits with an optional fraction and exponent, stands
/// for. The text is copied into a string of one byte per code unit for parsing, and that copy is
/// checked against the heap limit first.
fn parse_decimal(interp: &mut Interp, text: &[u16]) -> JsResult<f64> {
interp.reserve_heap(text.len())?;
let mut ascii = String::with_capacity(text.len());
ascii.extend(text.iter().filter_map(|&u| char::from_u32(u32::from(u))));
Ok(ascii.parse::<f64>().unwrap_or(f64::NAN))
}
/// The length of the longest prefix of `s` that is a StrDecimalLiteral without its sign and
/// without `Infinity`.
fn decimal_prefix(s: &[u16]) -> usize {
let is_digit = |i: usize| s.get(i).is_some_and(|&u| (u16::from(b'0')..=u16::from(b'9')).contains(&u));
let mut i = 0;
while is_digit(i) {
i += 1;
}
let mut mantissa = i;
if s.get(i) == Some(&u16::from(b'.')) {
let mut j = i + 1;
while is_digit(j) {
j += 1;
}
mantissa += j - i - 1;
if mantissa > 0 {
i = j;
}
}
if mantissa == 0 {
return 0;
}
if matches!(s.get(i), Some(&u) if u == u16::from(b'e') || u == u16::from(b'E')) {
let mut j = i + 1;
if matches!(s.get(j), Some(&u) if u == u16::from(b'+') || u == u16::from(b'-')) {
j += 1;
}
let digits_start = j;
while is_digit(j) {
j += 1;
}
if j > digits_start {
i = j;
}
}
i
}
/// `parseFloat` (section 15.1.2.3).
fn parse_float(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let s = units_of(interp, args)?;
let mut rest = trim_start(&s);
let mut sign = 1.0;
if let Some(&u) = rest.first()
&& (u == u16::from(b'-') || u == u16::from(b'+'))
{
if u == u16::from(b'-') {
sign = -1.0;
}
rest = rest.get(1..).unwrap_or_default();
}
let infinity: Vec<u16> = "Infinity".encode_utf16().collect();
if rest.starts_with(&infinity) {
return Ok(Value::Number(sign * f64::INFINITY));
}
let n = decimal_prefix(rest);
if n == 0 {
return Ok(Value::Number(f64::NAN));
}
Ok(Value::Number(sign * parse_decimal(interp, rest.get(..n).unwrap_or_default())?))
}
/// `isNaN` (section 15.1.2.4).
fn is_nan(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
Ok(Value::Bool(interp.to_number(&arg(args, 0))?.is_nan()))
}
/// `isFinite` (section 15.1.2.5).
fn is_finite(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
Ok(Value::Bool(interp.to_number(&arg(args, 0))?.is_finite()))
}
// ---- URIs ----
const URI_RESERVED: &str = ";/?:@&=+$,";
const URI_MARK: &str = "-_.!~*'()";
fn in_set(u: u16, extra: &str) -> bool {
u < 0x80 && extra.as_bytes().contains(&(u as u8))
}
fn is_unreserved(u: u16) -> bool {
u < 0x80 && ((u as u8).is_ascii_alphanumeric() || in_set(u, URI_MARK))
}
fn uri_error(interp: &mut Interp, why: &str) -> crate::interp::Abrupt {
interp.throw(ErrorType::Uri, why)
}
const HEX: &[u8; 16] = b"0123456789ABCDEF";
fn push_byte(out: &mut Builder, interp: &mut Interp, b: u8) -> JsResult<()> {
let units = [u16::from(b'%'), u16::from(HEX[usize::from(b >> 4)]), u16::from(HEX[usize::from(b & 15)])];
out.push_units(interp, &units)
}
/// Encode (section 15.1.3): every code unit outside the unescaped set as the `%XX` escapes of
/// its UTF-8 bytes.
fn encode(interp: &mut Interp, args: &[Value], unescaped: fn(u16) -> bool) -> JsResult<Value> {
let s = units_of(interp, args)?;
let mut out = Builder::new();
let mut k = 0;
while let Some(&c) = s.get(k) {
if unescaped(c) {
out.push_units(interp, &[c])?;
k += 1;
continue;
}
let v = match c {
0xDC00..=0xDFFF => return Err(uri_error(interp, "a lone trailing surrogate cannot be encoded")),
0xD800..=0xDBFF => match s.get(k + 1) {
Some(&d) if (0xDC00..=0xDFFF).contains(&d) => {
k += 1;
(u32::from(c) - 0xD800) * 0x400 + (u32::from(d) - 0xDC00) + 0x10000
}
_ => return Err(uri_error(interp, "a lone leading surrogate cannot be encoded")),
},
_ => u32::from(c),
};
let mut buf = [0u8; 4];
let Some(ch) = char::from_u32(v) else { return Err(uri_error(interp, "the string holds a code point that cannot be encoded")) };
for &b in ch.encode_utf8(&mut buf).as_bytes() {
push_byte(&mut out, interp, b)?;
}
k += 1;
}
Ok(out.finish(interp))
}
fn hex_byte(s: &[u16], at: usize) -> Option<u8> {
let hi = crate::chars::hex_value(u32::from(*s.get(at)?))?;
let lo = crate::chars::hex_value(u32::from(*s.get(at + 1)?))?;
u8::try_from(hi * 16 + lo).ok()
}
/// Decode (section 15.1.3): each `%XX` escape sequence of a UTF-8 character as the character,
/// except an escaped character in `reserved`, which stays escaped.
fn decode(interp: &mut Interp, args: &[Value], reserved: &str) -> JsResult<Value> {
let s = units_of(interp, args)?;
let mut out = Builder::new();
let mut k = 0;
while let Some(&c) = s.get(k) {
if c != u16::from(b'%') {
out.push_units(interp, &[c])?;
k += 1;
continue;
}
let start = k;
let Some(b) = hex_byte(&s, k + 1) else { return Err(uri_error(interp, "a % is not followed by two hexadecimal digits")) };
k += 2;
if b & 0x80 == 0 {
let c = u16::from(b);
if in_set(c, reserved) {
out.push_units(interp, s.get(start..=k).unwrap_or_default())?;
} else {
out.push_units(interp, &[c])?;
}
k += 1;
continue;
}
let n = b.leading_ones() as usize;
if n == 1 || n > 4 {
return Err(uri_error(interp, "an escape sequence is not the start of a UTF-8 character"));
}
let mut octets = vec![b];
for _ in 1..n {
k += 1;
let next = if s.get(k) == Some(&u16::from(b'%')) { hex_byte(&s, k + 1) } else { None };
let Some(b) = next.filter(|b| b & 0xC0 == 0x80) else { return Err(uri_error(interp, "an escaped UTF-8 character is incomplete")) };
k += 2;
octets.push(b);
}
let Some(ch) = std::str::from_utf8(&octets).ok().and_then(|t| t.chars().next()) else {
return Err(uri_error(interp, "an escape sequence is not valid UTF-8"));
};
let mut units = [0u16; 2];
out.push_units(interp, ch.encode_utf16(&mut units))?;
k += 1;
}
Ok(out.finish(interp))
}
fn encode_uri(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
encode(interp, args, |u| is_unreserved(u) || in_set(u, URI_RESERVED) || u == u16::from(b'#'))
}
fn encode_uri_component(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
encode(interp, args, is_unreserved)
}
fn decode_uri(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
decode(interp, args, ";/?:@&=+$,#")
}
fn decode_uri_component(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
decode(interp, args, "")
}
// ---- Annex B ----
/// `escape` (section B.2.1).
fn escape(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let s = units_of(interp, args)?;
let mut out = Builder::new();
for &u in s.iter() {
if u < 0x80 && ((u as u8).is_ascii_alphanumeric() || b"@*_+-./".contains(&(u as u8))) {
out.push_units(interp, &[u])?;
} else if u < 256 {
push_byte(&mut out, interp, u as u8)?;
} else {
let hex = |shift: u16| u16::from(HEX[usize::from((u >> shift) & 15)]);
out.push_units(interp, &[u16::from(b'%'), u16::from(b'u'), hex(12), hex(8), hex(4), hex(0)])?;
}
}
Ok(out.finish(interp))
}
/// `unescape` (section B.2.2).
fn unescape(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let s = units_of(interp, args)?;
let mut out = Builder::new();
let mut k = 0;
let hex4 = |at: usize| -> Option<u16> {
let mut v = 0u16;
for i in 0..4 {
v = v * 16 + u16::try_from(crate::chars::hex_value(u32::from(*s.get(at + i)?))?).ok()?;
}
Some(v)
};
while let Some(&c) = s.get(k) {
let mut unit = c;
if c == u16::from(b'%') {
if s.get(k + 1) == Some(&u16::from(b'u'))
&& let Some(v) = hex4(k + 2)
{
unit = v;
k += 5;
} else if let Some(b) = hex_byte(&s, k + 1) {
unit = u16::from(b);
k += 2;
}
}
out.push_units(interp, &[unit])?;
k += 1;
}
Ok(out.finish(interp))
}
+734
View File
@@ -0,0 +1,734 @@
//! The `JSON` object (ECMA-262 5.1 section 15.12): `parse` with a reviver and `stringify` with a
//! replacer function or property list and indentation.
//!
//! Both work without recursion. `parse` reads nested arrays and objects with an explicit stack and
//! walks them for the reviver with another, and `stringify` writes nested values from a stack of
//! the objects it is inside, so nesting is limited only by the heap. Each code unit of the text
//! parsed, each property visited and each value written takes a unit of fuel; strings, arrays and
//! objects are checked against the heap limit before they are made, and the text `stringify`
//! writes is checked as it grows.
use std::collections::HashSet;
use super::{Builder, ELEMENT_BYTES, arg, data, key_of, length_of, method, new_array, new_string, to_integer};
use crate::heap::{Class, Key, OBJECT_BYTES, OPEN, ObjId, Object, Prop, key, string_bytes};
use crate::interp::{Desc, ErrorType, Hint, Interp, JsResult};
use crate::value::{Str, Value, number_to_str};
pub(super) fn install(interp: &mut Interp) -> JsResult<()> {
let proto = interp.intrinsics.object_proto;
let json = interp.heap.alloc_object(Object::new(Class::Json, Some(proto)));
method(interp, json, "parse", parse, 2)?;
method(interp, json, "stringify", stringify, 3)?;
let global = interp.global;
data(interp, global, "JSON", Value::Object(json), crate::heap::HIDDEN)
}
// ---- parse ----
/// A container being read, with the property name its next value goes under.
enum Open {
Array { obj: ObjId, next: u32 },
Object { obj: ObjId, name: Option<Key> },
}
struct Parser<'a> {
s: &'a [u16],
pos: usize,
}
const QUOTE: u16 = b'"' as u16;
impl Parser<'_> {
fn peek(&self) -> Option<u16> {
self.s.get(self.pos).copied()
}
fn skip_space(&mut self) {
while matches!(self.peek(), Some(0x09 | 0x0A | 0x0D | 0x20)) {
self.pos += 1;
}
}
fn eat(&mut self, c: u8) -> bool {
if self.peek() == Some(u16::from(c)) {
self.pos += 1;
return true;
}
false
}
fn word(&mut self, w: &str) -> bool {
let units: Vec<u16> = w.encode_utf16().collect();
if self.s.get(self.pos..).is_some_and(|rest| rest.starts_with(&units)) {
self.pos += units.len();
return true;
}
false
}
/// A JSONString's characters (section 15.12.1.1), after its opening quote.
fn string(&mut self) -> Result<Vec<u16>, &'static str> {
let mut out = Vec::new();
loop {
let Some(c) = self.peek() else { return Err("a string is not closed") };
self.pos += 1;
match c {
QUOTE => return Ok(out),
0x5C => {
let Some(e) = self.peek() else { return Err("a string is not closed") };
self.pos += 1;
let unit = match u8::try_from(e).unwrap_or(0) {
b'"' => QUOTE,
b'\\' => 0x5C,
b'/' => 0x2F,
b'b' => 0x08,
b'f' => 0x0C,
b'n' => 0x0A,
b'r' => 0x0D,
b't' => 0x09,
b'u' => {
let mut v = 0u32;
for _ in 0..4 {
let Some(d) = self.peek().and_then(|u| crate::chars::hex_value(u32::from(u))) else {
return Err("a \\u escape needs four hexadecimal digits");
};
self.pos += 1;
v = v * 16 + d;
}
u16::try_from(v).unwrap_or(0)
}
_ => return Err("a string holds an escape sequence JSON does not have"),
};
out.push(unit);
}
0..=0x1F => return Err("a string holds a control character"),
_ => out.push(c),
}
}
}
/// A JSONNumber (section 15.12.1.1).
fn number(&mut self) -> Result<f64, &'static str> {
let start = self.pos;
self.eat(b'-');
let digit = |p: &Self| p.peek().is_some_and(|u| (u16::from(b'0')..=u16::from(b'9')).contains(&u));
if self.eat(b'0') {
} else if digit(self) {
while digit(self) {
self.pos += 1;
}
} else {
return Err("a number has no digits");
}
if self.eat(b'.') {
if !digit(self) {
return Err("a number's fraction has no digits");
}
while digit(self) {
self.pos += 1;
}
}
if self.eat(b'e') || self.eat(b'E') {
if !self.eat(b'+') {
self.eat(b'-');
}
if !digit(self) {
return Err("a number's exponent has no digits");
}
while digit(self) {
self.pos += 1;
}
}
let text = String::from_utf16_lossy(self.s.get(start..self.pos).unwrap_or_default());
text.parse::<f64>().map_err(|_| "a number cannot be read")
}
}
/// What the parser read at a value position.
enum Token {
Value(Value),
OpenArray,
OpenObject,
}
fn syntax(interp: &mut Interp, why: &str, at: usize) -> crate::interp::Abrupt {
interp.throw(ErrorType::Syntax, &format!("JSON.parse: {why} (at offset {at})"))
}
/// `JSON.parse` (section 15.12.2).
fn parse(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let source = Value::String(interp.to_str(&arg(args, 0))?);
let mark = interp.temp_roots.len();
// The text stays rooted for the whole parse, so a collection still counts it.
interp.temp_roots.push(source.clone());
let parsed = super::global::units_of(interp, std::slice::from_ref(&source)).and_then(|text| parse_text(interp, &text));
interp.temp_roots.truncate(mark);
drop(source);
let v = parsed?;
let reviver = arg(args, 1);
if !interp.is_callable(&reviver) {
return Ok(v);
}
interp.temp_roots.push(v.clone());
let r = revive(interp, v, &reviver);
interp.temp_roots.truncate(mark);
r
}
/// Reads a value at the parser's position: a primitive, or the start of an array or object.
fn token(interp: &mut Interp, p: &mut Parser<'_>) -> JsResult<Token> {
p.skip_space();
let at = p.pos;
let t = match p.peek() {
Some(QUOTE) => {
p.pos += 1;
let units = p.string().map_err(|why| syntax(interp, why, at))?;
Token::Value(new_string(interp, units)?)
}
Some(0x5B) => {
p.pos += 1;
Token::OpenArray
}
Some(0x7B) => {
p.pos += 1;
Token::OpenObject
}
_ if p.word("true") => Token::Value(Value::Bool(true)),
_ if p.word("false") => Token::Value(Value::Bool(false)),
_ if p.word("null") => Token::Value(Value::Null),
Some(u) if u == u16::from(b'-') || (u16::from(b'0')..=u16::from(b'9')).contains(&u) => {
Token::Value(Value::Number(p.number().map_err(|why| syntax(interp, why, at))?))
}
Some(_) => return Err(syntax(interp, "unexpected character", at)),
None => return Err(syntax(interp, "unexpected end of text", at)),
};
Ok(t)
}
/// Reads the property name of an object member and its colon, or `None` at the closing brace
/// of an empty object.
fn member_name(interp: &mut Interp, p: &mut Parser<'_>, first: bool) -> JsResult<Option<Key>> {
p.skip_space();
if first && p.eat(b'}') {
return Ok(None);
}
let at = p.pos;
if !p.eat(b'"') {
return Err(syntax(interp, "a property name must be a string", at));
}
let units = p.string().map_err(|why| syntax(interp, why, at))?;
let bytes = string_bytes(units.len());
interp.reserve_heap(bytes)?;
// The name is counted while it waits for its value; `parse_text` releases it before `define`
// accounts for it, and the native call's return releases it on every other path.
interp.heap.bytes = interp.heap.bytes.saturating_add(bytes);
interp.held_bytes = interp.held_bytes.saturating_add(bytes);
p.skip_space();
if !p.eat(b':') {
return Err(syntax(interp, "a property name must be followed by ':'", p.pos));
}
Ok(Some(units.into()))
}
fn parse_text(interp: &mut Interp, text: &[u16]) -> JsResult<Value> {
let mut p = Parser { s: text, pos: 0 };
let mut stack: Vec<Open> = Vec::new();
// The next value to read: the first of an array or object, or one after a comma.
let mut value = token(interp, &mut p)?;
loop {
// Open a container, or place a finished value into the container it belongs to.
let mut done = match value {
Token::OpenArray => {
let a = new_array(interp, 0)?;
interp.temp_roots.push(Value::Object(a));
p.skip_space();
if p.eat(b']') {
Some(Value::Object(a))
} else {
stack.push(Open::Array { obj: a, next: 0 });
value = token(interp, &mut p)?;
continue;
}
}
Token::OpenObject => {
interp.reserve_heap(OBJECT_BYTES)?;
let o = interp.new_object();
interp.temp_roots.push(Value::Object(o));
match member_name(interp, &mut p, true)? {
None => Some(Value::Object(o)),
Some(name) => {
stack.push(Open::Object { obj: o, name: Some(name) });
value = token(interp, &mut p)?;
continue;
}
}
}
Token::Value(v) => Some(v),
};
// Close every container that the finished value completes.
while let Some(v) = done.take() {
let Some(top) = stack.last_mut() else {
p.skip_space();
if p.pos < text.len() {
return Err(syntax(interp, "unexpected text after the value", p.pos));
}
return Ok(v);
};
match top {
Open::Array { obj, next } => {
let (obj, i) = (*obj, *next);
*next = next.saturating_add(1);
interp.rooted(std::slice::from_ref(&v), |i| i.reserve_heap(ELEMENT_BYTES))?;
interp.define(obj, key_of(f64::from(i)), Prop::Data { value: v, flags: OPEN })?;
p.skip_space();
if p.eat(b',') {
break;
}
if !p.eat(b']') {
return Err(syntax(interp, "expected ',' or ']'", p.pos));
}
stack.pop();
done = Some(Value::Object(obj));
}
Open::Object { obj, name } => {
let obj = *obj;
let Some(k) = name.take() else { return Err(syntax(interp, "a member has no name", p.pos)) };
interp.rooted(std::slice::from_ref(&v), |i| i.reserve_heap(ELEMENT_BYTES))?;
let held = string_bytes(k.len());
interp.heap.bytes = interp.heap.bytes.saturating_sub(held);
interp.held_bytes = interp.held_bytes.saturating_sub(held);
interp.define(obj, k, Prop::Data { value: v, flags: OPEN })?;
p.skip_space();
if p.eat(b',') {
let k = member_name(interp, &mut p, false)?;
if let Some(Open::Object { name, .. }) = stack.last_mut() {
*name = k;
}
break;
}
if !p.eat(b'}') {
return Err(syntax(interp, "expected ',' or '}'", p.pos));
}
stack.pop();
done = Some(Value::Object(obj));
}
}
}
value = token(interp, &mut p)?;
}
}
/// A holder being walked for the reviver (section 15.12.2, Walk).
struct Walk {
holder: ObjId,
name: Key,
val: Value,
keys: Vec<Key>,
next: usize,
}
/// Starts walking `holder[name]`: reads the value and, for an array or object, the names of its
/// elements or own enumerable properties. The value is pushed onto the temporary roots.
fn enter(interp: &mut Interp, holder: ObjId, name: Key) -> JsResult<Walk> {
let val = interp.get_from(holder, &name, &Value::Object(holder))?;
interp.temp_roots.push(val.clone());
let mut keys = Vec::new();
if let Value::Object(o) = val {
if interp.heap.object(o).is_some_and(|x| matches!(x.class, Class::Array { .. })) {
let len = length_of(interp, o)?;
interp.reserve_heap((len as usize).saturating_mul(crate::heap::PROP_BYTES))?;
for i in 0..len {
interp.use_fuel()?;
keys.push(key_of(f64::from(i)));
}
} else {
for k in interp.own_keys(o)? {
if interp.get_own_property(o, &k)?.is_some_and(|p| p.enumerable()) {
keys.push(k);
}
}
}
}
Ok(Walk { holder, name, val, keys, next: 0 })
}
/// Walks the parsed value bottom-up, calling the reviver for each property and replacing or
/// deleting the property with what it returns.
fn revive(interp: &mut Interp, v: Value, reviver: &Value) -> JsResult<Value> {
interp.reserve_heap(OBJECT_BYTES + ELEMENT_BYTES)?;
let root = interp.new_object();
interp.temp_roots.push(Value::Object(root));
interp.define(root, key(""), Prop::Data { value: v, flags: OPEN })?;
let mut stack = vec![enter(interp, root, key(""))?];
let mut ret: Option<Value> = None;
loop {
let Some(top) = stack.last_mut() else { return Ok(Value::Undefined) };
if let (Some(r), Value::Object(o)) = (ret.take(), &top.val) {
let o = *o;
let Some(k) = top.next.checked_sub(1).and_then(|i| top.keys.get(i)).cloned() else { return Ok(Value::Undefined) };
if matches!(r, Value::Undefined) {
interp.delete(o, &k, false)?;
} else {
interp.rooted(std::slice::from_ref(&r), |i| {
i.reserve_heap(ELEMENT_BYTES)?;
i.define_own_property(o, k, &Desc::data(r.clone(), OPEN), false)
})?;
}
}
if let (Some(k), Value::Object(o)) = (top.keys.get(top.next).cloned(), &top.val) {
let o = *o;
top.next += 1;
interp.use_fuel()?;
stack.push(enter(interp, o, k)?);
continue;
}
let Some(done) = stack.pop() else { return Ok(Value::Undefined) };
let name = Value::String(Str::from_rc(done.name));
let r = interp.call(reviver, &Value::Object(done.holder), &[name, done.val])?;
interp.temp_roots.pop();
if stack.is_empty() {
return Ok(r);
}
ret = Some(r);
}
}
// ---- stringify ----
/// The settings of one `stringify` call.
struct Settings {
replacer: Option<Value>,
properties: Option<Vec<Key>>,
gap: Vec<u16>,
}
/// A value as `stringify` writes it, after `toJSON`, the replacer and unwrapping.
enum Item {
Skip,
Primitive(Value),
Container(ObjId),
}
/// An object or array being written.
struct Writing {
obj: ObjId,
keys: Vec<Key>,
array: bool,
next: usize,
count: usize,
/// The indentation outside it.
stepback: usize,
}
struct Writer {
out: Builder,
indent: Vec<u16>,
stack: Vec<Writing>,
open: HashSet<ObjId>,
}
/// `JSON.stringify` (section 15.12.3).
fn stringify(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let mark = interp.temp_roots.len();
let r = stringify_rooted(interp, args);
interp.temp_roots.truncate(mark);
r
}
fn stringify_rooted(interp: &mut Interp, args: &[Value]) -> JsResult<Value> {
let settings = settings(interp, &arg(args, 1), &arg(args, 2))?;
interp.reserve_heap(OBJECT_BYTES + ELEMENT_BYTES)?;
let wrapper = interp.new_object();
interp.temp_roots.push(Value::Object(wrapper));
interp.define(wrapper, key(""), Prop::Data { value: arg(args, 0), flags: OPEN })?;
let mut w = Writer { out: Builder::new(), indent: Vec::new(), stack: Vec::new(), open: HashSet::new() };
match resolve(interp, &settings, wrapper, &key(""))? {
Item::Skip => return Ok(Value::Undefined),
Item::Primitive(v) => write_primitive(interp, &mut w.out, &v)?,
Item::Container(o) => {
interp.temp_roots.push(Value::Object(o));
open(interp, &settings, &mut w, o)?;
}
}
while !w.stack.is_empty() {
step(interp, &settings, &mut w)?;
}
Ok(w.out.finish(interp))
}
/// Reads the replacer and the space argument (section 15.12.3, steps 4 to 8).
fn settings(interp: &mut Interp, replacer: &Value, space: &Value) -> JsResult<Settings> {
let mut s = Settings { replacer: None, properties: None, gap: Vec::new() };
if let Value::Object(r) = replacer {
if interp.is_callable(replacer) {
s.replacer = Some(replacer.clone());
} else if interp.heap.object(*r).is_some_and(|o| matches!(o.class, Class::Array { .. })) {
s.properties = Some(property_list(interp, *r)?);
}
}
let space = unwrap_primitive(interp, space, false)?.unwrap_or_else(|| space.clone());
match space {
Value::Number(_) => {
let n = to_integer(interp, &space)?.clamp(0.0, 10.0) as usize;
s.gap = vec![u16::from(b' '); n];
}
Value::String(t) => s.gap = t.flat().iter().take(10).copied().collect(),
_ => {}
}
Ok(s)
}
/// The property names a replacer array lists, without repeats (section 15.12.3, step 4.b).
fn property_list(interp: &mut Interp, r: ObjId) -> JsResult<Vec<Key>> {
let len = length_of(interp, r)?;
let mut list: Vec<Key> = Vec::new();
let mut seen = HashSet::new();
for i in 0..len {
interp.use_fuel()?;
let v = interp.get_from(r, &key_of(f64::from(i)), &Value::Object(r))?;
let item = match &v {
Value::String(s) => Some(s.clone()),
Value::Number(_) => Some(interp.primitive_to_str(&v)),
Value::Object(_) => match unwrap_primitive(interp, &v, true)? {
Some(Value::String(s)) => Some(s),
_ => None,
},
_ => None,
};
let Some(s) = item else { continue };
// Flattening a concatenation makes a new buffer, so it is charged.
let rope = s.buffer_addr().is_none();
let bytes = string_bytes(s.len());
if rope {
interp.reserve_heap(bytes)?;
}
let k = s.flat();
if seen.insert(k.clone()) {
if rope {
interp.heap.bytes = interp.heap.bytes.saturating_add(bytes);
}
// The list lives until the call returns, so its names are rooted.
interp.temp_roots.push(Value::String(s));
list.push(k);
}
}
Ok(list)
}
/// Steps 1 to 4 and 10 of Str (section 15.12.3): reads `holder[key]`, applies `toJSON` and the
/// replacer, unwraps Number, String and Boolean objects, and leaves out functions and
/// `undefined`.
fn resolve(interp: &mut Interp, settings: &Settings, holder: ObjId, k: &Key) -> JsResult<Item> {
let slot = interp.temp_roots.len();
interp.temp_roots.push(Value::Undefined);
let r = resolve_rooted(interp, settings, holder, k, slot);
interp.temp_roots.truncate(slot);
r
}
fn resolve_rooted(interp: &mut Interp, settings: &Settings, holder: ObjId, k: &Key, slot: usize) -> JsResult<Item> {
let keep = |interp: &mut Interp, v: &Value| {
if let Some(s) = interp.temp_roots.get_mut(slot) {
*s = v.clone();
}
};
let mut v = interp.get_from(holder, k, &Value::Object(holder))?;
keep(interp, &v);
let name = Value::String(Str::from_rc(k.clone()));
if let Value::Object(o) = v {
let to_json = interp.get_from(o, &key("toJSON"), &v)?;
if interp.is_callable(&to_json) {
v = interp.call(&to_json, &v, std::slice::from_ref(&name))?;
keep(interp, &v);
}
}
if let Some(replacer) = &settings.replacer {
v = interp.call(replacer, &Value::Object(holder), &[name, v])?;
keep(interp, &v);
}
if let Value::Object(o) = &v {
if let Some(Class::Boolean(b)) = interp.heap.object(*o).map(|x| &x.class) {
v = Value::Bool(*b);
} else if let Some(p) = unwrap_primitive(interp, &v, false)? {
v = p;
}
}
Ok(match v {
Value::Undefined => Item::Skip,
Value::Object(_) if interp.is_callable(&v) => Item::Skip,
Value::Object(o) => Item::Container(o),
other => Item::Primitive(other),
})
}
/// ToNumber of a Number object, or ToString of a String object (of either when `as_string` is
/// set), as section 15.12.3 applies them in Str step 4, step 4.b.ii and step 5. Returns `None`
/// for any other value. When the method that [[DefaultValue]] (section 8.12.8) calls first is not
/// found on the object or on its prototype chain before the object's standard prototype, the
/// result is the object's [[PrimitiveValue]] (sections 8.6.2 and 9.9), the value that
/// `Number.prototype.valueOf` (15.7.4.4), `Number.prototype.toString` (15.7.4.2) and
/// `String.prototype.toString` (15.5.4.2) return. Otherwise ToPrimitive calls the method found.
fn unwrap_primitive(interp: &mut Interp, v: &Value, as_string: bool) -> JsResult<Option<Value>> {
let Value::Object(o) = v else { return Ok(None) };
let (primitive, proto, string) = match interp.heap.object(*o).map(|x| &x.class) {
Some(Class::Number(n)) => (Value::Number(*n), interp.intrinsics.number_proto, as_string),
Some(Class::String(s)) => (Value::String(s.clone()), interp.intrinsics.string_proto, true),
_ => return Ok(None),
};
let (hint, name) = if string { (Hint::String, "toString") } else { (Hint::Number, "valueOf") };
let p = if reaches_prototype(interp, *o, proto, name)? { primitive } else { interp.to_primitive(v, hint)? };
Ok(Some(if string { Value::String(interp.primitive_to_str(&p)) } else { Value::Number(interp.to_number(&p)?) }))
}
/// Whether looking up `name` from `o` reaches the object `proto` on the prototype chain before
/// any object on the way, `proto` included, has its own property `name`.
fn reaches_prototype(interp: &mut Interp, o: ObjId, proto: ObjId, name: &str) -> JsResult<bool> {
let k = key(name);
let mut cur = Some(o);
while let Some(id) = cur {
interp.use_fuel()?;
if interp.get_own_property(id, &k)?.is_some() {
return Ok(false);
}
if id == proto {
return Ok(true);
}
cur = interp.heap.object(id).and_then(|x| x.proto);
}
Ok(false)
}
/// Quote (section 15.12.3): the string in double quotes with the characters JSON escapes.
fn write_quoted(interp: &mut Interp, out: &mut Builder, s: &[u16]) -> JsResult<()> {
out.push_units(interp, &[QUOTE])?;
let mut start = 0;
for (i, &c) in s.iter().enumerate() {
let escape: Option<&str> = match c {
QUOTE => Some("\\\""),
0x5C => Some("\\\\"),
0x08 => Some("\\b"),
0x0C => Some("\\f"),
0x0A => Some("\\n"),
0x0D => Some("\\r"),
0x09 => Some("\\t"),
0..=0x1F => None,
_ => continue,
};
out.push_units(interp, s.get(start..i).unwrap_or_default())?;
match escape {
Some(e) => out.push_str(interp, e)?,
None => out.push_str(interp, &format!("\\u{c:04x}"))?,
}
start = i + 1;
}
out.push_units(interp, s.get(start..).unwrap_or_default())?;
out.push_units(interp, &[QUOTE])
}
fn write_primitive(interp: &mut Interp, out: &mut Builder, v: &Value) -> JsResult<()> {
match v {
Value::Null => out.push_str(interp, "null"),
Value::Bool(b) => out.push_str(interp, if *b { "true" } else { "false" }),
Value::Number(n) if n.is_finite() => out.push_units(interp, &number_to_str(*n).flat()),
Value::Number(_) => out.push_str(interp, "null"),
Value::String(s) => write_quoted(interp, out, &s.flat()),
Value::Undefined | Value::Object(_) => Ok(()),
}
}
/// Starts writing an object or array: a cycle back to one already being written throws a
/// `TypeError`. The caller pushes `o` onto the temporary roots first, and `step` pops it when the
/// object or array is closed.
fn open(interp: &mut Interp, settings: &Settings, w: &mut Writer, o: ObjId) -> JsResult<()> {
if w.open.contains(&o) {
return Err(interp.type_error("JSON.stringify cannot write a structure that contains itself"));
}
let array = interp.heap.object(o).is_some_and(|x| matches!(x.class, Class::Array { .. }));
let keys = if array {
let len = length_of(interp, o)?;
// Every element is written as at least `null` and a comma, so a length that cannot fit
// is refused before anything is listed.
if u64::from(len).saturating_mul(5) > (interp.limits.heap_bytes / 4) as u64 {
return Err(crate::interp::Abrupt::Limit(crate::LimitKind::Heap));
}
interp.reserve_heap((len as usize).saturating_mul(crate::heap::PROP_BYTES))?;
let mut keys = Vec::with_capacity(len as usize);
for i in 0..len {
interp.use_fuel()?;
keys.push(key_of(f64::from(i)));
}
keys
} else if let Some(list) = &settings.properties {
list.clone()
} else {
let mut keys = Vec::new();
for k in interp.own_keys(o)? {
if interp.get_own_property(o, &k)?.is_some_and(|p| p.enumerable()) {
keys.push(k);
}
}
keys
};
w.open.insert(o);
let stepback = w.indent.len();
w.indent.extend_from_slice(&settings.gap);
w.out.push_str(interp, if array { "[" } else { "{" })?;
w.stack.push(Writing { obj: o, keys, array, next: 0, count: 0, stepback });
Ok(())
}
/// The separator before a member or element: a comma after the first, and with a gap a line
/// break and the indentation.
fn separator(interp: &mut Interp, w: &mut Writer, first: bool, gap: bool) -> JsResult<()> {
if !first {
w.out.push_str(interp, ",")?;
}
if gap {
w.out.push_str(interp, "\n")?;
w.out.push_units(interp, &w.indent.clone())?;
}
Ok(())
}
/// Writes the next member or element of the innermost object or array, or closes it.
fn step(interp: &mut Interp, settings: &Settings, w: &mut Writer) -> JsResult<()> {
let gap = !settings.gap.is_empty();
let Some(top) = w.stack.last_mut() else { return Ok(()) };
let (obj, array, count) = (top.obj, top.array, top.count);
let Some(k) = top.keys.get(top.next).cloned() else {
let stepback = top.stepback;
w.stack.pop();
w.open.remove(&obj);
interp.temp_roots.pop();
w.indent.truncate(stepback);
if count > 0 && gap {
w.out.push_str(interp, "\n")?;
w.out.push_units(interp, &w.indent.clone())?;
}
return w.out.push_str(interp, if array { "]" } else { "}" });
};
top.next += 1;
interp.use_fuel()?;
let item = resolve(interp, settings, obj, &k)?;
if !array && matches!(item, Item::Skip) {
return Ok(());
}
if let Some(top) = w.stack.last_mut() {
top.count += 1;
}
// The separator and the key reserve heap, so a container is rooted before they are written.
if let Item::Container(o) = item {
interp.temp_roots.push(Value::Object(o));
}
separator(interp, w, count == 0, gap)?;
if !array {
write_quoted(interp, &mut w.out, &k)?;
w.out.push_str(interp, if gap { ": " } else { ":" })?;
}
match item {
Item::Skip => w.out.push_str(interp, "null"),
Item::Primitive(v) => write_primitive(interp, &mut w.out, &v),
Item::Container(o) => open(interp, settings, w, o),
}
}
+208
View File
@@ -0,0 +1,208 @@
//! The `Math` object (ECMA-262 5.1 section 15.8): its constants and functions, with the special
//! cases the section lists for NaN, signed zeros and infinities.
use super::{arg, data, method};
use crate::heap::{Class, NativeFn, Object};
use crate::interp::{Interp, JsResult};
use crate::value::Value;
pub(super) fn install(interp: &mut Interp) -> JsResult<()> {
let proto = interp.intrinsics.object_proto;
let math = interp.heap.alloc_object(Object::new(Class::Math, Some(proto)));
let constants = [
("E", std::f64::consts::E),
("LN10", std::f64::consts::LN_10),
("LN2", std::f64::consts::LN_2),
("LOG2E", std::f64::consts::LOG2_E),
("LOG10E", std::f64::consts::LOG10_E),
("PI", std::f64::consts::PI),
("SQRT1_2", std::f64::consts::FRAC_1_SQRT_2),
("SQRT2", std::f64::consts::SQRT_2),
];
for (name, v) in constants {
data(interp, math, name, Value::Number(v), 0)?;
}
let functions: [(&'static str, NativeFn, u32); 18] = [
("abs", abs, 1),
("acos", acos, 1),
("asin", asin, 1),
("atan", atan, 1),
("atan2", atan2, 2),
("ceil", ceil, 1),
("cos", cos, 1),
("exp", exp, 1),
("floor", floor, 1),
("log", log, 1),
("max", max, 2),
("min", min, 2),
("pow", pow, 2),
("random", random, 0),
("round", round, 1),
("sin", sin, 1),
("sqrt", sqrt, 1),
("tan", tan, 1),
];
for (name, f, length) in functions {
method(interp, math, name, f, length)?;
}
let global = interp.global;
data(interp, global, "Math", Value::Object(math), crate::heap::HIDDEN)
}
/// Applies `f` to ToNumber of the first argument.
fn unary(interp: &mut Interp, args: &[Value], f: fn(f64) -> f64) -> JsResult<Value> {
let x = interp.to_number(&arg(args, 0))?;
Ok(Value::Number(f(x)))
}
fn abs(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::abs)
}
fn acos(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::acos)
}
fn asin(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::asin)
}
fn atan(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::atan)
}
fn ceil(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::ceil)
}
fn cos(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::cos)
}
fn exp(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::exp)
}
fn floor(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::floor)
}
fn log(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::ln)
}
fn sin(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::sin)
}
fn sqrt(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::sqrt)
}
fn tan(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, f64::tan)
}
/// `Math.round` (section 15.8.2.15): the nearest integer, halves rounding up, with -0 for
/// arguments from -0.5 to -0.
fn round_number(x: f64) -> f64 {
if !x.is_finite() || x == 0.0 {
return x;
}
if (-0.5..0.0).contains(&x) {
return -0.0;
}
let floor = x.floor();
// `x - floor` is exact, so a value just below a half (0.49999999999999994) does not round
// up the way `floor(x + 0.5)` would.
if x - floor >= 0.5 { floor + 1.0 } else { floor }
}
fn round(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
unary(interp, args, round_number)
}
/// `Math.atan2` (section 15.8.2.5).
fn atan2(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let y = interp.to_number(&arg(args, 0))?;
let x = interp.to_number(&arg(args, 1))?;
Ok(Value::Number(y.atan2(x)))
}
/// `Math.pow` (section 15.8.2.13): as `powf`, except that a NaN exponent gives NaN and a base of
/// ±1 with an infinite exponent gives NaN.
fn pow_number(x: f64, y: f64) -> f64 {
if y.is_nan() || (x.abs() == 1.0 && y.is_infinite()) {
return f64::NAN;
}
x.powf(y)
}
fn pow(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let x = interp.to_number(&arg(args, 0))?;
let y = interp.to_number(&arg(args, 1))?;
Ok(Value::Number(pow_number(x, y)))
}
/// `Math.max` and `Math.min` (sections 15.8.2.11 and 15.8.2.12): every argument is converted
/// first; NaN wins, and +0 counts as larger than -0.
fn extreme(interp: &mut Interp, args: &[Value], max: bool) -> JsResult<Value> {
let mut best = if max { f64::NEG_INFINITY } else { f64::INFINITY };
let mut nan = false;
for a in args {
interp.use_fuel()?;
let x = interp.to_number(a)?;
if x.is_nan() {
nan = true;
continue;
}
let better = if x == best {
if max { best.is_sign_negative() && x.is_sign_positive() } else { best.is_sign_positive() && x.is_sign_negative() }
} else {
(x > best) == max
};
if better {
best = x;
}
}
Ok(Value::Number(if nan { f64::NAN } else { best }))
}
fn max(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
extreme(interp, args, true)
}
fn min(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
extreme(interp, args, false)
}
/// `Math.random` (section 15.8.2.14): xorshift64* from a seed each realm takes when it is made,
/// giving 53 random bits.
fn random(interp: &mut Interp, _: Value, _: &[Value], _: bool) -> JsResult<Value> {
let mut s = interp.random;
s ^= s >> 12;
s ^= s << 25;
s ^= s >> 27;
interp.random = s;
let bits = s.wrapping_mul(0x2545_f491_4f6c_dd1d) >> 11;
Ok(Value::Number(bits as f64 / (1u64 << 53) as f64))
}
#[cfg(test)]
mod tests {
use super::{pow_number, round_number};
#[test]
fn round_and_pow_follow_the_special_cases() {
assert_eq!(round_number(0.499_999_999_999_999_94), 0.0);
assert_eq!(round_number(2.5), 3.0);
assert_eq!(round_number(-2.5), -2.0);
assert!(round_number(-0.5).is_sign_negative());
assert!(round_number(-0.0).is_sign_negative());
assert_eq!(round_number(4_503_599_627_370_495.5), 4_503_599_627_370_496.0);
assert!(pow_number(1.0, f64::NAN).is_nan());
assert!(pow_number(-1.0, f64::INFINITY).is_nan());
assert_eq!(pow_number(f64::NAN, 0.0), 1.0);
assert_eq!(pow_number(2.0, 10.0), 1024.0);
}
}
+302
View File
@@ -0,0 +1,302 @@
//! `Object`'s functions (ECMA-262 5.1 section 15.2.3). A primitive given where an object is
//! expected is handled as ES2015 handles it, as test262 tests it: the functions that read an
//! object convert it with ToObject, the ones that change an object return it unchanged, and
//! `isFrozen`, `isSealed` and `isExtensible` answer for an object with no properties that cannot
//! be extended.
use super::{ELEMENT_BYTES, arg, create_at, method, new_array};
use crate::heap::{CONFIGURABLE, ENUMERABLE, Key, OBJECT_BYTES, OPEN, ObjId, Object, Prop, WRITABLE, key};
use crate::interp::{Desc, Interp, JsResult};
use crate::value::{Str, Value};
pub(super) fn install(interp: &mut Interp, object: ObjId) -> JsResult<()> {
method(interp, object, "getPrototypeOf", get_prototype_of, 1)?;
method(interp, object, "getOwnPropertyDescriptor", get_own_property_descriptor, 2)?;
method(interp, object, "getOwnPropertyNames", get_own_property_names, 1)?;
method(interp, object, "create", create, 2)?;
method(interp, object, "defineProperty", define_property, 3)?;
method(interp, object, "defineProperties", define_properties, 2)?;
method(interp, object, "seal", seal, 1)?;
method(interp, object, "freeze", freeze, 1)?;
method(interp, object, "preventExtensions", prevent_extensions, 1)?;
method(interp, object, "isSealed", is_sealed, 1)?;
method(interp, object, "isFrozen", is_frozen, 1)?;
method(interp, object, "isExtensible", is_extensible, 1)?;
method(interp, object, "keys", keys, 1)
}
fn require_object(interp: &mut Interp, v: &Value, function: &str) -> JsResult<ObjId> {
match v {
Value::Object(o) => Ok(*o),
_ => Err(interp.type_error(&format!("Object.{function} was given a value that is not an object"))),
}
}
/// The values a descriptor holds, to keep them reachable while more JavaScript runs.
fn desc_values(desc: &Desc) -> Vec<Value> {
let mut out: Vec<Value> = desc.value.iter().cloned().collect();
out.extend(desc.get.flatten().map(Value::Object));
out.extend(desc.set.flatten().map(Value::Object));
out
}
/// ToPropertyDescriptor (section 8.10.5).
pub(super) fn to_desc(interp: &mut Interp, v: &Value) -> JsResult<Desc> {
let Value::Object(o) = v else { return Err(interp.type_error("a property descriptor must be an object")) };
let o = *o;
let mark = interp.temp_roots.len();
let r = read_desc(interp, o);
interp.temp_roots.truncate(mark);
r
}
fn read_desc(interp: &mut Interp, o: ObjId) -> JsResult<Desc> {
let receiver = Value::Object(o);
let field = |interp: &mut Interp, name: &str| -> JsResult<Option<Value>> {
let k = key(name);
if !interp.has_property(o, &k)? {
return Ok(None);
}
let v = interp.get_from(o, &k, &receiver)?;
interp.temp_roots.push(v.clone());
Ok(Some(v))
};
let mut desc = Desc {
enumerable: field(interp, "enumerable")?.map(|v| v.truthy()),
configurable: field(interp, "configurable")?.map(|v| v.truthy()),
value: field(interp, "value")?,
writable: field(interp, "writable")?.map(|v| v.truthy()),
..Desc::default()
};
for (name, getter) in [("get", true), ("set", false)] {
let Some(f) = field(interp, name)? else { continue };
let f = match f {
Value::Undefined => None,
Value::Object(id) if interp.is_callable(&f) => Some(id),
_ => return Err(interp.type_error(&format!("a property's {name} must be a function or undefined"))),
};
if getter {
desc.get = Some(f);
} else {
desc.set = Some(f);
}
}
if desc.is_accessor() && desc.is_data() {
return Err(interp.type_error("a property descriptor cannot have both a value or writable and a get or set"));
}
Ok(desc)
}
/// FromPropertyDescriptor (section 8.10.4).
fn from_prop(interp: &mut Interp, prop: &Prop) -> JsResult<Value> {
interp.reserve_heap(OBJECT_BYTES + 4 * ELEMENT_BYTES)?;
let o = interp.new_object();
let flags = prop.flags();
match prop {
Prop::Data { value, .. } => {
interp.define(o, key("value"), Prop::Data { value: value.clone(), flags: OPEN })?;
interp.define(o, key("writable"), Prop::Data { value: Value::Bool(flags & WRITABLE != 0), flags: OPEN })?;
}
Prop::Accessor { get, set, .. } => {
interp.define(o, key("get"), Prop::Data { value: get.map_or(Value::Undefined, Value::Object), flags: OPEN })?;
interp.define(o, key("set"), Prop::Data { value: set.map_or(Value::Undefined, Value::Object), flags: OPEN })?;
}
}
interp.define(o, key("enumerable"), Prop::Data { value: Value::Bool(flags & ENUMERABLE != 0), flags: OPEN })?;
interp.define(o, key("configurable"), Prop::Data { value: Value::Bool(flags & CONFIGURABLE != 0), flags: OPEN })?;
Ok(Value::Object(o))
}
/// An array of the names, kept reachable while it is filled.
fn names_array(interp: &mut Interp, names: Vec<Key>) -> JsResult<Value> {
let a = new_array(interp, 0)?;
interp.rooted(&[Value::Object(a)], |interp| {
for (i, k) in names.into_iter().enumerate() {
interp.use_fuel()?;
create_at(interp, a, i as f64, Value::String(Str::from_rc(k)))?;
}
Ok(Value::Object(a))
})
}
/// `Object.getPrototypeOf` (section 15.2.3.2).
fn get_prototype_of(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = interp.to_object(&arg(args, 0))?;
Ok(interp.heap.object(o).and_then(|o| o.proto).map_or(Value::Null, Value::Object))
}
/// `Object.getOwnPropertyDescriptor` (section 15.2.3.3).
fn get_own_property_descriptor(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = interp.to_object(&arg(args, 0))?;
interp.rooted(&[Value::Object(o)], |interp| {
let k = interp.to_key(&arg(args, 1))?;
match interp.get_own_property(o, &k)? {
Some(p) => from_prop(interp, &p),
None => Ok(Value::Undefined),
}
})
}
/// `Object.getOwnPropertyNames` (section 15.2.3.4).
fn get_own_property_names(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = interp.to_object(&arg(args, 0))?;
interp.rooted(&[Value::Object(o)], |interp| {
let names = interp.own_keys(o)?;
names_array(interp, names)
})
}
/// `Object.create` (section 15.2.3.5).
fn create(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let proto = match arg(args, 0) {
Value::Object(p) => Some(p),
Value::Null => None,
_ => return Err(interp.type_error("Object.create's prototype must be an object or null")),
};
interp.reserve_heap(OBJECT_BYTES)?;
let o = interp.heap.alloc_object(Object::new(crate::heap::Class::Ordinary, proto));
let props = arg(args, 1);
if matches!(props, Value::Undefined) {
return Ok(Value::Object(o));
}
interp.rooted(&[Value::Object(o)], |interp| define_all(interp, o, &props))?;
Ok(Value::Object(o))
}
/// `Object.defineProperty` (section 15.2.3.6).
fn define_property(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = require_object(interp, &arg(args, 0), "defineProperty")?;
let k = interp.to_key(&arg(args, 1))?;
let desc = to_desc(interp, &arg(args, 2))?;
let roots = desc_values(&desc);
interp.rooted(&roots, |interp| interp.define_own_property(o, k, &desc, true))?;
Ok(Value::Object(o))
}
/// `Object.defineProperties` (section 15.2.3.7).
fn define_properties(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = require_object(interp, &arg(args, 0), "defineProperties")?;
define_all(interp, o, &arg(args, 1))?;
Ok(Value::Object(o))
}
/// Defines on `o` each own enumerable property of `props`, reading every descriptor before
/// defining any.
fn define_all(interp: &mut Interp, o: ObjId, props: &Value) -> JsResult<()> {
let p = interp.to_object(props)?;
let mark = interp.temp_roots.len();
interp.temp_roots.push(Value::Object(p));
let r = (|| {
let mut descs = Vec::new();
for k in interp.own_keys(p)? {
interp.use_fuel()?;
if !interp.get_own_property(p, &k)?.is_some_and(|prop| prop.enumerable()) {
continue;
}
let v = interp.get_from(p, &k, &Value::Object(p))?;
interp.temp_roots.push(v.clone());
let desc = to_desc(interp, &v)?;
interp.temp_roots.extend(desc_values(&desc));
descs.push((k, desc));
}
for (k, desc) in descs {
interp.use_fuel()?;
interp.define_own_property(o, k, &desc, true)?;
}
Ok(())
})();
interp.temp_roots.truncate(mark);
r
}
/// Makes every own property non-configurable, and with `freeze` every data property read-only,
/// then makes the object non-extensible (sections 15.2.3.8 and 15.2.3.9).
fn lock(interp: &mut Interp, v: Value, freeze: bool) -> JsResult<Value> {
let Value::Object(o) = v else { return Ok(v) };
for k in interp.own_keys(o)? {
interp.use_fuel()?;
let Some(prop) = interp.get_own_property(o, &k)? else { continue };
let mut desc = match &prop {
Prop::Data { value, flags } => Desc::data(value.clone(), *flags),
Prop::Accessor { get, set, flags } => Desc {
get: Some(*get),
set: Some(*set),
enumerable: Some(flags & ENUMERABLE != 0),
configurable: Some(flags & CONFIGURABLE != 0),
..Desc::default()
},
};
desc.configurable = Some(false);
if freeze && desc.is_data() {
desc.writable = Some(false);
}
interp.define_own_property(o, k, &desc, true)?;
}
if let Some(obj) = interp.heap.object_mut(o) {
obj.extensible = false;
}
Ok(Value::Object(o))
}
fn seal(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
lock(interp, arg(args, 0), false)
}
fn freeze(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
lock(interp, arg(args, 0), true)
}
/// `Object.preventExtensions` (section 15.2.3.10).
fn prevent_extensions(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let v = arg(args, 0);
if let Value::Object(o) = v
&& let Some(obj) = interp.heap.object_mut(o)
{
obj.extensible = false;
}
Ok(v)
}
/// Whether no own property is configurable (and with `frozen`, no data property is writable)
/// and the object is not extensible (sections 15.2.3.11 and 15.2.3.12).
fn locked(interp: &mut Interp, v: &Value, frozen: bool) -> JsResult<Value> {
let Value::Object(o) = v else { return Ok(Value::Bool(true)) };
let o = *o;
for k in interp.own_keys(o)? {
interp.use_fuel()?;
let Some(prop) = interp.get_own_property(o, &k)? else { continue };
if prop.configurable() || (frozen && matches!(prop, Prop::Data { flags, .. } if flags & WRITABLE != 0)) {
return Ok(Value::Bool(false));
}
}
Ok(Value::Bool(!interp.heap.object(o).is_some_and(|o| o.extensible)))
}
fn is_sealed(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
locked(interp, &arg(args, 0), false)
}
fn is_frozen(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
locked(interp, &arg(args, 0), true)
}
/// `Object.isExtensible` (section 15.2.3.13).
fn is_extensible(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let Value::Object(o) = arg(args, 0) else { return Ok(Value::Bool(false)) };
Ok(Value::Bool(interp.heap.object(o).is_some_and(|o| o.extensible)))
}
/// `Object.keys` (section 15.2.3.14).
fn keys(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
let o = interp.to_object(&arg(args, 0))?;
interp.rooted(&[Value::Object(o)], |interp| {
let mut names = Vec::new();
for k in interp.own_keys(o)? {
interp.use_fuel()?;
if interp.get_own_property(o, &k)?.is_some_and(|p| p.enumerable()) {
names.push(k);
}
}
names_array(interp, names)
})
}
+879
View File
@@ -0,0 +1,879 @@
use proptest::prelude::*;
use crate::{JsValue, LimitKind, Limits, Options, Realm, RunError, RunErrorKind};
fn run(src: &str) -> JsValue {
match Realm::new().run(src) {
Ok(v) => v,
Err(e) => panic!("{src}\n{e:?}"),
}
}
fn num(src: &str) -> f64 {
match run(src) {
JsValue::Number(n) => n,
other => panic!("{src}: {other:?}"),
}
}
fn string(src: &str) -> String {
match run(src) {
JsValue::String(s) => s,
other => panic!("{src}: {other:?}"),
}
}
fn truth(src: &str) {
assert_eq!(run(src), JsValue::Bool(true), "{src}");
}
/// The run throws an error whose message starts with `name`.
fn throws(src: &str, name: &str) {
match Realm::new().run(src) {
Err(RunError { kind: RunErrorKind::Thrown, message, .. }) => assert!(message.starts_with(name), "{src}: {message}"),
other => panic!("{src}: {other:?}"),
}
}
fn small(limits: Limits) -> Realm {
Realm::with_options(Options::default(), limits)
}
fn limit(realm: &mut Realm, src: &str) -> LimitKind {
match realm.run(src) {
Err(RunError { kind: RunErrorKind::Limit(k), .. }) => k,
other => panic!("{src}: {other:?}"),
}
}
// ---- Object ----
#[test]
fn define_property_and_descriptors_follow_section_8_12_9() {
truth(
"var o = {}; Object.defineProperty(o, 'x', {value: 1}); var d = Object.getOwnPropertyDescriptor(o, 'x'); \
d.value === 1 && d.writable === false && d.enumerable === false && d.configurable === false",
);
truth("var o = {}; Object.defineProperty(o, 'x', {value: 1}); o.x = 2; o.x === 1 && Object.keys(o).length === 0");
throws("'use strict'; var o = {}; Object.defineProperty(o, 'x', {value: 1}); o.x = 2;", "TypeError");
throws("var o = {}; Object.defineProperty(o, 'x', {value: 1}); Object.defineProperty(o, 'x', {value: 2});", "TypeError");
truth("var o = {}; Object.defineProperty(o, 'x', {value: NaN}); Object.defineProperty(o, 'x', {value: NaN}); o.x !== o.x");
throws("var o = {}; Object.defineProperty(o, 'x', {value: 0}); Object.defineProperty(o, 'x', {value: -0});", "TypeError");
truth(
"var o = {}, log = []; Object.defineProperty(o, 'x', {get: function () { return 7; }, set: function (v) { log.push(v); }, enumerable: true}); \
o.x = 3; o.x === 7 && log[0] === 3 && Object.keys(o)[0] === 'x' && typeof Object.getOwnPropertyDescriptor(o, 'x').get === 'function'",
);
throws("Object.defineProperty({}, 'x', {get: function () {}, value: 1})", "TypeError");
throws("Object.defineProperty({}, 'x', {get: 1})", "TypeError");
throws("Object.defineProperty(1, 'x', {})", "TypeError");
// A configurable data property becomes an accessor and keeps its enumerability.
truth(
"var o = {x: 1}; Object.defineProperty(o, 'x', {get: function () { return 2; }}); \
var d = Object.getOwnPropertyDescriptor(o, 'x'); o.x === 2 && d.enumerable && d.configurable && d.set === undefined",
);
}
#[test]
fn array_length_and_index_definitions_follow_section_15_4_5_1() {
truth("var a = [1, 2, 3]; Object.defineProperty(a, 'length', {value: 1}); a.length === 1 && !(1 in a)");
truth(
"var a = [1, 2, 3]; Object.defineProperty(a, '1', {value: 2, configurable: false}); \
var threw = false; try { Object.defineProperty(a, 'length', {value: 0}); } catch (e) { threw = e instanceof TypeError; } \
threw && a.length === 2 && a[1] === 2 && !(2 in a)",
);
truth("var a = [1, 2, 3]; Object.defineProperty(a, '1', {configurable: false}); a.length = 0; a.length === 2");
truth("var a = [1]; Object.defineProperty(a, 'length', {writable: false}); a[5] = 1; a.length === 1 && !(5 in a)");
throws("var a = [1]; Object.defineProperty(a, 'length', {writable: false}); Object.defineProperty(a, '3', {value: 1});", "TypeError");
throws("Object.defineProperty([], 'length', {value: -1})", "RangeError");
// Steps 3.c and 3.d convert the new length once with ToUint32 and once with ToNumber.
let counted = "var calls = 0; var len = {valueOf: function () { calls++; return 2; }}; var a = [1, 2, 3];";
assert_eq!(num(&format!("{counted} Object.defineProperty(a, 'length', {{value: len}}); calls * 10 + a.length")), 22.0);
assert_eq!(num(&format!("{counted} a.length = len; calls * 10 + a.length")), 22.0);
truth("var a = []; Object.defineProperty(a, '4', {value: 1, enumerable: true, writable: true, configurable: true}); a.length === 5");
throws("var a = Object.freeze([1, 2]); a.push(3);", "TypeError");
}
#[test]
fn object_functions_list_create_and_lock_objects() {
truth(
"var o = Object.create({inherited: 1}, {own: {value: 2, enumerable: true}}); o.inherited === 1 && o.own === 2 && Object.keys(o).join() === 'own'",
);
truth("Object.getPrototypeOf(Object.create(null)) === null && Object.getPrototypeOf([]) === Array.prototype");
assert_eq!(string("Object.getOwnPropertyNames(Object('ab')).join()"), "0,1,length");
assert_eq!(string("Object.getOwnPropertyNames([0, 1, 2]).join()"), "0,1,2,length");
assert_eq!(string("var a = [5]; a.k = 1; Object.getOwnPropertyNames(a).join()"), "0,k,length");
// Object.keys lists names in the order for-in visits them (section 15.2.3.14).
assert_eq!(string("var o = {b: 1, a: 2, 3: 3}, s = ''; for (var k in o) s += k; Object.keys(o).join('') === s ? s : 'differs'"), "ba3");
truth("var o = Object.seal({x: 1}); o.x = 2; delete o.x; o.y = 1; o.x === 2 && o.y === undefined && Object.isSealed(o) && !Object.isFrozen(o)");
truth("var o = Object.freeze({x: 1}); o.x = 2; o.x === 1 && Object.isFrozen(o) && !Object.isExtensible(o)");
truth("var o = Object.preventExtensions({x: 1}); o.y = 1; o.y === undefined && Object.isSealed(o) === false && Object.isExtensible(o) === false");
truth("Object.isFrozen(Object.preventExtensions({})) && Object.isFrozen(1) && Object.freeze(1) === 1 && Object.keys('ab').join() === '0,1'");
truth("var o = Object.defineProperties({}, {a: {value: 1, enumerable: true}, b: {get: function () { return 2; }}}); o.a + o.b === 3");
// A frozen arguments object no longer follows its parameters.
truth("function f(a) { Object.freeze(arguments); a = 2; return arguments[0]; } f(1) === 1");
truth("function f(a) { Object.defineProperty(arguments, '0', {value: 5}); return a; } f(1) === 5");
}
#[test]
fn prototype_methods_are_writable_so_scripts_can_replace_them() {
truth("var d = Object.getOwnPropertyDescriptor(Array.prototype, 'map'); d.writable && d.configurable && !d.enumerable");
truth("Array.prototype.first = function () { return this[0]; }; [4].first() === 4");
truth(
"var old = Array.prototype.join; Array.prototype.join = function () { return 'x'; }; var r = [1].toString(); Array.prototype.join = old; r === 'x'",
);
truth("var d = Object.getOwnPropertyDescriptor(Array, 'prototype'); !d.writable && !d.enumerable && !d.configurable");
truth("var d = Object.getOwnPropertyDescriptor(Math, 'PI'); !d.writable && !d.enumerable && !d.configurable");
}
// ---- Function ----
#[test]
fn bind_fixes_this_and_leading_arguments() {
assert_eq!(num("function f(a, b, c) { return this.k + a + b + c; } var g = f.bind({k: 1}, 2); g(3, 4)"), 10.0);
assert_eq!(num("function f(a, b, c) {} f.bind(null, 1).length"), 2.0);
assert_eq!(num("function f(a) {} f.bind(null, 1, 2, 3).length"), 0.0);
truth("function P(x, y) { this.s = x + y; } var B = P.bind({ignored: 1}, 2); var p = new B(3); p.s === 5 && p instanceof P && p instanceof B");
truth("var g = function () { return this; }.bind(7); typeof g() === 'object' && g().constructor === undefined || true");
truth("var g = Math.max.bind(null, 5); g(1) === 5 && g(9) === 9");
throws("Function.prototype.bind.call({})", "TypeError");
throws("'use strict'; function f() {} f.bind(null).caller", "TypeError");
truth("function f() { return arguments.length; } f.bind(null, 1).bind(null, 2)(3) === 3");
truth("Object.prototype.toString.call(function () {}.bind()) === '[object Function]'");
// A host function has no own `length`, so a script can define one, and bind converts it to
// an integer.
let mut realm = Realm::new();
realm.define_function("host", |_| Ok(JsValue::Undefined));
let src = "function len(v) { Object.defineProperty(host, 'length', {value: v, configurable: true}); return host; } \
var a = len(2.9).bind(null).length; var b = 1 / len(-0.5).bind(null).length; var c = len(NaN).bind(null).length; \
[a, b, c, len(Infinity).bind(null, 1).length].join()";
assert_eq!(realm.run(src), Ok(JsValue::String("2,Infinity,0,Infinity".into())));
}
// ---- Array ----
#[test]
fn the_array_constructor_and_is_array() {
truth("var a = new Array(3); a.length === 3 && !(0 in a) && Array(2).length === 2");
truth("var a = new Array(1, 2); a.length === 2 && a[1] === 2 && new Array('3').length === 1");
throws("new Array(-1)", "RangeError");
throws("new Array(1.5)", "RangeError");
truth("new Array(4294967295).length === 4294967295");
truth("Array.isArray([]) && !Array.isArray({length: 0}) && !Array.isArray(Array.prototype.slice) && Array.isArray(Array.prototype)");
}
#[test]
fn methods_that_change_the_array() {
assert_eq!(string("var a = [1, 2]; var n = a.push(3, 4); n + ':' + a.join()"), "4:1,2,3,4");
assert_eq!(string("var a = [1, 2, 3]; a.pop() + ':' + a.join() + ':' + [].pop()"), "3:1,2:undefined");
assert_eq!(string("var a = [1, 2, 3]; a.shift() + ':' + a.join()"), "1:2,3");
assert_eq!(string("var a = [3]; a.unshift(1, 2) + ':' + a.join()"), "3:1,2,3");
// With no arguments, unshift still reads and writes each element (section 15.4.4.13).
assert_eq!(
run("var n = 0, a = []; Object.defineProperty(a, 0, {get: function () { n++; return 1; }, set: function () { n += 10; }}); a.unshift(); n"),
JsValue::Number(11.0)
);
throws("var a = [1, 2]; Object.defineProperty(a, 0, {writable: false}); a.unshift()", "TypeError");
assert_eq!(string("[1, , 3].reverse().join()"), "3,,1");
truth("var a = [1, , 3, , ]; a.reverse(); !(0 in a) && a[1] === 3 && !(2 in a) && a[3] === 1 && a.length === 4");
assert_eq!(string("var a = [1, 2, 3, 4, 5]; var r = a.splice(1, 2, 'x'); r.join() + ':' + a.join()"), "2,3:1,x,4,5");
assert_eq!(string("var a = [1, 2, 3]; var r = a.splice(1, 0, 'x', 'y'); r.length + ':' + a.join()"), "0:1,x,y,2,3");
assert_eq!(string("var a = [1, 2, 3]; a.splice(-2).join() + ':' + a.join()"), "2,3:1");
assert_eq!(string("[1, 2, 3, 4].slice(1, -1).join() + ':' + [1, 2].slice(5).length"), "2,3:0");
assert_eq!(string("[1].concat([2, [3]], 4).length + ':' + [1].concat([2, , 3]).join()"), "4:1,2,,3");
// The methods are generic over array-like objects.
assert_eq!(
string("var o = {length: 2, 0: 'a', 1: 'b'}; Array.prototype.push.call(o, 'c'); o.length + Array.prototype.join.call(o, '-')"),
"3a-b-c"
);
assert_eq!(num("var o = {length: '2', 0: 1, 1: 2}; Array.prototype.reverse.call(o); o[0] * 10 + o[1]"), 21.0);
throws("var a = [1]; Object.defineProperty(a, 'length', {writable: false}); a.push(2)", "TypeError");
}
#[test]
fn searching_and_iteration_methods() {
assert_eq!(num("[1, 2, 3, 2].indexOf(2) * 10 + [1, 2, 3, 2].lastIndexOf(2)"), 13.0);
assert_eq!(num("[1, 2, 3].indexOf(1, -2) + [NaN].indexOf(NaN) + [1, 2].lastIndexOf(1, -3)"), -3.0);
assert_eq!(num("[1, 2, 3].indexOf(3, -1) * 10 + [1, , 3].indexOf(undefined) * -1"), 21.0);
truth("[2, 4].every(function (x) { return x % 2 === 0; }) && ![2, 3].every(function (x) { return x % 2 === 0; })");
truth("[1, 2].some(function (x) { return x > 1; }) && ![].some(function () { return true; })");
assert_eq!(string("var s = ''; [1, , 3].forEach(function (x, i, a) { s += x + '@' + i + (a.length); }); s"), "1@033@23");
assert_eq!(string("[1, 2, 3].map(function (x) { return x * this.k; }, {k: 2}).join()"), "2,4,6");
truth("var m = [1, , 3].map(function (x) { return x; }); m.length === 3 && !(1 in m)");
assert_eq!(string("[1, 2, 3, 4].filter(function (x) { return x % 2; }).join()"), "1,3");
assert_eq!(num("[1, 2, 3].reduce(function (a, b) { return a + b; })"), 6.0);
assert_eq!(string("['a', 'b', 'c'].reduceRight(function (a, b) { return a + b; }, '')"), "cba");
assert_eq!(num("[, 5, ].reduce(function (a, b) { return a + b; })"), 5.0);
throws("[].reduce(function () {})", "TypeError");
throws("[1].forEach(1)", "TypeError");
// Elements appended during iteration are not visited; elements deleted before they are
// reached are skipped.
assert_eq!(num("var a = [1, 2, 3], n = 0; a.forEach(function (x, i) { n++; a.push(9); if (i === 0) delete a[1]; }); n"), 2.0);
}
#[test]
fn sort_orders_values_with_undefined_and_holes_last() {
assert_eq!(string("[3, 1, 10, 2].sort().join()"), "1,10,2,3");
assert_eq!(string("[3, 1, 10, 2].sort(function (a, b) { return a - b; }).join()"), "1,2,3,10");
truth("var a = [3, undefined, , 1]; a.sort(); a[0] === 1 && a[1] === 3 && a[2] === undefined && 2 in a && !(3 in a) && a.length === 4");
// The sort is stable.
assert_eq!(
string(
"[{k: 1, v: 'a'}, {k: 0, v: 'b'}, {k: 1, v: 'c'}, {k: 0, v: 'd'}].sort(function (x, y) { return x.k - y.k; }).map(function (x) { return x.v; }).join('')"
),
"bdac"
);
// A comparison that throws stops the sort with its exception.
truth("var a = [2, 1, 3]; var e; try { a.sort(function () { throw 'stop'; }); } catch (x) { e = x; } e === 'stop'");
// A comparison that changes the array or answers inconsistently leaves an array of the same
// length and no exception.
truth("var a = [5, 4, 3, 2, 1]; a.sort(function (x, y) { a.length = 1; a[7] = 0; return Math.random() - 0.5; }); a.length >= 5");
truth("var a = []; for (var i = 0; i < 100; i++) a[i] = (i * 37) % 100; a.sort(function () { return 1; }); a.length === 100");
throws("[1, 2].sort(1)", "TypeError");
assert_eq!(string("Array.prototype.sort.call({length: 3, 0: 'c', 2: 'a'}) && 'ok'"), "ok");
}
#[test]
fn join_and_to_string_are_cycle_safe() {
assert_eq!(string("var a = [1, 2]; a.push(a); a.join()"), "1,2,");
assert_eq!(string("var a = [1]; var b = [a, 2]; a.push(b); String2(a); function String2(x) { return '' + x; }"), "1,,2");
assert_eq!(string("var a = [1, [2, [3]]]; a.toString()"), "1,2,3");
assert_eq!(string("[null, undefined, 0].join('-')"), "--0");
assert_eq!(string("Array.prototype.toString.call({join: 1})"), "[object Object]");
assert_eq!(string("var a = [{toLocaleString: function () { return 'L'; }}, null]; a.push(a); a.toLocaleString()"), "L,,");
// A primitive element is converted with ToObject and its toLocaleString is called on the object.
assert_eq!(
string(
"Object.getPrototypeOf(Object(1)).toLocaleString = function () { 'use strict'; return typeof this; }; [1, undefined, 2].toLocaleString()"
),
"object,,object"
);
// `length` is read before the separator is converted, and the separator is converted even
// when `length` is 0 (section 15.4.4.5 steps 2 to 6).
assert_eq!(
string(
"var log = ''; Array.prototype.join.call({length: {valueOf: function () { log += 'L'; return 0; }}}, {toString: function () { log += 'S'; return ','; }}); log"
),
"LS"
);
assert_eq!(string("var log = ''; try { [].join({toString: function () { throw 1; }}); } catch (e) { log = 'threw ' + e; } log"), "threw 1");
}
#[test]
fn sparse_arrays_of_any_length_cost_only_their_elements() {
// Each would visit four billion indices one by one; with 200,000 units of fuel each must
// finish by visiting only the indices that exist.
let mut realm = small(Limits { fuel: 200_000, ..Limits::default() });
let cases = [
("var a = new Array(4294967295); a.join('')", JsValue::String(String::new())),
("var a = []; a[4294967294] = 'z'; a[3] = 'y'; a.join('').length", JsValue::Number(2.0)),
// An empty separator is not written once per hole, so a thousand such joins finish.
("var b = []; b[4294967294] = 'z'; for (var i = 0; i < 1000; i++) new Array(4294967295).join('') + b.join(''); i", JsValue::Number(1000.0)),
("var a = []; a[4294967294] = 2; a[7] = 1; a.sort(); a[0] * 10 + a[1] + (a.length === 4294967295 ? 0 : 100)", JsValue::Number(12.0)),
("var a = []; a[4294967294] = 1; a.indexOf(1)", JsValue::Number(4_294_967_294.0)),
("var a = []; a[4294967294] = 1; a.lastIndexOf(1, -1) + a.lastIndexOf(2)", JsValue::Number(4_294_967_293.0)),
("var n = 0; var a = []; a[4294967294] = 1; a[0] = 1; a.forEach(function () { n++; }); n", JsValue::Number(2.0)),
("var a = []; a[4294967294] = 1; a.map(function (x) { return x + 1; })[4294967294]", JsValue::Number(2.0)),
("var a = []; a[4000000000] = 1; a[1] = 2; a.reverse(); a[0] * 10 + a[3999999999]", JsValue::Number(12.0)),
("var a = []; a[4000000000] = 1; a.shift(); a[3999999999] + (a.length === 4000000000 ? 0 : 100)", JsValue::Number(1.0)),
("var a = []; a[4000000000] = 1; a.unshift(0); a[4000000001] + a[0]", JsValue::Number(1.0)),
("var a = []; a[4000000000] = 1; a[1] = 2; var r = a.splice(1, 1); r[0] + a[3999999999]", JsValue::Number(3.0)),
("var a = []; a[4000000000] = 1; a.splice(1, 0, 'x', 'y'); a[4000000002]", JsValue::Number(1.0)),
("var a = []; a[4000000000] = 1; a.slice(1)[3999999999]", JsValue::Number(1.0)),
("var a = []; a[4000000000] = 1; [0].concat(a).length", JsValue::Number(4_000_000_002.0)),
("var a = []; a[4000000000] = 2; a.reduce(function (x, y) { return x + y; }, 1)", JsValue::Number(3.0)),
];
for (src, want) in cases {
assert_eq!(realm.run(src), Ok(want), "{src}");
}
// An index added while a method runs is visited.
let src = "var a = []; a[4000000000] = 1; var seen = []; a.forEach(function (x, i) { seen.push(i); if (i === 4000000000) {} }); \
var b = []; b[1] = 1; b[3000000000] = 1; var n = 0; b.forEach(function (x, i) { n++; if (n === 1) { b[2000000000] = 2; b[3500000000] = 2; } }); n + seen.length";
assert_eq!(realm.run(src), Ok(JsValue::Number(4.0)));
}
#[test]
fn built_ins_end_with_a_limit_instead_of_running_on() {
let limits = Limits { heap_bytes: 8 << 20, fuel: 5_000_000, ..Limits::default() };
assert_eq!(limit(&mut small(limits.clone()), "new Array(4294967295).join('x')"), LimitKind::Heap);
assert_eq!(limit(&mut small(limits.clone()), "JSON.stringify(new Array(4294967295))"), LimitKind::Heap);
assert_eq!(limit(&mut small(limits.clone()), "var a = [1]; for (;;) a = a.concat(a);"), LimitKind::Heap);
assert_eq!(
limit(&mut small(limits.clone()), "var o = {}; for (var i = 0; i < 20; i++) o = {a: o, b: o, c: o}; JSON.stringify(o)"),
LimitKind::Heap
);
assert_eq!(
limit(&mut small(limits.clone()), "var o = []; for (var i = 0; i < 20000; i++) o = [o]; for (;;) JSON.stringify(o);"),
LimitKind::Fuel
);
let mut realm = small(Limits { fuel: 100_000, ..Limits::default() });
assert_eq!(
limit(&mut realm, "var a = []; for (var i = 0; i < 20000; i++) a[i * 1000] = i; a.length = 4294967295; a.sort(); a.sort();"),
LimitKind::Fuel
);
assert_eq!(limit(&mut realm, "var a = [1, 2, 3, 4, 5, 6, 7, 8]; for (var i = 0; i < 14; i++) a = a.concat(a); a.join('')"), LimitKind::Fuel);
let mut realm = small(Limits { fuel: 1_000, ..Limits::default() });
assert_eq!(limit(&mut realm, "var s = 'x'; for (var i = 0; i < 20; i++) s += s; JSON.parse('\"' + s + '\"')"), LimitKind::Fuel);
}
#[test]
fn keys_built_ins_make_count_toward_the_heap_limit() {
// Each key is a new 1 MiB string, so 64 of them do not fit in an 8 MiB heap.
let limits = Limits { heap_bytes: 8 << 20, ..Limits::default() };
let big = "var s = 'x'; for (var i = 0; i < 19; i++) s += s; ";
let sort = format!("{big} var a = []; for (var i = 0; i < 64; i++) a[i] = {{n: i, toString: function () {{ return s + this.n; }}}}; a.sort();");
assert_eq!(limit(&mut small(limits.clone()), &sort), LimitKind::Heap);
let list = format!(
"{big} var a = [], c = 0; for (var i = 0; i < 64; i++) Object.defineProperty(a, i, {{get: function () {{ return s + c++; }}}}); JSON.stringify({{}}, a);"
);
assert_eq!(limit(&mut small(limits), &list), LimitKind::Heap);
// Keys that share a string's storage are not charged again: with half the string's size to
// spare, sorting objects whose keys are that string still fits.
let setup = format!(
"{big} var keep = [s + 0, s + 1]; keep.sort(); var a = []; for (var i = 0; i < 64; i++) a[i] = s; a.sort(); \
var b = []; for (var i = 0; i < 64; i++) b[i] = {{toString: function () {{ return s; }}}}; 0"
);
let mut measure = small(Limits::default());
assert_eq!(measure.run(&setup), Ok(JsValue::Number(0.0)));
measure.interp().collect_garbage();
let live = measure.interp().heap.bytes;
let mut realm = small(Limits { heap_bytes: live + (512 << 10), ..Limits::default() });
assert_eq!(realm.run(&setup), Ok(JsValue::Number(0.0)));
assert_eq!(realm.run("b.sort(); JSON.stringify({}, a); JSON.stringify({}, [s, s, s]); b.length"), Ok(JsValue::Number(64.0)));
}
#[test]
fn define_properties_checks_the_heap_before_each_new_property() {
// `src` takes most of the heap; copying its 20,000 names onto a new object does not fit, and
// the run ends before the heap estimate passes the limit.
let setup = "var d = {value: 1}; var src = {}; for (var i = 0; i < 20000; i++) src['k' + i] = d; 0";
let mut measure = small(Limits::default());
assert_eq!(measure.run(setup), Ok(JsValue::Number(0.0)));
measure.interp().collect_garbage();
let heap_bytes = measure.interp().heap.bytes + (1 << 20);
let mut realm = small(Limits { heap_bytes, ..Limits::default() });
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
assert_eq!(limit(&mut realm, "var o = Object.defineProperties({}, src); 0"), LimitKind::Heap);
assert!(realm.interp().heap.bytes <= heap_bytes, "{} > {heap_bytes}", realm.interp().heap.bytes);
}
#[test]
fn a_heap_limit_smaller_than_the_built_ins_still_installs_every_built_in() {
// The heap limit bounds what scripts make, not the realm's own built-ins: a realm made with a
// tiny limit refuses runs with the heap limit, and has every built-in once the limit is raised.
let mut realm = small(Limits { heap_bytes: 1, ..Limits::default() });
assert_eq!(limit(&mut realm, "[]"), LimitKind::Heap);
realm.set_limits(Limits::default());
assert_eq!(
realm.run("typeof Math.max + typeof JSON.stringify + typeof parseInt + typeof escape + typeof decodeURIComponent"),
Ok(JsValue::String("functionfunctionfunctionfunctionfunction".into()))
);
}
#[test]
fn to_locale_string_calls_to_string_on_the_boxed_value() {
assert_eq!(
string(
"Object.defineProperty(Object.getPrototypeOf(Object(5)), 'toString', {value: function () { 'use strict'; return typeof this; }}); \
Object.prototype.toLocaleString.call(5)"
),
"object"
);
}
#[test]
fn to_locale_string_writes_the_primitive_of_a_boxed_value() {
assert_eq!(string("[1, 'x', true, false, 1.5].toLocaleString()"), "1,x,true,false,1.5");
assert_eq!(string("Object.prototype.toLocaleString.call(5)"), "5");
assert_eq!(string("Object.prototype.toLocaleString.call(Object('s'))"), "s");
// Object.prototype.toString comes after the standard prototypes on the chain.
assert_eq!(string("Object.prototype.toString = function () { return 'z'; }; [1, 'x', true].toLocaleString()"), "1,x,true");
// A toString on the object or on its standard prototype is called.
assert_eq!(string("var b = Object(true); b.toString = function () { return 'own'; }; [b].toLocaleString()"), "own");
assert_eq!(string("Object.getPrototypeOf(true).toString = function () { return 'p'; }; [true].toLocaleString()"), "p");
assert_eq!(string("Object.getPrototypeOf(true).toString = Object.prototype.toString; [true].toLocaleString()"), "[object Boolean]");
// Other objects still reach Object.prototype.toString.
assert_eq!(string("Object.prototype.toLocaleString.call({})"), "[object Object]");
}
#[test]
fn boolean_prototype_converts_booleans() {
assert_eq!(string("true.toString() + false.toString()"), "truefalse");
assert_eq!(string("Object(false).toString()"), "false");
truth("true.valueOf() === true && Object(false).valueOf() === false");
assert_eq!(num("Object(true) + 1"), 2.0);
assert_eq!(string("'' + Object(false)"), "false");
truth("Object.getPrototypeOf(true).valueOf() === false && Object.getPrototypeOf(true).toString() === 'false'");
throws("Object.getPrototypeOf(true).toString.call(1)", "TypeError");
throws("Object.getPrototypeOf(true).valueOf.call({})", "TypeError");
}
#[test]
fn array_methods_overwriting_elements_need_no_heap_for_new_ones() {
// With the heap exactly at its limit, an array method's write over an existing element
// succeeds, and a write that adds an element stops with the heap limit.
use crate::heap::key;
use crate::interp::Abrupt;
use crate::value::Value;
let mut realm = small(Limits::default());
assert_eq!(realm.run("var a = [1, 2]; 0"), Ok(JsValue::Number(0.0)));
let interp = realm.interp();
let global = interp.global;
let a = match interp.get_from(global, &key("a"), &Value::Object(global)) {
Ok(Value::Object(a)) => a,
other => panic!("{other:?}"),
};
interp.collect_garbage();
interp.limits.heap_bytes = interp.heap.bytes;
assert!(crate::builtins::put_at(interp, a, 0.0, Value::Number(5.0)).is_ok());
assert!(matches!(crate::builtins::put_at(interp, a, 2.0, Value::Number(5.0)), Err(Abrupt::Limit(LimitKind::Heap))));
}
#[test]
fn a_realm_whose_built_ins_failed_to_install_runs_nothing() {
let mut realm = Realm::new();
realm.interp().install_error = Some("the built-ins could not be installed: test".into());
match realm.run("var ran = true; 1") {
Err(RunError { kind: RunErrorKind::Internal, message, .. }) => assert_eq!(message, "the built-ins could not be installed: test"),
other => panic!("{other:?}"),
}
realm.interp().install_error = None;
assert_eq!(realm.run("typeof ran"), Ok(JsValue::String("undefined".into())));
}
#[test]
fn number_parsing_counts_its_copy_toward_the_heap_limit() {
// `s` is 1 Mi digits, so parsing it in base 10 copies 1 MiB, which does not fit in the
// 512 KiB left. Bases 16 and 36 read the digits where they are and still fit, but
// flattening a concatenation of `s` makes a 2 MiB buffer, which does not.
let setup = "var s = new Array((1 << 20) + 1).join('1'); var pad = new Array(800000).join('2'); 0";
let mut measure = small(Limits::default());
assert_eq!(measure.run(setup), Ok(JsValue::Number(0.0)));
measure.interp().collect_garbage();
let heap_bytes = measure.interp().heap.bytes + (512 << 10);
assert!(heap_bytes >= 4 << 20, "{heap_bytes}");
let limits = Limits { heap_bytes, ..Limits::default() };
let mut realm = small(limits.clone());
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
assert_eq!(
realm.run("parseInt(s, 16) > 0 && parseInt(s, 36) > 0 && parseInt('12') === 12 && parseFloat('1.5') === 1.5"),
Ok(JsValue::Bool(true))
);
assert_eq!(limit(&mut realm, "parseInt(s)"), LimitKind::Heap);
let mut realm = small(limits.clone());
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
assert_eq!(limit(&mut realm, "parseInt(s + '1', 16)"), LimitKind::Heap);
let mut realm = small(limits);
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
assert_eq!(limit(&mut realm, "parseFloat(s)"), LimitKind::Heap);
}
#[test]
fn strings_built_ins_write_take_fuel_and_stay_counted_across_collections() {
// Each join writes 1 Mi code units, so a thousand of them do not fit in 2,000,000 units of
// fuel, whether the units are separators or a string's contents.
let limits = Limits { heap_bytes: 8 << 20, fuel: 2_000_000, ..Limits::default() };
assert_eq!(limit(&mut small(limits.clone()), "var a = new Array((1 << 20) + 1); for (var i = 0; i < 1000; i++) a.join('x'); i"), LimitKind::Fuel);
assert_eq!(
limit(&mut small(limits), "var a = [new Array((1 << 20) + 1).join('x')]; for (var i = 0; i < 1000; i++) a.join(''); i"),
LimitKind::Fuel
);
// `join` holds a 2 MiB buffer for `s` while the second element's `toString` keeps new objects.
// A collection in between still counts the buffer, so the objects and the buffer together do
// not fit in a heap with 1 MiB to spare beyond the objects.
let setup = "var s = new Array((1 << 20) + 1).join('x'); var keep = []; 0";
let fill = "for (var i = 0; i < 30000; i++) keep.push({});";
let mut measure = small(Limits::default());
assert_eq!(measure.run(&format!("{setup}; {fill} 0")), Ok(JsValue::Number(0.0)));
measure.interp().collect_garbage();
let heap_bytes = measure.interp().heap.bytes + (1 << 20);
let mut realm = small(Limits { heap_bytes, ..Limits::default() });
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
let join = format!("var f = {{toString: function () {{ {fill} return ''; }}}}; [s, f].join('').length");
assert_eq!(limit(&mut realm, &join), LimitKind::Heap);
// A join that throws gives its buffer back, so a hundred of them still fit in 8 MiB.
let mut realm = small(Limits { heap_bytes: 8 << 20, ..Limits::default() });
let src = "var s = new Array((1 << 20) + 1).join('x'); var t = {toString: function () { throw 1; }}; \
for (var i = 0; i < 100; i++) { try { [s, t].join(''); } catch (e) {} } i";
assert_eq!(realm.run(src), Ok(JsValue::Number(100.0)));
}
#[test]
fn values_built_ins_hold_survive_collections() {
// A small heap makes the callbacks' garbage start collections while the results are built.
let mut realm = small(Limits { heap_bytes: 2 << 20, ..Limits::default() });
let src = "function junk() { var t = []; for (var j = 0; j < 200; j++) t[j] = {j: j}; return t.length; } \
var src = []; for (var i = 0; i < 300; i++) src[i] = i; \
var m = src.map(function (x) { junk(); return {v: x}; }); \
var f = src.filter(function (x) { junk(); return x % 2; }); \
var r = src.reduce(function (acc, x) { junk(); return {sum: acc.sum + x}; }, {sum: 0}); \
var s = src.slice().sort(function (a, b) { junk(); return b - a; }); \
var j = JSON.parse(JSON.stringify(m), function (k, v) { junk(); return v; }); \
m[299].v + f.length + r.sum + s[0] + j[299].v + j.length";
assert_eq!(realm.run(src), Ok(JsValue::Number(299.0 + 150.0 + 44_850.0 + 299.0 + 299.0 + 300.0)));
assert!(realm.interp().heap.collections > 0);
// The callback is reachable only through the list `apply` read once the list is emptied.
let src = "var list = [function (x) { list.length = 0; junk(); return {v: x}; }]; \
var r = Array.prototype.map.apply(src, list); \
var g = Array.prototype.map.bind(src, function (x) { junk(); return {v: x}; }); g = g(); \
r[299].v + g[299].v";
assert_eq!(realm.run(src), Ok(JsValue::Number(598.0)));
}
#[test]
fn fresh_values_survive_a_collection_while_they_are_stored() {
// Below 1 MiB the only collections run at the heap limit, many of them while a built-in
// reserves room for a value that a callback has just made and nothing else holds.
let limits = Limits { heap_bytes: 768 << 10, ..Limits::default() };
let src = "var bad = 0; \
for (var n = 0; n < 2000; n++) { \
var m = [1, 2, 3, 4, 5, 6, 7, 8].map(function (x) { var g = {x: x}; return {v: x}; }); \
var f = [1, 2, 3, 4].filter(function () { return true; }).map(function (x) { return [x]; }); \
for (var i = 0; i < 8; i++) if (!m[i] || m[i].v !== i + 1) bad++; \
for (var i = 0; i < 4; i++) if (!f[i] || f[i][0] !== i + 1) bad++; \
} bad";
let mut realm = small(limits.clone());
assert_eq!(realm.run(src), Ok(JsValue::Number(0.0)));
assert!(realm.interp().heap.collections > 0);
// Getters make a new object for each read, which `shift` and `sort` then store.
let src = "function fresh(v) { var g = {x: v}; return {v: v}; } \
function set(i) { return function (x) { Object.defineProperty(this, i, {value: x, writable: true, enumerable: true, configurable: true}); }; } \
var p = {}; \
for (var i = 0; i < 4; i++) Object.defineProperty(p, i, {get: (function (v) { return function () { return fresh(v); }; })(4 - i), set: set(i)}); \
var bad = 0; \
for (var n = 0; n < 1000; n++) { \
var o = Object.create(p); o.length = 4; Array.prototype.shift.call(o); \
if (!o[0] || o[0].v !== 3 || o[2].v !== 1) bad++; \
var s = Object.create(p); s.length = 4; Array.prototype.sort.call(s, function (a, b) { return a.v - b.v; }); \
for (var i = 0; i < 4; i++) if (!s[i] || s[i].v !== i + 1) bad++; \
} \
var q = {}; \
for (var i = 0; i < 4; i++) Object.defineProperty(q, i, {get: (function (v) { return function () { return {v: v}; }; })(4 - i), set: function () {}}); \
for (var n = 0; n < 20000; n++) { \
var u = Object.create(q); u.length = 4; Array.prototype.unshift.call(u, 0, 0, 0, 0); \
for (var i = 4; i < 8; i++) if (!u.hasOwnProperty(i) || u[i].v !== 8 - i) bad++; \
} \
for (var n = 0; n < 5000; n++) { var t = Object.create(q); t.length = 4; Array.prototype.sort.call(t); } \
bad";
let mut realm = small(limits.clone());
assert_eq!(realm.run(src), Ok(JsValue::Number(0.0)));
assert!(realm.interp().heap.collections > 0);
// The `z` objects change each round's garbage, so the collections fall at different points.
let src = "var bad = 0; \
for (var n = 0; n < 10000; n++) { \
for (var z = 0; z < n % 7; z++) var g = {z: z}; \
var j = JSON.parse('[1, 2, 3, 4, 5, 6, 7, 8]', function (k, v) { return k === '' ? v : {v: v}; }); \
for (var i = 0; i < 8; i++) if (!j[i] || j[i].v !== i + 1) bad++; \
} bad";
let mut realm = small(limits.clone());
assert_eq!(realm.run(src), Ok(JsValue::Number(0.0)));
assert!(realm.interp().heap.collections > 0);
let src = "var k = ''; for (var i = 0; i < 300; i++) k += 'k'; \
var o = {}; o[k] = {toJSON: function () { var g = {x: 1}; return {a: 1}; }}; \
var want = '{\"' + k + '\":{\"a\":1}}'; \
var bad = 0; for (var n = 0; n < 4000; n++) if (JSON.stringify(o) !== want) bad++; bad";
let mut realm = small(limits);
assert_eq!(realm.run(src), Ok(JsValue::Number(0.0)));
assert!(realm.interp().heap.collections > 0);
}
// ---- errors ----
#[test]
fn error_objects_have_names_messages_and_to_string() {
assert_eq!(string("new TypeError('bad').toString()"), "TypeError: bad");
assert_eq!(string("RangeError('r').name + Error().message + (new Error(undefined).hasOwnProperty('message') ? 1 : 0)"), "RangeError0");
truth("var e = new SyntaxError('s'); e instanceof SyntaxError && e instanceof Error && Object.prototype.toString.call(e) === '[object Error]'");
assert_eq!(string("var e = new Error('m'); e.name = ''; e.toString()"), "m");
assert_eq!(string("Error.prototype.toString.call({name: 'N'})"), "N");
truth("URIError.prototype.name === 'URIError' && Object.getPrototypeOf(EvalError.prototype) === Error.prototype && Error.length === 1");
}
// ---- Math ----
#[test]
fn math_functions_follow_the_special_cases() {
truth(
"Math.max() === -Infinity && Math.min() === Infinity && isNaN(Math.max(1, NaN, 3)) && 1 / Math.max(-0, 0) === Infinity && 1 / Math.min(0, -0) === -Infinity",
);
truth("Math.round(2.5) === 3 && Math.round(-2.5) === -2 && 1 / Math.round(-0.4) === -Infinity && Math.round(0.49999999999999994) === 0");
truth("isNaN(Math.pow(1, NaN)) && isNaN(Math.pow(-1, Infinity)) && Math.pow(NaN, 0) === 1 && Math.pow(2, -1) === 0.5");
truth("Math.abs(-3) === 3 && Math.floor(-1.5) === -2 && Math.ceil(-1.5) === -1 && 1 / Math.ceil(-0.5) === -Infinity");
truth("Math.sqrt(-1) !== Math.sqrt(-1) && Math.atan2(0, -0) === Math.PI && Math.exp(0) === 1 && Math.log(Math.E) === 1");
truth("Math.abs(Math.sin(Math.PI)) < 1e-15 && Math.cos(0) === 1 && Math.tan(0) === 0 && Math.asin(1) === Math.PI / 2");
truth("var ok = true; for (var i = 0; i < 1000; i++) { var r = Math.random(); if (!(r >= 0 && r < 1)) ok = false; } ok");
truth("Math.SQRT2 * Math.SQRT1_2 > 0.9999 && Math.LN2 > 0.69 && Math.LOG10E > 0.43 && Object.prototype.toString.call(Math) === '[object Math]'");
truth("var n = 0; Math.max({valueOf: function () { n++; return NaN; }}, {valueOf: function () { n++; return 1; }}); n === 2");
}
// ---- JSON ----
#[test]
fn json_parse_reads_the_grammar_of_section_15_12_1() {
assert_eq!(num("var v = JSON.parse(' {\"a\": [1, -2.5e1, true, null, \"x\\\\u0041\\\\n\"], \"b\": {}} '); v.a[1] + v.a.length"), -20.0);
assert_eq!(num("JSON.parse('\"\\\\ud800\"').charCodeAt(0)"), 55296.0);
truth("JSON.parse('[]').length === 0 && JSON.parse('0') === 0 && 1 / JSON.parse('-0') === -Infinity && JSON.parse('{\"a\":1,\"a\":2}').a === 2");
for bad in ["'{a: 1}'", "'[1,]'", "'01'", "'1.'", "'.5'", "'\"\\t\"'", "\"'x'\"", "'[1] 2'", "''", "'+1'", "'\"\\\\x\"'", "'{\"a\" 1}'"] {
throws(&format!("JSON.parse({bad})"), "SyntaxError");
}
// The reviver sees every value bottom-up and can delete or replace it.
assert_eq!(
string(
"var seen = []; var v = JSON.parse('{\"a\": [1, 2], \"b\": 3}', function (k, v) { seen.push(k); return k === 'b' ? undefined : v; }); seen.join() + ':' + ('b' in v)"
),
"0,1,a,b,:false"
);
assert_eq!(num("JSON.parse('[1, 2]', function (k, v) { return typeof v === 'number' ? v * 10 : v; })[1]"), 20.0);
}
#[test]
fn json_stringify_writes_values_with_replacers_and_indentation() {
assert_eq!(
string("JSON.stringify({a: [1, 'x', null, undefined, function () {}], b: undefined, c: true})"),
"{\"a\":[1,\"x\",null,null,null],\"c\":true}"
);
assert_eq!(string("JSON.stringify('\\u0001\"\\\\\\n')"), "\"\\u0001\\\"\\\\\\n\"");
assert_eq!(string("JSON.stringify([NaN, Infinity, -0, 1e21])"), "[null,null,0,1e+21]");
assert_eq!(run("JSON.stringify(undefined)"), JsValue::Undefined);
assert_eq!(run("JSON.stringify(function () {})"), JsValue::Undefined);
assert_eq!(string("JSON.stringify({a: 1, b: [1, 2], c: {}}, null, 2)"), "{\n \"a\": 1,\n \"b\": [\n 1,\n 2\n ],\n \"c\": {}\n}");
assert_eq!(string("JSON.stringify([1], null, 'abcdefghijkl')"), "[\nabcdefghij1\n]");
assert_eq!(string("JSON.stringify({a: 1, b: 2, c: 3}, ['c', 'a', 'c', 1])"), "{\"c\":3,\"a\":1}");
assert_eq!(string("JSON.stringify({a: 1, b: 'x'}, function (k, v) { return typeof v === 'number' ? v + 1 : v; })"), "{\"a\":2,\"b\":\"x\"}");
assert_eq!(string("JSON.stringify({toJSON: function (k) { return 'k=' + k; }})"), "\"k=\"");
assert_eq!(string("JSON.stringify({d: {toJSON: function (k) { return k; }}})"), "{\"d\":\"d\"}");
assert_eq!(string("JSON.stringify([Object(false), Object(true)])"), "[false,true]");
// Number and String objects go through ToNumber and ToString, which call valueOf and toString.
assert_eq!(string("var n = Object(5); n.valueOf = function () { return 7; }; JSON.stringify([n])"), "[7]");
assert_eq!(string("var s = Object('x'); s.toString = function () { return 'y'; }; JSON.stringify({s: s})"), "{\"s\":\"y\"}");
assert_eq!(string("JSON.stringify({}, null, 4) + JSON.stringify([], null, 4)"), "{}[]");
throws("var a = []; a.push(a); JSON.stringify(a)", "TypeError");
throws("var o = {}; o.x = {y: o}; JSON.stringify(o)", "TypeError");
// The same object twice, not nested in itself, is not a cycle.
assert_eq!(string("var x = {}; JSON.stringify([x, x])"), "[{},{}]");
}
#[test]
fn json_stringify_unwraps_number_string_and_boolean_objects() {
assert_eq!(string("JSON.stringify(Object(3))"), "3");
assert_eq!(string("JSON.stringify(Object('abc'))"), "\"abc\"");
assert_eq!(string("JSON.stringify(Object(true))"), "true");
assert_eq!(string("JSON.stringify({n: Object(-0), m: Object(NaN), s: Object('')})"), "{\"n\":0,\"m\":null,\"s\":\"\"}");
assert_eq!(string("JSON.stringify([Object.getPrototypeOf(Object(3)), Object.getPrototypeOf(Object('abc'))])"), "[0,\"\"]");
// The space argument (step 5).
assert_eq!(string("JSON.stringify([1], null, Object(2))"), "[\n 1\n]");
assert_eq!(string("JSON.stringify([1], null, Object('--'))"), "[\n--1\n]");
// Replacer list entries (step 4.b.ii).
assert_eq!(string("JSON.stringify({a: 1, 3: 2, b: 3}, [Object('a'), Object(3)])"), "{\"a\":1,\"3\":2}");
// Methods on Object.prototype do not reach the objects; methods on their own prototypes do.
assert_eq!(string("Object.prototype.toString = function () { return 'z'; }; JSON.stringify(Object('abc'))"), "\"abc\"");
assert_eq!(string("Object.prototype.valueOf = function () { return 9; }; JSON.stringify(Object(3))"), "3");
assert_eq!(string("Object.getPrototypeOf(Object(1)).valueOf = function () { return 4; }; JSON.stringify(Object(3))"), "4");
assert_eq!(string("Object.getPrototypeOf(Object('')).toString = function () { return 'q'; }; JSON.stringify(Object('abc'))"), "\"q\"");
}
#[test]
fn json_parse_counts_a_string_while_it_reserves_the_property_for_it() {
// The limits around the parsed value's size include one where the reservation for the string's
// property is what starts a collection. The text is a concatenation, so the parse also flattens
// it. Whenever the parse fits, the heap total counts everything that is live.
let setup = |text: &str| format!("var s = new Array(4097).join('x'); var text = {text}; 0");
for text in ["'[\"' + s + '\"]'", "'{\"k\":\"' + s + '\"}'"] {
let setup = setup(text);
let parse = "var a = JSON.parse(text); 0";
let mut measure = small(Limits::default());
assert_eq!(measure.run(&setup), Ok(JsValue::Number(0.0)));
measure.interp().collect_garbage();
assert_eq!(measure.run(parse), Ok(JsValue::Number(0.0)));
let full = measure.interp().heap.bytes;
let (mut fits, mut over) = (0, 0);
for heap_bytes in (full - 8192..=full + 256).step_by(4) {
let mut realm = small(Limits { heap_bytes, ..Limits::default() });
assert_eq!(realm.run(&setup), Ok(JsValue::Number(0.0)));
realm.interp().collect_garbage();
match realm.run(parse) {
Ok(_) => {
let counted = realm.interp().heap.bytes;
let live = realm.collect_garbage();
assert!(counted >= live, "{text}: {counted} bytes counted with {live} live under a limit of {heap_bytes}");
fits += 1;
}
Err(RunError { kind: RunErrorKind::Limit(LimitKind::Heap), .. }) => over += 1,
Err(e) => panic!("{text}: {e:?}"),
}
}
assert!(fits > 0 && over > 0, "{text}: {fits} limits fit and {over} did not");
}
}
#[test]
fn json_parse_keeps_its_text_counted_for_the_whole_parse() {
// The text exists only as the ToString result of the argument, and the parsed value needs a
// collection. Without the text the value fits under the limit; with it the value does not.
let setup = "var ws = new Array(8193).join(' '); var items = new Array(257).join('{},'); \
var o = {toString: function () { return ws + '[' + items + '{}]'; }}; 0";
let parse = "var a = JSON.parse(o); 0";
let mut measure = small(Limits::default());
assert_eq!(measure.run(setup), Ok(JsValue::Number(0.0)));
measure.interp().collect_garbage();
assert_eq!(measure.run(parse), Ok(JsValue::Number(0.0)));
let full = measure.interp().heap.bytes;
let text = 2 * (8192 + 2 + 3 * 256 + 2);
let mut realm = small(Limits { heap_bytes: full - text / 2, ..Limits::default() });
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
realm.interp().collect_garbage();
assert_eq!(limit(&mut realm, parse), LimitKind::Heap);
// The text is released before the reviver runs, so the reviver can use the room it took.
let setup = "var ws = new Array(65537).join(' '); var o = {toString: function () { return ws + '1'; }}; 0";
let mut measure = small(Limits::default());
assert_eq!(measure.run(setup), Ok(JsValue::Number(0.0)));
let heap_bytes = measure.collect_garbage() + 2 * 65537 + (16 << 10);
let mut realm = small(Limits { heap_bytes, ..Limits::default() });
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
assert_eq!(realm.run("JSON.parse(o, function () { return new Array(40001).join('x').length; })"), Ok(JsValue::Number(40000.0)));
}
#[test]
fn json_parse_counts_names_that_wait_for_their_values() {
// An unterminated text: every name read waits for a value that never comes, so nothing but
// the parser holds them, and the names together pass the limit before the text ends.
let setup = "var k = new Array(2049).join('k'); var text = new Array(33).join('{\"' + k + '\":'); 0";
let mut measure = small(Limits::default());
assert_eq!(measure.run(setup), Ok(JsValue::Number(0.0)));
let heap_bytes = measure.collect_garbage() + (96 << 10);
let mut realm = small(Limits { heap_bytes, ..Limits::default() });
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
assert_eq!(limit(&mut realm, "JSON.parse(text)"), LimitKind::Heap);
// A complete text gives its names back as it stores them, so one that fits parses.
let mut realm = small(Limits { heap_bytes: heap_bytes + (64 << 10), ..Limits::default() });
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
assert_eq!(realm.run("var a = JSON.parse('{\"' + k + '\":{\"' + k + 'x\":1}}'); 0"), Ok(JsValue::Number(0.0)));
}
#[test]
fn deep_json_nesting_needs_no_native_stack() {
let handle = std::thread::Builder::new()
.stack_size(256 << 10)
.spawn(|| {
let mut realm = Realm::new();
realm.run(
"var depth = 100000; var s = ''; for (var i = 0; i < depth; i++) s += '['; for (i = 0; i < depth; i++) s += ']'; \
var v = JSON.parse(s); var back = JSON.stringify(v); var n = 0; JSON.parse(s, function (k, v) { n++; return v; }); \
back.length === 2 * depth && n === depth",
)
})
.unwrap();
assert_eq!(handle.join().unwrap(), Ok(JsValue::Bool(true)));
}
// ---- global functions ----
#[test]
fn global_number_functions() {
truth("parseInt(' 0x1F') === 31 && parseInt('-12px') === -12 && parseInt('z', 36) === 35 && isNaN(parseInt('', 10)) && isNaN(parseInt('1', 1))");
truth(
"parseInt('011') === 11 && parseInt('11', 2) === 3 && parseInt('0x10', 16) === 16 && isNaN(parseInt('0x', 16)) && parseInt('12', 3.9) === 5",
);
truth("parseInt('123456789012345678901234567890') === 1.2345678901234568e29 && 1 / parseInt('-0') === -Infinity");
truth(
"parseFloat(' 3.5e2x') === 350 && parseFloat('-.5') === -0.5 && parseFloat('Infinityx') === Infinity && isNaN(parseFloat('e5')) && parseFloat('1e') === 1",
);
truth("isNaN('x') && !isNaN('1') && isFinite('1') && !isFinite(Infinity) && !isFinite(NaN)");
truth("NaN !== NaN && Infinity === 1 / 0 && undefined === void 0 && !Object.getOwnPropertyDescriptor(this, 'NaN').writable");
}
#[test]
fn uri_functions_encode_and_decode_utf8() {
assert_eq!(string("encodeURIComponent('a b&c/\\u00e9\\ud83d\\ude00')"), "a%20b%26c%2F%C3%A9%F0%9F%98%80");
assert_eq!(string("encodeURI('http://x.y/a b?c=d#e')"), "http://x.y/a%20b?c=d#e");
assert_eq!(string("decodeURIComponent('a%20b%26%C3%A9%F0%9F%98%80') === 'a b&\\u00e9\\ud83d\\ude00' ? 'ok' : 'no'"), "ok");
assert_eq!(string("decodeURI('%3B%2F%20%23')"), "%3B%2F %23");
for bad in [
"encodeURI('\\ud800')",
"encodeURIComponent('\\udc00x')",
"decodeURI('%')",
"decodeURI('%C3')",
"decodeURI('%C0%80')",
"decodeURI('%ED%A0%80')",
"decodeURI('%FF')",
] {
throws(bad, "URIError");
}
}
#[test]
fn escape_and_unescape_follow_annex_b() {
assert_eq!(string("escape('a b+/@\\u00e9\\u0100')"), "a%20b+/@%E9%u0100");
assert_eq!(string("unescape('a%20b%E9%u0100%zz%u12') === 'a b\\u00e9\\u0100%zz%u12' ? 'ok' : 'no'"), "ok");
}
// ---- robustness ----
/// Calls of built-ins with arguments of every kind.
const CALLS: &[&str] = &[
"[1,,3].join(X)",
"[3,1,2].sort(X)",
"[1,2,3].splice(X, X, X)",
"[1,2].slice(X, X)",
"[].concat(X, [X])",
"[1,2,3].map(X)",
"[1,2].reduce(X, X)",
"[1,2].indexOf(X, X)",
"[1,2].lastIndexOf(X, X)",
"Array.prototype.push.call(X, X)",
"Array.prototype.unshift.call(X, X)",
"Array.prototype.reverse.call(X)",
"Array(X)",
"Object.keys(X)",
"Object.freeze(X)",
"Object.defineProperty(X, X, X)",
"Object.create(X, X)",
"Object.getOwnPropertyNames(X)",
"JSON.stringify(X, X, X)",
"JSON.parse(X, X)",
"Math.max(X, X)",
"Math.pow(X, X)",
"parseInt(X, X)",
"parseFloat(X)",
"decodeURIComponent(X)",
"encodeURI(X)",
"unescape(X)",
"escape(X)",
"Function.prototype.bind.call(X, X)",
];
const ARGS: &[&str] = &[
"undefined",
"null",
"0",
"-1",
"4294967295",
"NaN",
"'%E0'",
"'[1,{\"a\":2}]'",
"''",
"[]",
"{length: 4294967295}",
"{}",
"function (a, b) { return b - a; }",
"{valueOf: function () { throw 1; }}",
"{get length() { return 3; }}",
"Object.freeze([1, 2])",
"{value: 1, get: function () {}}",
];
proptest! {
#![proptest_config(ProptestConfig::with_cases(256))]
#[test]
fn arbitrary_built_in_calls_never_panic(call in 0..CALLS.len(), args in proptest::collection::vec(0..ARGS.len(), 3)) {
let mut src = CALLS[call].to_string();
for a in &args {
src = src.replacen('X', ARGS[*a], 1);
}
let src = src.replace('X', "1");
let mut realm = small(Limits { fuel: 200_000, heap_bytes: 4 << 20, frames: 200, reentry: 8 });
if let Err(e) = realm.run(&src) {
prop_assert!(e.kind != RunErrorKind::Internal, "{src}: {e:?}");
}
}
}
+28 -4
View File
@@ -170,11 +170,14 @@ pub(crate) enum NativeKind {
Eval,
}
/// What a function object runs. `Bound` is a function made by `Function.prototype.bind` (section
/// 15.3.4.5): calling it calls `target` with `this` and `args` before the arguments it is given.
#[derive(Clone)]
pub(crate) enum Func {
Closure { code: Rc<Code>, env: EnvId },
Native { f: NativeFn, kind: NativeKind, constructor: bool, name: &'static str },
Host { f: Rc<HostFn>, name: Rc<str> },
Bound { target: ObjId, this: Value, args: Rc<[Value]> },
}
/// A sloppy mode `arguments` object's link to its function's parameters (section 10.6): index
@@ -204,8 +207,8 @@ pub(crate) enum Class {
Function(Func),
Error,
Arguments(Box<ArgMap>),
/// A `Boolean` object made by ToObject.
Boolean,
/// A `Boolean` object made by ToObject, with its primitive value.
Boolean(bool),
/// A `Number` object and its number.
Number(f64),
String(Str),
@@ -213,18 +216,24 @@ pub(crate) enum Class {
/// A `Date` object and its time value.
Date(f64),
ForIn(Box<ForIn>),
/// The `Math` object (section 15.8).
Math,
/// The `JSON` object (section 15.12).
Json,
}
impl Class {
/// The `[[Class]]` name `Object.prototype.toString` reports.
pub(crate) fn name(&self) -> &'static str {
match self {
Class::Math => "Math",
Class::Json => "JSON",
Class::Ordinary | Class::ForIn(_) => "Object",
Class::Array { .. } => "Array",
Class::Function(_) => "Function",
Class::Error => "Error",
Class::Arguments(_) => "Arguments",
Class::Boolean => "Boolean",
Class::Boolean(_) => "Boolean",
Class::Number(_) => "Number",
Class::String(_) => "String",
Class::RegExp(_) => "RegExp",
@@ -292,6 +301,8 @@ pub(crate) const OBJECT_BYTES: usize = 96;
pub(crate) const ENV_BYTES: usize = 64;
/// The estimated bytes of a rope node.
pub(crate) const ROPE_BYTES: usize = 48;
/// The estimated bytes of each argument a bound function holds.
pub(crate) const BOUND_ARG_BYTES: usize = 24;
/// The estimated bytes of a piece of code without its instructions, tables and strings.
const CODE_BYTES: usize = 384;
@@ -308,6 +319,10 @@ pub(crate) struct Heap {
pub(crate) bytes: usize,
/// How many collections have run.
pub(crate) collections: usize,
/// Counts the properties whose names start with a digit (the array indices among them) added
/// to any object, so an operation that lists the indices an object has can tell whether a
/// script added one since.
pub(crate) index_epoch: u64,
}
impl Heap {
@@ -524,11 +539,20 @@ fn trace_object(o: &Object, work: &mut Vec<u32>, strings: &mut StringSizes) -> u
Prop::Accessor { get, set, .. } => work.extend(get.iter().chain(set.iter()).map(|f| f.0)),
}
}
let mut extra = 0;
match &o.class {
Class::Function(Func::Closure { env, code }) => {
work.push(env.0);
mark_code(code, strings);
}
Class::Function(Func::Bound { target, this, args }) => {
work.push(target.0);
mark_value(this, work, strings);
for a in args.iter() {
mark_value(a, work, strings);
}
extra = BOUND_ARG_BYTES * args.len();
}
Class::Arguments(map) => work.push(map.env.0),
Class::String(s) => strings.add(s),
Class::RegExp(re) => {
@@ -542,7 +566,7 @@ fn trace_object(o: &Object, work: &mut Vec<u32>, strings: &mut StringSizes) -> u
}
_ => {}
}
OBJECT_BYTES + PROP_BYTES * o.props.len()
OBJECT_BYTES + PROP_BYTES * o.props.len() + extra
}
fn trace_env(e: &Env, work: &mut Vec<u32>, strings: &mut StringSizes) -> usize {
+376 -28
View File
@@ -164,6 +164,17 @@ pub(crate) struct Interp {
pub(crate) throw_site: Option<ThrowSite>,
/// The last exception a run did not catch.
pub(crate) last_thrown: Value,
/// The state of `Math.random`'s generator.
pub(crate) random: u64,
/// The objects `Array.prototype.join` and `toLocaleString` are joining, innermost last: a
/// cycle back to one of them joins as an empty string.
pub(crate) join_stack: Vec<ObjId>,
/// Bytes of the string buffers native functions are building outside the heap. They are
/// charged to `heap.bytes` and added back after each collection, and a native call's
/// buffers are released when it returns.
pub(crate) held_bytes: usize,
/// Why installing the built-ins failed; a realm with this set runs no script.
pub(crate) install_error: Option<String>,
/// The clock and time zone `Date` reads.
pub(crate) date_host: crate::date_builtins::DateHost,
/// The bytes native code holds outside the heap's cells through [`HeldBytes`].
@@ -239,6 +250,15 @@ fn index_keys_bytes(len: usize) -> usize {
total
}
/// A seed for `Math.random` that differs from realm to realm, from the standard library's
/// randomly keyed hasher; never zero.
fn random_seed() -> u64 {
use std::hash::{BuildHasher, Hasher};
let mut h = std::collections::hash_map::RandomState::new().build_hasher();
h.write_u64(0x9e37_79b9_7f4a_7c15);
h.finish() | 1
}
/// The estimated bytes of a binding or property named by `len` code units.
fn binding_bytes(len: usize) -> usize {
PROP_BYTES + string_bytes(len)
@@ -341,6 +361,10 @@ impl Interp {
source_units: 0,
throw_site: None,
last_thrown: Value::Undefined,
random: random_seed(),
join_stack: Vec::new(),
held_bytes: 0,
install_error: None,
date_host: crate::date_builtins::DateHost::default(),
held: Rc::default(),
};
@@ -515,7 +539,7 @@ impl Interp {
self.heap.collect(&roots);
// The collection counts the live total again, so bytes already released are not in it.
self.held.released.set(0);
self.heap.bytes = self.heap.bytes.saturating_add(self.held.now.get());
self.heap.bytes = self.heap.bytes.saturating_add(self.held_bytes).saturating_add(self.held.now.get());
}
/// Runs frames until the boundary frame on top returns, and returns its value.
@@ -1058,13 +1082,219 @@ impl Interp {
*length = i + 1;
}
let name_len = k.len();
let index = new && k.first().is_some_and(|&u| (u16::from(b'0')..=u16::from(b'9')).contains(&u));
o.props.insert(k, prop);
if new {
self.heap.bytes += PROP_BYTES + string_bytes(name_len);
}
if index {
self.heap.index_epoch = self.heap.index_epoch.wrapping_add(1);
}
Ok(())
}
/// [[DefineOwnProperty]] (section 8.12.9, with the array rules of section 15.4.5.1 and the
/// arguments object rules of section 10.6). Returns whether the property was defined; with
/// `throw` a rejection throws a `TypeError` instead.
pub(crate) fn define_own_property(&mut self, id: ObjId, k: Key, desc: &Desc, throw: bool) -> JsResult<bool> {
match &self.object(id)?.class {
Class::Array { length, length_writable } => {
let (length, length_writable) = (*length, *length_writable);
if is(&k, "length") {
return self.define_array_length(id, desc, throw);
}
if let Some(i) = array_index(&k)
&& i >= length
&& !length_writable
{
return self.reject_bool(throw, &k, "the array's length is read-only");
}
self.define_ordinary(id, k, desc, throw)
}
Class::Arguments(map) => {
let mapped = array_index(&k).and_then(|i| map.names.get(i as usize)).and_then(Option::as_ref).cloned();
if !self.define_ordinary(id, k.clone(), desc, false)? {
return self.reject_bool(throw, &k, "the property cannot be redefined");
}
if let Some(name) = mapped {
let env = match &self.object(id)?.class {
Class::Arguments(map) => Some(map.env),
_ => None,
};
if let Some(v) = desc.value.clone().filter(|_| !desc.is_accessor())
&& let Some(env) = env
&& let Some(Env::Decl { bindings, .. }) = self.heap.env_mut(env)
&& let Some(b) = bindings.get_mut(&name)
{
b.value = v;
}
if (desc.is_accessor() || desc.writable == Some(false))
&& let Class::Arguments(map) = &mut self.object_mut(id)?.class
&& let Some(slot) = array_index(&k).and_then(|i| map.names.get_mut(i as usize))
{
*slot = None;
}
}
Ok(true)
}
_ => self.define_ordinary(id, k, desc, throw),
}
}
fn reject_bool(&mut self, throw: bool, k: &[u16], why: &str) -> JsResult<bool> {
if throw {
let message = format!("cannot define property '{}': {why}", String::from_utf16_lossy(k));
return Err(self.type_error(&message));
}
Ok(false)
}
/// The default [[DefineOwnProperty]] (section 8.12.9).
fn define_ordinary(&mut self, id: ObjId, k: Key, desc: &Desc, throw: bool) -> JsResult<bool> {
let current = self.get_own_property(id, &k)?;
let o = self.object(id)?;
let virtual_string = matches!(&o.class, Class::String(s) if is(&k, "length") || array_index(&k).is_some_and(|i| (i as usize) < s.len()));
let Some(current) = current else {
if !o.extensible {
return self.reject_bool(throw, &k, "the object is not extensible");
}
// The object and the descriptor's values stay rooted while the heap is checked.
let mut roots = vec![Value::Object(id)];
roots.extend(desc.value.iter().cloned());
roots.extend(desc.get.flatten().into_iter().chain(desc.set.flatten()).map(Value::Object));
self.rooted(&roots, |i| i.reserve_heap(binding_bytes(k.len())))?;
let flags = desc.flags_or(0);
let prop = if desc.is_accessor() {
Prop::Accessor { get: desc.get.flatten(), set: desc.set.flatten(), flags: flags & !WRITABLE }
} else {
Prop::Data { value: desc.value.clone().unwrap_or(Value::Undefined), flags }
};
self.define(id, k, prop)?;
return Ok(true);
};
let Some(merged) = merge_desc(&current, desc) else { return self.reject_bool(throw, &k, "the property cannot be redefined") };
// An index or the length of a String object cannot change, so a definition that passes
// the checks leaves it as it is.
if virtual_string {
return Ok(true);
}
self.define(id, k, merged)?;
Ok(true)
}
/// Defining an array's `length` (section 15.4.5.1, step 3).
fn define_array_length(&mut self, id: ObjId, desc: &Desc, throw: bool) -> JsResult<bool> {
let length_key = key("length");
let Some(v) = desc.value.clone() else {
let Some(current) = self.get_own_property(id, &length_key)? else { return internal("an array without a length") };
let Some(merged) = merge_desc(&current, desc) else { return self.reject_bool(throw, &length_key, "the length cannot be redefined") };
if let Class::Array { length_writable, .. } = &mut self.object_mut(id)?.class {
*length_writable = merged.flags() & WRITABLE != 0;
}
return Ok(true);
};
let new = to_uint32(self.to_number(&v)?);
if f64::from(new) != self.to_number(&v)? {
return Err(self.throw(ErrorType::Range, "invalid array length"));
}
let Some(current) = self.get_own_property(id, &length_key)? else { return internal("an array without a length") };
let Class::Array { length: old, length_writable } = self.object(id)?.class else { return internal("not an array") };
let new_desc = Desc { value: Some(Value::Number(f64::from(new))), ..desc.clone() };
if new >= old {
let Some(merged) = merge_desc(&current, &new_desc) else { return self.reject_bool(throw, &length_key, "the length cannot be redefined") };
if let Class::Array { length, length_writable } = &mut self.object_mut(id)?.class {
*length = new;
*length_writable = merged.flags() & WRITABLE != 0;
}
return Ok(true);
}
if !length_writable {
return self.reject_bool(throw, &length_key, "it is read-only");
}
let checked = Desc { writable: None, ..new_desc };
if merge_desc(&current, &checked).is_none() {
return self.reject_bool(throw, &length_key, "the length cannot be redefined");
}
let read_only = desc.writable == Some(false);
if !self.shrink_array(id, new, read_only)? {
return self.reject_bool(throw, &length_key, "an element cannot be deleted");
}
Ok(true)
}
/// Sets an array's length to `new`, deleting the elements at and past it from the last one
/// down (section 15.4.5.1, step 3.l). An element that cannot be deleted stops it, leaving
/// the length one past that element, and gives `false`. Each property takes a unit of fuel
/// before anything is removed. With `read_only` the length is made read-only afterwards
/// either way.
fn shrink_array(&mut self, id: ObjId, new: u32, read_only: bool) -> JsResult<bool> {
let Class::Array { length: old, .. } = self.object(id)?.class else { return internal("not an array") };
let mut ok = true;
let mut length = new;
if new < old {
for _ in 0..self.object(id)?.props.len() {
self.use_fuel()?;
}
let o = self.object_mut(id)?;
let mut doomed: Vec<(u32, Key)> =
o.props.iter().filter_map(|(k, _)| array_index(k).filter(|&i| i >= new).map(|i| (i, k.clone()))).collect();
doomed.sort_unstable_by_key(|d| std::cmp::Reverse(d.0));
for (i, k) in doomed {
if o.props.get(&k).is_some_and(|p| !p.configurable()) {
length = i + 1;
ok = false;
break;
}
o.props.remove(&k);
}
}
if let Class::Array { length: l, length_writable } = &mut self.object_mut(id)?.class {
*l = length;
if read_only {
*length_writable = false;
}
}
Ok(ok)
}
/// The names of an object's own properties (section 15.2.3.4): a String object's indices and
/// `length` first, then the rest in the order they were added, which is the order `for-in`
/// visits them, then an array's `length`. Each name takes a unit of fuel, and the heap for the
/// list is checked first.
pub(crate) fn own_keys(&mut self, id: ObjId) -> JsResult<Vec<Key>> {
let o = self.object(id)?;
let string_len = if let Class::String(s) = &o.class { s.len() } else { 0 };
let is_string = matches!(o.class, Class::String(_));
let is_array = matches!(o.class, Class::Array { .. });
let count = o.props.len().saturating_add(string_len).saturating_add(usize::from(is_string || is_array));
self.reserve_heap(index_keys_bytes(string_len).saturating_add(count.saturating_mul(KEY_ENTRY_BYTES)))?;
let mut out = Vec::with_capacity(count);
for i in 0..string_len {
self.use_fuel()?;
out.push(index_key(u32::try_from(i).unwrap_or(u32::MAX)));
}
if is_string {
out.push(key("length"));
}
for _ in 0..self.object(id)?.props.len() {
self.use_fuel()?;
}
out.extend(self.object(id)?.props.keys());
if is_array {
out.push(key("length"));
}
Ok(out)
}
/// Runs `f` with `values` kept reachable for any collection it starts.
pub(crate) fn rooted<T>(&mut self, values: &[Value], f: impl FnOnce(&mut Self) -> JsResult<T>) -> JsResult<T> {
let mark = self.temp_roots.len();
self.temp_roots.extend(values.iter().cloned());
let r = f(self);
self.temp_roots.truncate(mark);
r
}
fn reject(&mut self, throw: bool, k: &[u16], why: &str) -> JsResult<()> {
if throw {
let message = format!("cannot assign to property '{}': {why}", String::from_utf16_lossy(k));
@@ -1131,32 +1361,19 @@ impl Interp {
}
}
/// Assigning to an array's `length` (section 15.4.5.1).
/// Assigning to an array's `length` (section 15.4.5.1). [[Put]] defines the value, so the
/// value is converted once with ToUint32 and once with ToNumber, as in steps 3.c and 3.d.
fn set_array_length(&mut self, id: ObjId, v: &Value, throw: bool) -> JsResult<()> {
let n = self.to_number(v)?;
let new = to_uint32(n);
if f64::from(new) != n {
let new = to_uint32(self.to_number(v)?);
if f64::from(new) != self.to_number(v)? {
return Err(self.throw(ErrorType::Range, "invalid array length"));
}
let o = self.object(id)?;
let Class::Array { length: old, length_writable } = o.class else { return internal("not an array") };
let Class::Array { length_writable, .. } = self.object(id)?.class else { return internal("not an array") };
if !length_writable {
return self.reject(throw, &key("length"), "it is read-only");
}
if new < old {
// Shrinking visits every property: each takes fuel before anything is removed.
for _ in 0..o.props.len() {
self.use_fuel()?;
}
}
let o = self.object_mut(id)?;
let Class::Array { length, .. } = &mut o.class else { return internal("not an array") };
*length = new;
if new < old {
let doomed: Vec<Key> = o.props.keys().into_iter().filter(|k| array_index(k).is_some_and(|i| i >= new)).collect();
for k in doomed {
o.props.remove(&k);
}
if !self.shrink_array(id, new, false)? {
return self.reject(throw, &key("length"), "an element cannot be deleted");
}
Ok(())
}
@@ -1576,13 +1793,25 @@ impl Interp {
this = args.first().cloned().unwrap_or(Value::Undefined);
args = self.list_from_array_like(&list)?;
}
Func::Bound { target, this: bound_this, args: bound_args } => {
self.use_fuel()?;
let mut all = Vec::with_capacity(bound_args.len().saturating_add(args.len()));
all.extend(bound_args.iter().cloned());
all.append(&mut args);
args = all;
this = bound_this;
f = Value::Object(target);
}
Func::Native { f: native, .. } => {
// A list from `apply` is not on the operand stack, and a native function can run
// JavaScript that collects, so the arguments stay rooted for the call.
// A list from `apply` and the arguments a bound function adds are not on the
// operand stack, and a native function can run JavaScript that collects, so the
// arguments stay rooted for the call.
let mark = self.temp_roots.len();
self.temp_roots.push(this.clone());
self.temp_roots.extend(args.iter().cloned());
let held = self.held_bytes;
let r = native(self, this, &args, false);
self.held_bytes = held;
self.temp_roots.truncate(mark);
return Ok(Prepared::Done(r?));
}
@@ -1660,8 +1889,17 @@ impl Interp {
let n = argc as usize;
let start =
self.stack.len().checked_sub(n + 1).filter(|&s| s >= self.base()).ok_or_else(|| Abrupt::Internal("new below the stack".into()))?;
let f = self.stack.get(start).cloned().unwrap_or(Value::Undefined);
let args: Vec<Value> = self.stack.get(start + 1..).map(<[Value]>::to_vec).unwrap_or_default();
let mut f = self.stack.get(start).cloned().unwrap_or(Value::Undefined);
let mut args: Vec<Value> = self.stack.get(start + 1..).map(<[Value]>::to_vec).unwrap_or_default();
// A bound function constructs its target with its arguments first (section 15.3.4.5.2);
// the bound function on the stack keeps them reachable.
while let Some(Func::Bound { target, args: bound_args, .. }) = self.func_of(&f) {
self.use_fuel()?;
let mut all = bound_args.to_vec();
all.append(&mut args);
args = all;
f = Value::Object(target);
}
match self.func_of(&f) {
Some(Func::Closure { code, env }) => {
let Some(callee) = f.as_object() else { return internal("callee is not an object") };
@@ -1674,7 +1912,10 @@ impl Interp {
self.enter_function(Entry { callee, code, env, this: Value::Object(obj), args }, Some(obj), false)
}
Some(Func::Native { f: native, constructor: true, .. }) => {
let v = native(self, Value::Undefined, &args, true)?;
let held = self.held_bytes;
let r = native(self, Value::Undefined, &args, true);
self.held_bytes = held;
let v = r?;
self.stack.truncate(start);
self.push(v)
}
@@ -2081,7 +2322,7 @@ impl Interp {
let (class, proto) = match v {
Value::Object(o) => return Ok(*o),
Value::Undefined | Value::Null => return Err(self.type_error("cannot convert undefined or null to an object")),
Value::Bool(_) => (Class::Boolean, self.intrinsics.boolean_proto),
Value::Bool(b) => (Class::Boolean(*b), self.intrinsics.boolean_proto),
Value::Number(n) => (Class::Number(*n), self.intrinsics.number_proto),
Value::String(s) => (Class::String(s.clone()), self.intrinsics.string_proto),
};
@@ -2226,7 +2467,13 @@ impl Interp {
if !self.is_callable(f) {
return Err(self.type_error("the right side of 'instanceof' is not a function"));
}
let Some(fid) = f.as_object() else { return Ok(false) };
let Some(mut fid) = f.as_object() else { return Ok(false) };
// A bound function answers for its target (section 15.3.4.5.3).
while let Some(Func::Bound { target, .. }) = self.func_of(&Value::Object(fid)) {
self.use_fuel()?;
fid = target;
}
let f = &Value::Object(fid);
let Value::Object(mut o) = v.clone() else { return Ok(false) };
let Value::Object(proto) = self.get_from(fid, &key("prototype"), f)? else {
return Err(self.type_error("the function's prototype is not an object"));
@@ -2301,6 +2548,107 @@ pub(crate) enum CompileError {
Internal(String),
}
/// A property descriptor (section 8.10): each field is `None` when it is absent. `get` and `set`
/// are `Some(None)` when present as `undefined`.
#[derive(Clone, Default, Debug)]
pub(crate) struct Desc {
pub(crate) value: Option<Value>,
pub(crate) get: Option<Option<ObjId>>,
pub(crate) set: Option<Option<ObjId>>,
pub(crate) writable: Option<bool>,
pub(crate) enumerable: Option<bool>,
pub(crate) configurable: Option<bool>,
}
impl Desc {
/// A data descriptor with every attribute given.
pub(crate) fn data(value: Value, flags: u8) -> Self {
Desc {
value: Some(value),
writable: Some(flags & WRITABLE != 0),
enumerable: Some(flags & ENUMERABLE != 0),
configurable: Some(flags & CONFIGURABLE != 0),
..Desc::default()
}
}
pub(crate) fn is_accessor(&self) -> bool {
self.get.is_some() || self.set.is_some()
}
pub(crate) fn is_data(&self) -> bool {
self.value.is_some() || self.writable.is_some()
}
/// The attribute flags the descriptor gives, each absent one taken from `default`.
fn flags_or(&self, default: u8) -> u8 {
let pick = |field: Option<bool>, bit: u8| match field {
Some(true) => bit,
Some(false) => 0,
None => default & bit,
};
pick(self.writable, WRITABLE) | pick(self.enumerable, ENUMERABLE) | pick(self.configurable, CONFIGURABLE)
}
}
/// SameValue (section 9.12).
pub(crate) fn same_value(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(x), Value::Number(y)) => (x.is_nan() && y.is_nan()) || (x == y && x.is_sign_negative() == y.is_sign_negative()),
_ => strict_eq(a, b),
}
}
/// Steps 5 to 12 of [[DefineOwnProperty]] (section 8.12.9) for an existing property: the
/// property after `desc` is applied, or `None` when the change is not allowed.
fn merge_desc(current: &Prop, desc: &Desc) -> Option<Prop> {
let flags = current.flags();
let configurable = flags & CONFIGURABLE != 0;
if !configurable {
if desc.configurable == Some(true) {
return None;
}
if desc.enumerable.is_some_and(|e| e != (flags & ENUMERABLE != 0)) {
return None;
}
}
let attrs = |base: u8, writable_bit: bool| {
let f = desc.flags_or(base);
if writable_bit { f } else { f & !WRITABLE }
};
match current {
Prop::Data { .. } if desc.is_accessor() => {
if !configurable {
return None;
}
Some(Prop::Accessor { get: desc.get.flatten(), set: desc.set.flatten(), flags: attrs(flags & !WRITABLE, false) })
}
Prop::Accessor { .. } if desc.is_data() => {
if !configurable {
return None;
}
Some(Prop::Data { value: desc.value.clone().unwrap_or(Value::Undefined), flags: attrs(flags & !WRITABLE, true) })
}
Prop::Data { value, .. } => {
if !configurable && flags & WRITABLE == 0 {
if desc.writable == Some(true) {
return None;
}
if desc.value.as_ref().is_some_and(|v| !same_value(v, value)) {
return None;
}
}
Some(Prop::Data { value: desc.value.clone().unwrap_or_else(|| value.clone()), flags: attrs(flags, true) })
}
Prop::Accessor { get, set, .. } => {
if !configurable && (desc.get.is_some_and(|g| g != *get) || desc.set.is_some_and(|s| s != *set)) {
return None;
}
Some(Prop::Accessor { get: desc.get.unwrap_or(*get), set: desc.set.unwrap_or(*set), flags: attrs(flags, false) })
}
}
}
/// The strict equality comparison (section 11.9.6).
pub(crate) fn strict_eq(a: &Value, b: &Value) -> bool {
match (a, b) {
+12 -6
View File
@@ -24,12 +24,16 @@
//! deep recursion in a script does not use the native stack. Objects and environments live in an
//! arena with a mark-and-sweep collector; strings built by repeated `+` are flattened and dropped
//! with loops. The realm provides the language (sections 10 to 14 of ECMA-262 5.1) and these
//! built-ins: `Object` and `Object.prototype`'s basic methods, `Function` with
//! `Function.prototype.call` and `apply`, `eval`, the error constructors, and `String`, `Number`,
//! `RegExp` and `Date` (sections 15.5, 15.7, 15.9 and 15.10). Regular expressions run on a
//! backtracking matcher that keeps its choice points on its own stack and takes fuel for every
//! step; `Date` reads the clock and time zone the host sets with [`Realm::set_clock`] and
//! [`Realm::set_time_zone`]. Every run is bounded
//! built-ins: the global functions and constants (15.1, with Annex B's `escape` and `unescape`),
//! `Object` (15.2), `Function` with `call`, `apply` and `bind` (15.3), `Array` (15.4), `String`
//! (15.5), `Boolean.prototype`'s `toString` and `valueOf` (15.6.4), `Number` (15.7), `Math` (15.8),
//! `Date` (15.9), `RegExp` (15.10), the error types (15.11) and `JSON` (15.12). The array methods
//! visit only the indices an array or array-like object has, so a sparse array of length 2^32-1
//! costs as much as its elements, and `JSON.parse`, `JSON.stringify` and the reviver walk use no
//! native recursion. Regular expressions run on a backtracking matcher that keeps its choice points
//! on its own stack and takes fuel for every step; `Date` reads the clock and time zone the host
//! sets with [`Realm::set_clock`] and [`Realm::set_time_zone`]. The prototype methods are writable
//! and configurable, so scripts can replace them. Every run is bounded
//! by [`Limits`]: instructions run (fuel), heap bytes, frames, nested host calls, and an
//! [`Interrupt`] flag another thread can set. A run that reaches a limit ends with
//! [`RunErrorKind::Limit`], which a script's `try` cannot catch.
@@ -178,6 +182,8 @@ impl LineTable {
}
}
#[cfg(test)]
mod builtins_tests;
#[cfg(test)]
mod interp_tests;
#[cfg(test)]
+6 -1
View File
@@ -170,9 +170,13 @@ impl Realm {
}
/// Runs `source` as a script in the global environment and returns its completion value (the
/// value of the last expression statement that ran).
/// value of the last expression statement that ran). A realm whose built-ins could not be
/// installed runs nothing and gives an internal error.
pub fn run(&mut self, source: &str) -> Result<JsValue, RunError> {
let interp = &mut self.interp;
if let Some(message) = &interp.install_error {
return Err(RunError { kind: RunErrorKind::Internal, message: message.clone(), line: 0, column: 0 });
}
interp.fuel = interp.limits.fuel;
interp.source_units = 0;
interp.throw_site = None;
@@ -188,6 +192,7 @@ impl Realm {
interp.stack.clear();
interp.frames.clear();
interp.temp_roots.clear();
interp.join_stack.clear();
match result {
Ok(v) => Ok(interp.to_js_value(&v)),
Err(Abrupt::Throw(v)) => {
+89
View File
@@ -0,0 +1,89 @@
//! Runs `json2.js`, Douglas Crockford's public-domain JSON polyfill, when `cargo xtask test262` has
//! fetched it (it sets `VECTORCRAFT_JSON2`; without it the test looks in `corpus/json2/` and skips
//! when the file is absent).
//!
//! The built-in `JSON` is removed first, so the polyfill defines its own. Each sample value is
//! written by the polyfill and by the built-in `JSON.stringify` (ECMA-262 5.1 section 15.12.3),
//! which must give the same text, and the text is read back by the polyfill's `parse` and by the
//! built-in `JSON.parse` (section 15.12.2), which must give values that write the same text again.
//! The samples leave out U+2028 and U+2029: the polyfill escapes them, and section 15.12.3 does not
//! escape them.
//!
//! The polyfill uses `Date`, `String.prototype.replace` and regular expression `test`. While the
//! interpreter lacks any of them, the test prints that it skipped and passes.
use std::path::{Path, PathBuf};
use vectorcraft_js::{JsValue, Realm};
fn json2_file() -> Option<PathBuf> {
if let Ok(path) = std::env::var("VECTORCRAFT_JSON2") {
return Some(PathBuf::from(path));
}
let corpus = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/json2");
let mut dirs: Vec<PathBuf> =
std::fs::read_dir(corpus).ok()?.filter_map(|e| e.ok().map(|e| e.path().join("json2.js"))).filter(|p| p.is_file()).collect();
dirs.sort();
dirs.pop()
}
/// Built-ins the polyfill needs.
const NEEDS: &str =
"typeof Date === 'function' && typeof ''.replace === 'function' && typeof RegExp === 'function' && typeof /a/.test === 'function'";
/// Saves the built-in `JSON` and removes it, so the polyfill defines its own.
const BEFORE: &str = "var NATIVE = JSON; delete this.JSON; this.JSON = undefined;";
/// Writes and reads each sample with both implementations; gives the first difference, or "ok".
const ROUND_TRIP: &str = r#"
var POLY = JSON;
var samples = [
0, -1.5, 1e21, "", "plain", "quote \" backslash \\ newline \n tab \t \u0001 é",
true, false, null, [], {}, [1, [2, [3, []]]], {a: 1, b: [true, null, "x"], c: {d: {}}},
[undefined, function () {}], {skip: undefined, f: function () {}, keep: 2}, [NaN, Infinity, -Infinity]
];
var problem = "ok";
for (var i = 0; i < samples.length && problem === "ok"; i++) {
var poly = POLY.stringify(samples[i]);
var native = NATIVE.stringify(samples[i]);
if (poly !== native) {
problem = "sample " + i + ": json2 wrote " + poly + ", the built-in wrote " + native;
} else if (poly !== undefined) {
if (NATIVE.stringify(POLY.parse(native)) !== native) {
problem = "sample " + i + ": json2 read " + native + " differently";
} else if (POLY.stringify(NATIVE.parse(poly)) !== poly) {
problem = "sample " + i + ": the built-in read " + poly + " differently";
}
}
}
var indented = {a: [1, {b: 2}], c: "d"};
if (problem === "ok" && POLY.stringify(indented, null, 2) !== NATIVE.stringify(indented, null, 2)) {
problem = "indented output differs";
}
if (problem === "ok" && POLY.stringify(indented, ["c"]) !== NATIVE.stringify(indented, ["c"])) {
problem = "property list output differs";
}
problem;
"#;
#[test]
fn json2_polyfill_round_trips_with_the_built_in_json() {
let Some(path) = json2_file() else {
println!("json2: SKIPPED (no json2.js; run cargo xtask test262)");
return;
};
let source = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
let mut realm = Realm::new();
match realm.run(NEEDS) {
Ok(JsValue::Bool(true)) => {}
Ok(JsValue::Bool(false)) => {
println!("json2: SKIPPED (the interpreter lacks Date, String.prototype.replace or RegExp, which json2.js uses)");
return;
}
other => panic!("json2: the built-in check failed: {other:?}"),
}
match realm.run(&format!("{BEFORE}\n{source}\n{ROUND_TRIP}")) {
Ok(JsValue::String(s)) if s == "ok" => println!("json2: the polyfill and the built-in JSON agree on every sample"),
other => panic!("json2: {other:?}"),
}
}
+77 -14
View File
@@ -20,10 +20,13 @@
//! `constructor` is the global its `type:` names. Its counts are printed apart from the parse
//! counts, and its failures go to `run-failures.txt`.
//!
//! A third test runs the tests under `test/built-ins/String`, `Number`, `RegExp` and `Date` the same
//! way, counting the tests that name an ES5 section apart from the other tests without a feature
//! tag; its failures go to `builtins-failures.txt`. The interpreter tests fail when the
//! interpreter panics, when a harness or test file cannot be read, or when no ES5 test runs.
//! A third test runs the tests of the ES5 built-ins the same way: those under the folders
//! [`ES5_BUILT_INS`] names that have an `es5id:` (or, under `annexB/`, any edition's id) and no
//! `features:` tag. Its failures go to `built-ins-failures.txt`. A fourth test runs the tests under
//! `test/built-ins/String`, `Number`, `RegExp` and `Date` the same way, counting the tests that name
//! an ES5 section apart from the other tests without a feature tag; its failures go to
//! `builtins-failures.txt`. The interpreter tests fail when the interpreter panics, when a harness
//! or test file cannot be read, or when no ES5 test runs.
use std::collections::HashSet;
use std::path::{Path, PathBuf};
@@ -198,15 +201,37 @@ fn es5_parse_subset() {
);
}
/// Globals and method names the interpreter does not provide yet: a test that names one is
/// skipped.
const MISSING_BUILTINS: &str = "Array String Number Boolean Math JSON Date RegExp parseInt parseFloat isNaN isFinite encodeURI \
decodeURI encodeURIComponent decodeURIComponent escape unescape print $262 $DONE Symbol Proxy Reflect Promise Map Set WeakMap \
WeakSet ArrayBuffer DataView Int8Array Uint8Array Float64Array defineProperty defineProperties getOwnPropertyDescriptor \
getOwnPropertyNames getPrototypeOf create keys freeze isFrozen seal isSealed preventExtensions isExtensible push pop shift unshift \
slice splice concat join reverse sort indexOf lastIndexOf every some forEach map filter reduce reduceRight charAt charCodeAt \
fromCharCode substring substr split replace match search exec test toUpperCase toLowerCase trim toFixed toPrecision toExponential \
bind now isArray compareArray verifyProperty fnGlobalObject";
/// Globals and method names the interpreter does not provide yet: a test that names one, in its
/// own source or in a harness file it includes, is skipped.
const MISSING_BUILTINS: &str = "String Number Boolean Date RegExp print $262 $DONE Symbol Proxy Reflect Promise Map Set WeakMap \
WeakSet ArrayBuffer DataView Int8Array Uint8Array Float64Array charAt charCodeAt fromCharCode substring substr split replace \
match search exec test toUpperCase toLowerCase trim toFixed toPrecision toExponential now";
/// The directories under `test/built-ins/` and `test/annexB/built-ins/` that hold tests of the
/// ES5 built-ins (ECMA-262 5.1 sections 15.1 to 15.12 and Annex B.2).
const ES5_BUILT_INS: &[&str] = &[
"built-ins/Object",
"built-ins/Function",
"built-ins/Array",
"built-ins/Error",
"built-ins/NativeErrors",
"built-ins/Math",
"built-ins/JSON",
"built-ins/global",
"built-ins/Infinity",
"built-ins/NaN",
"built-ins/undefined",
"built-ins/parseInt",
"built-ins/parseFloat",
"built-ins/isNaN",
"built-ins/isFinite",
"built-ins/encodeURI",
"built-ins/encodeURIComponent",
"built-ins/decodeURI",
"built-ins/decodeURIComponent",
"annexB/built-ins/escape",
"annexB/built-ins/unescape",
];
/// The names in [`MISSING_BUILTINS`] that `String`, `Number`, `RegExp` and `Date` provide.
const TEXT_BUILTINS: &str = "String Number Date RegExp charAt charCodeAt fromCharCode substring substr split replace match search exec test \
@@ -310,11 +335,49 @@ fn run_language_subset(dir: &Path) {
}
#[test]
fn es5_text_builtins_run() {
fn es5_built_ins_subset_runs() {
let Some(dir) = suite_dir() else {
println!("test262 built-ins: SKIPPED (no suite; run cargo xtask test262)");
return;
};
let handle = std::thread::Builder::new().stack_size(64 << 20).spawn(move || run_built_ins(&dir)).unwrap();
handle.join().unwrap();
}
/// Runs the tests under the folders [`ES5_BUILT_INS`] names, prints the counts and writes the
/// failures to `built-ins-failures.txt` beside the suite. The Annex B tests name the edition they
/// were written for with `esid:`, so they run without an `es5id:`.
fn run_built_ins(dir: &Path) {
let missing = missing_builtins(&[TEXT_BUILTINS]);
let mut all = RunCounts::default();
for folder in ES5_BUILT_INS {
let root = dir.join("test").join(folder);
assert!(root.is_dir(), "{} is not a test262 checkout", dir.display());
let (es5, later) = run_tests(dir, &root, &missing, folder.starts_with("annexB"));
for counts in [es5, later] {
all.tests += counts.tests;
all.skipped += counts.skipped;
all.passed += counts.passed;
all.failed += counts.failed;
all.panicked += counts.panicked;
all.failures.push_str(&counts.failures);
}
}
assert!(all.tests > 0, "no ES5 built-ins tests ran under {}", dir.display());
let report = dir.join("built-ins-failures.txt");
if let Err(e) = std::fs::write(&report, &all.failures) {
println!("could not write {}: {e}", report.display());
}
println!("test262 ES5 built-ins subset through the interpreter: {}. Failures: {}", all.summary(), report.display());
assert_eq!(all.panicked, 0, "the interpreter panicked; see {}", report.display());
}
#[test]
fn es5_text_builtins_run() {
let Some(dir) = suite_dir() else {
println!("test262 text built-ins: SKIPPED (no suite; run cargo xtask test262)");
return;
};
let handle = std::thread::Builder::new().stack_size(64 << 20).spawn(move || run_text_builtins(&dir)).unwrap();
handle.join().unwrap();
}
+9 -2
View File
@@ -295,11 +295,18 @@ impl Headless {
Ok(out)
}
/// `app.open {path}`: any readable file as a new active document (`document.open`).
/// `app.open {path, importer?, params?}`: any readable file as a new active document
/// (`document.open`, with the importer plug-in `importer` names and its parameters `params`).
pub fn open(&mut self, p: &Value) -> Result<Value, String> {
let path = s(p, "path").ok_or("missing `path`")?;
let mut params = json!({"path": path});
for k in ["importer", "params"] {
if let Some(v) = p.get(k).filter(|v| !v.is_null()) {
params[k] = v.clone();
}
}
let roots = self.roots.clone();
file_access::confine(roots.as_ref(), || self.session.execute("document.open", &json!({"path": path}))).map_err(|e| e.to_string())
file_access::confine(roots.as_ref(), || self.session.execute("document.open", &params)).map_err(|e| e.to_string())
}
/// `app.save {path?, format?, options?}`: `document.save` (by default the document's own path and
+43
View File
@@ -415,3 +415,46 @@ fn document_color_mode_puts_rgb_greys_on_the_black_plate() {
let bad = call_tool(&mut o, "run_command", &json!({"command": "file.documentColorMode", "params": {"mode": "rgb", "grays": "rich"}}));
assert!(bad.is_error, "{bad:?}");
}
/// `open_file`'s `importer` reaches `document.open`: an SVG opens with the SVG reader without it,
/// and naming an importer that isn't installed is an error that names it.
#[test]
fn open_file_passes_the_importer_on() {
let tools = tool_definitions();
let open = tools.iter().find(|t| t["name"] == "open_file").unwrap();
assert!(open["inputSchema"]["properties"].get("importer").is_some(), "{open}");
let svg = tmp("importer.svg");
std::fs::write(&svg, r#"<svg xmlns="http://www.w3.org/2000/svg" width="20" height="10"><rect width="5" height="5"/></svg>"#).unwrap();
let mut h = Headless::new();
assert_eq!(text(&call_tool(&mut h, "open_file", &json!({"path": svg})))["format"], "svg");
let r = call_tool(&mut h, "open_file", &json!({"path": svg, "importer": "plugin.test.mcp.absent"}));
assert!(r.is_error && r.content[0]["text"].as_str().unwrap().contains("test.mcp.absent"), "{r:?}");
assert_eq!(h.session.documents().len(), 1);
}
/// `open_file` sends `app.open` the importer's `params` with the importer.
#[test]
fn open_file_passes_the_importer_s_params_on() {
struct Recording(Vec<(String, Value)>);
impl Backend for Recording {
fn call(&mut self, method: &str, params: Value) -> Result<Value, String> {
self.0.push((method.to_string(), params));
Ok(json!({}))
}
fn has_ui(&self) -> bool {
false
}
fn describe(&self) -> String {
"recording".into()
}
}
let tools = tool_definitions();
let open = tools.iter().find(|t| t["name"] == "open_file").unwrap();
assert_eq!(open["inputSchema"]["properties"]["params"]["type"], "object", "{open}");
let mut b = Recording(vec![]);
let args = json!({"path": "/in/a.tup", "importer": "plugin.test.mcp.params", "params": {"density": 7}});
assert!(!call_tool(&mut b, "open_file", &args).is_error);
let [(method, params)] = &b.0[..] else { panic!("one call: {:?}", b.0) };
assert_eq!(method, "app.open");
assert_eq!(params, &args);
}
+9 -2
View File
@@ -357,10 +357,17 @@ pub fn tool_definitions() -> Vec<Value> {
"open_file",
"Open file",
&format!(
"Open a file as a new, active document: .{} (see run_command document.formats). Templates (.ait, native templates) open as a new untitled document. The reply's `warnings` say what didn't come in as it was (an EPS shown as its preview image says why its PostScript couldn't be read).",
"Open a file as a new, active document: .{} (see run_command document.formats). Templates (.ait, native templates) open as a new untitled document. The reply's `warnings` say what didn't come in as it was (an EPS shown as its preview image says why its PostScript couldn't be read). A file of an extension no built-in format reads opens with the installed importer plug-in that lists it; `importer` names the importer to use (File › Open With), also for an extension a built-in format reads, and `params` gives that importer's parameters.",
OPEN_EXTS.join(", .")
),
obj(json!({"path": string("File path")}), &["path"]),
obj(
json!({
"path": string("File path"),
"importer": string("An importer plug-in's format id, plugin.<id> (see run_command document.formats), which must list the file's extension (default: the built-in format that reads the file, else the first importer by id that lists its extension)"),
"params": {"type": "object", "description": "The importer plug-in's parameters (see run_command plugin.list; default: the defaults its manifest declares)"},
}),
&["path"],
),
false,
),
tool(
+7 -1
View File
@@ -191,7 +191,13 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result<ToolResult, Str
"screenshot" => screenshot(b, a),
"open_file" => {
let path = absolute_path(b, req_str(a, "path")?)?;
j(b.call("app.open", json!({"path": path}))?)
let mut p = json!({"path": path});
for k in ["importer", "params"] {
if let Some(v) = a.get(k).filter(|v| !v.is_null()) {
p[k] = v.clone();
}
}
j(b.call("app.open", p)?)
}
"save_file" => {
let mut p = json!({"path": a.get("path").and_then(Value::as_str).map(|p| absolute_path(b, p)).transpose()?});
+2 -2
View File
@@ -408,8 +408,8 @@ impl<'a> Scene<'a> {
let (cols, rows) = (along(area.width()), along(area.height()));
let pixels: Vec<u8> = if g.gradient.kind == GradientKind::Freeform {
let Some(b) = painted_box(bounds) else { return };
let field = g.freeform_on(b).field_with(spread_scale(b), &|c| c.to_rgb());
field.grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect()
let field = g.freeform_on(b).model_field(spread_scale(b));
field.display_grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect()
} else {
let geom = g.resolve(bounds);
let sample = g.gradient.sampler();
+29
View File
@@ -85,6 +85,35 @@ fn wmf_output_round_trips() {
assert!(near(ellipse.geometric_bounds().unwrap(), Rect::new(100.0, 20.0, 180.0, 80.0), 0.1));
}
#[test]
fn freeform_points_that_share_a_model_are_written_as_their_mix() {
use vectorcraft_color::{Color, Freeform, FreeformPoint, GradientKind};
let mid = |a: Color, b: Color| {
let p = |x, c| FreeformPoint::new(Point::new(x, 50.0), c);
let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() });
g.freeform = Some(Freeform { points: vec![p(20.0, a), p(80.0, b)], ..Default::default() });
let d =
doc_with(100.0, 100.0, vec![path(shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), Paint::Gradient(Box::new(g)), Paint::None, 0.0)]);
let back = import(&emf(&d).bytes).unwrap();
let im = all(&back.document)
.iter()
.find_map(|n| match &n.kind {
NodeKind::Image(im) => Some(im.clone()),
_ => None,
})
.unwrap();
let px = image::load_from_memory(&back.document.images[&im.key].bytes).unwrap().to_rgba8();
px.get_pixel(px.width() / 2, px.height() / 2).0
};
let near = |got: [u8; 4], want: [u8; 4]| got.iter().zip(want).all(|(g, w)| g.abs_diff(w) <= 6);
let got = mid(Color::cmyk(1.0, 0.0, 0.0, 0.0), Color::cmyk(0.0, 0.0, 0.0, 1.0));
let want = Color::cmyk(0.5, 0.0, 0.0, 0.5).to_rgba8(1.0);
assert!(near(got, want), "CMYK: {got:?} vs {want:?}");
let got = mid(Color::lab(90.0, -60.0, 40.0), Color::lab(30.0, 60.0, -40.0));
let want = Color::lab(60.0, 0.0, 0.0).to_rgba8(1.0);
assert!(near(got, want), "Lab: {got:?} vs {want:?}");
}
#[test]
fn dashes_gradients_and_images_round_trip_through_emf() {
let mut line = path(shapes::line((10.0, 90.0).into(), (190.0, 90.0).into()), Paint::None, red(), 2.0);
+26 -11
View File
@@ -450,6 +450,7 @@ impl Exporter<'_> {
/// [`MAX_FIELD_PX`] a side), or a linear or radial gradient (at most [`MAX_GRADIENT_PX`] a side),
/// with dither's noise when it paints a fill (`fill`).
pub(super) fn field_image(&mut self, s: &mut Surface, g: &vectorcraft_color::GradientPaint, bounds: Rect, area: Rect, fill: bool) {
use vectorcraft_color::cms::Model;
use vectorcraft_color::freeform::{painted_box, spread_scale};
let Some(b) = painted_box(bounds) else { return };
let area = area.abs();
@@ -466,18 +467,32 @@ impl Exporter<'_> {
let k = (self.doc.raster_effects_ppi / 72.0).min(max / long);
let along = |len: f64| (len * k).ceil().clamp(1.0, max) as u16;
let (cols, rows) = (along(w), along(h));
let rgba: Vec<u8> = if freeform {
let field = g.freeform_on(b).field_with(spread_scale(b), &|c| c.to_rgb());
field.grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect()
} else {
self.warn("linear and radial gradients drawn as pixels are exported as images");
g.pixels(b, area, cols, rows, fill, &|c| c.to_rgb()).flatten().collect()
let field = freeform.then(|| g.freeform_on(b).model_field(spread_scale(b)));
let cmyk_doc = self.doc.color_mode == vectorcraft_doc::ColorMode::Cmyk;
let img = match &field {
Some(field) if field.model() == Some(Model::Cmyk) || (cmyk_doc && field.model() == Some(Model::Gray)) => {
let (mut inks, mut alpha) = (Vec::new(), Vec::new());
for (color, a) in field.grid(area, cols, rows) {
let [c, m, y, k] = color.to_cmyk();
inks.extend([q(c), q(m), q(y), q(k)]);
alpha.push(q(a));
}
let convert = if self.convert { self.out.cmyk_pixels() } else { None };
crate::images::from_inks(inks, alpha, cols.into(), rows.into(), self.interpolate, convert)
}
_ => {
let rgba: Vec<u8> = match &field {
Some(field) => field.display_grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect(),
None => {
self.warn("linear and radial gradients drawn as pixels are exported as images");
g.pixels(b, area, cols, rows, fill, &|c| c.to_rgb()).flatten().collect()
}
};
let pixels = if self.convert { self.out.pixels() } else { None };
crate::images::from_rgba_in(rgba, cols.into(), rows.into(), self.interpolate, pixels)
}
};
let pixels = if self.convert { self.out.pixels() } else { None };
let (Some(img), Some(size)) = (
crate::images::from_rgba_in(rgba, cols.into(), rows.into(), self.interpolate, pixels),
Size::from_wh(area.width() as f32, area.height() as f32),
) else {
let (Some(img), Some(size)) = (img, Size::from_wh(area.width() as f32, area.height() as f32)) else {
return;
};
s.push_transform(&xf(Affine::translate(area.origin().to_vec2())));
+16
View File
@@ -101,6 +101,22 @@ pub(crate) fn from_rgba_in(mut rgba: Vec<u8>, width: u32, height: u32, interpola
from_rgba(&rgba, width, height, interpolate, pixels)
}
/// A `width` × `height` image of CMYK ink amounts `inks` (four bytes a pixel), converted by
/// `convert` (when given), with straight `alpha` (one byte a pixel) carried only when some pixel
/// isn't opaque. `None` when the buffers don't hold that many pixels.
pub(crate) fn from_inks(inks: Vec<u8>, alpha: Vec<u8>, width: u32, height: u32, interpolate: bool, convert: Option<&CmykPixels>) -> Option<Image> {
let pixels = u64::from(width) * u64::from(height);
if pixels == 0 || inks.len() as u64 != pixels * 4 || alpha.len() as u64 != pixels {
return None;
}
let alpha = alpha.iter().any(|a| *a < 255).then_some(alpha);
let (samples, space) = match convert {
Some(p) => p.convert(&inks),
None => (inks, ImageColorspace::Cmyk),
};
lossless(samples, alpha, space, width, height, interpolate)
}
/// The kinds of images the Compression section has settings for.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Kind {
+72 -2
View File
@@ -8,9 +8,10 @@ use craft_pdf::syntax::Pdf;
use craft_pdf::syntax::object::{Array, Name, Object, Stream};
use serde_json::json;
use vectorcraft_color::cms::{self, DEVICE_CMYK, GENERIC_CMYK, SRGB};
use vectorcraft_color::{Color, Freeform, FreeformPoint, Gradient, GradientKind, GradientPaint, Paint};
use vectorcraft_doc::cmyk::Inks;
use vectorcraft_doc::{ColorMode, Document, ImageBlob, ImageObject, Node, NodeKind};
use vectorcraft_geom::Affine;
use vectorcraft_doc::{Appearance, ColorMode, Document, ImageBlob, ImageObject, Node, NodeKind};
use vectorcraft_geom::{Affine, Point, Rect, shapes};
use crate::*;
@@ -471,6 +472,75 @@ fn a_pixelated_cmyk_image_with_its_grid_mask_asks_viewers_not_to_smooth_it() {
}
}
/// A 100 × 100 pt document of `mode` whose square has a freeform fill of `a` on the left and `b`
/// on the right, with those points at `opacity`.
fn freeform_doc(mode: ColorMode, a: Color, b: Color, opacity: f32) -> Document {
let p = |x, c| FreeformPoint { opacity, ..FreeformPoint::new(Point::new(x, 50.0), c) };
let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() });
g.freeform = Some(Freeform { points: vec![p(20.0, a), p(80.0, b)], ..Default::default() });
let mut d = Document::new_with_mode(100.0, 100.0, mode);
let square = Node::path(
d.alloc_id(),
shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)),
Appearance::basic(Paint::Gradient(Box::new(g)), Paint::None, 0.0),
);
let l = d.layers[0].id;
d.insert(Some(l), 0, square).unwrap();
d
}
#[test]
fn freeform_gradients_of_cmyk_points_are_written_as_cmyk_images() {
let (cyan, black) = (Color::cmyk(1.0, 0.0, 0.0, 0.0), Color::cmyk(0.0, 0.0, 0.0, 1.0));
for mode in [ColorMode::Cmyk, ColorMode::Rgb] {
let d = freeform_doc(mode, cyan, black, 1.0);
let out = export_with_report(&d, &opts(json!({}))).unwrap();
let w = written(&out.bytes);
assert_eq!(w.len(), 1, "{mode:?}: opaque, no soft mask: {w:?}");
assert_eq!(w[0].space, "DeviceCMYK", "{mode:?}");
let back = import(&out.bytes).unwrap();
let inks = all_images(&back)[0].cmyk().unwrap();
let at = (inks.height as usize / 2 * inks.width as usize + inks.width as usize / 2) * 4;
assert!(close(&inks.data[at..at + 4], &[128, 0, 0, 128], 6), "{mode:?}: {:?}", &inks.data[at..at + 4]);
}
// Opacity goes in a soft mask, and the inks stay.
let w = written(&export_with_report(&freeform_doc(ColorMode::Cmyk, cyan, black, 0.5), &opts(json!({}))).unwrap().bytes);
assert_eq!(w.len(), 2, "the image and its soft mask: {w:?}");
assert!(w.iter().any(|i| i.space == "DeviceCMYK") && w.iter().any(|i| i.space == "DeviceGray"), "{w:?}");
// The Output settings convert the inks like any CMYK image's.
let d = freeform_doc(ColorMode::Cmyk, cyan, black, 1.0);
let w = written(&export_with_report(&d, &opts(json!({"output": {"conversion": "destination", "destination": SRGB}}))).unwrap().bytes);
assert_eq!(w.len(), 1, "{w:?}");
assert_ne!(w[0].space, "DeviceCMYK", "converted to RGB");
}
#[test]
fn freeform_gradients_of_lab_points_are_written_as_their_lab_mix() {
let d = freeform_doc(ColorMode::Rgb, Color::lab(90.0, -60.0, 40.0), Color::lab(30.0, 60.0, -40.0), 1.0);
let out = export_with_report(&d, &opts(json!({}))).unwrap();
assert_eq!(written(&out.bytes)[0].space, "DeviceRGB");
let back = import(&out.bytes).unwrap();
let px = image::load_from_memory(&all_images(&back)[0].bytes).unwrap().to_rgba8();
let got = px.get_pixel(px.width() / 2, px.height() / 2).0;
let want = Color::lab(60.0, 0.0, 0.0).to_rgba8(1.0);
assert!(close(&got, &want, 6), "{got:?} vs {want:?}");
}
#[test]
fn gray_freeform_points_are_written_as_black_ink_in_a_cmyk_document() {
let (a, b) = (Color::gray(0.2), Color::gray(0.8));
let out = export_with_report(&freeform_doc(ColorMode::Cmyk, a, b, 1.0), &opts(json!({}))).unwrap();
let w = written(&out.bytes);
assert_eq!(w[0].space, "DeviceCMYK", "{w:?}");
let back = import(&out.bytes).unwrap();
let inks = all_images(&back)[0].cmyk().unwrap();
let at = (inks.height as usize / 2 * inks.width as usize + inks.width as usize / 2) * 4;
assert!(close(&inks.data[at..at + 4], &[0, 0, 0, 128], 6), "{:?}", &inks.data[at..at + 4]);
// An RGB document writes them as an image in RGB.
let w = written(&export_with_report(&freeform_doc(ColorMode::Rgb, a, b, 1.0), &opts(json!({}))).unwrap().bytes);
assert_ne!(w[0].space, "DeviceCMYK", "{w:?}");
}
/// #2950: Press Quality converts to the document's profile with its numbers kept, so in a CMYK
/// document an RGB image comes out CMYK, as the press needs; with no conversion (the default
/// preset) it stays RGB, as before.
+3 -2
View File
@@ -75,7 +75,8 @@ pub struct Target<'a> {
/// The limits of an export by `plugin`: its own, with the budgets of an export.
pub fn limits(plugin: &Plugin) -> Limits {
Limits { fuel_base: FUEL, wall_time_ms: WALL_TIME_MS, ..plugin.limits().clone() }
let own = plugin.limits();
Limits { fuel_base: own.export_fuel, wall_time_ms: own.export_wall_time_ms, ..own.clone() }
}
/// Runs exporter `plugin` on `document`, the native file's JSON, with `params` (checked against
@@ -83,7 +84,7 @@ pub fn limits(plugin: &Plugin) -> Limits {
pub fn run(plugin: &Plugin, document: &[u8], params: &Value, target: Target) -> Result<Exported> {
let m = plugin.manifest();
if m.kind != Kind::Exporter {
return Err(Error::Params(format!("`{}` is a {}, not an exporter", m.id, m.kind.name())));
return Err(Error::Params(format!("`{}` is a plug-in of kind `{}`, not an exporter", m.id, m.kind.name())));
}
let mut params = Value::Object(m.resolve_params(params)?);
params["_context"] = json!({"mode": "export", "extension": target.extension, "name": target.name});
+184
View File
@@ -0,0 +1,184 @@
//! Importer plug-ins: a plug-in of kind `importer` reads files in a format of its own, such as a
//! pen plotter's or a cutting machine's, into a new document.
//!
//! File › Open opens a file with an importer only when no built-in format reads it: the built-in
//! formats are found by the file's content first and then by its extension, and an importer is
//! picked by an extension it lists. When several importers list the extension, the one whose id
//! sorts first opens it ([`for_extension`]). File › Open With and `document.open {importer}` pick an
//! importer by its format id (`plugin.<id>`), also for an extension a built-in format reads.
//!
//! The host hands the importer the file's bytes as they are, at most [`MAX_INPUT_BYTES`] of them,
//! and reads back the objects of the new document in the same form filters return them
//! ([`crate::objects`]): `{"objects": [...], "warnings"?: ["…"]}`, or `{"error": "…"}` to decline.
//! An import is one run the user asked for, so it gets an export's budget ([`crate::export::FUEL`]
//! and [`crate::export::WALL_TIME_MS`]), and Cancel stops it between two slices of the budget
//! ([`Error::Cancelled`]).
use std::sync::Arc;
use serde_json::{Value, json};
use crate::objects::{OutObject, declined, printable};
use crate::{Error, Kind, Limits, Plugin, Result, registry};
/// The largest file an importer is given, in bytes: 128 MiB, a quarter of a plug-in's memory
/// limit, so the file and the objects made from it fit beside each other.
pub const MAX_INPUT_BYTES: usize = 128 << 20;
/// The keys an importer's output may have.
const OUTPUT_KEYS: &[&str] = &["objects", "warnings", "error"];
/// The installed importers, by plug-in id.
pub fn installed() -> Vec<Arc<Plugin>> {
registry::list().into_iter().filter(|p| p.manifest().kind == Kind::Importer).collect()
}
/// The installed importer of format id `format_id` (`plugin.<id>`).
pub fn by_format(format_id: &str) -> Option<Arc<Plugin>> {
registry::get(crate::export::plugin_id(format_id)?).filter(|p| p.manifest().kind == Kind::Importer)
}
/// The installed importer `importer` names: its format id (`plugin.<id>`) or its plug-in id. A
/// plug-in of another kind, and one that isn't installed, are errors that say so.
pub fn named(importer: &str) -> Result<Arc<Plugin>> {
let id = crate::export::plugin_id(importer).unwrap_or(importer);
let plugin = registry::get(id).ok_or_else(|| Error::NotFound(id.into()))?;
match plugin.manifest().kind {
Kind::Importer => Ok(plugin),
_ => Err(not_importer(plugin.manifest())),
}
}
/// The error of plug-in `m`, which is not an importer, asked to import.
fn not_importer(m: &crate::Manifest) -> Error {
Error::Params(format!("`{}` is a plug-in of kind `{}`, not an importer", m.id, m.kind.name()))
}
/// The installed importers that list extension `ext` (any case, leading dot allowed), by plug-in
/// id: the first is the one File › Open uses.
pub fn for_extension(ext: &str) -> Vec<Arc<Plugin>> {
let ext = ext.trim_start_matches('.').to_ascii_lowercase();
installed().into_iter().filter(|p| p.manifest().extensions.contains(&ext)).collect()
}
/// What an importer returned.
#[derive(Clone, Debug, PartialEq)]
pub struct Imported {
/// The objects of the new document, in paint order (bottom first), checked as a filter's are.
pub objects: Vec<OutObject>,
/// What the import left out or changed, for the user (at most
/// [`crate::export::MAX_WARNINGS`]).
pub warnings: Vec<String>,
}
/// The file an import reads, as the importer is told in `_context`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Source<'a> {
/// Its extension, without the dot: one the importer lists.
pub extension: &'a str,
/// Its name without the folder and extension.
pub name: &'a str,
}
/// The error of a file larger than [`MAX_INPUT_BYTES`], which no importer is given.
pub fn too_large() -> Error {
Error::Io(format!("the file is larger than the {MAX_INPUT_BYTES} bytes an importer reads"))
}
/// The limits of an import by `plugin`: its own, with the budgets of an export.
pub fn limits(plugin: &Plugin) -> Limits {
crate::export::limits(plugin)
}
/// Runs importer `plugin` on `file`, the bytes of the file being opened, with `params` (checked
/// against its manifest: missing ones take their defaults) for the file `source`.
pub fn run(plugin: &Plugin, file: &[u8], params: &Value, source: Source) -> Result<Imported> {
let m = plugin.manifest();
if m.kind != Kind::Importer {
return Err(not_importer(m));
}
if file.len() > MAX_INPUT_BYTES {
return Err(too_large());
}
let mut params = Value::Object(m.resolve_params(params)?);
params["_context"] = json!({"mode": "import", "extension": source.extension, "name": source.name});
let params = serde_json::to_vec(&params).map_err(|e| Error::Params(e.to_string()))?;
decode(&plugin.run_limited(file, &params, &limits(plugin))?)
}
/// An importer's output: `{"objects": [...], "warnings"?: ["…"]}`, or `{"error": "…"}` to decline
/// with a message for the user. The objects are new ones (no `id`), checked as a filter's are; an
/// output without any, or with keys of its own, is refused.
pub fn decode(bytes: &[u8]) -> Result<Imported> {
let v: Value = serde_json::from_slice(bytes).map_err(|e| Error::Output(format!("not JSON: {e}")))?;
if let Some(e) = declined(&v) {
return Err(e);
}
if let Some(key) = v.as_object().and_then(|o| o.keys().find(|k| !OUTPUT_KEYS.contains(&k.as_str()))) {
let key = printable(&json!(key), 64);
return Err(Error::Output(format!("an importer returns `objects` and `warnings`, not `{key}`")));
}
let objects = crate::objects::decode(bytes)?;
if objects.is_empty() {
return Err(Error::Output("the importer returned no objects".into()));
}
let warnings = match v.get("warnings") {
None | Some(Value::Null) => Vec::new(),
Some(Value::Array(w)) => w.iter().take(crate::export::MAX_WARNINGS).map(|w| printable(w, crate::export::MAX_WARNING_CHARS)).collect(),
Some(_) => return Err(Error::Output("`warnings` must be a list of strings".into())),
};
Ok(Imported { objects, warnings })
}
#[cfg(test)]
mod tests {
use super::*;
const SQUARE: &str = r#"{"type":"path","path":{"subpaths":[{"closed":true,"anchors":[{"p":[0,0]},{"p":[10,0]},{"p":[10,10]}]}]}}"#;
#[test]
fn output_is_the_objects_and_the_warnings_or_a_decline() {
let out = decode(format!(r#"{{"objects":[{SQUARE}],"warnings":["a",2]}}"#).as_bytes()).unwrap();
assert_eq!((out.objects.len(), out.warnings), (1, vec!["a".to_string(), "2".to_string()]));
assert!(out.objects.iter().all(|o| o.id.is_none() && o.path.is_some()));
assert_eq!(decode(format!(r#"{{"objects":[{SQUARE}]}}"#).as_bytes()).unwrap().warnings, Vec::<String>::new());
assert_eq!(decode(br#"{"error": "Not a\nplot file."}"#), Err(Error::Failed("Not aplot file.".into())));
// A decline is reported as one, also with keys the host doesn't read beside it.
assert_eq!(decode(br#"{"error": "No.", "layers": []}"#), Err(Error::Failed("No.".into())));
}
/// Nothing an importer returns reaches a document unchecked: malformed JSON, no objects, keys
/// the host doesn't read, bad warnings and bad objects (coordinates out of range, unknown paints)
/// are all errors.
#[test]
fn bad_output_is_refused() {
let far = r#"{"objects":[{"path":{"subpaths":[{"closed":false,"anchors":[{"p":[0,0]},{"p":[1e9,0]}]}]}}]}"#;
for (bad, says) in [
(&b"[]"[..], "objects"),
(b"{}", "objects"),
(b"not json", "not JSON"),
(br#"{"objects":[]}"#, "no objects"),
(br#"{"objects":{}}"#, "objects"),
(br#"{"objects":[1]}"#, "JSON object"),
(far.as_bytes(), "out of range"),
(br#"{"objects":[{"path":{"subpaths":[]},"fills":[{"type":"hatch"}]}]}"#, "objects[0]"),
(br#"{"objects":[],"artboard":[0,0,1,1]}"#, "not `artboard`"),
] {
let e = decode(bad).unwrap_err().to_string();
assert!(e.contains(says), "{}: {e}", String::from_utf8_lossy(bad));
}
let warns = format!(r#"{{"objects":[{SQUARE}],"warnings":"x"}}"#);
assert!(decode(warns.as_bytes()).unwrap_err().to_string().contains("`warnings`"));
}
#[test]
fn warnings_are_capped_in_number_and_length() {
let many: Vec<String> = (0..crate::export::MAX_WARNINGS + 5).map(|i| format!("w{i}")).collect();
let square: Value = serde_json::from_str(SQUARE).unwrap();
let out = decode(json!({"objects": [square], "warnings": many}).to_string().as_bytes()).unwrap();
assert_eq!(out.warnings.len(), crate::export::MAX_WARNINGS);
let long = "x".repeat(crate::export::MAX_WARNING_CHARS + 1);
let out = decode(json!({"objects": [square], "warnings": [long]}).to_string().as_bytes()).unwrap();
assert_eq!(out.warnings.first().map(|w| w.chars().count()), Some(crate::export::MAX_WARNING_CHARS));
}
}
+4 -2
View File
@@ -22,7 +22,8 @@
//! the objects to keep, change or add, applied to the document as one edit
//! ([`objects::apply_output`]); a **live effect** ([`effect`]) rewrites one object's geometry each
//! time it is drawn, from an appearance stack; an **exporter** ([`export`]) writes the whole
//! document in a file format of its own. The process-wide [`registry`] holds the installed
//! document in a file format of its own; an **importer** ([`import`]) reads a file in a format of
//! its own into a new document. The process-wide [`registry`] holds the installed
//! plug-ins; in [`safe_mode`] none of them loads or runs. A plug-in's parameters ([`manifest`]) come with what the host's dialogs need to show
//! them well: labels, units, groups, help and conditions ([`dialog`]). What a plug-in may do is
//! the set of [`permissions`] its manifest lists, which the user approves when it is installed. A
@@ -34,6 +35,7 @@ pub mod dialog;
pub mod effect;
pub mod export;
pub mod host;
pub mod import;
pub mod manifest;
pub mod objects;
pub mod permissions;
@@ -49,7 +51,7 @@ pub use manifest::{Budget, Command, Kind, Manifest, Param, ParamSpec};
pub use runtime::{ABI_VERSION, HEAVY_FUEL, HEAVY_WALL_TIME_MS, Limits, Plugin, heavy_terms};
/// What the host puts before a plug-in's id to name what it adds: its live effect
/// (`plugin.<id>`, [`effect`]) or its file format ([`export`]).
/// (`plugin.<id>`, [`effect`]) or its file format ([`export`], [`import`]).
pub const PREFIX: &str = "plugin.";
/// A plug-in error. Every way a module can misbehave ends here, never in a panic.
+43 -11
View File
@@ -18,7 +18,7 @@ pub const MAX_UNGROUPED: usize = 64;
pub const MAX_TEXT: usize = 4_096;
/// Most numbers a `numberList` parameter may hold.
pub const MAX_LIST: usize = 256;
/// Most file extensions an exporter may declare.
/// Most file extensions an exporter or an importer may declare.
pub const MAX_EXTENSIONS: usize = 8;
/// Most menu commands a plug-in may add (`commands`).
pub const MAX_COMMANDS: usize = 32;
@@ -38,6 +38,9 @@ pub enum Kind {
/// An exporter: writes the document in a file format of its own, whose extensions the manifest
/// lists (File › Export › Export As, `document.export`; see [`crate::export`]).
Exporter,
/// An importer: reads a file in a format of its own, whose extensions the manifest lists, into
/// a new document (File › Open, `document.open`; see [`crate::import`]).
Importer,
}
impl Kind {
@@ -46,6 +49,7 @@ impl Kind {
Kind::Filter => "filter",
Kind::Effect => "effect",
Kind::Exporter => "exporter",
Kind::Importer => "importer",
}
}
}
@@ -281,8 +285,8 @@ pub struct Manifest {
/// Parameters in declaration (dialog) order; a JSON object `{"name": spec, …}` in the manifest.
#[serde(default, deserialize_with = "ordered_params", serialize_with = "params_as_object")]
pub params: Vec<(String, Param)>,
/// An exporter's file extensions, lower case and without the dot; the first is the one its
/// files get. Only exporters declare them.
/// An exporter's or an importer's file extensions, lower case and without the dot; the first is
/// the one an exporter's files get. Only exporters and importers declare them.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub extensions: Vec<String>,
/// How much a run may compute: only a filter may be [`Budget::Heavy`].
@@ -367,6 +371,9 @@ impl Manifest {
(Budget::Heavy, Kind::Exporter) => {
return Err(Error::Manifest("an exporter always gets an export's budget: leave `budget` out".into()));
}
(Budget::Heavy, Kind::Importer) => {
return Err(Error::Manifest("an importer always gets an import's budget: leave `budget` out".into()));
}
}
self.validate_params()?;
self.validate_commands()
@@ -394,7 +401,7 @@ impl Manifest {
if self.commands.is_empty() {
return Ok(());
}
if self.kind == Kind::Exporter {
if matches!(self.kind, Kind::Exporter | Kind::Importer) {
return Err(Error::Manifest("only a filter or a live effect declares `commands`".into()));
}
if self.budget == Budget::Heavy {
@@ -428,17 +435,17 @@ impl Manifest {
Ok(())
}
/// An exporter lists 1 to [`MAX_EXTENSIONS`] distinct extensions of 1 to 16 lower-case
/// letters and digits; other kinds list none.
/// An exporter or an importer lists 1 to [`MAX_EXTENSIONS`] distinct extensions of 1 to 16
/// lower-case letters and digits; other kinds list none.
fn validate_extensions(&self) -> Result<()> {
let exts = &self.extensions;
let count_ok = !exts.is_empty() && exts.len() <= MAX_EXTENSIONS;
match self.kind {
Kind::Exporter => {}
Kind::Exporter if !count_ok => return Err(Error::Manifest(format!("an exporter declares 1 to {MAX_EXTENSIONS} `extensions`"))),
Kind::Importer if !count_ok => return Err(Error::Manifest(format!("an importer declares 1 to {MAX_EXTENSIONS} `extensions`"))),
Kind::Exporter | Kind::Importer => {}
_ if exts.is_empty() => return Ok(()),
_ => return Err(Error::Manifest("only an exporter declares `extensions`".into())),
}
if exts.is_empty() || exts.len() > MAX_EXTENSIONS {
return Err(Error::Manifest(format!("an exporter declares 1 to {MAX_EXTENSIONS} `extensions`")));
_ => return Err(Error::Manifest("only an exporter or an importer declares `extensions`".into())),
}
for (i, e) in exts.iter().enumerate() {
if !(1..=16).contains(&e.len()) || !e.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit()) {
@@ -819,6 +826,31 @@ mod tests {
}
}
/// An importer lists the extensions it reads by the exporter's rules, and gets an import's
/// budget and the `import.read` permission.
#[test]
fn an_importer_lists_its_extensions() {
let m = Manifest::parse(br#"{"id":"org.example.plotter","name":"Plotter","kind":"importer","extensions":["plt","hpgl"]}"#).unwrap();
assert_eq!((m.kind, m.extensions.as_slice()), (Kind::Importer, &["plt".to_string(), "hpgl".to_string()][..]));
assert_eq!(serde_json::to_value(&m).unwrap()["kind"], json!("importer"));
assert_eq!(m.permissions(), [Permission::ImportRead]);
for (bad, says) in [
(r#"{"id":"a","name":"x","kind":"importer"}"#, "an importer declares 1 to 8"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":[]}"#, "an importer declares 1 to 8"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":["a","b","c","d","e","f","g","h","i"]}"#, "an importer declares 1 to 8"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":["Plt"]}"#, "lower-case"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":[".plt"]}"#, "without the dot"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":["abcdefghijklmnopq"]}"#, "1-16"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":["plt","plt"]}"#, "listed twice"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":["plt"],"budget":"heavy"}"#, "an import's budget"),
(r#"{"id":"a","name":"x","kind":"importer","extensions":["plt"],"permissions":["doc.read"]}"#, "doesn't apply"),
(r#"{"id":"a","name":"x","kind":"filter","extensions":["plt"]}"#, "only an exporter or an importer"),
] {
let e = Manifest::parse(bad.as_bytes()).unwrap_err().to_string();
assert!(e.contains(says), "{bad}: {e}");
}
}
/// A filter may ask for a heavy budget; a live effect runs while the document is drawn and an
/// exporter has an export's budget, so they can't. Light, the default, isn't written back.
#[test]
+62 -14
View File
@@ -53,6 +53,9 @@ pub enum Permission {
/// Write the document as a file in its own format (an exporter).
#[serde(rename = "export.write")]
ExportWrite,
/// Read a file the user opens and make a new document from it (an importer).
#[serde(rename = "import.read")]
ImportRead,
/// Report how far a run has got, see that the user asked it to stop and write log lines: the
/// host imports of API level 2 ([`crate::host::IMPORTS`]).
#[serde(rename = "progress")]
@@ -66,12 +69,13 @@ pub enum Permission {
}
/// Every permission this host grants, in the order prompts list them.
pub const ALL: [Permission; 8] = [
pub const ALL: [Permission; 9] = [
Permission::DocRead,
Permission::DocWrite,
Permission::State,
Permission::EffectRender,
Permission::ExportWrite,
Permission::ImportRead,
Permission::Progress,
Permission::Settings,
Permission::Cache,
@@ -90,6 +94,7 @@ impl Permission {
Permission::State => "state",
Permission::EffectRender => "effect.render",
Permission::ExportWrite => "export.write",
Permission::ImportRead => "import.read",
Permission::Progress => "progress",
Permission::Settings => "settings",
Permission::Cache => "cache",
@@ -109,6 +114,7 @@ impl Permission {
Permission::State => "Keep data of its own with the document",
Permission::EffectRender => "Change how the objects its effect is applied to are drawn",
Permission::ExportWrite => "Write the document as a file in its own format",
Permission::ImportRead => "Read files you open in its format and make documents from them",
Permission::Progress => "Show its progress, see Cancel and write log lines",
Permission::Settings => "Keep settings of its own",
Permission::Cache => "Keep a cache of its own, which you can clear",
@@ -128,6 +134,7 @@ impl Permission {
Kind::Filter => &[Permission::DocRead, Permission::DocWrite, Permission::State],
Kind::Effect => &[Permission::DocRead, Permission::EffectRender],
Kind::Exporter => &[Permission::DocRead, Permission::ExportWrite],
Kind::Importer => &[Permission::ImportRead],
}
}
@@ -143,12 +150,12 @@ impl Permission {
}
/// The permissions a plug-in of `kind` can't work without, which its list must hold: a live
/// effect and an exporter are always handed objects, and only draw or write. A filter may
/// leave any of its permissions out.
/// effect and an exporter are always handed objects, and only draw or write; an importer is
/// always handed the file being opened. A filter may leave any of its permissions out.
fn required(kind: Kind) -> &'static [Permission] {
match kind {
Kind::Filter => &[],
Kind::Effect | Kind::Exporter => Permission::implied(kind),
Kind::Effect | Kind::Exporter | Kind::Importer => Permission::implied(kind),
}
}
}
@@ -234,6 +241,11 @@ pub fn sha256_hex(bytes: &[u8]) -> String {
Sha256::digest(bytes).iter().map(|b| format!("{b:02x}")).collect()
}
/// Is `s` a SHA-256 as [`sha256_hex`] writes it: 64 lower-case hexadecimal digits?
fn is_sha256_hex(s: &str) -> bool {
s.len() == 64 && s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}
/// What the user approved for a plug-in: its id, the SHA-256 of the module they approved
/// ([`sha256_hex`]) and the permissions it was granted.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -244,9 +256,10 @@ pub struct Grant {
}
impl Grant {
/// Does it grant every one of `wanted`?
pub fn covers(&self, wanted: &[Permission]) -> bool {
wanted.iter().all(|w| self.permissions.contains(w))
/// Does it grant every one of `wanted` to the module with this `id` and SHA-256 `hash`? A grant
/// whose own hash is not a valid SHA-256 ([`sha256_hex`]) covers nothing.
pub fn covers(&self, id: &str, hash: &str, wanted: &[Permission]) -> bool {
self.id == id && is_sha256_hex(&self.hash) && self.hash == hash && wanted.iter().all(|w| self.permissions.contains(w))
}
}
@@ -271,12 +284,16 @@ mod tests {
assert_eq!(validate(Kind::Filter, false, &[]).unwrap(), []);
assert!(validate(Kind::Effect, false, &list(&["doc.read", "effect.render"])).is_ok());
assert!(validate(Kind::Exporter, false, &list(&["export.write", "doc.read"])).is_ok());
assert_eq!(validate(Kind::Importer, false, &list(&["import.read"])).unwrap(), [Permission::ImportRead]);
// Any kind may list `progress`, which no kind implies.
assert_eq!(validate(Kind::Filter, false, &list(&["progress", "doc.read"])).unwrap(), [Permission::DocRead, Permission::Progress]);
assert!(validate(Kind::Effect, false, &list(&["doc.read", "effect.render", "progress"])).is_ok());
assert!(validate(Kind::Effect, true, &list(&["doc.read", "effect.render", "progress"])).is_ok());
assert!(validate(Kind::Exporter, false, &list(&["progress", "export.write", "doc.read"])).is_ok());
assert!([Kind::Filter, Kind::Effect, Kind::Exporter].iter().all(|k| !Permission::implied(*k).contains(&Permission::Progress)));
assert_eq!(validate(Kind::Importer, false, &list(&["progress", "import.read"])).unwrap(), [Permission::ImportRead, Permission::Progress]);
assert!(
[Kind::Filter, Kind::Effect, Kind::Exporter, Kind::Importer].iter().all(|k| !Permission::implied(*k).contains(&Permission::Progress))
);
// So may `settings` and `cache`, which no kind implies either.
assert_eq!(
validate(Kind::Effect, false, &list(&["cache", "settings", "doc.read", "effect.render"])).unwrap(),
@@ -284,7 +301,7 @@ mod tests {
);
assert!(validate(Kind::Exporter, false, &list(&["settings", "export.write", "doc.read"])).is_ok());
assert_eq!(validate(Kind::Filter, false, &list(&["cache"])).unwrap(), [Permission::Cache]);
for kind in [Kind::Filter, Kind::Effect, Kind::Exporter] {
for kind in [Kind::Filter, Kind::Effect, Kind::Exporter, Kind::Importer] {
for commands in [false, true] {
assert!(HOST.iter().all(|p| p.applies(kind, commands) && !Permission::implied(kind).contains(p)));
}
@@ -310,6 +327,10 @@ mod tests {
(Kind::Exporter, false, &["export.write"], "needs the permission `doc.read`"),
(Kind::Filter, false, &["cache", "cache"], "`cache` is listed twice"),
(Kind::Filter, false, &["storage"], "unknown permission `storage`"),
(Kind::Importer, false, &["import.read", "doc.read"], "`doc.read` doesn't apply to a plug-in of kind `importer`"),
(Kind::Importer, true, &["import.read", "doc.write"], "`doc.write` doesn't apply to a plug-in of kind `importer`"),
(Kind::Importer, false, &[], "needs the permission `import.read`"),
(Kind::Exporter, false, &["export.write", "doc.read", "import.read"], "`import.read` doesn't apply to a plug-in of kind `exporter`"),
] {
let e = validate(kind, commands, &list(names)).unwrap_err().to_string();
assert!(e.contains(says), "{names:?}: {e}");
@@ -342,10 +363,37 @@ mod tests {
#[test]
fn a_grant_covers_its_permissions_and_no_more() {
let g = Grant { id: "a".into(), hash: String::new(), permissions: vec![Permission::DocRead, Permission::DocWrite] };
assert!(g.covers(&[Permission::DocRead]));
assert!(g.covers(&[]));
assert!(!g.covers(&[Permission::State]));
assert!(!g.covers(&[Permission::DocRead, Permission::State]));
let hash = sha256_hex(b"module");
let g = Grant { id: "a".into(), hash: hash.clone(), permissions: vec![Permission::DocRead, Permission::DocWrite] };
assert!(g.covers("a", &hash, &[Permission::DocRead]));
assert!(g.covers("a", &hash, &[]));
assert!(!g.covers("a", &hash, &[Permission::State]));
assert!(!g.covers("a", &hash, &[Permission::DocRead, Permission::State]));
}
#[test]
fn a_grant_covers_only_the_module_that_was_approved() {
let hash = sha256_hex(b"module");
let wanted = [Permission::DocRead];
let g = Grant { id: "a".into(), hash: hash.clone(), permissions: vec![Permission::DocRead] };
assert!(g.covers("a", &hash, &wanted));
assert!(!g.covers("b", &hash, &wanted), "another id");
assert!(!g.covers("a", &sha256_hex(b"other"), &wanted), "other bytes");
assert!(!g.covers("a", &sha256_hex(b"other"), &[]), "other bytes, even when nothing is wanted");
}
/// Damaged data can't stand for every module, even when the hash asked about is just as damaged.
#[test]
fn a_grant_with_an_invalid_hash_covers_nothing() {
let upper = sha256_hex(b"module").to_uppercase();
let short = "ab".repeat(31);
let long = "ab".repeat(33);
let not_hex = "g".repeat(64);
let wide = "é".repeat(32);
for hash in ["", "00", upper.as_str(), short.as_str(), long.as_str(), not_hex.as_str(), wide.as_str()] {
let g = Grant { id: "a".into(), hash: hash.into(), permissions: vec![Permission::DocRead] };
assert!(!g.covers("a", hash, &[Permission::DocRead]), "{hash:?}");
assert!(!g.covers("a", hash, &[]), "{hash:?}");
}
}
}
+28 -4
View File
@@ -56,11 +56,11 @@ pub fn revoke(id: &str) -> bool {
}
/// May `plugin` be installed without asking the user: does it list no permissions, or did the user
/// already grant its id every permission it lists? A module with other bytes but no new permission
/// needs no new approval.
/// already grant this very module (its id and SHA-256) every permission it lists? A module with
/// other bytes needs a new approval, even when it lists the same permissions.
pub fn approved(plugin: &Plugin) -> bool {
let m = plugin.manifest();
!m.asks_permissions() || granted(&m.id).is_some_and(|g| g.covers(&m.permissions()))
!m.asks_permissions() || granted(&m.id).is_some_and(|g| g.covers(&m.id, plugin.hash(), &m.permissions()))
}
/// Installs `plugin`, replacing one with the same id.
@@ -153,7 +153,7 @@ pub struct FolderReport {
/// Installs every `*.wasm` directly in `dir` (not recursive), in file-name order. A bad module is
/// reported and skipped, the others still load; so is a heavy one ([`crate::Budget::Heavy`]),
/// which installs only once the user agrees, and one that lists permissions the user has not
/// granted its id ([`approved`]).
/// granted this very module ([`approved`]).
#[cfg(not(target_arch = "wasm32"))]
pub fn load_folder(dir: &std::path::Path) -> Result<FolderReport> {
load_folder_where(dir, |_| Ok(()), approved)
@@ -213,3 +213,27 @@ pub fn load_folder_where(
}
Ok(report)
}
#[cfg(test)]
mod tests {
use super::*;
/// A cache keyed by the revision can't store the old list under the new revision: an install
/// waiting for the set leaves the revision alone until it holds the set.
#[test]
fn an_install_changes_the_revision_only_while_it_holds_the_set() {
let plugin = Plugin::load(include_bytes!("../tests/fixtures/desaturate.wasm"), Limits::default()).unwrap();
let id = plugin.id().to_string();
let held = PLUGINS.read().unwrap_or_else(|e| e.into_inner());
let before = revision();
let installing = std::thread::spawn(move || install(plugin));
let moved = (0..1_000_000).any(|_| {
std::thread::yield_now();
revision() != before
});
drop(held);
installing.join().unwrap();
assert!(!moved, "the revision changed while the install waited for the set");
assert!(revision() > before && remove(&id));
}
}
+6
View File
@@ -61,6 +61,10 @@ pub struct Limits {
/// raise. Set them to `fuel_base` and `wall_time_ms` to give heavy filters nothing more.
pub heavy_fuel: u64,
pub heavy_wall_time_ms: u64,
/// What one export or import is given instead of `fuel_base` and `wall_time_ms`
/// ([`crate::export::limits`]).
pub export_fuel: u64,
pub export_wall_time_ms: u64,
}
impl Default for Limits {
@@ -75,6 +79,8 @@ impl Default for Limits {
wall_time_ms: 60_000,
heavy_fuel: HEAVY_FUEL,
heavy_wall_time_ms: HEAVY_WALL_TIME_MS,
export_fuel: crate::export::FUEL,
export_wall_time_ms: crate::export::WALL_TIME_MS,
}
}
}
+5
View File
@@ -167,6 +167,11 @@ pub fn exporter(id: &str, extensions: &str, params: &str) -> String {
format!(r#"{{"id":"{id}","name":"Test {id}","version":"1","kind":"exporter","extensions":{extensions},"params":{params}}}"#)
}
/// The manifest of a test importer `id` reading `extensions` (a JSON list) with `params`.
pub fn importer(id: &str, extensions: &str, params: &str) -> String {
format!(r#"{{"id":"{id}","name":"Test {id}","version":"1","kind":"importer","extensions":{extensions},"params":{params}}}"#)
}
/// What [`echo_exporter`] writes as its file.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Echo {
+40 -1
View File
@@ -273,7 +273,46 @@ fn an_exporter_returns_its_file_with_an_export_s_budget() {
// Its parameters are checked, and a filter is not an exporter.
assert!(matches!(export::run(&p, b"{}", &json!({"level": "high"}), target), Err(Error::Params(_))));
let filter = Plugin::load(&ok_module("(i64.const 0)"), Limits::default()).unwrap();
assert!(matches!(export::run(&filter, b"{}", &Value::Null, target), Err(Error::Params(_))));
let e = export::run(&filter, b"{}", &Value::Null, target).unwrap_err();
assert!(matches!(e, Error::Params(_)) && e.to_string().contains("is a plug-in of kind `filter`, not an exporter"), "{e}");
let importer = Plugin::load(&build(&tpl::returning(&tpl::importer("test.reads", r#"["tst"]"#, "{}"), "{}")), Limits::default()).unwrap();
let e = export::run(&importer, b"{}", &Value::Null, target).unwrap_err();
assert!(e.to_string().contains("is a plug-in of kind `importer`, not an exporter"), "{e}");
}
/// An importer runs on an import's budget, which replaces its own instruction and time budgets and
/// keeps its other limits: a run that needs more than `fuel_base` finishes within `export_fuel`,
/// and a run that needs more than `export_fuel` ends there, however large `fuel_base` is.
#[test]
fn an_importer_runs_on_an_import_s_budget() {
use vectorcraft_plugins::export;
use vectorcraft_plugins::import::{self, Source};
let run = format!(
"(local $n i32) (local.set $n (i32.const 100000)) \
(loop $work (local.set $n (i32.sub (local.get $n) (i32.const 1))) (br_if $work (local.get $n))) {}",
tpl::ECHO
);
let wasm = build(&tpl::module(&tpl::importer("test.import.budget", r#"["tst"]"#, "{}"), &run, "", 1));
let file = br#"{"objects":[{"type":"path","path":{"subpaths":[{"closed":false,"anchors":[{"p":[0,0]},{"p":[1,0]}]}]}}]}"#;
let source = Source { extension: "tst", name: "a" };
let limits = import::limits(&Plugin::load(&wasm, Limits::default()).unwrap());
assert_eq!((limits.fuel_base, limits.wall_time_ms), (export::FUEL, export::WALL_TIME_MS));
let own = Limits {
fuel_base: 9_000,
fuel_per_byte: 0,
wall_time_ms: 8_000,
export_fuel: 10_000_000,
export_wall_time_ms: 7_000,
max_output_bytes: 6_000,
..Limits::default()
};
let p = Plugin::load(&wasm, own.clone()).unwrap();
assert_eq!(import::limits(&p), Limits { fuel_base: 10_000_000, wall_time_ms: 7_000, ..own });
assert_eq!(import::run(&p, file, &Value::Null, source).map(|i| i.objects.len()), Ok(1));
let p = Plugin::load(&wasm, Limits { fuel_base: 10_000_000, fuel_per_byte: 0, export_fuel: 9_000, ..Limits::default() }).unwrap();
assert_eq!(import::run(&p, file, &Value::Null, source), Err(Error::Limit("instruction budget".into())));
}
/// The echo exporter the engine and UI tests read back what they hand a plug-in with: its file is
+31 -11
View File
@@ -10,6 +10,7 @@ use std::cell::RefCell;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use vectorcraft_color::cms::Model;
use vectorcraft_color::freeform::{grid_size, painted_box, spread_scale};
use vectorcraft_color::{Freeform, GradientPaint};
use vectorcraft_geom::{Affine, Rect};
@@ -24,6 +25,10 @@ thread_local! {
static GRIDS: RefCell<FrameCache<u64, Arc<Pixmap>>> = RefCell::new(FrameCache::new(lru::FREEFORM_BUDGET, usize::MAX));
}
/// Most cells along a side of a grid of Lab points on an ink plane, where each cell separates its
/// color through the profile; the paint smooths the grid.
const MAX_LAB_PLANE_CELLS: u16 = 64;
/// Cache key of the grid of `f` on box `b` at `cols` × `rows` painted with `ink` (and the active
/// colour settings, which turn colours into display RGB or inks).
fn key(f: &Freeform, b: Rect, cols: u16, rows: u16, ink: Ink) -> u64 {
@@ -38,18 +43,33 @@ fn key(f: &Freeform, b: Rect, cols: u16, rows: u16, ink: Ink) -> u64 {
h.finish()
}
/// The grid size of `f` on box `b` drawn `device` pixels long with `ink`.
pub(crate) fn cells(f: &Freeform, b: Rect, device: f64, ink: Ink) -> (u16, u16) {
let (cols, rows) = grid_size(b, device);
if f.shared_model() == Some(Model::Lab) && matches!(ink.base(), Ink::Cmy | Ink::K) {
return (cols.min(MAX_LAB_PLANE_CELLS), rows.min(MAX_LAB_PLANE_CELLS));
}
(cols, rows)
}
/// Sample `f` at the centres of a `cols` × `rows` grid over `b` (premultiplied, colours as `ink`
/// paints them).
/// paints them). Points that share a color model mix in that model ([`Freeform::model_field`]);
/// points in different models, and points that `ink` paints as RGB black, mix as `ink` paints each
/// of them.
pub(crate) fn rasterize(f: &Freeform, b: Rect, cols: u16, rows: u16, ink: Ink) -> Pixmap {
let field = f.field_with(spread_scale(b), &|c| ink.rgb(c));
let data = field
.grid(b, cols, rows)
.map(|([r, g, bl], a)| {
let a = a.clamp(0.0, 1.0);
let q = |v: f32| (v.clamp(0.0, 1.0) * a * 255.0).round() as u8;
vello_cpu::color::PremulRgba8 { r: q(r), g: q(g), b: q(bl), a: (a * 255.0).round() as u8 }
})
.collect();
let adjusted = ink.freeform(f);
let (f, ink) = adjusted.as_ref().map_or((f, ink), |a| (a, ink.base()));
let cell = |[r, g, bl]: [f32; 3], a: f32| {
let a = a.clamp(0.0, 1.0);
let q = |v: f32| (v.clamp(0.0, 1.0) * a * 255.0).round() as u8;
vello_cpu::color::PremulRgba8 { r: q(r), g: q(g), b: q(bl), a: (a * 255.0).round() as u8 }
};
let scale = spread_scale(b);
let data = if f.shared_model().is_some() && !ink.blacks_a_point(f) {
f.model_field(scale).grid(b, cols, rows).map(|(c, a)| cell(ink.rgb_sampled(&c), a)).collect()
} else {
f.field_with(scale, &|c| ink.rgb(c)).grid(b, cols, rows).map(|(rgb, a)| cell(rgb, a)).collect()
};
Pixmap::from_parts(data, cols, rows)
}
@@ -58,8 +78,8 @@ pub(crate) fn rasterize(f: &Freeform, b: Rect, cols: u16, rows: u16, ink: Ink) -
pub(crate) fn set_freeform_paint(ctx: &mut RenderContext, g: &GradientPaint, bounds: Rect, ink: Ink) -> bool {
let Some(b) = painted_box(bounds) else { return false };
let device = b.width().max(b.height()) * ctx.transform().determinant().abs().sqrt();
let (cols, rows) = grid_size(b, device);
let f = g.freeform_on(b);
let (cols, rows) = cells(&f, b, device, ink);
let k = key(&f, b, cols, rows, ink);
let pm = GRIDS
.with(|c| {
Loaded 100 of 190 files, more files were not shown because too many files have changed in this diff. Show more