Skip to content

Commit d7e8db9

Browse files
committed
ref(wasm): split layout and input entrypoints
1 parent e40745b commit d7e8db9

10 files changed

Lines changed: 73 additions & 36 deletions

File tree

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/.agent-shell/
2-
/clayterm.wasm
3-
/wasm.ts
2+
/clayterm-layout.wasm
3+
/clayterm-input.wasm
4+
/wasm-layout.ts
5+
/wasm-input.ts
46
/build/
57
/node_modules/

Makefile

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
CC = clang
22
WASM_OPT ?= wasm-opt
3-
TARGET = clayterm.wasm
4-
SRC = src/module.c
53

64
CFLAGS = --target=wasm32 -nostdlib -Oz \
75
-ffunction-sections -fdata-sections \
86
-mbulk-memory \
97
-DCLAY_IMPLEMENTATION -DCLAY_WASM \
108
-Isrc -I.
119

12-
EXPORTS = \
10+
LDFLAGS_COMMON = -Wl,--no-entry \
11+
-Wl,--import-memory \
12+
-Wl,--stack-first \
13+
-Wl,--strip-all \
14+
-Wl,--gc-sections
15+
16+
LAYOUT_EXPORTS = \
1317
-Wl,--export=__heap_base \
1418
-Wl,--export=clayterm_size \
1519
-Wl,--export=init \
@@ -25,36 +29,46 @@ EXPORTS = \
2529
-Wl,--export=error_count \
2630
-Wl,--export=error_type \
2731
-Wl,--export=error_message_length \
28-
-Wl,--export=error_message_ptr \
32+
-Wl,--export=error_message_ptr
33+
34+
INPUT_EXPORTS = \
35+
-Wl,--export=__heap_base \
2936
-Wl,--export=input_size \
3037
-Wl,--export=input_init \
3138
-Wl,--export=input_scan \
3239
-Wl,--export=input_count \
3340
-Wl,--export=input_event \
3441
-Wl,--export=input_delay
3542

36-
LDFLAGS = -Wl,--no-entry \
37-
-Wl,--import-memory \
38-
-Wl,--stack-first \
39-
-Wl,--strip-all \
40-
-Wl,--gc-sections \
41-
-Wl,--undefined=Clay__MeasureText \
42-
-Wl,--undefined=Clay__QueryScrollOffset \
43-
$(EXPORTS)
43+
LAYOUT_LDFLAGS = $(LDFLAGS_COMMON) \
44+
-Wl,--undefined=Clay__MeasureText \
45+
-Wl,--undefined=Clay__QueryScrollOffset \
46+
$(LAYOUT_EXPORTS)
4447

45-
all: $(TARGET) wasm.ts
46-
@echo "Built $(TARGET) ($$(wc -c < $(TARGET)) bytes raw, $$(gzip -c $(TARGET) | wc -c) bytes gzip)"
48+
INPUT_LDFLAGS = $(LDFLAGS_COMMON) \
49+
$(INPUT_EXPORTS)
4750

