Skip to content

Commit bfac0b7

Browse files
committed
refactor: separate renderer initialization from resize
1 parent 3a7eab2 commit bfac0b7

6 files changed

Lines changed: 132 additions & 74 deletions

File tree

‎Makefile‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ CFLAGS = --target=wasm32 -nostdlib -O2 \
1313
EXPORTS = \
1414
-Wl,--export=__heap_base \
1515
-Wl,--export=clayterm_size \
16-
-Wl,--export=init \
16+
-Wl,--export=clayterm_init \
17+
-Wl,--export=clayterm_resize \
1718
-Wl,--export=set_capabilities \
18-
-Wl,--export=pointer_shape \
19-
-Wl,--export=set_pointer_shape \
20-
-Wl,--export=pointer_shape \
21-
-Wl,--export=set_pointer_shape \
2219
-Wl,--export=reduce \
2320
-Wl,--export=output \
2421
-Wl,--export=length \

‎src/clayterm.c‎

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* clayterm.c — WASM terminal rendering engine for Clay UI
22
*
33
* Public API (exported to WASM):
4-
* clayterm_size — compute arena size for given dimensions
5-
* init — initialize a Clayterm instance in provided memory
6-
* reduce — decode command buffer, run Clay layout, render to ANSI
7-
* output — pointer to output byte buffer
8-
* length — length of output byte buffer
9-
* measure — Clay text measurement callback
4+
* clayterm_size — compute arena size for given dimensions
5+
* clayterm_init — initialize a Clayterm instance in provided memory
6+
* clayterm_resize — rebuild dimension-dependent storage in place
7+
* reduce — decode command buffer, run Clay layout, render to ANSI
8+
* output — pointer to output byte buffer
9+
* length — length of output byte buffer
10+
* measure — Clay text measurement callback
1011
* error_count, error_type, error_message_length, error_message_ptr
11-
* — per-render Clay error accessors
12+
* — per-render Clay error accessors
1213
*/
1314

