diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c05f7c9..5b3d0b3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,13 @@ platform API version. ### Added +- **A `ram` flow symbol.** The flow symbols (`FlowSymbols`, the dataflow + pictograms a product draws a node as) have a 29th kind, `ram`, in the + memory family: a memory itself, with no operand and one result. `memory` + stays the interface that the loads and the stores of a memory go through. + The ram is drawn as the stack of a load, longer, with its shape (`text`) + after it, and it says `ram` when no shape is given. A product on an older + platform gets no spec for the kind and draws its node as before. - **Find in the diagram.** The palette's loupe opens a box at the top of the palette that finds nodes in the model, not in the drawing, so a node off screen or too small to be drawn at the zoom is found too. It matches a @@ -94,6 +101,11 @@ platform API version. ### Fixed +- **A flow symbol whose text is not a string.** A number in a depiction's + `text`, `glyph`, `mark` or `to` (from a producer's JSON) is read as its + digits, and any other value that is not a string counts as one not given. + A number in `text` used to size the symbol and place its pins at NaN, and + one in `to` threw. - **"Beside" never opens over the diagram.** A reveal or an open with `beside: true` from a diagram whose group also held the text in a tab behind it opened the text in that group, over the diagram, since diff --git a/packages/diagram-client/test/flow-symbols.test.ts b/packages/diagram-client/test/flow-symbols.test.ts index 60cdacd8..1dba8e38 100644 --- a/packages/diagram-client/test/flow-symbols.test.ts +++ b/packages/diagram-client/test/flow-symbols.test.ts @@ -16,12 +16,12 @@ const COUNTS: Record = { source: [0, 1], sink: [1, 0], constant: [1, 1], end: [4, 0], instance: [2, 2], bundle: [2, 1], unbundle: [1, 2], - load: [2, 2], store: [2, 2], memory: [4, 2], 'load-store-queue': [5, 3], + load: [2, 2], store: [2, 2], memory: [4, 2], 'load-store-queue': [5, 3], ram: [0, 1], operator: [2, 1], compare: [2, 1], width: [1, 1], not: [1, 1] }; const DIRECTIONS: FlowDirection[] = ['DOWN', 'RIGHT']; /** Kinds whose text widens them across the flow: not drawn the same turned over. */ -const TEXT_ROOM = new Set(['constant', 'end', 'instance', 'memory', 'load-store-queue', 'width']); +const TEXT_ROOM = new Set(['constant', 'end', 'instance', 'memory', 'load-store-queue', 'ram', 'width']); /** The frames in pixels: every symbol's, and a choice's, larger. */ const FRAME = FlowSymbols.FRAME * FlowSymbols.SCALE; @@ -49,7 +49,7 @@ function pointsOf(s: FlowShape): Array<[number, number]> { describe('flow symbols', () => { it('list every kind, each in a family', () => { - expect(FlowSymbols.KINDS).toHaveLength(28); + expect(FlowSymbols.KINDS).toHaveLength(29); expect(Object.keys(COUNTS).sort()).toEqual([...FlowSymbols.KINDS].sort()); }); @@ -142,7 +142,7 @@ describe('flow symbols', () => { fork: [1, 2], 'lazy-fork': [1, 2], join: [2, 1], blocker: [2, 1], mux: [3, 1], select: [3, 1], merge: [2, 1], end: [2, 0], instance: [2, 2] }; - const grown = new Set(['memory', 'load-store-queue', 'instance', 'end']); + const grown = new Set(['memory', 'load-store-queue', 'ram', 'instance', 'end']); for (const kind of FlowSymbols.KINDS) { if (grown.has(kind)) continue; const [ins, outs] = few[kind] ?? COUNTS[kind]; @@ -207,6 +207,7 @@ describe('flow symbols', () => { instance: { text: '@a_rather_long_callee_name_from_a_pass' }, memory: { text: '1024×1024×i32' }, 'load-store-queue': { text: '1024×1024×i32' }, + ram: { text: '1024×1024×i32' }, operator: { glyph: 'max', mark: 'u' }, compare: { glyph: 'ord', mark: 'f' }, width: { variant: 'widen', to: 'vector<4xi32>' } @@ -278,6 +279,76 @@ describe('flow symbols', () => { expect([many.width, many.height]).toEqual([FRAME, FRAME]); }); + it('draw a ram as the stack of a load, longer, with its shape, and one pin, its output', () => { + type Line = { x1: number; y1: number; x2: number; y2: number }; + const words = (kind: FlowSymbolKind, direction: FlowDirection, text?: string): Line[] => spec(kind, direction, text ? { text } : {}).shapes + .filter(s => s.shape === 'line' && s.classes.includes('flow-thin')) as unknown as Line[]; + const length = (w: Line): number => Math.abs(w.x2 - w.x1) + Math.abs(w.y2 - w.y1); + for (const direction of DIRECTIONS) { + const s = spec('ram', direction, { text: '64×i10' }); + expect(s.family).toBe('memory'); + expect(s.pins.map(p => `${p.of} ${p.index} ${p.side}`)).toEqual([`result 0 ${direction === 'DOWN' ? 'SOUTH' : 'EAST'}`]); + expect(s.shapes.find(t => t.shape === 'text')).toMatchObject({ text: '64×i10', level: 'name' }); + // Three words, as a load has, each longer than a word of a load + // and at most twice as long, however wide the text makes the body. + const inLoad = length(words('load', direction)[0]); + expect(words('load', direction)).toHaveLength(3); + for (const text of ['8×i8', '64×i10', '1024×1024×i32']) { + const inRam = words('ram', direction, text); + expect(inRam, `${direction} ${text}`).toHaveLength(3); + for (const w of inRam) { + expect(length(w), `${direction} ${text}`).toBeGreaterThan(inLoad); + expect(length(w), `${direction} ${text}`).toBeLessThanOrEqual(2 * inLoad + 0.01); + } + } + } + // The words lie across the flow and turn with it, as those of a load do. + expect(words('ram', 'DOWN').every(w => w.y1 === w.y2 && w.x1 !== w.x2)).toBe(true); + expect(words('ram', 'RIGHT').every(w => w.x1 === w.x2 && w.y1 !== w.y2)).toBe(true); + // With no shape, or a blank one, it says what it is. + expect(spec('ram', 'DOWN').shapes.find(t => t.shape === 'text')).toMatchObject({ text: 'ram' }); + expect(spec('ram', 'DOWN', { text: '' })).toEqual(spec('ram', 'DOWN')); + expect(spec('ram', 'RIGHT', { text: ' ' })).toEqual(spec('ram', 'RIGHT')); + // A shape past sixteen characters is cut short. + expect(spec('ram', 'DOWN', { text: '1024×1024×1024×i64' }).shapes.find(t => t.shape === 'text')).toMatchObject({ text: '1024×1024×1024×…' }); + // Down the page it fills the one frame along the flow and widens across + // it for its shape. To the right it is one frame across and takes the + // room for its shape along the flow. + expect(spec('ram', 'DOWN', { text: '8×i8' }).height).toBe(FRAME); + expect(spec('ram', 'DOWN', { text: '1024×1024×i32' }).height).toBe(FRAME); + expect(spec('ram', 'DOWN', { text: '1024×1024×i32' }).width).toBeGreaterThan(spec('ram', 'DOWN', { text: '8×i8' }).width); + expect(spec('ram', 'RIGHT', { text: '1024×1024×i32' }).height).toBe(FRAME); + expect(spec('ram', 'RIGHT', { text: '1024×1024×i32' }).width).toBeGreaterThan(spec('ram', 'RIGHT', { text: '8×i8' }).width); + // It takes no operand and gives one result. + expect(FlowSymbols.spec({ symbol: 'ram' }, 1, 1, 'DOWN')).toBeUndefined(); + expect(FlowSymbols.spec({ symbol: 'ram' }, 0, 2, 'DOWN')).toBeUndefined(); + expect(FlowSymbols.spec({ symbol: 'ram' }, 0, 0, 'DOWN')).toBeUndefined(); + }); + + it('read a number where a text goes as its digits, and any other value that is not a string as none given', () => { + // A producer may send anything, and what it sends where a text goes must not size the symbol. + const fields: Array<[FlowSymbolKind, string]> = [ + ['constant', 'text'], ['instance', 'text'], ['memory', 'text'], ['load-store-queue', 'text'], ['ram', 'text'], + ['operator', 'glyph'], ['compare', 'glyph'], ['operator', 'mark'], ['compare', 'mark'], ['width', 'to'] + ]; + for (const [kind, field] of fields) { + for (const direction of DIRECTIONS) { + const plain = spec(kind, direction); + for (const odd of [{ a: 1 }, [64], null, true, NaN, Infinity]) { + expect(spec(kind, direction, { [field]: odd }), `${kind} ${direction} ${field} ${String(odd)}`).toEqual(plain); + } + const digits = spec(kind, direction, { [field]: 64 }); + expect(digits, `${kind} ${direction} ${field} 64`).toEqual(spec(kind, direction, { [field]: '64' })); + expect(digits.shapes.some(t => t.shape === 'text' && t.text === '64'), `${kind} ${direction} ${field} 64`).toBe(true); + expect(Number.isFinite(digits.width) && Number.isFinite(digits.height)).toBe(true); + } + } + // An empty text is still a text: a constant, an instance and a memory draw none, as before. + for (const kind of ['constant', 'instance', 'memory'] as const) { + expect(spec(kind, 'DOWN', { text: '' }).shapes.find(t => t.shape === 'text'), kind).toMatchObject({ text: '' }); + } + }); + it('show an operator sign with its mark, from the middle zoom and close in', () => { const texts = spec('operator', 'DOWN', { glyph: '≫', mark: 'u' }).shapes.filter(s => s.shape === 'text'); expect(texts.map(t => [t.text, t.level])).toEqual([['≫', 'name'], ['u', 'detail']]); diff --git a/packages/shared/src/flow-symbols.ts b/packages/shared/src/flow-symbols.ts index fdbf2c19..9fdc8a15 100644 --- a/packages/shared/src/flow-symbols.ts +++ b/packages/shared/src/flow-symbols.ts @@ -38,7 +38,7 @@ export type FlowSymbolKind = | 'source' | 'sink' | 'constant' | 'end' | 'instance' | 'bundle' | 'unbundle' - | 'load' | 'store' | 'memory' | 'load-store-queue' + | 'load' | 'store' | 'memory' | 'load-store-queue' | 'ram' | 'operator' | 'compare' | 'width' | 'not'; /** A family of symbols, drawn in one tint. */ @@ -136,7 +136,7 @@ export namespace FlowSymbols { source: 'token', sink: 'token', constant: 'constant', end: 'terminal', instance: 'terminal', bundle: 'bundle', unbundle: 'bundle', - load: 'memory', store: 'memory', memory: 'memory', 'load-store-queue': 'memory', + load: 'memory', store: 'memory', memory: 'memory', 'load-store-queue': 'memory', ram: 'memory', operator: 'arith', compare: 'arith', width: 'arith', not: 'arith' }; @@ -152,7 +152,7 @@ export namespace FlowSymbols { source: [0, 0, 1, 1], sink: [1, 1, 0, 0], constant: [0, 1, 1, 1], end: [0, ANY, 0, 0], instance: [0, ANY, 0, ANY], bundle: [1, ANY, 1, ANY], unbundle: [1, ANY, 1, ANY], - load: [2, 2, 2, 2], store: [2, 2, 2, 2], memory: [0, ANY, 0, ANY], 'load-store-queue': [0, ANY, 0, ANY], + load: [2, 2, 2, 2], store: [2, 2, 2, 2], memory: [0, ANY, 0, ANY], 'load-store-queue': [0, ANY, 0, ANY], ram: [0, 0, 1, 1], operator: [1, 2, 1, 1], compare: [2, 2, 1, 1], width: [1, 1, 1, 1], not: [1, 1, 1, 1] }; @@ -259,6 +259,14 @@ export namespace FlowSymbols { const round = (v: number): number => Math.round(v * 100) / 100; /** A text past `max` characters, cut short with an ellipsis: a long value widens its node no further. */ const cut = (text: string, max: number): string => (text.length > max ? text.slice(0, max - 1) + '…' : text); + /** + * A text of a depiction, or `fallback` when the producer gave none. A + * producer may send anything. A number is read as its digits, and any + * other value that is not a string counts as none, because its length + * would size the symbol. + */ + const textOf = (value: unknown, fallback: string): string => + typeof value === 'string' ? value : typeof value === 'number' && Number.isFinite(value) ? String(value) : fallback; const operand = (index: number, f: number, c: number, onSide = false, back = false): PinAt => ({ of: 'operand', index, onSide, back, f, c }); const result = (index: number, f: number, c: number, onSide = false, back = false): PinAt => ({ of: 'result', index, onSide, back, f, c }); @@ -465,7 +473,7 @@ export namespace FlowSymbols { case 'constant': { // One with no trigger (an input) is a plain value, drawn dashed. const plain = ins === 0; - const value = cut(d.text ?? '?', 10); + const value = cut(textOf(d.text, '?'), 10); const text = textWidth(value, 10, true); const f0 = plain ? 2 : S; // The value runs across a flow that runs down (the tag spans @@ -501,7 +509,7 @@ export namespace FlowSymbols { }; } case 'instance': { - const label = cut(d.text ?? 'instance', 24); + const label = cut(textOf(d.text, 'instance'), 24); const n = Math.max(ins, outs, 1); // Across to the right the double sides stand beside the name and take its room. const base = room(textWidth(label, 10) + (down ? 22 : 32), FRAME, across(n, FRAME)); @@ -604,7 +612,7 @@ export namespace FlowSymbols { case 'load-store-queue': { const queue = kind === 'load-store-queue'; const n = Math.max(ins, outs, 2); - const label = cut(d.text ?? 'mem', 16); + const label = cut(textOf(d.text, 'mem'), 16); // Across to the right the name runs between the faint lines (and the // queue's band) and needs their room too. const base = room(textWidth(label, 9, true) + (down ? 18 : 28 + (queue ? 7 : 0)), queue ? 44 : 36, across(n, 44)); @@ -629,8 +637,36 @@ export namespace FlowSymbols { } }; } + case 'ram': { + // The memory itself, behind the interface that its loads and + // stores go through. It is drawn as the stack of a load and of + // a store, alone and longer, with its shape after it. It has no + // input, so the body starts at the edge, as the body of a plain + // constant does. With no shape, or a blank one, it says what it is. + const label = cut(textOf(d.text, '').trim() || 'ram', 16); + const text = textWidth(label, 9, true); + const f0 = 2, stackAt = f0 + 3, stackEnd = stackAt + 6; + // Down the page the stack and the shape under it fill the one + // frame. To the right the shape stands beside the stack and + // takes its room along the flow. + const F = down ? FRAME : Math.max(FRAME, text + 28); + const C = down ? Math.max(FRAME, text + 12) : FRAME; + // A word is at most twice as long as in a load. + const half = Math.min(C / 2 - 5, 14); + return { + F, C, pins: [result(0, F, C / 2)], + draw: g => { + g.rect(f0, 1, F - S, C - 1, 'square', 'flow-body'); + for (let k = 0; k < 3; k++) { + g.line(stackAt + 3 * k, C / 2 - half, stackAt + 3 * k, C / 2 + half, 'flow-thin'); + } + g.line(F - S, C / 2, F, C / 2); + g.value((stackEnd + F - S) / 2, C / 2, label, 9); + } + }; + } case 'operator': { - const glyph = d.glyph ?? '?'; + const glyph = textOf(d.glyph, '?'); const n = ins, C = across(n, FRAME), F = FRAME, R = 12; const pins = [...inputRow(n, C), result(0, F, C / 2)]; return { @@ -643,12 +679,12 @@ export namespace FlowSymbols { } g.line(F / 2 + R, C / 2, F, C / 2); g.circle(F / 2, C / 2, R, 'flow-body'); - g.glyph(F / 2, C / 2, glyph, glyph.length > 1 ? 8.5 : 15, d.mark); + g.glyph(F / 2, C / 2, glyph, glyph.length > 1 ? 8.5 : 15, textOf(d.mark, '')); } }; } case 'compare': { - const glyph = d.glyph ?? '?'; + const glyph = textOf(d.glyph, '?'); const C = across(2, FRAME), F = FRAME, rf = 12, rc = 13.5; const pins = [...inputRow(2, C), result(0, F, C / 2)]; return { @@ -661,12 +697,12 @@ export namespace FlowSymbols { } g.line(F / 2 + rf, C / 2, F, C / 2); g.poly([[F / 2 - rf, C / 2], [F / 2, C / 2 - rc], [F / 2 + rf, C / 2], [F / 2, C / 2 + rc]], 'flow-body'); - g.glyph(F / 2, C / 2, glyph, glyph.length > 1 ? 8 : 13, d.mark); + g.glyph(F / 2, C / 2, glyph, glyph.length > 1 ? 8 : 13, textOf(d.mark, '')); } }; } case 'width': { - const to = cut((d.to ?? '').replace(/[<>]/g, ''), 8); + const to = cut(textOf(d.to, '').replace(/[<>]/g, ''), 8); // The type sits halfway along, where the slanted sides take 6 of // each side's room. const base = room(textWidth(to, 8, true) + 14, FRAME, FRAME);