changeset 2129:e0843b2a636d

Make flattening of reactive HTML trees iterative instead of recursive, to avoid stack overflows
author Adam Chlipala <adam@chlipala.net>
date Sun, 08 Mar 2015 11:12:28 -0400
parents 248e2b32954b
children e8863d9f8c59 e10881cd92da
files lib/js/urweb.js
diffstat 1 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/lib/js/urweb.js	Sun Mar 08 11:05:54 2015 -0400
+++ b/lib/js/urweb.js	Sun Mar 08 11:12:28 2015 -0400
@@ -636,21 +636,25 @@
     return closures[n];
 }
 
-function flattenAcc(a, cls, tr) {
-    if (tr.cat1 != null) {
-        flattenAcc(a, cls, tr.cat1);
-        flattenAcc(a, cls, tr.cat2);
-    } else if (tr.closure != null) {
-        var cl = newClosure(tr.closure);
-        cls.v = cons(cl, cls.v);
-        a.push("cr(", cl.toString(), ")");
-    } else
-        a.push(tr);
+function flattenAcc(a, cls, trs) {
+    while (trs) {
+        var tr = trs.data;
+        trs = trs.next;
+
+        if (tr.cat1 != null) {
+            trs = cons(tr.cat1, cons(tr.cat2, trs));
+        } else if (tr.closure != null) {
+            var cl = newClosure(tr.closure);
+            cls.v = cons(cl, cls.v);
+            a.push("cr(", cl.toString(), ")");
+        } else
+            a.push(tr);
+    }
 }
 
 function flatten(cls, tr) {
     var a = [];
-    flattenAcc(a, cls, tr);
+    flattenAcc(a, cls, cons(tr, null));
     return a.join("");
 }