Skip to content
Open
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
6 changes: 4 additions & 2 deletions .github/workflows/verify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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
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
52 changes: 33 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
CC = clang
WASM_OPT ?= wasm-opt
TARGET = clayterm.wasm
SRC = src/module.c

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 @@ -25,36 +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 $@ $@

wasm.ts: $(TARGET)
deno run --allow-read --allow-write tasks/bundle-wasm.ts
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

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
4 changes: 3 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,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"]
},
"compilerOptions": {
"types": ["@types/node"]
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"
7 changes: 6 additions & 1 deletion tasks/build-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ if (!version) {
}

await build({
entryPoints: ["./mod.ts"],
entryPoints: [
"./mod.ts",
{ name: "./layout", path: "./layout.ts" },
{ name: "./input", path: "./input.ts" },
{ name: "./validate", path: "./validate.ts" },
],
outDir,
shims: {
deno: false,
Expand Down
12 changes: 9 additions & 3 deletions tasks/bundle-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ function encodeZ85(data: Uint8Array): string {
return out.join("");
}

const wasm = await Deno.readFile("clayterm.wasm");
const [input, output] = Deno.args;
if (!input || !output) {
console.error("Usage: bundle-wasm.ts <input.wasm> <output.ts>");
Deno.exit(1);
}

const wasm = await Deno.readFile(input);

const compressed = new Uint8Array(brotliCompressSync(wasm, {
params: {
Expand All @@ -46,9 +52,9 @@ const compressed=d(${JSON.stringify(z85)},${compressed.byteLength});
export const compiled=await WebAssembly.compile(brotliDecompressSync(compressed));
`;

await Deno.writeTextFile("wasm.ts", source);
await Deno.writeTextFile(output, source);
console.log(
`wrote wasm.ts (${wasm.length} → ${compressed.byteLength} bytes compressed, ${z85.length} bytes z85, ${
`wrote ${output} (${wasm.length} → ${compressed.byteLength} bytes compressed, ${z85.length} bytes z85, ${
Math.round(z85.length / wasm.length * 100)
}%)`,
);
2 changes: 1 addition & 1 deletion term-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface Native {
errorMessage(ct: number, index: number): string;
}

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

export async function createTermNative(
w: number,
Expand Down
Loading