diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5ad9e71a..f0503139 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -127,10 +127,43 @@ jobs: - name: Check the example's custom language package run: | - node lib/cli/bin.js language tests/fixtures/kv_lite --out example/modules/codepad-lang-kv_lite + node lib/cli/bin.js language tests/fixtures/kv_lite --out example/modules/codepad-lang-kv_lite --no-web git status --porcelain -- example/modules test -z "$(git status --porcelain -- example/modules)" + test-web: + runs-on: ubuntu-24.04 + + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Setup + uses: ./.github/actions/setup + + - name: Setup Emscripten + uses: mymindstorm/setup-emsdk@4528d102f7230f0e7b276855c01ea1159be0e984 # v16 + with: + version: 7.0.0 + + - name: Build the web core and grammars + run: | + yarn build:web + cpp/web/build.sh --test + yarn build:web-grammars + + - name: Build package + run: yarn prepare + + - name: Run the web unit tests + run: yarn jest src/web + + - name: Install the browsers + run: npx playwright install --with-deps chromium webkit + + - name: Run the browser tests + run: yarn test:web + build-android: runs-on: ubuntu-24.04 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6f296781..501beb4c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -32,6 +32,17 @@ jobs: - name: Build package run: yarn prepare + - name: Setup Emscripten + uses: mymindstorm/setup-emsdk@4528d102f7230f0e7b276855c01ea1159be0e984 # v16 + with: + version: 7.0.0 + + # The core ships lib/web and every language package ships web/.wasm. + - name: Build the web core and grammars + run: | + yarn build:web + yarn build:web-grammars + # Trusted publishing stages each package; the versions go live once approved on npmjs.com, the core first. # A re-run skips only the live versions, so approve or reject the staged ones before it. - name: Stage the core, then the language packages diff --git a/.gitignore b/.gitignore index a9f54e87..47051003 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,7 @@ languages/.cache/ # Swift package build .build/ .swiftpm/ + +# Grammar side modules for the web, built before publishing +languages/*/web/ +example/modules/*/web/ diff --git a/cpp/core/include/codepad.h b/cpp/core/include/codepad.h index 7a14e4aa..df532c96 100644 --- a/cpp/core/include/codepad.h +++ b/cpp/core/include/codepad.h @@ -10,8 +10,13 @@ extern "C" { #endif +#ifdef __EMSCRIPTEN__ +// The web build calls the core from JS, so every entry point is exported. +#define CP_API __attribute__((used, visibility("default"))) +#else // Empty on purpose: only bindings linked into the same library call the core, so nothing is exported. #define CP_API +#endif typedef uint16_t cp_unit; diff --git a/cpp/core/src/language.cpp b/cpp/core/src/language.cpp index e5a6b4b4..e994474a 100644 --- a/cpp/core/src/language.cpp +++ b/cpp/core/src/language.cpp @@ -1,6 +1,7 @@ #include "language.hpp" #include +#include #include #include diff --git a/cpp/web/build.sh b/cpp/web/build.sh new file mode 100755 index 00000000..4a0cba4e --- /dev/null +++ b/cpp/web/build.sh @@ -0,0 +1,79 @@ +#!/bin/bash +# Builds the CodePad core for the browser with Emscripten. +# cpp/web/build.sh lib/web/codepad.{js,wasm} and lib/web/grammar-imports.json +# cpp/web/build.sh --test build/web-test/codepad.{js,wasm} with the test hooks +# cpp/web/build.sh --grammar one grammar side module from /parser.c and scanner.c +set -euo pipefail + +EMSCRIPTEN_VERSION=7.0.0 +ROOT=$(cd "$(dirname "$0")/../.." && pwd) +WEB=$ROOT/cpp/web +TS=$ROOT/cpp/tree-sitter/lib +PC=$ROOT/cpp/pcre2/src +CORE=$ROOT/cpp/core +OPT=-O2 + +if ! command -v emcc >/dev/null; then + echo "emcc not found: install Emscripten $EMSCRIPTEN_VERSION" >&2 + exit 1 +fi +found=$(emcc --version | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1) +if [ "$found" != "$EMSCRIPTEN_VERSION" ]; then + echo "Emscripten $EMSCRIPTEN_VERSION is required, found $found" >&2 + exit 1 +fi + +if [ "${1:-}" = --grammar ]; then + src=$2 + out=$3 + srcs=("$src/parser.c") + [ -f "$src/scanner.c" ] && srcs+=("$src/scanner.c") + mkdir -p "$(dirname "$out")" + emcc $OPT -fPIC -sSIDE_MODULE=1 -std=c11 -w -I"$src" "${srcs[@]}" -o "$out" + exit 0 +fi + +OUT=$ROOT/lib/web +OBJ=$ROOT/build/web/obj +CORE_DEFS= +if [ "${1:-}" = --test ]; then + OUT=$ROOT/build/web-test + OBJ=$ROOT/build/web-test/obj + CORE_DEFS=-DCODEPAD_TEST_HOOKS +fi +rm -rf "$OBJ" +mkdir -p "$OBJ" "$OUT" + +compile() { + local src=$1 + local obj + obj=$OBJ/$(basename "$(dirname "$src")")_$(basename "$src").o + case $src in + */lib.c) + emcc -c $OPT -fPIC -std=c11 -D_POSIX_C_SOURCE=200809L -fvisibility=hidden -DTREE_SITTER_HIDE_SYMBOLS -w \ + -I"$TS/include" -I"$TS/src" "$src" -o "$obj" ;; + "$PC"/*) + emcc -c $OPT -fPIC -std=c11 -fvisibility=hidden -DPCRE2_CODE_UNIT_WIDTH=16 -DHAVE_CONFIG_H -w -I"$PC" \ + "$src" -o "$obj" ;; + *) + em++ -c $OPT -fPIC -std=c++20 -fvisibility=hidden -DTREE_SITTER_HIDE_SYMBOLS -DPCRE2_CODE_UNIT_WIDTH=16 \ + $CORE_DEFS -I"$ROOT/cpp" -I"$TS/include" -I"$TS/src" -I"$CORE/include" -I"$PC" "$src" -o "$obj" ;; + esac +} +export -f compile +export OBJ OPT TS PC CORE ROOT CORE_DEFS + +{ echo "$TS/src/lib.c"; ls "$PC"/*.c; ls "$CORE"/src/*.cpp; echo "$WEB/host.cpp"; } | + xargs -P 8 -I{} bash -c 'compile "$1"' _ {} + +exports=$(sed 's/^/_/' "$WEB/exports.txt" | paste -sd, -) +em++ $OPT -sMAIN_MODULE=2 -sALLOW_MEMORY_GROWTH=1 -sFORCE_FILESYSTEM=1 -sMODULARIZE=1 \ + -sEXPORT_NAME=createCodepadCore -sENVIRONMENT=web,node \ + -sEXPORTED_FUNCTIONS="$exports" \ + -sEXPORTED_RUNTIME_METHODS=FS,HEAPU8,HEAPU16,HEAP32,UTF8ToString,stringToNewUTF8 \ + "$OBJ"/*.o -o "$OUT/codepad.js" + +if [ "$OUT" = "$ROOT/lib/web" ]; then + sed 's/.*/"&"/' "$WEB/exports.txt" | paste -sd, - | sed 's/.*/[&]/' >"$OUT/grammar-imports.json" +fi +ls -l "$OUT/codepad.js" "$OUT/codepad.wasm" diff --git a/cpp/web/exports.txt b/cpp/web/exports.txt new file mode 100644 index 00000000..ba5e0e34 --- /dev/null +++ b/cpp/web/exports.txt @@ -0,0 +1,35 @@ +malloc +free +calloc +realloc +iswspace +iswalnum +iswalpha +iswdigit +iswxdigit +iswupper +iswlower +iswpunct +towupper +towlower +strcmp +strncmp +strncpy +strlen +memcmp +memcpy +memset +memmove +__assert_fail +abort +isalnum +isalpha +isdigit +isspace +tolower +toupper +strchr +bsearch +fwrite +exit +stderr diff --git a/cpp/web/host.cpp b/cpp/web/host.cpp new file mode 100644 index 00000000..4bb17b2c --- /dev/null +++ b/cpp/web/host.cpp @@ -0,0 +1,255 @@ +// The browser side of the CodePad core: one thread, the engine's worker and main queues drained by the page's pump. +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define CPW extern "C" EMSCRIPTEN_KEEPALIVE + +namespace { + +struct Work { + void (*fn)(void *); + void *arg; +}; + +struct Editor { + int32_t index = -1; + cp_editor *editor = nullptr; + cp_engine *engine = nullptr; + cp_layout *layout = nullptr; + cp_snapshot *snapshot = nullptr; + int32_t snapshots = 0; + double clock = -1; + bool closed = false; + std::vector changes; + std::vector text; +}; + +std::deque worker_q; +std::deque main_q; +std::map languages; +// Injection names mapped to language ids, empty for a name that never injects. +std::map aliases; +std::string last_error; +// Snapshots the host holds, for the leak checks of the test build. +int32_t held = 0; +// A closed editor keeps its slot until the engine's final post, which still reaches its context. +std::vector> editors; + +Editor *at(int32_t e) { + if (e < 0 || e >= static_cast(editors.size()) || !editors[e] || editors[e]->closed) return nullptr; + return editors[e].get(); +} + +void schedule(void *, void (*fn)(void *), void *arg) { worker_q.push_back({fn, arg}); } +void post_main(void *, void (*fn)(void *), void *arg) { main_q.push_back({fn, arg}); } +int64_t now_micros(void *) { return static_cast(emscripten_get_now() * 1000.0); } + +int64_t now_millis(void *ctx) { + double clock = static_cast(ctx)->clock; + return static_cast(clock >= 0 ? clock : emscripten_get_now()); +} + +const cp_language *find(const std::string &id) { + auto it = languages.find(id); + return it == languages.end() ? nullptr : it->second; +} + +// An injection name, lowercased. A loaded id wins over the aliases, like LanguageRegistry.canonicalInjectionId. +const cp_language *resolve(void *, const char *name) { + std::string lower(name ? name : ""); + for (char &c : lower) c = static_cast(tolower(static_cast(c))); + if (const cp_language *language = find(lower)) return language; + auto it = aliases.find(lower); + return it == aliases.end() || it->second.empty() ? nullptr : find(it->second); +} + +void on_change(void *ctx, const cp_change *c, const cp_unit *removed, const cp_unit *inserted, int32_t origin) { + auto *ed = static_cast(ctx); + auto removed_at = static_cast(ed->text.size()); + if (c->removed_len > 0) ed->text.insert(ed->text.end(), removed, removed + c->removed_len); + auto inserted_at = static_cast(ed->text.size()); + if (c->inserted_len > 0) ed->text.insert(ed->text.end(), inserted, inserted + c->inserted_len); + ed->changes.insert(ed->changes.end(), + {c->start, c->removed_len, c->inserted_len, c->revision, origin, removed_at, inserted_at}); +} + +void release(Editor *ed) { + if (!ed->snapshot) return; + cp_engine_release(ed->engine, ed->snapshot); + ed->snapshot = nullptr; + held--; +} + +void on_snapshot(void *ctx, cp_snapshot *snapshot) { + auto *ed = static_cast(ctx); + if (!snapshot) { + editors[ed->index].reset(); + return; + } + held++; + if (ed->closed) { + cp_engine_release(nullptr, snapshot); + held--; + return; + } + release(ed); + ed->snapshot = snapshot; + ed->snapshots++; +} + +} // namespace + +CPW const char *cpw_error(void) { return last_error.c_str(); } + +// Registers a grammar side module already written to the file system. Returns 1, or 0 with cpw_error set. +CPW int32_t cpw_language_load(const char *id, const char *path, const char *symbol, const char *highlights, + const char *injections) { + if (languages.count(id)) return 1; + void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL); + if (!handle) { + last_error = dlerror(); + return 0; + } + auto fn = reinterpret_cast(dlsym(handle, symbol)); + if (!fn) { + last_error = std::string(path) + " has no symbol " + symbol; + return 0; + } + if (!injections) injections = ""; + char error[512] = {0}; + cp_language *language = + cp_language_load(id, fn(), highlights, static_cast(strlen(highlights)), injections, + static_cast(strlen(injections)), error, sizeof error); + if (!language) { + last_error = error; + return 0; + } + languages[id] = language; + return 1; +} + +// Maps an injection name to a language id. An empty id makes the name never inject. +CPW void cpw_language_alias(const char *name, const char *id) { aliases[name] = id; } + +// Opens an editor with its engine and layout attached and returns its index. +CPW int32_t cpw_open(const cp_unit *text, uint32_t length, int64_t budget_micros) { + int32_t index = 0; + while (index < static_cast(editors.size()) && editors[index]) index++; + if (index == static_cast(editors.size())) editors.emplace_back(); + editors[index] = std::make_unique(); + Editor *ed = editors[index].get(); + ed->index = index; + cp_editor_host editor_host{ed, now_millis, on_change, nullptr}; + ed->editor = cp_editor_new(&editor_host, text, length); + ed->layout = cp_layout_new(); + cp_editor_attach_layout(ed->editor, ed->layout); + cp_host host{ed, schedule, post_main, now_micros, resolve}; + ed->engine = cp_engine_new(&host, budget_micros, on_snapshot); + cp_editor_attach(ed->editor, ed->engine); + return index; +} + +// Frees the editor now. Its engine frees itself in later pumps. +CPW void cpw_close(int32_t e) { + Editor *ed = at(e); + if (!ed) return; + cp_editor_attach(ed->editor, nullptr); + release(ed); + cp_engine_close(ed->engine); + ed->engine = nullptr; + ed->closed = true; + cp_editor_attach_layout(ed->editor, nullptr); + cp_layout_free(ed->layout); + cp_editor_free(ed->editor); + ed->layout = nullptr; + ed->editor = nullptr; + ed->changes.clear(); + ed->text.clear(); +} + +// An id with no loaded language clears it, so the editor draws plain text. Returns 1 when the id was found. +CPW int32_t cpw_set_language(int32_t e, const char *id) { + Editor *ed = at(e); + if (!ed) return 0; + const cp_language *language = find(id ? id : ""); + cp_engine_set_language(ed->engine, language); + return language ? 1 : 0; +} + +CPW cp_editor *cpw_editor(int32_t e) { + Editor *ed = at(e); + return ed ? ed->editor : nullptr; +} + +CPW cp_layout *cpw_layout(int32_t e) { + Editor *ed = at(e); + return ed ? ed->layout : nullptr; +} + +CPW cp_snapshot *cpw_snapshot(int32_t e) { + Editor *ed = at(e); + return ed ? ed->snapshot : nullptr; +} + +CPW int32_t cpw_snapshot_count(int32_t e) { + Editor *ed = at(e); + return ed ? ed->snapshots : 0; +} + +// Pins the editor's history clock to millis, for tests. Below zero follows the page clock again. +CPW void cpw_set_clock(int32_t e, double millis) { + if (Editor *ed = at(e)) ed->clock = millis; +} + +// Runs worker work, then main work, until both queues are empty or budget_ms is spent. Returns the items left. +CPW int32_t cpw_pump(double budget_ms) { + double end = emscripten_get_now() + budget_ms; + while ((!worker_q.empty() || !main_q.empty()) && emscripten_get_now() < end) { + std::deque &q = !worker_q.empty() ? worker_q : main_q; + Work w = q.front(); + q.pop_front(); + w.fn(w.arg); + } + return static_cast(worker_q.size() + main_q.size()); +} + +// Moves the changes since the last call into out, 7 ints each: start, removed length, inserted length, revision, +// origin, then the offsets of the removed and inserted units in text. Returns the ints needed and sets *text_len +// to the units needed. Nothing moves unless both fit. +CPW uint32_t cpw_take_changes(int32_t e, int32_t *out, uint32_t cap, cp_unit *text, uint32_t text_cap, + uint32_t *text_len) { + Editor *ed = at(e); + if (!ed) { + *text_len = 0; + return 0; + } + auto ints = static_cast(ed->changes.size()); + auto units = static_cast(ed->text.size()); + *text_len = units; + if (ints > cap || units > text_cap) return ints; + std::copy(ed->changes.begin(), ed->changes.end(), out); + std::copy(ed->text.begin(), ed->text.end(), text); + ed->changes.clear(); + ed->text.clear(); + return ints; +} + +#ifdef CODEPAD_TEST_HOOKS +CPW int32_t cpw_test_held_snapshots(void) { return held; } + +// Slots still taken, closed ones waiting for their engine's final post included. +CPW int32_t cpw_test_slots(void) { + int32_t n = 0; + for (auto &ed : editors) n += ed ? 1 : 0; + return n; +} +#endif diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 00000000..5094502d --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,3 @@ + +# Written by the web command +public/codepad/ diff --git a/example/app.json b/example/app.json index 0709d6f7..1264fc86 100644 --- a/example/app.json +++ b/example/app.json @@ -13,6 +13,10 @@ }, "android": { "package": "me.sectiontn.codepad.example" + }, + "web": { + "bundler": "metro", + "output": "single" } } } diff --git a/example/modules/codepad-lang-kv_lite/package.json b/example/modules/codepad-lang-kv_lite/package.json index 1ae75483..785d6d6d 100644 --- a/example/modules/codepad-lang-kv_lite/package.json +++ b/example/modules/codepad-lang-kv_lite/package.json @@ -12,6 +12,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "!android/build", "!android/.cxx" @@ -27,6 +28,7 @@ }, "codepad": { "generated": true, - "core": "0.1.12" + "core": "0.1.12", + "symbol": "kv_lite" } } diff --git a/example/package.json b/example/package.json index 3f7aac58..f7b428d1 100644 --- a/example/package.json +++ b/example/package.json @@ -7,18 +7,21 @@ "start": "expo start --dev-client", "ios": "expo run:ios", "android": "expo run:android --variant debugOptimized", + "web": "node ../lib/cli/bin.js web public && expo start --web --port 8082", "build:ios": "xcodebuild ONLY_ACTIVE_ARCH=YES -workspace ios/CodepadExample.xcworkspace -UseNewBuildSystem=YES -scheme CodepadExample -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build -quiet", "build:android": "cd android && ./gradlew :app:assembleDebugOptimized --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a" }, "dependencies": { "@expo-google-fonts/jetbrains-mono": "^0.4.1", "@expo/material-symbols": "^0.1.1", + "@expo/metro-runtime": "~57.0.18", "@expo/ui": "~57.0.25", "expo": "~57.0.30", "expo-font": "~57.0.4", "expo-linear-gradient": "~57.0.2", "expo-status-bar": "~57.0.1", "react": "19.2.3", + "react-dom": "19.2.3", "react-native": "0.86.3", "react-native-codepad-lang-c": "*", "react-native-codepad-lang-css": "*", @@ -43,6 +46,7 @@ "react-native-nitro-modules": "^0.37.1", "react-native-reanimated": "^4.7.1", "react-native-safe-area-context": "~5.7.0", + "react-native-web": "^0.21.2", "react-native-worklets": "^0.13.0" }, "devDependencies": { @@ -54,6 +58,8 @@ "@react-native/metro-config": "0.86.3", "@react-native/typescript-config": "0.86.3", "@types/react": "~19.2.4", + "@types/react-dom": "^19.2.0", + "@types/react-native-web": "^0", "babel-preset-expo": "~57.0.15", "react-native-builder-bob": "^0.43.1", "react-native-monorepo-config": "^0.4.0" diff --git a/example/src/App.web.tsx b/example/src/App.web.tsx new file mode 100644 index 00000000..732ad1c2 --- /dev/null +++ b/example/src/App.web.tsx @@ -0,0 +1,9 @@ +import { JetBrainsMono_400Regular, useFonts } from '@expo-google-fonts/jetbrains-mono'; +import { EditorApp } from './EditorApp.web'; + +// The web build has none of the native chrome, so it skips the keyboard and safe area providers. +export default function App() { + const [fontsLoaded, fontError] = useFonts({ JetBrainsMono_400Regular }); + if (!fontsLoaded && !fontError) return null; + return ; +} diff --git a/example/src/EditorApp.web.tsx b/example/src/EditorApp.web.tsx new file mode 100644 index 00000000..761f84ef --- /dev/null +++ b/example/src/EditorApp.web.tsx @@ -0,0 +1,134 @@ +import { useMemo, useRef, useState } from 'react'; +import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'; +import { CodeEditor, Codepad } from 'react-native-codepad'; +import { GENERATED_FILES, SAMPLE_FILES } from './files'; +import { NESTED_SNIPPET } from './screens/nestedSnippet'; +import { THEMES } from './themes'; + +type Demo = 'editor' | 'nested' | 'two'; +const FILES = [...SAMPLE_FILES, ...GENERATED_FILES.filter((f) => f.id === 'js10k')]; +const FONT = 'JetBrainsMono_400Regular'; + +function Chip({ label, on, onPress }: { label: string; on: boolean; onPress: () => void }) { + return ( + + {label} + + ); +} + +// The example on the web: a file picker, a theme and scrollEnabled toggle, a nested and a two editor demo. +export function EditorApp() { + const [fileId, setFileId] = useState('python'); + const [themeIndex, setThemeIndex] = useState(0); + const [scrollEnabled, setScrollEnabled] = useState(true); + const [demo, setDemo] = useState('editor'); + const theme = THEMES[themeIndex % THEMES.length]?.theme; + const file = FILES.find((f) => f.id === fileId) ?? FILES[0]; + const text = useMemo(() => file?.load() ?? '', [file]); + // Set when a file or a demo is picked, so the first paint counts from the tap. + const started = useRef(performance.now()); + const pick = (set: (value: T) => void, value: T) => { + started.current = performance.now(); + set(value); + }; + const ready = (label: string) => () => + console.log( + `[example] ${label} first paint ${Math.round(performance.now() - started.current)} ms`, + ); + const installed = Codepad.availableLanguages(); + + return ( + + + {(['editor', 'nested', 'two'] as Demo[]).map((d) => ( + pick(setDemo, d)} /> + ))} + setThemeIndex((i) => i + 1)} + /> + setScrollEnabled((s) => !s)} + /> + + + {FILES.map((f) => ( + pick(setFileId, f.id)} + /> + ))} + + {demo === 'editor' ? ( + + ) : null} + {demo === 'nested' ? ( + + + The editor below sizes to its content and the page scrolls, as AlgoTN lays it out. + + + End of the page. + + ) : null} + {demo === 'two' ? ( + + f.id === 'python')?.load()} + language="python" + theme={theme} + fontFamily={FONT} + /> + f.id === 'javascript')?.load()} + language="javascript" + theme={theme} + fontFamily={FONT} + /> + + ) : null} + + ); +} + +const styles = StyleSheet.create({ + page: { flex: 1, height: '100%' }, + bar: { flexDirection: 'row', gap: 8, padding: 8, alignItems: 'center' }, + files: { flexGrow: 0 }, + chip: { paddingHorizontal: 10, paddingVertical: 6, borderRadius: 14, backgroundColor: '#8884' }, + chipOn: { backgroundColor: '#0969da' }, + chipText: { fontSize: 13, color: '#888' }, + chipTextOn: { color: '#fff' }, + editor: { flex: 1 }, + row: { flexDirection: 'row' }, + half: { flex: 1 }, + filler: { padding: 16, fontSize: 15, lineHeight: 600 }, +}); diff --git a/languages/c/package.json b/languages/c/package.json index d1adc47c..9e3edc1f 100644 --- a/languages/c/package.json +++ b/languages/c/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/css/package.json b/languages/css/package.json index 94332ee8..f32eeb00 100644 --- a/languages/css/package.json +++ b/languages/css/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/generator/bin.test.ts b/languages/generator/bin.test.ts index 6c9db650..a6dcabdc 100644 --- a/languages/generator/bin.test.ts +++ b/languages/generator/bin.test.ts @@ -1,5 +1,5 @@ import assert from 'node:assert/strict'; -import { existsSync, mkdtempSync } from 'node:fs'; +import { cpSync, existsSync, mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { test } from 'node:test'; @@ -11,10 +11,23 @@ const out = () => path.join(mkdtempSync(path.join(tmpdir(), 'codepad-bin-')), 'p test('language writes the package and returns 0', () => { const dir = out(); - assert.equal(main(['language', FIXTURE, '--out', dir]), 0); + assert.equal(main(['language', FIXTURE, '--out', dir, '--no-web']), 0); assert.ok(existsSync(path.join(dir, 'package.json'))); }); +test('language warns and returns 0 when it skips the web build', (t) => { + const grammar = path.join(mkdtempSync(path.join(tmpdir(), 'codepad-bin-')), 'kv_lite'); + cpSync(FIXTURE, grammar, { recursive: true }); + rmSync(path.join(grammar, 'grammar.js')); + const written: string[] = []; + t.mock.method(process.stderr, 'write', (s: string) => written.push(s) > 0); + t.mock.method(process.stdout, 'write', () => true); + const dir = out(); + assert.equal(main(['language', grammar, '--out', dir]), 0); + assert.ok(existsSync(path.join(dir, 'package.json'))); + assert.match(written.join(''), /^warning: .*grammar\.js.*--no-web/s); +}); + test('a refused grammar or id returns 1', () => { assert.equal(main(['language', FIXTURE, '--id', 'python', '--out', out()]), 1); }); @@ -24,4 +37,11 @@ test('usage errors return 2', () => { assert.equal(main(['build', FIXTURE]), 2); assert.equal(main(['language']), 2); assert.equal(main(['language', FIXTURE, '--bogus']), 2); + assert.equal(main(['web']), 2); + assert.equal(main(['web', 'a', 'b']), 2); +}); + +test('web refuses a folder that is not an app and returns 1', () => { + const empty = mkdtempSync(path.join(tmpdir(), 'codepad-bin-')); + assert.equal(main(['web', path.join(empty, 'public'), '--root', empty]), 1); }); diff --git a/languages/generator/bin.ts b/languages/generator/bin.ts index 6c592d3c..04710616 100644 --- a/languages/generator/bin.ts +++ b/languages/generator/bin.ts @@ -1,11 +1,15 @@ #!/usr/bin/env node import { realpathSync } from 'node:fs'; +import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseArgs } from 'node:util'; import { CustomLanguageError, emitCustomLanguage } from './custom.ts'; +import { copyWebAssets, WebAssetsError } from './web-assets.ts'; -const USAGE = - 'usage: react-native-codepad language [--id ] [--out ] [--name ]'; +const USAGE = [ + 'usage: react-native-codepad language [--id ] [--out ] [--name ] [--no-web]', + ' react-native-codepad web [--root ]', +].join('\n'); function usage(): number { process.stderr.write(`${USAGE}\n`); @@ -15,22 +19,33 @@ function usage(): number { /** Runs the command line and returns the exit code. */ export function main(argv: string[]): number { const [command, ...rest] = argv; - if (command !== 'language') return usage(); + if (command === 'language') return language(rest); + if (command === 'web') return web(rest); + return usage(); +} + +function language(args: string[]): number { let parsed: ReturnType; try { - parsed = parseLanguageArgs(rest); + parsed = parseLanguageArgs(args); } catch { return usage(); } const [grammarDir, ...extra] = parsed.positionals; if (!grammarDir || extra.length > 0) return usage(); try { - const { id, npmName, outDir } = emitCustomLanguage({ + const { id, npmName, outDir, webSkipped } = emitCustomLanguage({ grammarDir, outDir: parsed.values.out, id: parsed.values.id, name: parsed.values.name, + web: !parsed.values['no-web'], }); + if (webSkipped) { + process.stderr.write( + `warning: skipped the web build. ${webSkipped}\nThe native package is complete. The web command stops on it until it has a web build. Pass --no-web when the app does not run on web.\n`, + ); + } process.stdout.write( `Wrote ${npmName} to ${outDir}.\nRebuild the app (run pod install first on iOS) and pass language="${id}" to CodeEditor.\n`, ); @@ -46,10 +61,43 @@ function parseLanguageArgs(args: string[]) { return parseArgs({ args, allowPositionals: true, - options: { id: { type: 'string' }, out: { type: 'string' }, name: { type: 'string' } }, + options: { + id: { type: 'string' }, + out: { type: 'string' }, + name: { type: 'string' }, + 'no-web': { type: 'boolean' }, + }, }); } +function web(args: string[]): number { + let parsed: ReturnType; + try { + parsed = parseWebArgs(args); + } catch { + return usage(); + } + const [publicDir, ...extra] = parsed.positionals; + if (!publicDir || extra.length > 0) return usage(); + try { + const out = path.resolve(publicDir); + const { languages } = copyWebAssets(path.resolve(parsed.values.root ?? '.'), out); + const ids = languages.map((l) => l.id).join(', ') || 'none'; + process.stdout.write( + `Copied the web core and ${languages.length} languages (${ids}) to ${path.join(out, 'codepad')}.\n`, + ); + return 0; + } catch (e) { + if (!(e instanceof WebAssetsError)) throw e; + process.stderr.write(`error: ${e.message}\n`); + return 1; + } +} + +function parseWebArgs(args: string[]) { + return parseArgs({ args, allowPositionals: true, options: { root: { type: 'string' } } }); +} + // npm links the bin through node_modules/.bin, so compare real paths. const entry = process.argv[1]; if (entry && realpathSync(entry) === fileURLToPath(import.meta.url)) { diff --git a/languages/generator/custom.test.ts b/languages/generator/custom.test.ts index 6472397f..f5cc89c5 100644 --- a/languages/generator/custom.test.ts +++ b/languages/generator/custom.test.ts @@ -6,6 +6,7 @@ import { mkdtempSync, readdirSync, readFileSync, + rmSync, statSync, writeFileSync, } from 'node:fs'; @@ -14,6 +15,7 @@ import path from 'node:path'; import { test } from 'node:test'; import { CustomLanguageError, emitCustomLanguage, SHIPPED_IDS } from './custom.ts'; import { loadManifest, repoRoot } from './manifest.ts'; +import { wasmImporting } from './wasm-fixture.ts'; const FIXTURE = path.join(repoRoot, 'tests/fixtures/kv_lite'); const CORE = JSON.parse(readFileSync(path.join(repoRoot, 'package.json'), 'utf8')) @@ -45,15 +47,20 @@ function rejects(fn: () => unknown, message: RegExp): void { test('emits a private package from the fixture', () => { const out = path.join(temp(), 'pkg'); - const r = emitCustomLanguage({ grammarDir: FIXTURE, outDir: out }); - assert.deepEqual(r, { id: 'kv_lite', npmName: 'codepad-lang-kv_lite', outDir: out }); + const r = emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out }); + assert.deepEqual(r, { + id: 'kv_lite', + npmName: 'codepad-lang-kv_lite', + outDir: out, + webSkipped: null, + }); const pkg = json(out, 'package.json'); assert.equal(pkg.name, 'codepad-lang-kv_lite'); assert.equal(pkg.private, true); assert.equal(pkg.license, 'UNLICENSED'); assert.equal(pkg.version, '1.0.0'); assert.deepEqual(pkg.peerDependencies, { 'react-native-codepad': `^${CORE}` }); - assert.deepEqual(pkg.codepad, { generated: true, core: CORE }); + assert.deepEqual(pkg.codepad, { generated: true, core: CORE, symbol: 'kv_lite' }); assert.equal(pkg.repository, undefined); assert.equal(pkg.author, undefined); assert.match( @@ -77,8 +84,14 @@ test('emits a private package from the fixture', () => { test('--id and --name rename the package, not the C entry point', () => { const out = path.join(temp(), 'pkg'); - const r = emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: 'kv', name: '@acme/kv' }); - assert.deepEqual(r, { id: 'kv', npmName: '@acme/kv', outDir: out }); + const r = emitCustomLanguage({ + web: false, + grammarDir: FIXTURE, + outDir: out, + id: 'kv', + name: '@acme/kv', + }); + assert.deepEqual(r, { id: 'kv', npmName: '@acme/kv', outDir: out, webSkipped: null }); assert.equal(json(out, 'package.json').name, '@acme/kv'); assert.match(read(out, 'ios/CPLangKv.swift'), /tree_sitter_kv_lite\(\)/); assert.match(read(out, 'ios/CPLangKv.swift'), /public let id: String = "kv"/); @@ -88,10 +101,15 @@ test('--id and --name rename the package, not the C entry point', () => { test('relative paths resolve against cwd, and the default out is cwd/modules', () => { const cwd = temp(); cpSync(FIXTURE, path.join(cwd, 'grammars/kv_lite'), { recursive: true }); - const r = emitCustomLanguage({ grammarDir: 'grammars/kv_lite', cwd }); + const r = emitCustomLanguage({ web: false, grammarDir: 'grammars/kv_lite', cwd }); assert.equal(r.outDir, path.join(cwd, 'modules/codepad-lang-kv_lite')); assert.ok(existsSync(path.join(r.outDir, 'package.json'))); - const other = emitCustomLanguage({ grammarDir: 'grammars/kv_lite', outDir: 'out/kv', cwd }); + const other = emitCustomLanguage({ + web: false, + grammarDir: 'grammars/kv_lite', + outDir: 'out/kv', + cwd, + }); assert.equal(other.outDir, path.join(cwd, 'out/kv')); }); @@ -105,24 +123,40 @@ test('an npm author object becomes one author string', () => { }), ); const out = path.join(temp(), 'pkg'); - emitCustomLanguage({ grammarDir: dir, outDir: out }); + emitCustomLanguage({ web: false, grammarDir: dir, outDir: out }); assert.equal(json(out, 'package.json').author, 'Ann (https://x)'); assert.equal(json(out, 'package.json').version, '2.0.0'); }); test('rejects ids that cannot name a package', () => { const out = path.join(temp(), 'pkg'); - rejects(() => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: 'Kv' }), /"Kv"/); - rejects(() => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: '1kv' }), /"1kv"/); rejects( - () => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: 'kv-lite' }), + () => emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out, id: 'Kv' }), + /"Kv"/, + ); + rejects( + () => emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out, id: '1kv' }), + /"1kv"/, + ); + rejects( + () => emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out, id: 'kv-lite' }), /"kv-lite"/, ); - rejects(() => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: 'fun' }), /Kotlin/); - rejects(() => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: 'int' }), /Java/); - rejects(() => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: 'python' }), /ships/); rejects( - () => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out, id: 'markdown_inline' }), + () => emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out, id: 'fun' }), + /Kotlin/, + ); + rejects( + () => emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out, id: 'int' }), + /Java/, + ); + rejects( + () => emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out, id: 'python' }), + /ships/, + ); + rejects( + () => + emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out, id: 'markdown_inline' }), /ships/, ); assert.equal(existsSync(out), false); @@ -137,24 +171,27 @@ test('rejects a grammar folder it cannot build', () => { }; const parser = (dir: string) => path.join(dir, 'src/parser.c'); rejects( - () => emitCustomLanguage({ grammarDir: path.join(temp(), 'empty'), outDir: out() }), + () => emitCustomLanguage({ web: false, grammarDir: path.join(temp(), 'empty'), outDir: out() }), /src\/parser\.c/, ); const noSymbol = broken((d) => writeFileSync(parser(d), read(d, 'src/parser.c').replace(/tree_sitter_kv_lite/g, 'kv')), ); - rejects(() => emitCustomLanguage({ grammarDir: noSymbol, outDir: out() }), /tree_sitter_/); rejects( - () => emitCustomLanguage({ grammarDir: withoutParserH(), outDir: out() }), + () => emitCustomLanguage({ web: false, grammarDir: noSymbol, outDir: out() }), + /tree_sitter_/, + ); + rejects( + () => emitCustomLanguage({ web: false, grammarDir: withoutParserH(), outDir: out() }), /src\/tree_sitter\/parser\.h/, ); const cxx = broken((d) => writeFileSync(path.join(d, 'src/scanner.cc'), '')); - rejects(() => emitCustomLanguage({ grammarDir: cxx, outDir: out() }), /scanner\.cc/); + rejects(() => emitCustomLanguage({ web: false, grammarDir: cxx, outDir: out() }), /scanner\.cc/); const outside = broken((d) => writeFileSync(path.join(d, 'src/scanner.c'), '#include"../common/scanner.h"\n'), ); rejects( - () => emitCustomLanguage({ grammarDir: outside, outDir: out() }), + () => emitCustomLanguage({ web: false, grammarDir: outside, outDir: out() }), /\.\.\/common\/scanner\.h/, ); const outsideHeader = broken((d) => { @@ -162,7 +199,7 @@ test('rejects a grammar folder it cannot build', () => { writeFileSync(path.join(d, 'src/tag.h'), '#include "../../common/tag.h"\n'); }); rejects( - () => emitCustomLanguage({ grammarDir: outsideHeader, outDir: out() }), + () => emitCustomLanguage({ web: false, grammarDir: outsideHeader, outDir: out() }), /src\/tag\.h includes "\.\.\/\.\.\/common\/tag\.h"/, ); const oldAbi = broken((d) => @@ -174,9 +211,9 @@ test('rejects a grammar folder it cannot build', () => { ), ), ); - rejects(() => emitCustomLanguage({ grammarDir: oldAbi, outDir: out() }), /ABI 12/); + rejects(() => emitCustomLanguage({ web: false, grammarDir: oldAbi, outDir: out() }), /ABI 12/); rejects( - () => emitCustomLanguage({ grammarDir: withoutHighlights(), outDir: out() }), + () => emitCustomLanguage({ web: false, grammarDir: withoutHighlights(), outDir: out() }), /queries\/highlights\.scm/, ); }); @@ -206,7 +243,7 @@ test('stops on a pattern it would drop, and on inherits', () => { '((key) @variable (#is-not? local))\n', ); rejects( - () => emitCustomLanguage({ grammarDir: dropped, outDir: path.join(temp(), 'pkg') }), + () => emitCustomLanguage({ web: false, grammarDir: dropped, outDir: path.join(temp(), 'pkg') }), /is-not\?/, ); const inherits = fixtureCopy(); @@ -215,7 +252,8 @@ test('stops on a pattern it would drop, and on inherits', () => { '; inherits: ecma\n(key) @property\n', ); rejects( - () => emitCustomLanguage({ grammarDir: inherits, outDir: path.join(temp(), 'pkg') }), + () => + emitCustomLanguage({ web: false, grammarDir: inherits, outDir: path.join(temp(), 'pkg') }), /inherits/, ); }); @@ -223,16 +261,19 @@ test('stops on a pattern it would drop, and on inherits', () => { test('refuses an output folder it did not generate', () => { const out = temp(); writeFileSync(path.join(out, 'package.json'), JSON.stringify({ name: 'my-app' })); - rejects(() => emitCustomLanguage({ grammarDir: FIXTURE, outDir: out }), /not generated/); + rejects( + () => emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out }), + /not generated/, + ); assert.equal(existsSync(path.join(out, 'android')), false); }); test('regenerates its own output and removes stale files', () => { const out = path.join(temp(), 'pkg'); - emitCustomLanguage({ grammarDir: FIXTURE, outDir: out }); + emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out }); const stale = path.join(out, 'queries/kv_lite/injections.scm'); writeFileSync(stale, ''); - emitCustomLanguage({ grammarDir: FIXTURE, outDir: out }); + emitCustomLanguage({ web: false, grammarDir: FIXTURE, outDir: out }); assert.equal(existsSync(stale), false); assert.ok(existsSync(path.join(out, 'queries/kv_lite/highlights.scm'))); }); @@ -251,7 +292,79 @@ test('refuses a grammar whose C name is a grammar CodePad ships', () => { ); rejects( () => - emitCustomLanguage({ grammarDir: dir, outDir: path.join(temp(), 'pkg'), id: 'python_ext' }), + emitCustomLanguage({ + web: false, + grammarDir: dir, + outDir: path.join(temp(), 'pkg'), + id: 'python_ext', + }), /tree_sitter_python.*grammar\.js/s, ); }); + +test('builds the web grammar when the core provides its imports', () => { + const r = emitCustomLanguage({ + grammarDir: FIXTURE, + outDir: path.join(temp(), 'pkg'), + buildWasm: (_dir, out) => writeFileSync(out, wasmImporting(['malloc'])), + allowedImports: ['malloc'], + }); + assert.ok(existsSync(path.join(r.outDir, 'web/kv_lite.wasm'))); + assert.equal(r.webSkipped, null); +}); + +// Each web build problem leaves the native package written and the web folder out, with the reason. +function skipsWeb(opts: Partial[0]>, reason: RegExp): void { + const r = emitCustomLanguage({ grammarDir: FIXTURE, outDir: path.join(temp(), 'pkg'), ...opts }); + assert.ok(existsSync(path.join(r.outDir, 'package.json'))); + assert.ok(existsSync(path.join(r.outDir, 'grammar/src/parser.c'))); + assert.equal(existsSync(path.join(r.outDir, 'web')), false); + assert.match(r.webSkipped ?? '', reason); +} + +test('skips the web build naming an import the core lacks', () => { + skipsWeb( + { + buildWasm: (_dir, out) => writeFileSync(out, wasmImporting(['malloc', 'qsort'])), + allowedImports: ['malloc'], + }, + /kv_lite\.wasm imports qsort, which the web core does not provide/, + ); +}); + +test('skips the web build when grammar.js is missing', () => { + const dir = fixtureCopy(); + rmSync(path.join(dir, 'grammar.js')); + skipsWeb({ grammarDir: dir, allowedImports: ['malloc'] }, /grammar\.js/); +}); + +test('skips the web build when the web core is not built', () => { + const coreRoot = temp(); + const header = 'cpp/tree-sitter/lib/include/tree_sitter/api.h'; + cpSync(path.join(repoRoot, 'package.json'), path.join(coreRoot, 'package.json')); + mkdirSync(path.dirname(path.join(coreRoot, header)), { recursive: true }); + cpSync(path.join(repoRoot, header), path.join(coreRoot, header)); + skipsWeb({ coreRoot, buildWasm: () => {} }, /lib\/web/); +}); + +test('skips the web build when the wasm build fails', () => { + skipsWeb( + { + buildWasm: () => { + throw new Error('network down'); + }, + allowedImports: ['malloc'], + }, + /tree-sitter build --wasm failed/, + ); +}); + +test('a renamed id keeps the grammar entry point for the web loader', () => { + const r = emitCustomLanguage({ + web: false, + grammarDir: FIXTURE, + outDir: path.join(temp(), 'pkg'), + id: 'kv', + }); + assert.equal(json(r.outDir, 'package.json').codepad.symbol, 'kv_lite'); +}); diff --git a/languages/generator/custom.ts b/languages/generator/custom.ts index 64f16daa..788537b0 100644 --- a/languages/generator/custom.ts +++ b/languages/generator/custom.ts @@ -1,4 +1,5 @@ -import { existsSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs'; +import { execFileSync } from 'node:child_process'; +import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { captureNames } from './captures.ts'; @@ -8,6 +9,7 @@ import { copyTree, parserAbi, readAbiRange } from './grammar-files.ts'; import { languageSymbol } from './host.ts'; import { parseInherits } from './queries.ts'; import { rewriteQuery } from './rewrite.ts'; +import { grammarImportsOutside } from './wasm-imports.ts'; /** A grammar folder, an id or an output folder the command refuses, with the reason for the user. */ export class CustomLanguageError extends Error {} @@ -49,7 +51,7 @@ const RESERVED = new Set( ); const INCLUDE = /^\s*#\s*include\s*"([^"]+)"/gm; const ID = /^[a-z][a-z0-9_]*$/; -const GENERATED_DIRS = ['android', 'ios', 'queries', 'src', 'grammar']; +const GENERATED_DIRS = ['android', 'ios', 'queries', 'src', 'grammar', 'web']; const GUIDE = 'https://reactnativecodepad.pages.dev/guides/custom-languages/'; // languages/generator in the repo and lib/cli in the package both sit two levels under the core root. @@ -62,9 +64,19 @@ export type CustomOptions = { name?: string; cwd?: string; coreRoot?: string; + // Builds web/.wasm for the browser, on by default. + web?: boolean; + buildWasm?: (grammarDir: string, out: string) => void; + allowedImports?: readonly string[]; }; -export type CustomResult = { id: string; npmName: string; outDir: string }; +// webSkipped says why the web build was left out, null when it was built or not asked for. +export type CustomResult = { + id: string; + npmName: string; + outDir: string; + webSkipped: string | null; +}; function fail(message: string): never { throw new CustomLanguageError(message); @@ -218,6 +230,15 @@ export function emitCustomLanguage(opts: CustomOptions): CustomResult { const highlightsFile = path.join(grammarDir, 'queries/highlights.scm'); if (!existsSync(highlightsFile)) fail('queries/highlights.scm not found.'); + const web = opts.web ?? true; + let webSkipped: string | null = null; + if (web && !opts.buildWasm && !existsSync(path.join(grammarDir, 'grammar.js'))) { + webSkipped = 'grammar.js is not next to src/, and the web build needs it.'; + } + const allowed = web && !webSkipped ? (opts.allowedImports ?? coreImports(coreRoot)) : null; + if (web && !webSkipped && !allowed) { + webSkipped = 'react-native-codepad has no web core build (lib/web).'; + } const injectionsFile = path.join(grammarDir, 'queries/injections.scm'); const queries: GrammarQueries = { highlights: customQuery(highlightsFile, 'queries/highlights.scm', false), @@ -250,13 +271,15 @@ export function emitCustomLanguage(opts: CustomOptions): CustomResult { 'ios', 'grammar', 'queries', + 'web', '*.podspec', '!android/build', '!android/.cxx', ], keywords: ['react-native', 'codepad', 'tree-sitter', id], peerDependencies: { 'react-native-codepad': `^${core}` }, - codepad: { generated: true, core }, + // The C entry point stays tree_sitter_ when --id renames the package. + codepad: { generated: true, core, symbol }, }; emitPackage({ entry: { id, grammars: [{ name: id, src: 'src', symbol }] }, @@ -272,5 +295,45 @@ export function emitCustomLanguage(opts: CustomOptions): CustomResult { podFallbacks: true, }, }); - return { id, npmName, outDir }; + if (allowed && !webSkipped) { + webSkipped = buildWeb(grammarDir, outDir, id, allowed, opts.buildWasm ?? treeSitterWasm); + } + return { id, npmName, outDir, webSkipped }; +} + +// The C functions the web core exports to grammars, written by cpp/web/build.sh. +function coreImports(coreRoot: string): string[] | null { + const list = readJson(path.join(coreRoot, 'lib/web/grammar-imports.json')); + return Array.isArray(list) ? (list as string[]) : null; +} + +// tree-sitter's own CLI builds the grammar with a compiler it downloads, so no Emscripten is needed here. +function treeSitterWasm(grammarDir: string, out: string): void { + execFileSync('npx', ['--yes', 'tree-sitter-cli@0.26', 'build', '--wasm', '-o', out, grammarDir], { + stdio: 'inherit', + }); +} + +// Builds web/.wasm and returns null, or removes the web folder and returns why it could not. +function buildWeb( + grammarDir: string, + outDir: string, + id: string, + allowed: readonly string[], + build: (grammarDir: string, out: string) => void, +): string | null { + const dir = path.join(outDir, 'web'); + const out = path.join(dir, `${id}.wasm`); + mkdirSync(dir, { recursive: true }); + let extra: string[]; + try { + build(grammarDir, out); + extra = grammarImportsOutside(readFileSync(out), allowed); + } catch { + rmSync(dir, { recursive: true, force: true }); + return 'tree-sitter build --wasm failed.'; + } + if (extra.length === 0) return null; + rmSync(dir, { recursive: true, force: true }); + return `${id}.wasm imports ${extra.join(', ')}, which the web core does not provide.`; } diff --git a/languages/generator/emit.test.ts b/languages/generator/emit.test.ts index 48591b98..d3fef243 100644 --- a/languages/generator/emit.test.ts +++ b/languages/generator/emit.test.ts @@ -37,6 +37,19 @@ test('a shipped package publishes its third party notices', () => { assert.ok((identity.packageJson.files as string[]).includes('THIRD_PARTY_NOTICES.md')); }); +// The browser loads each grammar from web/.wasm, built before publishing. +test('shipped package files include web', () => { + const entry = { id: 'ini', repo: 'r', rev: 'x', grammars: [{ name: 'ini', src: 'src' }] }; + const identity = shippedIdentity({ + entry, + version: '1.2.3', + queries: {}, + grammarLicense: null, + nvimRev: 'n', + }); + assert.ok((identity.packageJson.files as string[]).includes('web')); +}); + test('jniName escapes underscores in package segments', () => { assert.equal( jniName( diff --git a/languages/generator/emit.ts b/languages/generator/emit.ts index 43e59ff5..89d0ab44 100644 --- a/languages/generator/emit.ts +++ b/languages/generator/emit.ts @@ -321,6 +321,7 @@ function shippedPackageJson(a: ShippedArgs): string { 'ios', 'grammar', 'queries', + 'web', '*.podspec', 'THIRD_PARTY_NOTICES.md', '!android/build', diff --git a/languages/generator/wasm-fixture.ts b/languages/generator/wasm-fixture.ts new file mode 100644 index 00000000..09e574ae --- /dev/null +++ b/languages/generator/wasm-fixture.ts @@ -0,0 +1,18 @@ +// A minimal WASM module whose only content is function imports from env, for the import check tests. +export function wasmImporting(names: string[]): Uint8Array { + const text = (s: string) => [s.length, ...Buffer.from(s, 'utf8')]; + const imports = [names.length, ...names.flatMap((n) => [...text('env'), ...text(n), 0x00, 0x00])]; + const section = (id: number, body: number[]) => [id, body.length, ...body]; + return Uint8Array.from([ + 0x00, + 0x61, + 0x73, + 0x6d, + 0x01, + 0x00, + 0x00, + 0x00, + ...section(1, [0x01, 0x60, 0x00, 0x00]), + ...section(2, imports), + ]); +} diff --git a/languages/generator/wasm-imports.test.ts b/languages/generator/wasm-imports.test.ts new file mode 100644 index 00000000..2b2599ce --- /dev/null +++ b/languages/generator/wasm-imports.test.ts @@ -0,0 +1,11 @@ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { wasmImporting } from './wasm-fixture.ts'; +import { grammarImportsOutside } from './wasm-imports.ts'; + +test('finds an import outside the list', () => { + assert.deepEqual(grammarImportsOutside(wasmImporting(['malloc', 'qsort']), ['malloc']), [ + 'qsort', + ]); + assert.deepEqual(grammarImportsOutside(wasmImporting(['malloc']), ['malloc', 'free']), []); +}); diff --git a/languages/generator/wasm-imports.ts b/languages/generator/wasm-imports.ts new file mode 100644 index 00000000..281050a7 --- /dev/null +++ b/languages/generator/wasm-imports.ts @@ -0,0 +1,27 @@ +type WasmModule = object; +type Descriptor = { module: string; name: string; kind: string }; + +// The CLI compiles without the DOM lib, which is where TypeScript declares WebAssembly. +declare const WebAssembly: { + Module: { + new (bytes: Uint8Array): WasmModule; + imports(module: WasmModule): Descriptor[]; + exports(module: WasmModule): Omit[]; + }; +}; + +// What a grammar side module needs from the core that the core does not export. +export function grammarImportsOutside(wasm: Uint8Array, allowed: readonly string[]): string[] { + const module = new WebAssembly.Module(wasm); + const own = new Set(WebAssembly.Module.exports(module).map((e) => e.name)); + const ok = new Set(allowed); + const missing = new Set(); + for (const i of WebAssembly.Module.imports(module)) { + // GOT entries name the module's own symbols or the core's, functions and data alike. + const got = i.module === 'GOT.func' || i.module === 'GOT.mem'; + if (!got && i.kind !== 'function') continue; + if (got && own.has(i.name)) continue; + if (!ok.has(i.name)) missing.add(i.name); + } + return [...missing].sort(); +} diff --git a/languages/generator/web-assets.test.ts b/languages/generator/web-assets.test.ts new file mode 100644 index 00000000..9702734d --- /dev/null +++ b/languages/generator/web-assets.test.ts @@ -0,0 +1,138 @@ +import assert from 'node:assert/strict'; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path from 'node:path'; +import { test } from 'node:test'; +import { copyWebAssets, WebAssetsError } from './web-assets.ts'; + +function write(file: string, text: string): void { + mkdirSync(path.dirname(file), { recursive: true }); + writeFileSync(file, text); +} + +// An app with the core, two language packages (one holding two grammars) and a plain dependency. +function fakeApp(): { app: string; core: string; out: string } { + const dir = mkdtempSync(path.join(tmpdir(), 'codepad-web-')); + const app = path.join(dir, 'app'); + const core = path.join(dir, 'core'); + write(path.join(core, 'codepad.js'), 'js'); + write(path.join(core, 'codepad.wasm'), 'wasm'); + const deps = { 'lang-a': '*', 'lang-b': '*', lodash: '*' }; + write(path.join(app, 'package.json'), JSON.stringify({ name: 'app', dependencies: deps })); + const mod = (name: string) => path.join(app, 'node_modules', name); + write(path.join(mod('lang-a'), 'package.json'), JSON.stringify({ name: 'lang-a', codepad: {} })); + write(path.join(mod('lang-a'), 'web/a.wasm'), 'a'); + write(path.join(mod('lang-a'), 'queries/a/highlights.scm'), '(x) @a'); + write(path.join(mod('lang-a'), 'queries/a/injections.scm'), '(y) @injection.content'); + write(path.join(mod('lang-b'), 'package.json'), JSON.stringify({ name: 'lang-b', codepad: {} })); + for (const g of ['b', 'b_inline']) { + write(path.join(mod('lang-b'), `web/${g}.wasm`), g); + write(path.join(mod('lang-b'), `queries/${g}/highlights.scm`), `(${g}) @b`); + } + write(path.join(mod('lodash'), 'package.json'), JSON.stringify({ name: 'lodash' })); + return { app, core, out: path.join(dir, 'public') }; +} + +test('copies the core and each language with its queries', () => { + const { app, core, out } = fakeApp(); + const manifest = copyWebAssets(app, out, core); + const dest = path.join(out, 'codepad'); + assert.equal(readFileSync(path.join(dest, 'codepad.wasm'), 'utf8'), 'wasm'); + assert.equal(readFileSync(path.join(dest, 'codepad.js'), 'utf8'), 'js'); + assert.deepEqual(manifest, { + version: 1, + core: { js: 'codepad.js', wasm: 'codepad.wasm' }, + languages: [ + { + id: 'a', + wasm: 'a.wasm', + symbol: 'tree_sitter_a', + highlights: 'a.highlights.scm', + injections: 'a.injections.scm', + }, + { + id: 'b', + wasm: 'b.wasm', + symbol: 'tree_sitter_b', + highlights: 'b.highlights.scm', + injections: null, + }, + { + id: 'b_inline', + wasm: 'b_inline.wasm', + symbol: 'tree_sitter_b_inline', + highlights: 'b_inline.highlights.scm', + injections: null, + }, + ], + }); + assert.deepEqual(JSON.parse(readFileSync(path.join(dest, 'languages.json'), 'utf8')), manifest); + assert.equal(readFileSync(path.join(dest, 'b_inline.wasm'), 'utf8'), 'b_inline'); + assert.equal(readFileSync(path.join(dest, 'a.injections.scm'), 'utf8'), '(y) @injection.content'); +}); + +test('throws naming a package without its wasm', () => { + const { app, core, out } = fakeApp(); + write(path.join(app, 'node_modules/lang-b/queries/c/highlights.scm'), '(c) @c'); + assert.throws( + () => copyWebAssets(app, out, core), + (e: unknown) => { + assert.ok(e instanceof WebAssetsError); + assert.match(e.message, /lang-b has no web\/c\.wasm/); + return true; + }, + ); +}); + +test('ignores dependencies without a codepad field', () => { + const { app, core, out } = fakeApp(); + write(path.join(app, 'node_modules/lodash/web/x.wasm'), 'x'); + write(path.join(app, 'node_modules/lodash/queries/x/highlights.scm'), '(x) @x'); + const ids = copyWebAssets(app, out, core).languages.map((l) => l.id); + assert.deepEqual(ids, ['a', 'b', 'b_inline']); + assert.ok(!existsSync(path.join(out, 'codepad/x.wasm'))); +}); + +test('finds the custom languages in the app modules folder', () => { + const { app, core, out } = fakeApp(); + const local = path.join(app, 'modules/codepad-lang-algo'); + write( + path.join(local, 'package.json'), + JSON.stringify({ name: 'codepad-lang-algo', codepad: {} }), + ); + write(path.join(local, 'web/algo.wasm'), 'algo'); + write(path.join(local, 'queries/algo/highlights.scm'), '(x) @algo'); + const ids = copyWebAssets(app, out, core).languages.map((l) => l.id); + assert.deepEqual(ids, ['a', 'b', 'b_inline', 'algo']); +}); + +test('a custom package renamed with --id keeps its grammar entry point', () => { + const { app, core, out } = fakeApp(); + const local = path.join(app, 'modules/codepad-lang-kv'); + const json = { name: 'codepad-lang-kv', codepad: { symbol: 'kv_lite' } }; + write(path.join(local, 'package.json'), JSON.stringify(json)); + write(path.join(local, 'web/kv.wasm'), 'kv'); + write(path.join(local, 'queries/kv/highlights.scm'), '(x) @kv'); + const kv = copyWebAssets(app, out, core).languages.find((l) => l.id === 'kv'); + assert.equal(kv?.symbol, 'tree_sitter_kv_lite'); +}); + +test('refuses a grammar named codepad, which would replace the core', () => { + const { app, core, out } = fakeApp(); + const local = path.join(app, 'modules/codepad-lang-codepad'); + write( + path.join(local, 'package.json'), + JSON.stringify({ name: 'codepad-lang-codepad', codepad: {} }), + ); + write(path.join(local, 'web/codepad.wasm'), 'grammar'); + write(path.join(local, 'queries/codepad/highlights.scm'), '(x) @codepad'); + assert.throws( + () => copyWebAssets(app, out, core), + (e: unknown) => { + assert.ok(e instanceof WebAssetsError); + assert.match(e.message, /codepad-lang-codepad.*codepad/); + return true; + }, + ); + assert.equal(readFileSync(path.join(out, 'codepad/codepad.wasm'), 'utf8'), 'wasm'); +}); diff --git a/languages/generator/web-assets.ts b/languages/generator/web-assets.ts new file mode 100644 index 00000000..aa0af842 --- /dev/null +++ b/languages/generator/web-assets.ts @@ -0,0 +1,144 @@ +import { + copyFileSync, + existsSync, + mkdirSync, + readdirSync, + readFileSync, + writeFileSync, +} from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +export type WebLanguage = { + id: string; + wasm: string; + symbol: string; + highlights: string | null; + injections: string | null; +}; + +export type WebManifest = { + version: 1; + core: { js: 'codepad.js'; wasm: 'codepad.wasm' }; + languages: WebLanguage[]; +}; + +export class WebAssetsError extends Error {} + +type Json = Record; + +function readJson(file: string): Json | null { + try { + return JSON.parse(readFileSync(file, 'utf8')) as Json; + } catch { + return null; + } +} + +// The folder of an installed package, looked up through node_modules from dir upward like Node does. +function packageDir(name: string, from: string): string | null { + for (let dir = path.resolve(from); ; dir = path.dirname(dir)) { + const candidate = path.join(dir, 'node_modules', name); + if (existsSync(path.join(candidate, 'package.json'))) return candidate; + if (path.dirname(dir) === dir) return null; + } +} + +// The web core this command ships with: lib/web next to lib/cli, or the repo's lib/web when run from source. +function ownCore(): string { + const here = path.dirname(fileURLToPath(import.meta.url)); + const built = path.join(here, '..', 'web'); + return existsSync(path.join(built, 'codepad.wasm')) + ? built + : path.join(here, '..', '..', 'lib', 'web'); +} + +// symbol is the C name of a custom package's one grammar, kept when --id renamed the package. +type LanguagePackage = { name: string; dir: string; symbol?: string | undefined }; + +// Every language package of the app: its dependencies, then the custom packages in its modules folder. +function languagePackages(appRoot: string): LanguagePackage[] { + const pkg = readJson(path.join(appRoot, 'package.json')); + if (!pkg) throw new WebAssetsError(`${appRoot} has no package.json`); + const out: LanguagePackage[] = []; + const add = (dir: string, fallbackName: string) => { + const json = readJson(path.join(dir, 'package.json')); + const codepad = json?.codepad as Json | undefined; + if (!json || !codepad) return; + const name = typeof json.name === 'string' ? json.name : fallbackName; + const symbol = typeof codepad.symbol === 'string' ? codepad.symbol : undefined; + out.push({ name, dir, symbol }); + }; + for (const name of Object.keys((pkg.dependencies as Json | undefined) ?? {})) { + const dir = packageDir(name, appRoot); + if (dir) add(dir, name); + } + const modules = path.join(appRoot, 'modules'); + for (const entry of existsSync(modules) ? readdirSync(modules).sort() : []) { + add(path.join(modules, entry), entry); + } + return out; +} + +// Copies the web core and every installed language with its queries into /codepad, and writes the +// languages.json the browser loader reads. +export function copyWebAssets( + appRoot: string, + publicDir: string, + coreDir = ownCore(), +): WebManifest { + const dest = path.join(publicDir, 'codepad'); + for (const file of ['codepad.js', 'codepad.wasm']) { + if (!existsSync(path.join(coreDir, file))) { + throw new WebAssetsError( + `${coreDir} has no ${file}. The package was built without its web core.`, + ); + } + } + mkdirSync(dest, { recursive: true }); + copyFileSync(path.join(coreDir, 'codepad.js'), path.join(dest, 'codepad.js')); + copyFileSync(path.join(coreDir, 'codepad.wasm'), path.join(dest, 'codepad.wasm')); + const languages: WebLanguage[] = []; + for (const { name, dir, symbol } of languagePackages(appRoot)) { + const queries = path.join(dir, 'queries'); + const grammars = existsSync(queries) + ? readdirSync(queries) + .filter((g) => existsSync(path.join(queries, g, 'highlights.scm'))) + .sort() + : []; + for (const grammar of grammars) { + if (grammar === 'codepad') { + throw new WebAssetsError( + `${name} has a grammar named codepad, the name of the core's files. Build it with another --id.`, + ); + } + const wasm = path.join(dir, 'web', `${grammar}.wasm`); + if (!existsSync(wasm)) { + throw new WebAssetsError( + `${name} has no web/${grammar}.wasm. It was published without its web build.`, + ); + } + copyFileSync(wasm, path.join(dest, `${grammar}.wasm`)); + const copyQuery = (kind: string): string | null => { + const from = path.join(queries, grammar, `${kind}.scm`); + if (!existsSync(from)) return null; + copyFileSync(from, path.join(dest, `${grammar}.${kind}.scm`)); + return `${grammar}.${kind}.scm`; + }; + languages.push({ + id: grammar, + wasm: `${grammar}.wasm`, + symbol: `tree_sitter_${symbol && grammars.length === 1 ? symbol : grammar}`, + highlights: copyQuery('highlights'), + injections: copyQuery('injections'), + }); + } + } + const manifest: WebManifest = { + version: 1, + core: { js: 'codepad.js', wasm: 'codepad.wasm' }, + languages, + }; + writeFileSync(path.join(dest, 'languages.json'), `${JSON.stringify(manifest, null, 2)}\n`); + return manifest; +} diff --git a/languages/go/package.json b/languages/go/package.json index aceba866..78635dcf 100644 --- a/languages/go/package.json +++ b/languages/go/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/html/package.json b/languages/html/package.json index 44e397e4..cfc4a3f6 100644 --- a/languages/html/package.json +++ b/languages/html/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/ini/package.json b/languages/ini/package.json index cf8082c4..40a417a3 100644 --- a/languages/ini/package.json +++ b/languages/ini/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/java/package.json b/languages/java/package.json index 477860c2..c51dd1d8 100644 --- a/languages/java/package.json +++ b/languages/java/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/javascript/package.json b/languages/javascript/package.json index 40fd0b74..9e49f4d9 100644 --- a/languages/javascript/package.json +++ b/languages/javascript/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/json/package.json b/languages/json/package.json index d80061a8..70dacb43 100644 --- a/languages/json/package.json +++ b/languages/json/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/kotlin/package.json b/languages/kotlin/package.json index 7b638e2e..045cf005 100644 --- a/languages/kotlin/package.json +++ b/languages/kotlin/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/markdown/package.json b/languages/markdown/package.json index f81067bf..993c035a 100644 --- a/languages/markdown/package.json +++ b/languages/markdown/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/php/package.json b/languages/php/package.json index 7c9a0cb8..380d4abc 100644 --- a/languages/php/package.json +++ b/languages/php/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/python/package.json b/languages/python/package.json index 7994cd13..bea848a2 100644 --- a/languages/python/package.json +++ b/languages/python/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/rust/package.json b/languages/rust/package.json index 20dbea83..538c80d2 100644 --- a/languages/rust/package.json +++ b/languages/rust/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/sql/package.json b/languages/sql/package.json index 1d346590..c0d76b2a 100644 --- a/languages/sql/package.json +++ b/languages/sql/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/swift/package.json b/languages/swift/package.json index f4882da0..2e6e637c 100644 --- a/languages/swift/package.json +++ b/languages/swift/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/tsx/package.json b/languages/tsx/package.json index 0acfe2e3..9203694c 100644 --- a/languages/tsx/package.json +++ b/languages/tsx/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/typescript/package.json b/languages/typescript/package.json index 9b05e953..8e081532 100644 --- a/languages/typescript/package.json +++ b/languages/typescript/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/xml/package.json b/languages/xml/package.json index c3ff5e32..cdd5f722 100644 --- a/languages/xml/package.json +++ b/languages/xml/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/languages/yaml/package.json b/languages/yaml/package.json index b20dd0df..643270bd 100644 --- a/languages/yaml/package.json +++ b/languages/yaml/package.json @@ -10,6 +10,7 @@ "ios", "grammar", "queries", + "web", "*.podspec", "THIRD_PARTY_NOTICES.md", "!android/build", diff --git a/package.json b/package.json index 732edd2b..c2f283c0 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,12 @@ "types": "./lib/typescript/src/index.d.ts", "exports": { ".": { - "react-native-codepad-source": "./src/index.ts", + "react-native-codepad-source": { + "browser": "./src/index.web.ts", + "default": "./src/index.ts" + }, "types": "./lib/typescript/src/index.d.ts", + "browser": "./lib/module/index.web.js", "default": "./lib/module/index.js" }, "./package.json": "./package.json" @@ -41,6 +45,9 @@ "prepare": "bob build && yarn build:cli", "nitrogen": "nitrogen && node scripts/sync-view-config.mjs", "build:cli": "node scripts/build-cli.mjs", + "build:web": "cpp/web/build.sh", + "build:web-grammars": "node scripts/build-web-grammars.mjs", + "test:web": "node --test tests/web/*.e2e.mjs", "gen:languages": "node languages/generator/cli.ts gen", "gen:report": "node languages/generator/cli.ts report", "gen:unicode": "node scripts/gen-unicode.mjs", @@ -100,6 +107,7 @@ "jest": "^29.7.0", "lefthook": "^2.1.10", "nitrogen": "0.37.1", + "playwright": "1.64.1", "react": "19.2.3", "react-native": "0.87.1", "react-native-builder-bob": "^0.43.1", diff --git a/scripts/build-web-grammars.mjs b/scripts/build-web-grammars.mjs new file mode 100644 index 00000000..c1fe47df --- /dev/null +++ b/scripts/build-web-grammars.mjs @@ -0,0 +1,59 @@ +// Builds every grammar as a WASM side module into its package's web folder: yarn build:web-grammars [id...] +import { spawn } from 'node:child_process'; +import { existsSync, readdirSync, readFileSync } from 'node:fs'; +import { join } from 'node:path'; + +const root = join(import.meta.dirname, '..'); +const build = join(root, 'cpp/web/build.sh'); +const PARALLEL = 4; + +const jobs = []; +const { languages } = JSON.parse(readFileSync(join(root, 'languages/grammars.json'), 'utf8')); +for (const entry of languages) { + for (const g of entry.grammars) { + jobs.push({ + id: entry.id, + grammar: g.name, + src: join(root, 'languages', entry.id, 'grammar', g.src), + out: join(root, 'languages', entry.id, 'web', `${g.name}.wasm`), + }); + } +} +// The example's custom languages, one grammar each, named by their queries folder. +const modules = join(root, 'example/modules'); +for (const pkg of existsSync(modules) ? readdirSync(modules) : []) { + const dir = join(modules, pkg); + if (!existsSync(join(dir, 'grammar/src/parser.c'))) continue; + for (const grammar of readdirSync(join(dir, 'queries'))) { + jobs.push({ + id: grammar, + grammar, + src: join(dir, 'grammar/src'), + out: join(dir, 'web', `${grammar}.wasm`), + }); + } +} + +const wanted = process.argv.slice(2); +const queue = wanted.length ? jobs.filter((j) => wanted.includes(j.id)) : jobs; +if (wanted.length && queue.length === 0) { + console.error(`no grammar for ${wanted.join(', ')}`); + process.exit(1); +} + +let failed = 0; +async function worker() { + for (let job = queue.shift(); job; job = queue.shift()) { + const code = await new Promise((resolve) => { + const child = spawn(build, ['--grammar', job.src, job.out], { stdio: 'inherit' }); + child.on('close', resolve); + }); + if (code === 0) console.log(`${job.grammar}: ${job.out.slice(root.length + 1)}`); + else failed++; + } +} +await Promise.all(Array.from({ length: PARALLEL }, worker)); +if (failed) { + console.error(`${failed} grammar builds failed`); + process.exit(1); +} diff --git a/src/index.ts b/src/index.ts index 0cc25a0e..34ef5d3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,3 +30,6 @@ export type { export const Codepad: { availableLanguages(): string[] } = NitroModules.createHybridObject('Codepad'); + +// Points the web build at its files. Native finds its languages by itself, so this does nothing here. +export function configureWeb(_options: { assetsUrl?: string }): void {} diff --git a/src/index.web.ts b/src/index.web.ts new file mode 100644 index 00000000..e9937359 --- /dev/null +++ b/src/index.web.ts @@ -0,0 +1,32 @@ +import { availableLanguages } from './web/loader'; + +export { githubDark } from './themes/githubDark'; +export { githubLight } from './themes/githubLight'; +export { oneDark } from './themes/oneDark'; +export type { + CaretDirection, + ChangeEvent, + ChangeOrigin, + CodeEditorProps, + CodeEditorRef, + ContentInsets, + ContentSizeEvent, + Decoration, + GapLayoutEvent, + Position, + Range, + Rect, + ScrollEvent, + SearchHit, + SearchQuery, + Selection, + SelectionChangeEvent, + Suggestions, + Theme, + TokenStyle, +} from './types'; +export { CodeEditor } from './web/CodeEditor'; +export { configureWeb } from './web/configure'; + +// The languages the web command found in the app, once languages.json loaded. +export const Codepad: { availableLanguages(): string[] } = { availableLanguages }; diff --git a/src/web/CodeEditor.tsx b/src/web/CodeEditor.tsx new file mode 100644 index 00000000..af7f339c --- /dev/null +++ b/src/web/CodeEditor.tsx @@ -0,0 +1,282 @@ +/// +import { + type ComponentRef, + forwardRef, + useEffect, + useImperativeHandle, + useMemo, + useRef, + useState, +} from 'react'; +import { View } from 'react-native'; +import { githubLight } from '../themes/githubLight'; +import type { CodeEditorProps, CodeEditorRef, ContentInsets } from '../types'; +import { EditorView, type ViewOptions } from './EditorView'; +import { loadCore } from './loader'; + +const NO_INSETS: ContentInsets = {}; +const warned = new Set(); + +// Methods, props and events plans 2 and 3 bring to the web. Each warns once per page. +function notYet(name: string): void { + if (warned.has(name)) return; + warned.add(name); + console.warn(`[codepad] ${name} is not available on web yet`); +} + +function later(name: string, value: T): Promise { + notYet(name); + return Promise.resolve(value); +} + +// The editor on react-native-web: a View whose node holds lines drawn from the WASM core, outside React's render. +export const CodeEditor = forwardRef(function CodeEditor( + { + initialValue = '', + language = '', + theme = githubLight, + fontFamily = '', + fontSize = 14, + lineHeight = 1.4, + lineNumbers = true, + softWrap = false, + readOnly = false, + highlightActiveLine = true, + matchBrackets = true, + placeholder = '', + scrollEnabled = true, + contentInsets = NO_INSETS, + textPadding = 8, + avoidKeyboard = false, + tabSize = 4, + insertSpaces = true, + autoIndent = true, + autoCloseBrackets = true, + suggestions = 'off', + autoFocus = false, + onReady, + onScroll, + onSelectionChange, + onChange, + onChangeText, + onFocus, + onBlur, + onContentSizeChange, + decorations, + decorationsRevision: _decorationsRevision, + onGapLayout, + ...viewProps + }, + ref, +) { + const node = useRef>(null); + const [view, setView] = useState(null); + const viewRef = useRef(null); + // Writes made before the core loads, run in order once the view exists, or settled unapplied when none comes. + const queued = useRef<{ edit: (v: EditorView) => void; done: () => void }[]>([]); + const gone = useRef(false); + const handlers = useRef({ + onReady, + onScroll, + onSelectionChange, + onChange, + onChangeText, + onContentSizeChange, + }); + handlers.current = { + onReady, + onScroll, + onSelectionChange, + onChange, + onChangeText, + onContentSizeChange, + }; + const initial = useRef(initialValue); + + // The callbacks read the latest handlers and the view through refs, so the object only follows the props. + const options = useMemo>( + () => ({ + theme, + fontFamily, + fontSize, + lineHeight, + lineNumbers, + softWrap, + highlightActiveLine, + matchBrackets, + textPadding, + contentInsets, + scrollEnabled, + placeholder, + readOnly, + tabSize, + insertSpaces, + autoIndent, + autoCloseBrackets, + onContentSize: (width, height) => handlers.current.onContentSizeChange?.({ width, height }), + onScroll: (x, y, contentWidth, contentHeight) => + handlers.current.onScroll?.({ x, y, contentWidth, contentHeight }), + onFirstDraw: () => handlers.current.onReady?.(), + onChange: (c) => { + const h = handlers.current; + h.onChange?.({ + start: c.start, + end: c.start + c.removedLength, + text: c.inserted, + origin: c.origin, + revision: c.revision, + }); + if (h.onChangeText && viewRef.current) h.onChangeText(viewRef.current.core.text()); + }, + onSelection: (anchor, head, revision) => + handlers.current.onSelectionChange?.({ anchor, head, revision }), + }), + [ + theme, + fontFamily, + fontSize, + lineHeight, + lineNumbers, + softWrap, + highlightActiveLine, + matchBrackets, + textPadding, + contentInsets, + scrollEnabled, + placeholder, + readOnly, + tabSize, + insertSpaces, + autoIndent, + autoCloseBrackets, + ], + ); + const latest = useRef({ options, language }); + latest.current = { options, language }; + + // Options and language reach the view before its first draw, which the pump runs before React commits. + useEffect(() => { + let alive = true; + let created: EditorView | null = null; + gone.current = false; + const drop = () => { + for (const write of queued.current.splice(0)) write.done(); + }; + loadCore().then( + (m) => { + const host = node.current as unknown as HTMLElement | null; + if (!alive || !host) return; + const v = new EditorView(host, m, initial.current); + created = v; + v.setOptions(latest.current.options); + v.setLanguage(latest.current.language); + viewRef.current = v; + for (const write of queued.current.splice(0)) { + v.apply(() => write.edit(v)); + write.done(); + } + setView(v); + }, + (e: Error) => { + console.warn(e.message); + // No view comes for this editor, so later writes settle at once. + gone.current = true; + drop(); + }, + ); + return () => { + alive = false; + gone.current = true; + viewRef.current = null; + created?.destroy(); + // A StrictMode remount runs the effect again before this fires and keeps the waiting writes. + setTimeout(() => gone.current && drop(), 0); + }; + }, []); + + useEffect(() => { + if (avoidKeyboard) notYet('avoidKeyboard'); + if (autoFocus) notYet('autoFocus'); + if (suggestions !== 'off') notYet('suggestions'); + if (decorations) notYet('decorations'); + if (onGapLayout) notYet('onGapLayout'); + if (onFocus) notYet('onFocus'); + if (onBlur) notYet('onBlur'); + }, [avoidKeyboard, autoFocus, suggestions, decorations, onGapLayout, onFocus, onBlur]); + + useEffect(() => { + view?.setOptions(options); + }, [view, options]); + + useEffect(() => { + view?.setLanguage(language); + }, [view, language]); + + // Reads see an empty document until the core loads and after unmount. Writes made before the load wait for it. + useImperativeHandle(ref, () => { + const core = () => viewRef.current?.core; + const write = (edit: (v: EditorView) => void): Promise => { + const v = viewRef.current; + if (v) v.apply(() => edit(v)); + else if (!gone.current) return new Promise((done) => queued.current.push({ edit, done })); + return Promise.resolve(); + }; + return { + getText: () => core()?.text() ?? '', + getTextInRange: (range) => core()?.text(range.start, range.end) ?? '', + getLength: () => core()?.length ?? 0, + getLineCount: () => core()?.lineCount ?? 1, + getLine: (index) => core()?.line(index) ?? '', + getSelection: () => core()?.selection ?? { anchor: 0, head: 0 }, + getRevision: () => core()?.revision ?? 0, + offsetToPosition: (offset) => core()?.offsetToPosition(offset) ?? { line: 0, column: 0 }, + positionToOffset: (pos) => core()?.positionToOffset(pos.line, pos.column) ?? 0, + getCursorRect: () => { + const v = viewRef.current; + return v ? v.caretRect(v.core.selection.head) : null; + }, + getGapRect: () => { + notYet('getGapRect'); + return null; + }, + getVisibleLines: () => viewRef.current?.visibleLines() ?? { first: 0, last: 0 }, + canUndo: () => core()?.canUndo ?? false, + canRedo: () => core()?.canRedo ?? false, + isFocused: () => false, + setText: (text, opts) => write((v) => v.core.setText(text, opts?.keepHistory ?? false)), + setSelection: (sel) => write((v) => v.core.setSelection(sel.anchor, sel.head)), + selectAll: () => write((v) => v.core.selectAll()), + scrollToLine: (line) => { + viewRef.current?.scrollToLine(line); + return Promise.resolve(); + }, + revealSelection: () => { + viewRef.current?.revealSelection(); + return Promise.resolve(); + }, + replaceRange: (range, text) => write((v) => v.core.replace(range.start, range.end, text)), + insertText: (text) => write((v) => v.core.insertText(text)), + undo: () => write((v) => v.core.undo()), + redo: () => write((v) => v.core.redo()), + indent: () => write((v) => v.core.indent()), + outdent: () => write((v) => v.core.outdent()), + cut: () => later('cut', undefined), + copy: () => later('copy', undefined), + paste: () => later('paste', undefined), + focus: () => later('focus', undefined), + blur: () => later('blur', undefined), + setSearch: () => later('setSearch', { count: 0, limited: false }), + findNext: () => later('findNext', null), + findPrevious: () => later('findPrevious', null), + replaceCurrent: () => later('replaceCurrent', false), + replaceAll: () => later('replaceAll', 0), + clearSearch: () => later('clearSearch', undefined), + moveCaret: () => later('moveCaret', undefined), + typeText: () => later('typeText', undefined), + deleteBackward: () => later('deleteBackward', undefined), + deleteForward: () => later('deleteForward', undefined), + }; + }, []); + + return ; +}); diff --git a/src/web/EditorView.ts b/src/web/EditorView.ts new file mode 100644 index 00000000..34f874db --- /dev/null +++ b/src/web/EditorView.ts @@ -0,0 +1,804 @@ +/// +import { githubLight } from '../themes/githubLight'; +import type { ContentInsets, Rect, Theme } from '../types'; +import { EditorCore, type WebChange } from './core/EditorCore'; +import { Layout } from './core/Layout'; +import type { CoreModule } from './core/module'; +import { type DecodedFrame, decodeFrame } from './frame'; +import { ensureLanguage } from './loader'; +import { registerView, requestPump } from './pump'; +import { type CssTheme, cssTheme } from './theme'; + +export type ViewOptions = { + theme: Theme; + fontFamily: string; + fontSize: number; + lineHeight: number; + lineNumbers: boolean; + softWrap: boolean; + highlightActiveLine: boolean; + matchBrackets: boolean; + textPadding: number; + contentInsets: ContentInsets; + scrollEnabled: boolean; + placeholder: string; + readOnly: boolean; + tabSize: number; + insertSpaces: boolean; + autoIndent: boolean; + autoCloseBrackets: boolean; + onContentSize?: (width: number, height: number) => void; + onScroll?: (x: number, y: number, contentWidth: number, contentHeight: number) => void; + onFirstDraw?: () => void; + onChange?: (change: WebChange) => void; + onSelection?: (anchor: number, head: number, revision: number) => void; +}; + +// The defaults of website/docs/reference/props.md. +export const DEFAULT_OPTIONS: ViewOptions = { + theme: githubLight, + fontFamily: '', + fontSize: 14, + lineHeight: 1.4, + lineNumbers: true, + softWrap: false, + highlightActiveLine: true, + matchBrackets: true, + textPadding: 8, + contentInsets: {}, + scrollEnabled: true, + placeholder: '', + readOnly: false, + tabSize: 4, + insertSpaces: true, + autoIndent: true, + autoCloseBrackets: true, +}; + +const MONOSPACE = 'ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace'; +const SNAPSHOT_INFO_INTS = 7; + +type Entry = { + el: HTMLDivElement; + revision: number; + spans: Int32Array | null; + text: string; +}; + +// The edited line's previous spans moved along an edit that stayed inside it, kept until a parse lands. +type Kept = { line: number; revision: number; spans: Int32Array | null; tail: Int32Array | null }; + +// One editor drawn as DOM lines from the core, outside React's render. The web twin of CodeEditorView.swift. +export class EditorView { + readonly core: EditorCore; + readonly layout: Layout; + private readonly m: CoreModule; + private o: ViewOptions = DEFAULT_OPTIONS; + private css: CssTheme = cssTheme(githubLight); + private readonly root: HTMLDivElement; + private readonly scroller: HTMLDivElement; + private readonly content: HTMLDivElement; + private readonly gutter: HTMLDivElement; + private readonly textLayer: HTMLDivElement; + private readonly activeLine: HTMLDivElement; + private readonly placeholderEl: HTMLDivElement; + private readonly unregister: () => void; + private readonly resize: ResizeObserver | null; + private readonly cache = new Map(); + private readonly attached = new Set(); + private readonly gutterLines = new Map(); + private kept: Kept | null = null; + private language = ''; + private languageReady = false; + private appliedRevision = -1; + private seenSnapshots = 0; + private dirty = true; + private destroyed = false; + private drawn = false; + private rowHeight = 0; + private charWidth = 0; + private lastContent = { width: -1, height: -1 }; + private lastScroll = { x: 0, y: 0 }; + private lastSelection = { anchor: 0, head: 0 }; + private frameInts = 0; + private frameIntsCap = 0; + private frameText = 0; + private frameTextCap = 0; + private readonly onScrollEvent = () => this.invalidate(); + + constructor(host: HTMLElement, m: CoreModule, text = '') { + this.m = m; + this.core = new EditorCore(m, text); + this.layout = new Layout(this.core); + this.root = div('position:relative;overflow:hidden;width:100%;height:100%;direction:ltr;'); + this.scroller = div('position:absolute;inset:0;overflow:auto;'); + this.scroller.dataset.scroller = ''; + this.content = div('position:relative;'); + this.gutter = div('position:sticky;left:0;top:0;z-index:2;'); + this.textLayer = div('position:absolute;left:0;top:0;width:100%;height:100%;'); + this.activeLine = div('position:absolute;left:0;display:none;'); + this.placeholderEl = div('position:absolute;white-space:pre;display:none;pointer-events:none;'); + this.textLayer.append(this.activeLine, this.placeholderEl); + this.content.append(this.gutter, this.textLayer); + this.scroller.append(this.content); + this.root.append(this.scroller); + host.append(this.root); + this.scroller.addEventListener('scroll', this.onScrollEvent, { passive: true }); + this.resize = + typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(() => this.invalidate()); + this.resize?.observe(this.scroller); + document.fonts?.ready.then(() => this.remeasure()); + document.fonts?.addEventListener?.('loadingdone', this.remeasureEvent); + this.lastSelection = this.core.selection; + this.applyOptions(true); + this.unregister = registerView(this); + } + + setOptions(next: Partial): void { + if (this.destroyed) return; + const before = this.o; + this.o = { ...this.o, ...next }; + const metrics = + before.fontFamily !== this.o.fontFamily || + before.fontSize !== this.o.fontSize || + before.lineHeight !== this.o.lineHeight || + before.tabSize !== this.o.tabSize || + before.softWrap !== this.o.softWrap; + const looks = + metrics || + before.theme !== this.o.theme || + before.lineNumbers !== this.o.lineNumbers || + before.scrollEnabled !== this.o.scrollEnabled; + if (before.scrollEnabled !== this.o.scrollEnabled) this.listenToPage(!this.o.scrollEnabled); + if ( + before.tabSize !== this.o.tabSize || + before.insertSpaces !== this.o.insertSpaces || + before.autoIndent !== this.o.autoIndent || + before.autoCloseBrackets !== this.o.autoCloseBrackets + ) { + this.core.settings = { + tabSize: this.o.tabSize, + insertSpaces: this.o.insertSpaces, + autoIndent: this.o.autoIndent, + autoCloseBrackets: this.o.autoCloseBrackets, + }; + } + if (looks) this.applyOptions(metrics); + // Bold or italic tokens change widths and wrapped rows in a font that is not monospace. + if (!metrics && before.theme !== this.o.theme) this.relayout(); + this.invalidate(); + } + + // Draws plain text until the language and the languages it injects have loaded, then colors. + setLanguage(id: string): void { + if (this.destroyed) return; + if (id === this.language) return; + this.language = id; + this.languageReady = false; + this.core.setLanguage(''); + this.invalidate(); + if (!id) return; + ensureLanguage(this.m, id).then((ok) => { + if (this.destroyed || this.language !== id || !ok) return; + this.languageReady = this.core.setLanguage(id); + this.invalidate(); + }); + } + + // Runs an edit, then hands its changes and selection to the cache and the listeners. + apply(edit: () => void): void { + if (this.destroyed) return; + edit(); + const changes = this.core.takeChanges(); + for (const change of changes) this.changed(change, changes.length); + for (const change of changes) this.o.onChange?.(change); + const s = this.core.selection; + if (s.anchor !== this.lastSelection.anchor || s.head !== this.lastSelection.head) { + this.lastSelection = s; + this.o.onSelection?.(s.anchor, s.head, this.core.revision); + } + this.invalidate(); + } + + // Scrolls so the line is at the top of the editor, or of the page when the editor does not scroll. + scrollToLine(line: number): void { + if (this.destroyed) return; + const top = this.layout.insetTop + this.layout.rowStart(line) * this.rowHeight; + if (this.o.scrollEnabled) { + this.scroller.scrollTop = top - this.layout.insetTop; + } else { + window.scrollTo({ top: this.root.getBoundingClientRect().top + window.scrollY + top }); + } + this.invalidate(); + } + + // Scrolls the least that shows the caret. + revealSelection(): void { + if (this.destroyed) return; + const caret = this.caretRect(this.core.selection.head); + const line = this.core.lineAt(this.core.selection.head); + let row = this.layout.rowStart(line); + const el = this.cache.get(line)?.el; + if (caret && el) { + // The caret's row inside a wrapped line, from where the browser drew it. + const lineY = el.getBoundingClientRect().top - this.root.getBoundingClientRect().top; + row += Math.max(0, Math.round((caret.y - lineY) / this.rowHeight)); + } + const top = row * this.rowHeight; + const x = caret ? caret.x - this.layout.textLeft : 0; + if (this.layout.reveal(x, top, x + 1, top + this.rowHeight)) this.syncScroll(); + this.invalidate(); + } + + // The caret at offset in view coordinates, or null when its line is not drawn. + caretRect(offset: number): Rect | null { + if (this.destroyed) return null; + const line = this.core.lineAt(offset); + const entry = this.cache.get(line); + if (!entry || !this.attached.has(line)) return null; + const column = offset - this.core.lineStart(line); + const box = this.root.getBoundingClientRect(); + const lineBox = entry.el.getBoundingClientRect(); + // The base direction is left to right: the caret sits at the leading edge of the next character unless it + // reads right to left, then at the trailing edge of the previous one, so Chromium and WebKit agree. + const next = charRect(entry.el, column, true); + const prev = charRect(entry.el, column, false); + let edge: { x: number; top: number } = { x: lineBox.left, top: lineBox.top }; + if (next && !next.rtl) edge = { x: next.rect.left, top: next.rect.top }; + else if (prev && !prev.rtl) edge = { x: prev.rect.right, top: prev.rect.top }; + else if (next) edge = { x: next.rect.right, top: next.rect.top }; + else if (prev) edge = { x: prev.rect.left, top: prev.rect.top }; + return { x: edge.x - box.left, y: edge.top - box.top, width: 1, height: this.rowHeight }; + } + + visibleLines(): { first: number; last: number } { + if (this.destroyed) return { first: 0, last: 0 }; + return { first: this.layout.firstVisibleLine(), last: this.layout.lastVisibleLine() }; + } + + // One page frame: drawn only when something changed since the last one. + draw(): void { + if (this.destroyed) return; + if (this.core.snapshotCount !== this.seenSnapshots) { + this.seenSnapshots = this.core.snapshotCount; + this.dirty = true; + } + if (!this.dirty) return; + this.dirty = false; + this.configure(); + const [sx, sy] = this.scrollPosition(); + this.layout.beginDraw(sx, sy); + let moved = false; + try { + const frame = this.readFrame(); + moved = this.prepare(frame); + this.place(frame); + } finally { + this.layout.endDraw(); + } + if (moved) { + this.syncScroll(); + this.dirty = true; + } + this.report(); + if (this.dirty) requestPump(); + } + + destroy(): void { + if (this.destroyed) return; + this.destroyed = true; + this.unregister(); + this.listenToPage(false); + this.resize?.disconnect(); + document.fonts?.removeEventListener?.('loadingdone', this.remeasureEvent); + this.scroller.removeEventListener('scroll', this.onScrollEvent); + this.root.remove(); + if (this.frameInts) this.m._free(this.frameInts); + if (this.frameText) this.m._free(this.frameText); + this.core.close(); + // The engine finishes closing in the pump, which may have no other editor to run for. + requestPump(); + } + + private invalidate(): void { + if (this.destroyed) return; + this.dirty = true; + requestPump(); + } + + private readonly remeasureEvent = () => this.remeasure(); + + // A web font finished loading: every line is measured again. + private remeasure(): void { + if (this.destroyed) return; + this.applyOptions(true); + this.invalidate(); + } + + private listening = false; + + private listenToPage(on: boolean): void { + if (on === this.listening) return; + this.listening = on; + const method = on ? 'addEventListener' : 'removeEventListener'; + window[method]('scroll', this.onScrollEvent, { + capture: true, + passive: true, + } as AddEventListenerOptions); + window[method]('resize', this.onScrollEvent); + } + + private applyOptions(metrics: boolean): void { + const o = this.o; + this.css = cssTheme(o.theme); + const font = o.fontFamily ? `"${o.fontFamily}", ${MONOSPACE}` : MONOSPACE; + this.rowHeight = Math.max(1, Math.round(o.fontSize * o.lineHeight)); + Object.assign(this.root.style, { + background: this.css.background, + color: this.css.foreground, + fontFamily: font, + fontSize: `${o.fontSize}px`, + lineHeight: `${this.rowHeight}px`, + tabSize: String(o.tabSize), + height: o.scrollEnabled ? '100%' : this.root.style.height, + }); + this.scroller.style.overflowY = o.scrollEnabled ? 'auto' : 'hidden'; + this.scroller.style.overflowX = o.softWrap ? 'hidden' : 'auto'; + Object.assign(this.gutter.style, { + background: this.css.gutterBackground, + color: this.css.gutterForeground, + display: o.lineNumbers ? 'block' : 'none', + }); + this.activeLine.style.background = this.css.activeLine; + this.placeholderEl.style.color = this.css.gutterForeground; + if (metrics) { + this.charWidth = measureChar(this.root); + this.layout.rowHeight = this.rowHeight; + this.layout.wrap = o.softWrap; + this.relayout(); + } + } + + // Every line is laid out again on the next draw. + private relayout(): void { + for (const entry of this.cache.values()) entry.el.remove(); + this.cache.clear(); + this.attached.clear(); + this.layout.resetRows(); + } + + private configure(): void { + const o = this.o; + const insets = o.contentInsets; + const width = Math.round(this.scroller.clientWidth); + const height = o.scrollEnabled ? Math.round(this.scroller.clientHeight) : window.innerHeight; + const digits = Math.max(2, String(this.core.lineCount).length); + const gutter = o.lineNumbers ? Math.ceil(this.charWidth * (digits + 2)) : 0; + const before = this.layout.textAreaWidth; + if (this.layout.gutterWidth !== gutter) this.layout.gutterWidth = gutter; + if (this.layout.textPadding !== o.textPadding) this.layout.textPadding = o.textPadding; + this.layout.resize(width, height); + this.layout.setInsets(insets.top ?? 0, insets.bottom ?? 0, insets.left ?? 0, insets.right ?? 0); + if (o.softWrap && this.layout.textAreaWidth !== before) this.relayout(); + } + + // The scroll the core draws at: the editor's own, or with scrolling off where the page shows the editor. + private scrollPosition(): [number, number] { + if (this.o.scrollEnabled) return [this.scroller.scrollLeft, this.scroller.scrollTop]; + const top = this.root.getBoundingClientRect().top; + return [this.scroller.scrollLeft, Math.max(0, Math.round(-top))]; + } + + private syncScroll(): void { + if (this.o.scrollEnabled) this.scroller.scrollTop = this.layout.scrollY; + this.scroller.scrollLeft = this.layout.scrollX; + } + + private snapshotInfo(snapshot: number): { revision: number; basedOn: number } { + const out = this.m._malloc(SNAPSHOT_INFO_INTS * 4); + this.m._cp_snapshot_info_get(snapshot, out); + const revision = this.m.HEAP32[out >> 2] as number; + const basedOn = this.m.HEAP32[(out >> 2) + 1] as number; + this.m._free(out); + return { revision, basedOn }; + } + + // The draw's one frame call: the layout picks the lines, the frame says which need a layout and brings them. + private readFrame(): DecodedFrame { + const m = this.m; + const snapshot = this.languageReady ? this.core.snapshot : 0; + const info = snapshot ? this.snapshotInfo(snapshot) : null; + const incremental = info !== null && info.basedOn >= 0 && info.basedOn === this.appliedRevision; + const pairs: number[] = []; + for (const [line, entry] of this.cache) pairs.push(line, entry.revision); + const entries = m._malloc(Math.max(1, pairs.length) * 4); + m.HEAP32.set(pairs, entries >> 2); + const lenPtr = m._malloc(4); + try { + for (;;) { + if (!this.frameInts) this.growFrame(4096, 16384); + const needed = + m._cp_editor_frame( + this.core.handle, + this.layout.handle, + snapshot, + this.appliedRevision, + incremental ? 1 : 0, + entries, + pairs.length / 2, + this.frameInts, + this.frameIntsCap, + this.frameText, + this.frameTextCap, + lenPtr, + ) >>> 0; + const textLength = (m.HEAP32[lenPtr >> 2] as number) >>> 0; + if (needed <= this.frameIntsCap && textLength <= this.frameTextCap) { + const ints = m.HEAP32.subarray(this.frameInts >> 2, (this.frameInts >> 2) + needed); + const text = m.HEAPU16.subarray(this.frameText >> 1, (this.frameText >> 1) + textLength); + return decodeFrame(ints, text); + } + this.growFrame( + Math.max(needed, this.frameIntsCap), + Math.max(textLength, this.frameTextCap), + ); + } + } finally { + m._free(entries); + m._free(lenPtr); + } + } + + private growFrame(ints: number, units: number): void { + const m = this.m; + if (ints > this.frameIntsCap) { + if (this.frameInts) m._free(this.frameInts); + this.frameIntsCap = ints; + this.frameInts = m._malloc(ints * 4); + } + if (units > this.frameTextCap) { + if (this.frameText) m._free(this.frameText); + this.frameTextCap = units; + this.frameText = m._malloc(Math.max(2, units * 2)); + } + } + + // Builds the lines the frame marks dirty and ends with the draw's measure call. True when it moved the scroll. + private prepare(frame: DecodedFrame): boolean { + const snapshot = this.languageReady ? this.core.snapshot : 0; + const consistent = snapshot !== 0 && this.snapshotInfo(snapshot).revision === frame.revision; + const revision = consistent ? frame.revision : -1; + const pairs: number[] = []; + const fresh: [number, Entry][] = []; + for (const run of frame.runs) { + for (let line = run.lo; line <= run.hi; line++) { + const data = run.lines[line - run.lo]; + if (!data) continue; + const entry = this.cache.get(line); + let spans: Int32Array | null = null; + if (consistent) { + spans = data.spans.length > 0 ? data.spans : null; + if (entry && entry.text === data.text && sameSpans(entry.spans, spans)) { + entry.revision = revision; + continue; + } + } else { + spans = this.keptSpans(line, frame.revision); + } + const el = entry?.el ?? this.makeLine(); + el.dataset.line = String(line); + this.paint(el, data.text, spans); + // The width it wraps at, before prepare reads its height. + el.style.width = this.o.softWrap ? `${this.layout.wrapWidth}px` : ''; + const next = { el, revision, spans, text: data.text }; + this.cache.delete(line); + this.cache.set(line, next); + if (!el.isConnected) this.textLayer.append(el); + fresh.push([line, next]); + } + } + for (let line = frame.first; line <= frame.last; line++) { + const entry = this.cache.get(line); + if (entry && revision >= 0 && entry.revision !== revision && !inRuns(frame, line)) + entry.revision = revision; + } + if (revision >= 0) { + this.appliedRevision = revision; + this.kept = null; + } + let widest = -1; + for (const [line, entry] of fresh) { + const rows = this.o.softWrap + ? Math.max(1, Math.round(entry.el.offsetHeight / this.rowHeight)) + : 1; + pairs.push(line, rows); + if (!this.o.softWrap) widest = Math.max(widest, Math.ceil(entry.el.offsetWidth)); + } + return this.layout.measure(pairs, widest, frame.first, frame.last); + } + + // Places the window's lines, numbers and active line, and takes every other line out of the page. + private place(frame: DecodedFrame): void { + const layout = this.layout; + const scrollY = layout.scrollY; + const left = layout.textAreaLeft + layout.textPadding; + const width = layout.textAreaLeft + layout.contentWidth + layout.insetRight; + const height = layout.insetTop + layout.contentHeight + layout.insetBottom; + this.content.style.width = `${width}px`; + this.content.style.height = `${height}px`; + this.gutter.style.width = `${layout.insetLeft + layout.gutterWidth}px`; + this.gutter.style.height = `${height}px`; + if (!this.o.scrollEnabled) this.root.style.height = `${height}px`; + const visible = new Set(); + for (let line = frame.first; line <= frame.last; line++) { + const entry = this.cache.get(line); + if (!entry) continue; + visible.add(line); + const top = layout.lineTop(line) + scrollY; + const s = entry.el.style; + s.top = `${top}px`; + s.left = `${left}px`; + s.width = this.o.softWrap ? `${layout.wrapWidth}px` : ''; + if (!entry.el.isConnected) this.textLayer.append(entry.el); + this.attached.add(line); + this.number(line, top, frame.caretLine === line); + } + for (const line of this.attached) { + if (visible.has(line)) continue; + this.cache.get(line)?.el.remove(); + this.attached.delete(line); + } + for (const [line, el] of this.gutterLines) { + if (visible.has(line)) continue; + el.remove(); + this.gutterLines.delete(line); + } + this.evict(visible); + const collapsed = frame.anchor === frame.head; + const showActive = this.o.highlightActiveLine && collapsed && visible.has(frame.caretLine); + this.activeLine.style.display = showActive ? 'block' : 'none'; + if (showActive) { + this.activeLine.style.top = `${layout.lineTop(frame.caretLine) + scrollY}px`; + this.activeLine.style.height = `${layout.rows(frame.caretLine) * this.rowHeight}px`; + this.activeLine.style.width = `${width}px`; + } + const empty = frame.length === 0 && this.o.placeholder !== ''; + this.placeholderEl.style.display = empty ? 'block' : 'none'; + if (empty) { + this.placeholderEl.textContent = this.o.placeholder; + this.placeholderEl.style.top = `${layout.insetTop}px`; + this.placeholderEl.style.left = `${left}px`; + } + } + + private number(line: number, top: number, active: boolean): void { + if (!this.o.lineNumbers) return; + let el = this.gutterLines.get(line); + if (!el) { + el = div('position:absolute;left:0;text-align:right;'); + this.gutter.append(el); + this.gutterLines.set(line, el); + } + el.textContent = String(line + 1); + el.style.top = `${top}px`; + el.style.width = `${this.layout.insetLeft + this.layout.gutterWidth - this.charWidth}px`; + el.style.color = active ? this.css.gutterActiveForeground : ''; + } + + // Keeps about three screens of laid out lines. + private evict(visible: Set): void { + const cap = Math.max(64, 3 * (visible.size + 1)); + for (const line of this.cache.keys()) { + if (this.cache.size <= cap) break; + if (!visible.has(line)) this.cache.delete(line); + } + } + + // Fires the events of this draw: content size, the first draw, then scroll. + private report(): void { + const layout = this.layout; + const width = layout.textAreaLeft + layout.contentWidth + layout.insetRight; + const height = layout.insetTop + layout.contentHeight + layout.insetBottom; + if (width !== this.lastContent.width || height !== this.lastContent.height) { + this.lastContent = { width, height }; + this.o.onContentSize?.(width, height); + } + if (!this.drawn) { + this.drawn = true; + this.o.onFirstDraw?.(); + } + const x = layout.scrollX; + const y = this.o.scrollEnabled ? layout.scrollY : 0; + if (x !== this.lastScroll.x || y !== this.lastScroll.y) { + this.lastScroll = { x, y }; + this.o.onScroll?.(x, y, layout.contentWidth, layout.contentHeight); + } + } + + private makeLine(): HTMLDivElement { + return div('position:absolute;direction:ltr;'); + } + + // Fills a line element with its text, one span per styled run. + private paint(el: HTMLDivElement, text: string, spans: Int32Array | null): void { + el.style.whiteSpace = this.o.softWrap ? 'pre-wrap' : 'pre'; + el.style.overflowWrap = this.o.softWrap ? 'anywhere' : 'normal'; + el.replaceChildren(); + let at = 0; + if (spans) { + for (let i = 0; i + 2 < spans.length; i += 3) { + const start = Math.max(at, spans[i] as number); + const end = Math.min(text.length, spans[i + 1] as number); + if (end <= start) continue; + if (start > at) el.append(text.slice(at, start)); + const style = this.css.token(styleName(this.m, spans[i + 2] as number)); + const span = document.createElement('span'); + span.textContent = text.slice(start, end); + if (style) { + span.style.color = style.color; + if (style.bold) span.style.fontWeight = 'bold'; + if (style.italic) span.style.fontStyle = 'italic'; + } + el.append(span); + at = end; + } + } + if (at < text.length) el.append(text.slice(at)); + } + + // Moves the cache along an edit and keeps the edited line's spans shifted until the parse catches up. + private changed(change: WebChange, count: number): void { + if (count > 1) { + this.kept = null; + this.relayout(); + return; + } + const first = this.core.lineAt(change.start); + const oldCount = lineSpan(change.removed); + const newCount = lineSpan(change.inserted); + this.keepSpans(first, oldCount, newCount, change); + const delta = newCount - oldCount; + const moved: [number, Entry][] = []; + for (const [line, entry] of this.cache) { + if (line < first) moved.push([line, entry]); + else if (line >= first + oldCount) moved.push([line + delta, entry]); + else entry.el.remove(); + } + this.cache.clear(); + this.attached.clear(); + for (const [line, entry] of moved) { + entry.el.dataset.line = String(line); + this.cache.set(line, entry); + if (entry.el.isConnected) this.attached.add(line); + } + } + + private keepSpans(first: number, oldCount: number, newCount: number, change: WebChange): void { + this.kept = null; + const spans = this.cache.get(first)?.spans; + if (oldCount !== 1 || !spans) return; + const column = change.start - this.core.lineStart(first); + const revision = this.core.revision; + if (newCount === 1) { + const kept = shiftSpans(this.m, spans, column, change.removedLength, change.insertedLength); + this.kept = { line: first, revision, spans: kept, tail: null }; + } else if (newCount === 2 && change.removedLength === 0 && change.inserted.startsWith('\n')) { + const added = change.insertedLength - 1; + const tail = shiftSpans(this.m, spans, 0, column, added); + const lineLength = this.core.lineEnd(first + 1) - this.core.lineStart(first + 1); + const head = shiftSpans(this.m, spans, column, lineLength - added, 0); + this.kept = { line: first, revision, spans: head, tail }; + } + } + + private keptSpans(line: number, revision: number): Int32Array | null { + const kept = this.kept; + if (!kept || kept.revision !== revision) return null; + if (line === kept.line) return kept.spans; + if (line === kept.line + 1) return kept.tail; + return null; + } +} + +function div(css: string): HTMLDivElement { + const el = document.createElement('div'); + el.style.cssText = css; + return el; +} + +function measureChar(root: HTMLElement): number { + const probe = document.createElement('span'); + probe.style.cssText = 'position:absolute;visibility:hidden;white-space:pre;'; + probe.textContent = '0000000000'; + root.append(probe); + const width = probe.getBoundingClientRect().width / 10; + probe.remove(); + return width || 8; +} + +function lineSpan(text: string): number { + let n = 1; + for (let i = 0; i < text.length; i++) if (text.charCodeAt(i) === 10) n++; + return n; +} + +function inRuns(frame: DecodedFrame, line: number): boolean { + return frame.runs.some((r) => line >= r.lo && line <= r.hi); +} + +function sameSpans(a: Int32Array | null, b: Int32Array | null): boolean { + if (a === b) return true; + if (!a || !b || a.length !== b.length) return false; + for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false; + return true; +} + +const RTL = /^[֐-ࣿיִ-﷿ﹰ-]/; + +// The box of the character after column (forward) or before it, and whether it reads right to left. +function charRect( + el: HTMLElement, + column: number, + forward: boolean, +): { rect: DOMRect; rtl: boolean } | null { + const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT); + let at = 0; + for (let node = walker.nextNode() as Text | null; node; node = walker.nextNode() as Text | null) { + const length = node.data.length; + const inside = forward + ? column >= at && column < at + length + : column > at && column <= at + length; + if (inside) { + const i = column - at; + const low = (c: number) => c >= 0xdc00 && c <= 0xdfff; + const start = forward ? i : low(node.data.charCodeAt(i - 1)) && i > 1 ? i - 2 : i - 1; + const end = forward ? (low(node.data.charCodeAt(i + 1)) ? i + 2 : i + 1) : i; + const range = document.createRange(); + range.setStart(node, start); + range.setEnd(node, end); + const rect = range.getClientRects()[0] ?? range.getBoundingClientRect(); + return { rect, rtl: RTL.test(node.data.slice(start, end)) }; + } + at += length; + } + return null; +} + +const styleNames = new WeakMap>(); + +function styleName(m: CoreModule, id: number): string { + let names = styleNames.get(m); + if (!names) { + names = new Map(); + styleNames.set(m, names); + } + const cached = names.get(id); + if (cached !== undefined) return cached; + const out = m._malloc(256); + const length = m._cp_style_name(id, out, 256); + const name = m.UTF8ToString(out, Math.min(length, 255)); + m._free(out); + names.set(id, name); + return name; +} + +function shiftSpans( + m: CoreModule, + spans: Int32Array, + start: number, + removed: number, + inserted: number, +): Int32Array | null { + const cap = spans.length + 6; + const input = m._malloc(Math.max(1, spans.length) * 4); + const out = m._malloc(cap * 4); + try { + m.HEAP32.set(spans, input >> 2); + const n = m._cp_shift_spans(input, spans.length, start, removed, inserted, out, cap) >>> 0; + if (n > cap) return null; + return m.HEAP32.slice(out >> 2, (out >> 2) + n); + } finally { + m._free(input); + m._free(out); + } +} diff --git a/src/web/__tests__/CodeEditor.test.tsx b/src/web/__tests__/CodeEditor.test.tsx new file mode 100644 index 00000000..f5e2bc69 --- /dev/null +++ b/src/web/__tests__/CodeEditor.test.tsx @@ -0,0 +1,128 @@ +import { beforeEach, expect, jest, test } from '@jest/globals'; +import { act, render } from '@testing-library/react-native'; +import { createRef } from 'react'; +import type { CodeEditorRef } from '../../types'; +import type { ViewOptions } from '../EditorView'; + +// A view that draws in the microtask after its construction, as the page pump draws before React commits. +const mockViews: MockView[] = []; +class MockView { + options: Partial = {}; + language = ''; + written: string[] = []; + core = { + text: jest.fn(() => this.written.join('')), + setText: jest.fn((text: string) => this.written.push(text)), + }; + constructor(_host: unknown, _m: unknown, text = '') { + this.written.push(text); + mockViews.push(this); + queueMicrotask(() => this.options.onFirstDraw?.()); + } + setOptions(next: Partial) { + this.options = { ...this.options, ...next }; + } + setLanguage(id: string) { + this.language = id; + } + apply(edit: () => void) { + edit(); + } + destroy() {} +} + +// The core load, resolved by the test. +const mockCore: { + resolve: (m: object) => void; + reject: (e: Error) => void; + promise: Promise; +} = { + resolve: () => {}, + reject: () => {}, + promise: Promise.resolve({}), +}; + +jest.mock('../EditorView', () => ({ EditorView: MockView })); +jest.mock('../loader', () => ({ loadCore: () => mockCore.promise })); + +const { CodeEditor } = require('../CodeEditor') as typeof import('../CodeEditor'); + +const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); + +function settled(promise: Promise): Promise { + return Promise.race([promise, new Promise<'hung'>((r) => setTimeout(() => r('hung'), 100))]); +} + +beforeEach(() => { + mockViews.length = 0; + mockCore.promise = new Promise((resolve, reject) => { + mockCore.resolve = resolve; + mockCore.reject = reject; + }); +}); + +test('fires onReady when the first draw comes before React commits the view', async () => { + const onReady = jest.fn(); + await render(); + await act(async () => { + mockCore.resolve({}); + await tick(); + }); + expect(mockViews).toHaveLength(1); + expect(mockViews[0]?.language).toBe('python'); + expect(onReady).toHaveBeenCalledTimes(1); +}); + +test('applies a write made before the core loads once it does', async () => { + const ref = createRef(); + await render(); + const write = ref.current?.setText('saved'); + await act(async () => { + mockCore.resolve({}); + await tick(); + }); + await expect(settled(write as Promise)).resolves.toBeUndefined(); + expect(mockViews[0]?.core.setText).toHaveBeenCalledWith('saved', false); +}); + +test('settles a write left waiting when the editor unmounts before the core loads', async () => { + const ref = createRef(); + const api = await render(); + const write = ref.current?.setText('lost'); + await api.unmount(); + await expect(settled(write as Promise)).resolves.toBeUndefined(); + mockCore.resolve({}); + await tick(); + expect(mockViews).toHaveLength(0); +}); + +test('a handle kept after unmount neither reads nor writes the closed editor', async () => { + const ref = createRef(); + const api = await render(); + await act(async () => { + mockCore.resolve({}); + await tick(); + }); + const handle = ref.current as CodeEditorRef; + const view = mockViews[0] as MockView; + expect(handle.getText()).toBe('first'); + view.core.text.mockClear(); + await api.unmount(); + await handle.setText('late'); + expect(handle.getText()).toBe(''); + expect(view.core.setText).not.toHaveBeenCalled(); + expect(view.core.text).not.toHaveBeenCalled(); +}); + +test('settles a write made after the core failed to load', async () => { + const ref = createRef(); + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); + await render(); + await act(async () => { + mockCore.reject(new Error('[codepad] could not load /codepad/codepad.js')); + await tick(); + }); + const write = ref.current?.setText('late'); + await expect(settled(write as Promise)).resolves.toBeUndefined(); + warn.mockRestore(); +}); diff --git a/src/web/__tests__/core.test.ts b/src/web/__tests__/core.test.ts new file mode 100644 index 00000000..037c15f0 --- /dev/null +++ b/src/web/__tests__/core.test.ts @@ -0,0 +1,45 @@ +import { existsSync } from 'node:fs'; +import { join } from 'node:path'; +import { expect, test } from '@jest/globals'; +import { + builtCore, + cstring, + loadCore, + loadGrammar, + pumpAll, + ROOT, + utf16, +} from '../../../tests/web/core'; +import { EditorCore } from '../core/EditorCore'; + +const dir = builtCore('lib/web', 'yarn build:web'); +const python = join(ROOT, 'languages/python/web/python.wasm'); +const ready = dir !== null && existsSync(python); +if (dir && !ready) console.warn('python grammar not built, run yarn build:web-grammars'); + +(ready ? test : test.skip)('the core parses python and highlights in wasm', async () => { + const m = await loadCore(dir as string); + loadGrammar(m, python, 'python', join(ROOT, 'languages/python/queries/python')); + const e = utf16(m, 'def greet(name):\n return name\n', (p, n) => m._cpw_open(p, n, 8000n)); + expect(cstring(m, 'python', (id) => m._cpw_set_language(e, id))).toBe(1); + pumpAll(m); + const snapshot = m._cpw_snapshot(e); + expect(snapshot).not.toBe(0); + const out = m._malloc(4 * 256); + const count = m._cp_highlight(snapshot, 0, 0, 0, out, 256); + const ints = Array.from(m.HEAP32.subarray(out >> 2, (out >> 2) + count)); + m._free(out); + const keyword = cstring(m, 'keyword.function', (name) => m._cp_style_id(name)); + const triples = []; + for (let i = 1; i + 2 < ints.length; i += 3) triples.push(ints.slice(i, i + 3)); + expect(triples).toContainEqual([0, 3, keyword]); + m._cpw_close(e); +}); + +(dir ? test : test.skip)('reads a leading U+FEFF and half a surrogate pair as stored', async () => { + const m = await loadCore(dir as string); + const core = new EditorCore(m, 'x\u{1F600}'); + expect(core.text()).toBe('x\u{1F600}'); + expect(core.text(2, 3)).toBe('\uD83D'); + core.close(); +}); diff --git a/src/web/__tests__/frame.test.ts b/src/web/__tests__/frame.test.ts new file mode 100644 index 00000000..de2b303d --- /dev/null +++ b/src/web/__tests__/frame.test.ts @@ -0,0 +1,57 @@ +import { expect, test } from '@jest/globals'; +import { decodeFrame } from '../frame'; + +const units = (s: string) => Uint16Array.from(s, (c) => c.charCodeAt(0)); +// revision, length, line_count, anchor, head, caret_line, decorations_version, history, first, last +const header = (lineCount: number, first: number, last: number) => [ + 3, + 11, + lineCount, + 2, + 4, + 0, + 1, + 1, + first, + last, +]; + +test('decodes a frame with one run of two lines', () => { + const ints = Int32Array.from([ + ...header(2, 0, 1), + ...[0, 6, 12], + ...[1, 0, 1], + ...[1, 0, 3, 7], + ...[0], + ...[0, 5, 5, 5], + ]); + const frame = decodeFrame(ints, units('def ab = 1')); + expect(frame).toMatchObject({ revision: 3, length: 11, lineCount: 2, anchor: 2, head: 4 }); + expect(frame).toMatchObject({ decorationsVersion: 1, history: 1, first: 0, last: 1 }); + expect(frame.lineStarts).toEqual([0, 6, 12]); + expect(frame.runs).toHaveLength(1); + const [run] = frame.runs; + expect(run?.lo).toBe(0); + expect(run?.hi).toBe(1); + expect(run?.lines.map((l) => l.text)).toEqual(['def a', 'b = 1']); + expect(run?.lines.map((l) => Array.from(l.spans))).toEqual([[0, 3, 7], []]); +}); + +test('decodes a frame with no runs', () => { + const frame = decodeFrame(Int32Array.from([...header(1, 0, 0), 0, 4, 0]), units('')); + expect(frame.lineStarts).toEqual([0, 4]); + expect(frame.runs).toEqual([]); +}); + +test('keeps a leading U+FEFF in a line, so spans keep their offsets', () => { + const ints = Int32Array.from([ + ...header(2, 0, 1), + ...[0, 6, 12], + ...[1, 0, 1], + ...[1, 0, 3, 7], + ...[0], + ...[0, 5, 5, 5], + ]); + const frame = decodeFrame(ints, units('ef ab = 1')); + expect(frame.runs[0]?.lines.map((l) => l.text)).toEqual(['ef a', 'b = 1']); +}); diff --git a/src/web/__tests__/highlight.test.ts b/src/web/__tests__/highlight.test.ts new file mode 100644 index 00000000..d95ec9f7 --- /dev/null +++ b/src/web/__tests__/highlight.test.ts @@ -0,0 +1,61 @@ +import { readdirSync, readFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { expect, test } from '@jest/globals'; +import { + builtCore, + cstring, + highlight, + loadCore, + loadGrammar, + pumpAll, + ROOT, + styleName, + utf16, + webGrammars, +} from '../../../tests/web/core'; +import { INJECTION_ALIASES } from '../aliases'; + +const goldens = join(ROOT, 'tests/highlight'); +const dir = builtCore('build/web-test', 'cpp/web/build.sh --test'); +const grammars = webGrammars(); +const names = readdirSync(goldens) + .filter((f) => f.endsWith('.txt')) + .map((f) => f.slice(0, -4)) + .sort(); +const missing = names + .map((n) => n.split('.')[0] as string) + .filter((g) => !grammars.some((w) => w.grammar === g)); +if (dir && missing.length > 0) + console.warn(`grammars not built (${missing.join(', ')}), run yarn build:web-grammars`); + +(dir && missing.length === 0 ? test : test.skip)( + 'every highlight golden matches in wasm', + async () => { + const m = await loadCore(dir as string); + for (const g of grammars) loadGrammar(m, g.wasm, g.grammar, g.queries); + for (const [name, id] of Object.entries(INJECTION_ALIASES)) { + cstring(m, name, (n) => cstring(m, id, (i) => m._cpw_language_alias(n, i))); + } + for (const name of names) { + const text = readFileSync(join(goldens, `${name}.txt`), 'utf8'); + const e = utf16(m, text, (p, n) => m._cpw_open(p, n, 8000n)); + cstring(m, name.split('.')[0] as string, (id) => m._cpw_set_language(e, id)); + pumpAll(m); + const snapshot = m._cpw_snapshot(e); + expect(m._cp_snapshot_length(snapshot)).toBe(text.length); + const lines = text.split('\n'); + let actual = ''; + highlight(m, snapshot, 0, lines.length - 1, 0).forEach((runs, line) => { + for (let i = 0; i < runs.length; i += 3) { + actual += `${line} ${runs[i]} ${runs[i + 1]} ${styleName(m, runs[i + 2] as number)}\n`; + } + }); + m._cpw_close(e); + expect({ name, spans: actual }).toEqual({ + name, + spans: readFileSync(join(goldens, `${name}.spans`), 'utf8'), + }); + } + pumpAll(m); + }, +); diff --git a/src/web/__tests__/loader.test.ts b/src/web/__tests__/loader.test.ts new file mode 100644 index 00000000..5063df58 --- /dev/null +++ b/src/web/__tests__/loader.test.ts @@ -0,0 +1,228 @@ +import { afterEach, beforeEach, expect, jest, test } from '@jest/globals'; +import type { CoreModule } from '../core/module'; + +type Loader = typeof import('../loader'); + +const MANIFEST = { + version: 1, + core: { js: 'codepad.js', wasm: 'codepad.wasm' }, + languages: [ + lang('css', null), + lang('javascript', 'javascript.injections.scm'), + lang('html', 'html.injections.scm'), + lang('python', null), + ], +}; +const HTML_INJECTIONS = `((style_element (raw_text) @injection.content) + (#set! injection.language "css")) +((script_element (raw_text) @injection.content) + (#set! injection.language "javascript")) +((comment) @injection.content (#set! injection.language "comment"))`; +const JAVASCRIPT_INJECTIONS = `((template_string) @injection.content + (#set! injection.language "html"))`; + +function lang(id: string, injections: string | null) { + return { + id, + wasm: `${id}.wasm`, + symbol: `tree_sitter_${id}`, + highlights: `${id}.highlights.scm`, + injections, + }; +} + +// A module that records the languages it was asked to load, in order. +function fakeModule(loaded: string[]): CoreModule { + const strings: string[] = []; + return { + FS: { + writeFile: () => {}, + createPreloadedFile: ( + _p: string, + _n: string, + _d: unknown, + _r: boolean, + _w: boolean, + onload?: () => void, + ) => onload?.(), + }, + stringToNewUTF8: (s: string) => strings.push(s) - 1, + UTF8ToString: (p: number) => strings[p] ?? '', + _free: () => {}, + _cpw_language_alias: () => {}, + _cpw_error: () => strings.push('no error') - 1, + _cpw_language_load: (id: number) => { + loaded.push(strings[id] as string); + return 1; + }, + } as unknown as CoreModule; +} + +let loader: Loader; +let missing: Set; +let offline: Set; +let dropped: Set; +let scriptFails: number; +let manifestText: string; +let appended: number; +let warn: jest.SpiedFunction; +const loaded: string[] = []; + +beforeEach(() => { + jest.resetModules(); + loader = require('../loader') as Loader; + missing = new Set(); + offline = new Set(); + dropped = new Set(); + scriptFails = 0; + manifestText = JSON.stringify(MANIFEST); + appended = 0; + loaded.length = 0; + warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); + const g = globalThis as Record; + g.fetch = jest.fn(async (url: string) => { + const file = url.slice('/codepad/'.length); + if (offline.has(file)) throw new TypeError('Failed to fetch'); + if (missing.has(file)) return { ok: false, status: 404 }; + const bodies: Record = { + 'languages.json': manifestText, + 'html.injections.scm': HTML_INJECTIONS, + 'javascript.injections.scm': JAVASCRIPT_INJECTIONS, + }; + const body = bodies[file] ?? ''; + return { + ok: true, + status: 200, + text: async () => { + if (dropped.has(file)) throw new TypeError('network error'); + return body; + }, + arrayBuffer: async () => { + if (dropped.has(file)) throw new TypeError('network error'); + return new ArrayBuffer(8); + }, + }; + }); + g.createCodepadCore = undefined; + g.document = { + createElement: () => ({}), + head: { + appendChild: (script: { onload?: () => void; onerror?: () => void }) => { + appended++; + if (scriptFails > 0) { + scriptFails--; + setTimeout(() => script.onerror?.(), 0); + return; + } + g.createCodepadCore = async () => fakeModule(loaded); + setTimeout(() => script.onload?.(), 0); + }, + }, + }; +}); + +afterEach(() => { + warn.mockRestore(); +}); + +// Resolves with the value, or with 'hung' when the promise is still pending after a short wait. +function settled(promise: Promise): Promise { + return Promise.race([promise, new Promise<'hung'>((r) => setTimeout(() => r('hung'), 200))]); +} + +test('loads every language the injection queries name before it resolves', async () => { + const m = await loader.loadCore(); + await expect(loader.ensureLanguage(m, 'html')).resolves.toBe(true); + expect([...loaded].sort()).toEqual(['css', 'html', 'javascript']); + expect(warn).not.toHaveBeenCalled(); +}); + +test('loads two languages that inject each other when both start at once', async () => { + const m = await loader.loadCore(); + const both = Promise.all([ + loader.ensureLanguage(m, 'javascript'), + loader.ensureLanguage(m, 'html'), + ]); + await expect(settled(both)).resolves.toEqual([true, true]); + expect([...loaded].sort()).toEqual(['css', 'html', 'javascript']); +}); + +test('warns once and resolves false when languages.json is a page, not the manifest', async () => { + manifestText = ''; + const m = await loader.loadCore(); + await expect(settled(loader.ensureLanguage(m, 'python'))).resolves.toBe(false); + await expect(settled(loader.ensureLanguage(m, 'javascript'))).resolves.toBe(false); + expect(warn.mock.calls).toEqual([ + [ + '[codepad] /codepad/languages.json is not the file the web command writes. Run npx react-native-codepad web .', + ], + ]); + expect(loader.availableLanguages()).toEqual([]); +}); + +test('warns once and resolves false for an id the manifest lacks', async () => { + const m = await loader.loadCore(); + await expect(loader.ensureLanguage(m, 'cobol')).resolves.toBe(false); + await expect(loader.ensureLanguage(m, 'cobol')).resolves.toBe(false); + expect(warn.mock.calls).toEqual([ + ['[codepad] language "cobol" is not in /codepad/languages.json'], + ]); + expect(loaded).toEqual([]); +}); + +test('warns with the url when a grammar fetch fails', async () => { + missing.add('python.wasm'); + const m = await loader.loadCore(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(false); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(false); + expect(warn.mock.calls).toEqual([['[codepad] could not load /codepad/python.wasm: 404']]); + expect(loaded).toEqual([]); +}); + +test('retries the core script after a load that failed', async () => { + scriptFails = 1; + await expect(loader.loadCore()).rejects.toThrow('[codepad] could not load /codepad/codepad.js'); + await expect(loader.loadCore()).resolves.toBeDefined(); + expect(appended).toBe(2); +}); + +test('retries a grammar after a network error', async () => { + offline.add('python.wasm'); + const m = await loader.loadCore(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(false); + offline.clear(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(true); + expect(loaded).toEqual(['python']); +}); + +test('retries languages.json after a network error', async () => { + offline.add('languages.json'); + const m = await loader.loadCore(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(false); + offline.clear(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(true); + expect(loader.availableLanguages()).toContain('python'); +}); + +test('injects the core script once for two editors', async () => { + const [a, b] = await Promise.all([loader.loadCore(), loader.loadCore()]); + expect(a).toBe(b); + expect(appended).toBe(1); +}); + +test('retries a grammar whose body was cut off by a network error', async () => { + dropped.add('python.wasm'); + const m = await loader.loadCore(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(false); + dropped.clear(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(true); + expect(loaded).toEqual(['python']); +}); + +test('retries languages.json when its body was cut off by a network error', async () => { + dropped.add('languages.json'); + const m = await loader.loadCore(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(false); + dropped.clear(); + await expect(loader.ensureLanguage(m, 'python')).resolves.toBe(true); +}); diff --git a/src/web/__tests__/theme.test.ts b/src/web/__tests__/theme.test.ts new file mode 100644 index 00000000..73fe2208 --- /dev/null +++ b/src/web/__tests__/theme.test.ts @@ -0,0 +1,48 @@ +import { expect, test } from '@jest/globals'; +import { githubLight } from '../../themes/githubLight'; +import { cssTheme } from '../theme'; + +test('keeps the six accepted formats', () => { + const css = cssTheme({ + ...githubLight, + background: '#abc', + foreground: '#abcd', + cursor: '#aabbcc', + selection: '#aabbcc80', + activeLine: 'rgb(1, 2, 3)', + searchMatch: 'rgba(1,2,3,0.5)', + }); + expect(css.background).toBe('#abc'); + expect(css.foreground).toBe('#abcd'); + expect(css.cursor).toBe('#aabbcc'); + expect(css.selection).toBe('#aabbcc80'); + expect(css.activeLine).toBe('rgb(1, 2, 3)'); + expect(css.searchMatch).toBe('rgba(1,2,3,0.5)'); +}); + +test('falls back to the githubLight color of the key', () => { + const css = cssTheme({ + ...githubLight, + background: 'red', + cursor: '#12345', + gutterForeground: '', + }); + expect(css.background).toBe(githubLight.background); + expect(css.cursor).toBe(githubLight.cursor); + expect(css.gutterForeground).toBe(githubLight.gutterForeground); +}); + +test('falls back to githubLight.foreground for a token', () => { + const css = cssTheme({ + ...githubLight, + tokens: { keyword: 'nope', string: { color: 'blue', bold: true }, number: '#0550ae' }, + }); + expect(css.token('keyword.function')).toEqual({ + color: githubLight.foreground, + bold: false, + italic: false, + }); + expect(css.token('string')).toEqual({ color: githubLight.foreground, bold: true, italic: false }); + expect(css.token('number')).toEqual({ color: '#0550ae', bold: false, italic: false }); + expect(css.token('variable')).toBeNull(); +}); diff --git a/src/web/__tests__/vectors.test.ts b/src/web/__tests__/vectors.test.ts new file mode 100644 index 00000000..1d76b711 --- /dev/null +++ b/src/web/__tests__/vectors.test.ts @@ -0,0 +1,434 @@ +import { existsSync, readdirSync, readFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { afterAll, beforeAll, describe, expect, test } from '@jest/globals'; +import { builtCore, loadCore, loadGrammar, pumpAll, ROOT } from '../../../tests/web/core'; +import type { Decoration } from '../../types'; +import { EditorCore, type MoveUnit, SearchError, type WebChange } from '../core/EditorCore'; +import { type Anchor, Layout } from '../core/Layout'; +import type { CoreModule } from '../core/module'; + +type Json = Record; + +const dir = builtCore('build/web-test', 'cpp/web/build.sh --test'); +const files = readdirSync(join(ROOT, 'tests/vectors')) + .filter((f) => f.endsWith('.json')) + .sort(); +let m: CoreModule; + +beforeAll(async () => { + if (dir) m = await loadCore(dir); +}); + +afterAll(() => { + if (dir) pumpAll(m); +}); + +// Line for line port of tests/swift/VectorRunner.swift: the same ops, the same expect keys, the same messages. +class VectorRunner { + private clock = 0; + private readonly core: EditorCore; + private changes: WebChange[] = []; + private lastResult: unknown = null; + private lastError: string | null = null; + private layoutView: Layout | null = null; + private anchor: Anchor | null = null; + private widthChanged = false; + + constructor(private readonly testCase: Json) { + this.core = new EditorCore(m, (testCase.text as string | undefined) ?? ''); + this.core.setClock(0); + } + + private layout(op: Json): void { + let vp = this.layoutView; + if (!vp) { + vp = new Layout(this.core); + vp.attach(); + this.layoutView = vp; + } + const before = vp.textAreaWidth; + const insets = (op.insets as number[] | undefined) ?? [0, 0, 0, 0]; + vp.rowHeight = int(op, 'rowHeight'); + vp.gutterWidth = int(op, 'gutter'); + vp.textPadding = int(op, 'padding'); + vp.wrap = bool(op, 'wrap', false); + vp.resize(int(op, 'width'), int(op, 'height')); + vp.setInsets( + insets[0] as number, + insets[1] as number, + insets[2] as number, + insets[3] as number, + ); + this.widthChanged = vp.textAreaWidth !== before; + } + + run(): void { + const ops = (this.testCase.ops as Json[] | undefined) ?? []; + ops.forEach((op, i) => { + this.lastResult = null; + this.lastError = null; + this.apply(op); + this.changes.push(...this.core.takeChanges()); + if (op.expect) this.check(op.expect as Json, `after op ${i + 1} (${string(op, 'op')})`); + }); + if (this.testCase.expect) this.check(this.testCase.expect as Json, 'at end'); + } + + close(): void { + this.core.close(); + } + + private apply(op: Json): void { + const core = this.core; + const name = string(op, 'op'); + const vp = this.layoutView; + switch (name) { + case 'replace': + core.replace(int(op, 'start'), int(op, 'end'), string(op, 'text')); + break; + case 'setSelection': + core.setSelection(int(op, 'anchor'), int(op, 'head')); + break; + case 'selectAll': + core.selectAll(); + break; + case 'move': + core.move(string(op, 'unit') as MoveUnit, int(op, 'dir') > 0, bool(op, 'extend', false)); + break; + case 'selectWord': + core.selectWordAt(int(op, 'offset')); + break; + case 'type': + core.typeText(string(op, 'text')); + break; + case 'insertText': + core.insertText(string(op, 'text')); + break; + case 'setText': + core.setText(string(op, 'text'), bool(op, 'keepHistory', false)); + break; + case 'backspace': + core.backspace(); + break; + case 'delete': + core.delete(); + break; + case 'undo': + this.lastResult = core.undo(); + break; + case 'redo': + this.lastResult = core.redo(); + break; + case 'enter': + core.insertNewline(); + break; + case 'tab': + core.tab(); + break; + case 'shiftTab': + core.shiftTab(); + break; + case 'indent': + core.indent(); + break; + case 'outdent': + core.outdent(); + break; + case 'bracketMatch': + this.lastResult = core.bracketMatch(0); + break; + case 'search': + try { + core.setSearch({ + text: string(op, 'text'), + regex: bool(op, 'regex', false), + caseSensitive: bool(op, 'caseSensitive', false), + wholeWord: bool(op, 'wholeWord', false), + }); + core.ensureMatches(); + this.lastResult = core.matchCount; + } catch (error) { + this.lastError = error instanceof SearchError ? error.message : 'invalid'; + } + break; + case 'findNext': + this.lastResult = core.findNext(); + break; + case 'findPrevious': + this.lastResult = core.findPrevious(); + break; + case 'replaceCurrent': + this.lastResult = core.replaceCurrent(string(op, 'text')); + break; + case 'replaceAll': + this.lastResult = core.replaceAll(string(op, 'text')); + break; + case 'clearSearch': + core.clearSearch(); + break; + case 'config': { + const s = core.settings; + core.settings = { + tabSize: (op.tabSize as number | undefined) ?? s.tabSize, + insertSpaces: (op.insertSpaces as boolean | undefined) ?? s.insertSpaces, + autoIndent: (op.autoIndent as boolean | undefined) ?? s.autoIndent, + autoCloseBrackets: (op.autoCloseBrackets as boolean | undefined) ?? s.autoCloseBrackets, + }; + break; + } + case 'offsetToPosition': + this.lastResult = core.offsetToPosition(int(op, 'offset')); + break; + case 'positionToOffset': + this.lastResult = core.positionToOffset(int(op, 'line'), int(op, 'column')); + break; + case 'getLine': + this.lastResult = core.line(int(op, 'index')); + break; + case 'wait': + this.clock += int(op, 'ms'); + core.setClock(this.clock); + break; + case 'decorate': + core.setDecorations( + ((op.items as Json[] | undefined) ?? []).map(decorationOf), + (op.revision as number | undefined) ?? -1, + ); + break; + case 'layout': + this.layout(op); + break; + case 'measure': { + if (!vp) throw new Error('measure before layout'); + for (const pair of (op.pairs as number[][] | undefined) ?? []) { + vp.measured(pair[0] as number, pair[1] as number); + } + const widest = op.widest as number | undefined; + if (widest !== undefined && !vp.wrap) vp.widestLine = Math.max(vp.widestLine, widest); + break; + } + case 'scrollTo': + if (vp) this.lastResult = vp.scrollTo(int(op, 'x'), int(op, 'y')); + break; + case 'scrollBy': + if (vp) + this.lastResult = vp.scrollTo(vp.scrollX + int(op, 'dx'), vp.scrollY + int(op, 'dy')); + break; + case 'reveal': + if (vp) { + this.lastResult = vp.reveal( + int(op, 'left'), + int(op, 'top'), + int(op, 'right'), + int(op, 'bottom'), + ); + } + break; + case 'anchor': + this.anchor = vp?.anchor() ?? null; + break; + case 'restore': + if (this.anchor) vp?.restore(this.anchor); + break; + case 'resetRows': + vp?.resetRows(); + break; + default: + throw new Error(`op not implemented: ${name}`); + } + } + + private check(expect: Json, at: string): void { + const core = this.core; + const same = (key: string, actual: unknown, expected: unknown) => + expectEqual(`${key} ${at}`, actual, expected); + if (expect.decorations) { + same( + 'decorations', + core.decorations().map(describeDecoration), + (expect.decorations as Json[]).map((d) => describeDecoration(decorationOf(d))), + ); + } + if (expect.gapRows) { + const expected: Record = {}; + for (const [k, v] of Object.entries(expect.gapRows as Json)) expected[k] = v as number; + same('gapRows', Object.fromEntries(core.gapRows()), expected); + } + if (typeof expect.text === 'string') same('text', core.text(), expect.text); + if (typeof expect.length === 'number') same('length', core.length, expect.length); + if (typeof expect.lineCount === 'number') same('lineCount', core.lineCount, expect.lineCount); + if (typeof expect.revision === 'number') same('revision', core.revision, expect.revision); + if (expect.selection) { + const s = expect.selection as Json; + same('selection', core.selection, { anchor: int(s, 'anchor'), head: int(s, 'head') }); + } + if (expect.position) { + const p = expect.position as Json; + same('position', this.lastResult, { line: int(p, 'line'), column: int(p, 'column') }); + } + if (typeof expect.canUndo === 'boolean') same('canUndo', core.canUndo, expect.canUndo); + if (typeof expect.canRedo === 'boolean') same('canRedo', core.canRedo, expect.canRedo); + if (typeof expect.offset === 'number') same('offset', this.lastResult, expect.offset); + if (typeof expect.line === 'string') same('line', this.lastResult, expect.line); + if (expect.matches) same('matches', core.matches, (expect.matches as number[][]).flat()); + if ('found' in expect) { + const f = expect.found as Json | null; + same('found', this.lastResult, f ? { index: int(f, 'index'), total: int(f, 'total') } : null); + } + if ('result' in expect) this.checkResult(expect.result, at); + if (typeof expect.limited === 'boolean') same('limited', core.matchLimited, expect.limited); + if ('brackets' in expect) same('brackets', this.lastResult, expect.brackets); + if (expect.changes) { + const list = expect.changes as Json[]; + same('change count', this.changes.length, list.length); + list.forEach((e, i) => { + const c = this.changes[i]; + if (!c) return; + same(`change ${i} start`, c.start, int(e, 'start')); + same(`change ${i} end`, c.start + c.removedLength, int(e, 'end')); + same(`change ${i} text`, c.inserted, string(e, 'text')); + same(`change ${i} origin`, c.origin, string(e, 'origin')); + }); + } + if (typeof expect.error === 'boolean') { + same( + expect.error ? 'error expected' : 'no error expected', + this.lastError !== null, + expect.error, + ); + } + this.checkLayout(expect, at); + } + + private checkLayout(expect: Json, at: string): void { + const vp = this.layoutView; + if (!vp) return; + const same = (key: string, actual: unknown, expected: unknown) => + expectEqual(`${key} ${at}`, actual, expected); + if (typeof expect.widthChanged === 'boolean') + same('widthChanged', this.widthChanged, expect.widthChanged); + if (expect.scroll) same('scroll', [vp.scrollX, vp.scrollY], expect.scroll); + if (expect.maxScroll) same('maxScroll', [vp.maxScrollX, vp.maxScrollY], expect.maxScroll); + if (expect.content) same('content', [vp.contentWidth, vp.contentHeight], expect.content); + if (expect.visible) + same('visible', [vp.firstVisibleLine(), vp.lastVisibleLine()], expect.visible); + if (typeof expect.totalRows === 'number') same('totalRows', vp.totalRows, expect.totalRows); + for (const [k, v] of Object.entries((expect.rowStart as Json | undefined) ?? {})) { + same(`rowStart ${k}`, vp.rowStart(Number(k)), v); + } + for (const [k, v] of Object.entries((expect.lineAtRow as Json | undefined) ?? {})) { + same(`lineAtRow ${k}`, vp.lineAtRow(Number(k)), v); + } + for (const [k, v] of Object.entries((expect.isGapRow as Json | undefined) ?? {})) { + same(`isGapRow ${k}`, vp.isGapRow(Number(k)), v); + } + for (const [k, v] of Object.entries((expect.lineTop as Json | undefined) ?? {})) { + same(`lineTop ${k}`, vp.lineTop(Number(k)), v); + } + } + + private checkResult(expected: unknown, at: string): void { + if (typeof expected === 'boolean' || typeof expected === 'number') { + expectEqual(`result ${at}`, this.lastResult, expected); + } else { + throw new Error(`result ${at}: unsupported expectation ${String(expected)}`); + } + } +} + +function expectEqual(what: string, actual: unknown, expected: unknown): void { + expect({ what, value: actual }).toEqual({ what, value: expected }); +} + +function decorationOf(o: Json): Decoration { + switch (string(o, 'kind')) { + case 'underline': + return { + kind: 'underline', + range: { start: int(o, 'start'), end: int(o, 'end') }, + color: '#000000', + thickness: (o.thickness as number | undefined) ?? 1, + }; + case 'gap': + return { kind: 'gap', line: int(o, 'line'), rows: int(o, 'rows') }; + case 'lineBackground': + return { kind: 'lineBackground', line: int(o, 'line'), color: '#000000' }; + default: + throw new Error(`unknown decoration kind ${string(o, 'kind')}`); + } +} + +// Only what mapping changes: kind and range, or kind, line and rows. +function describeDecoration(d: Decoration): string { + switch (d.kind) { + case 'underline': + return `underline ${d.range.start} ${d.range.end}`; + case 'gap': + return `gap ${d.line} ${d.rows}`; + case 'lineBackground': + return `lineBackground ${d.line}`; + } +} + +function int(o: Json, key: string): number { + const v = o[key]; + if (typeof v !== 'number') throw new Error(`missing int ${key}`); + return v; +} + +function string(o: Json, key: string): string { + const v = o[key]; + if (typeof v !== 'string') throw new Error(`missing string ${key}`); + return v; +} + +function bool(o: Json, key: string, fallback: boolean): boolean { + const v = o[key]; + return typeof v === 'boolean' ? v : fallback; +} + +for (const file of files) { + const { cases } = JSON.parse(readFileSync(join(ROOT, 'tests/vectors', file), 'utf8')) as { + cases: Json[]; + }; + describe(file, () => { + for (const c of cases) { + const name = c.name as string; + // A pending case pins behavior the engine does not have yet. + const pending = typeof c.pending === 'string' && c.pending.length > 0; + (dir && !pending ? test : test.skip)(name, () => { + const runner = new VectorRunner(c); + try { + runner.run(); + } finally { + runner.close(); + } + }); + } + }); +} + +const javascript = join(ROOT, 'languages/javascript/web/javascript.wasm'); +if (dir && !existsSync(javascript)) + console.warn('javascript grammar not built, run yarn build:web-grammars'); + +(dir && existsSync(javascript) ? test : test.skip)( + 'closing an editor mid-parse leaves no live snapshot', + () => { + pumpAll(m); + const slots = m._cpw_test_slots(); + loadGrammar(m, javascript, 'javascript', join(ROOT, 'languages/javascript/queries/javascript')); + const js10k = readFileSync(join(ROOT, 'tests/highlight/javascript.txt'), 'utf8').repeat(15); + const core = new EditorCore(m, 'let a = 1;\n', 1000); + expect(core.setLanguage('javascript')).toBe(true); + pumpAll(m); + expect(core.snapshot).not.toBe(0); + core.setText(js10k); + m._cpw_pump(1); + expect(m._cpw_pump(0)).toBeGreaterThan(0); + core.close(); + pumpAll(m); + expect(m._cpw_test_held_snapshots()).toBe(0); + expect(m._cpw_test_slots()).toBe(slots); + }, +); diff --git a/src/web/aliases.ts b/src/web/aliases.ts new file mode 100644 index 00000000..bae18508 --- /dev/null +++ b/src/web/aliases.ts @@ -0,0 +1,69 @@ +// Names nvim queries and fence info strings use for languages shipped under another id. Empty means never inject. +// The same table as LanguageRegistry.kt and LanguageRegistry.swift. +export const INJECTION_ALIASES: Readonly> = Object.freeze({ + js: 'javascript', + jsx: 'javascript', + mjs: 'javascript', + cjs: 'javascript', + ts: 'typescript', + mts: 'typescript', + cts: 'typescript', + py: 'python', + python3: 'python', + kt: 'kotlin', + kts: 'kotlin', + rs: 'rust', + golang: 'go', + yml: 'yaml', + jsonc: 'json', + json5: 'json', + htm: 'html', + xhtml: 'html', + md: 'markdown', + h: 'c', + svg: 'xml', + xsd: 'xml', + xsl: 'xml', + xslt: 'xml', + rng: 'xml', + dosini: 'ini', + confini: 'ini', + cpp: '', + 'c++': '', + cc: '', + hpp: '', + objc: '', + bash: '', + sh: '', + shell: '', + zsh: '', + console: '', + text: '', + txt: '', + plain: '', + regex: '', + jsdoc: '', + phpdoc: '', + comment: '', + luap: '', + printf: '', + styled: '', + angular: '', + groq: '', + graphql: '', + glimmer: '', + sql: '', + c_sharp: '', +}); + +// Lowercases a fence or query name. An installed id wins over the aliases, null means never inject. +export function canonicalInjectionId( + name: string, + installed: (id: string) => boolean, +): string | null { + const lower = name.toLowerCase(); + if (!Object.hasOwn(INJECTION_ALIASES, lower)) return lower; + if (installed(lower)) return lower; + const mapped = INJECTION_ALIASES[lower] as string; + return mapped === '' ? null : mapped; +} diff --git a/src/web/configure.ts b/src/web/configure.ts new file mode 100644 index 00000000..d00bd373 --- /dev/null +++ b/src/web/configure.ts @@ -0,0 +1,11 @@ +let assetsUrl = '/codepad/'; + +// Where the browser loads the core and the languages from: the folder the web command filled, /codepad/ by default. +export function configureWeb(options: { assetsUrl?: string }): void { + if (options.assetsUrl === undefined) return; + assetsUrl = options.assetsUrl.endsWith('/') ? options.assetsUrl : `${options.assetsUrl}/`; +} + +export function webAssetsUrl(): string { + return assetsUrl; +} diff --git a/src/web/core/EditorCore.ts b/src/web/core/EditorCore.ts new file mode 100644 index 00000000..ac3647bf --- /dev/null +++ b/src/web/core/EditorCore.ts @@ -0,0 +1,489 @@ +import type { Decoration } from '../../types'; +import { type CoreModule, type Heap, heapOf, withCString } from './module'; + +export type Selection = { anchor: number; head: number }; +export type Position = { line: number; column: number }; +export type SearchHit = { index: number; total: number }; +export type SearchQuery = { + text: string; + regex: boolean; + caseSensitive: boolean; + wholeWord: boolean; +}; +export type EditorSettings = { + tabSize: number; + insertSpaces: boolean; + autoIndent: boolean; + autoCloseBrackets: boolean; +}; +export type ChangeOrigin = 'input' | 'api' | 'undo' | 'redo'; +export type MoveUnit = 'char' | 'word' | 'lineStart' | 'lineEnd' | 'docStart' | 'docEnd'; +export type WebChange = { + start: number; + removedLength: number; + insertedLength: number; + revision: number; + origin: ChangeOrigin; + removed: string; + inserted: string; +}; + +export class SearchError extends Error {} + +// The engine's parse budget per slice, so the page's pump stays inside a frame. +export const DEFAULT_BUDGET_MICROS = 4000; + +const ORIGINS: ChangeOrigin[] = ['input', 'api', 'undo', 'redo']; +const MOVES: MoveUnit[] = ['char', 'word', 'lineStart', 'lineEnd', 'docStart', 'docEnd']; +const Command = { + Newline: 0, + Tab: 1, + ShiftTab: 2, + Indent: 3, + Outdent: 4, + Backspace: 5, + Delete: 6, + Undo: 7, + Redo: 8, + SelectAll: 9, +} as const; +const Kind = { Underline: 0, Gap: 1, LineBackground: 2 } as const; +const SEARCH_REGEX = 1; +const SEARCH_CASE = 2; +const SEARCH_WORD = 4; + +// The editing state behind one editor, held by the core in the module's heap: document, selection, history and +// settings. The web twin of ios/Codepad/core/EditorCore.swift. +export class EditorCore { + readonly index: number; + readonly handle: number; + private readonly heap: Heap; + private payloads: Decoration[] = []; + private cachedVersion = -1; + private cached: Decoration[] = []; + private current: EditorSettings = { + tabSize: 4, + insertSpaces: true, + autoIndent: true, + autoCloseBrackets: true, + }; + private closed = false; + + constructor( + readonly m: CoreModule, + text = '', + budgetMicros = DEFAULT_BUDGET_MICROS, + ) { + this.heap = heapOf(m); + this.index = m._cpw_open(this.heap.units(text), text.length, BigInt(budgetMicros)); + this.handle = m._cpw_editor(this.index); + this.pushSettings(); + } + + get settings(): EditorSettings { + return { ...this.current }; + } + + set settings(s: EditorSettings) { + this.current = { ...s }; + this.pushSettings(); + } + + get selection(): Selection { + const packed = this.m._cp_editor_selection(this.handle); + return { + anchor: Number(BigInt.asIntN(32, packed >> 32n)), + head: Number(BigInt.asIntN(32, packed)), + }; + } + + get canUndo(): boolean { + return (this.m._cp_editor_history(this.handle) & 1) !== 0; + } + + get canRedo(): boolean { + return (this.m._cp_editor_history(this.handle) & 2) !== 0; + } + + get length(): number { + return this.m._cp_doc_length(this.handle) >>> 0; + } + + get lineCount(): number { + return this.m._cp_doc_line_count(this.handle); + } + + get revision(): number { + return this.m._cp_doc_revision(this.handle); + } + + lineStart(line: number): number { + return this.m._cp_doc_line_start(this.handle, line) >>> 0; + } + + lineEnd(line: number): number { + return this.m._cp_doc_line_end(this.handle, line) >>> 0; + } + + lineAt(offset: number): number { + return this.m._cp_doc_line_at(this.handle, clampUnsigned(offset)); + } + + // The text of [start, end), clamped to the document. + text(start = 0, end = this.length): string { + const length = this.length; + const s = Math.min(Math.max(0, start), length); + const e = Math.min(Math.max(s, end), length); + const out = this.heap.output((e - s) * 2); + const n = this.m._cp_doc_copy(this.handle, s, e, out, e - s) >>> 0; + return this.heap.readUnits(out, n); + } + + // The line's text without its newline, empty outside the document. + line(index: number): string { + if (index < 0 || index >= this.lineCount) return ''; + return this.text(this.lineStart(index), this.lineEnd(index)); + } + + offsetToPosition(offset: number): Position { + const out = this.heap.output(8); + this.m._cp_doc_position(out, this.handle, clampUnsigned(offset)); + const p = this.heap.readInts(out, 2); + return { line: p[0] as number, column: p[1] as number }; + } + + positionToOffset(line: number, column: number): number { + return this.m._cp_doc_offset(this.handle, line, column) >>> 0; + } + + replace(start: number, end: number, text: string, origin: ChangeOrigin = 'api'): void { + this.applyChange(start, end, text, origin, false, false); + } + + // Every mutation goes through here. typing marks single key input that may coalesce in the history. + // caretAfter puts a collapsed caret after the inserted text instead of mapping the selection. + applyChange( + start: number, + end: number, + text: string, + origin: ChangeOrigin, + typing: boolean, + caretAfter: boolean, + ): void { + this.m._cp_editor_replace( + this.handle, + clampUnsigned(start), + clampUnsigned(end), + this.heap.units(text), + text.length, + ORIGINS.indexOf(origin), + typing ? 1 : 0, + caretAfter ? 1 : 0, + ); + } + + insertText(text: string, origin: ChangeOrigin = 'api'): void { + this.m._cp_editor_insert( + this.handle, + this.heap.units(text), + text.length, + ORIGINS.indexOf(origin), + ); + } + + // A keyboard edit. The selection maps through the change like any edit. + inputReplace(start: number, end: number, text: string, typing = false): void { + this.applyChange(start, end, text, 'input', typing, false); + } + + setText(text: string, keepHistory = false): void { + this.m._cp_editor_set_text( + this.handle, + this.heap.units(text), + text.length, + keepHistory ? 1 : 0, + ); + } + + // revision is the document revision the items were computed for, below zero for the current one. False when the + // edits since then are no longer known, which keeps the current set. + setDecorations(items: Decoration[], revision: number): boolean { + const kept: Decoration[] = []; + const raw: number[] = []; + for (const d of items) { + if (d.kind === 'underline') { + // A thickness of zero or less would never advance the wave. + if ((d.thickness ?? 1) <= 0) continue; + raw.push(Kind.Underline, d.range.start, d.range.end, kept.length); + } else if (d.kind === 'gap') { + raw.push(Kind.Gap, d.line, d.rows, kept.length); + } else { + raw.push(Kind.LineBackground, d.line, 0, kept.length); + } + kept.push(d); + } + const ptr = this.heap.ints32(raw.length); + this.m.HEAP32.set(raw, ptr >> 2); + const accepted = + this.m._cp_editor_set_decorations(this.handle, ptr, kept.length, revision) !== 0; + if (accepted) this.payloads = kept; + return accepted; + } + + get decorationsVersion(): number { + return this.m._cp_editor_decorations_version(this.handle); + } + + // The decorations in current document coordinates, mapped by the core through every edit. + decorations(): Decoration[] { + const version = this.decorationsVersion; + if (version === this.cachedVersion) return this.cached; + const count = this.m._cp_editor_decorations(this.handle, 0, 0) >>> 0; + const out = this.heap.output(count * 16); + this.m._cp_editor_decorations(this.handle, out, count); + const raw = this.heap.readInts(out, count * 4); + const items: Decoration[] = []; + for (let i = 0; i < count; i++) { + const a = raw[i * 4 + 1] as number; + const b = raw[i * 4 + 2] as number; + const d = this.payloads[raw[i * 4 + 3] as number]; + if (!d) continue; + if (d.kind === 'underline') items.push({ ...d, range: { start: a, end: b } }); + else if (d.kind === 'gap') items.push({ kind: 'gap', line: a, rows: b }); + else items.push({ ...d, line: a }); + } + this.cached = items; + this.cachedVersion = version; + return items; + } + + // Total gap rows per line, lines with a gap only. + gapRows(): Map { + const out = new Map(); + for (const d of this.decorations()) { + if (d.kind === 'gap') out.set(d.line, (out.get(d.line) ?? 0) + d.rows); + } + return out; + } + + typeText(text: string): void { + this.m._cp_editor_type(this.handle, this.heap.units(text), text.length); + } + + insertNewline(): void { + this.command(Command.Newline); + } + tab(): void { + this.command(Command.Tab); + } + shiftTab(): void { + this.command(Command.ShiftTab); + } + indent(): void { + this.command(Command.Indent); + } + outdent(): void { + this.command(Command.Outdent); + } + backspace(): void { + this.command(Command.Backspace); + } + delete(): void { + this.command(Command.Delete); + } + selectAll(): void { + this.command(Command.SelectAll); + } + undo(): boolean { + return this.command(Command.Undo); + } + redo(): boolean { + return this.command(Command.Redo); + } + + setSelection(anchor: number, head: number): void { + this.m._cp_editor_set_selection(this.handle, clampInt(anchor), clampInt(head)); + } + + move(unit: MoveUnit, forward: boolean, extend: boolean): void { + this.m._cp_editor_move(this.handle, MOVES.indexOf(unit), forward ? 1 : 0, extend ? 1 : 0); + } + + selectWordAt(offset: number): void { + this.m._cp_editor_select_word_at(this.handle, clampUnsigned(offset)); + } + + // The bracket at the caret and its partner. A snapshot at the document's revision skips strings and comments. + bracketMatch(snapshot: number): [number, number] | null { + const out = this.heap.output(8); + if ( + this.m._cp_editor_bracket_match( + this.handle, + snapshot, + clampUnsigned(this.selection.head), + out, + ) === 0 + ) { + return null; + } + const pair = this.heap.readInts(out, 2); + return [pair[0] as number, pair[1] as number]; + } + + get searchActive(): boolean { + return this.m._cp_editor_search_active(this.handle) !== 0; + } + + // An invalid regex throws before any state changes, except that it clears the search. + setSearch(query: SearchQuery): void { + let flags = 0; + if (query.regex) flags |= SEARCH_REGEX; + if (query.caseSensitive) flags |= SEARCH_CASE; + if (query.wholeWord) flags |= SEARCH_WORD; + const error = this.heap.output(512); + const ok = this.m._cp_editor_set_search( + this.handle, + this.heap.units(query.text), + query.text.length, + flags, + error, + 512, + ); + if (ok === 0) throw new SearchError(this.m.UTF8ToString(error, 512)); + } + + clearSearch(): void { + this.m._cp_editor_clear_search(this.handle); + } + + // Computes the matches when they are stale. + ensureMatches(): void { + this.m._cp_editor_matches(this.handle, 0, 0); + } + + // The matches as start and end pairs, computed when stale. + get matches(): number[] { + const count = this.m._cp_editor_matches(this.handle, 0, 0) >>> 0; + if (count === 0) return []; + const out = this.heap.output(count * 4); + this.m._cp_editor_matches(this.handle, out, count); + return Array.from(this.heap.readInts(out, count)); + } + + get matchCount(): number { + return this.m._cp_editor_match_count(this.handle); + } + + // True when a match attempt hit PCRE2's match limit, so the matches stop there. + get matchLimited(): boolean { + return this.m._cp_editor_match_limited(this.handle) !== 0; + } + + get searchStale(): boolean { + return this.m._cp_editor_search_stale(this.handle) !== 0; + } + + findNext(): SearchHit | null { + return this.find(true); + } + + findPrevious(): SearchHit | null { + return this.find(false); + } + + replaceCurrent(text: string): boolean { + return this.m._cp_editor_replace_current(this.handle, this.heap.units(text), text.length) !== 0; + } + + replaceAll(text: string): number { + return this.m._cp_editor_replace_all(this.handle, this.heap.units(text), text.length); + } + + // A language id the page has not loaded clears the language, so the editor draws plain text. + setLanguage(id: string): boolean { + return withCString(this.m, id, (ptr) => this.m._cpw_set_language(this.index, ptr)) !== 0; + } + + // The latest snapshot, or 0 before the first parse. + get snapshot(): number { + return this.m._cpw_snapshot(this.index); + } + + get snapshotCount(): number { + return this.m._cpw_snapshot_count(this.index); + } + + // Pins the history clock to millis, for tests and recordings. Below zero follows the page clock. + setClock(millis: number): void { + this.m._cpw_set_clock(this.index, millis); + } + + // The changes since the last call, oldest first. + takeChanges(): WebChange[] { + const m = this.m; + const lenPtr = this.heap.output(4); + const needed = m._cpw_take_changes(this.index, 0, 0, 0, 0, lenPtr) >>> 0; + if (needed === 0) return []; + const units = m.HEAP32[lenPtr >> 2] as number; + const ints = m._malloc(needed * 4); + const text = m._malloc(Math.max(2, units * 2)); + try { + m._cpw_take_changes(this.index, ints, needed, text, units, lenPtr); + const raw = this.heap.readInts(ints, needed); + const out: WebChange[] = []; + for (let i = 0; i < needed; i += 7) { + const removedLength = raw[i + 1] as number; + const insertedLength = raw[i + 2] as number; + out.push({ + start: raw[i] as number, + removedLength, + insertedLength, + revision: raw[i + 3] as number, + origin: ORIGINS[raw[i + 4] as number] ?? 'api', + removed: this.heap.readUnits(text + (raw[i + 5] as number) * 2, removedLength), + inserted: this.heap.readUnits(text + (raw[i + 6] as number) * 2, insertedLength), + }); + } + return out; + } finally { + m._free(ints); + m._free(text); + } + } + + // Frees the editor now. Its engine frees itself in later pumps. + close(): void { + if (this.closed) return; + this.closed = true; + this.m._cpw_close(this.index); + } + + private find(forward: boolean): SearchHit | null { + const out = this.heap.output(8); + if (this.m._cp_editor_find(this.handle, forward ? 1 : 0, out, out + 4) === 0) return null; + const hit = this.heap.readInts(out, 2); + return { index: hit[0] as number, total: hit[1] as number }; + } + + private command(command: number): boolean { + return this.m._cp_editor_command(this.handle, command) !== 0; + } + + private pushSettings(): void { + const s = this.current; + const ptr = this.heap.ints32(4); + this.m.HEAP32.set( + [s.tabSize, s.insertSpaces ? 1 : 0, s.autoIndent ? 1 : 0, s.autoCloseBrackets ? 1 : 0], + ptr >> 2, + ); + this.m._cp_editor_set_settings(this.handle, ptr); + } +} + +function clampUnsigned(n: number): number { + return Math.min(Math.max(0, Math.trunc(n)), 0xffffffff); +} + +function clampInt(n: number): number { + return Math.min(Math.max(Math.trunc(n), -0x80000000), 0x7fffffff); +} diff --git a/src/web/core/Layout.ts b/src/web/core/Layout.ts new file mode 100644 index 00000000..e5ecb44f --- /dev/null +++ b/src/web/core/Layout.ts @@ -0,0 +1,408 @@ +import type { EditorCore } from './EditorCore'; +import type { CoreModule, Heap } from './module'; +import { heapOf } from './module'; + +export type Anchor = { line: number; offset: number }; +// A gap under a line: the view y where it starts and its height in rows. +export type Gap = { line: number; top: number; rows: number }; + +// cp_geometry, in the order of its fields. +const G = { + scrollX: 0, + scrollY: 1, + maxScrollX: 2, + maxScrollY: 3, + contentWidth: 4, + contentHeight: 5, + firstLine: 6, + lastLine: 7, + textAreaLeft: 8, + textAreaWidth: 9, + viewportHeight: 10, + wrapWidth: 11, + textLeft: 12, + totalRows: 13, + lineCount: 14, + rowHeight: 15, + widestLine: 16, +} as const; +const GEOMETRY_INTS = 17; +const CONFIG_INTS = 10; + +// Scroll position, rows per line and the mapping between rows and view pixels, held by the core's layout of one +// editor. Outside a draw each member is one core call. Inside one, the members read the copy the draw's scroll and +// measure calls returned. The web twin of ios/Codepad/layout/Viewport.swift. +export class Layout { + readonly handle: number; + private readonly m: CoreModule; + private readonly heap: Heap; + // row_height, width, height, inset_top, inset_bottom, inset_left, inset_right, gutter_width, text_padding, wrap + private readonly config = new Int32Array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + + private drawing = false; + private cached = false; + private known = new Int32Array(GEOMETRY_INTS); + private first = 0; + private last = -1; + private buffer = new Int32Array(0); + private needed = 0; + + constructor(private readonly core: EditorCore) { + this.m = core.m; + this.heap = heapOf(core.m); + this.handle = this.m._cpw_layout(core.index); + } + + // Takes the editor's lines, one row each, and its gaps. The size, insets and metrics stay. + attach(): void { + this.changing(); + this.m._cp_editor_attach_layout(this.core.handle, this.handle); + } + + get rowHeight(): number { + return this.config[0] as number; + } + set rowHeight(v: number) { + this.config[0] = v; + this.configure(); + } + get width(): number { + return this.config[1] as number; + } + get height(): number { + return this.config[2] as number; + } + get insetTop(): number { + return this.config[3] as number; + } + get insetBottom(): number { + return this.config[4] as number; + } + get insetLeft(): number { + return this.config[5] as number; + } + get insetRight(): number { + return this.config[6] as number; + } + get gutterWidth(): number { + return this.config[7] as number; + } + set gutterWidth(v: number) { + this.config[7] = v; + this.configure(); + } + get textPadding(): number { + return this.config[8] as number; + } + set textPadding(v: number) { + this.config[8] = v; + this.configure(); + } + get wrap(): boolean { + return this.config[9] !== 0; + } + set wrap(v: boolean) { + this.config[9] = v ? 1 : 0; + this.configure(); + } + get widestLine(): number { + return this.g(G.widestLine); + } + set widestLine(v: number) { + this.changing(); + this.m._cp_layout_set_widest(this.handle, v); + } + + get scrollX(): number { + return this.g(G.scrollX); + } + get scrollY(): number { + return this.g(G.scrollY); + } + get textAreaLeft(): number { + return this.g(G.textAreaLeft); + } + get textAreaWidth(): number { + return this.g(G.textAreaWidth); + } + get viewportHeight(): number { + return this.g(G.viewportHeight); + } + // Width a line wraps at. Zero means no wrapping. + get wrapWidth(): number { + return this.g(G.wrapWidth); + } + get textLeft(): number { + return this.g(G.textLeft); + } + get contentWidth(): number { + return this.g(G.contentWidth); + } + get contentHeight(): number { + return this.g(G.contentHeight); + } + get maxScrollX(): number { + return this.g(G.maxScrollX); + } + get maxScrollY(): number { + return this.g(G.maxScrollY); + } + get totalRows(): number { + return this.g(G.totalRows); + } + get lineCount(): number { + return this.g(G.lineCount); + } + + // Starts a draw at the scroll position, clamped. True when the position moved. + beginDraw(scrollX: number, scrollY: number): boolean { + this.drawing = true; + this.last = this.first - 1; + const out = this.heap.output(GEOMETRY_INTS * 4); + const moved = this.m._cp_layout_scroll_to(this.handle, scrollX, scrollY, out) !== 0; + this.known = this.heap.readInts(out, GEOMETRY_INTS); + this.cached = true; + return moved; + } + + // The draw's measure call: the rows laid out in this draw and the widest line, then the tops and rows of + // first..last and every gap. True when a measured line above the view moved the scroll. + measure(pairs: number[], widest: number, first: number, last: number): boolean { + const before = this.scrollY; + this.first = first; + this.last = last; + this.fill(pairs, widest); + return this.known[G.scrollY] !== before; + } + + endDraw(): void { + this.drawing = false; + this.cached = false; + this.last = this.first - 1; + } + + // Clamps and applies. Returns true when the position moved. + scrollTo(x: number, y: number): boolean { + const out = this.heap.output(GEOMETRY_INTS * 4); + const moved = this.m._cp_layout_scroll_to(this.handle, x, y, out) !== 0; + this.moving(out, moved); + return moved; + } + + clamp(): boolean { + return this.scrollTo(this.scrollX, this.scrollY); + } + + // Stores a view size. True when the text area width changed, which is when layouts must be rebuilt. + resize(width: number, height: number): boolean { + this.config[1] = width; + this.config[2] = height; + return this.configure(); + } + + // Stores the insets. True when the text area width changed. + setInsets(top: number, bottom: number, left: number, right: number): boolean { + this.config.set([top, bottom, left, right], 3); + return this.configure(); + } + + rowStart(line: number): number { + const top = this.drawnTop(line); + if (top !== null) { + return Math.trunc((top - this.insetTop + (this.known[G.scrollY] as number)) / this.rowHeight); + } + return this.m._cp_layout_row_start(this.handle, line); + } + + rows(line: number): number { + if (this.cached && line >= this.first && line <= this.last) { + return this.buffer[this.last - this.first + 2 + line - this.first] as number; + } + return this.m._cp_layout_rows(this.handle, line); + } + + gapRows(line: number): number { + return this.m._cp_layout_gap_rows(this.handle, line); + } + + lineAtRow(row: number): number { + return this.m._cp_layout_line_at_row(this.handle, row); + } + + // True when row is empty space under a line rather than one of its text rows. + isGapRow(row: number): boolean { + return this.m._cp_layout_is_gap_row(this.handle, row) !== 0; + } + + rowTop(row: number): number { + if (this.cached) + return this.insetTop + row * this.rowHeight - (this.known[G.scrollY] as number); + return this.m._cp_layout_row_top(this.handle, row); + } + + lineTop(line: number): number { + return this.drawnTop(line) ?? this.m._cp_layout_line_top(this.handle, line); + } + + // Content row under view y, possibly outside [0, totalRows). + rowAt(y: number): number { + return this.m._cp_layout_row_at(this.handle, y); + } + + // Every gap in line order. + gaps(): Gap[] { + if (!this.cached || this.last < this.first) { + const count = this.m._cp_layout_measure(this.handle, 0, 0, -1, 0, 0, 0, 0, 0) >>> 0; + const out = this.heap.output(count * 4); + this.m._cp_layout_measure(this.handle, 0, 0, -1, 0, 0, 0, out, count); + return parseGaps(this.heap.readInts(out, count), 3, count); + } + return parseGaps(this.buffer, 2 * (this.last - this.first + 1) + 1, this.needed); + } + + // Layout x for a view x. + contentX(x: number): number { + return x - this.textLeft; + } + + // View x for a layout x. + viewX(x: number): number { + return x + this.textLeft; + } + + firstVisibleLine(): number { + return this.g(G.firstLine); + } + + lastVisibleLine(): number { + return this.g(G.lastLine); + } + + // Smallest scroll that shows a rect in layout coordinates with the text padding around it. + reveal(left: number, top: number, right: number, bottom: number): boolean { + const out = this.heap.output(GEOMETRY_INTS * 4); + const moved = this.m._cp_layout_reveal(this.handle, left, top, right, bottom, out) !== 0; + this.moving(out, moved); + return moved; + } + + // Remembers the top visible line so a width or font change can put it back. + anchor(): Anchor { + const out = this.heap.output(8); + this.m._cp_layout_anchor(this.handle, out, out + 4); + const a = this.heap.readInts(out, 2); + return { line: a[0] as number, offset: a[1] as number }; + } + + restore(anchor: Anchor): void { + this.changing(); + this.m._cp_layout_restore(this.handle, anchor.line, anchor.offset); + } + + // Records a measurement and raises the widest line to widest when not wrapping. A line above the viewport shifts + // the scroll so the visible text stays put. + measured(line: number, rows: number, widest = -1): void { + if (this.drawing) { + this.fill([line, rows], widest); + } else { + const pairs = this.heap.ints32(2); + this.m.HEAP32.set([line, rows], pairs >> 2); + this.m._cp_layout_measure(this.handle, pairs, 1, widest, 0, -1, 0, 0, 0); + } + } + + // Every line back to one row. Gaps stay. + resetRows(): void { + this.changing(); + this.m._cp_layout_reset_rows(this.handle); + } + + // One measure call that refreshes the draw's copy, grown and repeated when the buffer is short. Applying the same + // rows and widest line again changes nothing. + private fill(pairs: number[], widest: number): void { + const m = this.m; + const pairPtr = this.heap.ints32(pairs.length); + m.HEAP32.set(pairs, pairPtr >> 2); + const geometry = this.heap.output(GEOMETRY_INTS * 4 + Math.max(this.buffer.length, 256) * 4); + const outPtr = geometry + GEOMETRY_INTS * 4; + let cap = Math.max(this.buffer.length, 256); + let n = + m._cp_layout_measure( + this.handle, + pairPtr, + pairs.length / 2, + widest, + this.first, + this.last, + geometry, + outPtr, + cap, + ) >>> 0; + if (n > cap) { + cap = n; + const grown = this.heap.output(GEOMETRY_INTS * 4 + cap * 4); + n = + m._cp_layout_measure( + this.handle, + pairPtr, + pairs.length / 2, + widest, + this.first, + this.last, + grown, + grown + GEOMETRY_INTS * 4, + cap, + ) >>> 0; + this.known = this.heap.readInts(grown, GEOMETRY_INTS); + this.buffer = this.heap.readInts(grown + GEOMETRY_INTS * 4, cap); + } else { + this.known = this.heap.readInts(geometry, GEOMETRY_INTS); + this.buffer = this.heap.readInts(outPtr, cap); + } + this.needed = n; + this.cached = true; + } + + private drawnTop(line: number): number | null { + return this.cached && this.last >= this.first && line >= this.first && line <= this.last + 1 + ? (this.buffer[line - this.first] as number) + : null; + } + + private g(field: number): number { + if (this.cached) return this.known[field] as number; + const out = this.heap.output(GEOMETRY_INTS * 4); + this.m._cp_layout_geometry(this.handle, out); + return this.m.HEAP32[(out >> 2) + field] as number; + } + + // A scroll inside a draw refreshes the geometry. Tops taken before a move are stale. + private moving(out: number, moved: boolean): void { + if (!this.drawing) return; + this.known = this.heap.readInts(out, GEOMETRY_INTS); + this.cached = true; + if (moved) this.last = this.first - 1; + } + + // A size, metric or row change has no place inside a draw: the copy would go stale. + private changing(): void { + if (!this.drawing) return; + this.cached = false; + this.last = this.first - 1; + } + + private configure(): boolean { + this.changing(); + const ptr = this.heap.ints32(CONFIG_INTS); + this.m.HEAP32.set(this.config, ptr >> 2); + return this.m._cp_layout_configure(this.handle, ptr) !== 0; + } +} + +function parseGaps(ints: Int32Array, start: number, end: number): Gap[] { + const out: Gap[] = []; + for (let i = start; i + 2 < end; i += 3) { + out.push({ line: ints[i] as number, top: ints[i + 1] as number, rows: ints[i + 2] as number }); + } + return out; +} diff --git a/src/web/core/module.ts b/src/web/core/module.ts new file mode 100644 index 00000000..675d396f --- /dev/null +++ b/src/web/core/module.ts @@ -0,0 +1,270 @@ +// The Emscripten module built by cpp/web/build.sh, typed by hand: the package never imports the glue. + +type Ptr = number; + +export interface CoreModule { + FS: { + writeFile(path: string, data: Uint8Array): void; + createPreloadedFile( + parent: string, + name: string, + url: string | Uint8Array, + canRead: boolean, + canWrite: boolean, + onload?: () => void, + onerror?: () => void, + ): void; + }; + HEAPU8: Uint8Array; + HEAPU16: Uint16Array; + HEAP32: Int32Array; + _malloc(size: number): Ptr; + _free(ptr: Ptr): void; + stringToNewUTF8(s: string): Ptr; + UTF8ToString(ptr: Ptr, maxBytes?: number): string; + + _cpw_error(): Ptr; + _cpw_language_load(id: Ptr, path: Ptr, symbol: Ptr, highlights: Ptr, injections: Ptr): number; + _cpw_language_alias(name: Ptr, id: Ptr): void; + _cpw_open(text: Ptr, length: number, budgetMicros: bigint): number; + _cpw_close(e: number): void; + _cpw_set_language(e: number, id: Ptr): number; + _cpw_editor(e: number): Ptr; + _cpw_layout(e: number): Ptr; + _cpw_snapshot(e: number): Ptr; + _cpw_snapshot_count(e: number): number; + _cpw_set_clock(e: number, millis: number): void; + _cpw_pump(budgetMs: number): number; + _cpw_take_changes( + e: number, + out: Ptr, + cap: number, + text: Ptr, + textCap: number, + textLen: Ptr, + ): number; + _cpw_test_held_snapshots(): number; + _cpw_test_slots(): number; + + _cp_doc_length(ed: Ptr): number; + _cp_doc_revision(ed: Ptr): number; + _cp_doc_line_count(ed: Ptr): number; + _cp_doc_line_start(ed: Ptr, line: number): number; + _cp_doc_line_end(ed: Ptr, line: number): number; + _cp_doc_line_at(ed: Ptr, offset: number): number; + _cp_doc_copy(ed: Ptr, start: number, end: number, out: Ptr, cap: number): number; + // Structs come back through a pointer passed first. + _cp_doc_position(out: Ptr, ed: Ptr, offset: number): void; + _cp_doc_offset(ed: Ptr, line: number, column: number): number; + _cp_editor_set_settings(ed: Ptr, settings: Ptr): void; + _cp_editor_replace( + ed: Ptr, + start: number, + end: number, + text: Ptr, + length: number, + origin: number, + typing: number, + caretAfter: number, + ): void; + _cp_editor_set_text(ed: Ptr, text: Ptr, length: number, keepHistory: number): void; + _cp_editor_insert(ed: Ptr, text: Ptr, length: number, origin: number): void; + _cp_editor_type(ed: Ptr, text: Ptr, length: number): void; + _cp_editor_command(ed: Ptr, command: number): number; + _cp_editor_set_selection(ed: Ptr, anchor: number, head: number): void; + _cp_editor_move(ed: Ptr, unit: number, forward: number, extend: number): void; + _cp_editor_select_word_at(ed: Ptr, offset: number): void; + _cp_editor_selection(ed: Ptr): bigint; + _cp_editor_history(ed: Ptr): number; + _cp_editor_bracket_match(ed: Ptr, snapshot: Ptr, caret: number, out: Ptr): number; + _cp_editor_set_decorations(ed: Ptr, items: Ptr, count: number, revision: number): number; + _cp_editor_decorations(ed: Ptr, out: Ptr, cap: number): number; + _cp_editor_decorations_version(ed: Ptr): number; + _cp_editor_set_search( + ed: Ptr, + query: Ptr, + length: number, + flags: number, + error: Ptr, + errorCap: number, + ): number; + _cp_editor_clear_search(ed: Ptr): void; + _cp_editor_search_active(ed: Ptr): number; + _cp_editor_search_stale(ed: Ptr): number; + _cp_editor_match_count(ed: Ptr): number; + _cp_editor_match_limited(ed: Ptr): number; + _cp_editor_matches(ed: Ptr, out: Ptr, cap: number): number; + _cp_editor_find(ed: Ptr, forward: number, index: Ptr, total: Ptr): number; + _cp_editor_replace_current(ed: Ptr, text: Ptr, length: number): number; + _cp_editor_replace_all(ed: Ptr, text: Ptr, length: number): number; + + _cp_layout_configure(layout: Ptr, config: Ptr): number; + _cp_layout_geometry(layout: Ptr, out: Ptr): void; + _cp_layout_scroll_to(layout: Ptr, x: number, y: number, out: Ptr): number; + _cp_layout_reveal( + layout: Ptr, + left: number, + top: number, + right: number, + bottom: number, + out: Ptr, + ): number; + _cp_layout_measure( + layout: Ptr, + pairs: Ptr, + pairCount: number, + widest: number, + first: number, + last: number, + out: Ptr, + topsRows: Ptr, + cap: number, + ): number; + _cp_layout_set_widest(layout: Ptr, widest: number): void; + _cp_layout_rows(layout: Ptr, line: number): number; + _cp_layout_gap_rows(layout: Ptr, line: number): number; + _cp_layout_row_start(layout: Ptr, line: number): number; + _cp_layout_line_at_row(layout: Ptr, row: number): number; + _cp_layout_is_gap_row(layout: Ptr, row: number): number; + _cp_layout_row_top(layout: Ptr, row: number): number; + _cp_layout_line_top(layout: Ptr, line: number): number; + _cp_layout_row_at(layout: Ptr, y: number): number; + _cp_layout_anchor(layout: Ptr, line: Ptr, offset: Ptr): void; + _cp_layout_restore(layout: Ptr, line: number, offset: number): number; + _cp_layout_reset_rows(layout: Ptr): void; + _cp_editor_attach_layout(ed: Ptr, layout: Ptr): void; + + _cp_highlight( + snapshot: Ptr, + first: number, + last: number, + firstLineStart: number, + out: Ptr, + cap: number, + ): number; + _cp_editor_frame( + ed: Ptr, + layout: Ptr, + snapshot: Ptr, + appliedRevision: number, + useChanged: number, + entries: Ptr, + entryCount: number, + out: Ptr, + cap: number, + text: Ptr, + textCap: number, + textLen: Ptr, + ): number; + _cp_shift_spans( + spans: Ptr, + count: number, + start: number, + removed: number, + inserted: number, + out: Ptr, + cap: number, + ): number; + _cp_snapshot_length(snapshot: Ptr): number; + // revision, based_on, latency_ms, fresh, reused, carried, changed_len + _cp_snapshot_info_get(snapshot: Ptr, out: Ptr): void; + _cp_style_id(name: Ptr): number; + _cp_style_name(id: number, out: Ptr, cap: number): number; +} + +// Fatal so a lone surrogate throws instead of turning into U+FFFD, and ignoreBOM keeps a leading U+FEFF. +const decoder = new TextDecoder('utf-16le', { fatal: true, ignoreBOM: true }); + +// Copies units as stored, lone surrogates included, a chunk at a time to stay under the argument limit. +function unitsToString(units: Uint16Array): string { + let s = ''; + for (let i = 0; i < units.length; i += 8192) + s += String.fromCharCode(...units.subarray(i, i + 8192)); + return s; +} + +// Grows on demand, one region per use. Callers never hold a region across another call that takes the same one. +class Region { + ptr = 0; + bytes = 0; + + constructor(private readonly m: CoreModule) {} + + take(bytes: number): number { + if (bytes > this.bytes) { + if (this.ptr) this.m._free(this.ptr); + this.bytes = Math.max(bytes, this.bytes * 2, 256); + this.ptr = this.m._malloc(this.bytes); + } + return this.ptr; + } +} + +// Scratch memory in the module's heap, shared by every editor on the page. +export class Heap { + private readonly text: Region; + private readonly ints: Region; + private readonly out: Region; + + constructor(readonly m: CoreModule) { + this.text = new Region(m); + this.ints = new Region(m); + this.out = new Region(m); + } + + // Writes s as UTF-16 units and returns its pointer. + units(s: string): number { + const ptr = this.text.take(s.length * 2 + 2); + const heap = this.m.HEAPU16; + const base = ptr >> 1; + for (let i = 0; i < s.length; i++) heap[base + i] = s.charCodeAt(i); + return ptr; + } + + // A region of count int32s for inputs, zeroed. + ints32(count: number): number { + const ptr = this.ints.take(Math.max(1, count) * 4); + this.m.HEAP32.fill(0, ptr >> 2, (ptr >> 2) + Math.max(1, count)); + return ptr; + } + + // A region of bytes for outputs. + output(bytes: number): number { + return this.out.take(Math.max(4, bytes)); + } + + readUnits(ptr: number, length: number): string { + if (length <= 0) return ''; + const units = this.m.HEAPU16.subarray(ptr >> 1, (ptr >> 1) + length); + try { + return decoder.decode(units); + } catch { + return unitsToString(units); + } + } + + readInts(ptr: number, count: number): Int32Array { + return Int32Array.from(this.m.HEAP32.subarray(ptr >> 2, (ptr >> 2) + count)); + } +} + +const heaps = new WeakMap(); + +export function heapOf(m: CoreModule): Heap { + let heap = heaps.get(m); + if (!heap) { + heap = new Heap(m); + heaps.set(m, heap); + } + return heap; +} + +// Runs use with s as a NUL terminated UTF-8 string in the heap. +export function withCString(m: CoreModule, s: string, use: (ptr: number) => T): T { + const ptr = m.stringToNewUTF8(s); + try { + return use(ptr); + } finally { + m._free(ptr); + } +} diff --git a/src/web/frame.ts b/src/web/frame.ts new file mode 100644 index 00000000..bbf531a3 --- /dev/null +++ b/src/web/frame.ts @@ -0,0 +1,76 @@ +export type FrameLine = { spans: Int32Array; text: string }; +export type FrameRun = { lo: number; hi: number; lines: FrameLine[] }; +export type DecodedFrame = { + revision: number; + length: number; + lineCount: number; + anchor: number; + head: number; + caretLine: number; + decorationsVersion: number; + history: number; + // The window, clamped to the document. + first: number; + last: number; + // first..last + 1. The line after the last one starts at length + 1. + lineStarts: number[]; + // The lines to lay out, with their spans as cp_highlight writes them and their text. + runs: FrameRun[]; +}; + +// ignoreBOM keeps a line's leading U+FEFF, so span offsets stay right. +const decoder = new TextDecoder('utf-16le', { ignoreBOM: true }); + +// Reads one cp_editor_frame result, laid out as codepad.h describes it. +export function decodeFrame(ints: Int32Array, text: Uint16Array): DecodedFrame { + let i = 0; + const next = () => ints[i++] as number; + const revision = next(); + const length = next(); + const lineCount = next(); + const anchor = next(); + const head = next(); + const caretLine = next(); + const decorationsVersion = next(); + const history = next(); + const first = next(); + const last = next(); + const lo = Math.min(Math.max(first, 0), lineCount - 1); + const hi = Math.min(Math.max(last, lo), lineCount - 1); + const lineStarts: number[] = []; + for (let line = lo; line <= hi + 1; line++) lineStarts.push(next()); + const runCount = next(); + const runs: FrameRun[] = []; + for (let r = 0; r < runCount; r++) runs.push({ lo: next(), hi: next(), lines: [] }); + const spans: Int32Array[] = []; + for (const run of runs) { + for (let line = run.lo; line <= run.hi; line++) { + const triples = next(); + spans.push(ints.slice(i, i + triples * 3)); + i += triples * 3; + } + } + let k = 0; + for (const run of runs) { + for (let line = run.lo; line <= run.hi; line++) { + const offset = next(); + const count = next(); + const lineText = count === 0 ? '' : decoder.decode(text.subarray(offset, offset + count)); + run.lines.push({ spans: spans[k++] as Int32Array, text: lineText }); + } + } + return { + revision, + length, + lineCount, + anchor, + head, + caretLine, + decorationsVersion, + history, + first: lo, + last: hi, + lineStarts, + runs, + }; +} diff --git a/src/web/loader.ts b/src/web/loader.ts new file mode 100644 index 00000000..4b81d25d --- /dev/null +++ b/src/web/loader.ts @@ -0,0 +1,232 @@ +/// +import { canonicalInjectionId, INJECTION_ALIASES } from './aliases'; +import { webAssetsUrl } from './configure'; +import { type CoreModule, withCString } from './core/module'; +import { setPumpCore } from './pump'; + +// languages.json, as the web command writes it. +type ManifestLanguage = { + id: string; + wasm: string; + symbol: string; + highlights: string | null; + injections: string | null; +}; +type Manifest = { version: 1; languages: ManifestLanguage[] }; + +type CreateCore = (options: { locateFile(file: string): string }) => Promise; + +const INJECTION_LANGUAGE = /#set!\s+injection\.language\s+"([^"]+)"/g; + +let core: Promise | null = null; +let manifest: Promise | null = null; +let ids: string[] = []; +// Each language registered on its own, resolving to the ids its injection query names, or null when it failed. +const languages = new Map>(); + +// The page's one core, injected by a script tag from the assets folder on first use. +export function loadCore(): Promise { + if (!core) { + const pending = createCore(webAssetsUrl()); + core = pending; + // A failed load is not kept, so the next editor tries again. + pending.catch(() => { + if (core === pending) core = null; + }); + } + return core; +} + +async function createCore(base: string): Promise { + const g = globalThis as { createCodepadCore?: CreateCore }; + if (!g.createCodepadCore) await injectScript(`${base}codepad.js`); + if (!g.createCodepadCore) throw new Error(`[codepad] ${base}codepad.js did not define the core`); + const m = await g.createCodepadCore({ locateFile: (file) => base + file }); + for (const [name, id] of Object.entries(INJECTION_ALIASES)) { + withCString(m, name, (n) => withCString(m, id, (i) => m._cpw_language_alias(n, i))); + } + setPumpCore(m); + return m; +} + +function injectScript(url: string): Promise { + return new Promise((resolve, reject) => { + const script = document.createElement('script'); + script.src = url; + script.async = true; + script.onload = () => resolve(); + script.onerror = () => reject(new Error(`[codepad] could not load ${url}`)); + document.head.appendChild(script); + }); +} + +// The ids in languages.json once it loaded, empty before. +export function availableLanguages(): string[] { + return [...ids]; +} + +function loadManifest(): Promise { + if (!manifest) { + const url = `${webAssetsUrl()}languages.json`; + manifest = fetchText(url).then( + (text) => { + if (text === null) return null; + const parsed = parseManifest(text); + if (!parsed) { + console.warn( + `[codepad] ${url} is not the file the web command writes. Run npx react-native-codepad web .`, + ); + return null; + } + ids = parsed.languages.map((l) => l.id); + return parsed; + }, + (e: unknown) => { + manifest = null; + throw e; + }, + ); + } + return manifest; +} + +// A dev server answers a missing file with its index page, so the text may not be JSON at all. +function parseManifest(text: string): Manifest | null { + try { + const parsed = JSON.parse(text) as Partial | null; + return Array.isArray(parsed?.languages) ? (parsed as Manifest) : null; + } catch { + return null; + } +} + +// True once the language and all it injects, at any depth, are registered. False and one warning when it fails. +export async function ensureLanguage(m: CoreModule, id: string): Promise { + const seen = new Set([id]); + const host = loadOne(m, id); + let wave = [host]; + while (wave.length > 0) { + const next: Promise[] = []; + for (const names of await Promise.all(wave)) { + for (const name of names ?? []) { + if (seen.has(name)) continue; + seen.add(name); + next.push(loadOne(m, name)); + } + } + wave = next; + } + return (await host) !== null; +} + +// Registers one language without waiting for the ones it injects: the engine resolves those when it parses. +function loadOne(m: CoreModule, id: string): Promise { + let pending = languages.get(id); + if (!pending) { + // A network error is not kept, so a later editor retries. A 404 or a missing id stays, warned once. + pending = loadLanguage(m, id).catch((e: unknown) => { + if (!(e instanceof Offline)) throw e; + languages.delete(id); + return null; + }); + languages.set(id, pending); + } + return pending; +} + +async function loadLanguage(m: CoreModule, id: string): Promise { + const base = webAssetsUrl(); + const list = await loadManifest(); + if (!list) return null; + const entry = list.languages.find((l) => l.id === id); + if (!entry) { + console.warn(`[codepad] language "${id}" is not in ${base}languages.json`); + return null; + } + const [wasm, highlights, injections] = await Promise.all([ + fetchBytes(base + entry.wasm), + entry.highlights ? fetchText(base + entry.highlights) : '', + entry.injections ? fetchText(base + entry.injections) : '', + ]); + if (wasm === null || highlights === null || injections === null) return null; + const installed = (name: string) => list.languages.some((l) => l.id === name); + const injected = new Set(); + for (const [, name] of injections.matchAll(INJECTION_LANGUAGE)) { + const target = canonicalInjectionId(name as string, installed); + if (target && target !== id && installed(target)) injected.add(target); + } + const path = `/${id}.so`; + const compiled = await new Promise((resolve) => + m.FS.createPreloadedFile( + '/', + `${id}.so`, + wasm, + true, + false, + () => resolve(true), + () => resolve(false), + ), + ); + if (!compiled) { + console.warn(`[codepad] could not compile ${base}${entry.wasm}`); + return null; + } + const ok = withCString(m, id, (i) => + withCString(m, path, (p) => + withCString(m, entry.symbol, (s) => + withCString(m, highlights, (h) => + withCString(m, injections, (j) => m._cpw_language_load(i, p, s, h, j)), + ), + ), + ), + ); + if (ok !== 1) { + console.warn( + `[codepad] could not load ${base}${entry.wasm}: ${m.UTF8ToString(m._cpw_error())}`, + ); + return null; + } + return [...injected]; +} + +async function fetchText(url: string): Promise { + const response = await request(url); + return response ? readBody(url, () => response.text()) : null; +} + +async function fetchBytes(url: string): Promise { + const response = await request(url); + return response ? new Uint8Array(await readBody(url, () => response.arrayBuffer())) : null; +} + +// A body cut off by the network is a network error too. +async function readBody(url: string, read: () => Promise): Promise { + try { + return await read(); + } catch (e) { + throw offline(url, e); + } +} + +// A fetch that never reached the server, which a later load may get past. +class Offline extends Error {} + +// Warns once for the url and returns the error a later load may get past. +function offline(url: string, e: unknown): Offline { + const message = `[codepad] could not load ${url}: ${e instanceof Error ? e.message : String(e)}`; + console.warn(message); + return new Offline(message); +} + +// The response, or null after a warning for an HTTP error. Throws Offline after a warning for a network error. +async function request(url: string): Promise { + let response: Response; + try { + response = await fetch(url); + } catch (e) { + throw offline(url, e); + } + if (response.ok) return response; + console.warn(`[codepad] could not load ${url}: ${response.status}`); + return null; +} diff --git a/src/web/pump.ts b/src/web/pump.ts new file mode 100644 index 00000000..72fd53c4 --- /dev/null +++ b/src/web/pump.ts @@ -0,0 +1,37 @@ +import type { CoreModule } from './core/module'; + +// The engine's share of a frame, so a key still draws inside it. +export const PUMP_BUDGET_MS = 6; + +type View = { draw(): void }; + +const views = new Set(); +let core: CoreModule | null = null; +let scheduled = false; + +export function setPumpCore(m: CoreModule): void { + core = m; +} + +// Adds a view to the page's frame loop and returns its removal. +export function registerView(view: View): () => void { + views.add(view); + requestPump(); + return () => { + views.delete(view); + }; +} + +// One shared frame: the engine runs its queued work, then every view draws. A view asks for the next frame here. +export function requestPump(): void { + if (scheduled) return; + scheduled = true; + requestAnimationFrame(frame); +} + +function frame(): void { + scheduled = false; + const left = core ? core._cpw_pump(PUMP_BUDGET_MS) : 0; + for (const view of [...views]) view.draw(); + if (left > 0) requestPump(); +} diff --git a/src/web/theme.ts b/src/web/theme.ts new file mode 100644 index 00000000..81a31a5b --- /dev/null +++ b/src/web/theme.ts @@ -0,0 +1,64 @@ +import { githubLight } from '../themes/githubLight'; +import type { Theme, TokenStyle } from '../types'; + +export type CssTokenStyle = { color: string; bold: boolean; italic: boolean }; +type ColorKey = Exclude; +export type CssTheme = Record & { + // Longest dotted prefix wins: keyword.function falls back to keyword, then to null for the plain foreground. + token(name: string): CssTokenStyle | null; +}; + +const HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i; +const RGB = /^rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(?:,\s*[0-9.]+\s*)?\)$/; + +// The color when it is one of the formats native accepts, #rgb, #rgba, #rrggbb, #rrggbbaa, rgb() and rgba(). +export function cssColor(value: string, fallback: string): string { + const v = typeof value === 'string' ? value.trim() : ''; + return HEX.test(v) || RGB.test(v) ? v : fallback; +} + +// The theme as CSS colors. A color that does not parse takes the githubLight color of its key, as on iOS. +export function cssTheme(theme: Theme): CssTheme { + const colors = {} as Record; + for (const key of Object.keys(githubLight) as (keyof Theme)[]) { + if (key === 'tokens') continue; + colors[key] = cssColor(theme[key], githubLight[key]); + } + const tokens = new Map(); + for (const [name, style] of Object.entries(theme.tokens ?? {})) { + tokens.set(name, tokenStyle(style)); + } + const cache = new Map(); + return { + ...colors, + token(name: string): CssTokenStyle | null { + const cached = cache.get(name); + if (cached !== undefined) return cached; + let key = name; + let found: CssTokenStyle | null = null; + for (;;) { + const style = tokens.get(key); + if (style) { + found = style; + break; + } + const dot = key.lastIndexOf('.'); + if (dot < 0) break; + key = key.slice(0, dot); + } + cache.set(name, found); + return found; + }, + }; +} + +function tokenStyle(style: TokenStyle): CssTokenStyle { + if (typeof style === 'string') { + return { color: cssColor(style, githubLight.foreground), bold: false, italic: false }; + } + return { + color: cssColor(style.color, githubLight.foreground), + bold: style.bold ?? false, + italic: style.italic ?? false, + }; +} diff --git a/tests/swift/CoreEngineTests.swift b/tests/swift/CoreEngineTests.swift index 4bc54431..3e9c0ff7 100644 --- a/tests/swift/CoreEngineTests.swift +++ b/tests/swift/CoreEngineTests.swift @@ -29,6 +29,11 @@ final class TestHost: @unchecked Sendable { init(threaded: Bool) { self.threaded = threaded } + // Runs an engine call that belongs on the main thread where this host runs its posts, as an app does. + func onMain(_ body: () -> Void) { + if threaded { main.sync(execute: body) } else { body() } + } + func register(_ id: String, _ language: OpaquePointer) { lock.withLock { languages[id] = language } } func host() -> cp_host { @@ -277,7 +282,7 @@ final class CoreEngineTests: XCTestCase { while host.waitDelivery(2) { for s in host.takeSnapshots() { cp_engine_release(engine, s) } } - cp_engine_close(engine) + host.onMain { cp_engine_close(engine) } XCTAssertTrue(host.waitDelivery()) for s in host.takeSnapshots() { cp_engine_release(nil, s) } XCTAssertEqual(host.finalPosts, 1) @@ -288,7 +293,7 @@ final class CoreEngineTests: XCTestCase { let engine = engine(host, budget: 50) cp_engine_set_language(engine, Self.javascript) attach(engine, String(repeating: "let a = [1, 2, 3];\n", count: 50_000)) - cp_engine_close(engine) + host.onMain { cp_engine_close(engine) } while host.waitDelivery() { if host.finalPosts == 1 { break } } diff --git a/tests/web/aliases.test.ts b/tests/web/aliases.test.ts new file mode 100644 index 00000000..e22fbe8f --- /dev/null +++ b/tests/web/aliases.test.ts @@ -0,0 +1,43 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import path from 'node:path'; +import { test } from 'node:test'; +import { canonicalInjectionId, INJECTION_ALIASES } from '../../src/web/aliases.ts'; + +const root = path.join(import.meta.dirname, '..', '..'); + +// The (name, id) pairs between start and the next line that closes it, in a native source file. +function table(file: string, start: string, close: string, pair: RegExp): Record { + const source = readFileSync(path.join(root, file), 'utf8'); + const from = source.indexOf(start); + const body = source.slice(from, source.indexOf(close, from)); + const out: Record = {}; + for (const [, name, id] of body.matchAll(pair)) out[name as string] = id as string; + return out; +} + +test('the web alias table matches Kotlin and Swift', () => { + const kotlin = table( + 'android/src/main/java/me/sectiontn/codepad/syntax/LanguageRegistry.kt', + 'aliases = mapOf(', + '\n )', + /"([^"]+)" to "([^"]*)"/g, + ); + const swift = table( + 'ios/Codepad/syntax/LanguageRegistry.swift', + 'aliases: [String: String] = [', + '\n ]', + /"([^"]+)": "([^"]*)"/g, + ); + assert.ok(Object.keys(kotlin).length > 40); + assert.deepEqual({ ...INJECTION_ALIASES }, kotlin); + assert.deepEqual({ ...INJECTION_ALIASES }, swift); +}); + +test('an installed id wins over its alias and an empty alias never injects', () => { + const installed = (id: string) => id === 'sql'; + assert.equal(canonicalInjectionId('JS', installed), 'javascript'); + assert.equal(canonicalInjectionId('sql', installed), 'sql'); + assert.equal(canonicalInjectionId('bash', installed), null); + assert.equal(canonicalInjectionId('constructor', installed), 'constructor'); +}); diff --git a/tests/web/core.ts b/tests/web/core.ts new file mode 100644 index 00000000..57c69528 --- /dev/null +++ b/tests/web/core.ts @@ -0,0 +1,129 @@ +// Loads a built WASM core into jest, for the tests under src/web/__tests__. +import { existsSync, readdirSync, readFileSync } from 'node:fs'; +import { createRequire } from 'node:module'; +import { join } from 'node:path'; + +export const ROOT = join(__dirname, '..', '..'); + +import type { CoreModule } from '../../src/web/core/module'; + +export type RawCore = CoreModule; + +// The folder holding codepad.js and codepad.wasm, or null with a warning when it is not built. +export function builtCore(dir: string, hint: string): string | null { + const path = join(ROOT, dir); + if (existsSync(join(path, 'codepad.wasm'))) return path; + console.warn(`${dir} not built, run ${hint}`); + return null; +} + +// Node's own require, so jest neither transforms the glue nor runs it in its sandbox. +export async function loadCore(dir: string): Promise { + const create = createRequire(__filename)(join(dir, 'codepad.js')) as () => Promise; + return create(); +} + +export function cstring(m: RawCore, s: string, use: (ptr: number) => T): T { + const ptr = m.stringToNewUTF8(s); + try { + return use(ptr); + } finally { + m._free(ptr); + } +} + +export function utf16(m: RawCore, s: string, use: (ptr: number, length: number) => T): T { + const ptr = m._malloc(Math.max(2, s.length * 2)); + for (let i = 0; i < s.length; i++) m.HEAPU16[(ptr >> 1) + i] = s.charCodeAt(i); + try { + return use(ptr, s.length); + } finally { + m._free(ptr); + } +} + +function query(dir: string, name: string): string { + const path = join(dir, `${name}.scm`); + return existsSync(path) ? readFileSync(path, 'utf8') : ''; +} + +// Writes a grammar side module into the module's files and registers it with the queries in queries. +export function loadGrammar(m: RawCore, wasm: string, id: string, queries: string): void { + const path = `/${id}.so`; + m.FS.writeFile(path, readFileSync(wasm)); + const ok = cstring(m, id, (idp) => + cstring(m, path, (pathp) => + cstring(m, `tree_sitter_${id}`, (sym) => + cstring(m, query(queries, 'highlights'), (hl) => + cstring(m, query(queries, 'injections'), (inj) => + m._cpw_language_load(idp, pathp, sym, hl, inj), + ), + ), + ), + ), + ); + if (ok !== 1) throw new Error(`${id}: ${m.UTF8ToString(m._cpw_error())}`); +} + +export type WebGrammar = { grammar: string; wasm: string; queries: string }; + +// Every built grammar module in languages and in the example's custom packages. +export function webGrammars(): WebGrammar[] { + const out: WebGrammar[] = []; + for (const base of ['languages', 'example/modules']) { + for (const pkg of readdirSync(join(ROOT, base)).sort()) { + const web = join(ROOT, base, pkg, 'web'); + if (!existsSync(web)) continue; + for (const file of readdirSync(web).filter((f) => f.endsWith('.wasm'))) { + const grammar = file.slice(0, -5); + out.push({ + grammar, + wasm: join(web, file), + queries: join(ROOT, base, pkg, 'queries', grammar), + }); + } + } + } + return out; +} + +export function styleName(m: RawCore, id: number): string { + const out = m._malloc(256); + const length = m._cp_style_name(id, out, 256); + const name = m.UTF8ToString(out).slice(0, length); + m._free(out); + return name; +} + +// Each line's (start column, end column, style id) triples for lines first..last. +export function highlight( + m: RawCore, + snapshot: number, + first: number, + last: number, + start: number, +): number[][] { + let cap = 4096; + let out = m._malloc(cap * 4); + let count = m._cp_highlight(snapshot, first, last, start, out, cap); + if (count > cap) { + m._free(out); + cap = count; + out = m._malloc(cap * 4); + count = m._cp_highlight(snapshot, first, last, start, out, cap); + } + const ints = m.HEAP32.subarray(out >> 2, (out >> 2) + count); + const lines: number[][] = []; + let i = 0; + for (let line = first; line <= last; line++) { + const n = i < count ? (ints[i++] as number) : 0; + lines.push(Array.from(ints.subarray(i, i + n * 3))); + i += n * 3; + } + m._free(out); + return lines; +} + +export function pumpAll(m: RawCore, budgetMs = 8): void { + for (let i = 0; i < 100000 && m._cpw_pump(budgetMs) > 0; i++); +} diff --git a/tests/web/harness/index.html b/tests/web/harness/index.html new file mode 100644 index 00000000..3f53a426 --- /dev/null +++ b/tests/web/harness/index.html @@ -0,0 +1,49 @@ + + + + + codepad web harness + + + +
+
+
+ + + diff --git a/tests/web/render.e2e.mjs b/tests/web/render.e2e.mjs new file mode 100644 index 00000000..a027971d --- /dev/null +++ b/tests/web/render.e2e.mjs @@ -0,0 +1,449 @@ +// The read-only web editor in Chromium and WebKit: yarn prepare, yarn build:web, cpp/web/build.sh --test and +// yarn build:web-grammars first. +import assert from 'node:assert/strict'; +import { readFileSync, realpathSync, rmSync } from 'node:fs'; +import { createServer } from 'node:http'; +import path from 'node:path'; +import { after, before, describe, test } from 'node:test'; +import { chromium, webkit } from 'playwright'; +import { copyWebAssets } from '../../lib/cli/web-assets.js'; + +const root = path.join(import.meta.dirname, '..', '..'); +const TYPES = { + '.html': 'text/html', + '.js': 'text/javascript', + '.wasm': 'application/wasm', + '.json': 'application/json', +}; +const KEYWORD = 'rgb(207, 34, 46)'; +// The folders the harness page loads from: itself, the built modules and the copied assets. +const SERVED = ['tests/web/harness', 'lib', 'build/web-e2e'].map( + (dir) => path.join(realpathSync(root), dir) + path.sep, +); + +let server; +let base; + +before(async () => { + const out = path.join(root, 'build/web-e2e'); + rmSync(out, { recursive: true, force: true }); + copyWebAssets(path.join(root, 'example'), path.join(out, 'ok')); + copyWebAssets(path.join(root, 'example'), path.join(out, 'missing')); + rmSync(path.join(out, 'missing/codepad/python.wasm')); + // The core with the test hooks, from cpp/web/build.sh --test. + copyWebAssets( + path.join(root, 'example'), + path.join(out, 'test'), + path.join(root, 'build/web-test'), + ); + server = createServer((req, res) => { + const file = path.resolve( + root, + `.${decodeURIComponent(new URL(req.url, 'http://x').pathname)}`, + ); + try { + // The real path, so a symlink cannot lead out of the served folders. + const real = realpathSync(file); + if (!SERVED.some((dir) => real.startsWith(dir))) throw new Error('not served'); + const body = readFileSync(real); + res.writeHead(200, { + 'content-type': TYPES[path.extname(file)] ?? 'application/octet-stream', + }); + res.end(body); + } catch { + res.writeHead(404); + res.end(); + } + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + base = `http://127.0.0.1:${server.address().port}`; +}); + +after(() => server.close()); + +for (const engine of [chromium, webkit]) { + describe(engine.name(), () => { + let browser; + before(async () => { + // Chromium keeps its scrollbars, so a test can see the space they take as on Windows and Linux. + browser = await engine.launch( + engine === chromium ? { ignoreDefaultArgs: ['--hide-scrollbars'] } : {}, + ); + }); + after(() => browser.close()); + + async function open(assets = 'ok') { + const page = await browser.newPage(); + page.setDefaultTimeout(10000); + const warnings = []; + const errors = []; + page.on('console', (m) => m.type() === 'warning' && warnings.push(m.text())); + page.on('pageerror', (e) => errors.push(e.message)); + await page.goto( + `${base}/tests/web/harness/index.html?assets=/build/web-e2e/${assets}/codepad/`, + ); + await page.waitForFunction(() => window.ready === true); + return { page, warnings, errors }; + } + + test('colors python keywords', async () => { + const { page, errors } = await open(); + const color = await page.evaluate(async () => { + const { EditorView, loadCore, until } = window.codepad; + const view = new EditorView( + document.getElementById('a'), + await loadCore(), + 'def greet(name):\n return name\n', + ); + view.setLanguage('python'); + const span = () => + [...document.querySelectorAll('#a [data-line="0"] span')].find( + (s) => s.textContent === 'def', + ); + await until(() => span()); + return getComputedStyle(span()).color; + }); + assert.equal(color, KEYWORD); + assert.deepEqual(errors, []); + await page.close(); + }); + + test("two editors keep separate languages and one survives the other's unmount", async () => { + const { page, errors } = await open(); + const result = await page.evaluate(async () => { + const { EditorView, loadCore, until } = window.codepad; + const m = await loadCore(); + // html injects javascript and javascript injects html, and both start loading at once. + const a = new EditorView(document.getElementById('a'), m, '\n'); + const b = new EditorView(document.getElementById('b'), m, 'let x = 1;\n'); + a.setLanguage('html'); + b.setLanguage('javascript'); + const colored = (id, word) => { + const s = [...document.querySelectorAll(`#${id} [data-line="0"] span`)].find( + (e) => e.textContent === word, + ); + return s ? getComputedStyle(s).color : null; + }; + await until(() => colored('a', 'let') && colored('b', 'let')); + a.destroy(); + b.apply(() => b.core.replace(0, 0, 'const y = 2;\n')); + await until(() => colored('b', 'const')); + return { + emptied: document.getElementById('a').childElementCount, + b: colored('b', 'const'), + }; + }); + assert.equal(result.emptied, 0); + assert.equal(result.b, KEYWORD); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('a destroyed view writes nothing into the editor opened after it', async () => { + const { page, errors } = await open(); + const text = await page.evaluate(async () => { + const { EditorView, loadCore, until } = window.codepad; + const m = await loadCore(); + const a = new EditorView(document.getElementById('a'), m, 'first'); + await until(() => document.querySelector('#a [data-line="0"]')); + a.destroy(); + const b = new EditorView(document.getElementById('b'), m, 'second'); + a.apply(() => a.core.replace(0, 0, 'ZZZ')); + return b.core.text(); + }); + assert.equal(text, 'second'); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('unmounting during a parse closes the engine and keeps no snapshot', async () => { + const { page, errors } = await open('test'); + const result = await page.evaluate(async () => { + const { EditorView, ensureLanguage, frames, loadCore, until } = window.codepad; + const m = await loadCore(); + await ensureLanguage(m, 'python'); + const host = document.getElementById('a'); + const text = Array.from({ length: 20000 }, (_, i) => `def f${i}(x):\n return x + ${i}`); + const view = new EditorView(host, m, text.join('\n')); + view.setLanguage('python'); + await until(() => host.querySelector('[data-line="0"]')); + view.destroy(); + for (let i = 0; i < 120 && m._cpw_test_slots() > 0; i++) await frames(1); + await frames(2); + return { + slots: m._cpw_test_slots(), + held: m._cpw_test_held_snapshots(), + children: host.childElementCount, + }; + }); + assert.deepEqual(result, { slots: 0, held: 0, children: 0 }); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('destroying the only editor on an idle page frees its engine', async () => { + const { page, errors } = await open('test'); + const result = await page.evaluate(async () => { + const { EditorView, frames, loadCore, until } = window.codepad; + const m = await loadCore(); + const host = document.getElementById('a'); + const view = new EditorView(host, m, 'def f():\n pass\n'); + view.setLanguage('python'); + await until(() => host.querySelector('[data-line="0"] span')); + await frames(10); + view.destroy(); + await frames(30); + return { slots: m._cpw_test_slots(), held: m._cpw_test_held_snapshots() }; + }); + assert.deepEqual(result, { slots: 0, held: 0 }); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('wrapped lines stay left of a vertical scrollbar that takes space', async () => { + const { page, errors } = await open(); + const result = await page.evaluate(async () => { + const { EditorView, frames, loadCore, until } = window.codepad; + const style = document.createElement('style'); + style.textContent = + '#a [data-scroller]::-webkit-scrollbar { width: 16px; } #a [data-scroller]::-webkit-scrollbar-thumb { background: #888; }'; + document.head.append(style); + const host = document.getElementById('a'); + const text = Array.from({ length: 60 }, () => 'word '.repeat(80)).join('\n'); + const view = new EditorView(host, await loadCore(), text); + view.setOptions({ softWrap: true }); + const scroller = host.querySelector('[data-scroller]'); + await until(() => host.querySelector('[data-line="0"]')); + await frames(5); + const edge = scroller.getBoundingClientRect().left + scroller.clientWidth; + const lines = [...host.querySelectorAll('[data-line]')]; + return { + bar: scroller.offsetWidth - scroller.clientWidth, + wrapped: lines[0].getBoundingClientRect().height > view.layout.rowHeight, + over: lines.filter((el) => el.getBoundingClientRect().right > edge + 0.5).length, + }; + }); + assert.ok(result.bar > 0, `no scrollbar space: ${result.bar}`); + assert.equal(result.wrapped, true); + assert.equal(result.over, 0); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('an editor that does not scroll reports no vertical scroll when the page scrolls', async () => { + const { page, errors } = await open(); + const ys = await page.evaluate(async () => { + const { EditorView, frames, loadCore, until } = window.codepad; + const host = document.getElementById('c'); + const text = Array.from({ length: 300 }, (_, i) => `line ${i}`).join('\n'); + const ys = []; + const view = new EditorView(host, await loadCore(), text); + view.setOptions({ scrollEnabled: false, onScroll: (_x, y) => ys.push(y) }); + await until(() => host.querySelector('[data-line="0"]')); + window.scrollTo(0, 2000); + await frames(5); + return ys; + }); + assert.ok( + ys.every((y) => y === 0), + String(ys), + ); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('a highlighted span after an emoji holds whole characters', async () => { + const { page, errors } = await open(); + const texts = await page.evaluate(async () => { + const { EditorView, loadCore, until } = window.codepad; + const view = new EditorView( + document.getElementById('a'), + await loadCore(), + 'const s = "\u{1F600}"; let t = 1;', + ); + view.setLanguage('javascript'); + const spans = () => [...document.querySelectorAll('#a [data-line="0"] span')]; + await until(() => spans().some((s) => s.textContent === 'let')); + return spans().map((s) => s.textContent); + }); + assert.ok(texts.includes('"\u{1F600}"'), JSON.stringify(texts)); + assert.ok(texts.includes('let'), JSON.stringify(texts)); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('wrapped lines of every length stack without overlapping', async () => { + const { page, errors } = await open(); + const count = await page.evaluate(async () => { + const { EditorView, frames, loadCore, overlaps, until } = window.codepad; + const host = document.getElementById('a'); + const text = Array.from({ length: 12 }, (_, i) => 'word '.repeat(10 + i * 7)).join('\n'); + const view = new EditorView(host, await loadCore(), text); + view.setOptions({ softWrap: true }); + await until(() => host.querySelector('[data-line="0"]')); + await frames(5); + return overlaps('a'); + }); + assert.equal(count, 0); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('a theme that makes keywords bold measures wrapped lines again', async () => { + const { page, errors } = await open(); + const result = await page.evaluate(async () => { + const { EditorView, frames, githubLight, loadCore, overlaps, until } = window.codepad; + const host = document.getElementById('a'); + const text = `x = a ${'and not b '.repeat(150)}\ny = 1\n`; + const view = new EditorView(host, await loadCore(), text); + view.setOptions({ softWrap: true, fontFamily: 'Arial' }); + view.setLanguage('python'); + await until(() => host.querySelector('[data-line="0"] span')); + await frames(3); + const bold = { ...githubLight, tokens: { ...githubLight.tokens } }; + for (const name of Object.keys(bold.tokens)) { + if (name.startsWith('keyword')) bold.tokens[name] = { color: '#cf222e', bold: true }; + } + view.setOptions({ theme: bold }); + await frames(5); + return { lines: host.querySelectorAll('[data-line]').length, overlaps: overlaps('a') }; + }); + assert.ok(result.lines >= 2, `only ${result.lines} line drawn, nothing to overlap`); + assert.equal(result.overlaps, 0); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('revealSelection shows the caret on a later row of a wrapped line', async () => { + const { page, errors } = await open(); + const y = await page.evaluate(async () => { + const { EditorView, frames, loadCore, until } = window.codepad; + const host = document.getElementById('a'); + const text = `${'word '.repeat(600)}\nend`; + const view = new EditorView(host, await loadCore(), text); + view.setOptions({ softWrap: true }); + await until(() => host.querySelector('[data-line="0"]')); + await frames(3); + const head = view.core.lineEnd(0); + view.apply(() => view.core.setSelection(head, head)); + view.revealSelection(); + await frames(5); + return view.caretRect(head)?.y ?? null; + }); + assert.ok(y !== null && y >= 0 && y < 300, `caret y ${y}`); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('turning scrolling back on gives the editor its host height again', async () => { + const { page, errors } = await open(); + const result = await page.evaluate(async () => { + const { EditorView, frames, loadCore, until } = window.codepad; + const host = document.getElementById('a'); + const text = Array.from({ length: 100 }, (_, i) => `line ${i}`).join('\n'); + const view = new EditorView(host, await loadCore(), text); + view.setOptions({ scrollEnabled: false }); + await until(() => host.firstElementChild.getBoundingClientRect().height > 300); + view.setOptions({ scrollEnabled: true }); + await frames(5); + const scroller = host.querySelector('[data-scroller]'); + return { + height: host.firstElementChild.getBoundingClientRect().height, + overflowY: getComputedStyle(scroller).overflowY, + }; + }); + assert.deepEqual(result, { height: 300, overflowY: 'auto' }); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('caret x grows across an emoji and Arabic', async () => { + const { page, errors } = await open(); + const result = await page.evaluate(async () => { + const { EditorView, loadCore, until } = window.codepad; + const view = new EditorView( + document.getElementById('a'), + await loadCore(), + 'a\u{1F600}ب c', + ); + await until(() => document.querySelector('#a [data-line="0"]')); + const xs = [0, 1, 3, 4, 5, 6].map((offset) => view.caretRect(offset)?.x ?? null); + const nodes = [...document.querySelectorAll('#a [data-line="0"] span, #a [data-line="0"]')]; + const split = nodes.some((n) => + [...n.childNodes].some( + (c) => c.nodeType === 3 && /^[\uDC00-\uDFFF]|[\uD800-\uDBFF]$/.test(c.data), + ), + ); + return { xs, split }; + }); + assert.ok( + result.xs.every((x) => typeof x === 'number'), + String(result.xs), + ); + for (let i = 1; i < result.xs.length; i++) + assert.ok(result.xs[i] > result.xs[i - 1], String(result.xs)); + assert.equal(result.split, false); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('content height without scrolling', async () => { + const { page, errors } = await open(); + const result = await page.evaluate(async () => { + const { EditorView, loadCore, until } = window.codepad; + const host = document.getElementById('c'); + const text = Array.from({ length: 300 }, (_, i) => `line ${i}`).join('\n'); + const sizes = []; + const view = new EditorView(host, await loadCore(), text); + view.setOptions({ scrollEnabled: false, onContentSize: (_w, h) => sizes.push(h) }); + await until(() => sizes.length > 0); + const first = sizes[sizes.length - 1]; + const content = view.layout.contentHeight; + const tall = host.getBoundingClientRect().height; + const scroller = host.querySelector('[data-scroller]'); + const inner = scroller.scrollHeight - scroller.clientHeight; + view.apply(() => view.core.replace(0, 0, 'new\n')); + await until(() => sizes[sizes.length - 1] !== first); + return { + first, + content, + tall, + inner, + next: sizes[sizes.length - 1], + row: view.layout.rowHeight, + }; + }); + assert.equal(result.first, result.content); + assert.equal(result.tall, result.first); + assert.equal(result.inner, 0); + assert.equal(result.next, result.first + result.row); + assert.deepEqual(errors, []); + await page.close(); + }); + + test('a missing grammar draws plain text', async () => { + const { page, warnings, errors } = await open('missing'); + const warned = page.waitForEvent('console', (m) => /python\.wasm/.test(m.text())); + await page.evaluate(async () => { + const { EditorView, loadCore, until } = window.codepad; + const view = new EditorView( + document.getElementById('a'), + await loadCore(), + 'def f():\n pass\n', + ); + view.setLanguage('python'); + await until(() => document.querySelector('#a [data-line="0"]')?.textContent === 'def f():'); + }); + await warned; + const result = await page.evaluate(async () => { + await window.codepad.frames(2); + return document.querySelectorAll('#a [data-line] span').length; + }); + assert.equal(result, 0); + assert.equal(warnings.length, 1, String(warnings)); + assert.match(warnings[0], /\/build\/web-e2e\/missing\/codepad\/python\.wasm/); + assert.deepEqual(errors, []); + await page.close(); + }); + }); +} diff --git a/yarn.lock b/yarn.lock index 2bfc8003..98aef3ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -310,6 +310,19 @@ __metadata: languageName: node linkType: hard +"@babel/generator@npm:^7.29.14": + version: 7.29.14 + resolution: "@babel/generator@npm:7.29.14" + dependencies: + "@babel/parser": "npm:^7.29.14" + "@babel/types": "npm:^7.29.14" + "@jridgewell/gen-mapping": "npm:^0.3.12" + "@jridgewell/trace-mapping": "npm:^0.3.28" + jsesc: "npm:^3.0.2" + checksum: 10c0/39db746c47e0b80290b727e64025dd0ee9060f346271b5ca75ba07485df05f704a74358910bbe8e9f6e2a8fca62ac98f886640d2c84e7740af5fec42fbe0b71a + languageName: node + linkType: hard + "@babel/helper-annotate-as-pure@npm:^7.29.7": version: 7.29.7 resolution: "@babel/helper-annotate-as-pure@npm:7.29.7" @@ -469,6 +482,13 @@ __metadata: languageName: node linkType: hard +"@babel/helper-string-parser@npm:^7.29.14": + version: 7.29.14 + resolution: "@babel/helper-string-parser@npm:7.29.14" + checksum: 10c0/7e296ef8457872da9f9306ef29a67470505bca7f21728ff4fede08746eac2bb122a3a3be39135d337a9c56985bae24e4c2dd8f13896afe64a32e9251363ed7bd + languageName: node + linkType: hard + "@babel/helper-string-parser@npm:^7.29.7": version: 7.29.7 resolution: "@babel/helper-string-parser@npm:7.29.7" @@ -522,6 +542,17 @@ __metadata: languageName: node linkType: hard +"@babel/parser@npm:^7.29.14": + version: 7.29.14 + resolution: "@babel/parser@npm:7.29.14" + dependencies: + "@babel/types": "npm:^7.29.14" + bin: + parser: ./bin/babel-parser.js + checksum: 10c0/5d76040cb386915a5086c864b5966a08bada9fa6074fa2b16cce42ed78ecae309003d3362fef88a96b9a40afc9761604502db93f7cea9db59af077d1ff2109f4 + languageName: node + linkType: hard + "@babel/plugin-bugfix-firefox-class-in-computed-class-key@npm:^7.29.7": version: 7.29.7 resolution: "@babel/plugin-bugfix-firefox-class-in-computed-class-key@npm:7.29.7" @@ -1749,6 +1780,13 @@ __metadata: languageName: node linkType: hard +"@babel/runtime@npm:^7.18.6": + version: 7.29.14 + resolution: "@babel/runtime@npm:7.29.14" + checksum: 10c0/dd809ac74c2154a549c9a9fbe73a3da84927c987d1291a1f1618f9263fa372a0de0d575c2616b3b420586a9974114c81ee47953c5d25074da66b58bf9d33d870 + languageName: node + linkType: hard + "@babel/runtime@npm:^7.25.0": version: 7.29.7 resolution: "@babel/runtime@npm:7.29.7" @@ -1797,6 +1835,21 @@ __metadata: languageName: node linkType: hard +"@babel/traverse@npm:^7.29.10": + version: 7.29.14 + resolution: "@babel/traverse@npm:7.29.14" + dependencies: + "@babel/code-frame": "npm:^7.29.7" + "@babel/generator": "npm:^7.29.14" + "@babel/helper-globals": "npm:^7.29.7" + "@babel/parser": "npm:^7.29.14" + "@babel/template": "npm:^7.29.7" + "@babel/types": "npm:^7.29.14" + debug: "npm:^4.3.1" + checksum: 10c0/f8d393411c7f03d1e5437620affa67857450b84501850414240a05c558894e59800f89310f810ec2711f7ed63fc03ef71d81eef9a3669f64d626c6d1be4c98f7 + languageName: node + linkType: hard + "@babel/types@npm:^7.0.0, @babel/types@npm:^7.20.7, @babel/types@npm:^7.21.3, @babel/types@npm:^7.26.0, @babel/types@npm:^7.27.1, @babel/types@npm:^7.28.2, @babel/types@npm:^7.29.0, @babel/types@npm:^7.29.7, @babel/types@npm:^7.29.8, @babel/types@npm:^7.3.3, @babel/types@npm:^7.4.4": version: 7.29.8 resolution: "@babel/types@npm:7.29.8" @@ -1807,6 +1860,16 @@ __metadata: languageName: node linkType: hard +"@babel/types@npm:^7.29.14": + version: 7.29.14 + resolution: "@babel/types@npm:7.29.14" + dependencies: + "@babel/helper-string-parser": "npm:^7.29.14" + "@babel/helper-validator-identifier": "npm:^7.29.7" + checksum: 10c0/40df9841cec707821b711b10c786be8ce52781e159be2ef8be41b546a6cf06021e99bd49b85fde1fd0313560df921335d44df5c442374c07785a4cdf6c2b3932 + languageName: node + linkType: hard + "@bcoe/v8-coverage@npm:^0.2.3": version: 0.2.3 resolution: "@bcoe/v8-coverage@npm:0.2.3" @@ -3731,6 +3794,22 @@ __metadata: languageName: node linkType: hard +"@expo/log-box@npm:^57.0.5": + version: 57.0.5 + resolution: "@expo/log-box@npm:57.0.5" + dependencies: + "@expo/dom-webview": "npm:^57.0.1" + anser: "npm:^1.4.9" + stacktrace-parser: "npm:^0.1.10" + peerDependencies: + "@expo/dom-webview": ^57.0.1 + expo: "*" + react: "*" + react-native: "*" + checksum: 10c0/14bc11e07e4d5e171de76cb499271f19abfa7dd3f8be6cdfbe36021352b28a7405e5dd26bee8c232bb036813b560eef50bbd263506d5c8b1f240448752a4e0af + languageName: node + linkType: hard + "@expo/material-symbols@npm:^0.1.1": version: 0.1.1 resolution: "@expo/material-symbols@npm:0.1.1" @@ -3789,6 +3868,28 @@ __metadata: languageName: node linkType: hard +"@expo/metro-runtime@npm:~57.0.18": + version: 57.0.18 + resolution: "@expo/metro-runtime@npm:57.0.18" + dependencies: + "@expo/log-box": "npm:^57.0.5" + anser: "npm:^1.4.9" + pretty-format: "npm:^29.7.0" + stacktrace-parser: "npm:^0.1.10" + whatwg-fetch: "npm:^3.0.0" + peerDependencies: + "@expo/log-box": ^57.0.5 + expo: "*" + react: "*" + react-dom: "*" + react-native: "*" + peerDependenciesMeta: + react-dom: + optional: true + checksum: 10c0/d9053c4f07b2a518b3ddf51d1b4cce24847cb51c75ce5605cd28a41e68983fafcf4f77755b533fb0bd0f84673db262a9694224c936bcd01820989c994d2af083 + languageName: node + linkType: hard + "@expo/metro@npm:~56.0.2": version: 56.0.2 resolution: "@expo/metro@npm:56.0.2" @@ -5709,6 +5810,13 @@ __metadata: languageName: node linkType: hard +"@react-native/asset-utils@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/asset-utils@npm:0.88.1" + checksum: 10c0/3189049a76b603ef0b13cbd65d6fafbc2e117643f81a2bebb9841254e48b78ab5c0f07b7110cd239bc232796c21aeaa3c9c5237e0c37ce0caa485ffe628d9b3c + languageName: node + linkType: hard + "@react-native/assets-registry@npm:0.86.3": version: 0.86.3 resolution: "@react-native/assets-registry@npm:0.86.3" @@ -5856,6 +5964,23 @@ __metadata: languageName: node linkType: hard +"@react-native/codegen@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/codegen@npm:0.88.1" + dependencies: + "@babel/core": "npm:^7.25.2" + "@babel/parser": "npm:^7.29.0" + flow-parser: "npm:0.327.0" + invariant: "npm:^2.2.4" + nullthrows: "npm:^1.1.1" + tinyglobby: "npm:^0.2.15" + yargs: "npm:^17.6.2" + peerDependencies: + "@babel/core": "*" + checksum: 10c0/33f3ef562dcfede10b39e3d68d5be0539314a2737421d385d334a18c96507ba00c430f1c99fad69a9e3c94c3e01c2f07b55fa0b95f8cf18623d32a7e984133cd + languageName: node + linkType: hard + "@react-native/community-cli-plugin@npm:0.86.3": version: 0.86.3 resolution: "@react-native/community-cli-plugin@npm:0.86.3" @@ -5901,6 +6026,29 @@ __metadata: languageName: node linkType: hard +"@react-native/community-cli-plugin@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/community-cli-plugin@npm:0.88.1" + dependencies: + "@react-native/asset-utils": "npm:0.88.1" + "@react-native/dev-middleware": "npm:0.88.1" + commander: "npm:^12.0.0" + debug: "npm:^4.4.0" + invariant: "npm:^2.2.4" + metro: "npm:^0.87.1" + semver: "npm:^7.1.3" + peerDependencies: + "@react-native-community/cli": "*" + "@react-native/metro-config": 0.88.1 + peerDependenciesMeta: + "@react-native-community/cli": + optional: true + "@react-native/metro-config": + optional: true + checksum: 10c0/aeee1cd978bdf4f60994888465ca6af8b94d6cb706a06b1fa2dad1b5e11f62e70c8042afe1beb246c7fb5bff684aa951606ad45809faf7a18a0e3e6a81a005b8 + languageName: node + linkType: hard + "@react-native/debugger-frontend@npm:0.86.3": version: 0.86.3 resolution: "@react-native/debugger-frontend@npm:0.86.3" @@ -5915,6 +6063,13 @@ __metadata: languageName: node linkType: hard +"@react-native/debugger-frontend@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/debugger-frontend@npm:0.88.1" + checksum: 10c0/b39d57e9f6aee083bf8b3d19fc9c22986767ba76d3d813eaf093a5e8b50a0a385d72aadd50de1c03754a5fdfbcaf4d12349f77ccd01fd18713321d49a3ddc04c + languageName: node + linkType: hard + "@react-native/debugger-shell@npm:0.86.3": version: 0.86.3 resolution: "@react-native/debugger-shell@npm:0.86.3" @@ -5937,6 +6092,17 @@ __metadata: languageName: node linkType: hard +"@react-native/debugger-shell@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/debugger-shell@npm:0.88.1" + dependencies: + cross-spawn: "npm:^7.0.6" + debug: "npm:^4.4.0" + fb-dotslash: "npm:0.5.8" + checksum: 10c0/98aeca1f2f2edf86c47f112b364c77dab00314cc77262ba1793ad85de8dd76ee5413a66320e033230e203a25ae864fa1fa18475e7577a4bbe25f7d4b7e1eef5d + languageName: node + linkType: hard + "@react-native/dev-middleware@npm:0.86.3": version: 0.86.3 resolution: "@react-native/dev-middleware@npm:0.86.3" @@ -5977,6 +6143,24 @@ __metadata: languageName: node linkType: hard +"@react-native/dev-middleware@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/dev-middleware@npm:0.88.1" + dependencies: + "@isaacs/ttlcache": "npm:^1.4.1" + "@react-native/debugger-frontend": "npm:0.88.1" + "@react-native/debugger-shell": "npm:0.88.1" + connect: "npm:^3.6.5" + debug: "npm:^4.4.0" + invariant: "npm:^2.2.4" + nullthrows: "npm:^1.1.1" + open: "npm:^8.4.2" + serve-static: "npm:^1.16.2" + ws: "npm:^7.5.10" + checksum: 10c0/740aecce1af5da2544495d317206dbf2826c5c5ae440df61f8105c972334e885defdff6defefe0274a3a8294bfe8666c6b14f4c2915b1856c6357ce11c880d86 + languageName: node + linkType: hard + "@react-native/gradle-plugin@npm:0.86.3": version: 0.86.3 resolution: "@react-native/gradle-plugin@npm:0.86.3" @@ -5991,6 +6175,13 @@ __metadata: languageName: node linkType: hard +"@react-native/gradle-plugin@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/gradle-plugin@npm:0.88.1" + checksum: 10c0/b78162926d85c4261338c1f00f0f423d949b619f61eaada6be20d07297e675c7923edeb1234df23be8b0cb3d334fd08e24af4a10f122df64e9d6ad2e5c4ed8fa + languageName: node + linkType: hard + "@react-native/jest-preset@npm:0.86.3": version: 0.86.3 resolution: "@react-native/jest-preset@npm:0.86.3" @@ -6075,6 +6266,20 @@ __metadata: languageName: node linkType: hard +"@react-native/normalize-colors@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/normalize-colors@npm:0.88.1" + checksum: 10c0/3db7e7b7ac754f648759cced1cccfb0e2526ed5088495312da7d2b797e1bb1b9df351fd583e4c6bf09fd5c99a757335bc503736673962df173fe9afeab6c1205 + languageName: node + linkType: hard + +"@react-native/normalize-colors@npm:^0.74.1": + version: 0.74.89 + resolution: "@react-native/normalize-colors@npm:0.74.89" + checksum: 10c0/6d0e5c91793ca5a66b4a0e5995361f474caacac56bde4772ac02b8ab470bd323076c567bd8856b0b097816d2b890e73a4040a3df01fd284adee683f5ba89d5ba + languageName: node + linkType: hard + "@react-native/typescript-config@npm:0.86.3": version: 0.86.3 resolution: "@react-native/typescript-config@npm:0.86.3" @@ -6116,6 +6321,23 @@ __metadata: languageName: node linkType: hard +"@react-native/virtualized-lists@npm:0.88.1": + version: 0.88.1 + resolution: "@react-native/virtualized-lists@npm:0.88.1" + dependencies: + invariant: "npm:^2.2.4" + nullthrows: "npm:^1.1.1" + peerDependencies: + "@types/react": ^19.2.0 + react: "*" + react-native: 0.88.1 + peerDependenciesMeta: + "@types/react": + optional: true + checksum: 10c0/663f3798bb619677549e047703d102e28244b569fedbcf493fbf4f056f5a06093cbf12470873593f7c436357c11660d41eed55638fbac404e2438b4b2e06fa13 + languageName: node + linkType: hard + "@release-it/conventional-changelog@npm:^12.0.0": version: 12.0.2 resolution: "@release-it/conventional-changelog@npm:12.0.2" @@ -6830,6 +7052,16 @@ __metadata: languageName: node linkType: hard +"@types/react-native-web@npm:^0": + version: 0.19.3 + resolution: "@types/react-native-web@npm:0.19.3" + dependencies: + "@types/react": "npm:*" + react-native: "npm:*" + checksum: 10c0/9fad06809257c5dcec722c30bfa635ae58096b444046faa045ad70315916bfc94fbeb478dfa78cb2ea3d1179d4e60de79cd7146c8effdacf11127546926fd68e + languageName: node + linkType: hard + "@types/react-reconciler@npm:~0.33.0": version: 0.33.1 resolution: "@types/react-reconciler@npm:0.33.1" @@ -7575,7 +7807,7 @@ __metadata: languageName: node linkType: hard -"asap@npm:~2.0.6": +"asap@npm:~2.0.3, asap@npm:~2.0.6": version: 2.0.6 resolution: "asap@npm:2.0.6" checksum: 10c0/c6d5e39fe1f15e4b87677460bd66b66050cd14c772269cee6688824c1410a08ab20254bb6784f9afb75af9144a9f9a7692d49547f4d19d715aeb7c0318f3136d @@ -9269,6 +9501,15 @@ __metadata: languageName: node linkType: hard +"cross-fetch@npm:^3.1.5": + version: 3.2.0 + resolution: "cross-fetch@npm:3.2.0" + dependencies: + node-fetch: "npm:^2.7.0" + checksum: 10c0/d8596adf0269130098a676f6739a0922f3cc7b71cc89729925411ebe851a87026171c82ea89154c4811c9867c01c44793205a52e618ce2684650218c7fbeeb9f + languageName: node + linkType: hard + "cross-spawn@npm:^7.0.3, cross-spawn@npm:^7.0.6": version: 7.0.6 resolution: "cross-spawn@npm:7.0.6" @@ -9322,6 +9563,15 @@ __metadata: languageName: node linkType: hard +"css-in-js-utils@npm:^3.1.0": + version: 3.1.0 + resolution: "css-in-js-utils@npm:3.1.0" + dependencies: + hyphenate-style-name: "npm:^1.0.3" + checksum: 10c0/8bb042e8f7701a7edadc3cce5ce2d5cf41189631d7e2aed194d5a7059b25776dded2a0466cb9da1d1f3fc6c99dcecb51e45671148d073b8a2a71e34755152e52 + languageName: node + linkType: hard + "css-loader@npm:^6.11.0": version: 6.11.0 resolution: "css-loader@npm:6.11.0" @@ -10851,6 +11101,28 @@ __metadata: languageName: node linkType: hard +"fbjs-css-vars@npm:^1.0.0": + version: 1.0.2 + resolution: "fbjs-css-vars@npm:1.0.2" + checksum: 10c0/dfb64116b125a64abecca9e31477b5edb9a2332c5ffe74326fe36e0a72eef7fc8a49b86adf36c2c293078d79f4524f35e80f5e62546395f53fb7c9e69821f54f + languageName: node + linkType: hard + +"fbjs@npm:^3.0.4": + version: 3.0.5 + resolution: "fbjs@npm:3.0.5" + dependencies: + cross-fetch: "npm:^3.1.5" + fbjs-css-vars: "npm:^1.0.0" + loose-envify: "npm:^1.0.0" + object-assign: "npm:^4.1.0" + promise: "npm:^7.1.1" + setimmediate: "npm:^1.0.5" + ua-parser-js: "npm:^1.0.35" + checksum: 10c0/66d0a2fc9a774f9066e35ac2ac4bf1245931d27f3ac287c7d47e6aa1fc152b243c2109743eb8f65341e025621fb51a12038fadb9fd8fda2e3ddae04ebab06f91 + languageName: node + linkType: hard + "fd-package-json@npm:^2.0.0": version: 2.0.0 resolution: "fd-package-json@npm:2.0.0" @@ -10985,6 +11257,13 @@ __metadata: languageName: node linkType: hard +"flow-estree@npm:0.327.0": + version: 0.327.0 + resolution: "flow-estree@npm:0.327.0" + checksum: 10c0/a6e62d4855e6d1096a3f45efbeb566afa8183cf4bc2beee8bb61dc6e2ab63982014a2dccd88ca23f1f06ace1b7e5dc0d00fa366af5d1fc9ff81d469b9a3cf30e + languageName: node + linkType: hard + "flow-estree@npm:0.331.0": version: 0.331.0 resolution: "flow-estree@npm:0.331.0" @@ -10992,6 +11271,22 @@ __metadata: languageName: node linkType: hard +"flow-estree@npm:0.336.0": + version: 0.336.0 + resolution: "flow-estree@npm:0.336.0" + checksum: 10c0/34123f04431bc7d5f0e62cf1606224e1a9a4f2f78faaee21ff9a7ad8119a6bab71f434e609667a94f10af8784c9532396a94e4de8cfbafc9fd8732aa52368c7f + languageName: node + linkType: hard + +"flow-parser@npm:0.327.0": + version: 0.327.0 + resolution: "flow-parser@npm:0.327.0" + dependencies: + flow-estree: "npm:0.327.0" + checksum: 10c0/8b299cef3d9ffeb9c5a315b2fa40707426abed1d68eec3752c8aebbb9f6513ff613ae2208e176a6b235d3177027c237e265f8f31fcfbe2b1672820f86a7d6ce6 + languageName: node + linkType: hard + "flow-parser@npm:0.331.0": version: 0.331.0 resolution: "flow-parser@npm:0.331.0" @@ -11001,6 +11296,15 @@ __metadata: languageName: node linkType: hard +"flow-parser@npm:0.336.0": + version: 0.336.0 + resolution: "flow-parser@npm:0.336.0" + dependencies: + flow-estree: "npm:0.336.0" + checksum: 10c0/a6cadc33838dceaf9f3f7b1c36a59c3404d28a0050a08c2606eaaa854bfe7089436a6abc84fd87e1ef66efdf1484baf30ce046c78caee54c6b227a59fa95d7e1 + languageName: node + linkType: hard + "follow-redirects@npm:^1.0.0": version: 1.16.1 resolution: "follow-redirects@npm:1.16.1" @@ -11619,6 +11923,13 @@ __metadata: languageName: node linkType: hard +"hermes-compiler@npm:260318099.0.5": + version: 260318099.0.5 + resolution: "hermes-compiler@npm:260318099.0.5" + checksum: 10c0/0194e815661498cd384aa4de24dfb3754e856fe473782fa6861d9191c84fde926ca3f3a6921231418e810655ee9e231e6e701d3f3dca10aa1079162dd0623ab9 + languageName: node + linkType: hard + "hermes-estree@npm:0.34.0": version: 0.34.0 resolution: "hermes-estree@npm:0.34.0" @@ -11971,6 +12282,13 @@ __metadata: languageName: node linkType: hard +"hyphenate-style-name@npm:^1.0.3": + version: 1.1.0 + resolution: "hyphenate-style-name@npm:1.1.0" + checksum: 10c0/bfe88deac2414a41a0d08811e277c8c098f23993d6a1eb17f14a0f11b54c4d42865a63d3cfe1914668eefb9a188e2de58f38b55a179a238fd1fef606893e194f + languageName: node + linkType: hard + "iconv-lite@npm:0.6.3, iconv-lite@npm:^0.6.3": version: 0.6.3 resolution: "iconv-lite@npm:0.6.3" @@ -12132,6 +12450,15 @@ __metadata: languageName: node linkType: hard +"inline-style-prefixer@npm:^7.0.1": + version: 7.0.1 + resolution: "inline-style-prefixer@npm:7.0.1" + dependencies: + css-in-js-utils: "npm:^3.1.0" + checksum: 10c0/15da5a396b7f286b5b6742efe315218cd577bc96b43de08aeb76af7697d9f1ab3bfc66cf19fad2173957dd5d617a790240b9d51898bdcf4c2efb40d3f8bcb370 + languageName: node + linkType: hard + "invariant@npm:^2.2.4": version: 2.2.4 resolution: "invariant@npm:2.2.4" @@ -14096,6 +14423,13 @@ __metadata: languageName: node linkType: hard +"memoize-one@npm:^6.0.0": + version: 6.0.0 + resolution: "memoize-one@npm:6.0.0" + checksum: 10c0/45c88e064fd715166619af72e8cf8a7a17224d6edf61f7a8633d740ed8c8c0558a4373876c9b8ffc5518c2b65a960266adf403cc215cb1e90f7e262b58991f54 + languageName: node + linkType: hard + "meow@npm:^14.0.0": version: 14.1.0 resolution: "meow@npm:14.1.0" @@ -14170,6 +14504,19 @@ __metadata: languageName: node linkType: hard +"metro-babel-transformer@npm:0.87.3": + version: 0.87.3 + resolution: "metro-babel-transformer@npm:0.87.3" + dependencies: + "@babel/core": "npm:^7.25.2" + flow-enums-runtime: "npm:^0.0.6" + flow-parser: "npm:0.336.0" + metro-cache-key: "npm:0.87.3" + nullthrows: "npm:^1.1.1" + checksum: 10c0/35547dfa61559bdfdc121747cd41e12fc80346044f76906666818dee3f1a4af43da4d4540ef5abe71c73394a22b66e3d42ed274e084c00b07ed6547e357d70e5 + languageName: node + linkType: hard + "metro-cache-key@npm:0.84.5": version: 0.84.5 resolution: "metro-cache-key@npm:0.84.5" @@ -14197,6 +14544,15 @@ __metadata: languageName: node linkType: hard +"metro-cache-key@npm:0.87.3": + version: 0.87.3 + resolution: "metro-cache-key@npm:0.87.3" + dependencies: + flow-enums-runtime: "npm:^0.0.6" + checksum: 10c0/d5280102b0f4e14be7f8980831949d502eaa8acaaffa9bfbdd8ac8e27a78b1cdef7257d7403d61e79ea4c89dcf58346e9a50bac0aee6ae52d11d1091c4051dd9 + languageName: node + linkType: hard + "metro-cache@npm:0.84.5": version: 0.84.5 resolution: "metro-cache@npm:0.84.5" @@ -14233,6 +14589,18 @@ __metadata: languageName: node linkType: hard +"metro-cache@npm:0.87.3": + version: 0.87.3 + resolution: "metro-cache@npm:0.87.3" + dependencies: + exponential-backoff: "npm:^3.1.1" + flow-enums-runtime: "npm:^0.0.6" + https-proxy-agent: "npm:^7.0.5" + metro-core: "npm:0.87.3" + checksum: 10c0/b12fad6690f66b9e6c6885edeae41eb175e39eed70572123d2cff25a30646e118efad42c29e239a0296c908a3f911bd8b35bfd9cb6e13638aebb1c0f60f091b9 + languageName: node + linkType: hard + "metro-config@npm:0.84.5": version: 0.84.5 resolution: "metro-config@npm:0.84.5" @@ -14280,6 +14648,21 @@ __metadata: languageName: node linkType: hard +"metro-config@npm:0.87.3": + version: 0.87.3 + resolution: "metro-config@npm:0.87.3" + dependencies: + connect: "npm:^3.6.5" + flow-enums-runtime: "npm:^0.0.6" + jest-validate: "npm:^29.7.0" + metro: "npm:0.87.3" + metro-cache: "npm:0.87.3" + metro-core: "npm:0.87.3" + metro-runtime: "npm:0.87.3" + checksum: 10c0/d961c8ab7184590bf8539f34d27dc43017284bbb5c9fa9ba1d3facaaf687c93c16accbfa77c0d5b0f6f6403ec9c5944e05bc1ea65d3b8e7c06615572b77efbc4 + languageName: node + linkType: hard + "metro-core@npm:0.84.5": version: 0.84.5 resolution: "metro-core@npm:0.84.5" @@ -14313,6 +14696,17 @@ __metadata: languageName: node linkType: hard +"metro-core@npm:0.87.3": + version: 0.87.3 + resolution: "metro-core@npm:0.87.3" + dependencies: + flow-enums-runtime: "npm:^0.0.6" + lodash.throttle: "npm:^4.1.1" + metro-resolver: "npm:0.87.3" + checksum: 10c0/a4f28bed31ccccb552aeccf8d84708c5f25ed9fce42e8dc45d2e9ca08e713556f96b909405d5633c6b6482ff52205773cb58502b21f4a9fc2b0b1b840d2e1f90 + languageName: node + linkType: hard + "metro-file-map@npm:0.84.5": version: 0.84.5 resolution: "metro-file-map@npm:0.84.5" @@ -14363,6 +14757,20 @@ __metadata: languageName: node linkType: hard +"metro-file-map@npm:0.87.3": + version: 0.87.3 + resolution: "metro-file-map@npm:0.87.3" + dependencies: + debug: "npm:^4.4.0" + fb-watchman: "npm:^2.0.0" + flow-enums-runtime: "npm:^0.0.6" + invariant: "npm:^2.2.4" + jest-worker: "npm:^29.7.0" + nullthrows: "npm:^1.1.1" + checksum: 10c0/67bc903fc4a738afadb6f885c802a24c645e572f4a0bda7c251c0ece60b7d65543d0bdf4329e85767d2a27d97a2b5f3da5415f34529504f948821afed0183a4d + languageName: node + linkType: hard + "metro-minify-terser@npm:0.84.5": version: 0.84.5 resolution: "metro-minify-terser@npm:0.84.5" @@ -14393,6 +14801,16 @@ __metadata: languageName: node linkType: hard +"metro-minify-terser@npm:0.87.3": + version: 0.87.3 + resolution: "metro-minify-terser@npm:0.87.3" + dependencies: + flow-enums-runtime: "npm:^0.0.6" + terser: "npm:^5.15.0" + checksum: 10c0/6db6f728abff59b1f9fa48c1cca0b8c43f6e46c54020a1f33fdbe9e4db30ea2a6de1d7278135b8c432ab399340b12b9d4279fa921fd4063072d06b198445f532 + languageName: node + linkType: hard + "metro-resolver@npm:0.84.5": version: 0.84.5 resolution: "metro-resolver@npm:0.84.5" @@ -14420,6 +14838,15 @@ __metadata: languageName: node linkType: hard +"metro-resolver@npm:0.87.3": + version: 0.87.3 + resolution: "metro-resolver@npm:0.87.3" + dependencies: + flow-enums-runtime: "npm:^0.0.6" + checksum: 10c0/e10f6b26cd3840847d8a0a39028d6fa22c96bfb599e39f536d7902448164e313775e91b6c0d28d22e360c2cf00cf434ff0e1a8127673bb7603bd35719a1b5f01 + languageName: node + linkType: hard + "metro-runtime@npm:0.84.5": version: 0.84.5 resolution: "metro-runtime@npm:0.84.5" @@ -14450,6 +14877,16 @@ __metadata: languageName: node linkType: hard +"metro-runtime@npm:0.87.3, metro-runtime@npm:^0.87.1": + version: 0.87.3 + resolution: "metro-runtime@npm:0.87.3" + dependencies: + "@babel/runtime": "npm:^7.25.0" + flow-enums-runtime: "npm:^0.0.6" + checksum: 10c0/333c791a003d6bbb16e08f38310ae91bce4275cee85e43ba8f53c192adfad1d572716cf669ecc1bef6369b0d8ac964da14105a04c6419bd1b3e0d33ad7a20580 + languageName: node + linkType: hard + "metro-source-map@npm:0.84.5": version: 0.84.5 resolution: "metro-source-map@npm:0.84.5" @@ -14501,6 +14938,23 @@ __metadata: languageName: node linkType: hard +"metro-source-map@npm:0.87.3, metro-source-map@npm:^0.87.1": + version: 0.87.3 + resolution: "metro-source-map@npm:0.87.3" + dependencies: + "@babel/core": "npm:^7.25.2" + "@babel/traverse": "npm:^7.29.10" + "@babel/types": "npm:^7.29.8" + flow-enums-runtime: "npm:^0.0.6" + invariant: "npm:^2.2.4" + metro-symbolicate: "npm:0.87.3" + nullthrows: "npm:^1.1.1" + ob1: "npm:0.87.3" + source-map: "npm:^0.5.6" + checksum: 10c0/4a40c06a532b4eff7b40006771a03ec6e763284219f68318d7fb97e9d6c9b3c7390dec868730c19035f7e534357580a5d2764fd0ed96ca9a72482d3a49c1e496 + languageName: node + linkType: hard + "metro-symbolicate@npm:0.84.5": version: 0.84.5 resolution: "metro-symbolicate@npm:0.84.5" @@ -14549,6 +15003,22 @@ __metadata: languageName: node linkType: hard +"metro-symbolicate@npm:0.87.3": + version: 0.87.3 + resolution: "metro-symbolicate@npm:0.87.3" + dependencies: + flow-enums-runtime: "npm:^0.0.6" + invariant: "npm:^2.2.4" + metro-source-map: "npm:0.87.3" + nullthrows: "npm:^1.1.1" + source-map: "npm:^0.5.6" + vlq: "npm:^1.0.0" + bin: + metro-symbolicate: src/index.js + checksum: 10c0/e7df5308f25181fa06b000f7ac10af81ef1f7ef1ec8a2da2985da3e939941f7793d51f5051276b70faaf30066ae6ab1686df550b10a339f013555569c844b930 + languageName: node + linkType: hard + "metro-transform-plugins@npm:0.84.5": version: 0.84.5 resolution: "metro-transform-plugins@npm:0.84.5" @@ -14591,6 +15061,20 @@ __metadata: languageName: node linkType: hard +"metro-transform-plugins@npm:0.87.3": + version: 0.87.3 + resolution: "metro-transform-plugins@npm:0.87.3" + dependencies: + "@babel/core": "npm:^7.25.2" + "@babel/generator": "npm:^7.29.1" + "@babel/template": "npm:^7.28.6" + "@babel/traverse": "npm:^7.29.10" + flow-enums-runtime: "npm:^0.0.6" + nullthrows: "npm:^1.1.1" + checksum: 10c0/2e35f2464a0c6e565be42cb072f9bcd2fd3033b8a3eba4991afbe21f7a5e09024e28978087bacbf69d39f17c51439072a6401da8f28db3f1a9d2e21cb9a03a5b + languageName: node + linkType: hard + "metro-transform-worker@npm:0.84.5": version: 0.84.5 resolution: "metro-transform-worker@npm:0.84.5" @@ -14654,6 +15138,28 @@ __metadata: languageName: node linkType: hard +"metro-transform-worker@npm:0.87.3": + version: 0.87.3 + resolution: "metro-transform-worker@npm:0.87.3" + dependencies: + "@babel/core": "npm:^7.25.2" + "@babel/generator": "npm:^7.29.1" + "@babel/parser": "npm:^7.29.9" + "@babel/types": "npm:^7.29.8" + "@jridgewell/sourcemap-codec": "npm:^1.5.0" + flow-enums-runtime: "npm:^0.0.6" + metro: "npm:0.87.3" + metro-babel-transformer: "npm:0.87.3" + metro-cache: "npm:0.87.3" + metro-cache-key: "npm:0.87.3" + metro-minify-terser: "npm:0.87.3" + metro-source-map: "npm:0.87.3" + metro-transform-plugins: "npm:0.87.3" + nullthrows: "npm:^1.1.1" + checksum: 10c0/cc31650dd5af5e3a0542a84cdbe86cc2c3e03a63c6723d5c7031ad26fbdba030d7fb07bf3193fefb534aaae2a032eeab673a44d04a2195db86a12a05afe4a3e8 + languageName: node + linkType: hard + "metro@npm:0.84.5": version: 0.84.5 resolution: "metro@npm:0.84.5" @@ -14797,6 +15303,52 @@ __metadata: languageName: node linkType: hard +"metro@npm:0.87.3, metro@npm:^0.87.1": + version: 0.87.3 + resolution: "metro@npm:0.87.3" + dependencies: + "@babel/code-frame": "npm:^7.29.0" + "@babel/core": "npm:^7.25.2" + "@babel/generator": "npm:^7.29.1" + "@babel/parser": "npm:^7.29.9" + "@babel/template": "npm:^7.28.6" + "@babel/traverse": "npm:^7.29.10" + "@babel/types": "npm:^7.29.8" + accepts: "npm:^2.0.0" + connect: "npm:^3.6.5" + debug: "npm:^4.4.0" + error-stack-parser: "npm:^2.0.6" + flow-enums-runtime: "npm:^0.0.6" + flow-parser: "npm:0.336.0" + invariant: "npm:^2.2.4" + jest-worker: "npm:^29.7.0" + jsc-safe-url: "npm:^0.2.2" + lodash.throttle: "npm:^4.1.1" + metro-babel-transformer: "npm:0.87.3" + metro-cache: "npm:0.87.3" + metro-cache-key: "npm:0.87.3" + metro-config: "npm:0.87.3" + metro-core: "npm:0.87.3" + metro-file-map: "npm:0.87.3" + metro-resolver: "npm:0.87.3" + metro-runtime: "npm:0.87.3" + metro-source-map: "npm:0.87.3" + metro-symbolicate: "npm:0.87.3" + metro-transform-plugins: "npm:0.87.3" + metro-transform-worker: "npm:0.87.3" + mime-types: "npm:^3.0.1" + nullthrows: "npm:^1.1.1" + serialize-error: "npm:^2.1.0" + source-map: "npm:^0.5.6" + throat: "npm:^5.0.0" + ws: "npm:^7.5.10" + yargs: "npm:^17.6.2" + bin: + metro: src/cli.js + checksum: 10c0/f4768cecface5e40ee410603d0fcfe444560342b32e94443e89dd9eeb2fd90df6792774a387e698e081d8e78f5aca57752a0b514efe0d862004b8429664c6515 + languageName: node + linkType: hard + "micromark-core-commonmark@npm:^2.0.0": version: 2.0.4 resolution: "micromark-core-commonmark@npm:2.0.4" @@ -15679,6 +16231,20 @@ __metadata: languageName: node linkType: hard +"node-fetch@npm:^2.7.0": + version: 2.7.0 + resolution: "node-fetch@npm:2.7.0" + dependencies: + whatwg-url: "npm:^5.0.0" + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + checksum: 10c0/b55786b6028208e6fbe594ccccc213cab67a72899c9234eb59dba51062a299ea853210fcf526998eaa2867b0963ad72338824450905679ff0fa304b8c5093ae8 + languageName: node + linkType: hard + "node-forge@npm:^1.3.3": version: 1.4.0 resolution: "node-forge@npm:1.4.0" @@ -15828,7 +16394,16 @@ __metadata: languageName: node linkType: hard -"object-assign@npm:^4.1.1": +"ob1@npm:0.87.3": + version: 0.87.3 + resolution: "ob1@npm:0.87.3" + dependencies: + flow-enums-runtime: "npm:^0.0.6" + checksum: 10c0/cdb2680c2c1397a20b9d68be3522b2f00faf6e4105fce75ad2849c43dee926021f8214e68aa44cb1906ddde9ef27203b46a6967e3b8fa66a95c608907fba5bb1 + languageName: node + linkType: hard + +"object-assign@npm:^4.1.0, object-assign@npm:^4.1.1": version: 4.1.1 resolution: "object-assign@npm:4.1.1" checksum: 10c0/1f4df9945120325d041ccf7b86f31e8bcc14e73d29171e37a7903050e96b81323784ec59f93f102ec635bcf6fa8034ba3ea0a8c7e69fa202b87ae3b6cec5a414 @@ -15974,7 +16549,7 @@ __metadata: languageName: node linkType: hard -"open@npm:^8.4.0": +"open@npm:^8.4.0, open@npm:^8.4.2": version: 8.4.2 resolution: "open@npm:8.4.2" dependencies: @@ -16493,6 +17068,26 @@ __metadata: languageName: node linkType: hard +"playwright-core@npm:1.64.1": + version: 1.64.1 + resolution: "playwright-core@npm:1.64.1" + bin: + playwright-core: cli.js + checksum: 10c0/91c4f5ed1cff4b0e18b6ac62f66770f5f1b6c204d4aebd6a321c07466dca18a70822ac692597c6059705a703f9768097ac551df179fb49294d639509148e04f3 + languageName: node + linkType: hard + +"playwright@npm:1.64.1": + version: 1.64.1 + resolution: "playwright@npm:1.64.1" + dependencies: + playwright-core: "npm:1.64.1" + bin: + playwright: cli.js + checksum: 10c0/edf52d0a79feca37bf383e841a467aa68750684128218eac560063991c9e4b615a94901f580cd8a088bb05fd53008ab4a0e5e8d976067698b60ae5d1de2459f3 + languageName: node + linkType: hard + "plist@npm:^3.0.5": version: 3.1.1 resolution: "plist@npm:3.1.1" @@ -17437,6 +18032,15 @@ __metadata: languageName: node linkType: hard +"promise@npm:^7.1.1": + version: 7.3.1 + resolution: "promise@npm:7.3.1" + dependencies: + asap: "npm:~2.0.3" + checksum: 10c0/742e5c0cc646af1f0746963b8776299701ad561ce2c70b49365d62c8db8ea3681b0a1bf0d4e2fe07910bf72f02d39e51e8e73dc8d7503c3501206ac908be107f + languageName: node + linkType: hard + "promise@npm:^8.3.0": version: 8.3.0 resolution: "promise@npm:8.3.0" @@ -17805,18 +18409,22 @@ __metadata: "@babel/runtime": "npm:^7.25.0" "@expo-google-fonts/jetbrains-mono": "npm:^0.4.1" "@expo/material-symbols": "npm:^0.1.1" + "@expo/metro-runtime": "npm:~57.0.18" "@expo/ui": "npm:~57.0.25" "@react-native/babel-preset": "npm:0.86.3" "@react-native/jest-preset": "npm:0.86.3" "@react-native/metro-config": "npm:0.86.3" "@react-native/typescript-config": "npm:0.86.3" "@types/react": "npm:~19.2.4" + "@types/react-dom": "npm:^19.2.0" + "@types/react-native-web": "npm:^0" babel-preset-expo: "npm:~57.0.15" expo: "npm:~57.0.30" expo-font: "npm:~57.0.4" expo-linear-gradient: "npm:~57.0.2" expo-status-bar: "npm:~57.0.1" react: "npm:19.2.3" + react-dom: "npm:19.2.3" react-native: "npm:0.86.3" react-native-builder-bob: "npm:^0.43.1" react-native-codepad-lang-c: "npm:*" @@ -17843,6 +18451,7 @@ __metadata: react-native-nitro-modules: "npm:^0.37.1" react-native-reanimated: "npm:^4.7.1" react-native-safe-area-context: "npm:~5.7.0" + react-native-web: "npm:^0.21.2" react-native-worklets: "npm:^0.13.0" languageName: unknown linkType: soft @@ -18038,6 +18647,7 @@ __metadata: jest: "npm:^29.7.0" lefthook: "npm:^2.1.10" nitrogen: "npm:0.37.1" + playwright: "npm:1.64.1" react: "npm:19.2.3" react-native: "npm:0.87.1" react-native-builder-bob: "npm:^0.43.1" @@ -18122,6 +18732,25 @@ __metadata: languageName: node linkType: hard +"react-native-web@npm:^0.21.2": + version: 0.21.4 + resolution: "react-native-web@npm:0.21.4" + dependencies: + "@babel/runtime": "npm:^7.18.6" + "@react-native/normalize-colors": "npm:^0.74.1" + fbjs: "npm:^3.0.4" + inline-style-prefixer: "npm:^7.0.1" + memoize-one: "npm:^6.0.0" + nullthrows: "npm:^1.1.1" + postcss-value-parser: "npm:^4.2.0" + styleq: "npm:^0.1.3" + peerDependencies: + react: ^18.0.0 || ^19.0.0 + react-dom: ^18.0.0 || ^19.0.0 + checksum: 10c0/4eee467365d8081b04c9c4cac8ab296cb19a1f06ac0ed30ad3d08172f42557f8dbd7eec9ea651ebfe9f5ddbd1e3326e45d9235e91d82f48e70b07822eb9c3873 + languageName: node + linkType: hard + "react-native-worklets@npm:^0.13.0": version: 0.13.0 resolution: "react-native-worklets@npm:0.13.0" @@ -18151,6 +18780,51 @@ __metadata: languageName: node linkType: hard +"react-native@npm:*": + version: 0.88.1 + resolution: "react-native@npm:0.88.1" + dependencies: + "@react-native/asset-utils": "npm:0.88.1" + "@react-native/codegen": "npm:0.88.1" + "@react-native/community-cli-plugin": "npm:0.88.1" + "@react-native/gradle-plugin": "npm:0.88.1" + "@react-native/normalize-colors": "npm:0.88.1" + "@react-native/virtualized-lists": "npm:0.88.1" + anser: "npm:^1.4.9" + ansi-regex: "npm:^5.0.0" + base64-js: "npm:^1.5.1" + flow-enums-runtime: "npm:^0.0.6" + flow-parser: "npm:0.327.0" + hermes-compiler: "npm:260318099.0.5" + invariant: "npm:^2.2.4" + memoize-one: "npm:^5.0.0" + metro-runtime: "npm:^0.87.1" + metro-source-map: "npm:^0.87.1" + nullthrows: "npm:^1.1.1" + pretty-format: "npm:^29.7.0" + promise: "npm:^8.3.0" + react-devtools-core: "npm:^6.1.5" + react-refresh: "npm:^0.14.0" + regenerator-runtime: "npm:^0.13.2" + scheduler: "npm:0.28.0" + semver: "npm:^7.1.3" + stacktrace-parser: "npm:^0.1.10" + tinyglobby: "npm:^0.2.15" + whatwg-fetch: "npm:^3.0.0" + ws: "npm:^7.5.10" + yargs: "npm:^17.6.2" + peerDependencies: + "@types/react": ^19.1.1 + react: ^19.3.0 + peerDependenciesMeta: + "@types/react": + optional: true + bin: + react-native: cli.js + checksum: 10c0/c000040ac4b1b2dc736f40811ef4d11c88986296147e7722ed0f817540c0cb950a322e8766f9bc997470ef01e6bc46e90ff58ed67255fe08e1d8dee7da9ecfb3 + languageName: node + linkType: hard + "react-native@npm:0.86.3": version: 0.86.3 resolution: "react-native@npm:0.86.3" @@ -18949,6 +19623,13 @@ __metadata: languageName: node linkType: hard +"scheduler@npm:0.28.0": + version: 0.28.0 + resolution: "scheduler@npm:0.28.0" + checksum: 10c0/783c8e5ce89d41273a02ed6dc0e3d24bd60e05c9e8f7e3adfc6c82e7c69935500d90e343f1ee26d592fc7504e8a2532573c6368def3e93bfe812391fa53efaf4 + languageName: node + linkType: hard + "schema-dts@npm:^1.1.2": version: 1.1.5 resolution: "schema-dts@npm:1.1.5" @@ -19126,6 +19807,13 @@ __metadata: languageName: node linkType: hard +"setimmediate@npm:^1.0.5": + version: 1.0.5 + resolution: "setimmediate@npm:1.0.5" + checksum: 10c0/5bae81bfdbfbd0ce992893286d49c9693c82b1bcc00dcaaf3a09c8f428fdeacf4190c013598b81875dfac2b08a572422db7df779a99332d0fce186d15a3e4d49 + languageName: node + linkType: hard + "setprototypeof@npm:1.2.0, setprototypeof@npm:~1.2.0": version: 1.2.0 resolution: "setprototypeof@npm:1.2.0" @@ -19733,6 +20421,13 @@ __metadata: languageName: node linkType: hard +"styleq@npm:^0.1.3": + version: 0.1.3 + resolution: "styleq@npm:0.1.3" + checksum: 10c0/975d951792e65052f1f6e41aaad46492642ce4922b3dc36d4b49b37c8509f9a776794d8f275360f00116a5e6ab1e31514bdcd5840656c4e3213da6803fa12941 + languageName: node + linkType: hard + "supports-color@npm:^5.3.0": version: 5.5.0 resolution: "supports-color@npm:5.5.0" @@ -20005,6 +20700,13 @@ __metadata: languageName: node linkType: hard +"tr46@npm:~0.0.3": + version: 0.0.3 + resolution: "tr46@npm:0.0.3" + checksum: 10c0/047cb209a6b60c742f05c9d3ace8fa510bff609995c129a37ace03476a9b12db4dbf975e74600830ef0796e18882b2381fb5fb1f6b4f96b832c374de3ab91a11 + languageName: node + linkType: hard + "tree-dump@npm:^1.0.3, tree-dump@npm:^1.1.0": version: 1.1.0 resolution: "tree-dump@npm:1.1.0" @@ -20171,6 +20873,15 @@ __metadata: languageName: node linkType: hard +"ua-parser-js@npm:^1.0.35": + version: 1.0.41 + resolution: "ua-parser-js@npm:1.0.41" + bin: + ua-parser-js: script/cli.js + checksum: 10c0/45dc1f7f3ce8248e0e64640d2e29c65c0ea1fc9cb105594de84af80e2a57bba4f718b9376098ca7a5b0ffe240f8995b0fa3714afa9d36861c41370a378f1a274 + languageName: node + linkType: hard + "undici-types@npm:>=7.24.0 <7.24.7": version: 7.24.6 resolution: "undici-types@npm:7.24.6" @@ -20637,6 +21348,13 @@ __metadata: languageName: node linkType: hard +"webidl-conversions@npm:^3.0.0": + version: 3.0.1 + resolution: "webidl-conversions@npm:3.0.1" + checksum: 10c0/5612d5f3e54760a797052eb4927f0ddc01383550f542ccd33d5238cfd65aeed392a45ad38364970d0a0f4fea32e1f4d231b3d8dac4a3bdd385e5cf802ae097db + languageName: node + linkType: hard + "webpack-bundle-analyzer@npm:^4.10.2": version: 4.10.2 resolution: "webpack-bundle-analyzer@npm:4.10.2" @@ -20850,6 +21568,16 @@ __metadata: languageName: node linkType: hard +"whatwg-url@npm:^5.0.0": + version: 5.0.0 + resolution: "whatwg-url@npm:5.0.0" + dependencies: + tr46: "npm:~0.0.3" + webidl-conversions: "npm:^3.0.0" + checksum: 10c0/1588bed84d10b72d5eec1d0faa0722ba1962f1821e7539c535558fb5398d223b0c50d8acab950b8c488b4ba69043fd833cc2697056b167d8ad46fac3995a55d5 + languageName: node + linkType: hard + "which@npm:^2.0.1": version: 2.0.2 resolution: "which@npm:2.0.2"