|
21 | 21 | IRBinaryOp, |
22 | 22 | IRBooleanOp, |
23 | 23 | IRBreakStatement, |
| 24 | + IRPassStatement, |
24 | 25 | IRCallExpr, |
25 | 26 | IRCanvasBlock, |
26 | 27 | IRClassDecl, |
@@ -381,6 +382,17 @@ class ReactiveEngine { |
381 | 382 | return Boolean(value); |
382 | 383 | } |
383 | 384 |
|
| 385 | +function __ml_iterate(obj) { |
| 386 | + if (obj == null) return []; |
| 387 | + if (Array.isArray(obj) || obj instanceof Set || obj instanceof Map || typeof obj === 'string') { |
| 388 | + return obj; |
| 389 | + } |
| 390 | + if (typeof obj === 'object') { |
| 391 | + return Object.keys(obj); |
| 392 | + } |
| 393 | + return obj; |
| 394 | +} |
| 395 | +
|
384 | 396 | function __ml_add(container, item) { |
385 | 397 | if (container instanceof Set) { |
386 | 398 | container.add(item); |
@@ -629,6 +641,8 @@ def _stmt_to_js(self, stmt: IRNode, indent: int) -> str: |
629 | 641 | return f"{pad}break;" |
630 | 642 | if isinstance(stmt, IRContinueStatement): |
631 | 643 | return f"{pad}continue;" |
| 644 | + if isinstance(stmt, IRPassStatement): |
| 645 | + return "" |
632 | 646 | if isinstance(stmt, IRImportStatement): |
633 | 647 | return "" |
634 | 648 | if isinstance(stmt, IRDelStatement): |
@@ -751,7 +765,7 @@ def _for_to_js(self, node: IRForLoop, indent: int) -> str: |
751 | 765 | lines.append(f"{pad}}}") |
752 | 766 | return "\n".join(lines) |
753 | 767 | iterable_js = self._expr_to_js(iterable) |
754 | | - lines = [f"{pad}for (const {target} of {iterable_js}) {{"] |
| 768 | + lines = [f"{pad}for (const {target} of __ml_iterate({iterable_js})) {{"] |
755 | 769 | lines.extend(self._stmt_to_js(stmt, indent + 1) for stmt in (node.body or [])) |
756 | 770 | lines.append(f"{pad}}}") |
757 | 771 | return "\n".join(lines) |
@@ -884,7 +898,7 @@ def _for_render_to_js(self, node: IRForLoop, parent_var: str, indent: int) -> st |
884 | 898 | ) |
885 | 899 | else: |
886 | 900 | iterable_js = self._expr_to_js(iterable) |
887 | | - lines.append(f"{pad}for (const {target} of {iterable_js}) {{") |
| 901 | + lines.append(f"{pad}for (const {target} of __ml_iterate({iterable_js})) {{") |
888 | 902 | for stmt in (node.body or []): |
889 | 903 | lines.extend(self._render_child(stmt, parent_var, indent + 1)) |
890 | 904 | lines.append(f"{pad}}}") |
|
0 commit comments