Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
with:
node-version: 22

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: Build WASM
run: make

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
with:
deno-version: v2.x

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: build wasm
run: make

Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
with:
deno-version: v2.x

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: build wasm
run: make

Expand Down Expand Up @@ -52,6 +55,9 @@ jobs:
with:
deno-version: v2.x

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: build wasm
run: make

Expand Down Expand Up @@ -125,6 +131,9 @@ jobs:
with:
deno-version: v2.x

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: build wasm
run: make

Expand Down
19 changes: 15 additions & 4 deletions .github/workflows/verify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ jobs:
with:
deno-version: v2.x

- name: format
run: deno task fmt:check
- name: verify
run: deno task verify

- name: lint
run: deno lint

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: build wasm
run: make

Expand All @@ -45,8 +48,10 @@ jobs:
with:
name: clayterm-wasm
path: |
clayterm.wasm
wasm.ts
layout.wasm
input.wasm
layout.wasm.ts
input.wasm.ts

test-alt-os:
needs: test
Expand Down Expand Up @@ -97,6 +102,9 @@ jobs:
with:
deno-version: v2.x

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: build wasm
run: make

Expand Down Expand Up @@ -126,6 +134,9 @@ jobs:
with:
node-version: 24

- name: install binaryen
run: sudo apt-get install -y binaryen

- name: build wasm
run: make

Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/.agent-shell/
/clayterm.wasm
/wasm.ts
/layout.wasm
/input.wasm
/layout.wasm.ts
/input.wasm.ts
/build/
/node_modules/
22 changes: 13 additions & 9 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ The local source build is driven by `make`.

It generates:

- `clayterm.wasm` — the compiled WebAssembly module built from the C sources
- `wasm.ts` — a generated TypeScript file derived from `clayterm.wasm`
- `layout.wasm` — the layout WebAssembly module built from the C sources
- `input.wasm` — the input WebAssembly module built from the C sources
- `layout.wasm.ts` — generated TypeScript derived from `layout.wasm`
- `input.wasm.ts` — generated TypeScript derived from `input.wasm`

`wasm.ts` is generated output, not hand-maintained source.
These `.ts` files are generated output, not hand-maintained source.

## Clone the repo with submodules

Expand Down Expand Up @@ -182,8 +184,10 @@ make

This should produce:

- `clayterm.wasm`
- `wasm.ts`
- `layout.wasm`
- `input.wasm`
- `layout.wasm.ts`
- `input.wasm.ts`

For a clean rebuild:

Expand All @@ -197,7 +201,7 @@ Re-run `make` when:

- you change files under `src/`
- you update the `clay` submodule
- `clayterm.wasm` or `wasm.ts` is missing
- `layout.wasm`, `input.wasm`, `layout.wasm.ts`, or `input.wasm.ts` is missing
- generated outputs look stale after switching branches or pulling changes

When in doubt, use a clean rebuild:
Expand Down Expand Up @@ -247,7 +251,7 @@ make clean && make
Symptoms may include:

- target-related `clang` errors mentioning `wasm32`
- linker failures while producing `clayterm.wasm`
- linker failures while producing the `.wasm` outputs

Recovery:

Expand All @@ -267,8 +271,8 @@ If the smoke test fails, fix the toolchain first and only then rerun `make`.

Symptoms may include:

- `clayterm.wasm` is missing
- `wasm.ts` is missing
- `layout.wasm` or `input.wasm` is missing
- `layout.wasm.ts` or `input.wasm.ts` is missing
- you changed `src/` or updated `clay/`, but the generated outputs do not match

Recovery:
Expand Down
56 changes: 36 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
CC = clang
TARGET = clayterm.wasm
SRC = src/module.c
WASM_OPT ?= wasm-opt

CFLAGS = --target=wasm32 -nostdlib -O2 \
CFLAGS = --target=wasm32 -nostdlib -Oz \
-ffunction-sections -fdata-sections \
-mbulk-memory \
-DCLAY_IMPLEMENTATION -DCLAY_WASM \
-Isrc -I.

EXPORTS = \
LDFLAGS_COMMON = -Wl,--no-entry \
-Wl,--import-memory \
-Wl,--stack-first \
-Wl,--strip-all \
-Wl,--gc-sections

LAYOUT_EXPORTS = \
-Wl,--export=__heap_base \
-Wl,--export=clayterm_size \
-Wl,--export=init \
Expand All @@ -24,35 +29,46 @@ EXPORTS = \
-Wl,--export=error_count \
-Wl,--export=error_type \
-Wl,--export=error_message_length \
-Wl,--export=error_message_ptr \
-Wl,--export=error_message_ptr

INPUT_EXPORTS = \
-Wl,--export=__heap_base \
-Wl,--export=input_size \
-Wl,--export=input_init \
-Wl,--export=input_scan \
-Wl,--export=input_count \
-Wl,--export=input_event \
-Wl,--export=input_delay

