@stillflow/engine 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +156 -3
- package/dist/index.cjs.js +1051 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.es.js +1023 -0
- package/dist/jsx-runtime.d.ts +43 -0
- package/dist/lib-DUuDpiwh.js +11975 -0
- package/dist/lib-lWz8P7iq.cjs +11979 -0
- package/dist/stillflow/abstracts/IR.d.ts +7 -0
- package/dist/stillflow/abstracts/meta.d.ts +8 -0
- package/dist/stillflow/abstracts/operators.d.ts +22 -0
- package/dist/stillflow/abstracts/state.d.ts +13 -0
- package/dist/stillflow/abstracts/view.d.ts +8 -0
- package/dist/stillflow/compiler/compile.d.ts +11 -0
- package/dist/stillflow/compiler/instantiate.d.ts +3 -0
- package/dist/stillflow/factories/factories.d.ts +10 -0
- package/dist/stillflow/index.d.ts +5 -0
- package/dist/stillflow/kernel/buffer.d.ts +10 -0
- package/dist/stillflow/kernel/kernel.d.ts +26 -0
- package/dist/stillflow/kernel/signal.d.ts +5 -0
- package/dist/stillflow/operators.d.ts +15 -0
- package/dist/stillflow/state.d.ts +15 -0
- package/dist/stillflow/stillflow.d.ts +28 -0
- package/dist/stillflow/tools/batcher.d.ts +9 -0
- package/dist/stillflow/variable.d.ts +12 -0
- package/dist/stillflow-collection/abstract/collection.delta.d.ts +10 -0
- package/dist/stillflow-collection/abstract/collection.flusher.d.ts +17 -0
- package/dist/stillflow-collection/abstract/collection.glee.d.ts +28 -0
- package/dist/stillflow-collection/abstract/collection.index.d.ts +9 -0
- package/dist/stillflow-collection/abstract/collection.logical.d.ts +13 -0
- package/dist/stillflow-collection/abstract/collection.physical.d.ts +13 -0
- package/dist/stillflow-collection/abstract/collection.serializable.d.ts +4 -0
- package/dist/stillflow-collection/array.d.ts +21 -0
- package/dist/stillflow-collection/fenwick.d.ts +28 -0
- package/dist/stillflow-collection/flushable.d.ts +38 -0
- package/dist/stillflow-collection/index.d.ts +6 -0
- package/dist/stillflow-collection/utilities/disjointed-set.d.ts +10 -0
- package/dist/stillflow-collection/utilities/fenwick-tree.d.ts +9 -0
- package/dist/stillflow-collection/utilities/index.d.ts +1 -0
- package/dist/stillflow-collection/utilities/interval-merger.d.ts +7 -0
- package/dist/stillflow-collection/utilities/range-fenwick-tree.d.ts +11 -0
- package/dist/stillflow-runtime/dom.d.ts +5 -0
- package/dist/stillflow-runtime/utilities/delegate-event.d.ts +1 -0
- package/dist/stillflow-runtime/utilities/index.d.ts +3 -0
- package/dist/stillflow-runtime/utilities/reconcile-if.d.ts +10 -0
- package/dist/stillflow-runtime/utilities/reconcile-list.d.ts +8 -0
- package/dist/stillflow-serialize/deserialize.d.ts +2 -0
- package/dist/stillflow-serialize/index.d.ts +2 -0
- package/dist/stillflow-serialize/serialize.d.ts +2 -0
- package/dist/stillflow-serialize/serialized.d.ts +4 -0
- package/dist/stillflow-vite/dom.pass.codegen.d.ts +8 -0
- package/dist/stillflow-vite/dom.pass.transform.d.ts +16 -0
- package/dist/stillflow-vite/dom.target.d.ts +2 -0
- package/dist/stillflow-vite/targets.plugin.d.ts +9 -0
- package/dist/stillflow-vite/vm-abstract/index.d.ts +3 -0
- package/dist/stillflow-vite/vm-abstract/vm.instruction.d.ts +7 -0
- package/dist/stillflow-vite/vm-abstract/vm.options.d.ts +4 -0
- package/dist/stillflow-vite/vm-abstract/vm.target.d.ts +23 -0
- package/dist/stillflow-vite/vm-compiler/bytecode-builder.d.ts +33 -0
- package/dist/stillflow-vite/vm-compiler/codes.d.ts +20 -0
- package/dist/stillflow-vite/vm-compiler/dirty.d.ts +7 -0
- package/dist/stillflow-vite/vm-compiler/fabric.d.ts +9 -0
- package/dist/stillflow-vite/vm-compiler/index.d.ts +8 -0
- package/dist/stillflow-vite/vm-compiler/nodes.d.ts +72 -0
- package/dist/stillflow-vite/vm-compiler/pass.analyze-dirty.d.ts +3 -0
- package/dist/stillflow-vite/vm-compiler/pass.analyze-dynamic.d.ts +2 -0
- package/dist/stillflow-vite/vm-compiler/pass.lowering.d.ts +4 -0
- package/dist/stillflow-vite/vm-compiler/pass.scopes.d.ts +14 -0
- package/dist/stillflow-vite/vm-targets/collect-components.d.ts +8 -0
- package/dist/stillflow-vite/vm-targets/collect-imports.d.ts +3 -0
- package/dist/stillflow-vite/vm-targets/find-component.d.ts +3 -0
- package/dist/targets.index.d.ts +1 -0
- package/dist/vite.cjs.js +261 -0
- package/dist/vite.es.js +260 -0
- package/dist/vite.plugin.d.ts +3 -0
- package/dist/vm.instruction-DPuxu4sV.cjs +9 -0
- package/dist/vm.instruction-oyM-GI-p.js +4 -0
- package/dist/vms.cjs.js +1096 -0
- package/dist/vms.es.js +1095 -0
- package/package.json +99 -5
package/dist/vms.cjs.js
ADDED
|
@@ -0,0 +1,1096 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _babel_core = require("@babel/core");
|
|
3
|
+
const require_vm_instruction = require("./vm.instruction-DPuxu4sV.cjs");
|
|
4
|
+
// src/stillflow-vite/vm-compiler/nodes.ts
|
|
5
|
+
var as_slot = (n) => n;
|
|
6
|
+
var as_chunk = (n) => n;
|
|
7
|
+
var as_intern = (n) => n;
|
|
8
|
+
var create_empty_meta = () => {
|
|
9
|
+
return {
|
|
10
|
+
scope: { captures: /* @__PURE__ */ new Set() },
|
|
11
|
+
analysis: {
|
|
12
|
+
is_dynamic: false,
|
|
13
|
+
dirty_mask: 0,
|
|
14
|
+
has_child_chunks: false
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
// src/stillflow-vite/vm-compiler/fabric.ts
|
|
19
|
+
var Fabric = class {
|
|
20
|
+
static expression(code, refs) {
|
|
21
|
+
return {
|
|
22
|
+
kind: "Expression",
|
|
23
|
+
code,
|
|
24
|
+
refs,
|
|
25
|
+
meta: create_empty_meta()
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
static element(tag, static_properties, dynamic_properties, children, delegated_events) {
|
|
29
|
+
return {
|
|
30
|
+
kind: "Element",
|
|
31
|
+
tag,
|
|
32
|
+
static_properties,
|
|
33
|
+
dynamic_properties,
|
|
34
|
+
delegated_events,
|
|
35
|
+
children,
|
|
36
|
+
dynamic_properties_mask: {},
|
|
37
|
+
delegated_events_mask: {},
|
|
38
|
+
meta: create_empty_meta()
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
static text(value) {
|
|
42
|
+
return {
|
|
43
|
+
kind: "Text",
|
|
44
|
+
value,
|
|
45
|
+
is_dynamic: typeof value !== "string",
|
|
46
|
+
meta: create_empty_meta()
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
static for(select, item_name, index_name, body) {
|
|
50
|
+
return {
|
|
51
|
+
kind: "For",
|
|
52
|
+
select,
|
|
53
|
+
index_name,
|
|
54
|
+
item_name,
|
|
55
|
+
body,
|
|
56
|
+
meta: create_empty_meta(),
|
|
57
|
+
dirty_context: {
|
|
58
|
+
tracked_variables: [],
|
|
59
|
+
variable_masks: /* @__PURE__ */ new Map()
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
static call(component_name, properties) {
|
|
64
|
+
return {
|
|
65
|
+
kind: "Call",
|
|
66
|
+
component_name,
|
|
67
|
+
properties,
|
|
68
|
+
meta: create_empty_meta()
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
static if(branches) {
|
|
72
|
+
return {
|
|
73
|
+
kind: "If",
|
|
74
|
+
branches,
|
|
75
|
+
meta: create_empty_meta()
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
// src/stillflow-vite/dom.pass.transform.ts
|
|
80
|
+
function transform(node, raw_components) {
|
|
81
|
+
const RESERVED = {
|
|
82
|
+
"for": transform_for,
|
|
83
|
+
"if": transform_if
|
|
84
|
+
};
|
|
85
|
+
const DYNAMIC_TABLE = { "class": "className" };
|
|
86
|
+
const BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
|
|
87
|
+
"String",
|
|
88
|
+
"Number",
|
|
89
|
+
"Boolean",
|
|
90
|
+
"Array",
|
|
91
|
+
"Object",
|
|
92
|
+
"Math",
|
|
93
|
+
"JSON",
|
|
94
|
+
"Date",
|
|
95
|
+
"RegExp",
|
|
96
|
+
"Map",
|
|
97
|
+
"Set",
|
|
98
|
+
"console",
|
|
99
|
+
"window",
|
|
100
|
+
"document",
|
|
101
|
+
"undefined",
|
|
102
|
+
"NaN",
|
|
103
|
+
"Infinity",
|
|
104
|
+
"isNaN",
|
|
105
|
+
"parseInt",
|
|
106
|
+
"parseFloat"
|
|
107
|
+
]);
|
|
108
|
+
const CONTINUATIONS = {
|
|
109
|
+
elif(previous, properties, children) {
|
|
110
|
+
if (previous === void 0 || previous.kind !== "If") throw new Error(`<elif> must immediately follow an <if> or an <elif> tag`);
|
|
111
|
+
if (previous.branches.at(-1)?.condition === void 0) throw new Error(`Unexpected <elif> after <else> `);
|
|
112
|
+
if ("condition" in properties === false) throw new Error(`<elif> must contain a "condition" attribute`);
|
|
113
|
+
previous.branches.push({
|
|
114
|
+
condition: properties["condition"],
|
|
115
|
+
children
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
else(previous, properties, children) {
|
|
119
|
+
if (!previous || previous.kind !== "If") throw new Error(`<else> must immediately follow an <if> or <elif> tag`);
|
|
120
|
+
if (previous.branches.at(-1)?.condition === void 0) throw new Error(`Duplicate <else> for the same <if>`);
|
|
121
|
+
previous.branches.push({ children });
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
function map_dynamic_property(name) {
|
|
125
|
+
if (name in DYNAMIC_TABLE) return DYNAMIC_TABLE[name];
|
|
126
|
+
return name;
|
|
127
|
+
}
|
|
128
|
+
function get_name(name) {
|
|
129
|
+
if (_babel_core.types.isJSXNamespacedName(name)) return `${name.namespace.name}:${name.name.name}`;
|
|
130
|
+
if (_babel_core.types.isJSXIdentifier(name)) return `${name.name}`;
|
|
131
|
+
if (_babel_core.types.isJSXMemberExpression(name)) return `${get_name(name.object)}.${name.property}`;
|
|
132
|
+
}
|
|
133
|
+
function unwrap_ts_types(node) {
|
|
134
|
+
while (_babel_core.types.isTSAsExpression(node) || _babel_core.types.isTSSatisfiesExpression(node) || _babel_core.types.isTSTypeAssertion(node) || _babel_core.types.isTSNonNullExpression(node)) node = node.expression;
|
|
135
|
+
return node;
|
|
136
|
+
}
|
|
137
|
+
function visit(node) {
|
|
138
|
+
if (_babel_core.types.isJSXText(node) || _babel_core.types.isJSXExpressionContainer(node)) return transform_text(node);
|
|
139
|
+
if (_babel_core.types.isJSXElement(node)) return transform_element(node);
|
|
140
|
+
throw new Error(`Undefined node ${node.type} at ${node.loc.filename} ${node.loc.start.line}:${node.loc.start.column}`);
|
|
141
|
+
}
|
|
142
|
+
function transform_expression_node(node) {
|
|
143
|
+
const refs = /* @__PURE__ */ new Set();
|
|
144
|
+
const expression = node;
|
|
145
|
+
const wrapper = _babel_core.types.file(_babel_core.types.program([_babel_core.types.expressionStatement(expression)]));
|
|
146
|
+
(0, _babel_core.traverse)(wrapper, { Identifier(path) {
|
|
147
|
+
if (!path.isReferencedIdentifier()) return;
|
|
148
|
+
const name = path.node.name;
|
|
149
|
+
if (BUILTIN_GLOBALS.has(name)) return;
|
|
150
|
+
if (path.scope.hasBinding(name)) return;
|
|
151
|
+
refs.add(name);
|
|
152
|
+
} });
|
|
153
|
+
let code = (0, _babel_core.generate)(unwrap_ts_types(expression)).code;
|
|
154
|
+
return Fabric.expression(code, Array.from(refs));
|
|
155
|
+
}
|
|
156
|
+
function transform_expression(node) {
|
|
157
|
+
const expression = node.expression;
|
|
158
|
+
if (_babel_core.types.isJSXEmptyExpression(expression)) throw new Error(`We do not support JSX empty expressions at ${expression.loc.filename} ${expression.loc.start.line}:${expression.loc.start.column}`);
|
|
159
|
+
return transform_expression_node(expression);
|
|
160
|
+
}
|
|
161
|
+
function transform_text(node) {
|
|
162
|
+
if (_babel_core.types.isJSXText(node)) {
|
|
163
|
+
const value = node.value.replace(/\s*\n\s*/g, String());
|
|
164
|
+
if (value !== String()) return Fabric.text(value);
|
|
165
|
+
}
|
|
166
|
+
if (_babel_core.types.isJSXExpressionContainer(node)) return Fabric.text(transform_expression(node));
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
function transform_for(_, dynamics, children) {
|
|
170
|
+
if ("select" in dynamics === false) throw new Error(`For expression should contain select attribute`);
|
|
171
|
+
if (children.length < 1) throw new Error(`For should have at least one child`);
|
|
172
|
+
const select = dynamics["select"];
|
|
173
|
+
return Fabric.for(select, "$it", "$index", children);
|
|
174
|
+
}
|
|
175
|
+
function transform_if(_, dynamics, children) {
|
|
176
|
+
if ("condition" in dynamics === false) throw new Error(`<if> must contain a "condition" attribute`);
|
|
177
|
+
return Fabric.if([{
|
|
178
|
+
condition: dynamics["condition"],
|
|
179
|
+
children
|
|
180
|
+
}]);
|
|
181
|
+
}
|
|
182
|
+
function parse_element_parts(node) {
|
|
183
|
+
const statics = {};
|
|
184
|
+
const dynamics = {};
|
|
185
|
+
const events = {};
|
|
186
|
+
const tag = get_name(node.openingElement.name);
|
|
187
|
+
const attributes = node.openingElement.attributes;
|
|
188
|
+
for (const attribute of attributes) {
|
|
189
|
+
if (!_babel_core.types.isJSXAttribute(attribute)) throw new Error(`We do not support spread attributes at ${attribute.loc.filename} ${attribute.loc.start.line}:${attribute.loc.start.column}`);
|
|
190
|
+
const name = get_name(attribute.name);
|
|
191
|
+
const value = attribute.value;
|
|
192
|
+
if (!value) throw new Error(`We do not support empty attribute values at ${attribute.loc.filename} ${attribute.loc.start.line}:${attribute.loc.start.column}`);
|
|
193
|
+
if (_babel_core.types.isStringLiteral(value)) statics[name] = value.value;
|
|
194
|
+
if (_babel_core.types.isJSXExpressionContainer(value)) {
|
|
195
|
+
if (_babel_core.types.isJSXEmptyExpression(value.expression)) throw new Error(`We don't support JSX empty expressions at ${value.expression.loc.filename} ${value.expression.loc.start.line}:${value.expression.loc.start.column}`);
|
|
196
|
+
if (name.startsWith("sf:")) {
|
|
197
|
+
const event_name = name.slice(3);
|
|
198
|
+
const expression = unwrap_ts_types(value.expression);
|
|
199
|
+
if (!_babel_core.types.isArrayExpression(expression)) throw new Error(`In sf.${event_name} expected array expression at ${expression.loc.filename} ${expression.loc.start.line}:${expression.loc.start.column}`);
|
|
200
|
+
if (_babel_core.types.isSpreadElement(expression.elements[0]) || _babel_core.types.isSpreadElement(expression.elements[1])) throw new Error(`We don not support spread elements in tuple sf.${event_name} at ${expression.loc.filename} ${expression.loc.start.line}:${expression.loc.start.column}`);
|
|
201
|
+
events[event_name] = {
|
|
202
|
+
handler: transform_expression_node(expression.elements[0]),
|
|
203
|
+
data: transform_expression_node(expression.elements[1])
|
|
204
|
+
};
|
|
205
|
+
} else dynamics[map_dynamic_property(name)] = transform_expression(value);
|
|
206
|
+
}
|
|
207
|
+
if (_babel_core.types.isJSXElement(value) || _babel_core.types.isJSXFragment(value)) throw new Error(`We do not support attribute values such as JSXElement or JSXFragment at ${attribute.loc.filename} ${attribute.loc.start.line}:${attribute.loc.start.column}`);
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
tag,
|
|
211
|
+
statics,
|
|
212
|
+
dynamics,
|
|
213
|
+
events
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function parse_element_children(node, hook) {
|
|
217
|
+
const children = [];
|
|
218
|
+
for (const child of node.children) {
|
|
219
|
+
if (_babel_core.types.isJSXElement(child)) {
|
|
220
|
+
if (hook(child, children)) continue;
|
|
221
|
+
}
|
|
222
|
+
const transformed_child = visit(child);
|
|
223
|
+
if (transformed_child !== null) children.push(transformed_child);
|
|
224
|
+
}
|
|
225
|
+
return children;
|
|
226
|
+
}
|
|
227
|
+
function transform_element(node) {
|
|
228
|
+
const { statics, dynamics, tag, events } = parse_element_parts(node);
|
|
229
|
+
const children = parse_element_children(node, function child_hook(child, children) {
|
|
230
|
+
const child_tag = get_name(child.openingElement.name);
|
|
231
|
+
if (child_tag in CONTINUATIONS) {
|
|
232
|
+
const { dynamics } = parse_element_parts(child);
|
|
233
|
+
const branch_children = parse_element_children(child, child_hook);
|
|
234
|
+
CONTINUATIONS[child_tag](children.at(-1), dynamics, branch_children);
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
return false;
|
|
238
|
+
});
|
|
239
|
+
if (tag in RESERVED) return RESERVED[tag](node, dynamics, children);
|
|
240
|
+
if (raw_components.has(tag)) return Fabric.call(tag, dynamics);
|
|
241
|
+
return Fabric.element(tag, statics, dynamics, children, events);
|
|
242
|
+
}
|
|
243
|
+
const root = visit(node);
|
|
244
|
+
const components = /* @__PURE__ */ new Map();
|
|
245
|
+
for (const [name, component] of raw_components) components.set(name, {
|
|
246
|
+
name,
|
|
247
|
+
params: component.parameters,
|
|
248
|
+
body: visit(component.body)
|
|
249
|
+
});
|
|
250
|
+
return {
|
|
251
|
+
root,
|
|
252
|
+
components
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
// src/stillflow-vite/vm-compiler/pass.scopes.ts
|
|
256
|
+
var SymbolScope = class {
|
|
257
|
+
parent;
|
|
258
|
+
_locals = /* @__PURE__ */ new Set();
|
|
259
|
+
_uses = /* @__PURE__ */ new Set();
|
|
260
|
+
constructor(parent = null) {
|
|
261
|
+
this.parent = parent;
|
|
262
|
+
}
|
|
263
|
+
declare(name) {
|
|
264
|
+
this._locals.add(name);
|
|
265
|
+
}
|
|
266
|
+
has_own(name) {
|
|
267
|
+
return this._locals.has(name);
|
|
268
|
+
}
|
|
269
|
+
is_captured(name) {
|
|
270
|
+
if (this._locals.has(name)) return false;
|
|
271
|
+
if (this.parent === null || this.parent.parent === null) return false;
|
|
272
|
+
if (this.parent.has_own(name)) return true;
|
|
273
|
+
return this.parent.is_captured(name);
|
|
274
|
+
}
|
|
275
|
+
mark_used(name) {
|
|
276
|
+
if (this._locals.has(name)) {
|
|
277
|
+
this._uses.add(name);
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
if (this.parent !== null) return this.parent.mark_used(name);
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
is_used(name) {
|
|
284
|
+
return this._uses.has(name);
|
|
285
|
+
}
|
|
286
|
+
has(name) {
|
|
287
|
+
if (this._locals.has(name)) return true;
|
|
288
|
+
if (this.parent !== null) return this.parent.has(name);
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
function resolve_scopes(node, scope) {
|
|
293
|
+
const captures = /* @__PURE__ */ new Set();
|
|
294
|
+
switch (node.kind) {
|
|
295
|
+
case "Expression":
|
|
296
|
+
for (const ref of node.refs) {
|
|
297
|
+
scope.mark_used(ref);
|
|
298
|
+
if (scope.is_captured(ref)) captures.add(ref);
|
|
299
|
+
}
|
|
300
|
+
break;
|
|
301
|
+
case "Text":
|
|
302
|
+
if (typeof node.value !== "string") for (const capture of resolve_scopes(node.value, scope)) captures.add(capture);
|
|
303
|
+
break;
|
|
304
|
+
case "Element":
|
|
305
|
+
for (const expression of Object.values(node.dynamic_properties)) for (const capture of resolve_scopes(expression, scope)) captures.add(capture);
|
|
306
|
+
for (const { handler, data } of Object.values(node.delegated_events)) {
|
|
307
|
+
for (const capture of resolve_scopes(handler, scope)) captures.add(capture);
|
|
308
|
+
for (const capture of resolve_scopes(data, scope)) captures.add(capture);
|
|
309
|
+
}
|
|
310
|
+
for (const child of node.children) for (const capture of resolve_scopes(child, scope)) captures.add(capture);
|
|
311
|
+
break;
|
|
312
|
+
case "For": {
|
|
313
|
+
for (const capture of resolve_scopes(node.select, scope)) captures.add(capture);
|
|
314
|
+
const body_scope = new SymbolScope(scope);
|
|
315
|
+
body_scope.declare(node.item_name);
|
|
316
|
+
body_scope.declare(node.index_name);
|
|
317
|
+
const body_captures = /* @__PURE__ */ new Set();
|
|
318
|
+
for (const child of node.body) {
|
|
319
|
+
const inner_captures = resolve_scopes(child, body_scope);
|
|
320
|
+
for (const capture of inner_captures) if (scope.is_captured(capture)) {
|
|
321
|
+
captures.add(capture);
|
|
322
|
+
body_captures.add(capture);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
node.meta.scope.captures = body_captures;
|
|
326
|
+
return captures;
|
|
327
|
+
}
|
|
328
|
+
case "Call":
|
|
329
|
+
for (const expression of Object.values(node.properties)) for (const capture of resolve_scopes(expression, scope)) captures.add(capture);
|
|
330
|
+
break;
|
|
331
|
+
case "If": {
|
|
332
|
+
const locals = new SymbolScope(scope);
|
|
333
|
+
for (const branch of node.branches) {
|
|
334
|
+
if (branch.condition !== void 0) for (const capture of resolve_scopes(branch.condition, locals)) captures.add(capture);
|
|
335
|
+
for (const child of branch.children) for (const capture of resolve_scopes(child, locals)) captures.add(capture);
|
|
336
|
+
}
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
node.meta.scope.captures = captures;
|
|
341
|
+
return captures;
|
|
342
|
+
}
|
|
343
|
+
// src/stillflow-vite/vm-compiler/dirty.ts
|
|
344
|
+
function create_dirty_context(variables) {
|
|
345
|
+
const tracked_variables = Array.from(variables);
|
|
346
|
+
if (tracked_variables.length > 31) throw new Error(`Scope tracks ${tracked_variables.length} variables (max 313 for 32-bit SMI)`);
|
|
347
|
+
const variable_masks = /* @__PURE__ */ new Map();
|
|
348
|
+
for (let index = 0; index < tracked_variables.length; index++) variable_masks.set(tracked_variables[index], 1 << index);
|
|
349
|
+
return {
|
|
350
|
+
variable_masks,
|
|
351
|
+
tracked_variables
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function compute_expression_mask(expression, context) {
|
|
355
|
+
if (context.tracked_variables.length === 0) return -1;
|
|
356
|
+
let mask = 0;
|
|
357
|
+
for (const ref of expression.refs) {
|
|
358
|
+
const bit = context.variable_masks.get(ref);
|
|
359
|
+
if (bit !== void 0) mask |= bit;
|
|
360
|
+
}
|
|
361
|
+
return mask;
|
|
362
|
+
}
|
|
363
|
+
// src/stillflow-vite/vm-compiler/pass.analyze-dirty.ts
|
|
364
|
+
function analyze_dirty(node, dirty) {
|
|
365
|
+
switch (node.kind) {
|
|
366
|
+
case "Expression":
|
|
367
|
+
node.meta.analysis.dirty_mask = compute_expression_mask(node, dirty);
|
|
368
|
+
return node.meta.analysis.dirty_mask;
|
|
369
|
+
case "Text":
|
|
370
|
+
if (typeof node.value !== "string") {
|
|
371
|
+
const mask = analyze_dirty(node.value, dirty);
|
|
372
|
+
node.meta.analysis.dirty_mask = mask;
|
|
373
|
+
} else node.meta.analysis.dirty_mask = 0;
|
|
374
|
+
return node.meta.analysis.dirty_mask;
|
|
375
|
+
case "Element": {
|
|
376
|
+
let dirty_mask = 0;
|
|
377
|
+
for (const [key, expression] of Object.entries(node.dynamic_properties)) {
|
|
378
|
+
const property_mask = analyze_dirty(expression, dirty);
|
|
379
|
+
node.dynamic_properties_mask[key] = property_mask;
|
|
380
|
+
dirty_mask |= property_mask;
|
|
381
|
+
}
|
|
382
|
+
for (const [key, { handler, data }] of Object.entries(node.delegated_events)) {
|
|
383
|
+
const handler_mask = analyze_dirty(handler, dirty);
|
|
384
|
+
const data_mask = analyze_dirty(data, dirty);
|
|
385
|
+
node.delegated_events_mask[key] = {
|
|
386
|
+
handler: handler_mask,
|
|
387
|
+
data: data_mask
|
|
388
|
+
};
|
|
389
|
+
dirty_mask |= handler_mask | data_mask;
|
|
390
|
+
}
|
|
391
|
+
for (const child of node.children) dirty_mask |= analyze_dirty(child, dirty);
|
|
392
|
+
node.meta.analysis.dirty_mask = dirty_mask;
|
|
393
|
+
return dirty_mask;
|
|
394
|
+
}
|
|
395
|
+
case "For": {
|
|
396
|
+
let outer_mask = analyze_dirty(node.select, dirty);
|
|
397
|
+
const inner_dirty = create_dirty_context(["$it", ...node.meta.scope.captures]);
|
|
398
|
+
node.dirty_context = inner_dirty;
|
|
399
|
+
for (const child of node.body) analyze_dirty(child, inner_dirty);
|
|
400
|
+
for (const capture of node.meta.scope.captures) {
|
|
401
|
+
const bit = dirty.variable_masks.get(capture);
|
|
402
|
+
if (bit !== void 0) outer_mask |= bit;
|
|
403
|
+
}
|
|
404
|
+
node.meta.analysis.dirty_mask = outer_mask;
|
|
405
|
+
return outer_mask;
|
|
406
|
+
}
|
|
407
|
+
case "Call": {
|
|
408
|
+
let dirty_mask = 0;
|
|
409
|
+
for (const expression of Object.values(node.properties)) dirty_mask |= analyze_dirty(expression, dirty);
|
|
410
|
+
const it_bit = dirty.variable_masks.get("$it");
|
|
411
|
+
if (it_bit !== void 0) dirty_mask |= it_bit;
|
|
412
|
+
node.meta.analysis.dirty_mask = dirty_mask;
|
|
413
|
+
return dirty_mask;
|
|
414
|
+
}
|
|
415
|
+
case "If": {
|
|
416
|
+
let dirty_mask = 0;
|
|
417
|
+
for (const branch of node.branches) {
|
|
418
|
+
if (branch.condition !== void 0) dirty_mask |= analyze_dirty(branch.condition, dirty);
|
|
419
|
+
for (const child of branch.children) dirty_mask |= analyze_dirty(child, dirty);
|
|
420
|
+
}
|
|
421
|
+
node.meta.analysis.dirty_mask = dirty_mask;
|
|
422
|
+
return dirty_mask;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
// src/stillflow-vite/vm-compiler/pass.analyze-dynamic.ts
|
|
427
|
+
function analyze_tree(node) {
|
|
428
|
+
switch (node.kind) {
|
|
429
|
+
case "Expression":
|
|
430
|
+
node.meta.analysis = {
|
|
431
|
+
is_dynamic: true,
|
|
432
|
+
dirty_mask: 0,
|
|
433
|
+
has_child_chunks: false
|
|
434
|
+
};
|
|
435
|
+
return node.meta.analysis;
|
|
436
|
+
case "Text":
|
|
437
|
+
node.meta.analysis = {
|
|
438
|
+
is_dynamic: node.is_dynamic,
|
|
439
|
+
dirty_mask: 0,
|
|
440
|
+
has_child_chunks: false
|
|
441
|
+
};
|
|
442
|
+
return node.meta.analysis;
|
|
443
|
+
case "Element": {
|
|
444
|
+
const child_results = node.children.map(analyze_tree);
|
|
445
|
+
const has_dynamic_props = Object.keys(node.dynamic_properties).length > 0;
|
|
446
|
+
const has_dynamic_child = child_results.some((r) => r.is_dynamic);
|
|
447
|
+
const has_child_chunks = child_results.some((r) => r.has_child_chunks);
|
|
448
|
+
node.meta.analysis = {
|
|
449
|
+
is_dynamic: has_dynamic_props || has_dynamic_child || has_child_chunks,
|
|
450
|
+
dirty_mask: 0,
|
|
451
|
+
has_child_chunks
|
|
452
|
+
};
|
|
453
|
+
return node.meta.analysis;
|
|
454
|
+
}
|
|
455
|
+
case "For":
|
|
456
|
+
analyze_tree(node.select);
|
|
457
|
+
for (const child of node.body) analyze_tree(child);
|
|
458
|
+
node.meta.analysis = {
|
|
459
|
+
is_dynamic: true,
|
|
460
|
+
dirty_mask: 0,
|
|
461
|
+
has_child_chunks: true
|
|
462
|
+
};
|
|
463
|
+
return node.meta.analysis;
|
|
464
|
+
case "Call":
|
|
465
|
+
node.meta.analysis = {
|
|
466
|
+
is_dynamic: true,
|
|
467
|
+
dirty_mask: 0,
|
|
468
|
+
has_child_chunks: true
|
|
469
|
+
};
|
|
470
|
+
return node.meta.analysis;
|
|
471
|
+
case "If":
|
|
472
|
+
for (const branch of node.branches) {
|
|
473
|
+
if (branch.condition !== void 0) branch.condition.meta.analysis = {
|
|
474
|
+
is_dynamic: true,
|
|
475
|
+
has_child_chunks: true,
|
|
476
|
+
dirty_mask: 0
|
|
477
|
+
};
|
|
478
|
+
for (const child of branch.children) child.meta.analysis = {
|
|
479
|
+
is_dynamic: true,
|
|
480
|
+
has_child_chunks: true,
|
|
481
|
+
dirty_mask: 0
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
node.meta.analysis = {
|
|
485
|
+
is_dynamic: true,
|
|
486
|
+
has_child_chunks: true,
|
|
487
|
+
dirty_mask: 0
|
|
488
|
+
};
|
|
489
|
+
return node.meta.analysis;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
// src/stillflow-vite/vm-compiler/bytecode-builder.ts
|
|
493
|
+
var Interner = class {
|
|
494
|
+
_pool = /* @__PURE__ */ new Map();
|
|
495
|
+
_table = [];
|
|
496
|
+
intern(value) {
|
|
497
|
+
let id = this._pool.get(value);
|
|
498
|
+
if (id === void 0) {
|
|
499
|
+
id = as_intern(this._table.length);
|
|
500
|
+
this._table.push(value);
|
|
501
|
+
this._pool.set(value, id);
|
|
502
|
+
}
|
|
503
|
+
return id;
|
|
504
|
+
}
|
|
505
|
+
get(id) {
|
|
506
|
+
return this._table[id];
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
var BytecodeBuilder = class {
|
|
510
|
+
chunks = [];
|
|
511
|
+
interner = new Interner();
|
|
512
|
+
component_chunks = /* @__PURE__ */ new Map();
|
|
513
|
+
is_controller_slot = [];
|
|
514
|
+
root_chunk_id = as_chunk(0);
|
|
515
|
+
_active;
|
|
516
|
+
_slots = 0;
|
|
517
|
+
allocate_slot() {
|
|
518
|
+
return as_slot(this._slots++);
|
|
519
|
+
}
|
|
520
|
+
allocate_chunk() {
|
|
521
|
+
const id = as_chunk(this.chunks.length);
|
|
522
|
+
this.chunks.push({
|
|
523
|
+
id,
|
|
524
|
+
mount: [],
|
|
525
|
+
patch: [],
|
|
526
|
+
captures: [],
|
|
527
|
+
root_slots: []
|
|
528
|
+
});
|
|
529
|
+
return id;
|
|
530
|
+
}
|
|
531
|
+
allocate_controller_slot() {
|
|
532
|
+
const id = this.allocate_slot();
|
|
533
|
+
this.is_controller_slot[id] = true;
|
|
534
|
+
return id;
|
|
535
|
+
}
|
|
536
|
+
intern(value) {
|
|
537
|
+
return this.interner.intern(value);
|
|
538
|
+
}
|
|
539
|
+
emit_mount(opcode, ...ops) {
|
|
540
|
+
this._active.mount.push(opcode, ...ops);
|
|
541
|
+
}
|
|
542
|
+
emit_patch(opcode, ...ops) {
|
|
543
|
+
this._active.patch.push(opcode, ...ops);
|
|
544
|
+
}
|
|
545
|
+
with_chunk(captures, site) {
|
|
546
|
+
const chunk = {
|
|
547
|
+
id: as_chunk(this.chunks.length),
|
|
548
|
+
root_slots: [],
|
|
549
|
+
mount: [],
|
|
550
|
+
patch: [],
|
|
551
|
+
captures
|
|
552
|
+
};
|
|
553
|
+
const previous = this._active;
|
|
554
|
+
this.chunks.push(chunk);
|
|
555
|
+
this._active = chunk;
|
|
556
|
+
site();
|
|
557
|
+
this._active = previous;
|
|
558
|
+
return chunk;
|
|
559
|
+
}
|
|
560
|
+
fill_chunk(id, captures, site) {
|
|
561
|
+
const chunk = this.chunks[id];
|
|
562
|
+
if (chunk === void 0) throw new Error(`Chunk #${id} was not allocated`);
|
|
563
|
+
Object.assign(chunk, { captures });
|
|
564
|
+
const previous = this._active;
|
|
565
|
+
this._active = chunk;
|
|
566
|
+
chunk.root_slots = site();
|
|
567
|
+
this._active = previous;
|
|
568
|
+
return chunk;
|
|
569
|
+
}
|
|
570
|
+
compile_subtree(captures, emit_root) {
|
|
571
|
+
const chunk = {
|
|
572
|
+
id: as_chunk(this.chunks.length),
|
|
573
|
+
root_slots: [],
|
|
574
|
+
mount: [],
|
|
575
|
+
patch: [],
|
|
576
|
+
captures
|
|
577
|
+
};
|
|
578
|
+
const previous = this._active;
|
|
579
|
+
this.chunks.push(chunk);
|
|
580
|
+
this._active = chunk;
|
|
581
|
+
chunk.root_slots = emit_root();
|
|
582
|
+
this._active = previous;
|
|
583
|
+
return chunk;
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
// src/stillflow-vite/vm-compiler/codes.ts
|
|
587
|
+
var Opcode = /* @__PURE__ */ function(Opcode) {
|
|
588
|
+
Opcode[Opcode["CREATE_ELEMENT"] = 0] = "CREATE_ELEMENT";
|
|
589
|
+
Opcode[Opcode["SET_STATIC_PROP"] = 1] = "SET_STATIC_PROP";
|
|
590
|
+
Opcode[Opcode["SET_DYNAMIC_PROP"] = 2] = "SET_DYNAMIC_PROP";
|
|
591
|
+
Opcode[Opcode["UPDATE_DYNAMIC_PROP"] = 3] = "UPDATE_DYNAMIC_PROP";
|
|
592
|
+
Opcode[Opcode["CREATE_STATIC_TEXT"] = 4] = "CREATE_STATIC_TEXT";
|
|
593
|
+
Opcode[Opcode["CREATE_DYNAMIC_TEXT"] = 5] = "CREATE_DYNAMIC_TEXT";
|
|
594
|
+
Opcode[Opcode["UPDATE_DYNAMIC_TEXT"] = 6] = "UPDATE_DYNAMIC_TEXT";
|
|
595
|
+
Opcode[Opcode["APPEND"] = 7] = "APPEND";
|
|
596
|
+
Opcode[Opcode["READ_EXPRESSION"] = 8] = "READ_EXPRESSION";
|
|
597
|
+
Opcode[Opcode["FOR"] = 9] = "FOR";
|
|
598
|
+
Opcode[Opcode["MOUNT_CALL"] = 10] = "MOUNT_CALL";
|
|
599
|
+
Opcode[Opcode["PATCH_CALL"] = 11] = "PATCH_CALL";
|
|
600
|
+
Opcode[Opcode["CREATE_ANCHOR"] = 12] = "CREATE_ANCHOR";
|
|
601
|
+
Opcode[Opcode["MOUNT_IF"] = 13] = "MOUNT_IF";
|
|
602
|
+
Opcode[Opcode["PATCH_IF"] = 14] = "PATCH_IF";
|
|
603
|
+
Opcode[Opcode["READ_PROP"] = 15] = "READ_PROP";
|
|
604
|
+
Opcode[Opcode["SET_EVENT"] = 16] = "SET_EVENT";
|
|
605
|
+
Opcode[Opcode["UPDATE_EVENT_DATA"] = 17] = "UPDATE_EVENT_DATA";
|
|
606
|
+
return Opcode;
|
|
607
|
+
}({});
|
|
608
|
+
// src/stillflow-vite/vm-compiler/pass.lowering.ts
|
|
609
|
+
function lower_to_bytecode(transformed, scope) {
|
|
610
|
+
const builder = new BytecodeBuilder();
|
|
611
|
+
for (const name of transformed.components.keys()) {
|
|
612
|
+
const id = builder.allocate_chunk();
|
|
613
|
+
builder.component_chunks.set(name, id);
|
|
614
|
+
}
|
|
615
|
+
resolve_scopes(transformed.root, scope);
|
|
616
|
+
analyze_tree(transformed.root);
|
|
617
|
+
const root_dirty = create_dirty_context([]);
|
|
618
|
+
analyze_dirty(transformed.root, root_dirty);
|
|
619
|
+
builder.root_chunk_id = builder.compile_subtree(root_dirty.tracked_variables, () => lower_node(transformed.root, builder, void 0)).id;
|
|
620
|
+
for (const [name, component] of transformed.components) {
|
|
621
|
+
const component_chunk_id = builder.component_chunks.get(name);
|
|
622
|
+
const component_scope = new SymbolScope(scope);
|
|
623
|
+
const params = [...component.params.ids, ...Object.keys(component.params.defaults)];
|
|
624
|
+
for (const parameter of params) component_scope.declare(parameter);
|
|
625
|
+
resolve_scopes(component.body, component_scope);
|
|
626
|
+
analyze_tree(component.body);
|
|
627
|
+
const params_dirty = create_dirty_context(params);
|
|
628
|
+
analyze_dirty(component.body, params_dirty);
|
|
629
|
+
builder.fill_chunk(component_chunk_id, params_dirty.tracked_variables, () => {
|
|
630
|
+
for (const [key, code] of Object.entries(component.params.defaults)) {
|
|
631
|
+
const key_id = builder.intern(key);
|
|
632
|
+
const default_id = builder.intern(code);
|
|
633
|
+
builder.emit_mount(Opcode.READ_PROP, key_id, default_id);
|
|
634
|
+
}
|
|
635
|
+
return lower_node(component.body, builder, void 0);
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
return builder;
|
|
639
|
+
}
|
|
640
|
+
function lower_node(node, builder, parent_slot) {
|
|
641
|
+
switch (node.kind) {
|
|
642
|
+
case "Element": {
|
|
643
|
+
const element_slot = builder.allocate_slot();
|
|
644
|
+
node.meta.analysis.slot = element_slot;
|
|
645
|
+
const tag_id = builder.intern(node.tag);
|
|
646
|
+
builder.emit_mount(Opcode.CREATE_ELEMENT, tag_id, element_slot);
|
|
647
|
+
for (const [key, value] of Object.entries(node.static_properties)) builder.emit_mount(Opcode.SET_STATIC_PROP, element_slot, builder.intern(key), builder.intern(value));
|
|
648
|
+
for (const [key, expression] of Object.entries(node.dynamic_properties)) {
|
|
649
|
+
const key_id = builder.intern(key);
|
|
650
|
+
const expression_id = builder.intern(expression.code);
|
|
651
|
+
const property_mask = node.dynamic_properties_mask[key];
|
|
652
|
+
builder.emit_mount(Opcode.SET_DYNAMIC_PROP, element_slot, key_id, expression_id);
|
|
653
|
+
if (property_mask !== 0) builder.emit_patch(Opcode.UPDATE_DYNAMIC_PROP, element_slot, key_id, expression_id, property_mask);
|
|
654
|
+
}
|
|
655
|
+
for (const [key, { handler, data }] of Object.entries(node.delegated_events)) {
|
|
656
|
+
const key_id = builder.intern(key);
|
|
657
|
+
const data_id = builder.intern(data.code);
|
|
658
|
+
const handler_id = builder.intern(handler.code);
|
|
659
|
+
const property_mask = node.delegated_events_mask[key];
|
|
660
|
+
builder.emit_mount(Opcode.SET_EVENT, element_slot, key_id, handler_id, data_id);
|
|
661
|
+
if (data_id !== 0 || handler_id !== 0) builder.emit_patch(Opcode.UPDATE_EVENT_DATA, element_slot, key_id, data_id, property_mask.data);
|
|
662
|
+
}
|
|
663
|
+
if (parent_slot !== void 0) builder.emit_mount(Opcode.APPEND, parent_slot, element_slot);
|
|
664
|
+
for (const child of node.children) lower_node(child, builder, element_slot);
|
|
665
|
+
return [element_slot];
|
|
666
|
+
}
|
|
667
|
+
case "Text": {
|
|
668
|
+
const text_slot = builder.allocate_slot();
|
|
669
|
+
node.meta.analysis.slot = text_slot;
|
|
670
|
+
if (typeof node.value !== "string") {
|
|
671
|
+
const expression_id = builder.intern(node.value.code);
|
|
672
|
+
const text_mask = node.meta.analysis.dirty_mask;
|
|
673
|
+
builder.emit_mount(Opcode.CREATE_DYNAMIC_TEXT, expression_id, text_slot);
|
|
674
|
+
if (text_mask !== 0) builder.emit_patch(Opcode.UPDATE_DYNAMIC_TEXT, expression_id, text_slot, text_mask);
|
|
675
|
+
} else {
|
|
676
|
+
const string_id = builder.intern(node.value);
|
|
677
|
+
builder.emit_mount(Opcode.CREATE_STATIC_TEXT, string_id, text_slot);
|
|
678
|
+
}
|
|
679
|
+
if (parent_slot !== void 0) builder.emit_mount(Opcode.APPEND, parent_slot, text_slot);
|
|
680
|
+
return [text_slot];
|
|
681
|
+
}
|
|
682
|
+
case "For": {
|
|
683
|
+
if (parent_slot === void 0) throw new Error(`"For"-node must have a parent container "Element"-node`);
|
|
684
|
+
const captures = node.dirty_context.tracked_variables;
|
|
685
|
+
const select_chunk = builder.with_chunk(captures, () => builder.emit_mount(Opcode.READ_EXPRESSION, builder.intern(node.select.code)));
|
|
686
|
+
const body_chunk = builder.compile_subtree(captures, () => lower_node(node.body[0], builder, void 0));
|
|
687
|
+
if (body_chunk.root_slots.length > 1) throw new Error(`For-node should not contain more than one child`);
|
|
688
|
+
const root_item_slot = body_chunk.root_slots[0];
|
|
689
|
+
const capture_ids = captures.map((name) => builder.intern(name));
|
|
690
|
+
const for_mask = node.meta.analysis.dirty_mask;
|
|
691
|
+
const instructions = [
|
|
692
|
+
body_chunk.id,
|
|
693
|
+
select_chunk.id,
|
|
694
|
+
parent_slot,
|
|
695
|
+
root_item_slot,
|
|
696
|
+
for_mask,
|
|
697
|
+
capture_ids.length,
|
|
698
|
+
...capture_ids
|
|
699
|
+
];
|
|
700
|
+
builder.emit_mount(Opcode.FOR, ...instructions);
|
|
701
|
+
if (for_mask !== 0) builder.emit_patch(Opcode.FOR, ...instructions);
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
case "Expression":
|
|
705
|
+
builder.emit_mount(Opcode.READ_EXPRESSION, builder.intern(node.code));
|
|
706
|
+
return;
|
|
707
|
+
case "Call": {
|
|
708
|
+
const call_slot = builder.allocate_controller_slot();
|
|
709
|
+
node.meta.analysis.slot = call_slot;
|
|
710
|
+
const component_name_id = builder.intern(node.component_name);
|
|
711
|
+
const property_entries = Object.entries(node.properties);
|
|
712
|
+
const encoded_properties = [];
|
|
713
|
+
for (const [key, expression] of property_entries) encoded_properties.push(builder.intern(key), builder.intern(expression.code));
|
|
714
|
+
builder.emit_mount(Opcode.MOUNT_CALL, component_name_id, parent_slot ?? -1, call_slot, property_entries.length, ...encoded_properties);
|
|
715
|
+
const call_mask = node.meta.analysis.dirty_mask;
|
|
716
|
+
if (call_mask !== 0) builder.emit_patch(Opcode.PATCH_CALL, call_slot, call_mask, property_entries.length, ...encoded_properties);
|
|
717
|
+
return [call_slot];
|
|
718
|
+
}
|
|
719
|
+
case "If": {
|
|
720
|
+
if (parent_slot === void 0) throw new Error(`"If"-node must have a parent contaienr "Element"-node`);
|
|
721
|
+
const captures = Array.from(node.meta.scope.captures);
|
|
722
|
+
const anchor_slot = builder.allocate_slot();
|
|
723
|
+
builder.emit_mount(Opcode.CREATE_ANCHOR, anchor_slot);
|
|
724
|
+
builder.emit_mount(Opcode.APPEND, parent_slot, anchor_slot);
|
|
725
|
+
const encoded_bracnhes = [];
|
|
726
|
+
for (const branch of node.branches) {
|
|
727
|
+
const branch_chunk = builder.compile_subtree(captures, () => {
|
|
728
|
+
const slots = [];
|
|
729
|
+
for (const child of branch.children) for (const slot of lower_node(child, builder, void 0)) slots.push(slot);
|
|
730
|
+
return slots;
|
|
731
|
+
});
|
|
732
|
+
const condition_id = branch.condition !== void 0 ? builder.intern(branch.condition.code) : -1;
|
|
733
|
+
encoded_bracnhes.push(condition_id, branch_chunk.id);
|
|
734
|
+
}
|
|
735
|
+
const capture_ids = captures.map((name) => builder.intern(name));
|
|
736
|
+
const if_mask = node.meta.analysis.dirty_mask;
|
|
737
|
+
const instructions = [
|
|
738
|
+
anchor_slot,
|
|
739
|
+
node.branches.length,
|
|
740
|
+
...encoded_bracnhes,
|
|
741
|
+
capture_ids.length,
|
|
742
|
+
...capture_ids
|
|
743
|
+
];
|
|
744
|
+
builder.emit_mount(Opcode.MOUNT_IF, ...instructions);
|
|
745
|
+
if (if_mask !== 0) builder.emit_patch(Opcode.PATCH_IF, ...instructions, if_mask);
|
|
746
|
+
return [];
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
// src/stillflow-vite/dom.pass.codegen.ts
|
|
751
|
+
function emit_dirty_prologue(tracked_variables, source) {
|
|
752
|
+
if (tracked_variables.length === 0) return [];
|
|
753
|
+
const lines = ["let __dirty = 0;"];
|
|
754
|
+
for (let index = 0; index < tracked_variables.length; index++) {
|
|
755
|
+
const name = tracked_variables[index];
|
|
756
|
+
const bit = 1 << index;
|
|
757
|
+
lines.push(source(name, bit));
|
|
758
|
+
}
|
|
759
|
+
lines.push("if (__dirty === 0) return;");
|
|
760
|
+
return lines;
|
|
761
|
+
}
|
|
762
|
+
function generate_dom(builder) {
|
|
763
|
+
const module_code = [];
|
|
764
|
+
const emitted_for_chunks = /* @__PURE__ */ new Set();
|
|
765
|
+
const emitted_component_chunks = /* @__PURE__ */ new Set();
|
|
766
|
+
function wrap_dirty(expression, mask) {
|
|
767
|
+
if (mask === -1) return expression;
|
|
768
|
+
return `if (__dirty & ${mask}) ${expression}`;
|
|
769
|
+
}
|
|
770
|
+
function dom_ref(slot) {
|
|
771
|
+
return builder.is_controller_slot[slot] ? `__s${slot}.element` : `__s${slot}`;
|
|
772
|
+
}
|
|
773
|
+
function emit_stream(stream, interner) {
|
|
774
|
+
const lines = [];
|
|
775
|
+
let pointer = 0;
|
|
776
|
+
while (pointer < stream.length) {
|
|
777
|
+
const opcode = stream[pointer++];
|
|
778
|
+
switch (require_vm_instruction.unpack_local(opcode)) {
|
|
779
|
+
case Opcode.CREATE_ELEMENT: {
|
|
780
|
+
const tag = interner.get(stream[pointer++]);
|
|
781
|
+
const slot = stream[pointer++];
|
|
782
|
+
lines.push(`const __s${slot} = document.createElement(${JSON.stringify(tag)});`);
|
|
783
|
+
break;
|
|
784
|
+
}
|
|
785
|
+
case Opcode.SET_STATIC_PROP: {
|
|
786
|
+
const slot = stream[pointer++];
|
|
787
|
+
const key = interner.get(stream[pointer++]);
|
|
788
|
+
const value = interner.get(stream[pointer++]);
|
|
789
|
+
lines.push(`__s${slot}.setAttribute(${JSON.stringify(key)}, ${JSON.stringify(value)});`);
|
|
790
|
+
break;
|
|
791
|
+
}
|
|
792
|
+
case Opcode.SET_DYNAMIC_PROP: {
|
|
793
|
+
const slot = stream[pointer++];
|
|
794
|
+
const key = interner.get(stream[pointer++]);
|
|
795
|
+
const expression = interner.get(stream[pointer++]);
|
|
796
|
+
lines.push(`__s${slot}.${key} = ${expression};`);
|
|
797
|
+
break;
|
|
798
|
+
}
|
|
799
|
+
case Opcode.UPDATE_DYNAMIC_PROP: {
|
|
800
|
+
const slot = stream[pointer++];
|
|
801
|
+
const key = interner.get(stream[pointer++]);
|
|
802
|
+
const expression = interner.get(stream[pointer++]);
|
|
803
|
+
const property_mask = stream[pointer++];
|
|
804
|
+
lines.push(wrap_dirty(`__s${slot}.${key} = ${expression};`, property_mask));
|
|
805
|
+
break;
|
|
806
|
+
}
|
|
807
|
+
case Opcode.CREATE_STATIC_TEXT: {
|
|
808
|
+
const text = interner.get(stream[pointer++]);
|
|
809
|
+
const slot = stream[pointer++];
|
|
810
|
+
lines.push(`const __s${slot} = document.createTextNode(${JSON.stringify(text)});`);
|
|
811
|
+
break;
|
|
812
|
+
}
|
|
813
|
+
case Opcode.CREATE_DYNAMIC_TEXT: {
|
|
814
|
+
const expression = interner.get(stream[pointer++]);
|
|
815
|
+
const slot = stream[pointer++];
|
|
816
|
+
lines.push(`const __s${slot} = document.createTextNode(${expression});`);
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
case Opcode.UPDATE_DYNAMIC_TEXT: {
|
|
820
|
+
const expression = interner.get(stream[pointer++]);
|
|
821
|
+
const slot = stream[pointer++];
|
|
822
|
+
const mask = stream[pointer++];
|
|
823
|
+
lines.push(wrap_dirty(`__s${slot}.data = ${expression};`, mask));
|
|
824
|
+
break;
|
|
825
|
+
}
|
|
826
|
+
case Opcode.APPEND: {
|
|
827
|
+
const parent = stream[pointer++];
|
|
828
|
+
const child = stream[pointer++];
|
|
829
|
+
lines.push(`__s${parent}.appendChild(__s${child});`);
|
|
830
|
+
break;
|
|
831
|
+
}
|
|
832
|
+
case Opcode.READ_EXPRESSION: {
|
|
833
|
+
const expression = interner.get(stream[pointer++]);
|
|
834
|
+
lines.push(expression);
|
|
835
|
+
break;
|
|
836
|
+
}
|
|
837
|
+
case Opcode.FOR: {
|
|
838
|
+
const body_chunk_id = stream[pointer++];
|
|
839
|
+
const select_chunk_id = stream[pointer++];
|
|
840
|
+
const container_slot = stream[pointer++];
|
|
841
|
+
const root_item_slot = stream[pointer++];
|
|
842
|
+
stream[pointer++];
|
|
843
|
+
const capture_count = stream[pointer++];
|
|
844
|
+
const captures = [];
|
|
845
|
+
for (let index = 0; index < capture_count; index++) captures.push(interner.get(stream[pointer++]));
|
|
846
|
+
if (!emitted_for_chunks.has(body_chunk_id)) {
|
|
847
|
+
emitted_for_chunks.add(body_chunk_id);
|
|
848
|
+
emit_for_function(builder.chunks[body_chunk_id], root_item_slot, interner);
|
|
849
|
+
}
|
|
850
|
+
const select_expression = emit_stream(builder.chunks[select_chunk_id].mount, interner)[0];
|
|
851
|
+
const args = [
|
|
852
|
+
`__s${container_slot}`,
|
|
853
|
+
select_expression,
|
|
854
|
+
...captures
|
|
855
|
+
].join(", ");
|
|
856
|
+
lines.push(`__for_${body_chunk_id}(${args});`);
|
|
857
|
+
break;
|
|
858
|
+
}
|
|
859
|
+
case Opcode.MOUNT_CALL: {
|
|
860
|
+
const component_name_id = stream[pointer++];
|
|
861
|
+
const parent_slot = stream[pointer++];
|
|
862
|
+
const call_slot = stream[pointer++];
|
|
863
|
+
const properties_count = stream[pointer++];
|
|
864
|
+
const property_pairs = [];
|
|
865
|
+
for (let index = 0; index < properties_count; index++) {
|
|
866
|
+
const key = interner.get(stream[pointer++]);
|
|
867
|
+
const expression = interner.get(stream[pointer++]);
|
|
868
|
+
property_pairs.push(`${JSON.stringify(key)}: ${expression}`);
|
|
869
|
+
}
|
|
870
|
+
const component_name = interner.get(component_name_id);
|
|
871
|
+
const chunk_id = builder.component_chunks.get(component_name);
|
|
872
|
+
if (!emitted_component_chunks.has(chunk_id)) {
|
|
873
|
+
emitted_component_chunks.add(chunk_id);
|
|
874
|
+
emit_component_function(builder.chunks[chunk_id], component_name, interner);
|
|
875
|
+
}
|
|
876
|
+
if (parent_slot !== -1) lines.push(`const __s${call_slot} = __comp_${component_name}(${dom_ref(parent_slot)}, { ${property_pairs.join(", ")} })`);
|
|
877
|
+
else lines.push(`const __s${call_slot} = __comp_${component_name}(null, { ${property_pairs.join(", ")} })`);
|
|
878
|
+
break;
|
|
879
|
+
}
|
|
880
|
+
case Opcode.PATCH_CALL: {
|
|
881
|
+
const call_slot = stream[pointer++];
|
|
882
|
+
const call_mask = stream[pointer++];
|
|
883
|
+
const properties_count = stream[pointer++];
|
|
884
|
+
const property_pairs = [];
|
|
885
|
+
for (let index = 0; index < properties_count; index++) {
|
|
886
|
+
const key = interner.get(stream[pointer++]);
|
|
887
|
+
const expression = interner.get(stream[pointer++]);
|
|
888
|
+
property_pairs.push(`${JSON.stringify(key)}: ${expression}`);
|
|
889
|
+
}
|
|
890
|
+
lines.push(wrap_dirty(`__s${call_slot}.patch({ ${property_pairs.join(", ")} })`, call_mask));
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
case Opcode.SET_EVENT: {
|
|
894
|
+
const slot = stream[pointer++];
|
|
895
|
+
const key = interner.get(stream[pointer++]);
|
|
896
|
+
const handler = interner.get(stream[pointer++]);
|
|
897
|
+
const data = interner.get(stream[pointer++]);
|
|
898
|
+
lines.push(`${dom_ref(slot)}.$$${key} = ${handler};`);
|
|
899
|
+
lines.push(`${dom_ref(slot)}.$$${key}_data = ${data};`);
|
|
900
|
+
lines.push(`delegate_event(${JSON.stringify(key)})`);
|
|
901
|
+
break;
|
|
902
|
+
}
|
|
903
|
+
case Opcode.UPDATE_EVENT_DATA: {
|
|
904
|
+
const slot = stream[pointer++];
|
|
905
|
+
const key = interner.get(stream[pointer++]);
|
|
906
|
+
const data = interner.get(stream[pointer++]);
|
|
907
|
+
const data_mask = stream[pointer++];
|
|
908
|
+
lines.push(wrap_dirty(`${dom_ref(slot)}.$$${key}_data = ${data};`, data_mask));
|
|
909
|
+
break;
|
|
910
|
+
}
|
|
911
|
+
case Opcode.CREATE_ANCHOR: {
|
|
912
|
+
const anchor_slot = stream[pointer++];
|
|
913
|
+
lines.push(`const __s${anchor_slot} = document.createComment("");`);
|
|
914
|
+
break;
|
|
915
|
+
}
|
|
916
|
+
case Opcode.PATCH_IF: {
|
|
917
|
+
const anchor_slot = stream[pointer++];
|
|
918
|
+
const branch_count = stream[pointer++];
|
|
919
|
+
const branches = [];
|
|
920
|
+
for (let index = 0; index < branch_count; index++) {
|
|
921
|
+
const condition_id = stream[pointer++];
|
|
922
|
+
const chunk_id = stream[pointer++];
|
|
923
|
+
branches.push({
|
|
924
|
+
condition_id,
|
|
925
|
+
chunk_id
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
const capture_count = stream[pointer++];
|
|
929
|
+
const captures_names = [];
|
|
930
|
+
for (let index = 0; index < capture_count; index++) captures_names.push(interner.get(stream[pointer++]));
|
|
931
|
+
const mask = stream[pointer++];
|
|
932
|
+
let active_expression = "-1";
|
|
933
|
+
for (let index = branches.length - 1; index >= 0; index--) {
|
|
934
|
+
const branch = branches[index];
|
|
935
|
+
if (branch.condition_id === -1) active_expression = `${index}`;
|
|
936
|
+
else active_expression = `(${interner.get(branch.condition_id)}) ? ${index} : ${active_expression}`;
|
|
937
|
+
}
|
|
938
|
+
const args = captures_names.join(", ");
|
|
939
|
+
const factories_code = `[${branches.map((branch) => `() => __if_${branch.chunk_id}(${args})`)}]`;
|
|
940
|
+
const call_code = `reconcile_if(__s${anchor_slot}, ${active_expression}, ${factories_code}, ${mask === -1 ? "-1" : "__dirty"})`;
|
|
941
|
+
lines.push(wrap_dirty(call_code, mask));
|
|
942
|
+
break;
|
|
943
|
+
}
|
|
944
|
+
case Opcode.MOUNT_IF: {
|
|
945
|
+
const anchor_slot = stream[pointer++];
|
|
946
|
+
const branch_count = stream[pointer++];
|
|
947
|
+
const branches = [];
|
|
948
|
+
for (let index = 0; index < branch_count; index++) {
|
|
949
|
+
const condition_id = stream[pointer++];
|
|
950
|
+
const chunk_id = stream[pointer++];
|
|
951
|
+
branches.push({
|
|
952
|
+
condition_id,
|
|
953
|
+
chunk_id
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
const capture_count = stream[pointer++];
|
|
957
|
+
const captures_names = [];
|
|
958
|
+
for (let index = 0; index < capture_count; index++) captures_names.push(interner.get(stream[pointer++]));
|
|
959
|
+
for (const branch of branches) emit_if_branch_function(builder.chunks[branch.chunk_id], interner);
|
|
960
|
+
let active_expression = "-1";
|
|
961
|
+
for (let index = branches.length - 1; index >= 0; index--) {
|
|
962
|
+
const branch = branches[index];
|
|
963
|
+
if (branch.condition_id === -1) active_expression = `${index}`;
|
|
964
|
+
else active_expression = `(${interner.get(branch.condition_id)}) ? ${index} : ${active_expression}`;
|
|
965
|
+
}
|
|
966
|
+
const args = captures_names.join(", ");
|
|
967
|
+
const factories_code = `[${branches.map((branch) => `() => __if_${branch.chunk_id}(${args})`)}]`;
|
|
968
|
+
const call_code = `reconcile_if(__s${anchor_slot}, ${active_expression}, ${factories_code}, -1)`;
|
|
969
|
+
lines.push(call_code);
|
|
970
|
+
break;
|
|
971
|
+
}
|
|
972
|
+
case Opcode.READ_PROP: {
|
|
973
|
+
const name = interner.get(stream[pointer++]);
|
|
974
|
+
const value = interner.get(stream[pointer++]);
|
|
975
|
+
lines.push(`let ${name} = $props.${name} === undefined ? (${value}) : $props.${name};`);
|
|
976
|
+
break;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
return lines;
|
|
981
|
+
}
|
|
982
|
+
function emit_for_function(body_chunk, root_item_slot, interner) {
|
|
983
|
+
const mount_body = emit_stream(body_chunk.mount, interner);
|
|
984
|
+
const patch_body = emit_stream(body_chunk.patch, interner);
|
|
985
|
+
const params = [
|
|
986
|
+
"$parent",
|
|
987
|
+
"$items",
|
|
988
|
+
...body_chunk.captures
|
|
989
|
+
].join(", ");
|
|
990
|
+
const capture_bindings = body_chunk.captures.length > 0 ? `let ${body_chunk.captures.map((capture) => `__cap_${capture} = ${capture}`).join(", ")};` : String();
|
|
991
|
+
const code = `
|
|
992
|
+
function __for_${body_chunk.id}(${params}) {
|
|
993
|
+
${capture_bindings}
|
|
994
|
+
reconcile_list($parent, $items, ($it, $index) => {
|
|
995
|
+
${mount_body.join("\n ")}
|
|
996
|
+
|
|
997
|
+
return {
|
|
998
|
+
element: ${dom_ref(root_item_slot)},
|
|
999
|
+
patch($next) {
|
|
1000
|
+
let __dirty = 0;
|
|
1001
|
+
|
|
1002
|
+
if ($next !== undefined) {
|
|
1003
|
+
$it = $next;
|
|
1004
|
+
__dirty |= 1;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
${patch_body.join("\n ")}
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
`.trim();
|
|
1013
|
+
module_code.push(code);
|
|
1014
|
+
}
|
|
1015
|
+
function emit_if_branch_function(chunk, interner) {
|
|
1016
|
+
const mount_body = emit_stream(chunk.mount, interner);
|
|
1017
|
+
const patch_body = emit_stream(chunk.patch, interner);
|
|
1018
|
+
const elements_array = `[${chunk.root_slots.map((slot) => dom_ref(slot)).join(", ")}]`;
|
|
1019
|
+
const params = chunk.captures.join(", ");
|
|
1020
|
+
const code = `
|
|
1021
|
+
function __if_${chunk.id}(${params}) {
|
|
1022
|
+
${mount_body.join("\n ")}
|
|
1023
|
+
return {
|
|
1024
|
+
elements: ${elements_array},
|
|
1025
|
+
patch(__dirty) {
|
|
1026
|
+
${patch_body.join("\n ")}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
`.trim();
|
|
1031
|
+
module_code.push(code);
|
|
1032
|
+
}
|
|
1033
|
+
function emit_component_function(target_chunk, component_name, interner) {
|
|
1034
|
+
if (target_chunk.root_slots.length > 1) throw new Error(`Chunk #${target_chunk.id} ("${component_name}") has multiple DOM roots`);
|
|
1035
|
+
const params = ["$parent", "$props"];
|
|
1036
|
+
const captures = target_chunk.captures.filter((name) => params.includes(name) === false);
|
|
1037
|
+
const mount_body = emit_stream(target_chunk.mount, interner);
|
|
1038
|
+
const patch_body = emit_stream(target_chunk.patch, interner);
|
|
1039
|
+
const has_properties = captures.length > 0;
|
|
1040
|
+
const properties_list = captures.join(", ");
|
|
1041
|
+
const properties_init = has_properties ? `let { ${properties_list} } = $props;` : String();
|
|
1042
|
+
const dirty_prologue = emit_dirty_prologue(target_chunk.captures, (name, bit) => `if (${name} !== $props.${name}) { ${name} = $props.${name}; __dirty |= ${bit}; }`);
|
|
1043
|
+
const code = `
|
|
1044
|
+
function __comp_${component_name}(${params.join(", ")}) {
|
|
1045
|
+
${properties_init}
|
|
1046
|
+
${mount_body.join("\n ")}
|
|
1047
|
+
|
|
1048
|
+
if ($parent !== null) {
|
|
1049
|
+
$parent.appendChild(${dom_ref(target_chunk.root_slots[0])});
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
return {
|
|
1053
|
+
element: ${dom_ref(target_chunk.root_slots[0])},
|
|
1054
|
+
patch($props) {
|
|
1055
|
+
${dirty_prologue.join("\n ")}
|
|
1056
|
+
${patch_body.join("\n ")}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
`.trim();
|
|
1061
|
+
module_code.push(code);
|
|
1062
|
+
}
|
|
1063
|
+
const root_chunk = builder.chunks[builder.root_chunk_id];
|
|
1064
|
+
return {
|
|
1065
|
+
module: module_code,
|
|
1066
|
+
mount: emit_stream(root_chunk.mount, builder.interner),
|
|
1067
|
+
patch: emit_stream(root_chunk.patch, builder.interner)
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
// src/stillflow-vite/dom.target.ts
|
|
1071
|
+
var target_dom = { main(node, components) {
|
|
1072
|
+
const lowered = lower_to_bytecode(transform(node, components), new SymbolScope());
|
|
1073
|
+
const generated = generate_dom(lowered);
|
|
1074
|
+
const module_code = _babel_core.template.statements.ast(generated.module.join("\n"));
|
|
1075
|
+
const mount_code = _babel_core.template.statements.ast(generated.mount.join("\n"));
|
|
1076
|
+
const patch_code = _babel_core.template.statements.ast(generated.patch.join("\n"));
|
|
1077
|
+
const root_slots = lowered.chunks[lowered.root_chunk_id].root_slots;
|
|
1078
|
+
for (const slot of root_slots) {
|
|
1079
|
+
const is_controller_slot = lowered.is_controller_slot[slot];
|
|
1080
|
+
mount_code.push(_babel_core.types.expressionStatement(_babel_core.types.callExpression(_babel_core.types.memberExpression(_babel_core.types.identifier("$parent"), _babel_core.types.identifier("appendChild")), [is_controller_slot ? _babel_core.types.memberExpression(_babel_core.types.identifier(`__s${slot}`), _babel_core.types.identifier(`element`)) : _babel_core.types.identifier(`__s${slot}`)])));
|
|
1081
|
+
}
|
|
1082
|
+
const patch_site = _babel_core.types.functionDeclaration(_babel_core.types.identifier("__patch"), [], _babel_core.types.blockStatement(patch_code));
|
|
1083
|
+
return {
|
|
1084
|
+
module: [_babel_core.types.importDeclaration([
|
|
1085
|
+
_babel_core.types.importSpecifier(_babel_core.types.identifier("reconcile_list"), _babel_core.types.identifier("reconcile_list")),
|
|
1086
|
+
_babel_core.types.importSpecifier(_babel_core.types.identifier("delegate_event"), _babel_core.types.identifier("delegate_event")),
|
|
1087
|
+
_babel_core.types.importSpecifier(_babel_core.types.identifier("reconcile_if"), _babel_core.types.identifier("reconcile_if"))
|
|
1088
|
+
], _babel_core.types.stringLiteral("stillflow.engine")), ...module_code],
|
|
1089
|
+
body: [
|
|
1090
|
+
...mount_code,
|
|
1091
|
+
patch_site,
|
|
1092
|
+
_babel_core.types.returnStatement(_babel_core.types.identifier("__patch"))
|
|
1093
|
+
]
|
|
1094
|
+
};
|
|
1095
|
+
} };
|
|
1096
|
+
exports.dom = target_dom;
|