diff --git a/.gitignore b/.gitignore index 99b6cdf..30f5beb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ /.agent-shell/ -/clayterm.wasm -/wasm.ts +/clayterm-layout.wasm +/clayterm-input.wasm +/wasm-layout.ts +/wasm-input.ts /build/ /node_modules/ diff --git a/Makefile b/Makefile index 1103b8e..90fb527 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,4 @@ CC = clang -TARGET = clayterm.wasm -SRC = src/module.c CFLAGS = --target=wasm32 -nostdlib -O2 \ -ffunction-sections -fdata-sections \ @@ -8,7 +6,13 @@ CFLAGS = --target=wasm32 -nostdlib -O2 \ -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 \ @@ -24,7 +28,10 @@ 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 \ @@ -32,27 +39,33 @@ EXPORTS = \ -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: clayterm-layout.wasm clayterm-input.wasm wasm-layout.ts wasm-input.ts + @echo "Built clayterm-layout.wasm ($$(wc -c < clayterm-layout.wasm) bytes raw, $$(gzip -c clayterm-layout.wasm | wc -c) bytes gzip)" + @echo "Built clayterm-input.wasm ($$(wc -c < clayterm-input.wasm) bytes raw, $$(gzip -c clayterm-input.wasm | wc -c) bytes gzip)" + +clayterm-layout.wasm: $(DEPS) + $(CC) $(CFLAGS) $(LAYOUT_LDFLAGS) -o $@ src/module-layout.c + +clayterm-input.wasm: $(DEPS) + $(CC) $(filter-out -DCLAY_IMPLEMENTATION -DCLAY_WASM, $(CFLAGS)) $(INPUT_LDFLAGS) -o $@ src/module-input.c + +wasm-layout.ts: clayterm-layout.wasm + deno run --allow-read --allow-write tasks/bundle-wasm.ts clayterm-layout.wasm wasm-layout.ts -wasm.ts: $(TARGET) - deno run --allow-read --allow-write tasks/bundle-wasm.ts +wasm-input.ts: clayterm-input.wasm + deno run --allow-read --allow-write tasks/bundle-wasm.ts clayterm-input.wasm wasm-input.ts clean: - rm -f $(TARGET) wasm.ts + rm -f clayterm.wasm clayterm-layout.wasm clayterm-input.wasm wasm.ts wasm-layout.ts wasm-input.ts .PHONY: all clean diff --git a/deno.json b/deno.json index 901feb9..71af68c 100644 --- a/deno.json +++ b/deno.json @@ -21,11 +21,13 @@ }, "exports": { ".": "./mod.ts", + "./layout": "./layout.ts", + "./input": "./input.ts", "./validate": "./validate.ts" }, "publish": { "include": ["*.ts"], - "exclude": ["!wasm.ts"] + "exclude": ["!wasm-layout.ts", "!wasm-input.ts"] }, "nodeModulesDir": "auto", "fmt": { diff --git a/input-native.ts b/input-native.ts index 7ed6d1b..34272b0 100644 --- a/input-native.ts +++ b/input-native.ts @@ -169,7 +169,7 @@ export interface InputNative { delay(st: number): number; } -import { compiled } from "./wasm.ts"; +import { compiled } from "./wasm-input.ts"; export async function createInputNative( escLatency: number, @@ -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 { diff --git a/layout.ts b/layout.ts new file mode 100644 index 0000000..1fe605e --- /dev/null +++ b/layout.ts @@ -0,0 +1,4 @@ +export * from "./ops.ts"; +export * from "./term.ts"; +export * from "./settings.ts"; +export * from "./termcodes.ts"; diff --git a/src/module-input.c b/src/module-input.c new file mode 100644 index 0000000..550d698 --- /dev/null +++ b/src/module-input.c @@ -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" diff --git a/src/module-layout.c b/src/module-layout.c new file mode 100644 index 0000000..f865f5f --- /dev/null +++ b/src/module-layout.c @@ -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" diff --git a/tasks/build-npm.ts b/tasks/build-npm.ts index da64792..174b949 100644 --- a/tasks/build-npm.ts +++ b/tasks/build-npm.ts @@ -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, diff --git a/tasks/bundle-wasm.ts b/tasks/bundle-wasm.ts index c517974..2cad7e8 100644 --- a/tasks/bundle-wasm.ts +++ b/tasks/bundle-wasm.ts @@ -27,7 +27,9 @@ function encodeZ85(data: Uint8Array): string { return out.join(""); } -const wasm = await Deno.readFile("clayterm.wasm"); +const [input = "clayterm.wasm", output = "wasm.ts"] = Deno.args; + +const wasm = await Deno.readFile(input); const compressed = new Uint8Array( brotliCompressSync(wasm, { @@ -50,9 +52,9 @@ const compressed=d(${JSON.stringify(z85)},${compressed.byteLength}); export const compiled=await WebAssembly.compile(new Uint8Array(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) }%)`, ); diff --git a/term-native.ts b/term-native.ts index 40e646d..548344f 100644 --- a/term-native.ts +++ b/term-native.ts @@ -31,7 +31,7 @@ export interface Native { errorMessage(ct: number, index: number): string; } -import { compiled } from "./wasm.ts"; +import { compiled } from "./wasm-layout.ts"; export async function createTermNative( w: number,