4851
DEPS = $(wildcard src/*.c src/*.h)
4952

50-
$(TARGET): $(DEPS)
51-
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC)
53+
all: clayterm-layout.wasm clayterm-input.wasm wasm-layout.ts wasm-input.ts
54+
@echo "Built clayterm-layout.wasm ($$(wc -c < clayterm-layout.wasm) bytes raw, $$(gzip -c clayterm-layout.wasm | wc -c) bytes gzip)"
55+
@echo "Built clayterm-input.wasm ($$(wc -c < clayterm-input.wasm) bytes raw, $$(gzip -c clayterm-input.wasm | wc -c) bytes gzip)"
56+
57+
clayterm-layout.wasm: $(DEPS)
58+
$(CC) $(CFLAGS) $(LAYOUT_LDFLAGS) -o $@ src/module-layout.c
5259
$(WASM_OPT) -Oz --enable-bulk-memory -o $@ $@
5360

54-
wasm.ts: $(TARGET)
55-
deno run --allow-read --allow-write tasks/bundle-wasm.ts
61+
clayterm-input.wasm: $(DEPS)
62+
$(CC) $(filter-out -DCLAY_IMPLEMENTATION -DCLAY_WASM, $(CFLAGS)) $(INPUT_LDFLAGS) -o $@ src/module-input.c
63+
$(WASM_OPT) -Oz --enable-bulk-memory -o $@ $@
64+
65+
wasm-layout.ts: clayterm-layout.wasm
66+
deno run --allow-read --allow-write tasks/bundle-wasm.ts clayterm-layout.wasm wasm-layout.ts
67+
68+
wasm-input.ts: clayterm-input.wasm
69+
deno run --allow-read --allow-write tasks/bundle-wasm.ts clayterm-input.wasm wasm-input.ts
5670

5771
clean:
58-
rm -f $(TARGET) wasm.ts
72+
rm -f clayterm.wasm clayterm-layout.wasm clayterm-input.wasm wasm.ts wasm-layout.ts wasm-input.ts
5973

6074
.PHONY: all clean

deno.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
},
2121
"exports": {
2222
".": "./mod.ts",
23+
"./layout": "./layout.ts",
24+
"./input": "./input.ts",
2325
"./validate": "./validate.ts"
2426
},
2527
"publish": {
2628
"include": ["*.ts"],
27-
"exclude": ["!wasm.ts"]
29+
"exclude": ["!wasm-layout.ts", "!wasm-input.ts"]
2830
},
2931
"compilerOptions": {
3032
"types": ["@types/node"]

input-native.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export interface InputNative {
169169
delay(st: number): number;
170170
}
171171

172-
import { compiled } from "./wasm.ts";
172+
import { compiled } from "./wasm-input.ts";
173173

174174
export async function createInputNative(
175175
escLatency: number,
@@ -178,14 +178,6 @@ export async function createInputNative(
178178

179179
let instance = await WebAssembly.instantiate(compiled, {
180180
env: { memory },
181-
clay: {
182-
measureTextFunction() {},
183-
queryScrollOffsetFunction(ret: number) {
184-
let v = new DataView(memory.buffer);
185-
v.setFloat32(ret, 0, true);
186-
v.setFloat32(ret + 4, 0, true);
187-
},
188-
},
189181
});
190182

191183
let exports = instance.exports as unknown as {

layout.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./ops.ts";
2+
export * from "./term.ts";
3+
export * from "./settings.ts";
4+
export * from "./termcodes.ts";

src/module-input.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* module-input.c — input-only compilation unit, no Clay layout engine */
2+
3+
#include "mem.c"
4+
#include "utf8.c"
5+
#include "trie.c"
6+
#include "input.c"

src/module-layout.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* module-layout.c — layout-only compilation unit, no input parser */
2+
3+
#include "../clay/clay.h"
4+
5+
#include "mem.c"
6+
#include "buffer.c"
7+
#include "cell.c"
8+
#include "utf8.c"
9+
#include "wcwidth.c"
10+
#include "clayterm.c"

tasks/build-npm.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ if (!version) {
1010
}
1111

1212
await build({
13-
entryPoints: ["./mod.ts"],
13+
entryPoints: [
14+
"./mod.ts",
15+
{ name: "./layout", path: "./layout.ts" },
16+
{ name: "./input", path: "./input.ts" },
17+
{ name: "./validate", path: "./validate.ts" },
18+
],
1419
outDir,
1520
shims: {
1621
deno: false,

tasks/bundle-wasm.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ function encodeZ85(data: Uint8Array): string {
2525
return out.join("");
2626
}
2727

28-
const wasm = await Deno.readFile("clayterm.wasm");
28+
const [input = "clayterm.wasm", output = "wasm.ts"] = Deno.args;
29+
30+
const wasm = await Deno.readFile(input);
2931

3032
const compressed = new Uint8Array(brotliCompressSync(wasm, {
3133
params: {
@@ -46,9 +48,9 @@ const compressed=d(${JSON.stringify(z85)},${compressed.byteLength});
4648
export const compiled=await WebAssembly.compile(brotliDecompressSync(compressed));
4749
`;
4850

49-
await Deno.writeTextFile("wasm.ts", source);
51+
await Deno.writeTextFile(output, source);
5052
console.log(
51-
`wrote wasm.ts (${wasm.length}${compressed.byteLength} bytes compressed, ${z85.length} bytes z85, ${
53+
`wrote ${output} (${wasm.length}${compressed.byteLength} bytes compressed, ${z85.length} bytes z85, ${
5254
Math.round(z85.length / wasm.length * 100)
5355
}%)`,
5456
);

term-native.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface Native {
3131
errorMessage(ct: number, index: number): string;
3232
}
3333

34-
import { compiled } from "./wasm.ts";
34+
import { compiled } from "./wasm-layout.ts";
3535

3636
export async function createTermNative(
3737
w: number,

0 commit comments

Comments
 (0)