@tishlang/tish 1.0.7 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +43 -0
- package/LICENSE +13 -0
- package/README.md +66 -0
- package/crates/js_to_tish/Cargo.toml +9 -0
- package/crates/js_to_tish/README.md +18 -0
- package/crates/js_to_tish/src/error.rs +61 -0
- package/crates/js_to_tish/src/lib.rs +11 -0
- package/crates/js_to_tish/src/span_util.rs +35 -0
- package/crates/js_to_tish/src/transform/expr.rs +608 -0
- package/crates/js_to_tish/src/transform/stmt.rs +474 -0
- package/crates/js_to_tish/src/transform.rs +60 -0
- package/crates/tish/Cargo.toml +44 -0
- package/crates/tish/src/main.rs +585 -0
- package/crates/tish/src/repl_completion.rs +200 -0
- package/crates/tish/tests/integration_test.rs +726 -0
- package/crates/tish_ast/Cargo.toml +7 -0
- package/crates/tish_ast/src/ast.rs +494 -0
- package/crates/tish_ast/src/lib.rs +5 -0
- package/crates/tish_build_utils/Cargo.toml +5 -0
- package/crates/tish_build_utils/src/lib.rs +175 -0
- package/crates/tish_builtins/Cargo.toml +12 -0
- package/crates/tish_builtins/src/array.rs +410 -0
- package/crates/tish_builtins/src/globals.rs +197 -0
- package/crates/tish_builtins/src/helpers.rs +38 -0
- package/crates/tish_builtins/src/lib.rs +14 -0
- package/crates/tish_builtins/src/math.rs +80 -0
- package/crates/tish_builtins/src/object.rs +36 -0
- package/crates/tish_builtins/src/string.rs +253 -0
- package/crates/tish_bytecode/Cargo.toml +15 -0
- package/crates/tish_bytecode/src/chunk.rs +97 -0
- package/crates/tish_bytecode/src/compiler.rs +1361 -0
- package/crates/tish_bytecode/src/encoding.rs +100 -0
- package/crates/tish_bytecode/src/lib.rs +19 -0
- package/crates/tish_bytecode/src/opcode.rs +110 -0
- package/crates/tish_bytecode/src/peephole.rs +159 -0
- package/crates/tish_bytecode/src/serialize.rs +163 -0
- package/crates/tish_bytecode/tests/constant_folding.rs +84 -0
- package/crates/tish_bytecode/tests/shortcircuit.rs +49 -0
- package/crates/tish_bytecode/tests/sort_optimization.rs +31 -0
- package/crates/tish_compile/Cargo.toml +21 -0
- package/crates/tish_compile/src/codegen.rs +3316 -0
- package/crates/tish_compile/src/lib.rs +71 -0
- package/crates/tish_compile/src/resolve.rs +631 -0
- package/crates/tish_compile/src/types.rs +304 -0
- package/crates/tish_compile_js/Cargo.toml +16 -0
- package/crates/tish_compile_js/examples/jsx_vdom_smoke.tish +8 -0
- package/crates/tish_compile_js/src/codegen.rs +794 -0
- package/crates/tish_compile_js/src/error.rs +20 -0
- package/crates/tish_compile_js/src/js_intrinsics.rs +82 -0
- package/crates/tish_compile_js/src/lib.rs +27 -0
- package/crates/tish_compile_js/src/tests_jsx.rs +32 -0
- package/crates/tish_compiler_wasm/Cargo.toml +19 -0
- package/crates/tish_compiler_wasm/src/lib.rs +55 -0
- package/crates/tish_compiler_wasm/src/resolve_virtual.rs +462 -0
- package/crates/tish_core/Cargo.toml +11 -0
- package/crates/tish_core/src/console_style.rs +128 -0
- package/crates/tish_core/src/json.rs +327 -0
- package/crates/tish_core/src/lib.rs +15 -0
- package/crates/tish_core/src/macros.rs +37 -0
- package/crates/tish_core/src/uri.rs +115 -0
- package/crates/tish_core/src/value.rs +376 -0
- package/crates/tish_cranelift/Cargo.toml +17 -0
- package/crates/tish_cranelift/src/lib.rs +41 -0
- package/crates/tish_cranelift/src/link.rs +120 -0
- package/crates/tish_cranelift/src/lower.rs +77 -0
- package/crates/tish_cranelift_runtime/Cargo.toml +19 -0
- package/crates/tish_cranelift_runtime/src/lib.rs +43 -0
- package/crates/tish_eval/Cargo.toml +26 -0
- package/crates/tish_eval/src/eval.rs +3205 -0
- package/crates/tish_eval/src/http.rs +122 -0
- package/crates/tish_eval/src/lib.rs +59 -0
- package/crates/tish_eval/src/natives.rs +301 -0
- package/crates/tish_eval/src/promise.rs +173 -0
- package/crates/tish_eval/src/regex.rs +298 -0
- package/crates/tish_eval/src/timers.rs +111 -0
- package/crates/tish_eval/src/value.rs +224 -0
- package/crates/tish_eval/src/value_convert.rs +85 -0
- package/crates/tish_fmt/Cargo.toml +16 -0
- package/crates/tish_fmt/src/bin/tish-fmt.rs +41 -0
- package/crates/tish_fmt/src/lib.rs +884 -0
- package/crates/tish_jsx_web/Cargo.toml +7 -0
- package/crates/tish_jsx_web/README.md +18 -0
- package/crates/tish_jsx_web/src/lib.rs +157 -0
- package/crates/tish_jsx_web/vendor/Lattish.tish +347 -0
- package/crates/tish_lexer/Cargo.toml +7 -0
- package/crates/tish_lexer/src/lib.rs +430 -0
- package/crates/tish_lexer/src/token.rs +155 -0
- package/crates/tish_lint/Cargo.toml +17 -0
- package/crates/tish_lint/src/bin/tish-lint.rs +77 -0
- package/crates/tish_lint/src/lib.rs +278 -0
- package/crates/tish_llvm/Cargo.toml +11 -0
- package/crates/tish_llvm/src/lib.rs +106 -0
- package/crates/tish_lsp/Cargo.toml +22 -0
- package/crates/tish_lsp/README.md +26 -0
- package/crates/tish_lsp/src/main.rs +615 -0
- package/crates/tish_native/Cargo.toml +14 -0
- package/crates/tish_native/src/build.rs +102 -0
- package/crates/tish_native/src/lib.rs +237 -0
- package/crates/tish_opt/Cargo.toml +11 -0
- package/crates/tish_opt/src/lib.rs +896 -0
- package/crates/tish_parser/Cargo.toml +9 -0
- package/crates/tish_parser/src/lib.rs +123 -0
- package/crates/tish_parser/src/parser.rs +1714 -0
- package/crates/tish_runtime/Cargo.toml +26 -0
- package/crates/tish_runtime/src/http.rs +308 -0
- package/crates/tish_runtime/src/http_fetch.rs +453 -0
- package/crates/tish_runtime/src/lib.rs +1004 -0
- package/crates/tish_runtime/src/native_promise.rs +26 -0
- package/crates/tish_runtime/src/promise.rs +77 -0
- package/crates/tish_runtime/src/promise_io.rs +41 -0
- package/crates/tish_runtime/src/timers.rs +125 -0
- package/crates/tish_runtime/src/ws.rs +725 -0
- package/crates/tish_runtime/tests/fetch_readable_stream.rs +99 -0
- package/crates/tish_vm/Cargo.toml +31 -0
- package/crates/tish_vm/src/lib.rs +39 -0
- package/crates/tish_vm/src/vm.rs +1399 -0
- package/crates/tish_wasm/Cargo.toml +13 -0
- package/crates/tish_wasm/src/lib.rs +358 -0
- package/crates/tish_wasm_runtime/Cargo.toml +25 -0
- package/crates/tish_wasm_runtime/src/lib.rs +36 -0
- package/justfile +260 -0
- package/package.json +8 -3
- package/platform/darwin-arm64/tish +0 -0
- package/platform/darwin-x64/tish +0 -0
- package/platform/linux-arm64/tish +0 -0
- package/platform/linux-x64/tish +0 -0
- package/platform/win32-x64/tish.exe +0 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# tish_jsx_web
|
|
2
|
+
|
|
3
|
+
Web-only crate for the Tish JS backend:
|
|
4
|
+
|
|
5
|
+
- **`VDOM_PRELUDE`** — vnode helpers + `window.__lattishVdomPatch` + `window.__LATTISH_JSX_VDOM` for `--jsx vdom`.
|
|
6
|
+
|
|
7
|
+
Native / non-JS compiler targets must not depend on this crate; only `tish_compile_js` pulls it in.
|
|
8
|
+
|
|
9
|
+
## Vendor runtime
|
|
10
|
+
|
|
11
|
+
`vendor/Lattish.tish` is a copy refreshed from the **lattish** npm package. Run `just refresh-lattish` to update from the sibling lattish package.
|
|
12
|
+
|
|
13
|
+
## JSX modes (`--jsx`)
|
|
14
|
+
|
|
15
|
+
| Mode | JSX lowers to | Preamble |
|
|
16
|
+
|------|----------------|----------|
|
|
17
|
+
| `lattish` (default) | Lattish-style JSX lowering | none — merge `Lattish.tish` by importing any export you need (or `import {} from "lattish"` for JSX-only) |
|
|
18
|
+
| `vdom` | `__vdom_h(...)` | VDOM prelude; Lattish `createRoot` patches the tree when `window.__LATTISH_JSX_VDOM` is set |
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
//! Web-only JSX helpers for `tish compile --target js`.
|
|
2
|
+
//! Native and WASM native paths must not depend on this crate.
|
|
3
|
+
|
|
4
|
+
/// VDOM: vnode `__vdom_h` + `window.__lattishVdomPatch` for Lattish batched flush.
|
|
5
|
+
pub const VDOM_PRELUDE: &str = r#"window.__LATTISH_JSX_VDOM = true;
|
|
6
|
+
const __Fragment = Symbol('Fragment');
|
|
7
|
+
function __vdom_h(tag, props, children) {
|
|
8
|
+
if (children === undefined || children === null) children = [];
|
|
9
|
+
if (!Array.isArray(children)) children = [children];
|
|
10
|
+
return { tag: tag, props: props || null, children: children, _el: null };
|
|
11
|
+
}
|
|
12
|
+
function __vdom_flatten(ch) {
|
|
13
|
+
const out = [];
|
|
14
|
+
function w(c) {
|
|
15
|
+
if (c == null) return;
|
|
16
|
+
if (Array.isArray(c)) { for (let i = 0; i < c.length; i++) w(c[i]); return; }
|
|
17
|
+
if (typeof c === 'object' && c && c.tag === __Fragment) {
|
|
18
|
+
const inner = c.children;
|
|
19
|
+
if (Array.isArray(inner)) for (let i = 0; i < inner.length; i++) w(inner[i]);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
out.push(c);
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(ch)) for (let i = 0; i < ch.length; i++) w(ch[i]);
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
function __vdom_mount(v) {
|
|
28
|
+
if (typeof v === 'string') return document.createTextNode(v);
|
|
29
|
+
if (v.tag === __Fragment) {
|
|
30
|
+
const f = document.createDocumentFragment();
|
|
31
|
+
const ch = __vdom_flatten(v.children);
|
|
32
|
+
for (let i = 0; i < ch.length; i++) f.appendChild(__vdom_mount(ch[i]));
|
|
33
|
+
return f;
|
|
34
|
+
}
|
|
35
|
+
const el = document.createElement(v.tag);
|
|
36
|
+
const p = v.props || {};
|
|
37
|
+
for (const k of Object.keys(p)) {
|
|
38
|
+
const val = p[k];
|
|
39
|
+
if (val === true) el.setAttribute(k, k);
|
|
40
|
+
else if (val !== false && val != null) {
|
|
41
|
+
if (k === 'class' || k === 'className') el.className = val;
|
|
42
|
+
else if (k.startsWith('on') && typeof val === 'function') el[k.toLowerCase()] = val;
|
|
43
|
+
else if (k === 'value' && (v.tag === 'input' || v.tag === 'textarea' || v.tag === 'select')) el.value = val;
|
|
44
|
+
else el.setAttribute(k, String(val));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const ch = __vdom_flatten(v.children);
|
|
48
|
+
for (let i = 0; i < ch.length; i++) el.appendChild(__vdom_mount(ch[i]));
|
|
49
|
+
v._el = el;
|
|
50
|
+
return el;
|
|
51
|
+
}
|
|
52
|
+
function __vdomPatchEl(el, ov, nv) {
|
|
53
|
+
if (!el || !ov || !nv) return false;
|
|
54
|
+
if (typeof nv === 'string') {
|
|
55
|
+
if (el.nodeType === 3) el.textContent = nv;
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
if (ov.tag !== nv.tag) return false;
|
|
59
|
+
nv._el = el;
|
|
60
|
+
const p = nv.props || {};
|
|
61
|
+
const op = ov.props || {};
|
|
62
|
+
for (const k of Object.keys(p)) {
|
|
63
|
+
if (p[k] !== op[k]) {
|
|
64
|
+
const val = p[k];
|
|
65
|
+
if (k === 'class' || k === 'className') el.className = val || '';
|
|
66
|
+
else if (k.startsWith('on')) el[k.toLowerCase()] = val || null;
|
|
67
|
+
else if (k === 'value' && (nv.tag === 'input' || nv.tag === 'textarea' || nv.tag === 'select')) {
|
|
68
|
+
if (el !== document.activeElement) el.value = val;
|
|
69
|
+
} else el.setAttribute(k, String(val));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const ocx = __vdom_flatten(ov.children);
|
|
73
|
+
const ncx = __vdom_flatten(nv.children);
|
|
74
|
+
let fullCh = ocx.length !== ncx.length;
|
|
75
|
+
if (!fullCh) {
|
|
76
|
+
for (let j = 0; j < ncx.length; j++) {
|
|
77
|
+
const o = ocx[j], n = ncx[j];
|
|
78
|
+
if (typeof n === 'string') { if (typeof o !== 'string') fullCh = true; }
|
|
79
|
+
else if (typeof o === 'string') fullCh = true;
|
|
80
|
+
else if (!o || o.tag !== n.tag) fullCh = true;
|
|
81
|
+
if (fullCh) break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (fullCh) {
|
|
85
|
+
const nodes = [];
|
|
86
|
+
for (let j = 0; j < ncx.length; j++) nodes.push(__vdom_mount(ncx[j]));
|
|
87
|
+
el.replaceChildren(...nodes);
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
let i = 0;
|
|
91
|
+
while (i < ncx.length) {
|
|
92
|
+
const o = ocx[i], n = ncx[i];
|
|
93
|
+
const childEl = el.childNodes[i];
|
|
94
|
+
if (n == null) {
|
|
95
|
+
if (childEl) el.removeChild(childEl);
|
|
96
|
+
i++;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (o == null) {
|
|
100
|
+
el.appendChild(__vdom_mount(n));
|
|
101
|
+
i++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (typeof n === 'string') {
|
|
105
|
+
if (childEl && childEl.nodeType === 3) childEl.textContent = n;
|
|
106
|
+
else {
|
|
107
|
+
const m = __vdom_mount(n);
|
|
108
|
+
if (childEl) el.replaceChild(m, childEl);
|
|
109
|
+
else el.appendChild(m);
|
|
110
|
+
}
|
|
111
|
+
i++;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (typeof o === 'string' || o.tag !== n.tag) {
|
|
115
|
+
const m = __vdom_mount(n);
|
|
116
|
+
if (childEl) el.replaceChild(m, childEl);
|
|
117
|
+
else el.appendChild(m);
|
|
118
|
+
i++;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!childEl || !__vdomPatchEl(childEl, o, n)) {
|
|
122
|
+
const m = __vdom_mount(n);
|
|
123
|
+
if (childEl) el.replaceChild(m, childEl);
|
|
124
|
+
else el.appendChild(m);
|
|
125
|
+
}
|
|
126
|
+
i++;
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
window.__lattishVdomPatch = function(container, oldTree, newTree) {
|
|
131
|
+
try {
|
|
132
|
+
if (oldTree == null) {
|
|
133
|
+
container.replaceChildren(__vdom_mount(newTree));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const el = container.firstChild;
|
|
137
|
+
if (!el) {
|
|
138
|
+
container.appendChild(__vdom_mount(newTree));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (typeof newTree === 'string' || oldTree.tag !== newTree.tag) {
|
|
142
|
+
container.replaceChildren(__vdom_mount(newTree));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (!__vdomPatchEl(el, oldTree, newTree)) {
|
|
146
|
+
container.replaceChildren(__vdom_mount(newTree));
|
|
147
|
+
}
|
|
148
|
+
} catch (err) {
|
|
149
|
+
console.warn('Lattish VDOM patch failed, full remount', err);
|
|
150
|
+
try {
|
|
151
|
+
container.replaceChildren(__vdom_mount(newTree));
|
|
152
|
+
} catch (e2) {
|
|
153
|
+
console.error('Lattish VDOM remount failed', e2);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
"#;
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// Lattish: Lattish runtime for compiled JSX. Publish to npm; consumed by tish and tish-playground.
|
|
2
|
+
// With `tish compile --target js --jsx vdom`, the compiler injects a prelude that sets
|
|
3
|
+
// window.__LATTISH_JSX_VDOM and __lattishVdomPatch; createRoot then reconciles vnodes.
|
|
4
|
+
|
|
5
|
+
export let Fragment = Symbol("Lattish.Fragment")
|
|
6
|
+
|
|
7
|
+
fn __lattishAppend(parent, c) {
|
|
8
|
+
if (c === null || c === undefined) {
|
|
9
|
+
return
|
|
10
|
+
}
|
|
11
|
+
if (Array.isArray(c)) {
|
|
12
|
+
let i = 0
|
|
13
|
+
while (i < c.length) {
|
|
14
|
+
__lattishAppend(parent, c[i])
|
|
15
|
+
i = i + 1
|
|
16
|
+
}
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
if (typeof c === "string") {
|
|
20
|
+
parent.appendChild(document.createTextNode(c))
|
|
21
|
+
} else {
|
|
22
|
+
parent.appendChild(c)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export fn h(tag, props, children) {
|
|
27
|
+
if (children === undefined || children === null) {
|
|
28
|
+
children = []
|
|
29
|
+
}
|
|
30
|
+
if (tag === Fragment) {
|
|
31
|
+
let f = document.createDocumentFragment()
|
|
32
|
+
let i = 0
|
|
33
|
+
while (i < children.length) {
|
|
34
|
+
__lattishAppend(f, children[i])
|
|
35
|
+
i = i + 1
|
|
36
|
+
}
|
|
37
|
+
return f
|
|
38
|
+
}
|
|
39
|
+
let el = document.createElement(tag)
|
|
40
|
+
let refVal = null
|
|
41
|
+
if (props && props !== null) {
|
|
42
|
+
let keys = Object.keys(props)
|
|
43
|
+
let ki = 0
|
|
44
|
+
while (ki < keys.length) {
|
|
45
|
+
let k = keys[ki]
|
|
46
|
+
let v = props[k]
|
|
47
|
+
if (k === "ref") {
|
|
48
|
+
refVal = v
|
|
49
|
+
} else {
|
|
50
|
+
if (k === "disabled") {
|
|
51
|
+
el.disabled = !!v
|
|
52
|
+
} else {
|
|
53
|
+
if (v === true) {
|
|
54
|
+
el.setAttribute(k, k)
|
|
55
|
+
} else {
|
|
56
|
+
if (v !== false && v !== null && v !== undefined) {
|
|
57
|
+
if (k === "class" || k === "className") {
|
|
58
|
+
el.className = v
|
|
59
|
+
} else {
|
|
60
|
+
if (k.length >= 3 && k[0] === "o" && k[1] === "n" && typeof v === "function") {
|
|
61
|
+
let eventType = k.slice(2).toLowerCase()
|
|
62
|
+
el.addEventListener(eventType, v)
|
|
63
|
+
} else {
|
|
64
|
+
if (k === "value" && (tag === "input" || tag === "textarea")) {
|
|
65
|
+
el.value = v
|
|
66
|
+
} else {
|
|
67
|
+
if (k !== "ref") {
|
|
68
|
+
el.setAttribute(k, String(v))
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
ki = ki + 1
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
let ci = 0
|
|
81
|
+
while (ci < children.length) {
|
|
82
|
+
__lattishAppend(el, children[ci])
|
|
83
|
+
ci = ci + 1
|
|
84
|
+
}
|
|
85
|
+
if (refVal !== null && refVal !== undefined && typeof refVal === "object") {
|
|
86
|
+
refVal.current = el
|
|
87
|
+
}
|
|
88
|
+
return el
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export fn text(s) {
|
|
92
|
+
return s
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let __state = []
|
|
96
|
+
let __cursor = 0
|
|
97
|
+
let __container = null
|
|
98
|
+
let __App = null
|
|
99
|
+
let __rootVnode = null
|
|
100
|
+
|
|
101
|
+
let __memoSlots = []
|
|
102
|
+
let __refSlots = []
|
|
103
|
+
let __layoutEffectSlots = []
|
|
104
|
+
let __effectSlots = []
|
|
105
|
+
|
|
106
|
+
let __flushScheduled = false
|
|
107
|
+
let __batchDepth = 0
|
|
108
|
+
let __dirtyFromBatch = false
|
|
109
|
+
let __effectsMicrotaskScheduled = false
|
|
110
|
+
|
|
111
|
+
fn __depsChanged(prev, next) {
|
|
112
|
+
if (prev === null) {
|
|
113
|
+
return true
|
|
114
|
+
}
|
|
115
|
+
if (next === null || prev === null) {
|
|
116
|
+
return true
|
|
117
|
+
}
|
|
118
|
+
if (prev.length !== next.length) {
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
let j = 0
|
|
122
|
+
while (j < next.length) {
|
|
123
|
+
if (prev[j] !== next[j]) {
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
j = j + 1
|
|
127
|
+
}
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
fn __runLayoutEffects() {
|
|
132
|
+
let i = 0
|
|
133
|
+
while (i < __layoutEffectSlots.length) {
|
|
134
|
+
let s = __layoutEffectSlots[i]
|
|
135
|
+
if (s && s.pending) {
|
|
136
|
+
s.pending = false
|
|
137
|
+
if (s.cleanup) {
|
|
138
|
+
s.cleanup()
|
|
139
|
+
s.cleanup = null
|
|
140
|
+
}
|
|
141
|
+
let c = s.effect()
|
|
142
|
+
if (typeof c === "function") {
|
|
143
|
+
s.cleanup = c
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
i = i + 1
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fn __scheduleEffectsFlush() {
|
|
151
|
+
if (__effectsMicrotaskScheduled) {
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
__effectsMicrotaskScheduled = true
|
|
155
|
+
queueMicrotask(() => {
|
|
156
|
+
__effectsMicrotaskScheduled = false
|
|
157
|
+
let i = 0
|
|
158
|
+
while (i < __effectSlots.length) {
|
|
159
|
+
let s = __effectSlots[i]
|
|
160
|
+
if (s && s.pending) {
|
|
161
|
+
s.pending = false
|
|
162
|
+
if (s.cleanup) {
|
|
163
|
+
s.cleanup()
|
|
164
|
+
s.cleanup = null
|
|
165
|
+
}
|
|
166
|
+
let c = s.effect()
|
|
167
|
+
if (typeof c === "function") {
|
|
168
|
+
s.cleanup = c
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
i = i + 1
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/// Runs synchronously after DOM is updated (before paint in typical browsers).
|
|
177
|
+
export fn useLayoutEffect(effect, deps) {
|
|
178
|
+
let i = __cursor
|
|
179
|
+
__cursor = __cursor + 1
|
|
180
|
+
while (i >= __layoutEffectSlots.length) {
|
|
181
|
+
__layoutEffectSlots.push(null)
|
|
182
|
+
}
|
|
183
|
+
let slot = __layoutEffectSlots[i]
|
|
184
|
+
if (slot === null) {
|
|
185
|
+
slot = { deps: null, effect: null, cleanup: null, pending: false }
|
|
186
|
+
__layoutEffectSlots[i] = slot
|
|
187
|
+
}
|
|
188
|
+
if (__depsChanged(slot.deps, deps)) {
|
|
189
|
+
slot.deps = deps
|
|
190
|
+
slot.effect = effect
|
|
191
|
+
slot.pending = true
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/// Runs after paint (microtask after flush).
|
|
196
|
+
export fn useEffect(effect, deps) {
|
|
197
|
+
let i = __cursor
|
|
198
|
+
__cursor = __cursor + 1
|
|
199
|
+
while (i >= __effectSlots.length) {
|
|
200
|
+
__effectSlots.push(null)
|
|
201
|
+
}
|
|
202
|
+
let slot = __effectSlots[i]
|
|
203
|
+
if (slot === null) {
|
|
204
|
+
slot = { deps: null, effect: null, cleanup: null, pending: false }
|
|
205
|
+
__effectSlots[i] = slot
|
|
206
|
+
}
|
|
207
|
+
if (__depsChanged(slot.deps, deps)) {
|
|
208
|
+
slot.deps = deps
|
|
209
|
+
slot.effect = effect
|
|
210
|
+
slot.pending = true
|
|
211
|
+
__scheduleEffectsFlush()
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fn __usingVdom() {
|
|
216
|
+
return typeof window !== "undefined" && window.__LATTISH_JSX_VDOM && window.__lattishVdomPatch
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
fn __flushRender() {
|
|
220
|
+
__cursor = 0
|
|
221
|
+
if (__container && __App) {
|
|
222
|
+
if (__usingVdom()) {
|
|
223
|
+
let newTree = __App()
|
|
224
|
+
window.__lattishVdomPatch(__container, __rootVnode, newTree)
|
|
225
|
+
__rootVnode = newTree
|
|
226
|
+
} else {
|
|
227
|
+
let el = __App()
|
|
228
|
+
__container.replaceChildren(el)
|
|
229
|
+
__rootVnode = null
|
|
230
|
+
}
|
|
231
|
+
__runLayoutEffects()
|
|
232
|
+
__scheduleEffectsFlush()
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
fn __scheduleFlush() {
|
|
237
|
+
if (__batchDepth > 0) {
|
|
238
|
+
__dirtyFromBatch = true
|
|
239
|
+
return
|
|
240
|
+
}
|
|
241
|
+
if (__flushScheduled) {
|
|
242
|
+
return
|
|
243
|
+
}
|
|
244
|
+
__flushScheduled = true
|
|
245
|
+
queueMicrotask(() => {
|
|
246
|
+
__flushScheduled = false
|
|
247
|
+
__flushRender()
|
|
248
|
+
})
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export fn unstable_batchedUpdates(run) {
|
|
252
|
+
__batchDepth = __batchDepth + 1
|
|
253
|
+
run()
|
|
254
|
+
__batchDepth = __batchDepth - 1
|
|
255
|
+
if (__batchDepth === 0 && __dirtyFromBatch) {
|
|
256
|
+
__dirtyFromBatch = false
|
|
257
|
+
__flushScheduled = false
|
|
258
|
+
__flushRender()
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export fn useState(init) {
|
|
263
|
+
let i = __cursor
|
|
264
|
+
__cursor = __cursor + 1
|
|
265
|
+
if (i >= __state.length) {
|
|
266
|
+
__state.push(init)
|
|
267
|
+
}
|
|
268
|
+
fn setState(v) {
|
|
269
|
+
if (typeof v === "function") {
|
|
270
|
+
__state[i] = v(__state[i])
|
|
271
|
+
} else {
|
|
272
|
+
__state[i] = v
|
|
273
|
+
}
|
|
274
|
+
__scheduleFlush()
|
|
275
|
+
}
|
|
276
|
+
return [__state[i], setState]
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export fn useMemo(deps, factory) {
|
|
280
|
+
let i = __cursor
|
|
281
|
+
__cursor = __cursor + 1
|
|
282
|
+
while (i >= __memoSlots.length) {
|
|
283
|
+
__memoSlots.push(null)
|
|
284
|
+
}
|
|
285
|
+
let slot = __memoSlots[i]
|
|
286
|
+
let stale = true
|
|
287
|
+
if (slot) {
|
|
288
|
+
stale = false
|
|
289
|
+
if (slot.deps.length !== deps.length) {
|
|
290
|
+
stale = true
|
|
291
|
+
} else {
|
|
292
|
+
let j = 0
|
|
293
|
+
while (j < deps.length) {
|
|
294
|
+
if (slot.deps[j] !== deps[j]) {
|
|
295
|
+
stale = true
|
|
296
|
+
break
|
|
297
|
+
}
|
|
298
|
+
j = j + 1
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (stale) {
|
|
303
|
+
__memoSlots[i] = { deps: deps, value: factory() }
|
|
304
|
+
}
|
|
305
|
+
return __memoSlots[i].value
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export fn useRef(initial) {
|
|
309
|
+
let i = __cursor
|
|
310
|
+
__cursor = __cursor + 1
|
|
311
|
+
while (i >= __refSlots.length) {
|
|
312
|
+
__refSlots.push(null)
|
|
313
|
+
}
|
|
314
|
+
if (__refSlots[i] === null) {
|
|
315
|
+
__refSlots[i] = { current: initial }
|
|
316
|
+
}
|
|
317
|
+
return __refSlots[i]
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export fn createRoot(container) {
|
|
321
|
+
fn render(App) {
|
|
322
|
+
__container = container
|
|
323
|
+
__App = App
|
|
324
|
+
__state = []
|
|
325
|
+
__memoSlots = []
|
|
326
|
+
__refSlots = []
|
|
327
|
+
__layoutEffectSlots = []
|
|
328
|
+
__effectSlots = []
|
|
329
|
+
__cursor = 0
|
|
330
|
+
__flushScheduled = false
|
|
331
|
+
__batchDepth = 0
|
|
332
|
+
__dirtyFromBatch = false
|
|
333
|
+
__effectsMicrotaskScheduled = false
|
|
334
|
+
__rootVnode = null
|
|
335
|
+
if (__usingVdom()) {
|
|
336
|
+
__rootVnode = App()
|
|
337
|
+
window.__lattishVdomPatch(container, null, __rootVnode)
|
|
338
|
+
} else {
|
|
339
|
+
let el = App()
|
|
340
|
+
container.replaceChildren(el)
|
|
341
|
+
}
|
|
342
|
+
__cursor = 0
|
|
343
|
+
__runLayoutEffects()
|
|
344
|
+
__scheduleEffectsFlush()
|
|
345
|
+
}
|
|
346
|
+
return { render: render }
|
|
347
|
+
}
|