1415
#include "clayterm.h"
@@ -71,8 +72,8 @@ struct Clayterm {
7172
Clay_ErrorData errors[MAX_ERRORS];
7273
int error_count;
7374
int animating_count;
74-
/* CAP_* bits pushed by the host via set_capabilities(); init() zeroes
75-
* them, so the host re-pushes after every re-init */
75+
/* RENDER_CAP_* bits pushed by the host via set_capabilities(), retained
76+
* across resize updates. */
7677
uint32_t caps;
7778
/* Pointer shape (renderer-spec 7.10): hover_shape is what this frame's
7879
* hit test resolved, pointer_shape is what was last emitted. Both index
@@ -647,8 +648,8 @@ static int align64(int n) { return (n + 63) & ~63; }
647648
* with the grid instead of shipping Clay's fixed 8192-element default. 2x cells
648649
* bounds sane trees (leaves >= 1 cell, branching >= 2); the ceiling preserves
649650
* today's capacity for large grids, the floor keeps headroom on tiny ones.
650-
* Must run before Clay_MinMemorySize in both clayterm_size and init so the
651-
* size the host queries always matches the arena init consumes. */
651+
* Must run before Clay_MinMemorySize in both clayterm_size and
652+
* clayterm_resize so the queried size matches the arena setup. */
652653
static void set_clay_capacity(int w, int h) {
653654
int elems = w * h * 2;
654655
if (elems < 2048)
@@ -720,41 +721,55 @@ int error_message_ptr(struct Clayterm *ct, int index) {
720721
return (int)ct->errors[index].errorText.chars;
721722
}
722723

723-
struct Clayterm *init(void *mem, int w, int h) {
724-
set_clay_capacity(w, h);
724+
struct Clayterm *clayterm_init(void *mem, int w, int h) {
725725
struct Clayterm *ct = (struct Clayterm *)mem;
726+
*ct = (struct Clayterm){0};
727+
clayterm_resize(ct, w, h);
728+
return ct;
729+
}
730+
731+
/* Rebuild viewport storage without resetting instance state such as
732+
* capabilities and the emitted pointer shape. */
733+
void clayterm_resize(struct Clayterm *ct, int w, int h) {
734+
set_clay_capacity(w, h);
726735
int cell_count = w * h;
727736
int cell_bytes = align8(cell_count * (int)sizeof(Cell));
728737
int out_bytes = align8(cell_count * OUT_BYTES_PER_CELL + OUT_FRAME_OVERHEAD);
729-
char *base = (char *)mem + align8((int)sizeof(struct Clayterm));
738+
char *base = (char *)ct + align8((int)sizeof(struct Clayterm));
739+
740+
ct->w = w;
741+
ct->h = h;
742+
ct->front = (Cell *)base;
743+
ct->back = (Cell *)(base + cell_bytes);
744+
ct->out = (Buffer){base + cell_bytes * 2, 0,
745+
cell_count * OUT_BYTES_PER_CELL + OUT_FRAME_OVERHEAD};
746+
ct->lastfg = 0xffffffff;
747+
ct->lastbg = 0xffffffff;
748+
ct->lastx = -1;
749+
ct->lasty = -1;
750+
751+
/* Discard results from the old layout. Traversal scratch is reset by
752+
* reduce() before use; the caret text pointer may refer to reused storage. */
753+
ct->error_count = 0;
754+
ct->animating_count = 0;
755+
ct->caret_text_chars = NULL;
756+
ct->had_caret_last_frame = 0;
730757

731758
char *clay_mem = base + cell_bytes * 2 + out_bytes;
732759
int clay_bytes = align64((int)Clay_MinMemorySize());
733760
Clay_Arena arena =
734761
Clay_CreateArenaWithCapacityAndMemory(clay_bytes, clay_mem);
762+
/* Clay reads its previous context during initialization. Fill cells only
763+
* afterward: the resized cell buffers may overlap that old context. */
735764
Clay_Initialize(arena, (Clay_Dimensions){(float)w, (float)h},
736765
(Clay_ErrorHandler){clay_error, ct});
737766

738-
*ct = (struct Clayterm){
739-
.w = w,
740-
.h = h,
741-
.front = (Cell *)base,
742-
.back = (Cell *)(base + cell_bytes),
743-
.out = {base + cell_bytes * 2, 0,
744-
cell_count * OUT_BYTES_PER_CELL + OUT_FRAME_OVERHEAD},
745-
.lastfg = 0xffffffff,
746-
.lastbg = 0xffffffff,
747-
.lastx = -1,
748-
.lasty = -1,
749-
};
750-
751767
// initialize back buffer with spaces and default fg/bg
752768
cells_fill(ct->back, w, h, ' ', ATTR_DEFAULT, ATTR_DEFAULT);
753769

754770
// initialize front buffer with zeros. Every cell will be
755771
// redrawn on the first render.
756772
cells_fill(ct->front, w, h, 0, 0, 0);
757-
return ct;
758773
}
759774

760775
int set_capabilities(struct Clayterm *ct, uint32_t caps) {
@@ -767,12 +782,6 @@ int set_capabilities(struct Clayterm *ct, uint32_t caps) {
767782
return restore;
768783
}
769784

770-
int pointer_shape(struct Clayterm *ct) { return ct->pointer_shape; }
771-
772-
void set_pointer_shape(struct Clayterm *ct, int shape) {
773-
ct->pointer_shape = shape;
774-
}
775-
776785
void reduce(struct Clayterm *ct, uint32_t *buf, int len, int mode, int row,
777786
float deltaTime, float px, float py, int pointer) {
778787
int i = 0;

‎src/clayterm.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ struct Clayterm;
2828

2929
/* WASM exports */
3030
int clayterm_size(int w, int h);
31-
struct Clayterm *init(void *mem, int w, int h);
31+
struct Clayterm *clayterm_init(void *mem, int w, int h);
32+
void clayterm_resize(struct Clayterm *ct, int w, int h);
3233
int set_capabilities(struct Clayterm *ct, uint32_t caps);
33-
int pointer_shape(struct Clayterm *ct);
34-
void set_pointer_shape(struct Clayterm *ct, int shape);
3534
void reduce(struct Clayterm *ct, uint32_t *buf, int len, int mode, int row,
3635
float deltaTime, float px, float py, int pointer);
3736
char *output(struct Clayterm *ct);

‎term-native.ts‎

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ export interface Native {
4444
statePtr: number;
4545
opsBuf: number;
4646
/**
47-
* Re-initialize renderer state for new dimensions in place
48-
* (renderer-spec 7.7). Reuses this instance and memory; statePtr and
49-
* opsBuf may change. Growing memory detaches prior buffer views.
47+
* Rebuild dimension-dependent renderer storage in place (renderer-spec 7.7).
48+
* Reuses this instance and memory; statePtr stays fixed and opsBuf may move.
49+
* Growing memory detaches prior buffer views.
5050
*/
5151
update(w: number, h: number): void;
5252
/**
5353
* Push capability evidence into the renderer. The renderer holds it across
54-
* frames; it is re-pushed internally after any re-init so it survives
55-
* resize updates. Returns true when withdrawing pointerShape leaves a
54+
* frames and resize updates. Returns true when withdrawing pointerShape leaves a
5655
* non-default shape on screen that the caller must reset.
5756
*/
5857
setCapabilities(caps: RenderCapabilities): boolean;
@@ -115,7 +114,8 @@ export async function createTermNative(
115114
let ct = exports as unknown as {
116115
__heap_base: WebAssembly.Global;
117116
clayterm_size(w: number, h: number): number;
118-
init(mem: number, w: number, h: number): number;
117+
clayterm_init(mem: number, w: number, h: number): number;
118+
clayterm_resize(ct: number, w: number, h: number): void;
119119
reduce(
120120
ct: number,
121121
buf: number,
@@ -130,8 +130,6 @@ export async function createTermNative(
130130
output(ct: number): number;
131131
length(ct: number): number;
132132
set_capabilities(ct: number, caps: number): number;
133-
pointer_shape(ct: number): number;
134-
set_pointer_shape(ct: number, shape: number): void;
135133
pointer_over_count(): number;
136134
pointer_over_id_string_length(index: number): number;
137135
pointer_over_id_string_ptr(index: number): number;
@@ -151,21 +149,17 @@ export async function createTermNative(
151149

152150
let statePtr: number | undefined;
153151
let opsBuf = 0;
154-
let capFlags = 0;
155-
156-
function pushCaps(ptr: number): boolean {
157-
return ct.set_capabilities(ptr, capFlags) !== 0;
158-
}
159-
160-
function setCaps(next: RenderCapabilities): void {
161-
capFlags = (next.syncOutput ? RENDER_CAP_SYNC_OUTPUT : 0) |
152+
function setCaps(next: RenderCapabilities): boolean {
153+
let flags = (next.syncOutput ? RENDER_CAP_SYNC_OUTPUT : 0) |
162154
(next.pointerShape ? RENDER_CAP_POINTER_SHAPE : 0);
155+
return ct.set_capabilities(statePtr!, flags) !== 0;
163156
}
164157

165158
// Renderer state and the fixed transfer buffer share linear memory as
166159
// [heap: state][opsBuf]; opsBuf moves when the state size changes. Memory is
167160
// grown to fit but never reclaimed, so a downsize keeps the high-water mark
168-
// (renderer-spec 7.7).
161+
// (renderer-spec 7.7). The instance is initialized once; resize rebuilds only
162+
// its dimension-dependent storage.
169163
function layout(lw: number, lh: number): void {
170164
let heap = ct.__heap_base.value as number;
171165
let size = ct.clayterm_size(lw, lh);
@@ -175,15 +169,15 @@ export async function createTermNative(
175169
if (pages > current) {
176170
memory.grow(pages - current);
177171
}
178-
let shape = statePtr === undefined ? 0 : ct.pointer_shape(statePtr);
179-
let ptr = ct.init(heap, lw, lh);
172+
if (statePtr === undefined) {
173+
statePtr = ct.clayterm_init(heap, lw, lh);
174+
} else {
175+
ct.clayterm_resize(statePtr, lw, lh);
176+
}
180177
opsBuf = (heap + size + 3) & ~3;
181-
pushCaps(ptr);
182-
ct.set_pointer_shape(ptr, shape);
183-
statePtr = ptr;
184178
}
185-
setCaps(caps);
186179
layout(w, h);
180+
setCaps(caps);
187181

188182
return {
189183
memory,
@@ -197,8 +191,7 @@ export async function createTermNative(
197191
layout(uw, uh);
198192
},
199193
setCapabilities(next: RenderCapabilities): boolean {
200-
setCaps(next);
201-
return pushCaps(statePtr!);
194+
return setCaps(next);
202195
},
203196
reduce: ct.reduce,
204197
output: ct.output,

‎test/pointer-shape.test.ts‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,47 @@ describe("pointer shape", () => {
288288
);
289289
});
290290

291-
it("keeps the emitted shape across a resize", () => {
291+
it("keeps the emitted shape and capability through growth and shrink", () => {
292292
term.render(layout(), LINK);
293-
expect(term.update([{ type: "resize", width: 50, height: 8 }])).toEqual(
294-
new Uint8Array(0),
295-
);
296-
expect(sequences(term.render(layout(), LINK).output)).toEqual([]);
293+
for (let [width, height] of [[120, 40], [40, 6]]) {
294+
expect(term.update([{ type: "resize", width, height }])).toEqual(
295+
new Uint8Array(0),
296+
);
297+
expect(term.capabilities.pointerShape).toBe(true);
298+
expect(sequences(term.render(layout(), LINK).output)).toEqual([]);
299+
expect(sequences(term.render(layout(), FIELD).output)).toEqual([
300+
osc22("text"),
301+
]);
302+
expect(sequences(term.render(layout(), LINK).output)).toEqual([
303+
osc22("pointer"),
304+
]);
305+
}
297306
expect(sequences(term.render(layout(), PLAIN).output)).toEqual([
298307
osc22("default"),
299308
]);
300309
});
310+
311+
it("resets once when capability withdrawal is interleaved with resizes", () => {
312+
term.render(layout(), LINK);
313+
let reset = term.update([
314+
{ type: "resize", width: 120, height: 40 },
315+
WITHDRAWN,
316+
{ type: "resize", width: 40, height: 6 },
317+
WITHDRAWN,
318+
]);
319+
expect(decode(reset)).toBe(osc22("default"));
320+
expect(term.capabilities.pointerShape).toBe(false);
321+
expect(sequences(term.render(layout(), FIELD).output)).toEqual([]);
322+
323+
expect(term.update([
324+
{ type: "resize", width: 120, height: 40 },
325+
SUPPORTED,
326+
{ type: "resize", width: 40, height: 6 },
327+
])).toEqual(new Uint8Array(0));
328+
expect(term.capabilities.pointerShape).toBe(true);
329+
expect(sequences(term.render(layout(), FIELD).output)).toEqual([
330+
osc22("text"),
331+
]);
332+
});
301333
});
302334
});

‎test/term.test.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,38 @@ hi
10281028
expect(out).not.toContain("\x1b[?2026");
10291029
});
10301030

1031-
it("keeps wrapping after a resize update", () => {
1031+
it("keeps wrapping through growth and shrink", () => {
10321032
term.update([syncOn]);
1033-
term.update([{ type: "resize", width: 50, height: 12 }]);
1033+
term.render(frame);
1034+
for (let [width, height] of [[120, 40], [20, 5]]) {
1035+
term.update([{ type: "resize", width, height }]);
1036+
expect(term.capabilities.syncOutput).toBe(true);
1037+
let out = decode(term.render(frame).output);
1038+
expect(out.startsWith("\x1b[?2026h")).toBe(true);
1039+
expect(out.endsWith("\x1b[?2026l")).toBe(true);
1040+
}
1041+
});
1042+
1043+
it("applies capability changes interleaved with resize updates", () => {
1044+
term.update([syncOn]);
1045+
term.render(frame);
1046+
term.update([
1047+
{ type: "resize", width: 120, height: 40 },
1048+
syncOff,
1049+
{ type: "resize", width: 20, height: 5 },
1050+
]);
1051+
expect(term.capabilities.syncOutput).toBe(false);
10341052
let out = decode(term.render(frame).output);
1053+
expect(out.length).toBeGreaterThan(0);
1054+
expect(out).not.toContain("\x1b[?2026");
1055+
1056+
term.update([
1057+
{ type: "resize", width: 120, height: 40 },
1058+
syncOn,
1059+
{ type: "resize", width: 20, height: 5 },
1060+
]);
1061+
expect(term.capabilities.syncOutput).toBe(true);
1062+
out = decode(term.render(frame).output);
10351063
expect(out.startsWith("\x1b[?2026h")).toBe(true);
10361064
expect(out.endsWith("\x1b[?2026l")).toBe(true);
10371065
});

0 commit comments

Comments
 (0)