LDFLAGS = -Wl,--no-entry \
-Wl,--import-memory \
-Wl,--stack-first \
-Wl,--strip-all \
-Wl,--gc-sections \
-Wl,--undefined=Clay__MeasureText \
-Wl,--undefined=Clay__QueryScrollOffset \
$(EXPORTS)
LAYOUT_LDFLAGS = $(LDFLAGS_COMMON) \
-Wl,--undefined=Clay__MeasureText \
-Wl,--undefined=Clay__QueryScrollOffset \
$(LAYOUT_EXPORTS)

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

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

$(TARGET): $(DEPS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRC)
all: layout.wasm input.wasm layout.wasm.ts input.wasm.ts
@echo "Built layout.wasm ($$(wc -c < layout.wasm) bytes raw, $$(gzip -c layout.wasm | wc -c) bytes gzip)"
@echo "Built input.wasm ($$(wc -c < input.wasm) bytes raw, $$(gzip -c input.wasm | wc -c) bytes gzip)"

layout.wasm: $(DEPS)
$(CC) $(CFLAGS) $(LAYOUT_LDFLAGS) -o $@ src/module-layout.c
$(WASM_OPT) -Oz --enable-bulk-memory -o $@ $@

input.wasm: $(DEPS)
$(CC) $(filter-out -DCLAY_IMPLEMENTATION -DCLAY_WASM, $(CFLAGS)) $(INPUT_LDFLAGS) -o $@ src/module-input.c
$(WASM_OPT) -Oz --enable-bulk-memory -o $@ $@

layout.wasm.ts: layout.wasm
deno run --allow-read --allow-write tasks/bundle-wasm.ts layout.wasm layout.wasm.ts

wasm.ts: $(TARGET)
deno run --allow-read --allow-write tasks/bundle-wasm.ts
input.wasm.ts: input.wasm
deno run --allow-read --allow-write tasks/bundle-wasm.ts input.wasm input.wasm.ts

clean:
rm -f $(TARGET) wasm.ts
rm -f layout.wasm input.wasm layout.wasm.ts input.wasm.ts

.PHONY: all clean
9 changes: 7 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"fmt:check": "deno fmt --check && clang-format --dry-run --Werror src/*.c src/*.h",
"build:npm": "deno run -A tasks/build-npm.ts",
"build:jsr": "deno run -A tasks/build-jsr.ts",
"bench": "deno run -A bench/mod.ts"
"bench": "deno run -A bench/mod.ts",
"gen-wcwidth": "deno run --allow-net --allow-write=src/wcwidth.c tasks/gen-wcwidth.ts",
"gen-wcwidth:check": "deno run --allow-net --allow-read=src/wcwidth.c tasks/gen-wcwidth.ts --check",
"verify": "deno task fmt:check && deno task gen-wcwidth:check"
},
"imports": {
"@std/testing": "jsr:@std/testing@1",
Expand All @@ -21,11 +24,13 @@
},
"exports": {
".": "./mod.ts",
"./layout": "./layout.ts",
"./input": "./input.ts",
"./validate": "./validate.ts"
},
"publish": {
"include": ["*.ts"],
"exclude": ["!wasm.ts"]
"exclude": ["!layout.wasm.ts", "!input.wasm.ts"]
},
"nodeModulesDir": "auto",
"fmt": {
Expand Down
10 changes: 1 addition & 9 deletions input-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export interface InputNative {
delay(st: number): number;
}

import { compiled } from "./wasm.ts";
import { compiled } from "./input.wasm.ts";

export async function createInputNative(
escLatency: number,
Expand All @@ -178,14 +178,6 @@ export async function createInputNative(

let instance = await WebAssembly.instantiate(compiled, {
env: { memory },
clay: {
measureTextFunction() {},
queryScrollOffsetFunction(ret: number) {
let v = new DataView(memory.buffer);
v.setFloat32(ret, 0, true);
v.setFloat32(ret + 4, 0, true);
},
},
});

let exports = instance.exports as unknown as {
Expand Down
4 changes: 4 additions & 0 deletions layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./ops.ts";
export * from "./term.ts";
export * from "./settings.ts";
export * from "./termcodes.ts";
6 changes: 6 additions & 0 deletions src/module-input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* module-input.c — input-only compilation unit, no Clay layout engine */

#include "mem.c"
#include "utf8.c"
#include "trie.c"
#include "input.c"
10 changes: 10 additions & 0 deletions src/module-layout.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* module-layout.c — layout-only compilation unit, no input parser */

#include "../clay/clay.h"

#include "mem.c"
#include "buffer.c"
#include "cell.c"
#include "utf8.c"
#include "wcwidth.c"
#include "clayterm.c"
Loading
Loading